Pixel-Composer/scripts/node_global/node_global.gml

298 lines
9.3 KiB
Text
Raw Normal View History

2025-02-03 05:28:15 +01:00
#region data
global.GLOBALVAR_TYPES = [ VALUE_TYPE.integer, VALUE_TYPE.float, VALUE_TYPE.boolean, VALUE_TYPE.color, VALUE_TYPE.gradient, VALUE_TYPE.path, VALUE_TYPE.curve, VALUE_TYPE.text ];
global.GLOBALVAR_TYPES_NAME = [ "Integer", "Float", "Boolean", "Color", "Gradient", "Path", "Curve", "Text" ];
global.GLOBALVAR_DISPLAY = [
2023-03-07 14:29:47 +01:00
/*Integer*/ [ "Default", "Range", "Rotation", "Rotation range", "Slider", "Slider range", "Padding", "Vector2", "Vector3", "Vector4", "Vector range", "Vector2 range", "Area" ],
/*Float*/ [ "Default", "Range", "Rotation", "Rotation range", "Slider", "Slider range", "Padding", "Vector2", "Vector3", "Vector4", "Vector range", "Vector2 range", "Area" ],
2023-02-14 05:32:32 +01:00
/*Boolean*/ [ "Default" ],
/*Color*/ [ "Default", "Palette" ],
/*Gradient*/[ "Default" ],
/*Path*/ [ "Read", "Write", "Font" ],
2023-02-14 05:32:32 +01:00
/*Curve*/ [ "Default", ],
/*Text*/ [ "Default", ],
2025-02-03 05:28:15 +01:00
];
2023-02-14 05:32:32 +01:00
2025-02-03 05:28:15 +01:00
global.GLOBALVAR_DISPLAY_MAP = {}
global.GLOBALVAR_DISPLAY_MAP[$ "Default"] = VALUE_DISPLAY._default;
global.GLOBALVAR_DISPLAY_MAP[$ "Range"] = VALUE_DISPLAY.range;
global.GLOBALVAR_DISPLAY_MAP[$ "Rotation"] = VALUE_DISPLAY.rotation;
global.GLOBALVAR_DISPLAY_MAP[$ "Rotation range"] = VALUE_DISPLAY.rotation_range;
global.GLOBALVAR_DISPLAY_MAP[$ "Slider"] = VALUE_DISPLAY.slider;
global.GLOBALVAR_DISPLAY_MAP[$ "Slider range"] = VALUE_DISPLAY.slider_range;
global.GLOBALVAR_DISPLAY_MAP[$ "Padding"] = VALUE_DISPLAY.padding;
global.GLOBALVAR_DISPLAY_MAP[$ "Vector2"] = VALUE_DISPLAY.vector;
global.GLOBALVAR_DISPLAY_MAP[$ "Vector3"] = VALUE_DISPLAY.vector;
global.GLOBALVAR_DISPLAY_MAP[$ "Vector4"] = VALUE_DISPLAY.vector;
global.GLOBALVAR_DISPLAY_MAP[$ "Vector range"] = VALUE_DISPLAY.vector_range;
global.GLOBALVAR_DISPLAY_MAP[$ "Vector2 range"] = VALUE_DISPLAY.vector_range;
global.GLOBALVAR_DISPLAY_MAP[$ "Area"] = VALUE_DISPLAY.area;
global.GLOBALVAR_DISPLAY_MAP[$ "Palette"] = VALUE_DISPLAY.palette;
global.GLOBALVAR_DISPLAY_MAP[$ "Read"] = VALUE_DISPLAY.path_load;
global.GLOBALVAR_DISPLAY_MAP[$ "Write"] = VALUE_DISPLAY.path_save;
global.GLOBALVAR_DISPLAY_MAP[$ "Font"] = VALUE_DISPLAY.path_font;
#endregion
function variable_editor(nodeVal) constructor {
value = nodeVal;
tb_name = new textBox(TEXTBOX_INPUT.text, function(s) /*=>*/ {
2025-01-30 06:21:41 +01:00
if(string_pos(" ", s)) { noti_warning("Global variable name can't have space."); return; }
var _node = value.node;
for( var i = 0, n = array_length(_node.inputs); i < n; i++ ) {
var _in = _node.inputs[i];
if(_in == value) continue;
if(_in.name == s) {
noti_warning("Duplicate variable name.");
return;
}
}
2025-01-30 06:21:41 +01:00
value.name = s;
2023-10-02 10:45:30 +02:00
RENDER_ALL
2025-02-03 05:28:15 +01:00
}).setHide(1).setSlide(false);
2023-03-08 07:35:51 +01:00
2025-02-03 05:28:15 +01:00
vb_range = new vectorBox(2, function(v, i) /*=>*/ { slider_range[i] = v; refreshInput(); }).setLinkable(false);
2025-01-30 06:21:41 +01:00
tb_step = new textBox(TEXTBOX_INPUT.number, function(v) /*=>*/ { slider_step = v; refreshInput(); });
2023-02-14 05:32:32 +01:00
2025-02-03 05:28:15 +01:00
sc_type = new scrollBox(global.GLOBALVAR_TYPES_NAME, function(v) /*=>*/ {
sc_disp.data_list = global.GLOBALVAR_DISPLAY[v];
2025-01-30 06:21:41 +01:00
type_index = v;
2023-03-07 14:29:47 +01:00
disp_index = 0;
refreshInput();
2023-10-02 10:45:30 +02:00
RENDER_ALL
2025-02-03 05:28:15 +01:00
}).setTextColor(CDEF.main_mdwhite).setUpdateHover(false);
sc_disp = new scrollBox(global.GLOBALVAR_DISPLAY[0], function(v) /*=>*/ {
2025-01-30 06:21:41 +01:00
disp_index = v;
2023-03-07 14:29:47 +01:00
refreshInput();
2023-10-02 10:45:30 +02:00
RENDER_ALL
2025-02-03 05:28:15 +01:00
}).setTextColor(CDEF.main_mdwhite).setUpdateHover(false);
2023-03-07 14:29:47 +01:00
2025-02-03 05:28:15 +01:00
type_index = 0;
2023-03-07 14:29:47 +01:00
_type_index = 0;
2025-02-03 05:28:15 +01:00
disp_index = 0;
2023-03-07 14:29:47 +01:00
_disp_index = 0;
2023-03-08 07:35:51 +01:00
slider_range = [ 0, 1 ];
slider_step = 0.01;
2025-01-30 06:21:41 +01:00
static setFont = function(_f) {
tb_name.setFont(_f);
sc_type.setFont(_f);
sc_disp.setFont(_f);
vb_range.setFont(_f);
tb_step.setFont(_f);
return self;
}
static refreshInput = function() {
2025-02-03 05:28:15 +01:00
value.setType(global.GLOBALVAR_TYPES[type_index]);
2023-03-07 14:29:47 +01:00
if(_type_index != type_index || _disp_index != disp_index) {
switch(value.type) {
case VALUE_TYPE.integer :
case VALUE_TYPE.float :
switch(sc_disp.data_list[disp_index]) {
case "Vector2" :
2024-03-14 14:35:19 +01:00
case "Range" :
2023-03-07 14:29:47 +01:00
case "Vector range" :
case "Slider range" :
2025-01-30 06:21:41 +01:00
case "Rotation range" : value.setValue([0, 0]); break;
case "Vector3" : value.setValue([0, 0, 0]); break;
2023-03-07 14:29:47 +01:00
case "Vector4" :
2023-09-26 14:35:25 +02:00
case "Vector2 range" :
2025-01-30 06:21:41 +01:00
case "Padding" : value.setValue([0, 0, 0, 0]); break;
case "Area" : value.setValue([0, 0, 0, 0, 0]); break;
default : value.setValue(0);
2023-03-07 14:29:47 +01:00
}
break;
2025-01-30 06:21:41 +01:00
2023-03-07 14:29:47 +01:00
case VALUE_TYPE.color :
switch(sc_disp.data_list[disp_index]) {
2025-01-30 06:21:41 +01:00
case "Palette" : value.setValue([0]); break;
default : value.setValue(0); break;
2023-03-07 14:29:47 +01:00
}
break;
2025-01-30 06:21:41 +01:00
case VALUE_TYPE.gradient : value.setValue(new gradientObject(c_black)); break;
case VALUE_TYPE.boolean : value.setValue(false); break;
2023-03-07 14:29:47 +01:00
case VALUE_TYPE.text :
2025-01-30 06:21:41 +01:00
case VALUE_TYPE.path : value.setValue(""); break;
case VALUE_TYPE.curve : value.setValue(CURVE_DEF_01); break;
2023-03-07 14:29:47 +01:00
}
}
2024-03-14 14:35:19 +01:00
_type_index = type_index;
_disp_index = disp_index;
2025-02-03 05:28:15 +01:00
var _dtype = global.GLOBALVAR_DISPLAY_MAP[$ sc_disp.data_list[disp_index]];
2024-03-14 14:35:19 +01:00
2023-03-07 14:29:47 +01:00
switch(sc_disp.data_list[disp_index]) {
2025-02-03 05:28:15 +01:00
case "Slider" :
case "Slider range" :
value.setDisplay(_dtype, { range: [slider_range[0], slider_range[1], slider_step] }); break;
case "Read" :
case "Write" :
value.setDisplay(_dtype, { filter: "" }); break;
2023-03-07 14:29:47 +01:00
2025-02-03 05:28:15 +01:00
default : value.setDisplay(_dtype); break;
2023-02-14 05:32:32 +01:00
}
}
2023-03-08 07:35:51 +01:00
2025-02-03 05:28:15 +01:00
static updateType = function() {
type_index = array_find(global.GLOBALVAR_TYPES, value.type);
disp_index = 0;
sc_disp.data_list = global.GLOBALVAR_DISPLAY[type_index];
var _disp = value.display_type;
var _disK = struct_find_key(global.GLOBALVAR_DISPLAY_MAP, _disp);
if(_disK == undefined) return self;
disp_index = array_find(sc_disp.data_list, _disK);
return self;
}
2025-01-30 06:21:41 +01:00
static draw = function(_x, _y, _w, _m, _focus, _hover, viewMode) {
2023-03-08 07:35:51 +01:00
var _h = 0;
2025-01-30 06:21:41 +01:00
var _font = viewMode == INSP_VIEW_MODE.spacious? f_p0 : f_p2;
2023-03-08 07:35:51 +01:00
2025-01-30 06:21:41 +01:00
var _wd_h = viewMode == INSP_VIEW_MODE.spacious? ui(32) : ui(24);
var _pd_h = viewMode == INSP_VIEW_MODE.spacious? ui(4) : ui(2)
2023-03-08 07:35:51 +01:00
switch(sc_disp.data_list[disp_index]) {
case "Slider" :
case "Slider range" :
2025-01-30 06:21:41 +01:00
if(viewMode == INSP_VIEW_MODE.compact) { _h += ui(2); _y += ui(2); }
2023-03-08 07:35:51 +01:00
2023-06-21 20:36:53 +02:00
vb_range.setFocusHover(_focus, _hover);
tb_step.setFocusHover(_focus, _hover);
2023-03-08 07:35:51 +01:00
2025-01-30 06:21:41 +01:00
vb_range.axis = [ __txt("min"), __txt("max") ];
tb_step.label = __txt("step");
2023-03-08 07:35:51 +01:00
2025-01-30 06:21:41 +01:00
var stw = _w / 3;
var _wx = _x;
var _ww = _w - (stw + ui(4));
vb_range.draw(_wx, _y, _ww, _wd_h, slider_range, noone, _m);
tb_step.draw(_x + _w - stw, _y, stw, _wd_h, slider_step , _m);
2023-03-08 07:35:51 +01:00
2025-01-30 06:21:41 +01:00
_h += _wd_h + ui(2);
_y += _wd_h + ui(2);
2023-03-08 07:35:51 +01:00
break;
}
return _h;
}
2024-03-14 14:35:19 +01:00
}
2023-02-14 05:32:32 +01:00
2023-03-07 14:29:47 +01:00
function Node_Global(_x = 0, _y = 0) : __Node_Base(_x, _y) constructor {
2025-02-03 05:28:15 +01:00
name = "GLOBAL";
2023-05-03 21:42:17 +02:00
display_name = "";
2022-01-13 05:24:03 +01:00
2025-02-03 05:28:15 +01:00
node_id = 0;
group = noone;
2023-10-02 14:41:44 +02:00
use_cache = CACHE_USE.none;
value = ds_map_create();
2022-01-13 05:24:03 +01:00
input_display_list = -1;
2025-02-03 05:28:15 +01:00
anim_priority = -999;
2022-01-13 05:24:03 +01:00
static isActiveDynamic = function(frame = CURRENT_FRAME) { return true; }
2024-02-12 10:25:23 +01:00
static valueUpdate = function(index) { RENDER_ALL }
2023-03-08 07:35:51 +01:00
static createValue = function() {
2025-02-03 05:28:15 +01:00
var _key = $"NewValue{array_length(inputs)}";
var _in = nodeValue_Float(_key, self, 0);
2023-05-03 21:42:17 +02:00
_in.editor = new variable_editor(_in);
2024-08-08 06:57:51 +02:00
array_push(inputs, _in);
2023-03-07 14:29:47 +01:00
return _in;
}
2023-03-07 14:29:47 +01:00
static inputExist = function(key) { return ds_map_exists(value, key); }
2023-03-08 07:35:51 +01:00
static inputGetable = function(from, key) {
2023-03-08 07:35:51 +01:00
if(!inputExist(key)) return false;
var to = value[? key];
if(!typeCompatible(from.type, to.type)) return false;
if(typeIncompatible(from, to)) return false;
2023-03-08 07:35:51 +01:00
return true;
}
2023-03-08 07:35:51 +01:00
static getInputKey = function(key, def = noone) {
2023-03-07 14:29:47 +01:00
if(!ds_map_exists(value, key)) return def;
return value[? key];
}
2023-02-14 05:32:32 +01:00
static step = function() {
2025-02-03 05:28:15 +01:00
ds_map_clear(value);
2024-08-08 06:57:51 +02:00
for( var i = 0; i < array_length(inputs); i++ ) {
var _inp = inputs[i];
2023-04-21 19:08:10 +02:00
value[? _inp.name] = _inp;
2025-01-30 06:21:41 +01:00
var val = true;
2023-04-21 19:08:10 +02:00
if(string_pos(" ", _inp.name)) val = false;
_inp.editor.tb_name.boxColor = val? c_white : COLORS._main_value_negative;
2023-02-14 05:32:32 +01:00
}
}
2022-01-13 05:24:03 +01:00
static serialize = function() {
2023-06-13 14:42:06 +02:00
var _map = {};
2023-03-07 14:29:47 +01:00
2023-06-13 14:42:06 +02:00
var _inputs = [];
2024-08-08 06:57:51 +02:00
for(var i = 0; i < array_length(inputs); i++) {
var _ser = inputs[i].serialize();
2023-03-07 14:29:47 +01:00
2024-08-08 06:57:51 +02:00
_ser.global_type = inputs[i].editor.type_index;
_ser.global_disp = inputs[i].editor.disp_index;
_ser.global_s_range = inputs[i].editor.slider_range;
_ser.global_s_step = inputs[i].editor.slider_step;
2023-03-07 14:29:47 +01:00
2023-06-13 14:42:06 +02:00
array_push(_inputs, _ser);
2022-01-13 05:24:03 +01:00
}
2023-03-07 14:29:47 +01:00
2023-06-13 14:42:06 +02:00
_map.inputs = _inputs;
_map.attri = attributes;
2022-01-13 05:24:03 +01:00
return _map;
}
2022-01-13 05:24:03 +01:00
static deserialize = function(_map) {
2023-06-13 14:42:06 +02:00
var _inputs = _map.inputs;
2022-01-13 05:24:03 +01:00
2023-06-13 14:42:06 +02:00
for(var i = 0; i < array_length(_inputs); i++) {
2024-02-12 10:25:23 +01:00
var _des = _inputs[i];
var _in = createValue();
2023-03-08 07:35:51 +01:00
2023-06-13 14:42:06 +02:00
_in.editor.type_index = struct_try_get(_des, "global_type", 0);
_in.editor.disp_index = struct_try_get(_des, "global_disp", 0);
_in.editor.disp_index = struct_try_get(_des, "global_disp", 0);
2023-03-08 07:35:51 +01:00
2023-06-13 14:42:06 +02:00
_in.editor.slider_range = _des.global_s_range;
_in.editor.slider_step = struct_try_get(_des, "global_s_step", 0.01);
2023-03-08 07:35:51 +01:00
_in.editor.refreshInput();
_in.applyDeserialize(_des);
2022-01-13 05:24:03 +01:00
}
2023-03-08 07:35:51 +01:00
if(struct_has(_map, "attr")) struct_override(attributes, _map.attr);
2023-03-08 07:35:51 +01:00
step();
}
2022-01-13 05:24:03 +01:00
}