Skip to content

Commit

Permalink
Merge pull request #13 from Quamolit/mouse-events
Browse files Browse the repository at this point in the history
remove filter of touch-area events
  • Loading branch information
TCXX authored Nov 23, 2020
2 parents 452a428 + b0b90ee commit efd91e5
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 30 deletions.
6 changes: 3 additions & 3 deletions 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.12"
requires "https://github.com/Quamolit/json-paint.nim#v0.0.16"
```

```nim
Expand Down Expand Up @@ -103,7 +103,8 @@ ops: [
['curve-to', [1, 2], [3, 4], [5, 6]],
['relative-curve-to', [1, 2], [3, 4], [5, 6]],
['arc', [1, 2], 1, [0, 6.28], false],
['close-path']
['new-path'],
['close-path'],
]
```

Expand Down Expand Up @@ -135,7 +136,6 @@ data: Data # JSON
x: 1
y: 1
radius: 20
events: ["mouse-down", "mouse-up", "mouse-move"]
```

### Events
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.14"
version = "0.0.17"
author = "jiyinyiyong"
description = "JSON DSL for canvas rendering"
license = "MIT"
Expand Down
2 changes: 2 additions & 0 deletions src/json_paint.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ proc renderCanvas*(tree: JsonNode) =
let base = TreeContext(x: 0, y: 0)
resetTouchStack()
ctx.processJsonTree(tree, base)
ctx.destroy()

# cairo surface -> sdl serface -> sdl texture -> copy to render
var dataPtr = surface.getData()
mainSurface.pixels = dataPtr
let mainTexture = renderer.createTextureFromSurface(mainSurface)
renderer.copy(mainTexture, nil, nil)
renderer.present()
mainTexture.destroy()

proc takeCanvasEvents*(handleEvent: proc(e: JsonNode):void) =
var event: sdl2.Event
Expand Down
8 changes: 6 additions & 2 deletions src/json_paint/shape_renderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@ proc renderText(ctx: ptr Context, tree: JsonNode, base: TreeContext) =
var extents: TextExtents
ctx.textExtents text.cstring, addr extents
var realX = x - extents.xBearing
case text
case align
of "center":
realX = x - extents.width / 2 - extents.xBearing
of "right":
realX = x - extents.width - extents.xBearing
else:
of "left":
discard
else:
echo "WARNING: unknown align value " & align & ", expects left, center, right"
let realY = y - extents.height / 2 - extents.yBearing
ctx.moveTo realX, realY
ctx.showText text
Expand Down Expand Up @@ -218,6 +220,8 @@ proc callOps(ctx: ptr Context, tree: JsonNode, base: TreeContext) =
ctx.rectangle point.x + base.x, point.y + base.y, size.x, size.y
of "close-path":
ctx.closePath()
of "new-path":
ctx.newPath()
else:
echo "WARNING: unknown op type: ", opType

Expand Down
25 changes: 3 additions & 22 deletions src/json_paint/touches.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import json
import options
import sets
import math

import ./error_util
Expand All @@ -25,7 +24,6 @@ type TouchArea* = object
path*: JsonNode
action*: JsonNode
data*: JsonNode
events: HashSet[TouchEvent]

var touchItemsStack: seq[TouchArea]

Expand All @@ -37,25 +35,9 @@ proc addTouchArea*(x: float, y: float, radius: float, tree: JsonNode) =
if tree.contains("path").not: showError("Expects path field")
if tree.contains("action").not: showError("Expects action field")

var events: HashSet[TouchEvent]
if tree.contains("events"):
for item in tree["events"].elems:
if item.kind == JString:
case item.getStr
of "mouse-down":
events.incl(touchDown)
of "mouse-up":
events.incl(touchUp)
of "mouse-move":
events.incl(touchMotion)
else:
showError("Unexpected event: " & item.getStr)
else:
echo "Expects string for event, got: ", item.kind

touchItemsStack.add TouchArea(
x: x, y: y, radius: radius,
path: tree["path"], events: events,
path: tree["path"],
action: tree["action"],
data: if tree.contains("data"): tree["data"] else: newJNull()
)
Expand All @@ -64,9 +46,8 @@ proc findTouchArea*(x: cint, y: cint, eventKind: TouchEvent): Option[TouchArea]
let lastIdx = touchItemsStack.len - 1
for idx in 0..lastIdx:
let item = touchItemsStack[lastIdx - idx]
if item.events.contains(eventKind):
if (x.float - item.x).pow(2) + (y.float - item.y).pow(2) <= item.radius.pow(2):
return some(item)
if (x.float - item.x).pow(2) + (y.float - item.y).pow(2) <= item.radius.pow(2):
return some(item)
return none(TouchArea)

proc trackMousedownPoint*(x, y: int) =
Expand Down
3 changes: 1 addition & 2 deletions tests/demo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ proc renderSomething() =
{
"type": "text",
"text": "this is a demo",
"align": "left",
"align": "center",
"x": 40,
"y": 40,
"color": [140, 80, 76]
Expand All @@ -72,7 +72,6 @@ proc renderSomething() =
"y": 80,
"path": ["a", 1],
"action": ":demo",
"events": ["mouse-down", "mouse-move"]
},
]
})
Expand Down

0 comments on commit efd91e5

Please sign in to comment.