function buttonPalette(_onApply, dialog = noone) : widget() constructor { onApply = _onApply; parentDialog = dialog; current_palette = []; side_button = noone; expanded = false; edit_color_index = -1; function apply(value) { #region if(!interactable) return; onApply(value); } #endregion static trigger = function() { #region var dialog = dialogCall(o_dialog_palette, WIN_W / 2, WIN_H / 2); dialog.setDefault(current_palette); dialog.onApply = apply; dialog.interactable = interactable; dialog.drop_target = self; if(parentDialog) parentDialog.addChildren(dialog); } #endregion static triggerSingle = function(_index) { #region edit_color_index = _index; current_palette = array_clone(current_palette); var dialog = dialogCall(o_dialog_color_selector, WIN_W / 2, WIN_H / 2); dialog.setDefault(current_palette[edit_color_index]); dialog.selector.onApply = editColor; dialog.onApply = editColor; dialog.interactable = interactable; } #endregion function editColor(col) { #region if(edit_color_index == -1) return; current_palette[edit_color_index] = col; apply(current_palette); } #endregion static drawParam = function(params) { return draw(params.x, params.y, params.w, params.h, params.data, params.m); } static draw = function(_x, _y, _w, _h, _color, _m) { #region x = _x; y = _y; w = _w; h = _h; var _bs = min(_h, ui(32)); hovering = hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + _h); if(_w - _bs > ui(100) && side_button && instanceof(side_button) == "buttonClass") { side_button.setFocusHover(active, hover); side_button.draw(_x + _w -_bs, _y + _h / 2 - _bs / 2, _bs, _bs, _m, THEME.button_hide); _w -= _bs + ui(8); } var _pw = _w - ui(4); var _ph = _h - ui(4); current_palette = _color; if(array_length(_color) > 0 && is_array(_color[0])) { if(array_length(_color[0]) == 0) return 0; h = ui(4) + array_length(_color) * _ph; current_palette = _color[0]; } else { h = _h; } if(!is_array(current_palette) || array_empty(current_palette) || is_array(current_palette[0])) return 0; var _colr_h = ui(16); var _drawSingle = !is_array(_color[0]); var _ppw = _drawSingle? _pw - ui(24) : _w; var _ppx = _drawSingle? _x + ui(2) + ui(24) : _x; var hoverRect = ihover && point_in_rectangle(_m[0], _m[1], _ppx, _y, _ppx + _ppw, _y + h); if(_drawSingle && expanded) h = _h + array_length(_color) * _colr_h + ui(2); if(hoverRect) { draw_sprite_stretched(THEME.button_def, 1, _x, _y, _w, h); if(mouse_press(mb_left, iactive)) trigger(); if(mouse_click(mb_left, iactive)) { draw_sprite_stretched(THEME.button_def, 2, _x, _y, _w, h); draw_sprite_stretched_ext(THEME.button_def, 3, _x, _y, _w, h, COLORS._main_accent, 1); } } else { draw_sprite_stretched(THEME.button_def, 0, _x, _y, _w, h); if(mouse_press(mb_left)) deactivate(); } if(_drawSingle) { var _pph = _ph; var _ppy = _y + ui(2); var _bbx = _x + ui(12); var _bby = _y + _pph / 2 + ui(2); var _bba = 0.5; var _bbc = COLORS._main_icon; if(hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + ui(24), _y + _pph)) { _bba = 1; if(mouse_press(mb_left)) expanded = !expanded; if(mouse_click(mb_left)) _bbc = COLORS._main_icon_light; } draw_sprite_ext(THEME.arrow, expanded? 3 : 0, _bbx, _bby + ui(expanded), 1, 1, 0, _bbc, _bba); if(expanded) { var _cx = _x + ui(2); var _cy = _y + _pph + ui(4); var _cw = _w - ui(4); var _ch = h - _pph - ui(4 + 2); draw_sprite_stretched_ext(THEME.menu_button_mask, 0, _cx, _cy, _cw, _ch, CDEF.main_mdblack, 1); for (var i = 0, n = array_length(_color); i < n; i++) { var _c = _color[i]; var _ccx = _cx; var _ccy = _cy + i * _colr_h; var _ccw = _cw; var _cch = _colr_h; draw_sprite_stretched_ext(THEME.palette_mask, 1, _ccx + ui(2), _ccy + ui(2), _ccw - ui(4), _cch - ui(4), _c, 1); if(hover && point_in_rectangle(_m[0], _m[1], _ccx, _ccy, _ccx + _ccw, _ccy + _cch - 1)) { BLEND_ADD draw_sprite_stretched_ext(THEME.menu_button_mask, 1, _ccx + ui(2), _ccy + ui(2), _ccw - ui(4), _cch - ui(4), c_white, .3); BLEND_NORMAL if(mouse_press(mb_left, active)) triggerSingle(i); } } } drawPalette(_color, _ppx, _ppy, _ppw, _pph); } else { expanded = false; for( var i = 0, n = array_length(_color); i < n; i++ ) { var _pal = _color[i]; var _px = _x + ui(2); var _py = _y + ui(2) + i * _ph; if(is_array(_pal)) drawPalette(_pal, _px, _py, _pw, _ph); } } if(WIDGET_CURRENT == self || (instance_exists(o_dialog_palette) && o_dialog_palette.drop_target == self)) draw_sprite_stretched_ext(THEME.widget_selecting, 0, _x, _y, _w, h, COLORS._main_accent, 1); if(DRAGGING && DRAGGING.type == "Palette" && hover && hoverRect) { draw_sprite_stretched_ext(THEME.ui_panel_active, 0, _x, _y, _w, h, COLORS._main_value_positive, 1); if(mouse_release(mb_left)) onApply(DRAGGING.data); } resetFocus(); return h; } #endregion static clone = function() { #region var cln = new buttonPalette(onApply, parentDialog); return cln; } #endregion } function drawPalette(_pal, _x, _y, _w, _h, _a = 1) { #region var aa = array_length(_pal); if(aa == 1) { draw_sprite_stretched_ext(THEME.palette_mask, 1, _x, _y, _w, _h, _pal[0], _a); return; } var ww = _w / aa; var _x0 = _x; var _in; for(var i = 0; i < aa; i++) { if(!is_numeric(_pal[i])) continue; if(i == 0) _in = 2; else if(i == aa - 1) _in = 3; else _in = 0; var _ca = _color_get_alpha(_pal[i]); if(_ca == 1) { draw_sprite_stretched_ext(THEME.palette_mask, _in, floor(_x0), _y, ceil(ww), _h, _pal[i], _a); } else { draw_sprite_stretched_ext(THEME.palette_mask, _in, floor(_x0), _y, ceil(ww), _h - ui(8), _pal[i], _a); draw_sprite_stretched_ext(THEME.palette_mask, 1, floor(_x0), _y + _h - ui(6), ceil(ww), ui(6), c_black, _a); draw_sprite_stretched_ext(THEME.palette_mask, 1, floor(_x0), _y + _h - ui(6), ceil(ww) * _ca, ui(6), c_white, _a); } _x0 += ww; } } #endregion function drawPaletteGrid(_pal, _x, _y, _w, _gs = 24, c_color = -1) { #region var amo = array_length(_pal); var col = floor(_w / _gs); var row = ceil(amo / col); var cx = -1, cy = -1; var _pd = ui(5); var _h = row * _gs; for(var i = 0; i < array_length(_pal); i++) { draw_set_color(_pal[i]); var _x0 = _x + safe_mod(i, col) * _gs; var _y0 = _y + floor(i / col) * _gs; draw_rectangle(_x0, _y0 + 1, _x0 + _gs, _y0 + _gs, false); if(c_color >= 0 && color_diff(c_color, _pal[i]) <= 0) { cx = _x0; cy = _y0; } } if(cx == -1) return _h; draw_sprite_stretched_ext(THEME.palette_selecting, 0, cx - _pd, cy + 1 - _pd, _gs + _pd * 2, _gs + _pd * 2); return _h; } #endregion