Skip to content

Commit

Permalink
double, triple, quadruple click
Browse files Browse the repository at this point in the history
  • Loading branch information
guzba committed Oct 24, 2021
1 parent 94688fc commit 2ee96e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/windy/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type
MouseRight
MouseMiddle
DoubleClick
TripleClick
QuadrupleClick
Key0
Key1
Key2
Expand Down
26 changes: 21 additions & 5 deletions src/windy/platforms/win32/platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var

initialized: bool
doubleClickInterval: float64
prevClickTime: float64
prevClickTimes: array[3, float64]
windows: seq[Window]

proc indexForHandle(windows: seq[Window], hWnd: HWND): int =
Expand Down Expand Up @@ -329,14 +329,30 @@ proc handleButtonPress(window: Window, button: Button) =

if button == MouseLeft:
let
clickTime = cpuTime()
intervalSinceLastClick = clickTime - prevClickTime
if intervalSinceLastClick <= doubleClickInterval:
clickTime = epochTime()
clickIntervals = [
clickTime - prevClickTimes[0],
clickTime - prevClickTimes[1],
clickTime - prevClickTimes[2]
]

if clickIntervals[0] <= doubleClickInterval:
window.handleButtonPress(DoubleClick)
prevClickTime = clickTime
if clickIntervals[1] <= 2 * doubleClickInterval:
window.handleButtonPress(TripleClick)
if clickIntervals[2] <= 3 * doubleClickInterval:
window.handleButtonPress(QuadrupleClick)

prevClickTimes[2] = prevClickTimes[1]
prevClickTimes[1] = prevClickTimes[0]
prevClickTimes[0] = clickTime

proc handleButtonRelease(window: Window, button: Button) =
if button == MouseLeft:
if QuadrupleClick in window.buttonDown:
window.handleButtonRelease(QuadrupleClick)
if TripleClick in window.buttonDown:
window.handleButtonRelease(TripleClick)
if DoubleClick in window.buttonDown:
window.handleButtonRelease(DoubleClick)

Expand Down

0 comments on commit 2ee96e4

Please sign in to comment.