Pixel-Composer/scripts/panel_preview/panel_preview.gml

918 lines
27 KiB
Text
Raw Normal View History

2022-11-14 03:16:15 +01:00
function Panel_Preview() : PanelContent() constructor {
2023-03-12 02:28:21 +01:00
title = "Preview";
2022-01-13 05:24:03 +01:00
context_str = "Preview";
2023-03-21 03:01:53 +01:00
icon = THEME.panel_preview;
2022-01-13 05:24:03 +01:00
last_focus = noone;
2022-11-14 03:16:15 +01:00
function initSize() {
canvas_x = w / 2 - ui(64);
canvas_y = h / 2 - ui(64);
}
initSize();
2022-11-03 11:44:49 +01:00
canvas_s = ui(1);
canvas_w = ui(128);
canvas_h = ui(128);
2023-01-25 06:49:00 +01:00
canvas_a = 0;
2022-01-13 05:24:03 +01:00
canvas_bg = -1;
do_fullView = false;
canvas_hover = true;
canvas_dragging = false;
2023-01-25 06:49:00 +01:00
canvas_drag_key = 0;
2022-01-13 05:24:03 +01:00
canvas_drag_mx = 0;
canvas_drag_my = 0;
canvas_drag_sx = 0;
canvas_drag_sy = 0;
2023-02-28 09:43:01 +01:00
sample_color = noone;
sample_x = noone;
sample_y = noone;
preview_node = [ noone, noone ];
preview_surface = [ 0, 0 ];
2023-02-19 02:13:19 +01:00
tile_surface = surface_create(1, 1);
2022-01-13 05:24:03 +01:00
2023-03-21 03:01:53 +01:00
inspect_node = noone;
2022-01-13 05:24:03 +01:00
preview_x = 0;
preview_x_to = 0;
preview_x_max = 0;
preview_sequence = [ 0, 0 ];
_preview_sequence = preview_sequence;
2022-01-13 05:24:03 +01:00
preview_rate = 10;
2022-11-03 11:44:49 +01:00
grid_show = false;
2022-12-19 13:35:30 +01:00
grid_snap = false;
2022-11-03 11:44:49 +01:00
grid_width = 16;
grid_height = 16;
grid_opacity = 0.5;
2022-11-18 03:20:31 +01:00
grid_color = COLORS.panel_preview_grid;
2022-01-13 05:24:03 +01:00
2023-03-11 01:40:17 +01:00
tool_x = 0;
tool_x_to = 0;
tool_x_max = 0;
tool_current = noone;
2022-01-13 05:24:03 +01:00
2023-03-11 01:40:17 +01:00
right_menu_y = 8;
mouse_on_preview = false;
resetViewOnDoubleClick = true;
splitView = 0;
splitPosition = 0.5;
splitSelection = 0;
splitViewDragging = false;
splitViewStart = 0;
splitViewMouse = 0;
2023-02-19 02:13:19 +01:00
tileMode = 0;
2022-12-10 05:06:01 +01:00
2022-11-03 11:44:49 +01:00
toolbar_height = ui(40);
toolbars = [
[
2022-11-18 03:20:31 +01:00
THEME.icon_reset_when_preview,
function() { return resetViewOnDoubleClick; },
2023-02-14 02:51:14 +01:00
function() { return resetViewOnDoubleClick? get_text("panel_preview_center_canvas_on_preview", "Center canvas on preview") :
get_text("panel_preview_keep_canvas_on_preview", "Keep canvas on preview"); },
function() { resetViewOnDoubleClick = !resetViewOnDoubleClick; }
],
[
2022-11-18 03:20:31 +01:00
THEME.icon_split_view,
function() { return splitView; },
function() {
switch(splitView) {
2023-02-14 02:51:14 +01:00
case 0 : return get_text("panel_preview_split_view_off", "Split view off");
case 1 : return get_text("panel_preview_horizontal_split_view", "Horizontal split view");
case 2 : return get_text("panel_preview_vertical_split_view", "Vertical split view");
}
2023-02-14 02:51:14 +01:00
return get_text("panel_preview_split_view", "Split view");
},
function() { splitView = (splitView + 1) % 3; }
],
2022-12-10 05:06:01 +01:00
[
THEME.icon_tile_view,
2023-02-19 02:13:19 +01:00
function() { var t = [3, 0, 1, 2]; return array_safe_get(t, tileMode); },
2022-12-10 05:06:01 +01:00
function() {
switch(tileMode) {
2023-02-14 02:51:14 +01:00
case 0 : return get_text("panel_preview_tile_off", "Tile off");
case 1 : return get_text("panel_preview_tile_horizontal", "Tile horizontal");
case 2 : return get_text("panel_preview_tile_vertical", "Tile vertical");
case 3 : return get_text("panel_preview_tile_both", "Tile both");
2022-12-10 05:06:01 +01:00
}
2023-02-14 02:51:14 +01:00
return get_text("panel_preview_tile_mode", "Tile mode");
2022-12-10 05:06:01 +01:00
},
2023-02-19 02:13:19 +01:00
function(data) {
menuCall(data.x + ui(28), data.y + ui(28), [
menuItem(get_text("panel_preview_tile_off", "Tile off"), function() { tileMode = 0; }),
menuItem(get_text("panel_preview_tile_horizontal", "Tile horizontal"), function() { tileMode = 1; }),
menuItem(get_text("panel_preview_tile_vertical", "Tile vertical"), function() { tileMode = 2; }),
menuItem(get_text("panel_preview_tile_both", "Tile both"), function() { tileMode = 3; }),
]);
}
2022-12-10 05:06:01 +01:00
],
2022-09-21 06:09:40 +02:00
[
2022-11-18 03:20:31 +01:00
THEME.icon_grid_setting,
2022-09-21 06:09:40 +02:00
function() { return 0; },
2023-02-14 02:51:14 +01:00
function() { return get_text("grid_title", "Grid setting") },
2022-09-21 06:09:40 +02:00
function(param) {
var gs = dialogCall(o_dialog_preview_grid, param.x, param.y);
gs.anchor = ANCHOR.bottom | ANCHOR.left;
}
],
];
actions = [
[
2022-11-18 03:20:31 +01:00
THEME.icon_center_canvas,
2023-02-14 02:51:14 +01:00
get_text("panel_preview_center_canvas", "Center canvas"),
function() { fullView(); }
],
2022-09-21 06:09:40 +02:00
[
2022-11-18 03:20:31 +01:00
THEME.icon_preview_export,
2023-02-14 02:51:14 +01:00
get_text("panel_preview_export_canvas", "Export canvas"),
2022-09-21 06:09:40 +02:00
function() { saveCurrentFrame(); }
],
]
2022-09-21 06:09:40 +02:00
tb_framerate = new textBox(TEXTBOX_INPUT.number, function(val) { preview_rate = real(val); });
2022-01-13 05:24:03 +01:00
2023-03-21 03:01:53 +01:00
addHotkey("Preview", "Focus content", "F", MOD_KEY.none, function() { PANEL_PREVIEW.fullView(); });
addHotkey("Preview", "Save current frame", "S", MOD_KEY.shift, function() { PANEL_PREVIEW.saveCurrentFrame(); });
addHotkey("Preview", "Save all current frame", -1, MOD_KEY.none, function() { PANEL_PREVIEW.saveAllCurrentFrames(); });
addHotkey("Preview", "Preview window", "P", MOD_KEY.ctrl, function() { PANEL_PREVIEW.previewWindow(PANEL_PREVIEW.getNodePreview()); });
addHotkey("Preview", "Toggle grid", "G", MOD_KEY.ctrl, function() { PANEL_PREVIEW.grid_show = !PANEL_PREVIEW.grid_show; });
2022-01-13 05:24:03 +01:00
function setNodePreview(node) {
if(resetViewOnDoubleClick)
do_fullView = true;
preview_node[splitView? splitSelection : 0] = node;
}
function getNodePreview() { return preview_node[splitView? splitSelection : 0]; }
function getNodePreviewSurface() { return preview_surface[splitView? splitSelection : 0]; }
function getNodePreviewSequence() { return preview_sequence[splitView? splitSelection : 0]; }
function getPreviewData() {
preview_surface = [ 0, 0 ];
preview_sequence = [ 0, 0 ];
for( var i = 0; i < 2; i++ ) {
var node = preview_node[i];
if(node == noone) continue;
2022-12-16 09:18:09 +01:00
var _prev_val = node.getPreviewValue();
2022-12-19 13:35:30 +01:00
if(_prev_val == undefined) continue;
2022-12-16 09:18:09 +01:00
if(_prev_val == noone) continue;
if(_prev_val.type != VALUE_TYPE.surface) continue;
var value = _prev_val.getValue();
2023-01-25 06:49:00 +01:00
if(is_array(value)) {
preview_sequence[i] = value;
2023-01-25 06:49:00 +01:00
canvas_a = array_length(value);
} else {
preview_surface[i] = value;
2023-01-25 06:49:00 +01:00
canvas_a = 0;
}
2022-11-03 11:44:49 +01:00
if(preview_sequence[i] != 0) {
if(array_length(preview_sequence[i]) == 0) return;
preview_surface[i] = preview_sequence[i][safe_mod(node.preview_index, array_length(preview_sequence[i]))];
}
}
var prevS = getNodePreviewSurface();
if(is_surface(prevS)) {
canvas_w = surface_get_width(prevS);
canvas_h = surface_get_height(prevS);
}
}
2023-03-13 10:45:56 +01:00
function onFocusBegin() { PANEL_PREVIEW = self; }
2022-01-13 05:24:03 +01:00
function dragCanvas() {
if(canvas_dragging) {
2022-12-23 04:45:52 +01:00
if(!MOUSE_WRAPPING) {
var dx = mx - canvas_drag_mx;
var dy = my - canvas_drag_my;
canvas_x += dx;
canvas_y += dy;
}
2022-01-13 05:24:03 +01:00
canvas_drag_mx = mx;
canvas_drag_my = my;
2022-12-23 04:45:52 +01:00
setMouseWrap();
2022-01-13 05:24:03 +01:00
2023-01-25 06:49:00 +01:00
if(mouse_release(canvas_drag_key))
2022-01-13 05:24:03 +01:00
canvas_dragging = false;
}
2022-11-14 03:16:15 +01:00
if(pFOCUS && pHOVER && canvas_hover) {
2023-01-25 06:49:00 +01:00
var hold = false;
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_middle)) {
2023-01-25 06:49:00 +01:00
hold = true;
canvas_drag_key = mb_middle;
} else if(mouse_press(mb_left) && key_mod_press(ALT)) {
hold = true;
canvas_drag_key = mb_left;
}
if(hold) {
2022-01-13 05:24:03 +01:00
canvas_dragging = true;
canvas_drag_mx = mx;
canvas_drag_my = my;
canvas_drag_sx = canvas_x;
canvas_drag_sy = canvas_y;
}
var _canvas_s = canvas_s;
2023-03-21 03:01:53 +01:00
var inc = 0.1;
2022-01-13 05:24:03 +01:00
if(canvas_s > 16) inc = 2;
else if(canvas_s > 8) inc = 1;
2023-03-21 03:01:53 +01:00
else if(canvas_s > 3) inc = 0.5;
else if(canvas_s > 1) inc = 0.25;
2022-01-13 05:24:03 +01:00
2023-03-19 09:17:39 +01:00
if(mouse_wheel_down()) canvas_s = max(round(canvas_s / inc) * inc - inc, 0.10);
if(mouse_wheel_up()) canvas_s = min(round(canvas_s / inc) * inc + inc, 64);
2022-01-13 05:24:03 +01:00
if(_canvas_s != canvas_s) {
var dx = (canvas_s - _canvas_s) * ((mx - canvas_x) / _canvas_s);
var dy = (canvas_s - _canvas_s) * ((my - canvas_y) / _canvas_s);
canvas_x -= dx;
canvas_y -= dy;
}
}
2023-01-25 06:49:00 +01:00
2023-03-11 01:40:17 +01:00
canvas_hover = point_in_rectangle(mx, my, 0, toolbar_height, w, h - toolbar_height);
2022-01-13 05:24:03 +01:00
}
function fullView() {
var prevS = getNodePreviewSurface();
2023-03-07 14:29:47 +01:00
if(!is_surface(prevS)) {
canvas_s = 1;
canvas_x = w / 2;
2023-03-12 02:28:21 +01:00
canvas_y = (h - toolbar_height * 2) / 2;
2023-03-07 14:29:47 +01:00
return;
}
canvas_w = surface_get_width(prevS);
canvas_h = surface_get_height(prevS);
2022-01-13 05:24:03 +01:00
2023-03-12 02:28:21 +01:00
var ss = min((w - 32) / canvas_w, (h - 32 - toolbar_height * 2) / canvas_h);
2022-01-13 05:24:03 +01:00
canvas_s = ss;
canvas_x = w / 2 - canvas_w * canvas_s / 2;
2023-03-12 02:28:21 +01:00
canvas_y = h / 2 - canvas_h * canvas_s / 2;
2022-01-13 05:24:03 +01:00
if(PANEL_GRAPH.node_focus) {
canvas_x -= PANEL_GRAPH.node_focus.preview_x * canvas_s;
canvas_y -= PANEL_GRAPH.node_focus.preview_y * canvas_s;
}
}
sbChannel = new scrollBox([], function(index) {
var node = getNodePreview();
if(node == noone) return;
2023-03-11 01:40:17 +01:00
node.preview_channel = array_safe_get(sbChannelIndex, index);
});
2023-01-09 03:14:20 +01:00
sbChannelIndex = [];
sbChannel.align = fa_left;
function drawNodeChannel(_x, _y) {
var _node = getNodePreview();
if(_node == noone) return;
if(ds_list_size(_node.outputs) < 2) return;
var chName = [];
2023-01-09 03:14:20 +01:00
sbChannelIndex = [];
2023-03-11 01:40:17 +01:00
var currName = _node.outputs[| _node.preview_channel].name;
2022-11-18 03:20:31 +01:00
draw_set_text(f_p0, fa_center, fa_center);
2023-03-11 01:40:17 +01:00
var ww = clamp(w - ui(240), string_width(currName) + ui(48), ui(200));
var hh = toolbar_height - ui(12);
for( var i = 0; i < ds_list_size(_node.outputs); i++ ) {
2022-09-21 06:09:40 +02:00
if(_node.outputs[| i].type != VALUE_TYPE.surface) continue;
2023-01-09 03:14:20 +01:00
array_push(chName, _node.outputs[| i].name);
2023-01-09 03:14:20 +01:00
array_push(sbChannelIndex, i);
2022-11-03 11:44:49 +01:00
ww = max(ww, string_width(_node.outputs[| i].name) + ui(40));
}
sbChannel.data_list = chName;
2022-11-14 03:16:15 +01:00
sbChannel.hover = pHOVER;
sbChannel.active = pFOCUS;
2023-03-11 01:40:17 +01:00
sbChannel.draw(_x - ww, _y - hh / 2, ww, hh, currName, [mx, my], x, y);
2022-11-03 11:44:49 +01:00
right_menu_y += ui(40);
}
function drawNodePreview() {
var ss = canvas_s;
2022-09-21 06:09:40 +02:00
var psx = 0, psy = 0;
var psw = 0, psh = 0;
var pswd = 0, pshd = 0;
var psx1 = 0, psy1 = 0;
var ssx = 0, ssy = 0;
var ssw = 0, ssh = 0;
if(is_surface(preview_surface[0])) {
2022-09-21 06:09:40 +02:00
psx = canvas_x + preview_node[0].preview_x * ss;
psy = canvas_y + preview_node[0].preview_y * ss;
2022-09-21 06:09:40 +02:00
psw = surface_get_width(preview_surface[0]);
psh = surface_get_height(preview_surface[0]);
pswd = psw * ss;
pshd = psh * ss;
2022-09-21 06:09:40 +02:00
psx1 = psx + pswd;
psy1 = psy + pshd;
2022-01-13 05:24:03 +01:00
}
if(is_surface(preview_surface[1])) {
var ssx = canvas_x + preview_node[1].preview_x * ss;
var ssy = canvas_y + preview_node[1].preview_y * ss;
2022-01-13 05:24:03 +01:00
var ssw = surface_get_width(preview_surface[1]);
var ssh = surface_get_height(preview_surface[1]);
2022-01-13 05:24:03 +01:00
}
2023-03-21 03:01:53 +01:00
var _node = getNodePreview();
if(_node)
title = _node.display_name == ""? _node.name : _node.display_name;
switch(splitView) {
case 0 :
if(is_surface(preview_surface[0])) {
preview_node[0].previewing = 1;
2022-12-10 05:06:01 +01:00
switch(tileMode) {
2022-12-16 09:18:09 +01:00
case 0 :
var aa = preview_node[0].preview_alpha;
draw_surface_ext_safe(preview_surface[0], psx, psy, ss, ss, 0, c_white, aa);
break;
2023-02-19 02:13:19 +01:00
case 1 :
tile_surface = surface_verify(tile_surface, w, surface_get_height(preview_surface[0]) * ss);
surface_set_target(tile_surface);
2023-03-19 09:17:39 +01:00
DRAW_CLEAR
2023-02-19 02:13:19 +01:00
draw_surface_tiled_ext_safe(preview_surface[0], psx, 0, ss, ss, c_white, 1);
surface_reset_target();
2023-03-19 09:17:39 +01:00
draw_surface_safe(tile_surface, 0, psy);
2023-02-19 02:13:19 +01:00
break;
case 2 :
tile_surface = surface_verify(tile_surface, surface_get_width(preview_surface[0]) * ss, h);
surface_set_target(tile_surface);
2023-03-19 09:17:39 +01:00
DRAW_CLEAR
2023-02-28 09:43:01 +01:00
draw_surface_tiled_ext_safe(preview_surface[0], 0, psy, ss, ss, c_white, 1);
2023-02-19 02:13:19 +01:00
surface_reset_target();
2023-03-19 09:17:39 +01:00
draw_surface_safe(tile_surface, psx, 0);
2023-02-19 02:13:19 +01:00
break;
2022-12-10 05:06:01 +01:00
case 3 : draw_surface_tiled_ext_safe(preview_surface[0], psx, psy, ss, ss, c_white, 1); break;
}
2022-01-13 05:24:03 +01:00
}
break;
case 1 :
var sp = splitPosition * w;
if(is_surface(preview_surface[0])) {
preview_node[0].previewing = 2;
var maxX = min(sp, psx1);
var sW = min(psw, (maxX - psx) / ss);
if(sW > 0)
draw_surface_part_ext_safe(preview_surface[0], 0, 0, sW, psh, psx, psy, ss, ss, 0, c_white, 1);
}
if(is_surface(preview_surface[1])) {
preview_node[1].previewing = 3;
var minX = max(ssx, sp);
var sX = (minX - ssx) / ss;
var spx = max(sp, ssx);
if(sX >= 0 && sX < ssw)
draw_surface_part_ext_safe(preview_surface[1], sX, 0, ssw - sX, ssh, spx, ssy, ss, ss, 0, c_white, 1);
}
break;
case 2 :
var sp = splitPosition * h;
if(is_surface(preview_surface[0])) {
preview_node[0].previewing = 4;
var maxY = min(sp, psy1);
var sH = min(psh, (maxY - psy) / ss);
if(sH > 0)
draw_surface_part_ext_safe(preview_surface[0], 0, 0, psw, sH, psx, psy, ss, ss, 0, c_white, 1);
}
if(is_surface(preview_surface[1])) {
preview_node[1].previewing = 5;
var minY = max(ssy, sp);
var sY = (minY - ssy) / ss;
var spy = max(sp, ssy);
if(sY >= 0 && sY < ssh)
draw_surface_part_ext_safe(preview_surface[1], 0, sY, ssw, ssh - sY, ssx, spy, ss, ss, 0, c_white, 1);
}
break;
2022-01-13 05:24:03 +01:00
}
2022-09-21 06:09:40 +02:00
2023-02-28 09:43:01 +01:00
if(!instance_exists(o_dialog_menubox)) {
sample_color = noone;
sample_x = noone;
sample_y = noone;
if(mouse_on_preview && (mouse_press(mb_right) || key_mod_press(CTRL))) {
var _sx = sample_x;
var _sy = sample_y;
sample_x = floor((mx - canvas_x) / canvas_s);
sample_y = floor((my - canvas_y) / canvas_s);
var surf = getNodePreviewSurface();
if(is_surface(surf))
sample_color = surface_getpixel_ext(surf, sample_x, sample_y);
}
}
2022-09-21 06:09:40 +02:00
if(is_surface(preview_surface[0])) {
if(grid_show) {
var _gw = grid_width * canvas_s;
var _gh = grid_height * canvas_s;
var gw = floor(pswd / _gw);
var gh = floor(pshd / _gh);
var cx = canvas_x;
var cy = canvas_y;
2022-11-03 11:44:49 +01:00
draw_set_color(grid_color);
draw_set_alpha(grid_opacity);
2022-09-21 06:09:40 +02:00
for( var i = 1; i < gw; i++ ) {
var _xx = cx + i * _gw;
draw_line(_xx, cy, _xx, cy + pshd);
}
for( var i = 1; i < gh; i++ ) {
var _yy = cy + i * _gh;
draw_line(cx, _yy, cx + pswd, _yy);
}
2022-11-03 11:44:49 +01:00
draw_set_alpha(1);
2022-09-21 06:09:40 +02:00
}
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS.panel_preview_surface_outline);
2022-09-21 06:09:40 +02:00
draw_rectangle(psx, psy, psx + pswd, psy + pshd, true);
}
2022-01-13 05:24:03 +01:00
}
2022-09-21 06:09:40 +02:00
function drawPreviewOverlay() {
2023-03-11 01:40:17 +01:00
right_menu_y = toolbar_height - ui(4);
2023-03-13 10:45:56 +01:00
if(PANEL_PREVIEW == self) {
draw_set_text(f_p0, fa_right, fa_top, COLORS._main_text_accent);
draw_text(w - ui(8), right_menu_y, "Active");
right_menu_y += string_height("l");
}
2023-01-01 02:06:02 +01:00
draw_set_text(f_p0, fa_right, fa_top, fps >= ANIMATOR.framerate? COLORS._main_text_sub : COLORS._main_value_negative);
2022-11-03 11:44:49 +01:00
draw_text(w - ui(8), right_menu_y, "fps " + string(fps));
right_menu_y += string_height("l");
2023-02-14 02:51:14 +01:00
draw_set_text(f_p0, fa_right, fa_top, COLORS._main_text_sub);
draw_text(w - ui(8), right_menu_y, get_text("frame", "Frame") + " " + string(ANIMATOR.current_frame) + "/" + string(ANIMATOR.frames_total));
2022-09-21 06:09:40 +02:00
var _node = getNodePreview();
if(_node == noone) return;
2022-11-03 11:44:49 +01:00
right_menu_y += string_height("l");
2023-01-25 06:49:00 +01:00
var txt = string(canvas_w) + "x" + string(canvas_h) + "px";
if(canvas_a) txt = string(canvas_a) + " x " + txt;
draw_text(w - ui(8), right_menu_y, txt);
2022-11-03 11:44:49 +01:00
right_menu_y += string_height("l");
draw_text(w - ui(8), right_menu_y, "x" + string(canvas_s));
2023-03-19 09:17:39 +01:00
right_menu_y += string_height("l");
var mpx = floor((mx - canvas_x) / canvas_s);
var mpy = floor((my - canvas_y) / canvas_s);
draw_text(w - ui(8), right_menu_y, "[" + string(mpx) + ", " + string(mpy) + "]");
2022-11-03 11:44:49 +01:00
right_menu_y += string_height("l");
2022-12-16 09:18:09 +01:00
var pseq = getNodePreviewSequence();
if(pseq == 0) return;
if(!array_equals(pseq, _preview_sequence)) {
_preview_sequence = pseq;
preview_x = 0;
preview_x_to = 0;
}
2022-11-03 11:44:49 +01:00
var prev_size = ui(48);
2022-12-10 05:06:01 +01:00
preview_x = lerp_float(preview_x, preview_x_to, 4);
2022-01-13 05:24:03 +01:00
2023-03-11 01:40:17 +01:00
if(pHOVER && my > h - toolbar_height - prev_size - ui(16) && my > toolbar_height) {
2022-12-16 09:18:09 +01:00
canvas_hover = false;
2023-01-25 06:49:00 +01:00
2022-12-16 09:18:09 +01:00
if(mouse_wheel_down()) preview_x_to = clamp(preview_x_to - prev_size, - preview_x_max, 0);
if(mouse_wheel_up()) preview_x_to = clamp(preview_x_to + prev_size, - preview_x_max, 0);
}
2022-01-13 05:24:03 +01:00
2022-12-16 09:18:09 +01:00
preview_x_max = 0;
var xx = preview_x + ui(8);
2023-01-17 08:11:55 +01:00
var yy = h - toolbar_height - prev_size - ui(8);
if(my > yy) mouse_on_preview = false;
2022-12-16 09:18:09 +01:00
for(var i = 0; i < array_length(pseq); i++) {
var prev = pseq[i];
if(!is_surface(prev)) continue;
2022-01-13 05:24:03 +01:00
2022-12-16 09:18:09 +01:00
var prev_w = surface_get_width(prev);
var prev_h = surface_get_height(prev);
var ss = prev_size / max(prev_w, prev_h);
var prev_sw = prev_w * ss;
draw_set_color(COLORS.panel_preview_surface_outline);
draw_rectangle(xx, yy, xx + prev_w * ss, yy + prev_h * ss, true);
2022-01-13 05:24:03 +01:00
2022-12-16 09:18:09 +01:00
if(pHOVER && point_in_rectangle(mx, my, xx, yy, xx + prev_sw, yy + prev_h * ss)) {
if(mouse_press(mb_left, pFOCUS)) {
_node.preview_index = i;
_node.onValueUpdate(0);
if(resetViewOnDoubleClick)
do_fullView = true;
2022-01-13 05:24:03 +01:00
}
2022-12-16 09:18:09 +01:00
draw_surface_ext_safe(prev, xx, yy, ss, ss, 0, c_white, 1);
} else {
draw_surface_ext_safe(prev, xx, yy, ss, ss, 0, c_white, 0.5);
}
2022-01-13 05:24:03 +01:00
2022-12-16 09:18:09 +01:00
if(i == _node.preview_index) {
draw_set_color(COLORS._main_accent);
draw_rectangle(xx, yy, xx + prev_sw, yy + prev_h * ss, true);
2022-01-13 05:24:03 +01:00
}
2022-12-16 09:18:09 +01:00
xx += prev_sw + ui(8);
preview_x_max += prev_sw + ui(8);
}
preview_x_max = max(preview_x_max - ui(100), 0);
2022-01-13 05:24:03 +01:00
2022-12-16 09:18:09 +01:00
var by = h - toolbar_height - prev_size - ui(56);
var bx = ui(10);
2022-01-13 05:24:03 +01:00
2022-12-16 09:18:09 +01:00
var b = buttonInstant(THEME.button_hide, bx, by, ui(40), ui(40), [mx, my], pFOCUS, pHOVER);
if(_node.preview_speed == 0) {
if(b) {
draw_sprite_ui_uniform(THEME.sequence_control, 1, bx + ui(20), by + ui(20), 1, COLORS._main_icon, 1);
if(b == 2) _node.preview_speed = preview_rate / game_get_speed(gamespeed_fps);
}
draw_sprite_ui_uniform(THEME.sequence_control, 1, bx + ui(20), by + ui(20), 1, COLORS._main_icon, 0.5);
} else {
if(b) {
draw_sprite_ui_uniform(THEME.sequence_control, 0, bx + ui(20), by + ui(20), 1, COLORS._main_accent, 1);
if(b == 2) _node.preview_speed = 0;
2022-01-13 05:24:03 +01:00
}
2022-12-16 09:18:09 +01:00
draw_sprite_ui_uniform(THEME.sequence_control, 0, bx + ui(20), by + ui(20), 1, COLORS._main_accent, .75);
2022-01-13 05:24:03 +01:00
}
2022-09-21 06:09:40 +02:00
}
2022-12-12 09:08:03 +01:00
function drawNodeTools(active, _node) {
2022-09-21 06:09:40 +02:00
var _mx = mx;
var _my = my;
2022-11-14 03:16:15 +01:00
var isHover = pHOVER && mouse_on_preview;
2022-01-13 05:24:03 +01:00
2022-09-21 06:09:40 +02:00
if(_node.tools != -1) {
2023-03-05 07:16:44 +01:00
var xx = ui(8);
var yy = ui(40);
2022-01-13 05:24:03 +01:00
2022-09-21 06:09:40 +02:00
for(var i = 0; i < array_length(_node.tools); i++) {
2022-11-18 03:20:31 +01:00
var b = buttonInstant(THEME.button, xx, yy, ui(40), ui(40), [_mx, _my], pFOCUS, isHover);
2022-09-21 06:09:40 +02:00
if(b > 0) active = false;
2022-11-03 11:44:49 +01:00
yy += ui(48);
2022-01-13 05:24:03 +01:00
}
}
2022-09-21 06:09:40 +02:00
2022-12-19 13:35:30 +01:00
var cx = canvas_x + _node.preview_x * canvas_s;
var cy = canvas_y + _node.preview_y * canvas_s;
var _snx = 0, _sny = 0;
2022-12-22 03:09:55 +01:00
if(key_mod_press(CTRL)) {
2022-12-19 13:35:30 +01:00
_snx = grid_show? grid_width : 1;
_sny = grid_show? grid_height : 1;
} else if(grid_snap) {
_snx = grid_width;
_sny = grid_height;
}
2023-03-11 01:40:17 +01:00
var overlayHover = active && isHover && point_in_rectangle(mx, my, 0, toolbar_height, w, h - toolbar_height);
if(_node.tools != -1)
overlayHover &= mx > ui(48);
_node.drawOverlay(overlayHover && !key_mod_press(CTRL), cx, cy, canvas_s, _mx, _my, _snx, _sny);
2022-01-13 05:24:03 +01:00
if(_node.tools != -1) {
2023-03-05 07:16:44 +01:00
var xx = ui(8);
var yy = ui(40);
2022-01-13 05:24:03 +01:00
for(var i = 0; i < array_length(_node.tools); i++) {
2022-11-18 03:20:31 +01:00
var b = buttonInstant(THEME.button, xx, yy, ui(40), ui(40), [_mx, _my], pFOCUS, isHover);
2023-03-11 01:40:17 +01:00
var tool = _node.tools[i];
if(b == 1) {
TOOLTIP = tool.name;
mouse_on_preview = false;
} if(b == 2)
tool.toggle();
2022-01-13 05:24:03 +01:00
2023-03-19 09:17:39 +01:00
if(pFOCUS && WIDGET_CURRENT == noone && keyboard_check_pressed(ord(string(i + 1))))
2023-03-11 01:40:17 +01:00
tool.toggle();
2022-01-13 05:24:03 +01:00
2023-03-11 01:40:17 +01:00
if(tool_current == tool)
2022-11-18 03:20:31 +01:00
draw_sprite_stretched(THEME.button, 2, xx, yy, ui(40), ui(40));
2022-01-13 05:24:03 +01:00
2023-03-11 01:40:17 +01:00
if(tool.subtools > 0)
draw_sprite_ui_uniform(tool.spr[tool.selecting], 0, xx + ui(20), yy + ui(20));
else
draw_sprite_ui_uniform(tool.spr, 0, xx + ui(20), yy + ui(20));
2022-11-03 11:44:49 +01:00
yy += ui(48);
2022-01-13 05:24:03 +01:00
}
2023-03-11 01:40:17 +01:00
} else
tool_current = noone;
}
2023-03-11 01:40:17 +01:00
function drawToolBar(_node) {
2022-11-03 11:44:49 +01:00
toolbar_height = ui(40);
var ty = h - toolbar_height;
2022-11-18 03:20:31 +01:00
//draw_sprite_stretched_ext(THEME.toolbar_shadow, 0, 0, ty - 12 + 4, w, 12, c_white, 0.5);
2023-02-28 09:43:01 +01:00
var scHeight = ui(32);
draw_set_color(COLORS.panel_toolbar_fill);
draw_rectangle(0, 0, w, scHeight, false);
draw_set_color(COLORS.panel_toolbar_outline);
2023-03-11 01:40:17 +01:00
draw_line(0, scHeight, w, scHeight);
if(tool_current != noone) { //tool settings
var settings = PANEL_GRAPH.node_focus.tool_settings;
var len = array_length(settings);
for( var i = 0; i < array_length(tool_current.settings); i++ )
settings[len + i] = tool_current.settings[i];
tool_x = lerp_float(tool_x, tool_x_to, 5);
var tolx = tool_x + ui(16);
var toly = ui(8);
2023-03-19 09:17:39 +01:00
var tolw = ui(48);
2023-03-11 01:40:17 +01:00
var tolh = toolbar_height - ui(20);
var tol_max_w = ui(32);
for( var i = 0; i < array_length(settings); i++ ) {
var sett = settings[i];
var nme = sett[0];
var wdg = sett[1];
var key = sett[2];
var atr = sett[3];
draw_set_text(f_p2, fa_left, fa_center, COLORS._main_text_sub);
draw_text(tolx, toolbar_height / 2 - ui(2), nme);
tolx += string_width(nme) + ui(8);
tol_max_w += string_width(nme) + ui(8);
wdg.setActiveFocus(pFOCUS, pHOVER);
switch(instanceof(wdg)) {
case "textBox" :
wdg.draw(tolx, toly, tolw, tolh, atr[$ key], [ mx, my ]);
break;
case "checkBoxGroup" :
tolw = tolh * wdg.size;
wdg.draw(tolx, toly, atr[$ key], [ mx, my ], tolh);
break;
case "checkBox" :
tolw = tolh;
wdg.draw(tolx, toly, atr[$ key], [ mx, my ], tolh);
break;
}
tolx += tolw + ui(16);
tol_max_w += tolw + ui(16);
}
tol_max_w = max(0, tol_max_w - w);
if(point_in_rectangle(mx, my, 0, 0, w, toolbar_height)) {
if(mouse_wheel_up()) tool_x_to = clamp(tool_x_to + ui(64), -tol_max_w, 0);
if(mouse_wheel_down()) tool_x_to = clamp(tool_x_to - ui(64), -tol_max_w, 0);
}
} else { //color sampler
var cx = ui(10);
var cy = ui(10);
var cw = ui(32);
var ch = scHeight - ui(16);
if(sample_color != noone) {
draw_set_color(sample_color);
draw_rectangle(cx, cy, cx + cw, cy + ch, false);
}
draw_set_color(COLORS.panel_toolbar_outline);
draw_rectangle(cx, cy, cx + cw, cy + ch, true);
2023-02-28 09:43:01 +01:00
2023-03-11 01:40:17 +01:00
if(sample_color != noone) {
var tx = cx + cw + ui(16);
var hx = color_get_hex(sample_color);
draw_set_text(f_p0, fa_left, fa_center, COLORS._main_text);
draw_text(tx, cy + ch / 2, hx);
2023-02-28 09:43:01 +01:00
2023-03-11 01:40:17 +01:00
tx += string_width(hx) + ui(8);
draw_set_color(COLORS._main_text_sub);
draw_text(tx, cy + ch / 2, "(" + string(color_get_alpha(sample_color)) + ")");
}
2023-03-13 10:45:56 +01:00
}
2023-02-28 09:43:01 +01:00
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS.panel_toolbar_fill);
draw_rectangle(0, ty, w, h, false);
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS.panel_toolbar_outline);
draw_line(0, ty, w, ty);
var tbx = toolbar_height / 2;
var tby = ty + toolbar_height / 2;
for( var i = 0; i < array_length(toolbars); i++ ) {
var tb = toolbars[i];
var tbSpr = tb[0];
var tbInd = tb[1]();
var tbTooltip = tb[2]();
2022-11-18 03:20:31 +01:00
var b = buttonInstant(THEME.button_hide, tbx - ui(14), tby - ui(14), ui(28), ui(28), [mx, my], pFOCUS, pHOVER, tbTooltip, tbSpr, tbInd);
2022-11-03 11:44:49 +01:00
if(b == 2) tb[3]( { x: x + tbx - ui(14), y: y + tby - ui(14) } );
2022-11-03 11:44:49 +01:00
tbx += ui(32);
}
tbx = w - toolbar_height / 2;
for( var i = 0; i < array_length(actions); i++ ) {
var tb = actions[i];
var tbSpr = tb[0];
var tbTooltip = tb[1];
2022-11-18 03:20:31 +01:00
var b = buttonInstant(THEME.button_hide, tbx - ui(14), tby - ui(14), ui(28), ui(28), [mx, my], pFOCUS, pHOVER, tbTooltip, tbSpr, 0);
if(b == 2) tb[2]();
2022-11-03 11:44:49 +01:00
tbx -= ui(32);
}
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS.panel_toolbar_separator);
2022-11-03 11:44:49 +01:00
draw_line_width(tbx + ui(12), tby - toolbar_height / 2 + ui(8), tbx + ui(12), tby + toolbar_height / 2 - ui(8), 2);
drawNodeChannel(tbx, tby);
}
function drawSplitView() {
if(splitView == 0) return;
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS.panel_preview_split_line);
if(splitViewDragging) {
if(splitView == 1) {
var cx = splitViewStart + (mx - splitViewMouse);
splitPosition = clamp(cx / w, .1, .9);
} else if(splitView == 2) {
var cy = splitViewStart + (my - splitViewMouse);
splitPosition = clamp(cy / h, .1, .9);
}
2022-12-10 05:06:01 +01:00
if(mouse_release(mb_left))
splitViewDragging = false;
}
if(splitView == 1) {
var sx = w * splitPosition;
2022-11-03 11:44:49 +01:00
if(mouse_on_preview && point_in_rectangle(mx, my, sx - ui(4), 0, sx + ui(4), h)) {
draw_line_width(sx, 0, sx, h, 2);
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, pFOCUS)) {
splitViewDragging = true;
splitViewStart = sx;
splitViewMouse = mx;
}
} else
draw_line_width(sx, 0, sx, h, 1);
2023-03-21 03:01:53 +01:00
draw_sprite_ui_uniform(THEME.icon_active_split, 0, splitSelection? sx + ui(16) : sx - ui(16), toolbar_height + ui(16),, COLORS._main_accent);
2022-12-10 05:06:01 +01:00
if(mouse_on_preview && mouse_press(mb_left, pFOCUS)) {
if(point_in_rectangle(mx, my, 0, 0, sx, h))
splitSelection = 0;
else if(point_in_rectangle(mx, my, sx, 0, w, h))
splitSelection = 1;
}
} else {
var sy = h * splitPosition;
2022-11-03 11:44:49 +01:00
if(mouse_on_preview && point_in_rectangle(mx, my, 0, sy - ui(4), w, sy + ui(4))) {
draw_line_width(0, sy, w, sy, 2);
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, pFOCUS)) {
splitViewDragging = true;
splitViewStart = sy;
splitViewMouse = my;
}
} else
draw_line_width(0, sy, w, sy, 1);
2022-11-18 03:20:31 +01:00
draw_sprite_ui_uniform(THEME.icon_active_split, 0, ui(16), splitSelection? sy + ui(16) : sy - ui(16),, COLORS._main_accent);
2022-12-10 05:06:01 +01:00
if(mouse_on_preview && mouse_press(mb_left, pFOCUS)) {
if(point_in_rectangle(mx, my, 0, 0, w, sy))
splitSelection = 0;
else if(point_in_rectangle(mx, my, 0, sy, w, h))
splitSelection = 1;
}
}
2022-01-13 05:24:03 +01:00
}
2023-03-11 06:40:34 +01:00
function drawContent(panel) {
2023-03-11 01:40:17 +01:00
mouse_on_preview = pHOVER && point_in_rectangle(mx, my, 0, toolbar_height, w, h - toolbar_height);
2022-11-18 03:20:31 +01:00
draw_clear(COLORS.panel_bg_clear);
2023-03-13 10:45:56 +01:00
if(canvas_bg == -1 && canvas_s >= 0.1)
2023-03-11 06:40:34 +01:00
draw_sprite_tiled_ext(s_transparent, 0, canvas_x, canvas_y, canvas_s, canvas_s, c_white, 0.5);
else
2022-01-13 05:24:03 +01:00
draw_clear(canvas_bg);
2023-03-21 03:01:53 +01:00
title = "Preview";
2022-01-13 05:24:03 +01:00
dragCanvas();
getPreviewData();
drawNodePreview();
2022-09-21 06:09:40 +02:00
drawPreviewOverlay();
2023-01-17 08:11:55 +01:00
2023-03-21 03:01:53 +01:00
if(PANEL_PREVIEW == self)
inspect_node = PANEL_GRAPH.node_focus;
if(inspect_node)
drawNodeTools(pFOCUS, inspect_node);
if(last_focus != inspect_node) {
last_focus = inspect_node;
2023-03-11 01:40:17 +01:00
tool_current = noone;
2022-01-13 05:24:03 +01:00
}
if(do_fullView) {
do_fullView = false;
fullView();
}
2023-03-11 01:40:17 +01:00
if(mouse_on_preview && mouse_press(mb_right, pFOCUS)) {
2023-02-19 02:13:19 +01:00
menuCall(,, [
2023-02-28 09:43:01 +01:00
menuItem(get_text("panel_graph_preview_window", "Send to preview window"), function() { previewWindow(getNodePreview()); }, noone, ["Preview", "Preview window"]),
-1,
2023-03-13 10:45:56 +01:00
menuItem(get_text("panel_preview_save", "Save current preview as") + "...", function() { saveCurrentFrame(); }),
menuItem(get_text("panel_preview_save_all", "Save all current previews as") + "...", function() { saveAllCurrentFrames(); }),
2023-02-28 09:43:01 +01:00
-1,
menuItem(get_text("panel_preview_copy_color", "Copy color [") + string(sample_color) + "]", function() { clipboard_set_text(sample_color); }),
menuItem(get_text("panel_preview_copy_color", "Copy hex [") + string(color_get_hex(sample_color)) + "]", function() { clipboard_set_text(color_get_hex(sample_color)); }),
2022-12-10 05:06:01 +01:00
]);
}
drawSplitView();
2023-03-11 01:40:17 +01:00
drawToolBar(PANEL_GRAPH.node_focus);
2022-01-13 05:24:03 +01:00
}
function saveCurrentFrame() {
var prevS = getNodePreviewSurface();
if(!is_surface(prevS)) return;
2022-01-13 05:24:03 +01:00
2023-02-28 09:43:01 +01:00
var path = get_save_filename(".png", "export");
key_release();
2022-01-13 05:24:03 +01:00
if(path == "") return;
2022-09-21 06:09:40 +02:00
if(filename_ext(path) != ".png") path += ".png";
2022-01-13 05:24:03 +01:00
2023-03-19 09:17:39 +01:00
surface_save_safe(prevS, path);
2022-01-13 05:24:03 +01:00
}
function saveAllCurrentFrames() {
2023-02-28 09:43:01 +01:00
var path = get_save_filename(".png", "export");
key_release();
2022-01-13 05:24:03 +01:00
if(path == "") return;
2022-09-21 06:09:40 +02:00
var ext = ".png";
2022-01-13 05:24:03 +01:00
var name = string_replace_all(path, ext, "");
var ind = 0;
var pseq = getNodePreviewSequence();
for(var i = 0; i < array_length(pseq); i++) {
var prev = pseq[i];
2022-01-13 05:24:03 +01:00
if(!is_surface(prev)) continue;
var _name = name + string(ind) + ext;
2023-03-19 09:17:39 +01:00
surface_save_safe(prev, _name);
2022-01-13 05:24:03 +01:00
ind++;
}
}
}