Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use nim set #19

Merged
merged 3 commits into from
Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 37 additions & 34 deletions src/windy/platforms/win32/platform.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ../../common, ../../internal, bitty, unicode, utils, vmath, windefs
import ../../common, ../../internal, unicode, utils, vmath, windefs

const
windowClassName = "WINDY0"
Expand Down Expand Up @@ -46,15 +46,15 @@ type
perFrame: PerFrame
trackMouseEventRegistered: bool
mousePos: IVec2
buttonPressed, buttonDown, buttonReleased, buttonToggle: BitArray
buttonPressed, buttonDown, buttonReleased, buttonToggle: set[Button]
exitFullscreenInfo: ExitFullscreenInfo

hWnd: HWND
hdc: HDC
hglrc: HGLRC

ButtonView* = object
states: BitArray
states: set[Button]

ExitFullscreenInfo = ref object
maximized: bool
Expand Down Expand Up @@ -316,18 +316,27 @@ proc loadLibraries() =
)

proc handleButtonPress(window: Window, button: Button) =
window.buttonDown[button.ord] = true
window.buttonPressed[button.ord] = true
window.buttonToggle[button.ord] = not window.buttonToggle[button.ord]
window.buttonDown.incl button
window.buttonPressed.incl button
if button in window.buttonToggle:
window.buttonToggle.excl button
else:
window.buttonToggle.incl button
if window.onButtonPress != nil:
window.onButtonPress(button)

proc handleButtonRelease(window: Window, button: Button) =
window.buttonDown[button.ord] = false
window.buttonReleased[button.ord] = true
window.buttonDown.excl button
window.buttonReleased.incl button
if window.onButtonRelease != nil:
window.onButtonRelease(button)

proc handleRune(window: Window, rune: Rune) =
if rune.uint32 < 32 or (rune.uint32 > 126 and rune.uint32 < 160):
return
if window.onRune != nil:
window.onRune(rune)

proc wndProc(
hWnd: HWND,
uMsg: UINT,
Expand Down Expand Up @@ -449,9 +458,7 @@ proc wndProc(
if uMsg == WM_UNICHAR and wParam == UNICODE_NOCHAR:
return TRUE
let codepoint = wParam.uint32
if codepoint >= 32 and not (codepoint > 126 and codepoint < 160):
if window.onRune != nil:
window.onRune(Rune(codepoint))
window.handleRune(Rune(codepoint))
return 0
else:
discard
Expand All @@ -471,8 +478,8 @@ proc pollEvents*() =
# Clear all per-frame data
for window in windows:
window.perFrame = PerFrame()
window.buttonPressed.clear()
window.buttonReleased.clear()
window.buttonPressed = {}
window.buttonReleased = {}

var msg: MSG
while PeekMessageW(msg.addr, 0, 0, 0, PM_REMOVE) > 0:
Expand All @@ -490,10 +497,10 @@ proc pollEvents*() =
if activeWindow != nil:
# When both shift keys are down the first one released does not trigger a
# key up event so we fake it here.
if activeWindow.buttonDown[KeyLeftShift.ord]:
if KeyLeftShift in activeWindow.buttonDown:
if (GetKeyState(VK_LSHIFT) and KF_UP) == 0:
activeWindow.handleButtonRelease(KeyLeftShift)
if activeWindow.buttonDown[KeyRightShift.ord]:
if KeyRightShift in activeWindow.buttonDown:
if (GetKeyState(VK_RSHIFT) and KF_UP) == 0:
activeWindow.handleButtonRelease(KeyRightShift)

Expand Down Expand Up @@ -566,21 +573,6 @@ proc mouseDelta*(window: Window): IVec2 =
proc scrollDelta*(window: Window): Vec2 =
window.perFrame.scrollDelta

proc buttonDown*(window: Window): ButtonView =
ButtonView(states: window.buttonDown)

proc buttonPressed*(window: Window): ButtonView =
ButtonView(states: window.buttonPressed)

proc buttonReleased*(window: Window): ButtonView =
ButtonView(states: window.buttonReleased)

proc buttonToggle*(window: Window): ButtonView =
ButtonView(states: window.buttonToggle)

proc `[]`*(buttonView: ButtonView, button: Button): bool =
buttonView.states[button.ord]

proc `title=`*(window: Window, title: string) =
window.title = title
var wideTitle = title.wstr()
Expand Down Expand Up @@ -772,10 +764,6 @@ proc newWindow*(
title,
size
)
result.buttonDown = newBitArray(Button.high.ord + 1)
result.buttonPressed = newBitArray(Button.high.ord + 1)
result.buttonReleased = newBitArray(Button.high.ord + 1)
result.buttonToggle = newBitArray(Button.high.ord + 1)

try:
result.hdc = getDC(result.hWnd)
Expand Down Expand Up @@ -865,3 +853,18 @@ proc newWindow*(
except WindyError as e:
destroy result
raise e

proc buttonDown*(window: Window): ButtonView =
ButtonView(states: window.buttonDown)

proc buttonPressed*(window: Window): ButtonView =
ButtonView(states: window.buttonPressed)

proc buttonReleased*(window: Window): ButtonView =
ButtonView(states: window.buttonReleased)

proc buttonToggle*(window: Window): ButtonView =
ButtonView(states: window.buttonToggle)

proc `[]`*(buttonView: ButtonView, button: Button): bool =
button in buttonView.states
1 change: 0 additions & 1 deletion windy.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ srcDir = "src"

requires "nim >= 1.4.8"
requires "vmath >= 1.1.0"
requires "bitty >= 0.1.2"

task bindings, "Generate bindings":

Expand Down