Pixel-Composer/scripts/canvas_tool_node/canvas_tool_node.gml

213 lines
5.3 KiB
Plaintext
Raw Normal View History

2024-04-13 14:14:42 +02:00
function canvas_tool_node(canvas, node) : canvas_tool() constructor {
self.canvas = canvas;
self.node = node;
2024-04-21 11:52:16 +02:00
override = true;
2024-07-02 12:18:32 +02:00
panel = noone;
2024-04-13 14:14:42 +02:00
2024-04-21 11:52:16 +02:00
applySelection = false;
2024-04-13 14:14:42 +02:00
static destroy = function() {
2024-04-14 07:58:38 +02:00
if(applySelection) canvas.tool_selection.apply();
2024-04-16 10:34:05 +02:00
cleanUp();
}
static cleanUp = function() {
UNDO_HOLDING = true;
2024-04-16 10:34:05 +02:00
surface_free_safe(targetSurface);
surface_free_safe(maskedSurface);
2024-04-18 07:12:31 +02:00
if(is_struct(nodeObject)) {
if(is_instanceof(nodeObject, Node))
nodeObject.destroy();
2024-04-18 07:12:31 +02:00
else {
var keys = struct_get_names(nodeObject);
for (var i = 0, n = array_length(keys); i < n; i++)
if(is_instanceof(nodeObject[$ keys[i]], Node))
nodeObject[$ keys[i]].destroy();
}
}
2024-07-02 12:18:32 +02:00
if(panel) panel.remove();
node.nodeTool = noone;
UNDO_HOLDING = false;
2024-04-13 14:14:42 +02:00
}
2024-04-21 11:52:16 +02:00
function init() {
applySelection = canvas.tool_selection.is_selected;
destiSurface = applySelection? canvas.tool_selection.selection_surface : canvas.getCanvasSurface();
if(!is_surface(destiSurface))
return noone;
sw = surface_get_width(destiSurface);
sh = surface_get_height(destiSurface);
targetSurface = surface_create(sw, sh);
maskedSurface = surface_create(sw, sh);
surface_set_shader(targetSurface, noone);
draw_surface_safe(destiSurface);
surface_reset_shader();
nodeObject = node.build(0, 0);
if(nodeObject == noone || !is_instanceof(nodeObject, Node)) {
noti_warning("Not tools only allows a single node.");
destroy();
return noone;
2024-04-13 14:14:42 +02:00
}
2024-04-21 11:52:16 +02:00
inputJunction = noone;
outputJunction = noone;
2024-07-02 13:25:59 +02:00
setColor = true;
2024-04-21 11:52:16 +02:00
for( var i = 0, n = ds_list_size(nodeObject.inputs); i < n; i++ ) {
var _in = nodeObject.inputs[| i];
2024-07-02 13:25:59 +02:00
if(_in.type == VALUE_TYPE.surface || _in.name == "Dimension")
2024-04-21 11:52:16 +02:00
inputJunction = _in;
2024-07-02 13:25:59 +02:00
if(_in.type == VALUE_TYPE.color && setColor) {
_in.setValue(CURRENT_COLOR);
setColor = false;
2024-04-21 11:52:16 +02:00
}
2024-07-02 13:25:59 +02:00
2024-04-13 14:14:42 +02:00
}
2024-04-21 11:52:16 +02:00
for( var i = 0, n = ds_list_size(nodeObject.outputs); i < n; i++ ) {
var _in = nodeObject.outputs[| i];
if(_in.type == VALUE_TYPE.surface) {
outputJunction = _in;
break;
}
}
if(outputJunction == noone) {
noti_warning("Selected node has no surface output.");
destroy();
return noone;
}
2024-07-02 12:18:32 +02:00
panel = panelAdd("Panel_Inspector", true, false);
panel.content.setInspecting(nodeObject, true, false);
panel.destroy_on_click_out = false;
2024-07-02 10:51:52 +02:00
2024-04-21 11:52:16 +02:00
return self;
2024-04-13 14:14:42 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
function apply() {
2024-04-16 10:34:05 +02:00
var _surf = surface_create(sw, sh);
var _repl = key_mod_press(SHIFT);
2024-04-13 14:14:42 +02:00
if(applySelection) {
var _fore = canvas.tool_selection.selection_surface;
2024-04-16 10:34:05 +02:00
if(_repl) {
surface_set_shader(_surf, noone);
draw_surface_safe(maskedSurface);
surface_reset_shader();
2024-04-16 10:34:05 +02:00
} else {
surface_set_shader(_surf, sh_blend_normal);
shader_set_surface("fore", maskedSurface);
shader_set_f("dimension", 1, 1);
shader_set_f("opacity", 1);
draw_surface_safe(_fore);
surface_reset_shader();
}
2024-04-16 10:34:05 +02:00
surface_free(_fore);
2024-04-16 10:34:05 +02:00
canvas.tool_selection.selection_surface = _surf;
2024-04-13 14:14:42 +02:00
canvas.tool_selection.apply();
} else {
var _fore = canvas.getCanvasSurface();
2024-04-13 14:14:42 +02:00
canvas.storeAction();
2024-04-16 10:34:05 +02:00
if(_repl) {
surface_set_shader(_surf, noone);
draw_surface_safe(maskedSurface);
surface_reset_shader();
2024-04-16 10:34:05 +02:00
} else {
surface_set_shader(_surf, sh_blend_normal);
shader_set_surface("fore", maskedSurface);
shader_set_f("dimension", 1, 1);
shader_set_f("opacity", 1);
draw_surface_safe(_fore);
surface_reset_shader();
}
2024-04-16 10:34:05 +02:00
canvas.setCanvasSurface(_surf);
2024-04-13 14:14:42 +02:00
canvas.surface_store_buffer();
}
PANEL_PREVIEW.tool_current = noone;
2024-04-16 10:34:05 +02:00
cleanUp();
2024-04-13 14:14:42 +02:00
}
function step(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
var _px, _py, _pw, _ph;
if(applySelection) {
_px = canvas.tool_selection.selection_position[0];
_py = canvas.tool_selection.selection_position[1];
_pw = canvas.tool_selection.selection_size[0];
_ph = canvas.tool_selection.selection_size[1];
} else {
_px = 0;
_py = 0;
_pw = canvas.attributes.dimension[0];
_ph = canvas.attributes.dimension[1];
}
var _dx = _x + _px * _s;
var _dy = _y + _py * _s;
if(inputJunction) {
if(inputJunction.type == VALUE_TYPE.surface)
inputJunction.setValue(targetSurface);
else if(inputJunction.name == "Dimension")
inputJunction.setValue([ sw, sh ]);
}
2024-04-16 10:34:05 +02:00
if(is_instanceof(nodeObject, Node_Collection))
RenderList(nodeObject.nodes);
else
nodeObject.update();
2024-04-13 14:14:42 +02:00
var _surf = outputJunction.getValue();
if(applySelection) {
maskedSurface = surface_verify(maskedSurface, sw, sh);
surface_set_shader(maskedSurface);
draw_surface_safe(_surf);
2024-04-13 14:14:42 +02:00
BLEND_MULTIPLY
draw_surface_safe(canvas.tool_selection.selection_mask);
2024-04-13 14:14:42 +02:00
BLEND_NORMAL
surface_reset_shader();
} else
maskedSurface = _surf;
if(!key_mod_press(SHIFT))
draw_surface_ext_safe(destiSurface, _dx, _dy, _s, _s);
2024-04-13 14:14:42 +02:00
draw_surface_ext_safe(maskedSurface, _dx, _dy, _s, _s);
2024-07-02 12:18:32 +02:00
var hov = nodeObject.drawOverlay(hover, active, _x, _y, _s, _mx, _my, _snx, _sny);
if(is_undefined(hov) || !hov) {
if(mouse_press(mb_left, active)) { apply(); MOUSE_BLOCK = true; }
else if(mouse_press(mb_right, active)) { destroy(); MOUSE_BLOCK = true; }
}
2024-04-13 14:14:42 +02:00
}
}