From 360a8110ba12e92d2b20e11f0a9336011aff9bfb Mon Sep 17 00:00:00 2001 From: Nimaoth Date: Tue, 3 Oct 2023 18:04:47 +0200 Subject: [PATCH] fixed some bugs, set cursor when clicking --- src/id.nim | 1 + src/model_document.nim | 2 +- .../widget_builder_model_document.nim | 118 ++++++++++++------ 3 files changed, 79 insertions(+), 42 deletions(-) diff --git a/src/id.nim b/src/id.nim index e30083ca..5e94d553 100644 --- a/src/id.nim +++ b/src/id.nim @@ -61,6 +61,7 @@ proc hashInt32(x: uint32): uint32 {.inline.} = result = ((result shr 16) xor result) * 0x45d9f3b result = ((result shr 16) xor result) * 0x45d9f3b result = (result shr 16) xor result + result = result and 0x7FFFFFFF.uint32 proc hash*(oid: Oid): Hash = ## Generates the hash of an OID for use in hashtables. diff --git a/src/model_document.nim b/src/model_document.nim index 02496b2d..bac7c2c5 100644 --- a/src/model_document.nim +++ b/src/model_document.nim @@ -152,7 +152,7 @@ type targetNodeOld*: UINode targetNode*: UINode targetCell*: Cell - handleClick*: proc(node: UINode, cell: Cell, path: seq[int]) + handleClick*: proc(node: UINode, cell: Cell, path: seq[int], cursor: CellCursor) proc `$`(op: ModelOperation): string = result = fmt"{op.kind}, '{op.value}'" diff --git a/src/platform/widget_builder_model_document.nim b/src/platform/widget_builder_model_document.nim index a6f0b29c..cc07ed6f 100644 --- a/src/platform/widget_builder_model_document.nim +++ b/src/platform/widget_builder_model_document.nim @@ -1,6 +1,6 @@ import std/[strformat, tables, sugar, strutils, json] import util, app, document_editor, model_document, text/text_document, custom_logger, platform, theme, config_provider, input -import widget_builders_base, widget_library, ui/node +import widget_builders_base, widget_library, ui/node, custom_unicode import vmath, bumpy, chroma import ast/[types, cells] @@ -260,6 +260,17 @@ proc createCompletions(self: ModelDocumentEditor, builder: UINodeBuilder, app: A completionsPanel.rawY = cursorBounds.y completionsPanel.pivot = vec2(0, 1) +proc getCursorPos(builder: UINodeBuilder, line: openArray[char], startOffset: RuneIndex, pos: Vec2): int = + var offsetFromLeft = pos.x / builder.charWidth + if false: # self.isThickCursor(): # todo + offsetFromLeft -= 0.0 + else: + offsetFromLeft += 0.5 + + let index = clamp(offsetFromLeft.int, 0, line.runeLen.int) + let byteIndex = line.runeOffset(startOffset + index.RuneCount) + return byteIndex + method createCellUI*(cell: Cell, builder: UINodeBuilder, app: App, ctx: CellLayoutContext, updateContext: UpdateContext, spaceLeft: bool, path: openArray[int]) {.base.} = discard method createCellUI*(cell: ConstantCell, builder: UINodeBuilder, app: App, ctx: CellLayoutContext, updateContext: UpdateContext, spaceLeft: bool, path: openArray[int]) = @@ -281,9 +292,14 @@ method createCellUI*(cell: ConstantCell, builder: UINodeBuilder, app: App, ctx: # builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color, userId = cell.id.newPrimaryId): builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color): updateContext.cellToWidget[cell.id] = currentNode - onClick MouseButton.Left: - capture cellPath: - updateContext.handleClick(currentNode, cell, cellPath) + onClickAny btn: + if btn == MouseButton.Left: + if cell.canSelect: + let offset = builder.getCursorPos(text, 0.RuneIndex, pos) + let cursor = cell.toCursor(offset) + capture cellPath: + updateContext.handleClick(currentNode, cell, cellPath, cursor) + # widget.fontSizeIncreasePercent = cell.fontSizeIncreasePercent # setBackgroundColor(app, cell, widget) @@ -306,9 +322,13 @@ method createCellUI*(cell: PlaceholderCell, builder: UINodeBuilder, app: App, ct # builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color, userId = cell.id.newPrimaryId): builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color): updateContext.cellToWidget[cell.id] = currentNode - onClick MouseButton.Left: - capture cellPath: - updateContext.handleClick(currentNode, cell, cellPath) + onClickAny btn: + if btn == MouseButton.Left: + if cell.canSelect: + let offset = builder.getCursorPos(text, 0.RuneIndex, pos) + let cursor = cell.toCursor(offset) + capture cellPath: + updateContext.handleClick(currentNode, cell, cellPath, cursor) # widget.fontSizeIncreasePercent = cell.fontSizeIncreasePercent # setBackgroundColor(app, cell, widget) @@ -331,14 +351,18 @@ method createCellUI*(cell: AliasCell, builder: UINodeBuilder, app: App, ctx: Cel # builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color, userId = cell.id.newPrimaryId): builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color): updateContext.cellToWidget[cell.id] = currentNode - onClick MouseButton.Left: - capture cellPath: - updateContext.handleClick(currentNode, cell, cellPath) + onClickAny btn: + if btn == MouseButton.Left: + if cell.canSelect: + let offset = builder.getCursorPos(text, 0.RuneIndex, pos) + let cursor = cell.toCursor(offset) + capture cellPath: + updateContext.handleClick(currentNode, cell, cellPath, cursor) # widget.fontSizeIncreasePercent = cell.fontSizeIncreasePercent # setBackgroundColor(app, cell, widget) -method createCellUI*(cell: NodeReferenceCell, builder: UINodeBuilder, app: App, ctx: CellLayoutContext, updateContext: UpdateContext, spaceLeft: bool, path: openArray[int]) = +method createCellUI*(cell: PropertyCell, builder: UINodeBuilder, app: App, ctx: CellLayoutContext, updateContext: UpdateContext, spaceLeft: bool, path: openArray[int]) = if cell.isVisible.isNotNil and not cell.isVisible(cell.node): return @@ -349,28 +373,25 @@ method createCellUI*(cell: NodeReferenceCell, builder: UINodeBuilder, app: App, stackSize.dec cell.logc fmt"createCellUI (ConstantCell) {path}" - if cell.child.isNil: - let reference = cell.node.reference(cell.reference) - let defaultColor = if cell.foregroundColor.a != 0: cell.foregroundColor else: color(1, 1, 1) - let textColor = if cell.themeForegroundColors.len == 0: defaultColor else: app.theme.anyColor(cell.themeForegroundColors, defaultColor) - - if spaceLeft: - ctx.addSpace() - - # builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = $reference, textColor = textColor, userId = cell.id.newPrimaryId): - builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = $reference, textColor = textColor): - updateContext.cellToWidget[cell.id] = currentNode - onClick MouseButton.Left: - capture cellPath: - updateContext.handleClick(currentNode, cell, cellPath) + if spaceLeft: + ctx.addSpace() - else: - cell.child.createCellUI(builder, app, ctx, updateContext, spaceLeft, path) + let (text, color) = app.getTextAndColor(cell) + # builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color, userId = cell.id.newPrimaryId): + builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color): + updateContext.cellToWidget[cell.id] = currentNode + onClickAny btn: + if btn == MouseButton.Left: + if cell.canSelect: + let offset = builder.getCursorPos(text, 0.RuneIndex, pos) + let cursor = cell.toCursor(offset) + capture cellPath: + updateContext.handleClick(currentNode, cell, cellPath, cursor) # widget.fontSizeIncreasePercent = cell.fontSizeIncreasePercent # setBackgroundColor(app, cell, widget) -method createCellUI*(cell: PropertyCell, builder: UINodeBuilder, app: App, ctx: CellLayoutContext, updateContext: UpdateContext, spaceLeft: bool, path: openArray[int]) = +method createCellUI*(cell: NodeReferenceCell, builder: UINodeBuilder, app: App, ctx: CellLayoutContext, updateContext: UpdateContext, spaceLeft: bool, path: openArray[int]) = if cell.isVisible.isNotNil and not cell.isVisible(cell.node): return @@ -381,16 +402,28 @@ method createCellUI*(cell: PropertyCell, builder: UINodeBuilder, app: App, ctx: stackSize.dec cell.logc fmt"createCellUI (ConstantCell) {path}" - if spaceLeft: - ctx.addSpace() + if cell.child.isNil: + let reference = cell.node.reference(cell.reference) + let defaultColor = if cell.foregroundColor.a != 0: cell.foregroundColor else: color(1, 1, 1) + let textColor = if cell.themeForegroundColors.len == 0: defaultColor else: app.theme.anyColor(cell.themeForegroundColors, defaultColor) - let (text, color) = app.getTextAndColor(cell) - # builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color, userId = cell.id.newPrimaryId): - builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color): - updateContext.cellToWidget[cell.id] = currentNode - onClick MouseButton.Left: - capture cellPath: - updateContext.handleClick(currentNode, cell, cellPath) + if spaceLeft: + ctx.addSpace() + + # builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = $reference, textColor = textColor, userId = cell.id.newPrimaryId): + let text = $reference + builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = textColor): + updateContext.cellToWidget[cell.id] = currentNode + onClickAny btn: + if btn == MouseButton.Left: + if cell.canSelect: + let offset = builder.getCursorPos(text, 0.RuneIndex, pos) + let cursor = cell.toCursor(offset) + capture cellPath: + updateContext.handleClick(currentNode, cell, cellPath, cursor) + + else: + cell.child.createCellUI(builder, app, ctx, updateContext, spaceLeft, path) # widget.fontSizeIncreasePercent = cell.fontSizeIncreasePercent # setBackgroundColor(app, cell, widget) @@ -662,11 +695,12 @@ method createUI*(self: ModelDocumentEditor, builder: UINodeBuilder, app: App): s continue self.cellWidgetContext.targetNode = nil - self.cellWidgetContext.handleClick = proc(node: UINode, cell: Cell, cellPath: seq[int]) = + self.cellWidgetContext.handleClick = proc(node: UINode, cell: Cell, cellPath: seq[int], cursor: CellCursor) = let bounds = node.bounds.transformRect(node.parent, uiae.parent) targetCellPath = cellPath # debugf"click: {self.scrollOffset} -> {bounds.y} | {cell.dump} | {node.dump}" self.scrollOffset = bounds.y + self.cursor = cursor self.markDirty() # echo self.scrollOffset @@ -676,9 +710,11 @@ method createUI*(self: ModelDocumentEditor, builder: UINodeBuilder, app: App): s cell.createCellUI(builder, app, myCtx, self.cellWidgetContext, false, targetCellPath) myCtx.finish() - if self.cellWidgetContext.targetNodeOld.isNotNil and self.cellWidgetContext.targetNodeOld != self.cellWidgetContext.targetNode: - self.cellWidgetContext.targetNodeOld.contentDirty = true - self.cellWidgetContext.targetNode.contentDirty = true + if self.cellWidgetContext.targetNodeOld != self.cellWidgetContext.targetNode: + if self.cellWidgetContext.targetNodeOld.isNotNil: + self.cellWidgetContext.targetNodeOld.contentDirty = true + if self.cellWidgetContext.targetNode.isNotNil: + self.cellWidgetContext.targetNode.contentDirty = true self.cellWidgetContext.targetNodeOld = self.cellWidgetContext.targetNode