Pixel-Composer/scripts/panel_preview_window/panel_preview_window.gml

177 lines
4.8 KiB
Text
Raw Normal View History

2024-08-09 13:30:09 +02:00
#macro CHECK_PANEL_PREVIEW_WINDOW if(!is_instanceof(FOCUS_CONTENT, Panel_Preview_Window)) return;
function panel_preview_window_reset() { CHECK_PANEL_PREVIEW_WINDOW CALL("preview_window_reset"); FOCUS_CONTENT.reset(); }
function panel_preview_window_inspect() { CHECK_PANEL_PREVIEW_WINDOW CALL("preview_window_inspect"); PANEL_GRAPH.nodes_selecting = [ FOCUS_CONTENT.node_target ]; }
function panel_preview_window_preview() { CHECK_PANEL_PREVIEW_WINDOW CALL("preview_window_preview"); PANEL_PREVIEW.setNodePreview(FOCUS_CONTENT.node_target); }
2024-08-10 07:30:41 +02:00
function __fnInit_Preview_Window() {
registerFunction("Preview Window", "Reset view", "", MOD_KEY.none, panel_preview_window_reset ).setMenu("preview_window_reset_view")
registerFunction("Preview Window", "Inspect", "", MOD_KEY.none, panel_preview_window_inspect ).setMenu("preview_window_inspect")
registerFunction("Preview Window", "Preview", "", MOD_KEY.none, panel_preview_window_preview ).setMenu("preview_window_preview")
}
2023-03-12 02:28:21 +01:00
function Panel_Preview_Window() : PanelContent() constructor {
2024-08-09 13:30:09 +02:00
min_w = ui(64);
min_h = ui(64);
2023-03-12 02:28:21 +01:00
padding = 8;
2023-03-23 13:38:50 +01:00
title_height = 24;
2023-03-12 02:28:21 +01:00
w = ui(200);
h = ui(200);
node_target = noone;
preview_channel = 0;
title_show = 0;
scale = 0;
2023-06-01 19:50:53 +02:00
scale_levels = [ 1/32, 1/24, 1/16, 1/12, 1/8, 1/4, 1/3, 1/2, 2/3, 1, 1.5, 2, 3, 4, 6, 8, 12, 16, 24, 32];
2023-03-12 02:28:21 +01:00
panx = 0;
pany = 0;
panning = false;
pan_mx = 0;
pan_my = 0;
pan_sx = 0;
pan_sy = 0;
2023-06-01 19:50:53 +02:00
2023-03-12 02:28:21 +01:00
function surfaceCheck() {
content_surface = surface_verify(content_surface, w, h);
}
function reset() {
scale = 0;
panx = 0;
pany = 0;
2023-03-12 02:28:21 +01:00
}
2024-08-18 13:13:40 +02:00
function changeChannel(_index) {
var channel = 0;
2024-08-08 06:57:51 +02:00
for( var i = 0; i < array_length(node_target.outputs); i++ ) {
var o = node_target.outputs[i];
2023-03-12 02:28:21 +01:00
if(o.type != VALUE_TYPE.surface) continue;
2024-08-18 13:13:40 +02:00
if(channel++ == _index)
2023-03-12 02:28:21 +01:00
preview_channel = i;
}
}
content_surface = noone;
surfaceCheck();
menu = [
2024-08-10 07:30:41 +02:00
MENU_ITEMS.preview_window_reset_view,
2023-03-12 02:28:21 +01:00
-1,
2024-08-10 07:30:41 +02:00
MENU_ITEMS.preview_window_inspect,
MENU_ITEMS.preview_window_preview,
2023-03-12 02:28:21 +01:00
-1,
]
function drawContent(panel) {
if(node_target == noone) return;
title = node_target.getFullName();
surfaceCheck();
surface_set_target(content_surface);
draw_clear(COLORS.panel_bg_clear);
2023-06-13 14:42:06 +02:00
draw_sprite_tiled_ext(s_transparent, 0, 0, 0, 1, 1, COLORS.panel_preview_transparent, 1);
2023-03-12 02:28:21 +01:00
var surf = node_target.getPreviewValues();
surf = is_array(surf)? array_spread(surf) : [ surf ];
2023-03-12 02:28:21 +01:00
var dx = 0;
var dy = 0;
var ind = 0;
var col = round(sqrt(array_length(surf)));
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(surf); i < n; i++ ) {
2023-03-12 02:28:21 +01:00
var s = surf[i];
2023-09-08 21:37:36 +02:00
var sw = surface_get_width_safe(s);
var sh = surface_get_height_safe(s);
2023-03-12 02:28:21 +01:00
if(scale == 0)
2023-03-25 12:27:04 +01:00
scale = min(w / sw, h / sh);
var sx = dx + w / 2 - (sw * scale) / 2 + panx;
var sy = dy + h / 2 - (sh * scale) / 2 + pany;
2023-03-12 02:28:21 +01:00
2023-03-19 09:17:39 +01:00
draw_surface_ext_safe(s, sx, sy, scale, scale, 0, c_white, 1);
2023-03-12 02:28:21 +01:00
draw_set_color(COLORS._main_icon);
draw_rectangle(sx, sy, sx + sw * scale, sy + sh * scale, true);
if(++ind >= col) {
ind = 0;
dx = 0;
dy += (sh + 2) * scale;
} else
dx += (sw + 2) * scale;
}
surface_reset_target();
draw_surface_safe(content_surface);
2023-03-12 02:28:21 +01:00
if(panning) {
panx = pan_sx + (mouse_mx - pan_mx);
pany = pan_sy + (mouse_my - pan_my);
if(mouse_release(mb_middle))
panning = false;
}
2023-03-12 02:28:21 +01:00
if(mouse_press(mb_middle, pFOCUS)) {
panning = true;
pan_mx = mouse_mx;
pan_my = mouse_my;
pan_sx = panx;
pan_sy = pany;
}
if(pHOVER) {
var inc = 0.5;
2023-06-01 19:50:53 +02:00
if(scale > 64) inc = 4;
else if(scale > 16) inc = 2;
else if(scale > 8) inc = 1;
else if(scale > 2) inc = 0.5;
else if(scale > 0.25) inc = 0.25;
else inc = 0.05;
2023-03-12 02:28:21 +01:00
//if(WINDOW_ACTIVE != noone) print($"{winwin_mouse_wheel_down(WINDOW_ACTIVE)} : {winwin_mouse_wheel_up(WINDOW_ACTIVE)} : {random(1)}");
2023-03-12 02:28:21 +01:00
var s = scale;
2023-06-01 19:50:53 +02:00
if(mouse_wheel_down()) {
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(scale_levels) - 1; i < n; i++ ) {
2023-06-01 19:50:53 +02:00
if(s > scale_levels[i] && s <= scale_levels[i + 1]) {
scale = scale_levels[i];
break;
}
}
}
if(mouse_wheel_up()) {
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(scale_levels) - 1; i < n; i++ ) {
2023-06-01 19:50:53 +02:00
if(s >= scale_levels[i] && s < scale_levels[i + 1]) {
scale = scale_levels[i + 1];
break;
}
}
}
2023-03-12 02:28:21 +01:00
var ds = scale - s;
panx = panx / s * scale;
pany = pany / s * scale;
}
if(mouse_click(mb_right, pFOCUS)) {
var _menu = array_clone(menu);
2024-08-18 13:13:40 +02:00
var _chan = 0;
2024-08-08 06:57:51 +02:00
for( var i = 0; i < array_length(node_target.outputs); i++ ) {
var o = node_target.outputs[i];
2023-03-12 02:28:21 +01:00
if(o.type != VALUE_TYPE.surface) continue;
2024-08-18 13:13:40 +02:00
array_push(_menu, menuItem(o.name, function(_dat) { changeChannel(_dat.index); }, noone, noone, noone, { index: _chan }));
_chan++;
2023-03-12 02:28:21 +01:00
}
2024-09-12 05:08:48 +02:00
menuCall("preview_window_menu", _menu, 0, 0, fa_left);
2023-03-12 02:28:21 +01:00
}
}
}