Pixel-Composer/scripts/curveBox/curveBox.gml

97 lines
2.4 KiB
Plaintext
Raw Normal View History

2022-01-14 02:44:58 +01:00
function curveBox(_onModify) constructor {
2022-01-13 05:24:03 +01:00
onModify = _onModify;
active = false;
hover = false;
node_dragging = -1;
drag_range = 0;
drag_max = 0;
2022-01-24 02:21:25 +01:00
static get_y = function(val, _y, _h, y_max, y_range) {
2022-01-13 05:24:03 +01:00
return _y + _h * clamp((y_max - val) / y_range, 0, 1);
}
2022-01-24 02:21:25 +01:00
static draw = function(_x, _y, _w, _h, _data, _m) {
2022-01-13 05:24:03 +01:00
static curve_amo = 3;
2022-12-10 05:06:01 +01:00
var curve_h = _h;
2022-01-13 05:24:03 +01:00
#region curve
var _range;
2022-01-14 02:44:58 +01:00
_range = bezier_range(_data[0], _data[1], _data[2], _data[3]);
2022-01-13 05:24:03 +01:00
var y_min = min(0, _range[0]);
var y_max = max(1, _range[1]);
var y_range = y_max - y_min;
var _y_0 = get_y(0, _y, curve_h, y_max, y_range);
var _y_1 = get_y(1, _y, curve_h, y_max, y_range);
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS.widget_curve_outline);
draw_set_alpha(0.5);
2022-01-13 05:24:03 +01:00
draw_line(_x + _w / 3, _y, _x + _w / 3, _y + curve_h);
draw_line(_x + _w / 3 * 2, _y, _x + _w / 3 * 2, _y + curve_h);
2022-11-18 03:20:31 +01:00
2022-01-13 05:24:03 +01:00
draw_line(_x, _y_0, _x + _w, _y_0);
draw_line(_x, _y_1, _x + _w, _y_1);
2022-11-18 03:20:31 +01:00
draw_set_alpha(1);
2022-01-13 05:24:03 +01:00
draw_rectangle(_x, _y, _x + _w, _y + curve_h, true);
if(node_dragging != -1) {
var _my = -((_m[1] - _y) / curve_h * drag_range - drag_max);
_data[node_dragging] = _my;
2022-12-10 05:06:01 +01:00
if(mouse_release(mb_left)) {
2022-01-13 05:24:03 +01:00
onModify(_data);
node_dragging = -1;
}
}
var _y0 = _data[0];
var _y1 = _data[1];
var _y2 = _data[2];
var _y3 = _data[3];
var _dy = _y + (y_max - 1) / y_range * curve_h;
var _dh = -curve_h / y_range;
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS.widget_curve_line);
2022-01-14 02:44:58 +01:00
draw_line_bezier_cubic(_x, _dy, _w, _dh, _y0, _y1, _y2, _y3);
2022-01-13 05:24:03 +01:00
var node_hovering = -1;
for(var i = 0; i < 4; i++) {
var _nx = i / 3 * _w + _x;
var _ny = get_y(_data[i], _y, curve_h, y_max, y_range);
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS.widget_curve_outline);
2022-01-13 05:24:03 +01:00
draw_circle(_nx, _ny, 3, false);
if(hover && point_in_circle(_m[0], _m[1], _nx, _ny, 6)) {
draw_circle(_nx, _ny, 5, false);
node_hovering = i;
}
}
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, active)) {
if(node_hovering != -1) {
node_dragging = node_hovering;
drag_range = y_range;
drag_max = y_max;
}
} else if(mouse_press(mb_right, active)) {
switch(node_hovering) {
case 0 : _data[0] = 0; break;
case 1 : _data[1] = 0; break;
case 2 : _data[2] = 1; break;
case 3 : _data[3] = 1; break;
}
onModify(_data);
}
2022-01-13 05:24:03 +01:00
#endregion
active = false;
hover = false;
}
}