Pixel-Composer/scripts/canvas_freeform/canvas_freeform.gml

73 lines
2.0 KiB
Plaintext
Raw Normal View History

2024-04-12 11:45:21 +02:00
function canvas_freeform_step(active, _x, _y, _s, _mx, _my, _draw) { #region
var _dim = attributes.dimension;
var _mmx = (_mx - _x) / _s;
var _mmy = (_my - _y) / _s;
if(mouse_holding) {
if(abs(_mmx - mouse_pre_x) + abs(_mmy - mouse_pre_y) >= 1) {
if(_draw) {
surface_set_target(drawing_surface);
2024-04-18 09:17:44 +02:00
canvas_draw_line_brush(brush, round(mouse_pre_x - 0.5), round(mouse_pre_y - 0.5), round(_mmx - 0.5), round(_mmy - 0.5), true);
2024-04-12 11:45:21 +02:00
surface_reset_target();
}
mouse_pre_x = _mmx;
mouse_pre_y = _mmy;
array_push(freeform_shape, new __vec2(_mmx, _mmy) );
}
if(mouse_release(mb_left)) {
surface_set_target(drawing_surface);
2024-04-18 09:17:44 +02:00
canvas_draw_line_brush(brush, _mmx, _mmy, freeform_shape[0].x, freeform_shape[0].y, true);
2024-04-12 11:45:21 +02:00
surface_reset_target();
if(array_length(freeform_shape) > 3) {
var _triangles = polygon_triangulate(freeform_shape, 1)[0];
2024-04-12 11:45:21 +02:00
var temp_surface = surface_create(_dim[0], _dim[1]);
surface_set_target(temp_surface);
DRAW_CLEAR
draw_primitive_begin(pr_trianglelist);
for( var i = 0, n = array_length(_triangles); i < n; i++ ) {
var p0 = _triangles[i][0];
var p1 = _triangles[i][1];
var p2 = _triangles[i][2];
draw_vertex(round(p0.x), round(p0.y));
draw_vertex(round(p1.x), round(p1.y));
draw_vertex(round(p2.x), round(p2.y));
}
draw_primitive_end();
draw_surface(drawing_surface, 0, 0);
surface_reset_target();
surface_set_shader(drawing_surface, sh_freeform_fill_cleanup);
shader_set_f("dimension", _dim);
draw_surface_ext(temp_surface, 0, 0, 1, 1, 0, draw_get_color(), draw_get_alpha());
surface_reset_shader();
surface_free(temp_surface);
}
mouse_holding = false;
}
} else if(mouse_press(mb_left, active)) {
mouse_pre_x = _mmx;
mouse_pre_y = _mmy;
mouse_holding = true;
freeform_shape = [ new __vec2(_mmx, _mmy) ];
2024-04-13 07:32:04 +02:00
node.tool_pick_color(_mmx, _mmy);
2024-04-12 11:45:21 +02:00
surface_clear(drawing_surface);
}
} #endregion