浏览代码

rewrite calculator example :eyes:

Ethosa 4 年之前
父节点
当前提交
12ee059378
共有 3 个文件被更改,包括 56 次插入89 次删除
  1. 39 81
      examples/calculator/main.nim
  2. 16 4
      src/nodesnim/core/scene_builder.nim
  3. 1 4
      src/nodesnim/nodescontrol/label.nim

+ 39 - 81
examples/calculator/main.nim

@@ -5,46 +5,10 @@ import strutils
 Window("Calc")
 
 var
-  main = Scene("Main")
-
   first: string = ""
   second: string = ""
   sign: string = ""
 
-  vbox = VBox()
-  result = Label("Result")
-  buttons = GridBox("Buttons")
-
-  button_7 = Button("Button 7")
-  button_8 = Button("Button 8")
-  button_9 = Button("Button 9")
-  button_4 = Button("Button 4")
-  button_5 = Button("Button 5")
-  button_6 = Button("Button 6")
-  button_1 = Button("Button 1")
-  button_2 = Button("Button 2")
-  button_3 = Button("Button 3")
-  button_0 = Button("Button 0")
-  button_00 = Button("Button 00")
-  button_add = Button("Button +")
-  button_sub = Button("Button -")
-  button_mul = Button("Button *")
-  button_div = Button("Button /")
-  button_eq = Button("Button =")
-
-main.addChild(vbox)
-vbox.addChild(result)
-vbox.addChild(buttons)
-vbox.setChildAnchor(0.5, 0.5, 0.5, 0.5)
-vbox.setSizeAnchor(1, 1)
-buttons.setRow(4)
-buttons.addChilds(
-  button_7, button_8, button_9, button_add,
-  button_4, button_5, button_6, button_sub,
-  button_1, button_2, button_3, button_mul,
-  button_0, button_00, button_div, button_eq)
-
-
 proc number(self: ButtonRef, x, y: float) =
   if sign == "":
     first &= self.text
@@ -52,15 +16,17 @@ proc number(self: ButtonRef, x, y: float) =
     second &= self.text
 
 proc equal(): string =
+  let f = parseFloat(first)
   result =
-    if sign == "+":
-      $(parseFloat(first) + parseFloat(second))
-    elif sign == "-":
-      $(parseFloat(first) - parseFloat(second))
-    elif sign == "*":
-      $(parseFloat(first) * parseFloat(second))
-    elif sign == "/":
-      $(parseFloat(first) / parseFloat(second))
+    case sign:
+    of "+":
+      $(f + parseFloat(second))
+    of "-":
+      $(f - parseFloat(second))
+    of "x":
+      $(f * parseFloat(second))
+    of "/":
+      $(f / parseFloat(second))
     else:
       first
 
@@ -72,37 +38,34 @@ proc on_sign(self: ButtonRef, x, y: float) =
   elif first != "":
     sign = self.text
 
-
-button_1.text = "1"
-button_2.text = "2"
-button_3.text = "3"
-button_4.text = "4"
-button_5.text = "5"
-button_6.text = "6"
-button_7.text = "7"
-button_8.text = "8"
-button_9.text = "9"
-button_0.text = "0"
-button_00.text = "00"
-
-button_sub.text = "-"
-button_add.text = "+"
-button_mul.text = "*"
-button_div.text = "/"
-
-button_1.on_touch = number
-button_2.on_touch = number
-button_3.on_touch = number
-button_4.on_touch = number
-button_5.on_touch = number
-button_6.on_touch = number
-button_7.on_touch = number
-button_8.on_touch = number
-button_9.on_touch = number
-button_add.on_touch = on_sign
-button_sub.on_touch = on_sign
-button_mul.on_touch = on_sign
-button_div.on_touch = on_sign
+build:
+  - Scene main:
+    - Vbox vbox:
+      call setChildAnchor(0.5, 0.5, 0.5, 0.5)
+      call setSizeAnchor(1, 1)
+      - Label result:
+        call setTextAlign(1, 0, 1, 0)
+        call resize(160, 32)
+      - GridBox buttons:
+        call setRow(4)
+        - Button button_7(text: "7", on_touch: number)
+        - Button button_8(text: "8", on_touch: number)
+        - Button button_9(text: "9", on_touch: number)
+        - Button button_4(text: "4", on_touch: number)
+        - Button button_5(text: "5", on_touch: number)
+        - Button button_6(text: "6", on_touch: number)
+        - Button button_1(text: "1", on_touch: number)
+        - Button button_2(text: "2", on_touch: number)
+        - Button button_3(text: "3", on_touch: number)
+        - Button button_0(text: "0")
+        - Button button_00(text: "00")
+        - Button button_add(text: "+", on_touch: on_sign)
+        # Signs
+        - Button button_sub(text: "-", on_touch: on_sign)
+        - Button button_mul(text: "x", on_touch: on_sign)
+        - Button button_div(text: "/", on_touch: on_sign)
+        - Button button_eq:
+          text: "="
 
 
 button_0@on_touch(self, x, y):
@@ -118,15 +81,12 @@ button_00@on_touch(self, x, y):
     second &= "00"
 
 
-button_eq.text = "="
 button_eq@on_touch(self, x, y):
   first = equal()
   if sign != "":
     second = ""
     sign = ""
 
-result.setTextAlign(1, 0, 1, 0)
-result.resize(160, 32)
 result@on_process(self):
   if sign == "":
     result.text = first
@@ -135,7 +95,5 @@ result@on_process(self):
   else:
     result.text = first & " " & sign & " " & second
 
-
-addScene(main)
-setMainScene("Main")
+addMainScene(main)
 windowLaunch()

+ 16 - 4
src/nodesnim/core/scene_builder.nim

@@ -4,19 +4,31 @@ import
 
 proc addNode(level: var seq[NimNode], code: NimNode): NimNode {.compileTime.} =
   result = newStmtList()
-  if code.kind == nnkStmtList:
+  if code.kind in [nnkStmtList, nnkObjConstr]:
     for line in code.children():
       if line.kind == nnkPrefix:
         if line[0].kind == nnkIdent and line[1].kind == nnkCommand:
           if $line[0] == "-":
-            result.add(newVarStmt(line[1][1], newCall($line[1][0])))
+            if line[1][1].kind == nnkIdent:
+              result.add(newVarStmt(line[1][1], newCall($line[1][0])))
+            elif line[1][1].kind == nnkObjConstr:
+              result.add(newVarStmt(line[1][1][0], newCall($line[1][0])))
             if level.len() > 0:
               # - Scene main_scene:
-              result.add(newCall("addChild", level[^1], line[1][1]))
+              if line[1][1].kind == nnkIdent:
+                result.add(newCall("addChild", level[^1], line[1][1]))
+              elif line[1][1].kind == nnkObjConstr:
+                result.add(newCall("addChild", level[^1], line[1][1][0]))
+                level.add(line[1][1][0])
+                var nodes = addNode(level, line[1][1])
+                for i in nodes.children():
+                  result.add(i)
+      # call methodName(arg1, arg2) -> currentNode.methodName(arg1, arg2)
       elif line.kind == nnkCommand and $line[0] == "call" and level.len() > 0:
         line[1].insert(1, level[^1])
         result.add(line[1])
-      elif line.kind == nnkCall and level.len() > 1:
+      # property: value -> currentNode.property = value
+      elif line.kind in [nnkCall, nnkExprColonExpr] and level.len() > 1:
         var attr = newNimNode(nnkAsgn)
         attr.add(newNimNode(nnkDotExpr))
         attr[0].add(level[^1])

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

@@ -49,14 +49,11 @@ proc Label*(name: string = "Label"): LabelRef =
 
 method draw*(self: LabelRef, 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
 
-  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