Files
armorpaint/base/Sources/tab_swatches.ts
T

290 lines
9.3 KiB
TypeScript
Raw Normal View History

2024-03-14 15:31:22 +01:00
let _tab_swatches_empty: image_t;
let tab_swatches_drag_pos: i32 = -1;
function tab_swatches_empty_set(image: image_t) {
_tab_swatches_empty = image;
}
function tab_swatches_empty_get(): image_t {
if (_tab_swatches_empty == null) {
2024-03-20 16:13:54 +01:00
let b: u8_array_t = u8_array_create(4);
2024-03-14 15:31:22 +01:00
b[0] = 255;
b[1] = 255;
b[2] = 255;
b[3] = 255;
_tab_swatches_empty = image_from_bytes(b.buffer, 1, 1);
}
return _tab_swatches_empty;
}
2024-04-07 09:52:45 +02:00
let _tab_swatches_draw_i: i32;
2024-03-14 15:31:22 +01:00
function tab_swatches_draw(htab: zui_handle_t) {
let ui: zui_t = ui_base_ui;
let statush: i32 = config_raw.layout[layout_size_t.STATUS_H];
if (zui_tab(htab, tr("Swatches")) && statush > ui_status_default_status_h * zui_SCALE(ui)) {
zui_begin_sticky();
if (config_raw.touch_ui) {
2024-04-25 22:04:43 +02:00
let row: f32[] = [1 / 5, 1 / 5, 1 / 5, 1 / 5, 1 / 5];
zui_row(row);
2024-03-14 15:31:22 +01:00
}
else {
2024-04-25 22:04:43 +02:00
let row: f32[] = [1 / 14, 1 / 14, 1 / 14, 1 / 14, 1 / 14];
zui_row(row);
2024-03-14 15:31:22 +01:00
}
if (zui_button(tr("New"))) {
context_set_swatch(make_swatch());
2024-03-20 16:13:54 +01:00
array_push(project_raw.swatches, context_raw.swatch);
}
if (ui.is_hovered) {
zui_tooltip(tr("Add new swatch"));
2024-03-14 15:31:22 +01:00
}
if (zui_button(tr("Import"))) {
2024-03-20 16:13:54 +01:00
ui_menu_draw(function (ui: zui_t) {
2024-03-14 15:31:22 +01:00
if (ui_menu_button(ui, tr("Replace Existing"))) {
project_import_swatches(true);
context_set_swatch(project_raw.swatches[0]);
}
if (ui_menu_button(ui, tr("Append"))) {
project_import_swatches(false);
}
}, 2);
}
2024-03-20 16:13:54 +01:00
if (ui.is_hovered) {
zui_tooltip(tr("Import swatches"));
}
2024-03-14 15:31:22 +01:00
2024-03-20 16:13:54 +01:00
if (zui_button(tr("Export"))) {
project_export_swatches();
}
if (ui.is_hovered) {
zui_tooltip(tr("Export swatches"));
}
2024-03-14 15:31:22 +01:00
if (zui_button(tr("Clear"))) {
context_set_swatch(make_swatch());
project_raw.swatches = [context_raw.swatch];
}
if (zui_button(tr("Restore"))) {
project_set_default_swatches();
context_set_swatch(project_raw.swatches[0]);
}
2024-03-20 16:13:54 +01:00
if (ui.is_hovered) {
zui_tooltip(tr("Restore default swatches"));
}
2024-03-14 15:31:22 +01:00
zui_end_sticky();
zui_separator(3, false);
let slotw: i32 = math_floor(26 * zui_SCALE(ui));
let num: i32 = math_floor(ui._w / (slotw + 3));
let drag_pos_set: bool = false;
let uix: f32 = 0.0;
let uiy: f32 = 0.0;
for (let row: i32 = 0; row < math_floor(math_ceil(project_raw.swatches.length / num)); ++row) {
let ar: f32[] = [];
2024-03-20 16:13:54 +01:00
for (let i: i32 = 0; i < num; ++i) {
array_push(ar, 1 / num);
}
2024-03-14 15:31:22 +01:00
zui_row(ar);
ui._x += 2;
2024-03-20 16:13:54 +01:00
if (row > 0) {
ui._y += 6;
}
2024-03-14 15:31:22 +01:00
for (let j: i32 = 0; j < num; ++j) {
let i: i32 = j + row * num;
if (i >= project_raw.swatches.length) {
zui_end_element(slotw);
continue;
}
if (context_raw.swatch == project_raw.swatches[i]) {
let w: i32 = 32;
2024-04-10 19:39:17 +02:00
zui_fill(-2, -2, w, w, ui.ops.theme.HIGHLIGHT_COL);
2024-03-14 15:31:22 +01:00
}
uix = ui._x;
uiy = ui._y;
// Draw the drag position indicator
if (base_drag_swatch != null && tab_swatches_drag_pos == i) {
2024-04-10 19:39:17 +02:00
zui_fill(-1, -2 , 2, 32, ui.ops.theme.HIGHLIGHT_COL);
2024-03-14 15:31:22 +01:00
}
2024-05-03 21:29:39 +02:00
let state: zui_state_t = _zui_image(tab_swatches_empty_get(), project_raw.swatches[i].base, slotw);
2024-03-14 15:31:22 +01:00
if (state == zui_state_t.STARTED) {
context_set_swatch(project_raw.swatches[i]);
base_drag_off_x = -(mouse_x - uix - ui._window_x - 2 * slotw);
base_drag_off_y = -(mouse_y - uiy - ui._window_y + 1);
base_drag_swatch = context_raw.swatch;
}
else if (state == zui_state_t.HOVERED) {
tab_swatches_drag_pos = (mouse_x > uix + ui._window_x + slotw / 2) ? i + 1 : i; // Switch to the next position if the mouse crosses the swatch rectangle center
drag_pos_set = true;
}
else if (state == zui_state_t.RELEASED) {
if (time_time() - context_raw.select_time < 0.25) {
2024-04-07 09:52:45 +02:00
_tab_swatches_draw_i = i;
2024-03-20 16:13:54 +01:00
ui_menu_draw(function (ui: zui_t) {
2024-03-14 15:31:22 +01:00
ui.changed = false;
2024-03-24 10:26:56 +01:00
let h: zui_handle_t = zui_handle(__ID__);
2024-03-14 15:31:22 +01:00
h.color = context_raw.swatch.base;
2024-05-03 21:29:39 +02:00
context_raw.swatch.base = zui_color_wheel(h, false, -1, 11 * ui.ops.theme.ELEMENT_H * zui_SCALE(ui), true, function () {
2024-03-14 15:31:22 +01:00
context_raw.color_picker_previous_tool = context_raw.tool;
context_select_tool(workspace_tool_t.PICKER);
2024-04-07 09:52:45 +02:00
2024-03-20 16:13:54 +01:00
context_raw.color_picker_callback = function (color: swatch_color_t) {
2024-04-07 09:52:45 +02:00
let i: i32 = _tab_swatches_draw_i;
2024-03-14 15:31:22 +01:00
project_raw.swatches[i] = project_clone_swatch(color);
};
});
2024-04-07 09:52:45 +02:00
2024-03-24 10:26:56 +01:00
let hopacity: zui_handle_t = zui_handle(__ID__);
2024-03-14 15:31:22 +01:00
hopacity.value = context_raw.swatch.opacity;
context_raw.swatch.opacity = zui_slider(hopacity, "Opacity", 0, 1, true);
2024-03-24 10:26:56 +01:00
let hocclusion: zui_handle_t = zui_handle(__ID__);
2024-03-14 15:31:22 +01:00
hocclusion.value = context_raw.swatch.occlusion;
context_raw.swatch.occlusion = zui_slider(hocclusion, "Occlusion", 0, 1, true);
2024-03-24 10:26:56 +01:00
let hroughness: zui_handle_t = zui_handle(__ID__);
2024-03-14 15:31:22 +01:00
hroughness.value = context_raw.swatch.roughness;
context_raw.swatch.roughness = zui_slider(hroughness, "Roughness", 0, 1, true);
2024-03-24 10:26:56 +01:00
let hmetallic: zui_handle_t = zui_handle(__ID__);
2024-03-14 15:31:22 +01:00
hmetallic.value = context_raw.swatch.metallic;
context_raw.swatch.metallic = zui_slider(hmetallic, "Metallic", 0, 1, true);
2024-03-24 10:26:56 +01:00
let hheight: zui_handle_t = zui_handle(__ID__);
2024-03-14 15:31:22 +01:00
hheight.value = context_raw.swatch.height;
context_raw.swatch.height = zui_slider(hheight, "Height", 0, 1, true);
2024-03-20 16:13:54 +01:00
if (ui.changed || ui.is_typing) {
ui_menu_keep_open = true;
}
if (ui.input_released) {
context_set_swatch(context_raw.swatch); // Trigger material preview update
}
2024-03-14 15:31:22 +01:00
}, 16, math_floor(mouse_x - 200 * zui_SCALE(ui)), math_floor(mouse_y - 250 * zui_SCALE(ui)));
}
context_raw.select_time = time_time();
}
if (ui.is_hovered && ui.input_released_r) {
context_set_swatch(project_raw.swatches[i]);
let add: i32 = project_raw.swatches.length > 1 ? 1 : 0;
///if (krom_windows || krom_linux || krom_darwin)
add += 1; // Copy
///end
///if (is_paint || is_sculpt)
add += 3;
///end
///if is_lav
add += 1;
///end
2024-04-07 09:52:45 +02:00
_tab_swatches_draw_i = i;
2024-03-20 16:13:54 +01:00
ui_menu_draw(function (ui: zui_t) {
2024-04-07 09:52:45 +02:00
let i: i32 = _tab_swatches_draw_i;
2024-03-14 15:31:22 +01:00
if (ui_menu_button(ui, tr("Duplicate"))) {
context_set_swatch(project_clone_swatch(context_raw.swatch));
2024-03-20 16:13:54 +01:00
array_push(project_raw.swatches, context_raw.swatch);
2024-03-14 15:31:22 +01:00
}
///if (krom_windows || krom_linux || krom_darwin)
else if (ui_menu_button(ui, tr("Copy Hex Code"))) {
let color: i32 = context_raw.swatch.base;
color = color_set_ab(color, context_raw.swatch.opacity * 255);
let val: i32 = color;
2024-03-20 16:13:54 +01:00
if (val < 0) {
val += 4294967296;
}
2024-03-24 10:26:56 +01:00
krom_copy_to_clipboard(i32_to_string(val));
2024-03-14 15:31:22 +01:00
}
///end
else if (project_raw.swatches.length > 1 && ui_menu_button(ui, tr("Delete"), "delete")) {
tab_swatches_delete_swatch(project_raw.swatches[i]);
}
///if (is_paint || is_sculpt)
else if (ui_menu_button(ui, tr("Create Material"))) {
tab_materials_accept_swatch_drag(project_raw.swatches[i]);
}
else if (ui_menu_button(ui, tr("Create Color Layer"))) {
let color: i32 = project_raw.swatches[i].base;
color = color_set_ab(color, project_raw.swatches[i].opacity * 255);
base_create_color_layer(color, project_raw.swatches[i].occlusion, project_raw.swatches[i].roughness, project_raw.swatches[i].metallic);
}
///end
}, add);
}
if (ui.is_hovered) {
let color: i32 = project_raw.swatches[i].base;
color = color_set_ab(color, project_raw.swatches[i].opacity * 255);
let val: i32 = color;
2024-03-20 16:13:54 +01:00
if (val < 0) {
val += 4294967296;
}
2024-03-24 10:26:56 +01:00
zui_tooltip("#" + i32_to_string_hex(val));
2024-03-14 15:31:22 +01:00
}
}
}
// Draw the rightmost line next to the last swatch
if (base_drag_swatch != null && tab_swatches_drag_pos == project_raw.swatches.length) {
ui._x = uix; // Reset the position because otherwise it would start in the row below
ui._y = uiy;
2024-04-10 19:39:17 +02:00
zui_fill(28, -2, 2, 32, ui.ops.theme.HIGHLIGHT_COL);
2024-03-14 15:31:22 +01:00
}
// Currently there is no valid dragPosition so reset it
if (!drag_pos_set) {
tab_swatches_drag_pos = -1;
}
let in_focus: bool = ui.input_x > ui._window_x && ui.input_x < ui._window_x + ui._window_w &&
2024-03-20 16:13:54 +01:00
ui.input_y > ui._window_y && ui.input_y < ui._window_y + ui._window_h;
2024-03-14 15:31:22 +01:00
if (in_focus && ui.is_delete_down && project_raw.swatches.length > 1) {
ui.is_delete_down = false;
tab_swatches_delete_swatch(context_raw.swatch);
}
}
}
function tab_swatches_accept_swatch_drag(swatch: swatch_color_t) {
// No valid position available
2024-03-20 16:13:54 +01:00
if (tab_swatches_drag_pos == -1) {
return;
}
2024-03-14 15:31:22 +01:00
2024-03-20 16:13:54 +01:00
let swatch_pos: i32 = array_index_of(project_raw.swatches, swatch);
2024-03-14 15:31:22 +01:00
// A new swatch from color picker
if (swatch_pos == -1) {
2024-03-20 16:13:54 +01:00
array_insert(project_raw.swatches, tab_swatches_drag_pos, swatch);
2024-03-14 15:31:22 +01:00
}
else if (math_abs(swatch_pos - tab_swatches_drag_pos) > 0) { // Existing swatch is reordered
array_remove(project_raw.swatches, swatch);
// If the new position is after the old one, decrease by one because the swatch has been deleted
let new_pos: i32 = tab_swatches_drag_pos - swatch_pos > 0 ? tab_swatches_drag_pos -1 : tab_swatches_drag_pos;
2024-03-20 16:13:54 +01:00
array_insert(project_raw.swatches, new_pos, swatch);
2024-03-14 15:31:22 +01:00
}
}
function tab_swatches_delete_swatch(swatch: swatch_color_t) {
2024-03-20 16:13:54 +01:00
let i: i32 = array_index_of(project_raw.swatches, swatch);
2024-03-14 15:31:22 +01:00
context_set_swatch(project_raw.swatches[i == project_raw.swatches.length - 1 ? i - 1 : i + 1]);
2024-03-20 16:13:54 +01:00
array_splice(project_raw.swatches, i, 1);
2024-03-14 15:31:22 +01:00
ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
}