Quellcode durchsuchen

fix `sample_messenger` example :eyes:

Ethosa vor 3 Jahren
Ursprung
Commit
11aede1fd3

+ 10 - 1
examples/hello_world/main.nim

@@ -1,4 +1,13 @@
-import nodesnim
+import
+  nodesnim/window,
+  nodesnim/core/anchor,
+  nodesnim/core/scene_builder,
+  nodesnim/core/color,
+  nodesnim/nodes/scene,
+  nodesnim/nodes/node,
+  nodesnim/nodes/canvas,
+  nodesnim/nodescontrol/label,
+  nodesnim/nodescontrol/control
 
 Window("Hello, world!")
 

+ 1 - 0
examples/sample_messenger/main.nim

@@ -9,6 +9,7 @@ when isMainModule:
   Window("Client", 320, 640)
 
   addKeyAction("send", 13)  # Enter
+  changeTheme("light")
 
 
   addScene(chat_scene)

+ 46 - 9
examples/sample_messenger/scenes/chat.nim

@@ -16,11 +16,13 @@ gradient.setCornerColors(Color("#C9D6FF"), Color("#C9D6FF"), Color("#E2E2E2"), C
 build:
   - Scene (chat_scene):
     - Control background:
-      call setBackground(gradient)
-      call setSizeAnchor(1, 1)
+      call:
+        setBackground(gradient)
+        setSizeAnchor(1, 1)
 
-      - Label chat:
-        call setSizeAnchor(1, 1)
+      - VBox vbox:
+        call:
+          setSizeAnchor(1, 0.8)
 
       - EditText message:
         call:
@@ -38,16 +40,51 @@ proc listenChat(arg: tuple[scene: ptr SceneRef, username: ptr string, timed_chat
   while true:
     var
       response = waitFor client.get("http://127.0.0.1:5000/getchat")
-      text = ""
+      tmp_chat = arg.timed_chat
     arg.timed_chat[] = parseJson(waitFor response.body())["data"].getElems
 
+    {.cast(gcsafe).}:
+      arg.scene[][0].removeChild(0)
+      build:
+        - VBox chat:
+          separator: 8
+          call:
+            resize(arg.scene[].rect_size.x, 0)
+            setSizeAnchor(1, 0.65)
+      arg.scene[][0].insertChild(0, chat)
+
     var i = 0
     while i < arg.timed_chat[].len:
-      text &= (arg.timed_chat[][i].str & ": " & arg.timed_chat[][i+1].str & "\n\n")
+      if tmp_chat[][i].kind != JString:
+        continue
+      {.cast(gcsafe).}:
+        build:
+          - Control back:
+            - Label label:
+              call:
+                setText(arg.timed_chat[][i].str & ": " & arg.timed_chat[][i+1].str)
+            call:
+              setSizeAnchor(1, 0)
+              resize(0, label.rect_size.y)
+        chat.addChild(back)
+
+        if tmp_chat[][i].str == username:
+          label.setStyle(style({
+              border-radius: 10,
+              border-detail: 8,
+              background-color: "#ffc"
+            }))
+          label.setAnchor(1, 0, 1, 0)
+        else:
+          label.setStyle(style({
+              border-radius: 10,
+              border-detail: 8,
+              background-color: "#dda"
+            }))
+        chat.calcPositionAnchor()
+        back.calcPositionAnchor()
+        label.calcPositionAnchor()
       inc i, 2
-    
-    # TODO: Fix it
-    # arg.scene[].getNode("background/chat").LabelRef.setText(text)
 
     waitFor sleepAsync(100)
 

+ 2 - 1
src/nodesnim/core/themes.nim

@@ -34,7 +34,8 @@ var
              "foreground": Color("#39414b"),
              "url_color": Color("#2a9afc")}.toTable())
   ]
-  current_theme* = themes[0].deepCopy()
+{.cast(noSideEffect).}:
+  var current_theme* = themes[0].deepCopy()
 
 proc addTheme*(theme: ThemeRef) =
   themes.add(theme)

+ 10 - 0
src/nodesnim/nodes/node.nim

@@ -190,6 +190,9 @@ method getRootNode*(self: NodeRef): NodeRef {.base.} =
   while result.parent != nil:
     result = result.parent
 
+method insertChild*(self: NodeRef, index: int, node: NodeRef) {.base.} =
+  self.children.insert(node, index)
+
 method isParentOf*(self, other: NodeRef): bool {.base, inline.} =
   other in self.children
 
@@ -247,6 +250,9 @@ method removeChild*(self: NodeRef, other: NodeRef) {.base.} =
   if index != -1:
     self.removeChild(index)
 
+method removeChildren*(self: NodeRef) {.base.} =
+  self.children = @[]
+
 method show*(self: NodeRef) {.base.} =
   self.visibility = VISIBLE
 
@@ -254,11 +260,15 @@ method delete*(self: NodeRef) {.base.} =
   ## Deletes current node.
   if self.parent != nil:
     self.parent.removeChild(self)
+  self.removeChildren()
 
 
 method `[]`*(self: NodeRef, index: int): NodeRef {.base, inline.} =
   self.getChild(index)
 
+method `[]`*(self: NodeRef, index: string): NodeRef {.base, inline.} =
+  self.getNode(index)
+
 method `~`*(self: NodeRef, path: string): NodeRef {.base, inline.} =
   self.getNode(path)
 

+ 1 - 1
tests/test6.nim

@@ -156,7 +156,7 @@ suite "Work with 2D nodes.":
           setMode(TILEMAP_ISOMETRIC)
           setTileSet(tileset)
           move(-2048, -1024)
-          #                         map size   layer count
+          #                 map size   layer count
           resizeMap(Vector2(32, 32), layer_count=4)
           fill(Vector2(1, 0))
           drawRect(3, 3, 10, 5, Vector2(15, 1))