瀏覽代碼

add new Example.

SakiKawasaki 5 年之前
父節點
當前提交
99d8be9103

+ 220 - 0
examples/calculator/main.nim

@@ -0,0 +1,220 @@
+import nodesnim
+import strutils
+
+
+Window("Calc")
+
+var
+  main_obj: SceneObj
+  main = Scene("Main", main_obj)
+
+  first: string = ""
+  second: string = ""
+  sign: string = ""
+
+  vbox_obj: VBoxObj
+  vbox = VBox(vbox_obj)
+
+  result_obj: LabelObj
+  result = Label("Result", result_obj)
+
+  buttons_obj: GridBoxObj
+  buttons = GridBox("Buttons", buttons_obj)
+
+  button_7_obj: ButtonObj
+  button_8_obj: ButtonObj
+  button_9_obj: ButtonObj
+  button_4_obj: ButtonObj
+  button_5_obj: ButtonObj
+  button_6_obj: ButtonObj
+  button_1_obj: ButtonObj
+  button_2_obj: ButtonObj
+  button_3_obj: ButtonObj
+  button_0_obj: ButtonObj
+  button_00_obj: ButtonObj
+  button_add_obj: ButtonObj
+  button_sub_obj: ButtonObj
+  button_mul_obj: ButtonObj
+  button_div_obj: ButtonObj
+  button_eq_obj: ButtonObj
+  button_7 = Button("Button 7", button_7_obj)
+  button_8 = Button("Button 8", button_8_obj)
+  button_9 = Button("Button 9", button_9_obj)
+  button_4 = Button("Button 4", button_4_obj)
+  button_5 = Button("Button 5", button_5_obj)
+  button_6 = Button("Button 6", button_6_obj)
+  button_1 = Button("Button 1", button_1_obj)
+  button_2 = Button("Button 2", button_2_obj)
+  button_3 = Button("Button 3", button_3_obj)
+  button_0 = Button("Button 0", button_0_obj)
+  button_00 = Button("Button 00", button_00_obj)
+  button_add = Button("Button +", button_add_obj)
+  button_sub = Button("Button -", button_sub_obj)
+  button_mul = Button("Button *", button_mul_obj)
+  button_div = Button("Button /", button_div_obj)
+  button_eq = Button("Button =", button_eq_obj)
+
+main.addChild(vbox)
+vbox.addChild(result)
+vbox.addChild(buttons)
+vbox.setChildAnchor(0.5, 0.5, 0.5, 0.5)
+vbox.setSizeAnchor(1, 1)
+buttons.setRaw(4)
+
+buttons.addChild(button_7)
+button_7.text = "7"
+button_7.on_click =
+  proc(x, y: float) =
+    if sign == "":
+      first &= "7"
+    else:
+      second &= "7"
+
+buttons.addChild(button_8)
+button_8.text = "8"
+button_8.on_click =
+  proc(x, y: float) =
+    if sign == "":
+      first &= "8"
+    else:
+      second &= "8"
+
+buttons.addChild(button_9)
+button_9.text = "9"
+button_9.on_click =
+  proc(x, y: float) =
+    if sign == "":
+      first &= "9"
+    else:
+      second &= "9"
+
+buttons.addChild(button_add)
+button_add.text = "+"
+button_add.on_click =
+  proc(x, y: float) =
+    sign = "+"
+
+buttons.addChild(button_4)
+button_4.text = "4"
+button_4.on_click =
+  proc(x, y: float) =
+    if sign == "":
+      first &= "4"
+    else:
+      second &= "4"
+
+buttons.addChild(button_5)
+button_5.text = "5"
+button_5.on_click =
+  proc(x, y: float) =
+    if sign == "":
+      first &= "5"
+    else:
+      second &= "5"
+
+buttons.addChild(button_6)
+button_6.text = "6"
+button_6.on_click =
+  proc(x, y: float) =
+    if sign == "":
+      first &= "6"
+    else:
+      second &= "6"
+
+buttons.addChild(button_sub)
+button_sub.text = "-"
+button_sub.on_click =
+  proc(x, y: float) =
+    sign = "-"
+
+buttons.addChild(button_1)
+button_1.text = "1"
+button_1.on_click =
+  proc(x, y: float) =
+    if sign == "":
+      first &= "1"
+    else:
+      second &= "1"
+
+buttons.addChild(button_2)
+button_2.text = "2"
+button_2.on_click =
+  proc(x, y: float) =
+    if sign == "":
+      first &= "2"
+    else:
+      second &= "2"
+
+buttons.addChild(button_3)
+button_3.text = "3"
+button_3.on_click =
+  proc(x, y: float) =
+    if sign == "":
+      first &= "3"
+    else:
+      second &= "3"
+
+buttons.addChild(button_mul)
+button_mul.text = "*"
+button_mul.on_click =
+  proc(x, y: float) =
+    sign = "*"
+
+buttons.addChild(button_0)
+button_0.text = "0"
+button_0.on_click =
+  proc(x, y: float) =
+    if sign == "" and first != "":
+      first &= "0"
+    elif sign != "/":
+      second &= "0"
+
+buttons.addChild(button_00)
+button_00.text = "00"
+button_00.on_click =
+  proc(x, y: float) =
+    if sign == "" and first != "":
+      first &= "00"
+    elif second != "":
+      second &= "00"
+
+buttons.addChild(button_div)
+button_div.text = "/"
+button_div.on_click =
+  proc(x, y: float) =
+    sign = "/"
+
+buttons.addChild(button_eq)
+button_eq.text = "="
+button_eq.on_click =
+  proc(x, y: float) =
+    first =
+      if sign == "+":
+        $(parseFloat(first) + parseFloat(second))
+      elif sign == "-":
+        $(parseFloat(first) - parseFloat(second))
+      elif sign == "*":
+        $(parseFloat(first) * parseFloat(second))
+      elif sign == "/":
+        $(parseFloat(first) / parseFloat(second))
+      else:
+        first
+    if sign != "":
+      second = ""
+      sign = ""
+
+result.setTextAlign(1, 0, 1, 0)
+result.resize(160, 32)
+result.process =
+  proc() =
+    if sign == "":
+      result.text = first
+    elif second == "":
+      result.text = first & " " & sign
+    else:
+      result.text = first & " " & sign & " " & second
+
+
+addScene(main)
+setMainScene("Main")
+windowLaunch()

+ 1 - 0
examples/calculator/nim.cfg

@@ -0,0 +1 @@
+--path:"../../src"

二進制
examples/calculator/one.gif


+ 5 - 0
examples/calculator/readme.md

@@ -0,0 +1,5 @@
+# Calculator
+
+See [`main.nim`](https://github.com/Ethosa/nodesnim/blob/master/examples/calculator/main.nim) file.
+
+![gif here](https://github.com/Ethosa/nodesnim/blob/master/examples/calculator/one.gif)

+ 1 - 0
examples/readme.md

@@ -1,3 +1,4 @@
 # Examples
 
 1. [Hello world](https://github.com/Ethosa/nodesnim/blob/master/examples/hello_world)
+2. [Calculator](https://github.com/Ethosa/nodesnim/blob/master/examples/calculator)

+ 7 - 0
src/nodesnim/nodescontrol/box.nim

@@ -52,6 +52,13 @@ method addChild*(self: BoxPtr, child: NodePtr) =
 
 
 method draw*(self: BoxPtr, w, h: GLfloat) =
+  let
+    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)
+
   for child in self.children:
     child.position.x = self.rect_size.x*self.child_anchor.x1 - child.rect_size.x*self.child_anchor.x2
     child.position.y = self.rect_size.y*self.child_anchor.y1 - child.rect_size.y*self.child_anchor.y2

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

@@ -7,6 +7,7 @@ import
   ../core/anchor,
   ../core/input,
   ../core/enums,
+  ../core/color,
 
   ../nodes/node,
   ../nodes/canvas
@@ -19,6 +20,7 @@ type
     focused*: bool
 
     mousemode*: MouseMode
+    background_color*: ColorRef
 
     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.
@@ -36,6 +38,7 @@ template controlpattern*: untyped =
   variable.pressed = false
 
   variable.mousemode = MOUSEMODE_SEE
+  variable.background_color = Color()
 
   variable.mouse_enter = proc(x, y: float) = discard
   variable.mouse_exit = proc(x, y: float) = discard
@@ -67,6 +70,13 @@ method calcPositionAnchor*(self: ControlPtr) =
 method draw*(self: ControlPtr, w, h: GLfloat) =
   {.warning[LockLevel]: off.}
   self.calcGlobalPosition()
+  let
+    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)
+
   # Press
   if self.pressed:
     self.press(last_event.x, last_event.y)
@@ -107,3 +117,6 @@ method handle*(self: ControlPtr, event: InputEvent, mouse_on: var NodePtr) =
   if not mouse_pressed and self.pressed:
     self.pressed = false
     self.release(event.x, event.y)
+
+method setBackgroundColor*(self: ControlPtr, color: ColorRef) {.base.} =
+  self.background_color = color

+ 0 - 2
src/nodesnim/nodescontrol/edittext.nim

@@ -28,7 +28,6 @@ type
     hint_text*: string
     color*: ColorRef        ## Text color.
     hint_color*: ColorRef   ## Hint color.
-    background_color*: ColorRef
     caret_color*: ColorRef
     text_align*: AnchorRef  ## Text align.
   EditTextPtr* = ptr EditTextObj
@@ -46,7 +45,6 @@ proc EditText*(name: string, variable: var EditTextObj): EditTextPtr =
   variable.text_align = Anchor(0, 0, 0, 0)
   variable.color = Color(1f, 1f, 1f)
   variable.hint_color = Color(0.8, 0.8, 0.8)
-  variable.background_color = Color(0x212121ff)
   variable.hint_text = "Edit text ..."
   variable.caret_position = 0
   variable.blit_caret = true

+ 7 - 0
src/nodesnim/nodescontrol/grid_box.nim

@@ -68,6 +68,13 @@ method addChild*(self: GridBoxPtr, child: NodePtr) =
 
 
 method draw*(self: GridBoxPtr, w, h: GLfloat) =
+  let
+    x1 = -w/2 + self.global_position.x
+    y1 = h/2 - self.global_position.y
+
+  glColor4f(self.background_color.r, self.background_color.g, self.background_color.b, self.background_color.a)
+  glRectf(x1, y1, x1+self.rect_size.x, y1-self.rect_size.y)
+
   var
     raw = 0
     fakesize = self.getChildSize()

+ 7 - 0
src/nodesnim/nodescontrol/hbox.nim

@@ -54,6 +54,13 @@ method addChild*(self: HBoxPtr, child: NodePtr) =
 
 
 method draw*(self: HBoxPtr, w, h: GLfloat) =
+  let
+    x1 = -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(x1, y, x1+self.rect_size.x, y-self.rect_size.y)
+
   var
     fakesize = self.getChildSize()
     x = self.rect_size.x*self.child_anchor.x1 - fakesize.x*self.child_anchor.x2

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

@@ -48,6 +48,10 @@ method draw*(self: LabelPtr, 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.color.r, self.color.g, self.color.b, self.color.a)
   var th = 0f
 

+ 4 - 0
src/nodesnim/nodescontrol/texture_rect.nim

@@ -44,6 +44,10 @@ method draw*(self: TextureRectPtr, 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)
+
+
   if self.texture > 0:
     glEnable(GL_TEXTURE_2D)
     glBindTexture(GL_TEXTURE_2D, self.texture)

+ 7 - 0
src/nodesnim/nodescontrol/vbox.nim

@@ -54,6 +54,13 @@ method addChild*(self: VBoxPtr, child: NodePtr) =
 
 
 method draw*(self: VBoxPtr, w, h: GLfloat) =
+  let
+    x = -w/2 + self.global_position.x
+    y1 = 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, y1, x+self.rect_size.x, y1-self.rect_size.y)
+
   var
     fakesize = self.getChildSize()
     y = self.rect_size.y*self.child_anchor.y1 - fakesize.y*self.child_anchor.y2