Pixel-Composer/scripts/node_tunnel_in/node_tunnel_in.gml

287 lines
7.6 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Tunnel_In(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-12-03 05:02:04 +01:00
name = "Tunnel In";
2023-01-25 06:49:00 +01:00
color = COLORS.node_blend_tunnel;
2023-12-04 12:09:31 +01:00
is_group_io = true;
preview_draw = false;
2023-01-25 06:49:00 +01:00
2024-04-22 09:56:37 +02:00
setDimension(32, 32);
isHovering = false;
hover_scale = 0;
hover_scale_to = 0;
hover_alpha = 0;
2023-01-25 06:49:00 +01:00
2024-04-22 14:26:08 +02:00
preview_connecting = false;
preview_scale = 1;
junction_hover = false;
2024-04-22 14:26:08 +02:00
var tname = "";
2024-09-13 08:25:00 +02:00
if(!LOADING && !APPENDING) tname = $"tunnel{ds_map_size(project.tunnels_in_map)}";
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_Text("Name", self, tname ))
2023-02-14 05:32:32 +01:00
.rejectArray();
2023-01-25 06:49:00 +01:00
2024-08-20 10:15:53 +02:00
newInput(1, nodeValue("Value in", self, CONNECT_TYPE.input, VALUE_TYPE.any, noone ))
2023-01-25 06:49:00 +01:00
.setVisible(true, true);
error_notification = noone;
2024-11-26 05:52:57 +01:00
setTrigger(2, "Create tunnel out", [ THEME.tunnel, 0, c_white ]);
2023-03-02 07:59:14 +01:00
static onInspector2Update = function() {
var _node = nodeBuild("Node_Tunnel_Out", x + 128, y).skipDefault();
var _key = inputs[0].getValue();
_node.inputs[0].setValue(_key);
}
2023-03-02 07:59:14 +01:00
2024-11-07 07:59:04 +01:00
static update = function(frame = CURRENT_FRAME) {
onValueUpdate();
}
2023-01-25 06:49:00 +01:00
static resetMap = function() {
2024-08-08 06:57:51 +02:00
var _key = inputs[0].getValue();
2024-09-13 08:25:00 +02:00
project.tunnels_in_map[? node_id] = _key;
project.tunnels_in[? _key] = inputs[1];
}
resetMap();
2023-01-25 06:49:00 +01:00
static checkDuplicate = function() {
2024-08-08 06:57:51 +02:00
var _key = inputs[0].getValue();
2024-09-13 08:25:00 +02:00
var amo = ds_map_size(project.tunnels_in_map);
var k = ds_map_find_first(project.tunnels_in_map);
2024-04-22 08:08:37 +02:00
var dup = false;
2023-01-25 06:49:00 +01:00
repeat(amo) {
2024-09-13 08:25:00 +02:00
if(k != node_id && project.tunnels_in_map[? k] == _key)
2023-01-25 06:49:00 +01:00
dup = true;
2024-09-13 08:25:00 +02:00
k = ds_map_find_next(project.tunnels_in_map, k);
2023-01-25 06:49:00 +01:00
}
if(dup && error_notification == noone) {
error_notification = noti_error($"Duplicated key: {_key}");
2023-01-25 06:49:00 +01:00
error_notification.onClick = function() { PANEL_GRAPH.focusNode(self); };
} else if(!dup && error_notification) {
noti_remove(error_notification);
error_notification = noone;
}
}
2023-01-25 06:49:00 +01:00
static onValueUpdate = function(index = -1) {
2024-08-08 06:57:51 +02:00
var _key = inputs[0].getValue();
2023-01-25 06:49:00 +01:00
resetMap();
2024-09-13 08:25:00 +02:00
var amo = ds_map_size(project.tunnels_in_map);
var k = ds_map_find_first(project.tunnels_in_map);
2023-01-25 06:49:00 +01:00
repeat(amo) {
var _n = project.nodeMap[? k];
2024-09-24 11:51:04 +02:00
k = ds_map_find_next(project.tunnels_in_map, k);
if(!is_instanceof(_n, Node_Tunnel_In)) continue;
if(!_n.active) continue;
_n.resetMap();
2023-01-25 06:49:00 +01:00
}
2024-09-13 08:25:00 +02:00
var k = ds_map_find_first(project.tunnels_in_map);
2023-01-25 06:49:00 +01:00
repeat(amo) {
var _n = project.nodeMap[? k];
2024-09-24 11:51:04 +02:00
k = ds_map_find_next(project.tunnels_in_map, k);
if(!is_instanceof(_n, Node_Tunnel_In)) continue;
if(!_n.active) continue;
2024-09-24 11:51:04 +02:00
_n.checkDuplicate();
2023-01-25 06:49:00 +01:00
}
2023-03-05 07:16:44 +01:00
2023-10-07 16:23:40 +02:00
if(index == 0) { RENDER_ALL_REORDER }
}
2023-01-25 06:49:00 +01:00
2024-11-07 07:59:04 +01:00
static onValueFromUpdate = function(index) {
var _frm = inputs[1].value_from;
2023-01-25 06:49:00 +01:00
2024-11-07 07:59:04 +01:00
inputs[1].setType( _frm? _frm.type : VALUE_TYPE.any);
inputs[1].setDisplay(_frm? _frm.display_type : VALUE_DISPLAY._default);
2023-01-25 06:49:00 +01:00
2024-11-07 07:59:04 +01:00
}
static step = function() {
value_validation[VALIDATION.error] = error_notification != noone;
}
2023-01-25 06:49:00 +01:00
static getNextNodes = function() {
var nodes = [];
2023-06-17 18:59:20 +02:00
var nodeNames = [];
var _key = inputs[0].getValue();
var amo = ds_map_size(project.tunnels_out);
var k = ds_map_find_first(project.tunnels_out);
2023-01-25 06:49:00 +01:00
2023-06-17 18:59:20 +02:00
LOG_BLOCK_START();
LOG_IF(global.FLAG.render == 1, $"→→→→→ Call get next node from: {INAME}");
2023-06-17 18:59:20 +02:00
2023-01-25 06:49:00 +01:00
repeat(amo) {
2024-09-13 08:25:00 +02:00
if(project.tunnels_out[? k] == _key) {
2023-07-06 19:49:16 +02:00
array_push(nodes, PROJECT.nodeMap[? k]);
array_push(nodeNames, PROJECT.nodeMap[? k].internalName);
2023-06-17 18:59:20 +02:00
}
2023-01-25 06:49:00 +01:00
2024-09-13 08:25:00 +02:00
k = ds_map_find_next(project.tunnels_out, k);
2023-01-25 06:49:00 +01:00
}
2023-03-28 06:58:28 +02:00
LOG_IF(global.FLAG.render == 1, $"→→ Push {nodeNames} to queue.");
2023-06-17 18:59:20 +02:00
LOG_BLOCK_END();
2023-03-28 06:58:28 +02:00
return nodes;
}
2023-01-25 06:49:00 +01:00
2024-04-22 09:56:37 +02:00
/////////////////////////////////////////////////////////////////////////////
static pointIn = function(_x, _y, _mx, _my, _s) {
2024-04-22 09:56:37 +02:00
var xx = x * _s + _x;
var yy = y * _s + _y;
return point_in_circle(_mx, _my, xx, yy, _s * 24);
}
2024-04-22 09:56:37 +02:00
static preDraw = function(_x, _y, _s) {
2024-04-22 09:56:37 +02:00
var xx = x * _s + _x;
var yy = y * _s + _y;
2024-08-08 06:57:51 +02:00
inputs[0].x = xx;
inputs[0].y = yy;
2024-04-22 09:56:37 +02:00
2024-08-08 06:57:51 +02:00
inputs[1].x = xx;
inputs[1].y = yy;
}
2024-04-22 09:56:37 +02:00
static drawBadge = function(_x, _y, _s) {}
static drawJunctionNames = function(_x, _y, _mx, _my, _s) {}
static onDrawNodeBehind = function(_x, _y, _mx, _my, _s) {
2023-12-04 12:09:31 +01:00
var xx = _x + x * _s;
var yy = _y + y * _s;
var hover = isHovering || hover_alpha == 1;
2023-12-04 12:09:31 +01:00
var tun = findPanel("Panel_Tunnels");
hover |= tun && tun.tunnel_hover == self;
if(!hover) return;
2024-08-08 06:57:51 +02:00
var _key = inputs[0].getValue();
2024-09-13 08:25:00 +02:00
var _keys = ds_map_keys_to_array(project.tunnels_out);
2024-04-22 09:56:37 +02:00
2024-08-08 06:57:51 +02:00
draw_set_color(inputs[1].color_display);
draw_set_alpha(0.5);
for (var i = 0, n = array_length(_keys); i < n; i++) {
var _k = _keys[i];
2023-12-04 12:09:31 +01:00
2024-09-13 08:25:00 +02:00
if(project.tunnels_out[? _k] != _key) continue;
if(!ds_map_exists(PROJECT.nodeMap, _k)) continue;
var node = PROJECT.nodeMap[? _k];
if(node.group != group) continue;
2024-04-22 14:26:08 +02:00
preview_connecting = true;
node.preview_connecting = true;
var tox = _x + node.x * _s;
var toy = _y + node.y * _s;
draw_line_dotted(xx, yy, tox, toy, 2 * _s, current_time / 10, 3);
2023-12-04 12:09:31 +01:00
}
2024-04-22 09:56:37 +02:00
draw_set_alpha(1);
}
2024-04-22 09:56:37 +02:00
2024-11-24 06:07:17 +01:00
static drawJunctions = function(_draw, _x, _y, _mx, _my, _s) {
2024-04-22 09:56:37 +02:00
var xx = x * _s + _x;
var yy = y * _s + _y;
isHovering = point_in_circle(_mx, _my, xx, yy, _s * 24);
2024-11-24 06:07:17 +01:00
junction_hover = inputs[1].drawJunction(_draw, _s, _mx, _my);
2024-04-22 09:56:37 +02:00
if(!isHovering) return noone;
if(!junction_hover) draw_sprite_ext(THEME.view_pan, 0, _mx + ui(16), _my + ui(24), 1, 1, 0, COLORS._main_accent);
2024-04-22 09:56:37 +02:00
hover_scale_to = 1;
return junction_hover? inputs[1] : noone;
}
2023-12-04 12:09:31 +01:00
2024-11-24 06:07:17 +01:00
static drawNode = function(_draw, _x, _y, _mx, _my, _s) {
if(!_draw) return drawJunctions(_draw, _x, _y, _mx, _my, _s);
2024-04-22 09:56:37 +02:00
var xx = x * _s + _x;
var yy = y * _s + _y;
hover_alpha = 0.5;
if(active_draw_index > -1) {
hover_alpha = 1;
hover_scale_to = 1;
active_draw_index = -1;
}
2024-04-22 14:26:08 +02:00
#region draw arc
var prev_s = preview_connecting? 1 + sin(current_time / 100) * 0.1 : 1;
preview_scale = lerp_float(preview_scale, prev_s, 5);
2024-04-22 14:26:08 +02:00
preview_connecting = false;
2024-04-22 09:56:37 +02:00
2024-04-22 14:26:08 +02:00
shader_set(sh_node_arc);
2024-08-08 06:57:51 +02:00
shader_set_color("color", inputs[1].color_display, hover_alpha);
2024-04-22 14:26:08 +02:00
shader_set_f("angle", degtorad(90));
var _r = preview_scale * _s * 20;
shader_set_f("amount", 0.4, 0.5);
draw_sprite_stretched(s_fx_pixel, 0, xx - _r, yy - _r, _r * 2, _r * 2);
var _r = preview_scale * _s * 30;
shader_set_f("amount", 0.45, 0.525);
draw_sprite_stretched(s_fx_pixel, 0, xx - _r, yy - _r, _r * 2, _r * 2);
var _r = preview_scale * _s * 40;
shader_set_f("amount", 0.475, 0.55);
draw_sprite_stretched(s_fx_pixel, 0, xx - _r, yy - _r, _r * 2, _r * 2);
shader_reset();
#endregion
2024-04-22 09:56:37 +02:00
if(hover_scale > 0) {
var _r = hover_scale * _s * 16;
shader_set(sh_node_circle);
shader_set_color("color", COLORS._main_accent, hover_alpha);
draw_sprite_stretched(s_fx_pixel, 0, xx - _r, yy - _r, _r * 2, _r * 2);
shader_reset();
}
hover_scale = lerp_float(hover_scale, hover_scale_to && !junction_hover, 3);
2024-04-22 09:56:37 +02:00
hover_scale_to = 0;
draw_set_text(f_sdf, fa_center, fa_bottom, COLORS._main_text);
2024-08-08 06:57:51 +02:00
draw_text_transformed(xx, yy - 12 * _s, string(inputs[0].getValue()), _s * .3, _s * .3, 0);
2023-01-25 06:49:00 +01:00
2024-11-24 06:07:17 +01:00
return drawJunctions(_draw, _x, _y, _mx, _my, _s);
}
2023-02-14 05:32:32 +01:00
2023-10-07 16:23:40 +02:00
static onClone = function() { onValueUpdate(0); }
2024-11-07 07:59:04 +01:00
static postConnect = function() {
onValueUpdate(0);
onValueFromUpdate(0);
}
2023-02-14 05:32:32 +01:00
static onDestroy = function() {
2023-02-14 05:32:32 +01:00
if(error_notification != noone)
noti_remove(error_notification);
var _key = inputs[0].getValue();
ds_map_delete(project.tunnels_in_map, node_id);
ds_map_delete(project.tunnels_in, _key);
}
static onRestore = function() {
resetMap();
}
2023-01-25 06:49:00 +01:00
}