Browse Source

fix Scroll node.

Ethosa 5 years ago
parent
commit
e7076a7c11
4 changed files with 75 additions and 16 deletions
  1. 5 0
      src/nodesnim/nodes/node.nim
  2. 5 0
      src/nodesnim/nodes/scene.nim
  3. 35 5
      src/nodesnim/nodescontrol/scroll.nim
  4. 30 11
      tests/test20.nim

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

@@ -85,6 +85,11 @@ method draw*(self: NodePtr, w, h: GLfloat) {.base.} =
   ## This used in the Window object.
   discard
 
+method draw2stage*(self: NodePtr, w, h: GLfloat) {.base.} =
+  ## Draws node.
+  ## This used in the Window object.
+  discard
+
 method dublicate*(self: NodePtr, obj: var NodeObj): NodePtr {.base.} =
   obj = self[]
   obj.addr

+ 5 - 0
src/nodesnim/nodes/scene.nim

@@ -32,6 +32,11 @@ method drawScene*(scene: ScenePtr, w, h: GLfloat, paused: bool) {.base.} =
         child.is_ready = true
       child.process()
       child.draw(w, h)
+  for child in scene.getChildIter():
+    if paused and child.getPauseMode() != PROCESS:
+      continue
+    if child.visible:
+      child.draw2stage(w, h)
 
 method dublicate*(self: ScenePtr, obj: var SceneObj): ScenePtr {.base.} =
   obj = self[]

+ 35 - 5
src/nodesnim/nodescontrol/scroll.nim

@@ -39,6 +39,7 @@ proc Scroll*(name: string, variable: var ScrollObj): ScrollPtr =
   variable.thumb_color = Color(0, 0, 0, 128)
   variable.thumb_y_has_mouse = false
   variable.thumb_x_has_mouse = false
+  variable.mousemode = MOUSEMODE_IGNORE
 
 proc Scroll*(obj: var ScrollObj): ScrollPtr {.inline.} =
   Scroll("Scroll", obj)
@@ -66,6 +67,15 @@ method draw*(self: ScrollPtr, w, h: GLfloat) =
   glColor4f(self.background_color.r, self.background_color.g, self.background_color.b, self.background_color.a)
   glRectf(x, y, x+self.viewport_w, y-self.viewport_h)
 
+  # Press
+  if self.pressed:
+    self.press(last_event.x, last_event.y)
+
+method draw2stage*(self: ScrollPtr, w, h: GLfloat) =
+  let
+    x = -w/2 + self.global_position.x
+    y = h/2 - self.global_position.y
+
   if self.children.len() > 0:
     var child = self.children[0]
     self.resize(child.rect_size.x, child.rect_size.y)
@@ -88,9 +98,14 @@ method draw*(self: ScrollPtr, w, h: GLfloat) =
       glColor4f(self.thumb_color.r, self.thumb_color.g, self.thumb_color.b, self.thumb_color.a)
       glRectf(x + self.viewport_w - self.thumb_width, y - thumb_y, x+self.viewport_w, y - thumb_y - thumb_h)
 
-  # Press
-  if self.pressed:
-    self.press(last_event.x, last_event.y)
+    if self.viewport_w < self.rect_size.x:
+      # Back:
+      glColor4f(self.back_color.r, self.back_color.g, self.back_color.b, self.back_color.a)
+      glRectf(x, y - self.viewport_h + self.thumb_height, x + self.viewport_w - self.thumb_width, y-self.viewport_h)
+
+      # Thumb:
+      glColor4f(self.thumb_color.r, self.thumb_color.g, self.thumb_color.b, self.thumb_color.a)
+      glRectf(x + thumb_x, y - self.viewport_h + self.thumb_height, x + thumb_x + thumb_w, y-self.viewport_h)
 
 method scrollBy*(self: ScrollPtr, x, y: float) {.base.} =
   if x + self.viewport_x + self.viewport_w < self.rect_size.x and x + self.viewport_x > 0:
@@ -124,16 +139,31 @@ method handle*(self: ScrollPtr, event: InputEvent, mouse_on: var NodePtr) =
         self.thumb_width,
         thumb_h
       ).hasPoint(event.x, event.y)
+    mouse_in_x = Rect2(
+        self.global_position.x + thumb_x,
+        self.global_position.y + self.viewport_h - self.thumb_height,
+        thumb_w,
+        self.thumb_height
+      ).hasPoint(event.x, event.y)
 
-  if mouse_in:
+  if mouse_in:  # Keyboard movement
     if event.kind == KEYBOARD:
       if event.key_cint in pressed_keys_cints:  # Special chars
         if event.key_cint == K_UP:
           self.scrollBy(0, -40)
         elif event.key_cint == K_DOWN:
           self.scrollBy(0, 40)
-  if (mouse_in_y and mouse_pressed) or self.thumb_y_has_mouse:
+
+  # Mouse Y
+  if (mouse_in_y and mouse_pressed and event.kind == MOUSE) or self.thumb_y_has_mouse:
     self.thumb_y_has_mouse = true
     self.scrollBy(0, -event.yrel)
   if not mouse_pressed and self.thumb_y_has_mouse:
     self.thumb_y_has_mouse = false
+
+  # Mouse X
+  if (mouse_in_x and mouse_pressed and event.kind == MOUSE) or self.thumb_x_has_mouse:
+    self.thumb_x_has_mouse = true
+    self.scrollBy(-event.xrel, 0)
+  if not mouse_pressed and self.thumb_x_has_mouse:
+    self.thumb_x_has_mouse = false

+ 30 - 11
tests/test20.nim

@@ -11,8 +11,8 @@ var
   scrollobj: ScrollObj
   scroll = Scroll(scrollobj)
 
-  vboxobj: VBoxObj     # Create a VBoxObj.
-  vbox = VBox(vboxobj)  # Create pointer to the VBoxObj.
+  grid_boxobj: GridBoxObj     # Create a GridBoxObj.
+  grid_box = GridBox(grid_boxobj)  # Create pointer to the GridBoxObj.
 
   redobj: ColorRectObj
   red = ColorRect(redobj)  # #ff6699
@@ -23,24 +23,43 @@ var
   orangeobj: ColorRectObj
   orange = ColorRect(orangeobj)  # #ffaa00
 
+  mangoobj: ColorRectObj
+  mango = ColorRect(mangoobj)  # #ffcc33
+
+  yellowobj: ColorRectObj
+  yellow = ColorRect(yellowobj)  # #ffcc66
+
+  red2obj: ColorRectObj
+  red2 = ColorRect(red2obj)  # #ff6655
+
 
 red.color = Color(0xff6699ff'u32)
 pink.color = Color(0xff64ffff'u32)
 orange.color = Color(0xffaa00ff'u32)
+mango.color = Color(0xffcc33ff'u32)
+yellow.color = Color(0xffcc66ff'u32)
+red2.color = Color(0xff6655ff'u32)
 
-red.resize(128, 128)
-pink.resize(128, 128)
-orange.resize(128, 128)
+red.resize(150, 150)
+pink.resize(50, 50)
+orange.resize(50, 50)
+mango.resize(50, 50)
+yellow.resize(50, 50)
+red2.resize(150, 150)
 
 # Add rects in the Box node.
-vbox.addChild(red)
-vbox.addChild(pink)
-vbox.addChild(orange)
+grid_box.addChild(red)
+grid_box.addChild(pink)
+grid_box.addChild(orange)
+grid_box.addChild(mango)
+grid_box.addChild(yellow)
+grid_box.addChild(red2)
 
 main.addChild(scroll)
-scroll.addChild(vbox)
-vbox.setAnchor(0, 0.5, 0, 0.5)  # Box anchor in the scene.
-vbox.setChildAnchor(0, 1, 0, 1)
+grid_box.setAnchor(0.5, 0.5, 0.5, 0.5)
+grid_box.setRaw(3)
+grid_box.setSeparator(2)
+scroll.addChild(grid_box)
 
 
 addScene(main)