Pixel-Composer/scripts/scrollPane/scrollPane.gml

193 lines
5 KiB
Text
Raw Normal View History

2023-01-17 08:11:55 +01:00
function scrollPane(_w, _h, ondraw) : widget() constructor {
2022-01-13 05:24:03 +01:00
scroll_y = 0;
scroll_y_raw = 0;
scroll_y_to = 0;
2024-12-14 03:39:34 +01:00
whover = false;
wactive = false;
2023-02-15 10:43:24 +01:00
x = 0;
y = 0;
2022-01-13 05:24:03 +01:00
w = _w;
h = _h;
2022-12-16 09:18:09 +01:00
surface_w = _w - ui(12);
2022-01-13 05:24:03 +01:00
surface_h = _h;
2022-11-03 11:44:49 +01:00
surface = surface_create_valid(surface_w, surface_h);
2022-01-13 05:24:03 +01:00
drawFunc = ondraw;
2023-12-29 14:30:54 +01:00
content_h = 0;
is_scroll = true;
2024-09-13 08:25:00 +02:00
always_scroll = true;
2023-12-29 14:30:54 +01:00
show_scroll = true;
2024-09-13 09:43:09 +02:00
scroll_resize = true;
2022-01-13 05:24:03 +01:00
scroll_step = 64;
2023-10-04 09:49:31 +02:00
scroll_lock = false;
2022-01-13 05:24:03 +01:00
2022-09-27 06:37:28 +02:00
is_scrolling = false;
2022-12-19 13:35:30 +01:00
scroll_ms = 0;
2022-01-13 05:24:03 +01:00
pen_scrolling = false;
pen_scroll_my = 0;
pen_scroll_sy = 0;
pen_scroll_py = 0;
2024-07-28 13:55:57 +02:00
hover_content = false;
scroll_s = sprite_get_width(THEME.ui_scrollbar);
scroll_w = scroll_s;
2024-12-14 03:39:34 +01:00
scroll_color_bg = COLORS.scrollbar_bg;
scroll_color_bar = COLORS.scrollbar_idle;
scroll_color_bar_hover = COLORS.scrollbar_hover;
scroll_color_bar_active = COLORS.scrollbar_active;
2022-01-24 02:21:25 +01:00
static resize = function(_w, _h) {
2022-01-13 05:24:03 +01:00
w = _w;
h = _h;
2024-09-13 09:43:09 +02:00
surface_w = _w - ui(12) * (is_scroll || scroll_resize);
2024-09-12 12:42:35 +02:00
surface_h = _h;
2022-01-13 05:24:03 +01:00
}
2024-09-14 05:36:18 +02:00
static setScroll = function(_scroll_y) { INLINE scroll_y_to = _scroll_y; }
2023-10-15 15:04:42 +02:00
static draw = function(x, y, mx = mouse_mx - x, my = mouse_my - y) {
2023-02-15 10:43:24 +01:00
self.x = x;
self.y = y;
2024-12-14 03:39:34 +01:00
whover = hover;
wactive = active;
hover &= point_in_rectangle( mx, my, 0, 0, surface_w, surface_h);
hover &= pen_scrolling != 2;
2023-09-07 20:59:14 +02:00
surface = surface_verify(surface, surface_w, surface_h);
2024-07-28 13:55:57 +02:00
hover_content = false;
2024-12-14 03:39:34 +01:00
/// Draw
2022-01-13 05:24:03 +01:00
surface_set_target(surface);
2022-11-18 03:20:31 +01:00
draw_clear(COLORS.panel_bg_clear);
var hh = drawFunc(scroll_y, [ mx, my ], [ x, y ]);
2024-11-30 06:50:20 +01:00
hh = real(hh);
2022-12-16 09:18:09 +01:00
content_h = max(0, hh - surface_h);
2022-01-13 05:24:03 +01:00
surface_reset_target();
2022-12-16 09:18:09 +01:00
var sc = is_scroll;
is_scroll = hh > surface_h;
if(sc != is_scroll) resize(w, h);
2024-12-14 03:39:34 +01:00
/// Scrolling
2022-12-16 09:18:09 +01:00
2023-01-17 08:11:55 +01:00
scroll_y_to = clamp(scroll_y_to, -content_h, 0);
scroll_y_raw = lerp_float(scroll_y_raw, scroll_y_to, 4);
scroll_y = round(scroll_y_raw);
2022-01-13 05:24:03 +01:00
draw_surface_safe(surface, x, y);
2024-05-18 13:46:01 +02:00
if(hover && !scroll_lock && !key_mod_press(SHIFT) && !key_mod_press(CTRL)) {
2023-04-07 21:25:27 +02:00
if(mouse_wheel_down()) scroll_y_to -= scroll_step * SCROLL_SPEED;
if(mouse_wheel_up()) scroll_y_to += scroll_step * SCROLL_SPEED;
2022-01-13 05:24:03 +01:00
}
2023-10-04 09:49:31 +02:00
2024-12-14 03:39:34 +01:00
/// Pen scroll
2024-07-28 13:55:57 +02:00
if(pen_scrolling == 0 && mouse_press(mb_left, !hover_content && hover && PEN_USE)) {
pen_scrolling = 1;
pen_scroll_my = 0;
}
if(pen_scrolling == 1) {
pen_scroll_my += PEN_Y_DELTA;
2024-07-28 13:55:57 +02:00
if(abs(pen_scroll_my) > 0)
pen_scrolling = 2;
if(mouse_release(mb_left)) pen_scrolling = 0;
} else if(pen_scrolling == 2) {
2024-07-28 13:55:57 +02:00
scroll_y_to = clamp(scroll_y_to + PEN_Y_DELTA * 2, -content_h, 0);
scroll_y_raw = scroll_y_to;
scroll_y = round(scroll_y_raw);
pen_scroll_py = abs(PEN_Y_DELTA) > abs(pen_scroll_py)? PEN_Y_DELTA : lerp_float(pen_scroll_py, PEN_Y_DELTA, 10);
2024-07-28 13:55:57 +02:00
if(mouse_release(mb_left))
pen_scrolling = 0;
} else {
pen_scroll_py = lerp_float(pen_scroll_py, 0, 30, 1);
scroll_y_to += pen_scroll_py;
}
2024-09-13 09:43:09 +02:00
if(show_scroll && (abs(content_h) > 0 || (always_scroll && scroll_resize))) {
var _p = PEN_USE && (is_scrolling || point_in_rectangle(x + mx, y + my, x + w - scroll_w - 2, y, x + w, y + surface_h));
scroll_w = lerp_float(scroll_w, _p? 12 : scroll_s, 5);
2024-12-14 03:39:34 +01:00
draw_scroll(x + w - scroll_w, y + ui(6), true, surface_h - ui(12), -scroll_y / content_h, surface_h / (surface_h + content_h), x + mx, y + my, scroll_w);
2022-01-13 05:24:03 +01:00
}
scroll_lock = false;
2022-01-13 05:24:03 +01:00
}
2024-12-14 03:39:34 +01:00
static draw_scroll = function(scr_x, scr_y, is_vert, scr_s, scr_prog, scr_ratio, mx, my, bar_spr_w) {
2022-12-19 13:35:30 +01:00
var scr_scale_s = scr_s * scr_ratio;
2022-01-13 05:24:03 +01:00
var scr_prog_s = scr_prog * (scr_s - scr_scale_s);
var scr_w, scr_h, bar_w, bar_h, bar_x, bar_y;
if(is_vert) {
scr_w = bar_spr_w;
2022-01-13 05:24:03 +01:00
scr_h = scr_s;
2022-12-19 13:35:30 +01:00
bar_w = bar_spr_w;
2022-01-13 05:24:03 +01:00
bar_h = scr_scale_s;
2022-12-19 13:35:30 +01:00
2022-01-13 05:24:03 +01:00
bar_x = scr_x;
bar_y = scr_y + scr_prog_s;
} else {
scr_w = scr_s;
scr_h = bar_spr_w;
2022-12-19 13:35:30 +01:00
2022-01-13 05:24:03 +01:00
bar_w = scr_scale_s;
bar_h = bar_spr_w;
2022-12-19 13:35:30 +01:00
2022-01-13 05:24:03 +01:00
bar_x = scr_x + scr_prog_s;
bar_y = scr_y;
}
2022-12-23 04:45:52 +01:00
2022-12-19 13:35:30 +01:00
if(is_scrolling) {
var delta = (is_vert? my : mx) - scroll_ms;
scroll_ms = is_vert? my : mx;
scroll_y_to -= (delta / scr_scale_s) * scr_s;
scroll_y_to = clamp(scroll_y_to, -content_h, 0);
if(mouse_release(mb_left))
is_scrolling = false;
}
2023-02-14 05:32:32 +01:00
var bx0 = clamp(bar_x, scr_x, scr_x + scr_w);
var bx1 = clamp(bar_x + bar_w, scr_x, scr_x + scr_w);
var ww = bx1 - bx0;
var by0 = clamp(bar_y, scr_y, scr_y + scr_h);
var by1 = clamp(bar_y + bar_h, scr_y, scr_y + scr_h);
var hh = by1 - by0;
2024-12-14 03:39:34 +01:00
draw_sprite_stretched_ext(THEME.ui_scrollbar, 0, scr_x, scr_y, scr_w, scr_h, scroll_color_bg, 1);
2023-02-14 05:32:32 +01:00
2024-12-14 03:39:34 +01:00
var cc = scroll_color_bar;
if(whover && point_in_rectangle(mx, my, scr_x - 2, scr_y - 2, scr_x + scr_w + 2, scr_y + scr_h + 2)) {
cc = scroll_color_bar_hover;
if(mouse_press(mb_left, wactive)) {
2022-01-13 05:24:03 +01:00
is_scrolling = true;
2022-12-19 13:35:30 +01:00
scroll_ms = is_vert? my : mx;
}
}
2024-12-14 03:39:34 +01:00
if(is_scrolling) cc = scroll_color_bar_active;
draw_sprite_stretched_ext(THEME.ui_scrollbar, 0, bx0, by0, ww, hh, cc, 1);
2022-01-13 05:24:03 +01:00
}
2024-11-24 11:39:17 +01:00
static free = function() {
surface_free(surface);
}
2022-01-13 05:24:03 +01:00
}