Bläddra i källkod

add alpha channel support.

Ethosa 5 år sedan
förälder
incheckning
f881ce2fc8
4 ändrade filer med 68 tillägg och 12 borttagningar
  1. 13 12
      src/nodesnim/nodescontrol/control.nim
  2. 2 0
      src/nodesnim/window.nim
  3. 2 0
      tests/README.md
  4. 51 0
      tests/test5.nim

+ 13 - 12
src/nodesnim/nodescontrol/control.nim

@@ -17,13 +17,13 @@ type
     pressed*: bool
     focused*: bool
 
-    mouse_enter*: proc(x, y: float): void
-    mouse_exit*: proc(x, y: float): void
-    click*: proc(x, y: float): void
-    press*: proc(x, y: float): void
-    release*: proc(x, y: float): void
-    focus*: proc(): void
-    unfocus*: proc(): void
+    mouse_enter*: proc(x, y: float): void  ## This called when the mouse enters the Control node.
+    mouse_exit*: proc(x, y: float): void   ## This called when the mouse exit from the Control node.
+    click*: proc(x, y: float): void        ## This called when the user clicks on the Control node.
+    press*: proc(x, y: float): void        ## This called when the user holds on the mouse on the Control node.
+    release*: proc(x, y: float): void      ## This called when the user no more holds on the mouse.
+    focus*: proc(): void                   ## This called when the Control node gets focus.
+    unfocus*: proc(): void                 ## This called when the Control node loses focus.
   ControlPtr* = ptr ControlObj
 
 
@@ -69,20 +69,21 @@ method handle*(self: ControlPtr, event: InputEvent, mouse_on: var NodePtr) =
     if not self.hovered:
       self.mouse_enter(event.x, event.y)
       self.hovered = true
-    else:
-      self.mouse_exit(event.x, event.y)
-      self.hovered = false
     # Focus
-    if not self.focused:
+    if not self.focused and mouse_pressed:
       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:
+  if self.focused and mouse_pressed and not hasmouse:
     self.unfocus()
+    self.focused = false
   if not mouse_pressed and self.pressed:
     self.pressed = false
     self.release(event.x, event.y)

+ 2 - 0
src/nodesnim/window.nim

@@ -38,6 +38,8 @@ proc display {.cdecl.} =
   let (r, g, b, a) = env.color.toFloatTuple()
   glClearColor(r, g, b, a)
   glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
+  glEnable(GL_BLEND)
+  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
 
   # Draw current scene.
   current_scene.drawScene(width.GLfloat, height.GLfloat, paused)

+ 2 - 0
tests/README.md

@@ -3,3 +3,5 @@
 1. [Create a window and set up the main scene.](https://github.com/Ethosa/nodesnim/blob/master/tests/test1.nim)
 2. [Use Canvas node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test2.nim)
 3. [Window events handling.](https://github.com/Ethosa/nodesnim/blob/master/tests/test3.nim)
+4. [Use ColorRect node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test4.nim)
+5. [Handle Control node events.](https://github.com/Ethosa/nodesnim/blob/master/tests/test5.nim)

+ 51 - 0
tests/test5.nim

@@ -0,0 +1,51 @@
+# --- Test 5. Handle Control node events. --- #
+import nodesnim
+
+
+Window("hello world")
+
+var
+  mainobj: SceneObj
+  main = Scene("Main", mainobj)
+
+  colorrectobj: ColorRectObj
+  colorrect = ColorRect(colorrectobj)
+
+  colorrect1obj: ColorRectObj
+  colorrect1 = ColorRect(colorrect1obj)
+
+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)
+
+colorrect.press =
+  proc(x, y: float) =  # This called when the user holds on the mouse on the Control node.
+    colorrect.color.r -= 0.001
+
+colorrect.release =
+  proc(x, y: float) =  # This called when the user no more holds on the mouse.
+    colorrect.color.r = 1
+
+colorrect.focus =
+  proc() =  # This called when the Control node gets focus.
+    echo "hello ^^."
+
+colorrect.unfocus =
+  proc() =  # This called when the Control node loses focus.
+    echo "bye :("
+
+colorrect1.mouse_enter =
+  proc(x, y: float) =  # This called when the mouse enters the Control node.
+    colorrect1.color = Color(1, 0.6, 1, 0.5)
+
+colorrect1.mouse_exit =
+  proc(x, y: float) =  # This called when the mouse exit from the Control node.
+    colorrect1.color = Color(1f, 1f, 1f)
+
+
+addScene(main)
+setMainScene("Main")
+windowLaunch()