Browse Source

add Drawable :eyes:

Ethosa 3 years ago
parent
commit
f3cf5a21c2
3 changed files with 61 additions and 3 deletions
  1. 32 3
      src/nodesnim/nodescontrol/control.nim
  2. 1 0
      tests/README.md
  3. 28 0
      tests/test45.nim

+ 32 - 3
src/nodesnim/nodescontrol/control.nim

@@ -9,11 +9,13 @@ import
   ../core/input,
   ../core/enums,
   ../core/color,
+  ../core/stylesheet,
 
   ../graphics/drawable,
 
   ../nodes/node,
-  ../nodes/canvas
+  ../nodes/canvas,
+  strutils
 
 
 type
@@ -88,8 +90,8 @@ method draw*(self: ControlRef, w, h: GLfloat) =
     x = -w/2 + self.global_position.x
     y = h/2 - self.global_position.y
 
-  glColor4f(self.background_color.r, self.background_color.g, self.background_color.b, self.background_color.a)
-  glRectf(x, y, x+self.rect_size.x, y-self.rect_size.y)
+  # glColor4f(self.background_color.r, self.background_color.g, self.background_color.b, self.background_color.a)
+  # glRectf(x, y, x+self.rect_size.x, y-self.rect_size.y)
 
   self.background.draw(x, y, self.rect_size.x, self.rect_size.y)
 
@@ -142,3 +144,30 @@ method handle*(self: ControlRef, event: InputEvent, mouse_on: var NodeRef) =
 method setBackgroundColor*(self: ControlRef, color: ColorRef) {.base.} =
   ## Changes Control background color.
   self.background_color = color
+
+method setStyle*(self: ControlRef, style: StyleSheetRef) {.base.} =
+  self.background.setStyle(style)
+  for i in style.dict:
+    case i.key
+    # size-anchor: 1.0
+    # size-anchor: 0.5 1
+    of "size-anchor":
+      let tmp = i.value.split(Whitespace)
+      if tmp.len() == 1:
+        self.setSizeAnchor(Vector2(parseFloat(tmp[0])))
+      elif tmp.len() == 2:
+        self.setSizeAnchor(Vector2(parseFloat(tmp[0]), parseFloat(tmp[1])))
+    # position-anchor: 1
+    # position-anchor: 0.5 1 0.5 1
+    of "position-anchor":
+      let tmp = i.value.split(Whitespace)
+      if tmp.len() == 1:
+        let tmp2 = parseFloat(tmp[0])
+        self.setAnchor(Anchor(tmp2, tmp2, tmp2, tmp2))
+      elif tmp.len() == 4:
+        self.setAnchor(Anchor(
+          parseFloat(tmp[0]), parseFloat(tmp[1]),
+          parseFloat(tmp[2]), parseFloat(tmp[3]))
+        )
+    else:
+      discard

+ 1 - 0
tests/README.md

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

+ 28 - 0
tests/test45.nim

@@ -0,0 +1,28 @@
+# --- Test 45. Use Drawable and Control. --- #
+import nodesnim
+
+
+Window("drawable oops")
+
+build:
+  - Scene scene:
+    - Control ctrl
+
+
+scene.addChild(ctrl)
+ctrl.resize(256, 96)
+ctrl.move(64, 64)
+ctrl.setStyle(style(
+  {
+    background-color: rgb(33, 65, 87),
+    border-radius: 8,
+    border-width: 1,
+    border-color: rgb(0, 0, 0),
+    shadow: true,
+    shadow-offset: 3,
+    size-anchor: "0.5 0.7"
+  }
+))
+
+addMainScene(scene)
+windowLaunch()