Skip to content

Commit

Permalink
fix missing logics of attaching action data; aump 0.0.23
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Dec 2, 2020
1 parent 8c11dda commit a0907ec
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 24 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### Usage

```nim
requires "https://github.com/Quamolit/json-paint.nim#v0.0.22"
requires "https://github.com/Quamolit/json-paint.nim#v0.0.23"
```

```nim
Expand Down Expand Up @@ -271,6 +271,10 @@ Example logs:
event: {"type":"key-down","sym":100,"repeat":false,"scancode":"SDL_SCANCODE_D","name":"d"}
event: {"type":"text-input","text":"d"}
event: {"type":"key-up","sym":100,"repeat":false,"scancode":"SDL_SCANCODE_D","name":"d"}
// keydown with actions
event: {"type":"key-down","sym":97,"repeat":false,"scancode":"SDL_SCANCODE_A","name":"a","path":["a",1],"action":":hit-key","data":"demo data"}
event: {"type":"text-input","text":"a"}
event: {"type":"key-up","sym":97,"repeat":false,"scancode":"SDL_SCANCODE_A","name":"a","path":["a",1],"action":":hit-key","data":"demo data"}
```

### License
Expand Down
2 changes: 1 addition & 1 deletion json_paint.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.0.22"
version = "0.0.23"
author = "jiyinyiyong"
description = "JSON DSL for canvas rendering"
license = "MIT"
Expand Down
63 changes: 47 additions & 16 deletions src/json_paint.nim
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,60 @@ proc takeCanvasEvents*(handleEvent: proc(e: JsonNode):void) =
"x": event.motion.x,
"y": event.motion.y,
})
of KeyDown:
# echo "keydown event: ", event.key[]
handleEvent(%* {
"type": "key-down",
"sym": event.key.keysym.sym,
"repeat": event.key.repeat,
"scancode": $event.key.keysym.scancode,
"name": attachKeyName(event.key.keysym.sym)
})
of TextInput:
# echo "input: ", event.text.text[0]
handleEvent(%* {
"type": "text-input",
"text": $event.text.text[0]
})
of KeyDown:
# echo "keydown event: ", event.key[]
let name = attachKeyName(event.key.keysym.sym)
let targets = findKeyListener(name)
# echo "targets", targets, " ", name
if targets.len > 0:
for item in targets:
handleEvent(%* {
"type": "key-down",
"sym": event.key.keysym.sym,
"repeat": event.key.repeat,
"scancode": $event.key.keysym.scancode,
"name": attachKeyName(event.key.keysym.sym),
"path": item.path,
"action": item.action,
"data": item.data,
})
else:
handleEvent(%* {
"type": "key-down",
"sym": event.key.keysym.sym,
"repeat": event.key.repeat,
"scancode": $event.key.keysym.scancode,
"name": attachKeyName(event.key.keysym.sym)
})
of KeyUp:
handleEvent(%* {
"type": "key-up",
"sym": event.key.keysym.sym,
"repeat": event.key.repeat,
"scancode": $event.key.keysym.scancode,
"name": attachKeyName(event.key.keysym.sym)
})
let name = attachKeyName(event.key.keysym.sym)
let targets = findKeyListener(name)
if targets.len > 0:
for item in targets:
handleEvent(%* {
"type": "key-up",
"sym": event.key.keysym.sym,
"repeat": event.key.repeat,
"scancode": $event.key.keysym.scancode,
"name": name,
"path": item.path,
"action": item.action,
"data": item.data,
})
else:
handleEvent(%* {
"type": "key-up",
"sym": event.key.keysym.sym,
"repeat": event.key.repeat,
"scancode": $event.key.keysym.scancode,
"name": name
})
of QuitEvent:
handleEvent(%* {
"type": "quit"
Expand Down
21 changes: 17 additions & 4 deletions src/json_paint/key_listener.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,24 @@ var keyListenersStack: seq[KeyListener]
proc resetKeyListenerStack*() =
keyListenersStack = @[]

proc addKeyListener*() =
discard
proc addKeyListener*(tree: JsonNode) =
if tree.kind != JObject: showError("Expects object for touch area")
if tree.contains("key").not: showError("Expects key field")
if tree.contains("path").not: showError("Expects path field")
if tree.contains("action").not: showError("Expects action field")

proc findKeyListener*() =
discard
keyListenersStack.add(KeyListener(
key: tree["key"].getStr,
path: tree["path"],
action: tree["action"],
data: if tree.contains("data"): tree["data"] else: newJNull()
))

proc findKeyListener*(k: string): seq[KeyListener] =
for item in keyListenersStack:
# echo "looking for k: ", k, " ", item
if item.key == k:
result.add(item)

# https://wiki.libsdl.org/SDLKeycodeLookup
proc attachKeyName*(k: int): string =
Expand Down
3 changes: 1 addition & 2 deletions src/json_paint/shape_renderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ proc renderTouchArea(ctx: ptr Context, tree: JsonNode, base: TreeContext) =
ctx.fill()

proc renderKeyListener*(ctx: ptr Context, tree: JsonNode) =
echo "key listener", tree
addKeyListener()
addKeyListener(tree)

proc processJsonTree*(ctx: ptr Context, tree: JsonNode, base: TreeContext) =
if verboseMode:
Expand Down

0 comments on commit a0907ec

Please sign in to comment.