瀏覽代碼

add CheckBox :eyes:

Ethosa 3 年之前
父節點
當前提交
4509f06c58

+ 42 - 39
README.md

@@ -7,7 +7,7 @@
 [![time tracker](https://wakatime.com/badge/github/Ethosa/nodesnim.svg)](https://wakatime.com/badge/github/Ethosa/nodesnim)
 [![test](https://github.com/Ethosa/nodesnim/workflows/test/badge.svg)](https://github.com/Ethosa/nodesnim/actions)
 
-<h4>Stable version - 0.2.1</h4>
+<h4>Stable version - 0.2.2</h4>
 </div>
 
 ## Install
@@ -59,31 +59,30 @@
 
 ## Now available
 This section contains links to documentation for all nodes.
-|Core                |Default nodes             |Control nodes                  |2D Nodes                    |3D Nodes                     |
-|:--:                |:--:                      |:--:                           |  :--:                      |:--:                         |
-|[Anchor][Anchor]    |[Node][Node]              |[Control][Control]             |[Node2D][Node2D]            |[Node3D][Node3D]             |
-|[Color][Color]      |[Canvas][Canvas]          |[ColorRect][ColorRect]         |[Sprite][Sprite]            |[GeometryInstance][ginstance]|
-|[ColorText][clrtext]|[Scene][Scene]            |[TextureRect][TextureRect]     |[AnimatedSprite][asprite]   |                             |
-|[Enums][Enums]      |[AudioStreamPlayer][aplay]|[Label][Label]                 |[YSort][YSort]              |                             |
-|[Exceptions][except]|[AnimationPlayer][aplayer]|[Button][Button]               |[CollisionShape2D][cshape2d]|                             |
-|[Image][Image]      |                          |[EditText][EditText]           |[Camera2D][Camera2D]        |                             |
-|[Input][Input]      |                          |[RichLabel][RichLabel]         |                            |                             |
-|[Rect2][Rect2]      |                          |[RichEditText][RichEditText]   |                            |                             |
-|[Vector2][Vector2]  |                          |[Box][Box]                     |                            |                             |
-|[Circle2][Circle2]  |                          |[HBox][HBox]                   |                            |                             |
-|[Polygon2][Polygon2]|                          |[VBox][VBox]                   |                            |                             |
-|[AudioStream][astrm]|                          |[GridBox][GridBox]             |                            |                             |
-|[Animation][anim]   |                          |[Scroll][Scroll]               |                            |                             |
-|[Vector3][Vector3]  |                          |[ProgressBar][ProgressBar]     |                            |                             |
-|[SceneBuilder][SBld]|                          |[Slider][Slider]               |                            |                             |
-|                    |                          |[VSlider][VSlider]             |                            |                             |
-|                    |                          |[Popup][Popup]                 |                            |                             |
-|                    |                          |[TextureButton][TextureButton] |                            |                             |
-|                    |                          |[TextureProgressBar][tprogress]|                            |                             |
-|                    |                          |[Counter][Counter]             |                            |                             |
-|                    |                          |[Switch][Switch]               |                            |                             |
-|                    |                          |[SubWindow][swindow]           |                            |                             |
-|                    |                          |[LineEdit][lineedit]           |                            |                             |
+|Core            |Default nodes        |Control nodes         |2D Nodes            |3D Nodes            |Graphics    |
+|:--:            |:--:                 |:--:                  |  :--:              |:--:                |:--:        |
+|[Anchor][]      |[Node][]             |[Control][]           |[Node2D][]          |[Node3D][]          |[Drawable][]|
+|[Color][]       |[Canvas][]           |[ColorRect][]         |[Sprite][]          |[GeometryInstance][]|            |
+|[Font][]        |[Scene][]            |[TextureRect][]       |[AnimatedSprite][]  |                    |            |
+|[Enums][]       |[AudioStreamPlayer][]|[Label][]             |[YSort][]           |                    |            |
+|[Exceptions][]  |[AnimationPlayer][]  |[Button][]            |[CollisionShape2D][]|                    |            |
+|[Image][]       |                     |[EditText][]          |[Camera2D][]        |                    |            |
+|[Input][]       |                     |[Box][]               |                    |                    |            |
+|[Rect2][]       |                     |[HBox][]              |                    |                    |            |
+|[Vector2][]     |                     |[VBox][]              |                    |                    |            |
+|[Circle2][]     |                     |[GridBox][]           |                    |                    |            |
+|[Polygon2][]    |                     |[Scroll][]            |                    |                    |            |
+|[AudioStream][] |                     |[ProgressBar][]       |                    |                    |            |
+|[Animation][]   |                     |[Slider][]            |                    |                    |            |
+|[Vector3][]     |                     |[VSlider][]           |                    |                    |            |
+|[SceneBuilder][]|                     |[Popup][]             |                    |                    |            |
+|[StyleSheet][]  |                     |[TextureButton][]     |                    |                    |            |
+|                |                     |[TextureProgressBar][]|                    |                    |            |
+|                |                     |[Counter][]           |                    |                    |            |
+|                |                     |[Switch][]            |                    |                    |            |
+|                |                     |[SubWindow][]         |                    |                    |            |
+|                |                     |[LineEdit][]          |                    |                    |            |
+|                |                     |[CheckBox][]          |                    |                    |            |
 
 
 
@@ -130,25 +129,26 @@ Use the [`Nim compiler user guide`](https://nim-lang.org/docs/nimc.html#dynlibov
 
 [Anchor]:https://ethosa.github.io/nodesnim/nodesnim/core/anchor.html
 [Color]:https://ethosa.github.io/nodesnim/nodesnim/core/color.html
-[clrtext]:https://ethosa.github.io/nodesnim/nodesnim/core/color_text.html
 [Enums]:https://ethosa.github.io/nodesnim/nodesnim/core/enums.html
-[except]:https://ethosa.github.io/nodesnim/nodesnim/core/exceptions.html
+[Exceptions]:https://ethosa.github.io/nodesnim/nodesnim/core/exceptions.html
 [Image]:https://ethosa.github.io/nodesnim/nodesnim/core/image.html
 [Input]:https://ethosa.github.io/nodesnim/nodesnim/core/input.html
 [Rect2]:https://ethosa.github.io/nodesnim/nodesnim/core/rect2.html
 [Vector2]:https://ethosa.github.io/nodesnim/nodesnim/core/vector2.html
 [Circle2]:https://ethosa.github.io/nodesnim/nodesnim/core/circle2.html
 [Polygon2]:https://ethosa.github.io/nodesnim/nodesnim/core/polygon2.html
-[astrm]:https://ethosa.github.io/nodesnim/nodesnim/core/audio_stream.html
-[anim]:https://ethosa.github.io/nodesnim/nodesnim/core/animation.html
+[AudioStream]:https://ethosa.github.io/nodesnim/nodesnim/core/audio_stream.html
+[Animation]:https://ethosa.github.io/nodesnim/nodesnim/core/animation.html
 [Vector3]:https://ethosa.github.io/nodesnim/nodesnim/core/vector3.html
-[SBld]:https://ethosa.github.io/nodesnim/nodesnim/core/scene_builder.html
+[SceneBuilder]:https://ethosa.github.io/nodesnim/nodesnim/core/scene_builder.html
+[Font]:https://ethosa.github.io/nodesnim/nodesnim/core/font.html
+[StyleSheet]:https://ethosa.github.io/nodesnim/nodesnim/core/stylesheet.html
 
 [Node]:https://ethosa.github.io/nodesnim/nodesnim/nodes/node.html
 [Canvas]:https://ethosa.github.io/nodesnim/nodesnim/nodes/canvas.html
 [Scene]:https://ethosa.github.io/nodesnim/nodesnim/nodes/scene.html
-[aplay]:https://ethosa.github.io/nodesnim/nodesnim/nodes/audio_stream_player.html
-[aplayer]:https://ethosa.github.io/nodesnim/nodesnim/nodes/animation_player.html
+[AudioStreamPlayer]:https://ethosa.github.io/nodesnim/nodesnim/nodes/audio_stream_player.html
+[AnimationPlayer]:https://ethosa.github.io/nodesnim/nodesnim/nodes/animation_player.html
 
 [Control]:https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/control.html
 [ColorRect]:https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/color_rect.html
@@ -168,23 +168,26 @@ Use the [`Nim compiler user guide`](https://nim-lang.org/docs/nimc.html#dynlibov
 [VSlider]:https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/vslider.html
 [Popup]:https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/popup.html
 [TextureButton]:https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/texture_button.html
-[tprogress]:https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/texture_progress_bar.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
-[swindow]:https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/subwindow.html
-[lineedit]:https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/lindeedit.html
+[SubWindow]:https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/subwindow.html
+[LineEdit]:https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/lindeedit.html
+[CheckBox]:https://ethosa.github.io/nodesnim/nodesnim/nodescontrol/checkbox.html
 
 [Node2D]:https://ethosa.github.io/nodesnim/nodesnim/nodes2d/node2d.html
 [Sprite]:https://ethosa.github.io/nodesnim/nodesnim/nodes2d/sprite.html
-[asprite]:https://ethosa.github.io/nodesnim/nodesnim/nodes2d/animated_sprite.html
+[AnimatedSprite]:https://ethosa.github.io/nodesnim/nodesnim/nodes2d/animated_sprite.html
 [YSort]:https://ethosa.github.io/nodesnim/nodesnim/nodes2d/ysort.html
-[cshape2d]:https://ethosa.github.io/nodesnim/nodesnim/nodes2d/collision_shape2d.html
+[CollisionShape2D]:https://ethosa.github.io/nodesnim/nodesnim/nodes2d/collision_shape2d.html
 [KinematicBody2D]:https://ethosa.github.io/nodesnim/nodesnim/nodes2d/kinematic_body2d.html
 [Camera2D]:https://ethosa.github.io/nodesnim/nodesnim/nodes2d/camera2d.html
 [Node2D]:https://ethosa.github.io/nodesnim/nodesnim/nodes2d/node2d.html
 
 [Node3D]:https://ethosa.github.io/nodesnim/nodesnim/nodes3d/node3d.html
-[ginstance]:https://ethosa.github.io/nodesnim/nodesnim/nodes3d/geometry_instance.html
+[GeometryInstance]:https://ethosa.github.io/nodesnim/nodesnim/nodes3d/geometry_instance.html
+
+[Drawable]:https://ethosa.github.io/nodesnim/nodesnim/graphics/drawable.html
 
 [Examples]:https://github.com/Ethosa/nodesnim/blob/master/examples
 [Wiki]:https://github.com/Ethosa/nodesnim/wiki

+ 1 - 1
nodesnim.nimble

@@ -1,7 +1,7 @@
 [Package]
 name = "nodesnim"
 author = "Ethosa"
-version = "0.2.1"
+version = "0.2.2"
 description = "The Nim GUI/2D framework based on OpenGL and SDL2."
 license = "MIT"
 srcDir = "src"

+ 1 - 0
src/nodesnim/core/enums.nim

@@ -29,6 +29,7 @@ type
     # Control nodes
     BOX_NODE,
     BUTTON_NODE,
+    CHECKBOX_NODE,
     COLOR_RECT_NODE,
     CONTROL_NODE,
     COUNTER_NODE,

+ 23 - 1
src/nodesnim/nodes/node.nim

@@ -311,7 +311,7 @@ macro `@`*(node: NodeRef, event_name, code: untyped): untyped =
         proc(`self`: NodeRef, `arg`: InputEvent) =
           `code`
 
-  of "on_toggle":
+  of "on_switch":
     var
       name = event_name[0]
       self = event_name[1]
@@ -322,6 +322,17 @@ macro `@`*(node: NodeRef, event_name, code: untyped): untyped =
         proc(`self`: SwitchRef, `arg`: bool) =
           `code`
 
+  of "on_toggle":
+    var
+      name = event_name[0]
+      self = event_name[1]
+      arg = event_name[2]
+
+    result = quote do:
+      `node`.`name` =
+        proc(`self`: CheckBoxRef, `arg`: bool) =
+          `code`
+
   of "on_changed":
     var
       name = event_name[0]
@@ -332,5 +343,16 @@ macro `@`*(node: NodeRef, event_name, code: untyped): untyped =
       `node`.`name` =
         proc(`self`: SliderRef, `arg`: uint) =
           `code`
+
+  of "on_edit":
+    var
+      name = event_name[0]
+      self = event_name[1]
+      arg = event_name[2]
+
+    result = quote do:
+      `node`.`name` =
+        proc(`self`: EditTextRef, `arg`: string) =
+          `code`
   else:
     discard

+ 3 - 2
src/nodesnim/nodescontrol.nim

@@ -19,10 +19,11 @@ import
   nodescontrol/switch,
   nodescontrol/subwindow,
   nodescontrol/lineedit,
-  nodescontrol/vslider
+  nodescontrol/vslider,
+  nodescontrol/checkbox
 
 export
   control, color_rect, texture_rect, label, button,
   box, hbox, vbox, grid_box, edittext, scroll, progress_bar,
   slider, popup, texture_button, texture_progress_bar,
-  counter, switch, subwindow, lineedit, vslider
+  counter, switch, subwindow, lineedit, vslider, checkbox

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

@@ -0,0 +1,101 @@
+# author: Ethosa
+import
+  ../thirdparty/opengl,
+
+  ../core/enums,
+  ../core/color,
+  ../core/input,
+  ../core/vector2,
+  ../core/font,
+
+  ../graphics/drawable,
+
+  ../nodes/node,
+  ../nodes/canvas,
+
+  label,
+  control
+
+
+type
+  CheckBoxRef* = ref object of ControlRef
+    enabled*: bool
+
+    box: DrawableRef
+    text: LabelRef
+
+    on_toggle*: proc(self: CheckBoxRef, toggled: bool): void  ## This called when switch toggled.
+
+proc CheckBox*(name: string = "CheckBox"): CheckBoxRef =
+  ## Creates a new CheckBox.
+  ##
+  ## Arguments:
+  ## - `name` is a node name.
+  runnableExamples:
+    var text = CheckBox("CheckBox")
+  nodepattern(CheckBoxRef)
+  controlpattern()
+  result.enabled = false
+  result.box = Drawable()
+  result.text = Label()
+  result.kind = CHECKBOX_NODE
+
+  result.box.setCornerRadius(8)
+  result.box.setCornerDetail(8)
+  result.box.setColor(Color("#444444"))
+  result.box.setBorderColor(Color("#555555"))
+  result.box.setBorderWidth(1)
+  result.on_toggle = proc(self: CheckBoxRef, toggled: bool) = discard
+
+
+method disable*(self: CheckBoxRef) {.base.} =
+  self.enabled = false
+  self.on_toggle(self, self.enabled)
+
+method draw*(self: CheckBoxRef, w, h: GLfloat) =
+  ## This uses in the `window.nim`.
+  procCall self.ControlRef.draw(w, h)
+  let
+    x = -w/2 + self.global_position.x
+    y = h/2 - self.global_position.y
+
+  if not self.text.text.rendered:
+    self.text.text.render(self.text.rect_size, self.text.text_align)
+  self.text.text.renderTo(Vector2(x+36, y-4), self.text.rect_size, self.text.text_align)
+  self.rect_min_size = self.text.text.getTextSize()
+  self.rect_min_size.x += 36
+  self.resize(self.rect_size.x, self.rect_size.y)
+
+  self.box.draw(x+4, y-4, 24, 24)
+  if self.enabled:
+    glColor4f(1f, 1f, 1f, 1f)
+    glBegin(GL_LINES)
+    glVertex2f(x+10, y-10)
+    glVertex2f(x+22, y-22)
+
+    glVertex2f(x+22, y-10)
+    glVertex2f(x+10, y-22)
+    glEnd()
+
+method duplicate*(self: CheckBoxRef): CheckBoxRef {.base.} =
+  ## Duplicates ChechBox object and create a new ChechBox.
+  self.deepCopy()
+
+method enable*(self: CheckBoxRef) {.base.} =
+  self.enabled = true
+  self.on_toggle(self, self.enabled)
+
+method setText*(self: CheckBoxRef, value: string, save_properties: bool = false) {.base.} =
+  self.text.setText(value, save_properties)
+
+method toggle*(self: CheckBoxRef) {.base.} =
+  self.enabled = not self.enabled
+  self.on_toggle(self, self.enabled)
+
+method handle*(self: CheckBoxRef, event: InputEvent, mouse_on: var NodeRef) =
+  ## Handles user input. This uses in the `window.nim`.
+  procCall self.ControlRef.handle(event, mouse_on)
+
+  if self.hovered and self.focused:
+    if event.kind == MOUSE and not mouse_pressed and event.button_index == 0:
+      self.toggle()

+ 3 - 3
src/nodesnim/nodescontrol/switch.nim

@@ -20,7 +20,7 @@ type
     color_enable*, color_disable*: ColorRef
     back_enable*, back_disable*: ColorRef
 
-    on_toggle*: proc(self: SwitchRef, toggled: bool): void  ## This called when switch toggled.
+    on_switch*: proc(self: SwitchRef, toggled: bool): void  ## This called when switch toggled.
   SwitchRef* = ref SwitchObj
 
 
@@ -40,7 +40,7 @@ proc Switch*(name: string = "Switch"): SwitchRef =
   result.value = false
   result.rect_size.x = 50
   result.rect_size.y = 20
-  result.on_toggle = proc(self: SwitchRef, toggled: bool) = discard
+  result.on_switch = proc(self: SwitchRef, toggled: bool) = discard
   result.kind = COLOR_RECT_NODE
 
 
@@ -77,7 +77,7 @@ method handle*(self: SwitchRef, event: InputEvent, mouse_on: var NodeRef) =
 
   if self.hovered and event.kind == MOUSE and event.pressed:
     self.value = not self.value
-    self.on_toggle(self, self.value)
+    self.on_switch(self, self.value)
 
 
 method toggle*(self: SwitchRef) {.base.} =

+ 1 - 0
tests/README.md

@@ -43,3 +43,4 @@
 - [Use AnimationPlayer node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test41.nim)
 - [Use StyleSheet object.](https://github.com/Ethosa/nodesnim/blob/master/tests/test42.nim)
 - [Use Drawable and Control.](https://github.com/Ethosa/nodesnim/blob/master/tests/test43.nim)
+- [Use CheckBox node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test44.nim)

+ 1 - 1
tests/test32.nim

@@ -12,7 +12,7 @@ var
 main.addChild(switch)
 switch.move(128, 64)
 
-switch.on_toggle =
+switch.on_switch =
   proc(self: SwitchRef, toggled: bool) =  # this called when the user toggles switch.
     echo toggled
 

+ 23 - 0
tests/test44.nim

@@ -0,0 +1,23 @@
+# --- Test 44. Use CheckBox node. --- #
+import nodesnim
+
+
+Window("drawable oops")
+
+build:
+  - Scene scene:
+    - CheckBox box:
+      call setText("smth checkbox")
+      call enable()
+    - ColorRect rect:
+      call move(100, 100)
+
+box@on_toggle(self, value):
+  if value:
+    rect.color.a = 1f
+  else:
+    rect.color.a = 0f
+
+
+addMainScene(scene)
+windowLaunch()