Skip to content

Commit

Permalink
Merge pull request #20 from guzba/master
Browse files Browse the repository at this point in the history
double, triple, quadruple click
  • Loading branch information
treeform authored Oct 24, 2021
2 parents 21f2f24 + 2ee96e4 commit 4219cf3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/windy/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type
MouseLeft
MouseRight
MouseMiddle
DoubleClick
TripleClick
QuadrupleClick
Key0
Key1
Key2
Expand Down Expand Up @@ -61,7 +64,7 @@ type
KeyBackslash
KeyCapsLock
KeySemicolon
KeyApostraphe
KeyApostrophe
KeyEnter
KeyLeftShift
KeyComma
Expand Down Expand Up @@ -116,7 +119,7 @@ type
NumpadDecimal
NumpadEnter
NumpadAdd
NumbadSubtract
NumpadSubtract
NumpadMultiply
NumpadDivide
NumpadEqual
35 changes: 33 additions & 2 deletions src/windy/platforms/win32/platform.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ../../common, ../../internal, unicode, utils, vmath, windefs
import ../../common, ../../internal, times, unicode, utils, vmath, windefs

const
windowClassName = "WINDY0"
Expand Down Expand Up @@ -80,6 +80,8 @@ var
onQuitRequest*: Callback

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

proc indexForHandle(windows: seq[Window], hWnd: HWND): int =
Expand All @@ -101,7 +103,7 @@ proc registerWindowClass(windowClassName: string, wndProc: WNDPROC) =

var wc: WNDCLASSEXW
wc.cbSize = sizeof(WNDCLASSEXW).UINT
wc.style = CS_HREDRAW or CS_VREDRAW or CS_DBLCLKS
wc.style = CS_HREDRAW or CS_VREDRAW
wc.lpfnWndProc = wndProc
wc.hInstance = GetModuleHandleW(nil)
wc.hCursor = LoadCursorW(0, IDC_ARROW)
Expand Down Expand Up @@ -325,7 +327,35 @@ proc handleButtonPress(window: Window, button: Button) =
if window.onButtonPress != nil:
window.onButtonPress(button)

if button == MouseLeft:
let
clickTime = epochTime()
clickIntervals = [
clickTime - prevClickTimes[0],
clickTime - prevClickTimes[1],
clickTime - prevClickTimes[2]
]

if clickIntervals[0] <= doubleClickInterval:
window.handleButtonPress(DoubleClick)
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)

window.buttonDown.excl button
window.buttonReleased.incl button
if window.onButtonRelease != nil:
Expand Down Expand Up @@ -472,6 +502,7 @@ proc init*() =
discard SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)
loadOpenGL()
registerWindowClass(windowClassName, wndProc)
doubleClickInterval = GetDoubleClickTime().float64 / 1000
initialized = true

proc pollEvents*() =
Expand Down
4 changes: 2 additions & 2 deletions src/windy/platforms/win32/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const scancodeToButton* = block:
s[0x02b] = KeyBackslash
s[0x03a] = KeyCapsLock
s[0x027] = KeySemicolon
s[0x028] = KeyApostraphe
s[0x028] = KeyApostrophe
s[0x01c] = KeyEnter
s[0x02a] = KeyLeftShift
s[0x033] = KeyComma
Expand Down Expand Up @@ -133,7 +133,7 @@ const scancodeToButton* = block:
s[0x053] = NumpadDecimal
s[0x11c] = NumpadEnter
s[0x04e] = NumpadAdd
s[0x04a] = NumbadSubtract
s[0x04a] = NumpadSubtract
s[0x037] = NumpadMultiply
s[0x135] = NumpadDivide
s[0x059] = NumpadEqual
Expand Down
2 changes: 2 additions & 0 deletions src/windy/platforms/win32/windefs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ proc SendMessageW*(
lParam: LPARAM
): LRESULT {.importc, stdcall, dynlib: "User32".}

proc GetDoubleClickTime*(): UINT {.importc, stdcall, dynlib: "User32".}

proc ChoosePixelFormat*(
hdc: HDC,
ppfd: ptr PIXELFORMATDESCRIPTOR
Expand Down

0 comments on commit 4219cf3

Please sign in to comment.