Pixel-Composer/scripts/node_number/node_number.gml

238 lines
7.4 KiB
Text
Raw Normal View History

2024-04-24 03:46:53 +02:00
function Node_Number(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-12-03 05:02:04 +01:00
name = "Number";
2022-11-18 03:20:31 +01:00
color = COLORS.node_blend_number;
2022-01-13 05:24:03 +01:00
2024-03-28 14:18:02 +01:00
setDimension(96, 32 + 24 * 1);
2022-01-13 05:24:03 +01:00
2024-03-27 11:51:14 +01:00
wd_slider = slider(0, 1, 0.01, function(val) { inputs[| 0].setValue(val); } );
2023-03-05 07:16:44 +01:00
wd_slider.spr = THEME.node_slider;
2023-10-16 07:25:22 +02:00
slider_value = -1;
slider_surface = -1;
slider_dragging = false;
slider_mx = 0;
slider_sx = 0;
slider_m = 0;
rotator_surface = -1;
rotator_dragging = false;
rotator_s = 0;
rotator_p = 0;
rotator_m = 0;
2023-03-05 07:16:44 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 0] = nodeValue("Value", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0)
2022-01-19 03:05:13 +01:00
.setVisible(true, true);
2022-01-13 05:24:03 +01:00
2023-03-05 07:16:44 +01:00
inputs[| 1] = nodeValue("Integer", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false);
inputs[| 2] = nodeValue("Display", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
.setDisplay(VALUE_DISPLAY.enum_scroll, { data: [ "Number", "Slider", "Rotator" ], update_hover: false });
2023-03-05 07:16:44 +01:00
inputs[| 3] = nodeValue("Range", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 1 ])
.setDisplay(VALUE_DISPLAY.range);
2023-03-05 07:16:44 +01:00
inputs[| 4] = nodeValue("Step", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0.01)
2024-04-24 03:46:53 +02:00
inputs[| 5] = nodeValue("Clamp to range", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true);
2023-02-14 05:32:32 +01:00
outputs[| 0] = nodeValue("Number", self, JUNCTION_CONNECT.output, VALUE_TYPE.float, 0);
2022-01-13 05:24:03 +01:00
2024-03-14 14:35:19 +01:00
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) { #region
2023-10-08 13:20:31 +02:00
var __ax = getInputData(0);
2023-01-25 06:49:00 +01:00
if(is_array(__ax)) return;
2024-03-14 14:35:19 +01:00
inputs[| 0].drawOverlay(hover, active, _x, _y, _s, _mx, _my, _snx, _sny);
2023-10-16 07:25:22 +02:00
} #endregion
2023-01-01 02:06:02 +01:00
2023-10-16 07:25:22 +02:00
static step = function() { #region
2023-10-08 13:20:31 +02:00
var int = getInputData(1);
var disp = getInputData(2);
2023-03-05 07:16:44 +01:00
2024-03-28 14:18:02 +01:00
if(!show_parameter) setDimension(96, 56, false);
2023-10-12 14:14:08 +02:00
2023-03-05 07:16:44 +01:00
switch(disp) {
case 0 :
inputs[| 3].setVisible(false);
inputs[| 4].setVisible(false);
inputs[| 5].setVisible(false);
2023-03-05 07:16:44 +01:00
break;
case 1 :
if(inputs[| 0].value_from == noone && !show_parameter) setDimension(160, 96, false);
2024-03-28 14:18:02 +01:00
2023-03-05 07:16:44 +01:00
inputs[| 3].setVisible(true);
inputs[| 4].setVisible(true);
inputs[| 5].setVisible(true);
2023-03-05 07:16:44 +01:00
break;
case 2 :
if(inputs[| 0].value_from == noone && !show_parameter) setDimension(128, 128, false);
2024-03-28 14:18:02 +01:00
2023-03-05 07:16:44 +01:00
inputs[| 3].setVisible(false);
inputs[| 4].setVisible(false);
inputs[| 5].setVisible(false);
2023-03-05 07:16:44 +01:00
break;
}
for( var i = 0; i < 1; i++ ) {
2023-10-07 16:23:40 +02:00
inputs[| i].setType(int? VALUE_TYPE.integer : VALUE_TYPE.float);
inputs[| i].editWidget.setSlidable(int? 0.1 : 0.01);
2023-03-05 07:16:44 +01:00
}
2023-10-07 16:23:40 +02:00
outputs[| 0].setType(int? VALUE_TYPE.integer : VALUE_TYPE.float);
2023-10-16 07:25:22 +02:00
} #endregion
2023-03-05 07:16:44 +01:00
2024-04-24 03:46:53 +02:00
static processNumber = function(_val, _int) {
if(is_string(_val)) return _int? round(toNumber(_val)) : toNumber(_val);
if(is_numeric(_val)) return _int? round(_val) : _val;
2023-11-26 13:16:38 +01:00
2024-04-24 03:46:53 +02:00
if(is_array(_val)) {
for (var i = 0, n = array_length(_val); i < n; i++)
_val[i] = processNumber(_val[i], _int);
}
2023-12-05 04:28:49 +01:00
2024-04-24 03:46:53 +02:00
return _val;
}
static update = function() { #region
var _dat = getInputData(0);
var _int = getInputData(1);
var _res = processNumber(_dat, _int);
outputs[| 0].setValue(_res);
2023-10-16 07:25:22 +02:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-10-16 07:25:22 +02:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) { #region
2023-01-04 02:30:04 +01:00
var bbox = drawGetBbox(xx, yy, _s);
2023-10-16 07:25:22 +02:00
var val = getInputData(0);
2023-10-18 14:58:55 +02:00
var _int = getInputData(1);
2023-10-08 13:20:31 +02:00
var disp = getInputData(2);
var rang = getInputData(3);
var stp = getInputData(4);
var cmp = getInputData(5);
2023-10-16 07:25:22 +02:00
var _col = getColor();
2023-03-05 07:16:44 +01:00
2024-03-26 04:03:45 +01:00
if(disp == 0 || inputs[| 0].value_from != noone || bbox.h < line_get_height(f_p2)) {
draw_set_text(f_sdf, fa_center, fa_center, COLORS._main_text);
2023-12-15 12:56:36 +01:00
draw_text_bbox(bbox, string(val));
2023-03-05 07:16:44 +01:00
return;
2024-03-26 04:03:45 +01:00
}
2023-03-05 07:16:44 +01:00
switch(disp) {
2024-03-28 14:18:02 +01:00
2023-10-16 07:25:22 +02:00
case 1 : #region
draw_set_text(f_sdf, fa_center, fa_center, _col);
2024-04-24 03:46:53 +02:00
draw_text_transformed(bbox.xc, bbox.y0 + 16 * _s, string(_int? round(val) : val), _s * 0.5, _s * 0.5, 0);
2023-10-16 07:25:22 +02:00
var sl_w = bbox.w - 8 * _s;
var sl_h = _s * 40;
var sl_x0 = bbox.x0 + 4 * _s;
var sl_x1 = sl_x0 + sl_w;
var sl_y0 = (bbox.y0 + (24 * _s) + bbox.y1) / 2 - sl_h / 2;
var sl_y1 = sl_y0 + sl_h;
var c0 = (draggable && !slider_dragging)? colorMultiply(CDEF.main_grey, _col) : colorMultiply(CDEF.main_white, _col);
var c1 = colorMultiply(CDEF.main_dkgrey, _col);
2023-03-05 07:16:44 +01:00
2023-10-16 07:25:22 +02:00
var _minn = rang[0];
var _maxx = rang[1];
slider_surface = surface_verify(slider_surface, sl_w, sl_h);
slider_value = slider_value == -1? val : lerp_float(slider_value, val, 2.5);
2023-03-05 07:16:44 +01:00
2023-10-16 07:25:22 +02:00
surface_set_shader(slider_surface, sh_ui_slider);
shader_set_color("c0", c0);
shader_set_color("c1", c1);
shader_set_dim("dimension", slider_surface);
shader_set_f("mouseProg", animation_curve_eval(ac_ripple, slider_m));
shader_set_f("prog", clamp((slider_value - _minn) / (_maxx - _minn), 0.1, 0.9));
draw_sprite_stretched(s_fx_pixel, 0, 0, 0, sl_w, sl_h);
surface_reset_shader();
2023-03-05 07:16:44 +01:00
2023-10-16 07:25:22 +02:00
draw_surface(slider_surface, sl_x0, sl_y0);
if(slider_dragging) {
slider_m = lerp_float(slider_m, 1, 4);
var _valM = (_mx - sl_x0) / (sl_x1 - sl_x0);
var _valL = lerp(_minn, _maxx, _valM);
_valL = value_snap(_valL, stp);
if(cmp) _valL = clamp(_valL, _minn, _maxx);
2023-10-16 07:25:22 +02:00
2023-11-20 05:10:55 +01:00
if(inputs[| 0].setValue(_valL))
UNDO_HOLDING = true;
if(mouse_release(mb_left)) {
2023-10-16 07:25:22 +02:00
slider_dragging = false;
2023-11-20 05:10:55 +01:00
UNDO_HOLDING = false;
}
2023-10-16 07:25:22 +02:00
} else
slider_m = lerp_float(slider_m, 0, 5);
if(_hover && point_in_rectangle(_mx, _my, sl_x0, sl_y0, sl_x1, sl_y1)) {
if(mouse_press(mb_left, _focus) && is_real(val)) {
slider_dragging = true;
slider_mx = _mx;
slider_sx = val;
}
draggable = false;
2023-03-05 07:16:44 +01:00
}
2023-10-16 07:25:22 +02:00
break; #endregion
2024-04-24 03:46:53 +02:00
2023-10-16 07:25:22 +02:00
case 2 : #region
var _ss = min(bbox.w, bbox.h);
var c0 = (draggable && !rotator_dragging)? colorMultiply(CDEF.main_grey, _col) : colorMultiply(CDEF.main_white, _col);
var c1 = colorMultiply(CDEF.main_dkgrey, _col);
var _dst = point_distance(_mx, _my, bbox.xc, bbox.yc);
var _x0 = bbox.xc - _ss / 2;
var _y0 = bbox.yc - _ss / 2;
rotator_surface = surface_verify(rotator_surface, _ss, _ss);
surface_set_shader(rotator_surface, sh_ui_rotator);
shader_set_color("c0", c0);
shader_set_color("c1", c1);
shader_set_f("angle", degtorad(val));
shader_set_f("mouse", (_mx - _x0) / _ss, (_my - _y0) / _ss);
shader_set_f("mouseProg", animation_curve_eval(ac_ripple, rotator_m));
draw_sprite_stretched(s_fx_pixel, 0, 0, 0, _ss, _ss);
surface_reset_shader();
draw_surface(rotator_surface, _x0, _y0);
if(rotator_dragging) {
rotator_m = lerp_float(rotator_m, 1, 4);
var dir = point_direction(bbox.xc, bbox.yc, _mx, _my);
var dx = angle_difference(dir, rotator_p);
rotator_p = dir;
2023-11-20 05:10:55 +01:00
if(inputs[| 0].setValue(val + dx))
UNDO_HOLDING = true;
2023-10-16 07:25:22 +02:00
2023-11-20 05:10:55 +01:00
if(mouse_release(mb_left)) {
2023-10-16 07:25:22 +02:00
rotator_dragging = false;
2023-11-20 05:10:55 +01:00
UNDO_HOLDING = false;
}
2023-10-16 07:25:22 +02:00
} else
rotator_m = lerp_float(rotator_m, 0, 5);
if(_hover && point_in_circle(_mx, _my, bbox.xc, bbox.yc, _ss / 2)) {
if(mouse_press(mb_left, _focus) && is_real(val)) {
rotator_dragging = true;
rotator_s = val;
rotator_p = point_direction(bbox.xc, bbox.yc, _mx, _my);
}
draggable = false;
}
draw_set_text(f_sdf, fa_center, fa_center, colorMultiply(CDEF.main_white, _col));
2024-04-24 03:46:53 +02:00
draw_text_transformed(bbox.xc, bbox.yc, _int? string(round(val)) : string_format(val, -1, 2), _s * .5, _s * .5, 0);
2023-10-16 07:25:22 +02:00
break; #endregion
2023-03-05 07:16:44 +01:00
}
2023-10-16 07:25:22 +02:00
} #endregion
2024-03-28 14:18:02 +01:00
}