Skip to content

Commit dce8af0

Browse files
committed
context menu improvements
1 parent b5a8204 commit dce8af0

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

src/contextMenu.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,40 @@ export class ContextMenu {
232232

233233
#submenuPosition: Vector2;
234234

235-
public render(ctx: CanvasRenderingContext2D, position: Vector2, graphScale: number, mousePosition: Vector2 | undefined): ContextEntry | null {
235+
public render(ctx: CanvasRenderingContext2D, pppp: Vector2, graphScale: number, mousePosition: Vector2 | undefined, openRight: boolean): ContextEntry | null {
236236
const menuScale = 1.25;
237237
const scaledEntryHeight = menuScale * contextEntryHeight;
238-
const scaledEntryWidth = menuScale * (this.#getMaxWidthForText(ctx, menuScale) + 20); // contextEntryWidth;
238+
const scaledEntryWidth = (menuScale * 40) + (this.#getMaxWidthForText(ctx, menuScale)); // contextEntryWidth;
239239

240240
let totalScaledHeight = 0
241241
for (let i = 0; i < this.#groups.Count(); i++) {
242242
totalScaledHeight += this.#groups.At(i).height();
243243
}
244244
totalScaledHeight *= menuScale;
245245

246+
const position = { x: 0, y: 0 };
247+
CopyVector2(position, pppp)
248+
249+
if (!openRight) {
250+
position.x -= scaledEntryWidth;
251+
}
252+
253+
// Clamp the position so it's not spilling off the canvas
254+
if (position.y + totalScaledHeight > ctx.canvas.clientHeight) {
255+
position.y = ctx.canvas.clientHeight - totalScaledHeight;
256+
}
257+
258+
let submenuOpenRight = openRight;
259+
if (openRight && position.x + scaledEntryWidth > ctx.canvas.clientWidth) {
260+
position.x = ctx.canvas.clientWidth - scaledEntryWidth;
261+
submenuOpenRight = !submenuOpenRight;
262+
}
263+
246264
this.#tempBox.Size.x = scaledEntryWidth;
247265
this.#tempBox.Size.y = scaledEntryHeight;
248266
CopyVector2(this.#tempBox.Position, position)
249267

250-
// ctx.canvas.clientHeight
268+
251269

252270
ctx.textAlign = TextAlign.Left;
253271
ctx.fillStyle = Theme.ContextMenu.BackgroundColor;
@@ -281,7 +299,10 @@ export class ContextMenu {
281299
entryMousedOver = true
282300
if (entry.subMenu !== undefined) {
283301
this.#openSubMenu = entry.subMenu;
284-
this.#submenuPosition = { x: position.x + scaledEntryWidth, y: this.#tempBox.Position.y }
302+
this.#submenuPosition = { x: position.x, y: this.#tempBox.Position.y }
303+
if (submenuOpenRight) {
304+
this.#submenuPosition.x += scaledEntryWidth
305+
}
285306
subOpenedThisFrame = true;
286307
} else {
287308
this.#openSubMenu = undefined;
@@ -332,7 +353,7 @@ export class ContextMenu {
332353
}
333354

334355
if (this.#openSubMenu !== undefined) {
335-
const mouseOverSub = this.#openSubMenu.render(ctx, this.#submenuPosition, menuScale, mousePosition)
356+
const mouseOverSub = this.#openSubMenu.render(ctx, this.#submenuPosition, menuScale, mousePosition, submenuOpenRight)
336357
if (mouseOverSub !== null) {
337358
mouseIsOver = mouseOverSub;
338359
} else if (!subOpenedThisFrame) {

src/graph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ export class NodeFlowGraph {
451451
if (this.#openedContextMenu !== null) {
452452
const pos = VectorPool.get();
453453
this.#camera.graphSpaceToScreenSpace(this.#openedContextMenu.Position, pos)
454-
this.#contextMenuEntryHovering = this.#openedContextMenu.Menu.render(this.#ctx, pos, this.#camera.zoom, this.#mousePosition);
454+
this.#contextMenuEntryHovering = this.#openedContextMenu.Menu.render(this.#ctx, pos, this.#camera.zoom, this.#mousePosition, true);
455455

456456
if (this.#contextMenuEntryHovering !== null) {
457457
this.#cursor = CursorStyle.Pointer;

0 commit comments

Comments
 (0)