Color picker improvements

This commit is contained in:
barsdeveloper
2022-10-16 13:56:38 +02:00
parent 0b19d89416
commit 192f2a4c11
23 changed files with 500 additions and 231 deletions

View File

@@ -81,7 +81,7 @@ export default class BlueprintTemplate extends ITemplate {
<div class="ueb-viewport-overlay"></div>
<div class="ueb-viewport-body">
<div class="ueb-grid"
.style="--ueb-additional-x: ${this.element}; --ueb-additional-y: ${this.element.translateY}; --ueb-translate-x: ${this.element.translateX}; --ueb-translate-y: ${this.element.translateY};">
style="--ueb-additional-x: ${this.element}; --ueb-additional-y: ${this.element.translateY}; --ueb-translate-x: ${this.element.translateX}; --ueb-translate-y: ${this.element.translateY};">
<div class="ueb-grid-content">
<div data-links></div>
<div data-nodes></div>

View File

@@ -15,7 +15,7 @@ export default class BoolPinTemplate extends PinTemplate {
super.firstUpdated(changedProperties)
this.#input = this.element.querySelector(".ueb-pin-input")
let self = this
this.onChangeHandler = _ => this.element.setDefaultValue(self.#input.checked ? true : false)
this.onChangeHandler = _ => this.element.setDefaultValue(self.#input.checked)
this.#input.addEventListener("change", this.onChangeHandler)
}
@@ -31,22 +31,10 @@ export default class BoolPinTemplate extends PinTemplate {
]
}
getInputs() {
return [this.#input.checked ? "true" : "false"]
}
/**
* @param {Boolean[]} values
* @param {String[]} rawValues
*/
setDefaultValue(values = [], rawValues) {
this.element.setDefaultValue(values[0])
}
renderInput() {
if (this.element.isInput()) {
return html`
<input type="checkbox" class="ueb-pin-input" checked="${this.element.defaultValue ? "" : nothing}" />
<input type="checkbox" class="ueb-pin-input" ?checked="${this.element.defaultValue ? "" : nothing}" />
`
}
return super.renderInput()

View File

@@ -1,32 +1,10 @@
import IDraggableTemplate from "./IDraggableTemplate"
import MouseMoveDraggable from "../input/mouse/MouseMoveDraggable"
import IDraggableControlTemplate from "./IDraggableControlTemplate"
import Utility from "../Utility"
/** @typedef {import("../element/ColorHandlerElement").default} ColorHandlerElement */
/** @extends {IDraggableTemplate<ColorHandlerElement>} */
export default class ColorHandlerTemplate extends IDraggableTemplate {
#locationChangeCallback
connectedCallback() {
super.connectedCallback()
this.window = this.element.closest("ueb-window")
this.movementSpace = this.element.parentElement
const bounding = this.movementSpace.getBoundingClientRect()
this.movementSpaceSize = [bounding.width, bounding.height]
}
createDraggableObject() {
return new MouseMoveDraggable(this.element, this.element.blueprint, {
draggableElement: this.element.parentElement,
ignoreTranslateCompensate: true,
moveEverywhere: true,
movementSpace: this.element.parentElement,
repositionOnClick: true,
stepSize: 1,
})
}
/** @extends {IDraggableControlTemplate<ColorHandlerElement>} */
export default class ColorHandlerTemplate extends IDraggableControlTemplate {
/** @param {[Number, Number]} param0 */
adjustLocation([x, y]) {
@@ -37,11 +15,12 @@ export default class ColorHandlerTemplate extends IDraggableTemplate {
r = Math.min(r, radius), [x, y] = Utility.getCartesianCoordinates([r, theta])
x = Math.round(x + radius)
y = Math.round(-y + radius)
this.#locationChangeCallback?.([x, y])
const hsva = this.getColor().toHSVA()
this.locationChangeCallback?.([x, y], radius, hsva[2], hsva[3])
return [x, y]
}
setLocationChangeCallback(callback) {
this.#locationChangeCallback = callback
getColor() {
return this.element.windowElement.template.color
}
}

View File

@@ -1,16 +1,14 @@
import { html } from "lit"
import { styleMap } from "lit/directives/style-map.js"
import ColorHandlerElement from "../element/ColorHandlerElement"
import Configuration from "../Configuration"
import ColorSliderElement from "../element/ColorSliderElement"
import LinearColorEntity from "../entity/LinearColorEntity"
import Utility from "../Utility"
import WindowTemplate from "./WindowTemplate"
/** @typedef {import("../element/WindowElement").default} WindowElement */
export default class ColorPickerWindowTemplate extends WindowTemplate {
static windowName = html`Color Picker`
/** @type {LinearColorEntity} */
#color
get color() {
@@ -34,26 +32,38 @@ export default class ColorPickerWindowTemplate extends WindowTemplate {
firstUpdated(changedProperties) {
const wheelHandler = new ColorHandlerElement()
const spectrumHandler = new ColorHandlerElement()
wheelHandler.template.setLocationChangeCallback(([x, y]) => {
const [r, theta] = Utility.getPolarCoordinates([x, y])
this.color = LinearColorEntity.fromWheelLocation(x, y)
})
this.element.querySelector(".ueb-color-picker-wheel").appendChild(new ColorHandlerElement())
const saturationSlider = new ColorSliderElement()
wheelHandler.template.locationChangeCallback = ([x, y], radius, v, a) => {
this.color = LinearColorEntity.fromWheelLocation([x, y], radius, v, a)
}
this.element.querySelector(".ueb-color-picker-wheel").appendChild(wheelHandler)
}
renderContent() {
const [h, s, v] = this.color.toHSVA()
const style = {
"--ueb-color-r": this.color.R.toString(),
"--ueb-color-g": this.color.G.toString(),
"--ueb-color-b": this.color.B.toString(),
"--ueb-color-a": this.color.A.toString(),
"--ueb-color-h": h.toString(),
"--ueb-color-s": s.toString(),
"--ueb-color-v": v.toString(),
}
return html`
<div class="ueb-color-picker"
.style="--ueb-color-r: ${this.color.R}; --ueb-color-g: ${this.color.G}; --ueb-color-b: ${this.color.B}; --ueb-color-a: ${this.color.A};">
<div class="ueb-color-picker" style=${styleMap(style)}>
<div class="ueb-color-picker-toolbar">
<div class="ueb-color-picker-theme"></div>
<div class="ueb-color-picker-srgb"></div>
</div>
<div class="ueb-color-picker-main">
<div class="ueb-color-picker-wheel"></div>
<div class="ueb-color-picker-saturation"></div>
<div class="ueb-color-picker-value"></div>
<div class="ueb-color-picker-saturation">
<div class="ueb-color-picker-slider"></div>
</div>
<div class="ueb-color-picker-value">
<div class="ueb-color-picker-slider"></div>
</div>
<div class="ueb-color-picker-preview">
<div class="ueb-color-picker-preview-old"></div>
<div class="ueb-color-picker-preview-new"></div>
@@ -77,7 +87,7 @@ export default class ColorPickerWindowTemplate extends WindowTemplate {
`
}
cleanup() {
this.element.blueprint.removeEventListener(Configuration.colorWindowEventName, this.colorWindowHandler)
renderWindowName() {
return html`Color Picker`
}
}

View File

@@ -0,0 +1,21 @@
import IDraggableControlTemplate from "./IDraggableControlTemplate"
import Utility from "../Utility"
/** @typedef {import("../element/ColorHandlerElement").default} ColorHandlerElement */
/** @extends {IDraggableControlTemplate<ColorHandlerElement>} */
export default class ColorSliderTemplate extends IDraggableControlTemplate {
/** @param {[Number, Number]} param0 */
adjustLocation([x, y]) {
x = 0
y = Utility.clamp(y, 0, this.movementSpaceSize[1])
const hsva = this.getColor().toHSVA()
this.locationChangeCallback?.([x, y])
return [x, y]
}
getColor() {
return this.element.windowElement.template.color
}
}

View File

@@ -0,0 +1,48 @@
import IDraggableTemplate from "./IDraggableTemplate"
import MouseMoveDraggable from "../input/mouse/MouseMoveDraggable"
/**
* @typedef {import("../element/IDraggableElement").default} IDraggableElement
*/
/**
* @template {IDraggableElement} T
* @extends {IDraggableTemplate<T>}
*/
export default class IDraggableControlTemplate extends IDraggableTemplate {
#locationChangeCallback = ([x, y], ...args) => [x, y]
get locationChangeCallback() {
return this.#locationChangeCallback
}
set locationChangeCallback(callback) {
this.#locationChangeCallback = callback
}
movementSpace
movementSpaceSize = [0, 0]
connectedCallback() {
super.connectedCallback()
this.movementSpace = this.element.parentElement
const bounding = this.movementSpace.getBoundingClientRect()
this.movementSpaceSize = [bounding.width, bounding.height]
}
createDraggableObject() {
return new MouseMoveDraggable(this.element, this.element.blueprint, {
draggableElement: this.movementSpace,
ignoreTranslateCompensate: true,
moveEverywhere: true,
movementSpace: this.movementSpace,
repositionOnClick: true,
stepSize: 1,
})
}
/** @param {[Number, Number]} param0 */
adjustLocation([x, y]) {
this.#locationChangeCallback([x, y])
return [x, y]
}
}

View File

@@ -31,7 +31,7 @@ export default class LinearColorPinTemplate extends IInputPinTemplate {
/** @param {LinearColorEntity} color */
setPinColor: color => this.element.setDefaultValue(color),
},
})
}),
]
}
@@ -47,7 +47,7 @@ export default class LinearColorPinTemplate extends IInputPinTemplate {
if (this.element.isInput()) {
return html`
<span class="ueb-pin-input" data-linear-color="${this.element.defaultValue.toString()}"
.style="--ueb-linear-color:rgba(${this.element.defaultValue.toString()})">
style="--ueb-linear-color:rgba(${this.element.defaultValue.toString()})">
</span>
`
}

View File

@@ -7,8 +7,6 @@ import MouseMoveDraggable from "../input/mouse/MouseMoveDraggable"
/** @extends {IDraggableTemplate<WindowElement>} */
export default class WindowTemplate extends IDraggableTemplate {
static windowName = html`Window`
toggleAdvancedDisplayHandler
getDraggableElement() {
@@ -24,20 +22,11 @@ export default class WindowTemplate extends IDraggableTemplate {
})
}
createInputObjects() {
return [
...super.createInputObjects(),
this.createDraggableObject(),
]
}
render() {
return html`
<div class="ueb-window">
<div class="ueb-window-top">
<div class="ueb-window-name ueb-ellipsis-nowrap-text">${
// @ts-expect-error
this.constructor.windowName}</div>
<div class="ueb-window-name ueb-ellipsis-nowrap-text">${this.renderWindowName()}</div>
<div class="ueb-window-close">
<svg viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
<line x1="2" y1="2" x2="30" y2="30" stroke="currentColor" stroke-width="4" />
@@ -52,6 +41,10 @@ export default class WindowTemplate extends IDraggableTemplate {
`
}
renderWindowName() {
return html`Window`
}
renderContent() {
return html``
}