Pixel-Composer/scripts/canvas_tool_selection/canvas_tool_selection.gml

391 lines
11 KiB
Text
Raw Normal View History

2024-04-12 11:45:21 +02:00
function canvas_tool_selection(selector = noone) : canvas_tool() constructor {
self.selector = selector;
selection_surface = surface_create_empty(1, 1);
selection_mask = surface_create_empty(1, 1);
selection_position = [ 0, 0 ];
2024-04-13 14:14:42 +02:00
selection_size = [ 0, 0 ];
2024-04-12 11:45:21 +02:00
is_selecting = false;
is_selected = false;
is_select_drag = false;
selection_sx = 0;
selection_sy = 0;
selection_mx = 0;
selection_my = 0;
mouse_cur_x = 0;
mouse_cur_y = 0;
mouse_pre_x = 0;
mouse_pre_y = 0;
function createSelection(_mask, sel_x0, sel_y0, sel_w, sel_h) {
2024-04-13 14:14:42 +02:00
if(is_selected)
apply();
2024-04-16 10:34:05 +02:00
else {
createNewSelection(_mask, sel_x0, sel_y0, sel_w, sel_h);
return;
}
2024-04-13 14:14:42 +02:00
if(key_mod_press(SHIFT))
modifySelection(_mask, sel_x0, sel_y0, sel_w, sel_h, true);
else if(key_mod_press(ALT))
modifySelection(_mask, sel_x0, sel_y0, sel_w, sel_h, false);
else
createNewSelection(_mask, sel_x0, sel_y0, sel_w, sel_h);
}
2024-04-13 14:14:42 +02:00
function createSelectionFromSurface(surface, sel_x0 = 0, sel_y0 = 0) {
if(!surface_exists(surface)) return;
var sel_w = surface_get_width(surface);
var sel_h = surface_get_height(surface);
selection_surface = surface_verify(selection_surface, sel_w, sel_h);
selection_mask = surface_verify(selection_mask, sel_w, sel_h);
surface_set_target(selection_surface);
DRAW_CLEAR
BLEND_OVERRIDE
draw_surface(surface, 0, 0);
BLEND_NORMAL
surface_reset_target();
surface_set_target(selection_mask);
DRAW_CLEAR
BLEND_OVERRIDE
draw_surface(surface, 0, 0);
BLEND_NORMAL
surface_reset_target();
selection_position = [ sel_x0, sel_y0 ];
selection_size = [ sel_w, sel_h ];
is_selected = true;
}
function modifySelection(_mask, sel_x0, sel_y0, sel_w, sel_h, _add) {
2024-04-13 14:14:42 +02:00
if(sel_w == 1 && sel_h == 1) return;
var _x0, _y0, _x1, _y1;
if(_add) {
_x0 = min(sel_x0, selection_position[0]);
_y0 = min(sel_y0, selection_position[1]);
_x1 = max(sel_x0 + sel_w, selection_position[0] + selection_size[0]);
_y1 = max(sel_y0 + sel_h, selection_position[1] + selection_size[1]);
} else {
var __nx0 = sel_x0;
var __ny0 = sel_y0;
var __nx1 = sel_x0 + sel_w;
var __ny1 = sel_y0 + sel_h;
_x0 = selection_position[0];
_y0 = selection_position[1];
_x1 = selection_position[0] + selection_size[0];
_y1 = selection_position[1] + selection_size[1];
if(__nx0 <= _x0 && __nx1 >= _x1) {
if(__ny0 <= _y0) _y0 = max(_y0, __ny1);
if(__ny1 >= _y1) _y1 = min(_y1, __ny0);
}
if(__ny0 <= _y0 && __ny1 >= _y1) {
if(__nx0 <= _x0) _x0 = max(_x0, __nx1);
if(__nx1 >= _x1) _x1 = min(_x1, __nx0);
}
}
if(_x1 - _x0 <= 0 || _y1 - _y0 <= 0) return;
var _ox = selection_position[0] - _x0;
var _oy = selection_position[1] - _y0;
var _nx = sel_x0 - _x0;
var _ny = sel_y0 - _y0;
var _nw = _x1 - _x0;
var _nh = _y1 - _y0;
2024-04-21 11:52:16 +02:00
var _selection_mask = surface_create(_nw, _nh);
2024-04-13 14:14:42 +02:00
surface_set_target(_selection_mask);
DRAW_CLEAR
BLEND_OVERRIDE
draw_surface_safe(selection_mask, _ox, _oy);
if(_add) BLEND_ADD
else BLEND_SUBTRACT
draw_surface_safe(_mask, _nx, _ny);
BLEND_NORMAL
surface_reset_target();
2024-04-21 11:52:16 +02:00
createNewSelection(_selection_mask, _x0, _y0, _nw, _nh);
surface_free(_selection_mask);
}
2024-04-13 14:14:42 +02:00
function createNewSelection(_mask, sel_x0, sel_y0, sel_w, sel_h) {
2024-04-12 11:45:21 +02:00
if(sel_w == 1 && sel_h == 1) return;
2024-04-13 14:14:42 +02:00
selection_surface = surface_verify(selection_surface, sel_w, sel_h);
selection_mask = surface_verify(selection_mask, sel_w, sel_h);
2024-04-12 11:45:21 +02:00
surface_set_target(selection_surface);
DRAW_CLEAR
draw_surface_safe(_canvas_surface, -sel_x0, -sel_y0);
BLEND_MULTIPLY
draw_surface_safe(_mask, 0, 0);
BLEND_NORMAL
surface_reset_target();
surface_set_target(selection_mask);
DRAW_CLEAR
draw_surface_safe(_mask, 0, 0);
surface_reset_target();
2024-04-13 14:14:42 +02:00
2024-04-12 11:45:21 +02:00
node.storeAction();
surface_set_target(_canvas_surface);
gpu_set_blendmode(bm_subtract);
draw_surface_safe(selection_surface, sel_x0, sel_y0);
gpu_set_blendmode(bm_normal);
surface_reset_target();
node.surface_store_buffer();
selection_position = [ sel_x0, sel_y0 ];
2024-04-13 14:14:42 +02:00
selection_size = [ sel_w, sel_h ];
is_selected = true;
}
2024-04-12 11:45:21 +02:00
2024-07-02 10:51:52 +02:00
function selectAll() {
var sel_w = surface_get_width(_canvas_surface);
var sel_h = surface_get_height(_canvas_surface);
selection_surface = surface_verify(selection_surface, sel_w, sel_h);
selection_mask = surface_verify(selection_mask, sel_w, sel_h);
surface_set_target(selection_surface);
DRAW_CLEAR
draw_surface_safe(_canvas_surface, 0, 0);
surface_reset_target();
surface_set_target(selection_mask);
draw_clear(c_white);
surface_reset_target();
node.storeAction();
surface_set_target(_canvas_surface);
DRAW_CLEAR
surface_reset_target();
node.surface_store_buffer();
selection_position = [ 0, 0 ];
selection_size = [ sel_w, sel_h ];
is_selected = true;
}
function copySelection() {
2024-04-21 11:52:16 +02:00
var s = surface_encode(selection_surface, false);
s.position = selection_position;
clipboard_set_text(json_stringify(s));
}
2024-04-12 11:45:21 +02:00
function apply() {
2024-04-13 07:32:04 +02:00
var _drawLay = node.tool_attribute.drawLayer;
var _sw = surface_get_width(_canvas_surface);
var _sh = surface_get_height(_canvas_surface);
2024-04-13 14:14:42 +02:00
var _selectionSurf = surface_create(_sw, _sh);
var _drawnSurface = surface_create(_sw, _sh);
2024-04-13 07:32:04 +02:00
2024-04-13 14:14:42 +02:00
surface_set_shader(_selectionSurf, noone);
draw_surface(selection_surface, selection_position[0], selection_position[1]);
surface_reset_shader();
surface_set_shader(_drawnSurface, sh_canvas_apply_draw);
shader_set_i("drawLayer", _drawLay);
shader_set_i("eraser", 0);
2024-04-18 09:17:44 +02:00
shader_set_f("channels", 1, 1, 1, 1);
2024-04-13 14:14:42 +02:00
shader_set_f("alpha", 1);
2024-04-13 07:32:04 +02:00
2024-04-13 14:14:42 +02:00
shader_set_surface("back", _canvas_surface);
shader_set_surface("fore", _selectionSurf);
2024-04-13 07:32:04 +02:00
2024-04-13 14:14:42 +02:00
draw_sprite_stretched(s_fx_pixel, 0, 0, 0, _sw, _sh);
surface_reset_shader();
2024-04-13 07:32:04 +02:00
node.setCanvasSurface(_drawnSurface);
surface_free(_canvas_surface);
2024-04-13 14:14:42 +02:00
_canvas_surface = _drawnSurface;
2024-04-13 07:32:04 +02:00
2024-04-12 11:45:21 +02:00
node.surface_store_buffer();
2024-04-13 07:32:04 +02:00
is_selected = false;
}
2024-04-12 11:45:21 +02:00
function onSelected(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
2024-04-12 11:45:21 +02:00
if(!is_surface(selection_surface)) {
is_selected = false;
return;
}
if(is_select_drag) {
var px = selection_sx + (mouse_cur_x - selection_mx);
var py = selection_sy + (mouse_cur_y - selection_my);
selection_position[0] = px;
selection_position[1] = py;
2024-04-13 14:14:42 +02:00
2024-04-12 11:45:21 +02:00
if(mouse_release(mb_left))
is_select_drag = false;
}
if(mouse_press(mb_left, active)) {
var pos_x = selection_position[0];
var pos_y = selection_position[1];
var sel_w = surface_get_width_safe(selection_surface);
var sel_h = surface_get_height_safe(selection_surface);
var _app = true;
2024-04-18 09:17:44 +02:00
if(point_in_rectangle(mouse_cur_x, mouse_cur_y, pos_x, pos_y, pos_x + sel_w, pos_y + sel_h)) {
2024-05-11 04:56:08 +02:00
var _c = surface_getpixel_ext(selection_mask, mouse_cur_x - pos_x, mouse_cur_y - pos_y);
if(_c > 0) {
is_select_drag = true;
selection_sx = pos_x;
selection_sy = pos_y;
selection_mx = mouse_cur_x;
selection_my = mouse_cur_y;
_app = false;
2024-05-11 04:56:08 +02:00
}
2024-04-12 11:45:21 +02:00
}
if(_app && PANEL_PREVIEW.tool_current == noone)
apply();
2024-04-12 11:45:21 +02:00
}
if(key_press(vk_delete)) {
is_selected = false;
surface_free(selection_surface);
2024-04-18 07:12:31 +02:00
} else if(key_press(vk_escape))
2024-04-18 07:12:31 +02:00
apply();
}
2024-04-12 11:45:21 +02:00
function step(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
2024-04-12 11:45:21 +02:00
mouse_cur_x = round((_mx - _x) / _s - 0.5);
mouse_cur_y = round((_my - _y) / _s - 0.5);
if(is_selected) { onSelected(hover, active, _x, _y, _s, _mx, _my, _snx, _sny); return; }
else if(is_surface(selection_surface)) { apply(); }
}
2024-04-12 11:45:21 +02:00
2024-04-14 07:58:38 +02:00
function onDrawMask(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {}
function drawMask(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
2024-04-14 07:58:38 +02:00
var sel_x0, sel_y0;
if(is_selecting) {
sel_x0 = min(selection_sx, mouse_cur_x);
sel_y0 = min(selection_sy, mouse_cur_y);
} else {
sel_x0 = selection_position[0];
sel_y0 = selection_position[1];
2024-04-12 11:45:21 +02:00
}
2024-04-14 07:58:38 +02:00
var _dx = _x + sel_x0 * _s;
var _dy = _y + sel_y0 * _s;
draw_surface_ext_safe(selection_mask, _dx, _dy, _s, _s);
onDrawMask(hover, active, _x, _y, _s, _mx, _my, _snx, _sny);
}
2024-04-12 11:45:21 +02:00
function drawOverlay(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
2024-06-30 08:52:55 +02:00
if(!is_selected) {
if(!is_selecting) {
var x0 = _x + mouse_cur_x * _s;
var y0 = _y + mouse_cur_y * _s;
draw_set_color(c_white);
draw_rectangle(x0, y0, x0 + _s, y0 + _s, true);
}
return;
}
2024-04-12 11:45:21 +02:00
var pos_x = _x + selection_position[0] * _s;
var pos_y = _y + selection_position[1] * _s;
var sel_w = surface_get_width_safe(selection_surface) * _s;
var sel_h = surface_get_height_safe(selection_surface) * _s;
draw_surface_ext_safe(selection_surface, pos_x, pos_y, _s, _s, 0, c_white, 1);
draw_set_color(c_black);
draw_rectangle(pos_x, pos_y, pos_x + sel_w, pos_y + sel_h, true);
draw_set_color(c_white);
draw_rectangle_dashed(pos_x, pos_y, pos_x + sel_w, pos_y + sel_h, true, 6, current_time / 100);
}
2024-06-30 08:52:55 +02:00
2024-04-13 07:32:04 +02:00
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function rotate90cw() {
2024-04-13 07:32:04 +02:00
var _sw = surface_get_width(selection_surface);
var _sh = surface_get_height(selection_surface);
var _newS = surface_create(_sh, _sw);
surface_set_shader(_newS, noone);
draw_surface_ext(selection_surface, _sh, 0, 1, 1, -90, c_white, 1);
surface_reset_shader();
surface_free(selection_surface);
selection_surface = _newS;
}
2024-04-13 07:32:04 +02:00
function rotate90ccw() {
2024-04-13 07:32:04 +02:00
var _sw = surface_get_width(selection_surface);
var _sh = surface_get_height(selection_surface);
var _newS = surface_create(_sh, _sw);
surface_set_shader(_newS, noone);
draw_surface_ext(selection_surface, 0, _sw, 1, 1, 90, c_white, 1);
surface_reset_shader();
surface_free(selection_surface);
selection_surface = _newS;
}
2024-04-13 07:32:04 +02:00
function flipH() {
2024-04-13 07:32:04 +02:00
var _sw = surface_get_width(selection_surface);
var _sh = surface_get_height(selection_surface);
var _newS = surface_create(_sw, _sh);
surface_set_shader(_newS, noone);
draw_surface_ext(selection_surface, _sw, 0, -1, 1, 0, c_white, 1);
surface_reset_shader();
surface_free(selection_surface);
selection_surface = _newS;
}
2024-04-13 07:32:04 +02:00
function flipV() {
2024-04-13 07:32:04 +02:00
var _sw = surface_get_width(selection_surface);
var _sh = surface_get_height(selection_surface);
var _newS = surface_create(_sw, _sh);
surface_set_shader(_newS, noone);
draw_surface_ext(selection_surface, 0, _sh, 1, -1, 0, c_white, 1);
surface_reset_shader();
surface_free(selection_surface);
selection_surface = _newS;
}
2024-04-12 11:45:21 +02:00
}