Skip to content

Commit

Permalink
added mouse event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Sep 26, 2023
1 parent 487f1e7 commit dde5e39
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 46 deletions.
17 changes: 13 additions & 4 deletions src/platform/browser_platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ method init*(self: BrowserPlatform) =

var input = toInput(ke.key, ke.code, ke.keyCode)
# debugf"{inputToString(input)}, {modifiers}"
self.onKeyPress.invoke (input, modifiers)
if not self.builder.handleKeyPressed(input, modifiers):
self.onKeyPress.invoke (input, modifiers)
)

self.content.addEventListener("wheel", proc(e: dom.Event) =
Expand Down Expand Up @@ -140,7 +141,8 @@ method init*(self: BrowserPlatform) =
let x = me.pageX.float - currentTargetRect.x
let y = me.pageY.float - currentTargetRect.y
# debugf"click {me.button}, {modifiers}, {x}, {y}"
self.onMousePress.invoke (mouseButton, modifiers, vec2(x.float, y.float))

var events = @[mouseButton]

if mouseButton == MouseButton.Left:
if self.doubleClickTimer.elapsed.float < self.doubleClickTime:
Expand All @@ -159,6 +161,10 @@ method init*(self: BrowserPlatform) =
else:
self.doubleClickCounter = 0

for event in events:
if not self.builder.handleMousePressed(event, modifiers, vec2(x.float, y.float)):
self.onMousePress.invoke (event, modifiers, vec2(x.float, y.float))

)

self.content.addEventListener("mouseup", proc(e: dom.Event) =
Expand All @@ -174,7 +180,9 @@ method init*(self: BrowserPlatform) =
let x = me.pageX.float - currentTargetRect.x
let y = me.pageY.float - currentTargetRect.y
# debugf"click {me.button}, {modifiers}, {x}, {y}"
self.onMouseRelease.invoke (mouseButton, modifiers, vec2(x.float, y.float))

if not self.builder.handleMouseReleased(mouseButton, modifiers, vec2(x.float, y.float)):
self.onMouseRelease.invoke (mouseButton, modifiers, vec2(x.float, y.float))
)

self.content.addEventListener("mousemove", proc(e: dom.Event) =
Expand All @@ -186,7 +194,8 @@ method init*(self: BrowserPlatform) =
let modifiers = me.getModifiers

# debugf"move {me.button}, {modifiers}, {me.clientX}, {me.clientY}, {me.movementX}, {me.movementY}, {me.getMouseButtons}"
self.onMouseMove.invoke (vec2(me.clientX.float, me.clientY.float), vec2(me.movementX.float, me.movementY.float), modifiers, me.getMouseButtons) # @todo: buttons
if not self.builder.handleMouseMoved(vec2(me.clientX.float, me.clientY.float), me.getMouseButtons):
self.onMouseMove.invoke (vec2(me.clientX.float, me.clientY.float), vec2(me.movementX.float, me.movementY.float), modifiers, me.getMouseButtons) # @todo: buttons
)

proc console[T](t: T) {.importjs: "console.log(#);".}
Expand Down
21 changes: 14 additions & 7 deletions src/platform/gui_platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ method init*(self: GuiPlatform) =

self.window.onMouseMove = proc() =
inc self.eventCounter
self.onMouseMove.invoke (self.window.mousePos.vec2, self.window.mouseDelta.vec2, {}, self.currentMouseButtons)
if not self.builder.handleMouseMoved(self.window.mousePos.vec2, self.currentMouseButtons):
self.onMouseMove.invoke (self.window.mousePos.vec2, self.window.mouseDelta.vec2, {}, self.currentMouseButtons)

proc toMouseButton(button: Button): MouseButton =
inc self.eventCounter
Expand All @@ -152,13 +153,15 @@ method init*(self: GuiPlatform) =
# debugf"button {button} {inputToString(button.toInput, self.currentModifiers)}"

if self.lastEvent.getSome(event):
self.onKeyPress.invoke event
if not self.builder.handleKeyPressed(event[0], event[1]):
self.onKeyPress.invoke event
self.lastEvent = (int64, Modifiers).none

case button
of MouseLeft, MouseRight, MouseMiddle, MouseButton4, MouseButton5, DoubleClick, TripleClick, QuadrupleClick:
self.currentMouseButtons.incl button.toMouseButton
self.onMousePress.invoke (button.toMouseButton, self.currentModifiers, self.window.mousePos.vec2)
if not self.builder.handleMousePressed(button.toMouseButton, self.currentModifiers, self.window.mousePos.vec2):
self.onMousePress.invoke (button.toMouseButton, self.currentModifiers, self.window.mousePos.vec2)
of KeyLeftShift, KeyRightShift: self.currentModifiers = self.currentModifiers + {Shift}
of KeyLeftControl, KeyRightControl: self.currentModifiers = self.currentModifiers + {Control}
of KeyLeftAlt, KeyRightAlt: self.currentModifiers = self.currentModifiers + {Alt}
Expand All @@ -170,19 +173,22 @@ method init*(self: GuiPlatform) =
inc self.eventCounter

if self.lastEvent.getSome(event):
self.onKeyPress.invoke event
if not self.builder.handleKeyPressed(event[0], event[1]):
self.onKeyPress.invoke event
self.lastEvent = (int64, Modifiers).none

case button
of MouseLeft, MouseRight, MouseMiddle, MouseButton4, MouseButton5, DoubleClick, TripleClick, QuadrupleClick:
self.currentMouseButtons.excl button.toMouseButton
self.onMouseRelease.invoke (button.toMouseButton, self.currentModifiers, self.window.mousePos.vec2)
if not self.builder.handleMouseReleased(button.toMouseButton, self.currentModifiers, self.window.mousePos.vec2):
self.onMouseRelease.invoke (button.toMouseButton, self.currentModifiers, self.window.mousePos.vec2)
of KeyLeftShift, KeyRightShift: self.currentModifiers = self.currentModifiers - {Shift}
of KeyLeftControl, KeyRightControl: self.currentModifiers = self.currentModifiers - {Control}
of KeyLeftAlt, KeyRightAlt: self.currentModifiers = self.currentModifiers - {Alt}
# of KeyLeftSuper, KeyRightSuper: currentModifiers = currentModifiers - {Super}
else:
self.onKeyRelease.invoke (button.toInput, self.currentModifiers)
if not self.builder.handleKeyPressed(button.toInput, self.currentModifiers):
self.onKeyRelease.invoke (button.toInput, self.currentModifiers)

method deinit*(self: GuiPlatform) =
self.window.close()
Expand Down Expand Up @@ -259,7 +265,8 @@ method processEvents*(self: GuiPlatform): int =
pollEvents()

if self.lastEvent.getSome(event):
self.onKeyPress.invoke event
if not self.builder.handleKeyPressed(event[0], event[1]):
self.onKeyPress.invoke event
self.lastEvent = (int64, Modifiers).none

if self.window.closeRequested:
Expand Down
49 changes: 43 additions & 6 deletions src/platform/widget_builder_text_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,19 @@ else:

proc updateBaseIndexAndScrollOffset*(height: float, previousBaseIndex: var int, scrollOffset: var float, lines: int, totalLineHeight: float, targetLine: Option[int])

proc getCursorPos(self: TextDocumentEditor, node: UINode, line: int, startOffset: RuneIndex, pos: Vec2): int =
var offsetFromLeft = pos.x / self.platform.charWidth
if self.isThickCursor():
offsetFromLeft -= 0.0
else:
offsetFromLeft += 0.5

let index = clamp(offsetFromLeft.int, 0, node.textRuneLen)
let byteIndex = self.document.lines[line].toOpenArray.runeOffset(startOffset + index.RuneCount)
return byteIndex

proc renderLine*(
builder: UINodeBuilder, app: App,
self: TextDocumentEditor, builder: UINodeBuilder, app: App,
line: StyledLine, lineOriginal: openArray[char],
lineId: int32, parentId: Id, curs: Option[int], cursorLine: int,
lineNumber: int, lineNumbers: LineNumbers,
Expand Down Expand Up @@ -73,15 +84,29 @@ proc renderLine*(
start += part.text.len
startRune += part.text.runeLen

let startRune = startRune

var partNode: UINode
builder.panel(&{DrawText, FillBackground, SizeToContentX, SizeToContentY}, text = part.text):
currentNode.backgroundColor = backgroundColor
currentNode.textColor = if part.scope.len == 0: textColor else: app.theme.tokenColor(part, textColor)

partNode = currentNode

onClick Left:
echo "clicked "
capture line, currentNode, startRune:
onClickAny btn:
if btn == Left:
let offset = self.getCursorPos(currentNode, line.index, startRune.RuneIndex, pos)
self.selection = (line.index, offset).toSelection
self.markDirty()
elif btn == TripleClick:
self.selection = ((line.index, 0), (line.index, self.document.lineLength(line.index)))
self.markDirty()

onDrag Left:
let offset = self.getCursorPos(currentNode, line.index, startRune.RuneIndex, pos)
self.selection = (line.index, offset).toSelection
self.markDirty()

# cursor
if curs.getSome(curs):
Expand All @@ -98,7 +123,19 @@ proc renderLine*(
result = some (currentNode, "", rect(lastPartXW, 0, builder.charWidth, builder.textHeight))

# Fill rest of line with background
builder.panel(&{FillX, FillY, FillBackground}, backgroundColor = backgroundColor)
builder.panel(&{FillX, FillY, FillBackground}, backgroundColor = backgroundColor):
capture line, currentNode:
onClickAny btn:
if btn == Left:
self.selection = (line.index, self.document.lineLength(line.index)).toSelection
self.markDirty()
elif btn == TripleClick:
self.selection = ((line.index, 0), (line.index, self.document.lineLength(line.index)))
self.markDirty()

onDrag Left:
self.selection = (line.index, self.document.lineLength(line.index)).toSelection
self.markDirty()

proc createHeader(self: TextDocumentEditor, builder: UINodeBuilder, app: App, headerColor: Color, textColor: Color): UINode =
if self.renderHeader:
Expand Down Expand Up @@ -170,7 +207,7 @@ proc createLines(self: TextDocumentEditor, builder: UINodeBuilder, app: App, bac
let column = if cursor.line == i: cursor.column.some else: int.none
let line = self.getStyledText i

if builder.renderLine(app, line, self.document.lines[i], self.document.lineIds[i], self.userId, column, cursorLine, i, lineNumbers, y, sizeToContentX, lineNumberWidth, lineNumberBounds.x, backgroundColor, textColor).getSome(cl):
if self.renderLine(builder, app, line, self.document.lines[i], self.document.lineIds[i], self.userId, column, cursorLine, i, lineNumbers, y, sizeToContentX, lineNumberWidth, lineNumberBounds.x, backgroundColor, textColor).getSome(cl):
result = cl.some

y = builder.currentChild.yh
Expand All @@ -186,7 +223,7 @@ proc createLines(self: TextDocumentEditor, builder: UINodeBuilder, app: App, bac
let column = if cursor.line == i: cursor.column.some else: int.none
let line = self.getStyledText i

if builder.renderLine(app, line, self.document.lines[i], self.document.lineIds[i], self.userId, column, cursorLine, i, lineNumbers, y, sizeToContentX, lineNumberWidth, lineNumberBounds.x, backgroundColor, textColor).getSome(cl):
if self.renderLine(builder, app, line, self.document.lines[i], self.document.lineIds[i], self.userId, column, cursorLine, i, lineNumbers, y, sizeToContentX, lineNumberWidth, lineNumberBounds.x, backgroundColor, textColor).getSome(cl):
result = cl.some

builder.currentChild.rawY = builder.currentChild.y - builder.currentChild.h
Expand Down
Loading

0 comments on commit dde5e39

Please sign in to comment.