Browse Source

add Switch node.

SakiKawasaki 5 years ago
parent
commit
56321c832f
5 changed files with 129 additions and 1 deletions
  1. 1 0
      README.md
  2. 2 1
      src/nodesnim.nim
  3. 101 0
      src/nodesnim/nodescontrol/switch.nim
  4. 1 0
      tests/README.md
  5. 24 0
      tests/test34.nim

+ 1 - 0
README.md

@@ -67,6 +67,7 @@
    -  [TextureButton](https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/texture_button.html)
    -  [TextureProgressBar](https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/texture_progress_bar.html)
    -  [Counter](https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/counter.html)
+   -  [Switch](https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/switch.html)
 -  2D nodes
    -  [Node2D](https://ethosa.github.io/nodesnim/nodesnim/nodes2d/node2d.html)
    -  [Sprite](https://ethosa.github.io/nodesnim/nodesnim/nodes2d/sprite.html)

+ 2 - 1
src/nodesnim.nim

@@ -58,6 +58,7 @@ import
   nodesnim/nodescontrol/texture_button,
   nodesnim/nodescontrol/texture_progress_bar,
   nodesnim/nodescontrol/counter,
+  nodesnim/nodescontrol/switch,
 
   nodesnim/nodes2d/node2d,
   nodesnim/nodes2d/sprite,
@@ -79,6 +80,6 @@ export
   # Control nodes
   control, color_rect, texture_rect, label, button, box, hbox, vbox, grid_box, edittext,
   rich_label, rich_edit_text, scroll, progress_bar, vprogress_bar, slider, vslider, popup,
-  texture_button, texture_progress_bar, counter,
+  texture_button, texture_progress_bar, counter, switch,
   # 2D nodes
   node2d, sprite, animated_sprite, ysort, collision_shape2d, kinematic_body2d

+ 101 - 0
src/nodesnim/nodescontrol/switch.nim

@@ -0,0 +1,101 @@
+# author: Ethosa
+## Displays colour rectangle.
+import
+  ../thirdparty/opengl,
+
+  ../core/vector2,
+  ../core/rect2,
+  ../core/anchor,
+  ../core/input,
+  ../core/color,
+  ../core/enums,
+
+  ../nodes/node,
+  control
+
+
+type
+  SwitchObj* = object of ControlPtr
+    value*: bool
+    color_enable*, color_disable*: ColorRef
+    back_enable*, back_disable*: ColorRef
+
+    on_toggle*: proc(toggled: bool): void  ## This called when switch toggled.
+  SwitchPtr* = ptr SwitchObj
+
+
+proc Switch*(name: string, variable: var SwitchObj): SwitchPtr =
+  ## Creates a new Switch pointer.
+  ##
+  ## Arguments:
+  ## - `name` is a node name.
+  ## - `variable` is a SwitchObj variable
+  runnableExamples:
+    var
+      colorrect1_obj: SwitchObj
+      colorrect1 = Switch("Switch", colorrect1_obj)
+  nodepattern(SwitchObj)
+  controlpattern()
+  variable.color_disable = Color(0.4, 0.4, 0.4)
+  variable.color_enable = Color(0.4, 0.8, 0.4)
+  variable.back_disable = Color(0.16, 0.16, 0.16)
+  variable.back_enable = Color(0.16, 0.36, 0.16)
+  variable.value = false
+  variable.rect_size.x = 50
+  variable.rect_size.y = 20
+  variable.on_toggle = proc(toggled: bool) = discard
+  variable.kind = COLOR_RECT_NODE
+
+proc Switch*(obj: var SwitchObj): SwitchPtr {.inline.} =
+  ## Creates a new Switch pointer with default node name "Switch".
+  ##
+  ## Arguments:
+  ## - `variable` is a SwitchObj variable
+  runnableExamples:
+    var
+      colorrect1_obj: SwitchObj
+      colorrect1 = Switch(colorrect1_obj)
+  Switch("Switch", obj)
+
+
+method draw*(self: SwitchPtr, w, h: GLfloat) =
+  ## this method uses in the `window.nim`.
+  self.calcGlobalPosition()
+  let
+    x = -w/2 + self.global_position.x
+    y = h/2 - self.global_position.y
+    color = if self.value: self.color_enable else: self.color_disable
+    back = if self.value: self.back_enable else: self.back_disable
+
+  glColor4f(back.r, back.g, back.b, back.a)
+  glRectf(x, y, x+self.rect_size.x, y-self.rect_size.y)
+
+  glColor4f(color.r, color.g, color.b, color.a)
+  if self.value:
+    glRectf(x + self.rect_size.x - 10, y, x + self.rect_size.x, y-self.rect_size.y)
+  else:
+    glRectf(x, y, x + 10, y-self.rect_size.y)
+
+  # Press
+  if self.pressed:
+    self.press(last_event.x, last_event.y)
+
+
+method duplicate*(self: SwitchPtr, obj: var SwitchObj): SwitchPtr {.base.} =
+  ## Duplicates Switch object and create a new Switch pointer.
+  obj = self[]
+  obj.addr
+
+
+method handle*(self: SwitchPtr, event: InputEvent, mouse_on: var NodePtr) =
+  ## Handles user input. This uses in the `window.nim`.
+  procCall self.ControlPtr.handle(event, mouse_on)
+
+  if self.hovered and event.kind == MOUSE and event.pressed:
+    self.value = not self.value
+    self.on_toggle(self.value)
+
+
+method toggle*(self: SwitchPtr) {.base.} =
+  ## Toggles value.
+  self.value = not self.value

+ 1 - 0
tests/README.md

@@ -33,3 +33,4 @@
 31. [Use Counter node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test31.nim)
 32. [Use CollisionShape2D node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test32.nim)
 33. [Use KinematicBody2D node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test33.nim)
+34. [Use Switch node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test34.nim)

+ 24 - 0
tests/test34.nim

@@ -0,0 +1,24 @@
+# --- Test 34. Use Switch node. --- #
+import nodesnim
+
+
+Window("hello world")
+
+var
+  mainobj: SceneObj
+  main = Scene("Main", mainobj)
+
+  switchobj: SwitchObj
+  switch = Switch(switchobj)
+
+main.addChild(switch)
+switch.move(128, 64)
+
+switch.on_toggle =
+  proc(toggled: bool) =  # this called when the user toggles switch.
+    echo toggled
+
+
+addScene(main)
+setMainScene("Main")
+windowLaunch()