Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotary-like control for angles #981

Merged
merged 29 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f716d1a
Rotary-like control for angles
fatih-erikli Nov 13, 2023
922e690
Update knob style
fatih-erikli Nov 14, 2023
d1caa68
Rotary control improvements, Add demo panel for rotary control
fatih-erikli Nov 14, 2023
3aeecdd
Implement wheel for knob, fix minus degree glitch
fatih-erikli Nov 14, 2023
74698ed
Delete number input in rotary control
fatih-erikli Nov 14, 2023
2f0e4f8
Rotary control should dispatch values between -180, 180
fatih-erikli Nov 15, 2023
33605b7
Handle mousemove event in an overlay, delete atan function, use dista…
fatih-erikli Nov 17, 2023
b79b1ca
Delete not necessary modus
fatih-erikli Nov 17, 2023
0f5b5e5
Delete unused functions
fatih-erikli Nov 17, 2023
4d4310f
Dispatch oppositewise angle
fatih-erikli Nov 17, 2023
56956c4
Delete trigonometric conversions
fatih-erikli Nov 17, 2023
fa554a5
Let -> const
fatih-erikli Nov 17, 2023
06efd31
Add dragBegin dragEnd to the dispatched event
fatih-erikli Nov 20, 2023
128004b
Delete onwheel, use dispatchEvent for continuous changes
fatih-erikli Nov 29, 2023
6df4913
Use html.input
fatih-erikli Nov 29, 2023
aabda3d
Delete RotaryControlDemoPanel
fatih-erikli Dec 22, 2023
c4372ef
Revert "Delete RotaryControlDemoPanel"
fatih-erikli Dec 25, 2023
d2fe75a
Angle in number input should be opposite side in knob
fatih-erikli Dec 25, 2023
186d384
Dragging the thumb up should adjust opposite side
fatih-erikli Dec 25, 2023
9b8754c
Delete RotaryControlDemoPanel
fatih-erikli Dec 22, 2023
b3f9d96
Cache rotation axis if the difference exceeds a threshold
fatih-erikli Dec 25, 2023
f2153aa
Revert "Cache rotation axis if the difference exceeds a threshold"
fatih-erikli Dec 25, 2023
0ae3bef
Adjust the angle only in vertical movement
fatih-erikli Dec 25, 2023
5c00ad7
Keep only clientY when drag begin
fatih-erikli Dec 25, 2023
1a01fd8
Deleted rotary control import in editor.js
fatih-erikli Dec 25, 2023
e54acb8
Delete vertical margin of the knob
fatih-erikli Dec 26, 2023
455f1a2
Delete .thumb child, use :before pseudo selector instead
fatih-erikli Jan 1, 2024
5e765f4
Use eplicit closure for the rotary changes; style tweak
justvanrossum Jan 2, 2024
07e7146
simplify
justvanrossum Jan 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions src/fontra/client/web-components/rotary-control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import * as html from "../core/html-utils.js";

export class RotaryControl extends html.UnlitElement {
static styles = `
:host {
--knob-size: 1.4rem;
--thumb-size: calc(var(--knob-size) / 5);
}

.knob {
width: var(--knob-size);
height: var(--knob-size);
border-radius: 50%;
background: #e3e3e3;
display: flex;
justify-content: center;
}

.thumb {
width: var(--thumb-size);
height: var(--thumb-size);
background: rgb(89, 89, 89);
border-radius: 50%;
margin-top: calc(var(--knob-size) / 8);
}

.rotary-control {
display: flex;
gap: 0.4rem;
margin: 0.2rem;
}

.overlay {
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 1;
}
`;

constructor() {
super();
this.onChangeCallback = () => {};
}

set value(value) {
this._value = value;
if (this.knob) {
this.knob.style.transform = `rotate(${this.value}deg)`;
}
}

get value() {
return this._value;
}

dispatch() {
const event = { value: this.value };

if (this.dragBegin) {
event.dragBegin = true;
this.dragBegin = false;
}

if (this.dragEnd) {
event.dragEnd = true;
this.dragEnd = false;
}

this.onChangeCallback(event);
}

attachOverlay() {
const overlay = html.div(
{
class: "overlay",
onmouseup: () => {
this.coordinatesDragBegin = undefined;
this.angleWhenDragStart = undefined;
this.shadowRoot.removeChild(overlay);
this.dragEnd = true;
this.dispatch();
},
onmousemove: (event) => {
if (this.coordinatesDragBegin === undefined) {
return;
}
const diffX = event.clientX - this.coordinatesDragBegin.x;
const diffY = event.clientY - this.coordinatesDragBegin.y;
const value =
this.angleWhenDragStart +
(Math.abs(diffX) > Math.abs(diffY) ? diffX : diffY);
this.value = value;
this.dispatch();
},
},
[]
);
this.overlay = overlay;
this.shadowRoot.appendChild(overlay);
}

render() {
return html.div({ class: "rotary-control" }, [
(this.knob = html.div(
{
class: "knob",
style: `transform: rotate(${this.value}deg);`,
onmousedown: (event) => {
this.coordinatesDragBegin = { x: event.clientX, y: event.clientY };
this.angleWhenDragStart = this.value;
this.dragBegin = true;
event.preventDefault();
this.attachOverlay();
},
},
[html.div({ class: "thumb" })]
)),
]);
}
}

customElements.define("rotary-control", RotaryControl);
44 changes: 44 additions & 0 deletions src/fontra/client/web-components/ui-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SimpleElement } from "../core/html-utils.js";
import { QueueIterator } from "../core/queue-iterator.js";
import { hyphenatedToCamelCase } from "../core/utils.js";
import { RangeSlider } from "/web-components/range-slider.js";
import "/web-components/rotary-control.js";

export class Form extends SimpleElement {
static styles = `
Expand Down Expand Up @@ -186,6 +187,49 @@ export class Form extends SimpleElement {
valueElement.appendChild(inputElement);
}

_addEditAngle(valueElement, fieldItem) {
const inputElement = html.input({
type: "number",
value: fieldItem.value,
step: "any",
disabled: fieldItem.disabled,
onchange: () => {
let value = parseFloat(inputElement.value);
this._fieldChanging(fieldItem.key, value);
rotaryControl.value = value;
},
});
let valueStream;
const rotaryControl = html.createDomElement("rotary-control", {
justvanrossum marked this conversation as resolved.
Show resolved Hide resolved
value: fieldItem.value,
onChangeCallback: (event) => {
const value = event.value * -1;
inputElement.value = value;
if (event.dragBegin) {
valueStream = new QueueIterator(5, true);
this._fieldChanging(fieldItem.key, value, valueStream);
}
if (valueStream) {
valueStream.put(value);
this._dispatchEvent("doChange", { key: fieldItem.key, value: value });
if (event.dragEnd) {
valueStream.done();
valueStream = undefined;
this._dispatchEvent("endChange", { key: fieldItem.key });
}
} else {
this._fieldChanging(fieldItem.key, value, undefined);
}
},
});

this._fieldGetters[fieldItem.key] = () => inputElement.value;
this._fieldSetters[fieldItem.key] = (value) => (inputElement.value = value);
valueElement.appendChild(
html.div({ style: "display: flex" }, [inputElement, rotaryControl])
);
}

_addEditNumberSlider(valueElement, fieldItem) {
const rangeElement = new RangeSlider();
rangeElement.value = fieldItem.value;
Expand Down
1 change: 1 addition & 0 deletions src/fontra/views/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { themeController } from "/core/theme-settings.js";
import { MenuItemDivider, showMenu } from "/web-components/menu-panel.js";
import { dialog, dialogSetup } from "/web-components/modal-dialog.js";
import { parsePluginBasePath } from "/web-components/plugin-manager.js";
import { RotaryControl } from "/web-components/rotary-control.js";
justvanrossum marked this conversation as resolved.
Show resolved Hide resolved

import DesignspaceNavigationPanel from "./panel-designspace-navigation.js";
import GlyphSearchPanel from "./panel-glyph-search.js";
Expand Down
2 changes: 1 addition & 1 deletion src/fontra/views/editor/panel-selection-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export default class SelectionInfoPanel extends Panel {
]) {
const value = component.transformation[key];
formContents.push({
type: "edit-number",
type: key === "rotation" ? "edit-angle" : "edit-number",
key: componentKey("transformation", key),
label: key,
value: value,
Expand Down