Pixel-Composer/scripts/curveBox/curveBox.gml
2023-02-14 08:51:14 +07:00

191 lines
5.2 KiB
Plaintext

function curveBox(_onModify) : widget() constructor {
onModify = _onModify;
curve_surface = surface_create(1, 1);
node_dragging = -1;
node_drag_typ = -1;
static get_x = function(val, _x, _w) { return _x + _w * clamp( val, 0, 1); }
static get_y = function(val, _y, _h) { return _y + _h * clamp(1 - val, 0, 1); }
static register = function() {}
static draw = function(_x, _y, _w, _h, _data, _m) {
x = _x; y = _y;
w = _w; h = _h;
curve_surface = surface_verify(curve_surface, _w, _h);
var points = array_length(_data) / 6;
draw_set_color(COLORS.widget_curve_outline);
draw_rectangle(_x, _y, _x + _w, _y + _h, true);
if(node_dragging != -1) {
if(node_drag_typ == 0) {
var node_point = (node_dragging - 2) / 6;
if(node_point > 0 && node_point < points - 1) {
var _mx = (_m[0] - _x) / _w;
_mx = clamp(_mx, 0, 1);
var bfx = _data[node_dragging - 6];
var afx = _data[node_dragging + 6];
if(_mx == bfx) node_dragging -= 6;
else if(_mx == afx) node_dragging += 6;
else _data[node_dragging + 0] = _mx;
}
var _my = 1 - (_m[1] - _y) / _h;
_my = clamp(_my, 0, 1);
_data[node_dragging + 1] = _my;
//sort by x
var _xindex = [];
var _pindex = [];
for( var i = 0; i < points; i++ ) {
var ind = i * 6;
var _x0 = _data[ind + 2];
array_push(_xindex, _x0);
array_push(_pindex, _x0);
}
array_sort(_xindex, true);
if(node_point > 0 && node_point < points - 1) {
var sorted = [];
for( var i = 0; i < points; i++ ) {
var prog = _xindex[i];
var ind = array_find(_pindex, prog);
array_push(sorted, _data[ind * 6 + 0]);
array_push(sorted, _data[ind * 6 + 1]);
array_push(sorted, _data[ind * 6 + 2]);
array_push(sorted, _data[ind * 6 + 3]);
array_push(sorted, _data[ind * 6 + 4]);
array_push(sorted, _data[ind * 6 + 5]);
}
if(onModify(sorted))
UNDO_HOLDING = true;
} else if(onModify(_data))
UNDO_HOLDING = true;
} else {
var _px = _data[node_dragging + 0];
var _py = _data[node_dragging + 1];
var _mx = (_m[0] - _x) / _w;
_mx = clamp(_mx, 0, 1);
_data[node_dragging - 2] = (_px - _mx) * node_drag_typ;
_data[node_dragging + 2] = (_mx - _px) * node_drag_typ;
var _my = 1 - (_m[1] - _y) / _h;
_my = clamp(_my, 0, 1);
_data[node_dragging - 1] = (_py - _my) * node_drag_typ;
_data[node_dragging + 3] = (_my - _py) * node_drag_typ;
if(onModify(_data))
UNDO_HOLDING = true;
}
if(mouse_release(mb_left)) {
node_dragging = -1;
node_drag_typ = -1;
UNDO_HOLDING = false;
}
}
var node_hovering = -1;
var node_hover_typ = -1;
var point_insert = 1;
var _x1 = 0;
for( var i = 0; i < points; i++ ) {
var ind = i * 6;
var _x0 = _data[ind + 2];
var _y0 = _data[ind + 3];
var bx0 = _x0 + _data[ind + 0];
var by0 = _y0 + _data[ind + 1];
var ax0 = _x0 + _data[ind + 4];
var ay0 = _y0 + _data[ind + 5];
bx0 = get_x(bx0, _x, _w);
by0 = get_y(by0, _y, _h);
_x0 = get_x(_x0, _x, _w);
_y0 = get_y(_y0, _y, _h);
ax0 = get_x(ax0, _x, _w);
ay0 = get_y(ay0, _y, _h);
draw_set_color(COLORS.widget_curve_line);
if(i > 0) { //draw pre line
draw_line(bx0, by0, _x0, _y0);
draw_circle(bx0, by0, 3, false);
if(hover && point_in_circle(_m[0], _m[1], bx0, by0, 10)) {
draw_circle(bx0, by0, 5, false);
node_hovering = ind + 2;
node_hover_typ = -1;
}
}
if(i < points - 1) { //draw post line
draw_line(ax0, ay0, _x0, _y0);
draw_circle(ax0, ay0, 3, false);
if(hover && point_in_circle(_m[0], _m[1], ax0, ay0, 10)) {
draw_circle(ax0, ay0, 5, false);
node_hovering = ind + 2;
node_hover_typ = 1;
}
}
draw_set_color(COLORS._main_accent);
draw_circle(_x0, _y0, 3, false);
if(hover && point_in_circle(_m[0], _m[1], _x0, _y0, 10)) {
draw_circle(_x0, _y0, 5, false);
node_hovering = ind + 2;
node_hover_typ = 0;
}
if(_m[0] >= _x1 && _m[0] <= _x0)
point_insert = i;
_x1 = _x0;
}
draw_set_color(COLORS._main_accent);
draw_curve(_x, _y, _w, -_h, _data);
if(hover && point_in_rectangle(_m[0], _m[1], _x - ui(5), _y - ui(5), _x + _w + ui(5), _y + _h + ui(5))) {
if(mouse_press(mb_left, active)) {
if(node_hovering == -1) {
var _ind = point_insert * 6;
var _px = (_m[0] - _x) / _w;
var _py = 1 - (_m[1] - _y) / _h;
array_insert(_data, _ind + 0, -0.1);
array_insert(_data, _ind + 1, 0);
array_insert(_data, _ind + 2, _px);
array_insert(_data, _ind + 3, _py);
array_insert(_data, _ind + 4, 0.1);
array_insert(_data, _ind + 5, 0);
if(onModify(_data))
UNDO_HOLDING = true;
node_dragging = _ind + 2;
node_drag_typ = 0;
} else {
node_dragging = node_hovering;
node_drag_typ = node_hover_typ;
}
} else if(mouse_press(mb_right, active)) {
var node_point = (node_hovering - 2) / 6;
if(node_hover_typ == 0 && node_point > 0 && node_point < points - 1) {
array_delete(_data, node_point * 6, 6);
if(onModify(_data))
UNDO_HOLDING = true;
}
}
}
resetFocus();
}
}