Pixel-Composer/scripts/node_canvas_group/node_canvas_group.gml

331 lines
8.7 KiB
Text
Raw Normal View History

2024-06-29 05:22:20 +02:00
function Node_Canvas_Group(_x, _y, _group) : Node_Collection(_x, _y, _group) constructor {
2024-06-11 05:06:42 +02:00
name = "Canvas Group";
color = COLORS.node_blend_canvas;
2024-06-30 08:52:55 +02:00
icon = THEME.icon_canvas;
2024-06-11 05:06:42 +02:00
2024-06-29 12:31:24 +02:00
timeline_item_group = new timelineItemGroup_Canvas(self);
PROJECT.timelines.addItem(timeline_item_group);
2024-06-11 05:06:42 +02:00
modifiable = false;
2024-08-18 06:16:20 +02:00
newInput( 0, nodeValue_Dimension(self));
2024-06-11 05:06:42 +02:00
2024-08-08 06:57:51 +02:00
custom_input_index = array_length(inputs);
2024-06-29 05:22:20 +02:00
2024-06-30 08:52:55 +02:00
attributes.show_slope_check = true;
array_push(attributeEditors, "Display");
array_push(attributeEditors, [ "Draw Guide", function() { return attributes.show_slope_check; }, new checkBox(function() { attributes.show_slope_check = !attributes.show_slope_check; }) ]);
2024-06-11 11:26:00 +02:00
layers = {};
2024-06-11 05:06:42 +02:00
canvases = [];
composite = noone;
canvas_sel = noone;
2024-06-29 09:37:07 +02:00
frame_renderer_x = 0;
frame_renderer_x_to = 0;
frame_renderer_x_max = 0;
2024-06-11 05:06:42 +02:00
layer_height = 0;
layer_renderer = new Inspector_Custom_Renderer(function(_x, _y, _w, _m, _hover, _focus) {
var _h = ui(4);
if(composite == noone) return _h;
2024-06-11 11:26:00 +02:00
composite.canvas_draw = self;
2024-06-11 05:06:42 +02:00
var _layer_ren = composite.layer_renderer;
_layer_ren.register(layer_renderer.parent);
_layer_ren.rx = layer_renderer.rx;
_layer_ren.ry = layer_renderer.ry;
2024-06-11 11:26:00 +02:00
var _yy = _y + _h;
2024-06-11 05:06:42 +02:00
var bx = _x;
2024-06-11 11:26:00 +02:00
var by = _yy;
2024-06-11 05:06:42 +02:00
var bs = ui(24);
if(buttonInstant(THEME.button_hide, bx, by, bs, bs, _m, _focus, _hover, "", THEME.add_16, 0, COLORS._main_value_positive) == 2)
layerAdd();
2024-06-11 11:26:00 +02:00
_h += bs + ui(8);
_yy += bs + ui(8);
2024-06-11 05:06:42 +02:00
var _wdh = _layer_ren.draw(_x, _yy, _w, _m, _hover, _focus);
if(!is_undefined(_wdh)) _h += _wdh;
return _h;
});
2024-06-11 11:26:00 +02:00
frame_renderer = new Inspector_Custom_Renderer(function(_x, _y, _w, _m, _hover, _focus) {
var _h = 0;
2024-06-11 11:26:00 +02:00
var _yy = _y;
draw_sprite_stretched_ext(THEME.ui_panel_bg, 1, _x, _y, _w, frame_renderer.h, COLORS.node_composite_bg_blend, 1);
2024-06-29 09:37:07 +02:00
var _cnt_hover = _hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + frame_renderer.h);
frame_renderer_x_max = 0;
for (var i = array_length(canvases) - 1; i >= 0; i--) {
2024-06-11 11:26:00 +02:00
var _canvas = canvases[i];
var _frame_render = _canvas.frame_renderer;
_frame_render.register(frame_renderer.parent);
_frame_render.rx = frame_renderer.rx;
_frame_render.ry = frame_renderer.ry;
2024-06-29 09:37:07 +02:00
var _wdh = _frame_render.draw(_x, _yy, _w, _m, _hover, _focus, false, frame_renderer_x);
if(is_undefined(_wdh)) continue;
frame_renderer_x_max = max(frame_renderer_x_max, _frame_render.node.frame_renderer_x_max);
_h += _wdh - ui(2);
_yy += _wdh - ui(2);
}
_h += ui(2);
frame_renderer_x = lerp_float(frame_renderer_x, frame_renderer_x_to, 3);
if(_cnt_hover) {
if(mouse_wheel_down()) frame_renderer_x_to = clamp(frame_renderer_x_to + 80, 0, frame_renderer_x_max);
if(mouse_wheel_up()) frame_renderer_x_to = clamp(frame_renderer_x_to - 80, 0, frame_renderer_x_max);
2024-06-11 11:26:00 +02:00
}
frame_renderer.h = _h;
2024-06-11 11:26:00 +02:00
return _h;
});
2024-06-29 05:22:20 +02:00
group_input_display_list = [ 0,
2024-06-11 05:06:42 +02:00
["Layers", false], layer_renderer,
2024-06-29 12:31:24 +02:00
["Frames", true], frame_renderer,
2024-06-29 05:22:20 +02:00
["Inputs", false],
2024-06-11 05:06:42 +02:00
];
static refreshNodes = function() {
canvases = [];
composite = noone;
for (var i = 0, n = array_length(nodes); i < n; i++) {
var _node = nodes[i];
if(is_instanceof(_node, Node_Canvas))
array_push(canvases, _node);
else if(is_instanceof(_node, Node_Composite))
composite = _node;
2024-06-11 11:26:00 +02:00
_node.modifiable = false;
_node.modify_parent = self;
}
refreshLayer();
}
static refreshLayer = function() {
layers = {};
if(composite == noone) return;
var _amo = composite.getInputAmount();
for(var i = 0; i < _amo; i++) {
var index = composite.input_fix_len + i * composite.data_length;
2024-08-08 06:57:51 +02:00
var _can = composite.inputs[index].value_from;
2024-06-11 11:26:00 +02:00
if(_can == noone) continue;
var _nod = _can.node;
var _lay = _can.node;
var _modStack = [ _nod ];
while(!is_instanceof(_nod, Node_Canvas)) {
2024-08-08 06:57:51 +02:00
if(_nod.inputs[0].type != VALUE_TYPE.surface)
2024-06-11 11:26:00 +02:00
break;
2024-08-08 06:57:51 +02:00
if(_nod.inputs[0].value_from == noone)
2024-06-11 11:26:00 +02:00
break;
2024-08-08 06:57:51 +02:00
_nod = _nod.inputs[0].value_from.node;
2024-06-11 11:26:00 +02:00
array_push(_modStack, _nod);
}
if(!is_instanceof(_nod, Node_Canvas)) continue;
array_pop(_modStack);
layers[$ _lay.node_id] = {
input : _lay,
canvas : _nod,
modifier : _modStack,
};
2024-06-11 05:06:42 +02:00
}
2024-06-11 11:26:00 +02:00
2024-06-11 05:06:42 +02:00
}
2024-06-29 05:22:20 +02:00
static onAdd = function(node) {
2024-06-11 05:06:42 +02:00
node.modifiable = false;
node.modify_parent = self;
2024-06-11 11:26:00 +02:00
2024-06-29 12:31:24 +02:00
if(is_instanceof(node, Node_Canvas)) {
array_push(canvases, node);
node.timeline_item.removeSelf();
timeline_item_group.addItem(node.timeline_item);
} else if(is_instanceof(node, Node_Composite)) {
composite = node;
composite.canvas_group = self;
}
2024-06-11 11:26:00 +02:00
refreshLayer();
2024-06-29 12:31:24 +02:00
onLayerChanged();
2024-06-11 05:06:42 +02:00
}
static layerAdd = function() {
var _l = undefined;
var _b = undefined;
if(array_empty(nodes)) {
_l = x;
_b = y;
}
for (var i = 0, n = array_length(nodes); i < n; i++) {
var _node = nodes[i];
_l = _l == undefined? _node.x : min(_l, _node.x);
_b = _b == undefined? _node.y + _node.h : max(_b, _node.y + _node.h);
}
_b += 32;
var _canvas = nodeBuild("Node_Canvas", _l, _b);
2024-06-29 12:31:24 +02:00
_canvas.setDisplayName($"Layer {array_length(canvases)}");
2024-08-08 06:57:51 +02:00
_canvas.inputs[12].setValue(true);
2024-06-11 11:26:00 +02:00
2024-08-08 06:57:51 +02:00
composite.dummy_input.setFrom(_canvas.outputs[0]);
2024-06-11 05:06:42 +02:00
2024-06-29 05:22:20 +02:00
add(_canvas);
2024-06-11 05:06:42 +02:00
return _canvas;
}
2024-06-11 11:26:00 +02:00
static deleteLayer = function(index) {
if(composite == noone) return;
var idx = composite.input_fix_len + index * composite.data_length;
2024-08-08 06:57:51 +02:00
var inp = composite.inputs[idx];
2024-06-11 11:26:00 +02:00
var nod = inp.value_from? inp.value_from.node : noone;
if(!nod) return;
nod.destroy();
2024-06-29 12:31:24 +02:00
onLayerChanged();
}
static onLayerChanged = function() {
if(composite == noone) return;
var imageAmo = composite.getInputAmount();
var _canvas_tm = [];
var _grp_cont = timeline_item_group.contents;
for(var i = 0; i < imageAmo; i++) {
var _ind = composite.input_fix_len + i * composite.data_length;
2024-08-08 06:57:51 +02:00
var _inp = composite.inputs[_ind];
2024-06-29 12:31:24 +02:00
var _junc = _inp.value_from? _inp.value_from.node : noone;
if(_junc == noone) continue;
if(!struct_has(layers, _junc.node_id)) continue;
var _jun_layer = layers[$ _junc.node_id];
var _junc_canvas = _jun_layer.canvas;
array_remove(_grp_cont, _junc_canvas.timeline_item);
array_insert(_grp_cont, 0, _junc_canvas.timeline_item);
}
2024-06-11 11:26:00 +02:00
}
2024-06-11 05:06:42 +02:00
if(NODE_NEW_MANUAL) {
2024-06-29 05:22:20 +02:00
var _canvas = nodeBuild("Node_Canvas", x - 160, y);
2024-08-08 06:57:51 +02:00
_canvas.inputs[12].setValue(true);
2024-06-29 12:31:24 +02:00
_canvas.setDisplayName($"Background");
2024-06-11 05:06:42 +02:00
2024-06-29 05:22:20 +02:00
var _compose = nodeBuild("Node_Composite", x, y);
2024-08-08 06:57:51 +02:00
_compose.dummy_input.setFrom(_canvas.outputs[0]);
2024-06-11 05:06:42 +02:00
2024-06-29 05:22:20 +02:00
add(_canvas);
add(_compose);
var _output = nodeBuild("Node_Group_Output", x + 160, y, self);
2024-08-08 06:57:51 +02:00
_output.inputs[0].setFrom(_compose.outputs[0]);
2024-06-11 05:06:42 +02:00
}
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
if(canvas_sel) canvas_sel.drawOverlay(hover, active, _x, _y, _s, _mx, _my, _snx, _sny);
}
2024-07-04 13:51:04 +02:00
static drawTools = function(_mx, _my, xx, yy, tool_size, hover, focus) {
if(canvas_sel) return canvas_sel.drawTools(_mx, _my, xx, yy, tool_size, hover, focus);
return 0;
}
2024-06-11 05:06:42 +02:00
static step = function() {
tools = -1;
tool_settings = [];
rightTools = -1;
canvas_sel = noone;
if(composite == noone) return;
if(composite.getInputAmount()) {
var _ind = composite.surface_selecting;
if(_ind == noone)
_ind = composite.input_fix_len;
2024-08-08 06:57:51 +02:00
var _inp = composite.inputs[_ind];
var _can = _inp? _inp.value_from : noone;
2024-06-11 11:26:00 +02:00
if(_can && struct_has(layers, _can.node.node_id))
canvas_sel = layers[$ _can.node.node_id].canvas;
2024-06-11 05:06:42 +02:00
}
if(canvas_sel) {
tools = canvas_sel.tools;
tool_settings = canvas_sel.tool_settings;
rightTools = canvas_sel.rightTools;
}
2024-06-29 12:31:24 +02:00
if(timeline_item_group) {
timeline_item_group.name = getDisplayName();
timeline_item_group.color = getColor();
}
2024-06-30 08:52:55 +02:00
for (var i = 0, n = array_length(canvases); i < n; i++)
2024-06-30 08:52:55 +02:00
canvases[i].attributes.show_slope_check = attributes.show_slope_check;
2024-06-11 05:06:42 +02:00
}
2024-06-11 11:26:00 +02:00
static update = function() {
refreshLayer();
var _dim = getInputData(0);
for (var i = 0, n = array_length(canvases); i < n; i++)
2024-08-08 06:57:51 +02:00
canvases[i].inputs[0].setValue(_dim);
2024-06-11 11:26:00 +02:00
}
static dropPath = function(path) {
if(canvas_sel) canvas_sel.dropPath(path);
}
2024-06-11 05:06:42 +02:00
static getPreviewValues = function() { return composite == noone? noone : composite.getPreviewValues(); }
static postDeserialize = function() {
refreshNodes();
}
2024-06-29 05:22:20 +02:00
sortIO();
2024-06-29 12:31:24 +02:00
}
function timelineItemGroup_Canvas(node = noone) : timelineItemGroup() constructor {
self.node = node;
static onSerialize = function(_map) {
_map.node_id = is_struct(node)? node.node_id : -4;
}
static onDeserialize = function(_map) {
var _node_id = _map.node_id;
if(ds_map_exists(PROJECT.nodeMap, _node_id)) {
node = PROJECT.nodeMap[? _node_id];
node.timeline_item_group = self;
}
}
}