SakiKawasaki 5 years ago
parent
commit
eb82f29046

+ 2 - 3
src/nodesnim/core/input.nim

@@ -166,10 +166,9 @@ proc isActionPressed*(a: type Input, name: string): bool =
   result = false
   for action in actionlist:
     if action.name == name:
-      if action.kind == MOUSE and (last_event.kind == MOUSE or last_event.kind == MOTION):
+      if action.kind == MOUSE:
         if action.button_index == last_event.button_index and mouse_pressed:
-          if press_state > 0:
-            result = true
+          result = true
       elif action.kind == TOUCH and last_event.kind == TOUCH:
         if press_state > 0:
           result = true

+ 4 - 1
src/nodesnim/nodes/canvas.nim

@@ -10,6 +10,9 @@ import
   node
 
 
+const PI2 = PI + PI
+
+
 type
   DrawCommandType* {.size: sizeof(int8).} = enum
     POINT, LINE, RECT, FILL, CIRCLE
@@ -102,7 +105,7 @@ method circle*(canvas: CanvasPtr, x, y, radius: GLfloat, color: ColorRef, qualit
   ## - `quality` - circle quality.
   var pnts: seq[GLfloat]
   for i in 0..quality:
-    let angle = 2*PI*i.float/quality.float
+    let angle = PI2*i.float/quality.float
     pnts.add(radius*cos(angle))
     pnts.add(radius*sin(angle))
   canvas.commands.add(DrawCommand(kind: CIRCLE, x1: x, y1: y, color: color, points: pnts))

+ 2 - 5
src/nodesnim/nodes/node.nim

@@ -42,7 +42,7 @@ template nodepattern*(nodetype: untyped): untyped =
     enter: proc() = discard,
     exit: proc() = discard,
     is_ready: false, pausemode: INHERIT, visible: true,
-    anchor: Anchor(0, 0, 0, 0)
+    anchor: nil
   )
   result = variable.addr
 
@@ -201,10 +201,7 @@ method move*(self: NodePtr, vec2: Vector2Ref) {.base, inline.} =
   ## Arguments:
   ## - `vec2`: how much to add to the position on the X,Y axes.
   self.position += vec2
-  self.anchor.x1 = 0
-  self.anchor.x2 = 0
-  self.anchor.y1 = 0
-  self.anchor.y2 = 0
+  self.anchor = nil
 
 method removeChild*(self: NodePtr, index: int) {.base.} =
   ## Removes node child at a specific position.

+ 1 - 1
src/nodesnim/nodes/scene.nim

@@ -56,5 +56,5 @@ method reAnchorScene*(scene: ScenePtr, paused: bool) {.base.} =
   for child in scene.getChildIter():
     if paused and child.getPauseMode() != PROCESS:
       continue
-    if child.visible:
+    if child.visible and child.anchor != nil:
       child.calcPositionAnchor()

+ 15 - 9
src/nodesnim/nodescontrol/control.nim

@@ -60,9 +60,14 @@ method draw*(self: ControlPtr, w, h: GLfloat) =
   if self.pressed:
     self.press(last_event.x, last_event.y)
 
+method getGlobalMousePosition*(self: ControlPtr): Vector2Ref {.base.} =
+  Vector2Ref(x: last_event.x, y: last_event.y)
+
 method handle*(self: ControlPtr, event: InputEvent, mouse_on: var NodePtr) =
   {.warning[LockLevel]: off.}
-  let hasmouse = Rect2(self.global_position, self.rect_size).hasPoint(event.x, event.y)
+  let
+    hasmouse = Rect2(self.global_position, self.rect_size).hasPoint(event.x, event.y)
+    click = mouse_pressed and event.kind == MOUSE
   if mouse_on == nil and hasmouse:
     mouse_on = self
     # Hover
@@ -70,20 +75,21 @@ method handle*(self: ControlPtr, event: InputEvent, mouse_on: var NodePtr) =
       self.mouse_enter(event.x, event.y)
       self.hovered = true
     # Focus
-    if not self.focused and mouse_pressed:
+    if not self.focused and click:
       self.focused = true
       self.focus()
     # Click
     if mouse_pressed and not self.pressed:
       self.pressed = true
       self.click(event.x, event.y)
-  if not hasmouse and self.hovered:
-    self.mouse_exit(event.x, event.y)
-    self.hovered = false
-  # Unfocus
-  if self.focused and mouse_pressed and not hasmouse:
-    self.unfocus()
-    self.focused = false
+  elif not hasmouse:
+    if not mouse_pressed and self.hovered:
+      self.mouse_exit(event.x, event.y)
+      self.hovered = false
+    # Unfocus
+    if self.focused and click:
+      self.unfocus()
+      self.focused = false
   if not mouse_pressed and self.pressed:
     self.pressed = false
     self.release(event.x, event.y)

+ 1 - 0
tests/test5.nim

@@ -17,6 +17,7 @@ var
 main.addChild(colorrect)
 colorrect.addChild(colorrect1)
 
+
 colorrect1.click =
   proc(x, y: float) =  # This called when the user clicks on the Control node (ColorRect in this case).
     colorrect1.move(3, 3)