Ethosa 3 gadi atpakaļ
vecāks
revīzija
b5f2584455

+ 3 - 0
src/nodesnim/core/color.nim

@@ -105,6 +105,9 @@ proc Color*(): ColorRef {.inline.} =
   ## Creates a new Color object with RGBA value (0, 0, 0, 0)
   ColorRef(r: 0, g: 0, b: 0, a: 0)
 
+proc Color*(clr: ColorRef): ColorRef {.inline.} =
+  ColorRef(r: clr.r, g: clr.g, b: clr.b, a: clr.a)
+
 proc getBrightness*(self: ColorRef): float {.inline.} =
   (self.r + self.g + self.b) / 3f
 

+ 21 - 11
src/nodesnim/graphics/gradient_drawable.nim

@@ -12,16 +12,16 @@ import
 
 type
   GradientDrawableObj* = object of DrawableObj
-    corners*: tuple[p0, p1, p2, p3: ColorRef]
+    corners*: array[4, ColorRef]
   GradientDrawableRef* = ref GradientDrawableObj
 
 
 proc GradientDrawable*: GradientDrawableRef =
   drawablepattern(GradientDrawableRef)
-  result.corners = (Color(1f, 1f, 1f, 1.0),
+  result.corners = [Color(1f, 1f, 1f, 1.0),
                     Color(1f, 1f, 1f, 1.0),
                     Color(1f, 1f, 1f, 1.0),
-                    Color(1f, 1f, 1f, 1.0))
+                    Color(1f, 1f, 1f, 1.0)]
 
 
 template draw_template*(drawtype, color, function, secondfunc: untyped, is_gradient: bool = true): untyped =
@@ -74,9 +74,6 @@ method draw*(self: GradientDrawableRef, x1, y1, width, height: float) =
   if self.border_width > 0f:
     draw_template(GL_LINE_LOOP, self.border_color, glLineWidth(self.border_width), glLineWidth(1), false)
 
-method setCornerColors*(self: GradientDrawableRef, corners: tuple[p0, p1, p2, p3: ColorRef]) {.base.} =
-  self.corners = corners
-
 method setCornerColors*(self: GradientDrawableRef, c0, c1, c2, c3: ColorRef) {.base.} =
   ## Changes corners colors
   ##
@@ -85,10 +82,23 @@ method setCornerColors*(self: GradientDrawableRef, c0, c1, c2, c3: ColorRef) {.b
   ## -  `c1` is right-top color.
   ## -  `c2` is right-bottom color.
   ## -  `c3` is left-bottom color.
-  self.corners[0] = c0
-  self.corners[1] = c1
-  self.corners[2] = c2
-  self.corners[3] = c3
+  self.corners = [c0, c1, c2, c3]
+
+method setCornerColors*(self: GradientDrawableRef, corners: array[4, ColorRef]) {.base.} =
+  ## Changes corners colors
+  ##
+  ## See also:
+  ## * `setCornerColors method <#setCornerColors.e,GradientDrawableRef,ColorRef,ColorRef,ColorRef,ColorRef>`_
+  ## * `setCornerColors(GradientDrawableRef, ColorRef) method <#setCornerColors.e,GradientDrawableRef,ColorRef>`_
+  self.corners = corners
+
+method setCornerColors*(self: GradientDrawableRef, clr: ColorRef) {.base.} =
+  ## Changes corners colors
+  ##
+  ## See also:
+  ## * `setCornerColors method <#setCornerColors.e,GradientDrawableRef,ColorRef,ColorRef,ColorRef,ColorRef>`_
+  ## * `setCornerColors(GradientDrawableRef, array[4, ColorRef]) method <#setCornerColors.e,GradientDrawableRef,array[,ColorRef]>`_
+  self.corners = [Color(clr), Color(clr), Color(clr), Color(clr)]
 
 method setStyle*(self: GradientDrawableRef, s: StyleSheetRef) =
   ## Sets a new stylesheet.
@@ -100,7 +110,7 @@ method setStyle*(self: GradientDrawableRef, s: StyleSheetRef) =
     of "corner-color":
       let tmp = i.value.split(" ")
       if tmp.len() == 1:
-        self.setCornerColors(Color(tmp[0]), Color(tmp[0]), Color(tmp[0]), Color(tmp[0]))
+        self.setCornerColors(Color(tmp[0]))
       elif tmp.len() == 4:
         self.setCornerColors(Color(tmp[0]), Color(tmp[1]), Color(tmp[2]), Color(tmp[3]))
     else:

+ 30 - 3
src/nodesnim/nodes/canvas.nim

@@ -53,7 +53,7 @@ proc Canvas*(name: string = "Canvas"): CanvasRef =
   result.canvas_texture = 0
   result.surface = createRGBSurface(
       0, 40, 40, 32,
-      0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000'u32)
+      0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000u32)
   result.renderer = result.surface.createSoftwareRenderer()
   result.kind = CANVAS_NODE
   result.type_of_node = NODE_TYPE_CONTROL
@@ -120,6 +120,10 @@ method move*(self: CanvasRef, vec2: Vector2Obj) {.base, inline.} =
   ##
   ## Arguments:
   ## - `vec2`: how much to add to the position on the X,Y axes.
+  ##
+  ## See also:
+  ## - `move method <#move.e,CanvasRef,float,float>`_
+  ## - `moveTo method <#moveTo.e,CanvasRef,Vector2Obj>`_
   self.position += vec2
   self.anchor.clear()
 
@@ -129,6 +133,10 @@ method move*(self: CanvasRef, x, y: float) {.base, inline.} =
   ## Arguments:
   ## - `x`: how much to add to the position on the X axis.
   ## - `y`: how much to add to the position on the Y axis.
+  ##
+  ## See also:
+  ## - `move method <#move.e,CanvasRef,Vector2Obj>`_
+  ## - `moveTo method <#moveTo.e,CanvasRef,float,float>`_
   self.position += Vector2(x, y)
   self.anchor.clear()
 
@@ -195,15 +203,25 @@ method setAnchor*(self: CanvasRef, x1, y1, x2, y2: float) {.base.} =
 
 
 method setSizeAnchor*(self: CanvasRef, anchor: Vector2Obj) {.base.} =
+  ## Changes the size anchor to the size of the parent.
   self.size_anchor = anchor
 
 method setSizeAnchor*(self: CanvasRef, x, y: float) {.base.} =
+  ## Changes the size anchor to the size of the parent.
   self.size_anchor = Vector2(x, y)
 
 
 # --- Draw functions --- #
 proc bezier*(canvas: CanvasRef, x1, y1, x2, y2, x3, y3: GLfloat, color: ColorRef) =
   ## Draws a quadric bezier curve at 3 points.
+  ##
+  ## Arguments:
+  ## - `x1` `y1` - first point.
+  ## - `x2` `y2` - second point.
+  ## - `x3` `y3` - third point.
+  ##
+  ## See also:
+  ## - `cubicBezier proc <#cubicBezier,CanvasRef,GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,ColorRef>`_
   loadColor(color)
   for pnt in bezier_iter(0.001, Vector2(x1, y1), Vector2(x2, y2), Vector2(x3, y3)):
     canvas.renderer.drawPoint(pnt.x.cint, pnt.y.cint)
@@ -224,8 +242,17 @@ proc circle*(canvas: CanvasRef, x, y, radius: GLfloat, color: ColorRef, quality:
     canvas.renderer.drawPoint((x + radius*cos(angle)).cint, (y + radius*sin(angle)).cint)
   loadGL(canvas)
 
-proc cubic_bezier*(canvas: CanvasRef, x1, y1, x2, y2, x3, y3, x4, y4: GLfloat, color: ColorRef) =
-  ## Draws a quadric bezier curve at 3 points.
+proc cubicBezier*(canvas: CanvasRef, x1, y1, x2, y2, x3, y3, x4, y4: GLfloat, color: ColorRef) =
+  ## Draws a quadric bezier curve at 4 points.
+  ##
+  ## Arguments:
+  ## - `x1` `y1` - first point.
+  ## - `x2` `y2` - second point.
+  ## - `x3` `y3` - third point.
+  ## - `x4` `y4` - fourth point.
+  ##
+  ## See also:
+  ## - `bezier proc <#bezier,CanvasRef,GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,GLfloat,ColorRef>`_
   loadColor(color)
   for pnt in cubic_bezier_iter(0.001, Vector2(x1, y1), Vector2(x2, y2), Vector2(x3, y3), Vector2(x4, y4)):
     canvas.renderer.drawPoint(pnt.x.cint, pnt.y.cint)

+ 43 - 18
src/nodesnim/nodes/node.nim

@@ -13,20 +13,22 @@ import
 
 
 type
+  NodeHandler* = proc(self: NodeRef)
+  NodeEvHandler* = proc(self: NodeRef, event: InputEvent)
   NodeObj* = object of RootObj
     kind*: NodeKind
-    type_of_node*: NodeTypes
-    visibility*: Visibility
-    is_ready*: bool
-    pausemode*: PauseMode            ## Pause mode, by default is INHERIT.
-    name*: string                    ## Node name.
-    parent*: NodeRef                 ## Node parent.
-    children*: seq[NodeRef]          ## Node children.
-    on_enter*: proc(self: NodeRef)                   ## This called when scene changed.
-    on_exit*: proc(self: NodeRef)                    ## This called when exit from the scene.
-    on_input*: proc(self: NodeRef, event: InputEvent)  ## This called on user input.
-    on_ready*: proc(self: NodeRef)                   ## This called when the scene changed and the `enter` was called.
-    on_process*: proc(self: NodeRef)                 ## This called every frame.
+    type_of_node*: NodeTypes     ## default, gui, 2d or 3d.
+    visibility*: Visibility      ## visible, invisible or gone.
+    is_ready*: bool              ## true, when scene is ready.
+    pausemode*: PauseMode        ## Pause mode, by default is INHERIT.
+    name*: string                ## Node name.
+    parent*: NodeRef             ## Node parent.
+    children*: seq[NodeRef]      ## Node children.
+    on_enter*: NodeHandler       ## This called when scene changed.
+    on_exit*: NodeHandler        ## This called when exit from the scene.
+    on_input*: NodeEvHandler     ## This called on user input.
+    on_ready*: NodeHandler       ## This called when the scene changed and the `enter` was called.
+    on_process*: NodeHandler     ## This called every frame.
   NodeRef* = ref NodeObj
 
 
@@ -54,6 +56,10 @@ method addChild*(self: NodeRef, child: NodeRef) {.base.} =
   ##
   ## Arguments:
   ## - `child`: other node.
+  ##
+  ## See also:
+  ## - `addChildren method <#addChildren.e,NodeRef,varargs[NodeRef]>`_
+  ## - `getChild method <#getChild.e,NodeRef,int>`_
   self.children.add(child)
   child.parent = self
 
@@ -63,6 +69,9 @@ method addChildren*(self: NodeRef, childs: varargs[NodeRef]) {.base.} =
   ##
   ## Arguments:
   ## - `child`: other node.
+  ##
+  ## See also:
+  ## - `addChild method <#addChild.e,NodeRef,NodeRef>`_
   for node in childs:
     self.addChild(node)
 
@@ -80,29 +89,42 @@ method getChild*(self: NodeRef, index: int): NodeRef {.base.} =
   ##
   ## Arguments:
   ## - `index`: child index.
+  ##
+  ## See also:
+  ## - `addChild method <#addChild.e,NodeRef,NodeRef>`_
+  ## - `getChildCount method <#getChildCount.e,NodeRef>`_
   self.children[index]
 
 method getChildCount*(self: NodeRef): int {.base, inline.} =
   ## Returns child count.
+  ##
+  ## See also:
+  ## - `getChild method <#getChild.e,NodeRef,int>`_
   self.children.len()
 
 method getChildIndex*(self: NodeRef, name: string): int {.base.} =
   ## Returns `child` index or -1, if another node is not the child.
+  ##
+  ## See also:
+  ## - `getChildIndex method <#getChildIndex.e,NodeRef,NodeRef>`_
   var i = 0
   for node in self.children:
     if node.name == name:
       return i
     inc i
-  return -1
+  -1
 
 method getChildIndex*(self: NodeRef, child: NodeRef): int {.base.} =
   ## Returns `child` index or -1, if another node is not the child.
+  ##
+  ## See also:
+  ## - `getChildIndex method <#getChildIndex.e,NodeRef,string>`_
   var i = 0
   for node in self.children:
     if child == node:
       return i
     inc i
-  return -1
+  -1
 
 method getChildIter*(self: NodeRef): seq[NodeRef] {.base.} =
   ## Returns all children iter.
@@ -171,14 +193,14 @@ method handle*(self: NodeRef, event: InputEvent, mouse_on: var NodeRef) {.base.}
   ## This used in the Window object.
   discard
 
-method hasNode*(self: NodeRef, name: string): bool {.base.} =
+method hasNode*(self: NodeRef, name: string): bool {.base, inline.} =
   ## Returns true, if a node with name `name` in children.
   ##
   ## Arguments:
   ## - `name`: node name.
   self.getChildIndex(name) != -1
 
-method hasNode*(self: NodeRef, other: NodeRef): bool {.base.} =
+method hasNode*(self: NodeRef, other: NodeRef): bool {.base, inline.} =
   ## Returns true, if `other` in self children.
   ##
   ## Arguments:
@@ -187,7 +209,7 @@ method hasNode*(self: NodeRef, other: NodeRef): bool {.base.} =
 
 method hasParent*(self: NodeRef): bool {.base, inline.} =
   ## Returns true, when node has parent.
-  self.parent != nil
+  not self.parent.isNil()
 
 method hide*(self: NodeRef) {.base.} =
   self.visibility = INVISIBLE
@@ -205,6 +227,9 @@ method removeChild*(self: NodeRef, index: int) {.base.} =
   ##
   ## Arguments:
   ## - `index`: child index.
+  ##
+  ## See also:
+  ## - `addChild method <#addChild.e,NodeRef,NodeRef>`_
   self.children[index].parent = nil
   self.children.delete(index)
 
@@ -213,7 +238,7 @@ method removeChild*(self: NodeRef, other: NodeRef) {.base.} =
   ##
   ## Arguments:
   ## - `other`: other node.
-  var index: int = self.getChildIndex(other)
+  let index: int = self.getChildIndex(other)
   if index != -1:
     self.removeChild(index)
 

+ 27 - 0
src/nodesnim/nodescontrol/label.nim

@@ -60,6 +60,10 @@ method duplicate*(self: LabelRef): LabelRef {.base.} =
   self.deepCopy()
 
 method getText*(self: LabelRef): string {.base.} =
+  ## Returns `StyleText` as `string`.
+  ##
+  ## See also:
+  ## * `setText method <#setText.e,LabelRef,string,bool>`_
   $self.text
 
 method handle*(self: LabelRef, event: InputEvent, mouse_on: var NodeRef) =
@@ -93,6 +97,9 @@ method setText*(self: LabelRef, text: string, save_properties: bool = false) {.b
   ## Arguments:
   ## - `text` is a new Label text.
   ## - `save_properties` - saves old text properties, if `true`.
+  ##
+  ## See also:
+  ## * `getText method <#getText.e,LabelRef>`_
   var st = stext(text)
   if self.text.font.isNil():
     self.text.font = standard_font
@@ -109,18 +116,38 @@ method setText*(self: LabelRef, text: string, save_properties: bool = false) {.b
   self.text.rendered = false
 
 method setTextAlign*(self: LabelRef, x1, y1, x2, y2: float) {.base.} =
+  ## Changes text alignment.
+  ##
+  ## Arguments:
+  ## - `x1` `y1` - parent anchor.
+  ## - `x2` `y2` - self anchor.
+  ##
+  ## See also:
+  ## * `setTextAlign method <#setTextAlign.e,LabelRef,AnchorObj>`_
   self.text_align = Anchor(x1, y1, x2, y2)
   self.text.rendered = false
 
 method setTextAlign*(self: LabelRef, align: AnchorObj) {.base.} =
+  ## Changes text alignment.
+  ##
+  ## See also:
+  ## * `setTextAlign method <#setTextAlign.e,LabelRef,float,float,float,float>`_
   self.text_align = align
   self.text.rendered = false
 
 method setTextColor*(self: LabelRef, color: ColorRef) {.base.} =
+  ## Changes text color.
+  ##
+  ## Arguments:
+  ## - `color` - new text color.
   self.text.setColor(color)
   self.text.rendered = false
 
 method setTextFont*(self: LabelRef, font: FontPtr) {.base.} =
+  ## Changes text font.
+  ##
+  ## Arguments:
+  ## - `font` - new text font.
   self.text.font = font
   self.text.rendered = false