Files
armorpaint/base/Sources/TabSwatches.ts
T

258 lines
8.9 KiB
TypeScript
Raw Normal View History

2021-01-29 20:51:46 +01:00
class TabSwatches {
2024-01-17 18:53:31 +01:00
static _empty: Image;
static set empty(image: Image) {
TabSwatches._empty = image;
}
2022-03-02 17:37:06 +01:00
2024-01-17 18:53:31 +01:00
static get empty(): Image {
if (TabSwatches._empty == null) {
let b = new Uint8Array(4);
2023-12-07 23:35:02 +01:00
b[0] = 255;
b[1] = 255;
b[2] = 255;
b[3] = 255;
2024-01-17 18:53:31 +01:00
TabSwatches._empty = Image.fromBytes(b.buffer, 1, 1);
2022-03-02 17:37:06 +01:00
}
2024-01-17 18:53:31 +01:00
return TabSwatches._empty;
2022-03-02 17:37:06 +01:00
}
2024-01-17 18:53:31 +01:00
static dragPosition: i32 = -1;
2021-01-29 20:51:46 +01:00
2024-01-17 18:53:31 +01:00
static draw = (htab: Handle) => {
let ui = UIBase.inst.ui;
let statush = Config.raw.layout[LayoutSize.LayoutStatusH];
2023-03-24 21:53:48 +01:00
if (ui.tab(htab, tr("Swatches")) && statush > UIStatus.defaultStatusH * ui.SCALE()) {
2021-01-29 20:51:46 +01:00
ui.beginSticky();
2022-11-29 19:25:16 +01:00
if (Config.raw.touch_ui) {
ui.row([1 / 5, 1 / 5, 1 / 5, 1 / 5, 1 / 5]);
}
else {
ui.row([1 / 14, 1 / 14, 1 / 14, 1 / 14, 1 / 14]);
}
2021-01-29 20:51:46 +01:00
if (ui.button(tr("New"))) {
2023-04-18 14:33:50 +02:00
Context.setSwatch(Project.makeSwatch());
2023-02-27 21:13:25 +01:00
Project.raw.swatches.push(Context.raw.swatch);
2021-01-29 20:51:46 +01:00
}
2022-01-28 16:07:07 +01:00
if (ui.isHovered) ui.tooltip(tr("Add new swatch"));
2021-08-03 15:02:15 +02:00
2022-01-28 16:07:07 +01:00
if (ui.button(tr("Import"))) {
2024-01-17 18:53:31 +01:00
UIMenu.draw((ui: Zui) => {
2023-05-18 22:53:58 +02:00
if (UIMenu.menuButton(ui, tr("Replace Existing"))) {
2022-01-28 16:07:07 +01:00
Project.importSwatches(true);
2023-04-18 14:33:50 +02:00
Context.setSwatch(Project.raw.swatches[0]);
2022-01-28 16:07:07 +01:00
}
2023-05-18 22:53:58 +02:00
if (UIMenu.menuButton(ui, tr("Append"))) {
2022-01-28 16:07:07 +01:00
Project.importSwatches(false);
}
2023-05-18 22:53:58 +02:00
}, 2);
2023-12-07 23:35:02 +01:00
}
2021-02-15 12:00:18 +01:00
if (ui.isHovered) ui.tooltip(tr("Import swatches"));
2021-08-03 15:02:15 +02:00
2022-01-28 16:07:07 +01:00
if (ui.button(tr("Export"))) Project.exportSwatches();
if (ui.isHovered) ui.tooltip(tr("Export swatches"));
2021-08-03 15:02:15 +02:00
if (ui.button(tr("Clear"))) {
2023-04-18 14:33:50 +02:00
Context.setSwatch(Project.makeSwatch());
2023-02-27 21:13:25 +01:00
Project.raw.swatches = [Context.raw.swatch];
2021-08-03 15:02:15 +02:00
}
if (ui.button(tr("Restore"))) {
Project.setDefaultSwatches();
2023-04-18 14:33:50 +02:00
Context.setSwatch(Project.raw.swatches[0]);
2021-01-29 20:51:46 +01:00
}
2022-01-28 16:07:07 +01:00
if (ui.isHovered) ui.tooltip(tr("Restore default swatches"));
2021-01-29 20:51:46 +01:00
ui.endSticky();
ui.separator(3, false);
2024-01-17 18:53:31 +01:00
let slotw = Math.floor(26 * ui.SCALE());
let num = Math.floor(ui._w / (slotw + 3));
let dragPositionSet = false;
2021-01-29 20:51:46 +01:00
2024-01-17 18:53:31 +01:00
let uix = 0.0;
let uiy = 0.0;
for (let row = 0; row < Math.floor(Math.ceil(Project.raw.swatches.length / num)); ++row) {
let ar = [];
for (let i = 0; i < num; ++i) ar.push(1 / num);
ui.row(ar);
2021-01-29 20:51:46 +01:00
ui._x += 2;
if (row > 0) ui._y += 6;
2024-01-17 18:53:31 +01:00
for (let j = 0; j < num; ++j) {
let i = j + row * num;
2021-01-29 20:51:46 +01:00
if (i >= Project.raw.swatches.length) {
2023-12-12 14:57:44 +01:00
ui.endElement(slotw);
2021-01-29 20:51:46 +01:00
continue;
}
2023-02-27 21:13:25 +01:00
if (Context.raw.swatch == Project.raw.swatches[i]) {
2024-01-17 18:53:31 +01:00
let off = row % 2 == 1 ? 1 : 0;
let w = 32;
2021-01-29 20:51:46 +01:00
ui.fill(-2, -2, w, w, ui.t.HIGHLIGHT_COL);
}
2022-03-02 17:37:06 +01:00
uix = ui._x;
uiy = ui._y;
// Draw the drag position indicator
2024-01-17 18:53:31 +01:00
if (Base.dragSwatch != null && TabSwatches.dragPosition == i) {
2022-03-02 17:37:06 +01:00
ui.fill(-1, -2 , 2, 32, ui.t.HIGHLIGHT_COL);
2021-01-29 20:51:46 +01:00
}
2021-01-30 12:50:30 +01:00
2024-01-17 18:53:31 +01:00
let state = ui.image(TabSwatches.empty, Project.raw.swatches[i].base, slotw);
2021-01-29 20:51:46 +01:00
if (state == State.Started) {
2023-04-18 14:33:50 +02:00
Context.setSwatch(Project.raw.swatches[i]);
2021-01-30 12:50:30 +01:00
2024-01-17 18:53:31 +01:00
let mouse = Input.getMouse();
2023-12-12 14:57:44 +01:00
Base.dragOffX = -(mouse.x - uix - ui._windowX - 2 * slotw);
Base.dragOffY = -(mouse.y - uiy - ui._windowY + 1);
Base.dragSwatch = Context.raw.swatch;
2021-01-29 20:51:46 +01:00
}
2022-03-02 17:37:06 +01:00
else if (state == State.Hovered) {
2024-01-17 18:53:31 +01:00
let mouse = Input.getMouse();
TabSwatches.dragPosition = (mouse.x > uix + ui._windowX + slotw / 2) ? i + 1 : i; // Switch to the next position if the mouse crosses the swatch rectangle center
2022-03-02 17:37:06 +01:00
dragPositionSet = true;
}
2021-01-29 20:51:46 +01:00
else if (state == State.Released) {
2023-02-27 21:13:25 +01:00
if (Time.time() - Context.raw.selectTime < 0.25) {
2024-01-17 18:53:31 +01:00
UIMenu.draw((ui: Zui) => {
2021-01-29 20:51:46 +01:00
ui.changed = false;
2024-01-17 18:53:31 +01:00
let h = Zui.handle("tabswatches_0");
2023-02-27 21:13:25 +01:00
h.color = Context.raw.swatch.base;
2022-03-15 14:50:53 +01:00
2024-01-17 18:53:31 +01:00
Context.raw.swatch.base = ui.colorWheel(h, false, null, 11 * ui.t.ELEMENT_H * ui.SCALE(), true, () => {
2023-02-27 21:13:25 +01:00
Context.raw.colorPickerPreviousTool = Context.raw.tool;
2024-01-17 18:53:31 +01:00
Context.selectTool(WorkspaceTool.ToolPicker);
Context.raw.colorPickerCallback = (color: TSwatchColor) => {
2022-03-15 22:05:42 +01:00
Project.raw.swatches[i] = Project.cloneSwatch(color);
2022-03-12 15:15:34 +01:00
};
});
2024-01-17 18:53:31 +01:00
let hopacity = Zui.handle("tabswatches_1");
2023-02-27 21:13:25 +01:00
hopacity.value = Context.raw.swatch.opacity;
Context.raw.swatch.opacity = ui.slider(hopacity, "Opacity", 0, 1, true);
2024-01-17 18:53:31 +01:00
let hocclusion = Zui.handle("tabswatches_2");
2023-02-27 21:13:25 +01:00
hocclusion.value = Context.raw.swatch.occlusion;
Context.raw.swatch.occlusion = ui.slider(hocclusion, "Occlusion", 0, 1, true);
2024-01-17 18:53:31 +01:00
let hroughness = Zui.handle("tabswatches_3");
2023-02-27 21:13:25 +01:00
hroughness.value = Context.raw.swatch.roughness;
Context.raw.swatch.roughness = ui.slider(hroughness, "Roughness", 0, 1, true);
2024-01-17 18:53:31 +01:00
let hmetallic = Zui.handle("tabswatches_4");
2023-02-27 21:13:25 +01:00
hmetallic.value = Context.raw.swatch.metallic;
Context.raw.swatch.metallic = ui.slider(hmetallic, "Metallic", 0, 1, true);
2024-01-17 18:53:31 +01:00
let hheight = Zui.handle("tabswatches_5");
2023-02-27 21:13:25 +01:00
hheight.value = Context.raw.swatch.height;
Context.raw.swatch.height = ui.slider(hheight, "Height", 0, 1, true);
2022-03-15 14:50:53 +01:00
2021-06-11 00:13:04 +02:00
if (ui.changed || ui.isTyping) UIMenu.keepOpen = true;
2023-04-18 14:33:50 +02:00
if (ui.inputReleased) Context.setSwatch(Context.raw.swatch); // Trigger material preview update
2024-01-17 18:53:31 +01:00
}, 16, Math.floor(Input.getMouse().x - 200 * ui.SCALE()), Math.floor(Input.getMouse().y - 250 * ui.SCALE()));
2021-01-29 20:51:46 +01:00
}
2023-02-27 21:13:25 +01:00
Context.raw.selectTime = Time.time();
2021-01-29 20:51:46 +01:00
}
if (ui.isHovered && ui.inputReleasedR) {
2023-04-18 14:33:50 +02:00
Context.setSwatch(Project.raw.swatches[i]);
2024-01-17 18:53:31 +01:00
let add = Project.raw.swatches.length > 1 ? 1 : 0;
///if (krom_windows || krom_linux || krom_darwin)
2021-10-03 12:13:06 +02:00
add += 1; // Copy
2024-01-17 18:53:31 +01:00
///end
///if (is_paint || is_sculpt)
add += 3;
///end
///if is_lav
add += 1;
///end
2021-10-03 12:13:06 +02:00
2024-01-17 18:53:31 +01:00
UIMenu.draw((ui: Zui) => {
2023-05-18 22:53:58 +02:00
if (UIMenu.menuButton(ui, tr("Duplicate"))) {
2023-04-18 14:33:50 +02:00
Context.setSwatch(Project.cloneSwatch(Context.raw.swatch));
2023-02-27 21:13:25 +01:00
Project.raw.swatches.push(Context.raw.swatch);
2021-09-03 13:03:41 +02:00
}
2024-01-17 18:53:31 +01:00
///if (krom_windows || krom_linux || krom_darwin)
2023-05-18 22:53:58 +02:00
else if (UIMenu.menuButton(ui, tr("Copy Hex Code"))) {
2024-01-17 18:53:31 +01:00
let color = Context.raw.swatch.base;
color = color_set_ab(color, Context.raw.swatch.opacity * 255);
let val = color;
if (val < 0) val += 4294967296;
Krom.copyToClipboard(val.toString(16));
2021-10-01 08:29:03 +02:00
}
2024-01-17 18:53:31 +01:00
///end
2023-05-18 22:53:58 +02:00
else if (Project.raw.swatches.length > 1 && UIMenu.menuButton(ui, tr("Delete"), "delete")) {
2024-01-17 18:53:31 +01:00
TabSwatches.deleteSwatch(Project.raw.swatches[i]);
2021-01-29 20:51:46 +01:00
}
2024-01-17 18:53:31 +01:00
///if (is_paint || is_sculpt)
2023-05-18 22:53:58 +02:00
else if (UIMenu.menuButton(ui, tr("Create Material"))) {
TabMaterials.acceptSwatchDrag(Project.raw.swatches[i]);
}
2023-05-18 22:53:58 +02:00
else if (UIMenu.menuButton(ui, tr("Create Color Layer"))) {
2024-01-17 18:53:31 +01:00
let color = Project.raw.swatches[i].base;
color = color_set_ab(color, Project.raw.swatches[i].opacity * 255);
Base.createColorLayer(color, Project.raw.swatches[i].occlusion, Project.raw.swatches[i].roughness, Project.raw.swatches[i].metallic);
}
2024-01-17 18:53:31 +01:00
///end
}, add);
2021-01-29 20:51:46 +01:00
}
2021-01-31 10:40:05 +01:00
if (ui.isHovered) {
2024-01-17 18:53:31 +01:00
let color = Project.raw.swatches[i].base;
color = color_set_ab(color, Project.raw.swatches[i].opacity * 255);
let val = color;
if (val < 0) val += 4294967296;
ui.tooltip("#" + val.toString(16));
2021-01-31 10:40:05 +01:00
}
2021-01-29 20:51:46 +01:00
}
}
2021-12-06 14:04:28 +01:00
2022-06-22 15:21:34 +02:00
// Draw the rightmost line next to the last swatch
2024-01-17 18:53:31 +01:00
if (Base.dragSwatch != null && TabSwatches.dragPosition == Project.raw.swatches.length) {
2022-03-02 17:37:06 +01:00
ui._x = uix; // Reset the position because otherwise it would start in the row below
ui._y = uiy;
ui.fill(28, -2, 2, 32, ui.t.HIGHLIGHT_COL);
}
2022-06-22 15:21:34 +02:00
// Currently there is no valid dragPosition so reset it
2023-04-20 00:38:27 +02:00
if (!dragPositionSet) {
2024-01-17 18:53:31 +01:00
TabSwatches.dragPosition = -1;
2023-04-20 00:38:27 +02:00
}
2022-03-02 17:37:06 +01:00
2024-01-17 18:53:31 +01:00
let inFocus = ui.inputX > ui._windowX && ui.inputX < ui._windowX + ui._windowW &&
ui.inputY > ui._windowY && ui.inputY < ui._windowY + ui._windowH;
if (inFocus && ui.isDeleteDown && Project.raw.swatches.length > 1) {
2021-12-06 14:04:28 +01:00
ui.isDeleteDown = false;
2024-01-17 18:53:31 +01:00
TabSwatches.deleteSwatch(Context.raw.swatch);
2021-12-06 14:04:28 +01:00
}
2021-01-29 20:51:46 +01:00
}
}
2021-12-06 14:04:28 +01:00
2024-01-17 18:53:31 +01:00
static acceptSwatchDrag = (swatch: TSwatchColor) => {
2022-03-02 17:37:06 +01:00
// No valid position available
if (TabSwatches.dragPosition == -1) return;
2024-01-17 18:53:31 +01:00
let swatchPosition = Project.raw.swatches.indexOf(swatch);
2023-04-12 12:18:22 +02:00
// A new swatch from color picker
if (swatchPosition == -1) {
2024-01-17 18:53:31 +01:00
Project.raw.swatches.splice(TabSwatches.dragPosition, 0, swatch);
2022-03-02 17:37:06 +01:00
}
2024-01-17 18:53:31 +01:00
else if (Math.abs(swatchPosition - TabSwatches.dragPosition) > 0) { // Existing swatch is reordered
array_remove(Project.raw.swatches, swatch);
2023-04-12 12:18:22 +02:00
// If the new position is after the old one, decrease by one because the swatch has been deleted
2024-01-17 18:53:31 +01:00
let newPosition = TabSwatches.dragPosition - swatchPosition > 0 ? TabSwatches.dragPosition -1 : TabSwatches.dragPosition;
Project.raw.swatches.splice(newPosition, 0, swatch);
2022-03-02 17:37:06 +01:00
}
}
2024-01-17 18:53:31 +01:00
static deleteSwatch = (swatch: TSwatchColor) => {
let i = Project.raw.swatches.indexOf(swatch);
2023-04-18 14:33:50 +02:00
Context.setSwatch(Project.raw.swatches[i == Project.raw.swatches.length - 1 ? i - 1 : i + 1]);
2021-12-06 14:04:28 +01:00
Project.raw.swatches.splice(i, 1);
2024-01-17 18:53:31 +01:00
UIBase.inst.hwnds[TabArea.TabStatus].redraws = 2;
2021-12-06 14:04:28 +01:00
}
2021-01-29 20:51:46 +01:00
}