Pixel-Composer/scripts/transformBox/transformBox.gml

168 lines
5.3 KiB
Text
Raw Normal View History

2023-06-21 20:36:53 +02:00
enum TRANSFORM {
pos_x,
pos_y,
rot,
sca_x,
sca_y
}
function transformBox(_onModify) : widget() constructor {
onModify = _onModify;
onModifySingle[TRANSFORM.pos_x] = function(val) { onModify(val, TRANSFORM.pos_x); }
onModifySingle[TRANSFORM.pos_y] = function(val) { onModify(val, TRANSFORM.pos_y); }
onModifySingle[TRANSFORM.rot ] = function(val) { onModify(val, TRANSFORM.rot ); } //unused
onModifySingle[TRANSFORM.sca_x] = function(val) { onModify(val, TRANSFORM.sca_x); }
onModifySingle[TRANSFORM.sca_y] = function(val) { onModify(val, TRANSFORM.sca_y); }
2023-06-21 20:36:53 +02:00
rot = new rotator(function(val) { onModify(val, TRANSFORM.rot); });
2024-03-26 04:03:45 +01:00
labels = [ "x", "y", "rot", "sx", "sy" ];
2023-06-23 15:39:24 +02:00
2023-06-21 20:36:53 +02:00
for(var i = 0; i < 5; i++) {
tb[i] = new textBox(TEXTBOX_INPUT.number, onModifySingle[i]);
tb[i].slidable = true;
2024-03-26 04:03:45 +01:00
tb[i].label = labels[i];
2023-06-21 20:36:53 +02:00
}
2024-11-29 06:18:28 +01:00
rot.tb_value.label = "rot";
static setInteract = function(interactable = noone) {
2023-06-21 20:36:53 +02:00
self.interactable = interactable;
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(tb); i < n; i++ )
2023-06-23 15:39:24 +02:00
tb[i].setInteract(interactable);
rot.setInteract(interactable);
}
2023-06-21 20:36:53 +02:00
static register = function(parent = noone) {
2023-06-23 15:39:24 +02:00
tb[TRANSFORM.pos_x].register(parent);
tb[TRANSFORM.pos_y].register(parent);
rot.register(parent);
tb[TRANSFORM.sca_x].register(parent);
tb[TRANSFORM.sca_y].register(parent);
2023-06-21 20:36:53 +02:00
}
static isHovering = function() {
2024-03-28 14:18:02 +01:00
for( var i = 0, n = array_length(tb); i < n; i++ ) if(tb[i].isHovering()) return true;
2024-03-29 05:20:49 +01:00
return hovering;
}
2024-03-28 14:18:02 +01:00
static drawParam = function(params) {
2024-03-27 11:51:14 +01:00
setParam(params);
2024-11-27 05:32:43 +01:00
rot.setParam(params);
rot.tb_value.setParam(params);
2024-03-27 11:51:14 +01:00
for(var i = 0; i < 5; i++) tb[i].setParam(params);
2024-03-26 04:03:45 +01:00
return draw(params.x, params.y, params.w, params.h, params.data, params.m);
}
2023-06-21 20:36:53 +02:00
static draw = function(_x, _y, _w, _h, _data, _m) {
2023-06-21 20:36:53 +02:00
x = _x;
y = _y;
w = _w;
2024-03-26 04:03:45 +01:00
h = _h * 3 + ui(4) * 2;
2023-06-21 20:36:53 +02:00
2023-08-24 11:59:05 +02:00
if(!is_array(_data)) return 0;
if(array_empty(_data)) return 0;
if(is_array(_data[0])) return 0;
2024-03-29 05:20:49 +01:00
hovering = point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + _h);
2023-06-23 15:39:24 +02:00
rot.setFocusHover(active, hover);
for(var i = 0; i < array_length(_data); i++) {
2023-06-21 20:36:53 +02:00
tb[i].setFocusHover(active, hover);
tb[i].hide = true;
}
2023-06-21 20:36:53 +02:00
2024-11-29 06:18:28 +01:00
if(true) { // new
h = _h;
var _spc = ui(4);
var _tbw = (w - _spc / 2) / array_length(_data);
var _tbh = _h;
var _tbx = _x;
draw_sprite_stretched_ext(THEME.textbox, 3, _tbx, _y, _tbw * 2, _tbh, boxColor, 1);
draw_sprite_stretched_ext(THEME.textbox, 0, _tbx, _y, _tbw * 2, _tbh, boxColor, 0.5 + 0.5 * interactable);
tb[TRANSFORM.pos_x].draw(_tbx, _y, _tbw, _tbh, _data[TRANSFORM.pos_x], _m);
tb[TRANSFORM.pos_y].draw(_tbx + _tbw, _y, _tbw, _tbh, _data[TRANSFORM.pos_y], _m);
_tbx += _tbw * 2 + _spc;
rot.draw(_tbx, _y, _tbw, _tbh, _data[TRANSFORM.rot], _m);
_tbx += _tbw + _spc;
if(array_length(_data) == 4) {
draw_sprite_stretched_ext(THEME.textbox, 3, _tbx, _y, _tbw, _tbh, boxColor, 1);
draw_sprite_stretched_ext(THEME.textbox, 0, _tbx, _y, _tbw, _tbh, boxColor, 0.5 + 0.5 * interactable);
tb[TRANSFORM.sca_x].draw(_tbx, _y, _tbw, _tbh, _data[TRANSFORM.sca_x], _m);
} else if(array_length(_data) == 5) {
draw_sprite_stretched_ext(THEME.textbox, 3, _tbx, _y, _tbw * 2, _tbh, boxColor, 1);
draw_sprite_stretched_ext(THEME.textbox, 0, _tbx, _y, _tbw * 2, _tbh, boxColor, 0.5 + 0.5 * interactable);
tb[TRANSFORM.sca_x].draw(_tbx, _y, _tbw, _tbh, _data[TRANSFORM.sca_x], _m);
tb[TRANSFORM.sca_y].draw(_tbx + _tbw, _y, _tbw, _tbh, _data[TRANSFORM.sca_y], _m);
}
} else {
h = _h * 3 + ui(4) * 2;
var _lab = _w > ui(160);
draw_set_text(font, fa_left, fa_center, CDEF.main_dkgrey);
var lbw = _lab? string_width(__txt("Position")) + ui(8) : 0;
var tbw = (_w - lbw) / 2;
var tbh = _h;
var tbx = _x + lbw;
if(_lab) draw_text_add(_x, _y + tbh / 2, __txt("Position"));
draw_sprite_stretched_ext(THEME.textbox, 3, tbx, _y, _w - lbw, tbh, boxColor, 1);
draw_sprite_stretched_ext(THEME.textbox, 0, tbx, _y, _w - lbw, tbh, boxColor, 0.5 + 0.5 * interactable);
tb[TRANSFORM.pos_x].draw(tbx, _y, tbw, tbh, _data[TRANSFORM.pos_x], _m);
tb[TRANSFORM.pos_y].draw(tbx + tbw, _y, tbw, tbh, _data[TRANSFORM.pos_y], _m);
_y += tbh + ui(4);
draw_set_text(font, fa_left, fa_center, CDEF.main_dkgrey);
if(_lab) draw_text_add(_x, _y + tbh / 2, __txt("Rotation"));
rot.draw(tbx, _y, _w - lbw, tbh, _data[TRANSFORM.rot], _m);
_y += tbh + ui(4);
draw_set_text(font, fa_left, fa_center, CDEF.main_dkgrey);
if(_lab) draw_text_add(_x, _y + tbh / 2, __txt("Scale"));
draw_sprite_stretched_ext(THEME.textbox, 3, tbx, _y, _w - lbw, tbh, boxColor, 1);
draw_sprite_stretched_ext(THEME.textbox, 0, tbx, _y, _w - lbw, tbh, boxColor, 0.5 + 0.5 * interactable);
tbw = array_length(_data) > 4? (_w - lbw) / 2 : _w - lbw;
tb[TRANSFORM.sca_x].draw(tbx, _y, tbw, tbh, _data[TRANSFORM.sca_x], _m);
if(array_length(_data) > 4)
tb[TRANSFORM.sca_y].draw(tbx + tbw, _y, tbw, tbh, _data[TRANSFORM.sca_y], _m);
}
2023-06-21 20:36:53 +02:00
resetFocus();
return h;
}
2024-03-31 11:10:14 +02:00
static clone = function() {
2024-03-31 11:10:14 +02:00
var cln = new transformBox(onModify);
return cln;
}
2024-11-24 11:39:17 +01:00
static free = function() {
for( var i = 0, n = array_length(tb); i < n; i++ ) tb[i].free();
}
2023-06-21 20:36:53 +02:00
}