Skip to content

Commit

Permalink
refactor(imgui): update mouse hover handling
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 12, 2019
1 parent d381d33 commit 8e907e0
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 72 deletions.
11 changes: 4 additions & 7 deletions packages/imgui/src/components/button.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { pointInside, rect } from "@thi.ng/geom";
import { rect } from "@thi.ng/geom";
import { IShape } from "@thi.ng/geom-api";
import { hash, ReadonlyVec, ZERO2 } from "@thi.ng/vectors";
import { IGridLayout, LayoutBox, MouseButton } from "../api";
import { IGridLayout, LayoutBox } from "../api";
import { handleButtonKeys } from "../behaviors/button";
import { IMGUI } from "../gui";
import { isLayout } from "../layout";
Expand Down Expand Up @@ -80,12 +80,9 @@ export const buttonRaw = (
info?: string
) => {
gui.registerID(id, hash);
const hover = pointInside(shape, gui.mouse);
const hover = gui.isHover(id, shape);
if (hover) {
gui.hotID = id;
if (gui.activeID === "" && gui.buttons & MouseButton.LEFT) {
gui.activeID = id;
}
gui.isMouseDown() && (gui.activeID = id);
info && tooltipRaw(gui, info);
}
const focused = gui.requestFocus(id);
Expand Down
17 changes: 8 additions & 9 deletions packages/imgui/src/components/dial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
TAU
} from "@thi.ng/math";
import { cartesian2, hash } from "@thi.ng/vectors";
import { LayoutBox, MouseButton } from "../api";
import { LayoutBox } from "../api";
import { dialVal } from "../behaviors/dial";
import { handleSlider1Keys } from "../behaviors/slider";
import { IMGUI } from "../gui";
Expand Down Expand Up @@ -70,18 +70,17 @@ export const dialRaw = (
) => {
const r = Math.min(w, h) / 2;
const pos = [x + w / 2, y + h / 2];
const key = hash([x, y, r]);
gui.registerID(id, key);
const hover = pointInCircle(gui.mouse, pos, r);
let active = false;
const thetaGap = PI / 2;
const startTheta = HALF_PI + thetaGap / 2;
const key = hash([x, y, r]);
gui.registerID(id, key);
const aid = gui.activeID;
const hover =
aid === id || (aid === "" && pointInCircle(gui.mouse, pos, r));
if (hover) {
gui.hotID = id;
const aid = gui.activeID;
if ((aid === "" || aid === id) && gui.buttons & MouseButton.LEFT) {
if (gui.isMouseDown()) {
gui.activeID = id;
active = true;
val[i] = dialVal(
gui.mouse,
pos,
Expand Down Expand Up @@ -125,5 +124,5 @@ export const dialRaw = (
return true;
}
gui.lastID = id;
return active;
return gui.activeID === id;
};
12 changes: 5 additions & 7 deletions packages/imgui/src/components/ring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from "@thi.ng/math";
import { map, normRange } from "@thi.ng/transducers";
import { cartesian2, hash, Vec } from "@thi.ng/vectors";
import { MouseButton } from "../api";
import { dialVal } from "../behaviors/dial";
import { handleSlider1Keys } from "../behaviors/slider";
import { IMGUI } from "../gui";
Expand Down Expand Up @@ -99,16 +98,15 @@ export const ringRaw = (
const key = hash([x, y, r]);
gui.registerID(id, key);
const pos = [x + r, y + r];
const hover = pointInRect(gui.mouse, [x, y], [w, h]);
let active = false;
const startTheta = HALF_PI + thetaGap / 2;
const endTheta = HALF_PI + TAU - thetaGap / 2;
const aid = gui.activeID;
const hover =
aid === id || (aid === "" && pointInRect(gui.mouse, [x, y], [w, h]));
if (hover) {
gui.hotID = id;
const aid = gui.activeID;
if ((aid === "" || aid === id) && gui.buttons & MouseButton.LEFT) {
if (gui.isMouseDown()) {
gui.activeID = id;
active = true;
val[i] = dialVal(
gui.mouse,
pos,
Expand Down Expand Up @@ -161,5 +159,5 @@ export const ringRaw = (
return true;
}
gui.lastID = id;
return active;
return gui.activeID === id;
};
14 changes: 5 additions & 9 deletions packages/imgui/src/components/sliderh.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Fn } from "@thi.ng/api";
import { pointInside, rect } from "@thi.ng/geom";
import { rect } from "@thi.ng/geom";
import { fit, norm } from "@thi.ng/math";
import { hash } from "@thi.ng/vectors";
import { IGridLayout, LayoutBox, MouseButton } from "../api";
import { IGridLayout, LayoutBox } from "../api";
import { handleSlider1Keys, slider1Val } from "../behaviors/slider";
import { IMGUI } from "../gui";
import { isLayout } from "../layout";
Expand Down Expand Up @@ -70,14 +70,10 @@ export const sliderHRaw = (
const key = hash([x, y, w, h]);
gui.registerID(id, key);
const box = gui.resource(id, key, () => rect([x, y], [w, h], {}));
const hover = pointInside(box, gui.mouse);
let active = false;
const hover = gui.isHover(id, box);
if (hover) {
gui.hotID = id;
const aid = gui.activeID;
if ((aid === "" || aid === id) && gui.buttons & MouseButton.LEFT) {
if (gui.isMouseDown()) {
gui.activeID = id;
active = true;
val[i] = slider1Val(
fit(gui.mouse[0], x, x + w - 1, min, max),
min,
Expand Down Expand Up @@ -109,5 +105,5 @@ export const sliderHRaw = (
return true;
}
gui.lastID = id;
return active;
return gui.activeID === id;
};
14 changes: 5 additions & 9 deletions packages/imgui/src/components/sliderv.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Fn } from "@thi.ng/api";
import { pointInside, rect } from "@thi.ng/geom";
import { rect } from "@thi.ng/geom";
import { fit, norm } from "@thi.ng/math";
import { hash, ZERO2 } from "@thi.ng/vectors";
import { IGridLayout, LayoutBox, MouseButton } from "../api";
import { IGridLayout, LayoutBox } from "../api";
import { handleSlider1Keys, slider1Val } from "../behaviors/slider";
import { IMGUI } from "../gui";
import { isLayout } from "../layout";
Expand Down Expand Up @@ -71,15 +71,11 @@ export const sliderVRaw = (
const key = hash([x, y, w, h]);
gui.registerID(id, key);
const box = gui.resource(id, key, () => rect([x, y], [w, h], {}));
const hover = pointInside(box, gui.mouse);
const ymax = y + h;
let active = false;
const hover = gui.isHover(id, box);
if (hover) {
gui.hotID = id;
const aid = gui.activeID;
if ((aid === "" || aid === id) && gui.buttons & MouseButton.LEFT) {
if (gui.isMouseDown()) {
gui.activeID = id;
active = true;
val[i] = slider1Val(
fit(gui.mouse[1], ymax - 1, y, min, max),
min,
Expand Down Expand Up @@ -122,5 +118,5 @@ export const sliderVRaw = (
return true;
}
gui.lastID = id;
return active;
return gui.activeID === id;
};
16 changes: 5 additions & 11 deletions packages/imgui/src/components/textfield.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { Predicate } from "@thi.ng/api";
import { pointInside, rect } from "@thi.ng/geom";
import { rect } from "@thi.ng/geom";
import { fitClamped } from "@thi.ng/math";
import { hash } from "@thi.ng/vectors";
import {
IGridLayout,
Key,
LayoutBox,
MouseButton
} from "../api";
import { IGridLayout, Key, LayoutBox } from "../api";
import { IMGUI } from "../gui";
import { isLayout } from "../layout";
import { textLabelRaw } from "./textlabel";
Expand Down Expand Up @@ -48,11 +43,10 @@ export const textFieldRaw = (
const key = hash([x, y, w, h]);
gui.registerID(id, key);
const box = gui.resource(id, key, () => rect([x, y], [w, h], {}));
const hover = pointInside(box, gui.mouse);
const hover = gui.isHover(id, box);
if (hover) {
gui.hotID = id;
if (gui.buttons & MouseButton.LEFT) {
gui.activeID === "" && (gui.activeID = id);
if (gui.isMouseDown()) {
gui.activeID = id;
label[1] = Math.min(
Math.round(
fitClamped(
Expand Down
11 changes: 4 additions & 7 deletions packages/imgui/src/components/toggle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pointInside, rect } from "@thi.ng/geom";
import { rect } from "@thi.ng/geom";
import { hash } from "@thi.ng/vectors";
import { IGridLayout, LayoutBox, MouseButton } from "../api";
import { IGridLayout, LayoutBox } from "../api";
import { handleButtonKeys } from "../behaviors/button";
import { IMGUI } from "../gui";
import { isLayout } from "../layout";
Expand Down Expand Up @@ -63,12 +63,9 @@ export const toggleRaw = (
const key = hash([x, y, w, h]);
gui.registerID(id, key);
const box = gui.resource(id, key, () => rect([x, y], [w, h]));
const hover = pointInside(box, gui.mouse);
const hover = gui.isHover(id, box);
if (hover) {
gui.hotID = id;
if (gui.activeID === "" && gui.buttons & MouseButton.LEFT) {
gui.activeID = id;
}
gui.isMouseDown() && (gui.activeID = id);
info && tooltipRaw(gui, info);
}
const focused = gui.requestFocus(id);
Expand Down
21 changes: 8 additions & 13 deletions packages/imgui/src/components/xypad.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Fn } from "@thi.ng/api";
import { line, pointInside, rect } from "@thi.ng/geom";
import { line, rect } from "@thi.ng/geom";
import { fit2, hash, Vec } from "@thi.ng/vectors";
import { IGridLayout, LayoutBox, MouseButton } from "../api";
import { IGridLayout, LayoutBox } from "../api";
import { handleSlider2Keys, slider2Val } from "../behaviors/slider";
import { IMGUI } from "../gui";
import { textLabelRaw } from "./textlabel";
import { tooltipRaw } from "./tooltip";

/**
*
* `mode` interpretation:
*
* - -2 = square
Expand Down Expand Up @@ -90,22 +89,18 @@ export const xyPadRaw = (
fmt?: Fn<Vec, string>,
info?: string
) => {
const maxX = x + w - 1;
const maxY = y + h - 1;
const maxX = x + w;
const maxY = y + h;
const pos = yUp ? [x, maxY] : [x, y];
const maxPos = yUp ? [maxX, y] : [maxX, y + h - 1];
const maxPos = yUp ? [maxX, y] : [maxX, maxY];
const key = hash([x, y, w, h]);
gui.registerID(id, key);
const box = gui.resource(id, key, () => rect([x, y], [w, h]));
const col = gui.textColor(false);
const hover = pointInside(box, gui.mouse);
let active = false;
const hover = gui.isHover(id, box);
if (hover) {
gui.hotID = id;
const aid = gui.activeID;
if ((aid === "" || aid === id) && gui.buttons & MouseButton.LEFT) {
if (gui.isMouseDown()) {
gui.activeID = id;
active = true;
slider2Val(
fit2(val, gui.mouse, pos, maxPos, min, max),
min,
Expand Down Expand Up @@ -140,5 +135,5 @@ export const xyPadRaw = (
return true;
}
gui.lastID = id;
return active;
return gui.activeID === id;
};
14 changes: 14 additions & 0 deletions packages/imgui/src/gui.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Fn0, IToHiccup } from "@thi.ng/api";
import { pointInside } from "@thi.ng/geom";
import { IShape } from "@thi.ng/geom-api";
import { setC2, Vec } from "@thi.ng/vectors";
import {
DEFAULT_THEME,
Expand Down Expand Up @@ -125,6 +127,18 @@ export class IMGUI implements IToHiccup {
this.key = "";
}

isHover(id: string, shape: IShape) {
const aid = this.activeID;
const hover =
aid === id || (aid === "" && pointInside(shape, this.mouse));
hover && (this.hotID = id);
return hover;
}

isMouseDown() {
return this.buttons & MouseButton.LEFT;
}

isShiftDown() {
return (this.modifiers & KeyModifier.SHIFT) > 0;
}
Expand Down

0 comments on commit 8e907e0

Please sign in to comment.