Ethosa před 5 roky
rodič
revize
1b1f3e057b

+ 3 - 2
src/nodesnim.nim

@@ -20,11 +20,12 @@ import
 
   nodesnim/nodescontrol/control,
   nodesnim/nodescontrol/color_rect,
-  nodesnim/nodescontrol/texture_rect
+  nodesnim/nodescontrol/texture_rect,
+  nodesnim/nodescontrol/label
 
 export
   opengl, glut,
   window, environment,
   vector2, rect2, enums, anchor, color, exceptions, input, image,
   node, scene, canvas,
-  control, color_rect, texture_rect
+  control, color_rect, texture_rect, label

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

@@ -0,0 +1,72 @@
+# author: Ethosa
+import
+  strutils,
+  ../thirdparty/opengl,
+  ../thirdparty/opengl/glut,
+
+  ../core/vector2,
+  ../core/rect2,
+  ../core/anchor,
+  ../core/input,
+  ../core/enums,
+  ../core/color,
+
+  ../nodes/node,
+  control
+
+
+type
+  LabelObj* = object of ControlPtr
+    font*: pointer          ## Glut font data.
+    spacing*: float         ## Font spacing.
+    size*: float            ## Font size.
+    text*: string           ## Label text.
+    color*: ColorRef        ## Text color.
+    text_align*: AnchorRef  ## Text align.
+  LabelPtr* = ptr LabelObj
+
+
+proc Label*(name: string, variable: var LabelObj): LabelPtr =
+  nodepattern(LabelObj)
+  controlpattern()
+  variable.rect_size.x = 40
+  variable.rect_size.y = 40
+  variable.text = ""
+  variable.font = GLUT_BITMAP_HELVETICA_12
+  variable.size = 12
+  variable.spacing = 2
+  variable.text_align = Anchor(0, 0, 0, 0)
+  variable.color = Color(1f, 1f, 1f)
+
+proc Label*(obj: var LabelObj): LabelPtr {.inline.} =
+  Label("Label", obj)
+
+
+method draw*(self: LabelPtr, w, h: GLfloat) =
+  self.calcGlobalPosition()
+  let
+    x = -w/2 + self.global_position.x
+    y = h/2 - self.global_position.y
+
+  glColor4f(self.color.r, self.color.g, self.color.b, self.color.a)
+  var
+    th = 0f
+    ty = 0f
+
+  for line in self.text.splitLines():  # get text height
+    th += self.spacing + self.size
+  ty = y - self.rect_size.y*self.text_align.y1 + th * self.text_align.y2
+
+  for line in self.text.splitLines():
+    var tw = self.font.glutBitmapLength(line).float
+    # Draw text:
+    var tx = x + self.rect_size.x*self.text_align.x1 - tw * self.text_align.x2
+    for c in line:
+      glRasterPos2f(tx, ty)  # set char position
+      self.font.glutBitmapCharacter(c.int)  # render char
+      tx += self.font.glutBitmapWidth(c.int).float
+    ty -= self.spacing + self.size
+
+  # Press
+  if self.pressed:
+    self.press(last_event.x, last_event.y)

+ 2 - 2
src/nodesnim/thirdparty/sdl2/ttf.nim

@@ -10,10 +10,10 @@ when not defined(SDL_Static):
 else:
   static: echo "SDL_Static option is deprecated and will soon be removed. Instead please use --dynlibOverride:SDL2."
 
-import sdl2
+import ../sdl2
 
 type
-  FontPtr* = ptr object{.pure.}
+  FontPtr*{.pure.} = ptr object
 
 # Set up for C function definitions, even when using C++
 # Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL

+ 9 - 0
src/nodesnim/window.nim

@@ -138,11 +138,17 @@ proc motion(x, y: cint) {.cdecl.} =
 # ---- Public ---- #
 proc addScene*(scene: ScenePtr) =
   ## Adds a new scenes in app.
+  ##
+  ## Arguments:
+  ## - `scene` - pointer to the Scene object.
   if scene notin scenes:
     scenes.add(scene)
 
 proc changeScene*(name: string): bool {.discardable.} =
   ## Changes current scene.
+  ##
+  ## Arguments:
+  ## - `name` - name of the added scene.
   result = false
   for scene in scenes:
     if scene.name == name:
@@ -157,6 +163,9 @@ proc changeScene*(name: string): bool {.discardable.} =
 
 proc setMainScene*(name: string) =
   ## Set up main scene.
+  ##
+  ## Arguments:
+  ## - `name` - name of the added scene.
   for scene in scenes:
     if scene.name == name:
       main_scene = scene

+ 2 - 0
tests/README.md

@@ -7,3 +7,5 @@
 5. [Handle Control node events.](https://github.com/Ethosa/nodesnim/blob/master/tests/test5.nim)
 6. [Anchor setting.](https://github.com/Ethosa/nodesnim/blob/master/tests/test6.nim)
 7. [Change scenes.](https://github.com/Ethosa/nodesnim/blob/master/tests/test7.nim)
+8. [Use TextureRect node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test8.nim)
+9. [Use Label node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test9.nim)

+ 23 - 0
tests/test9.nim

@@ -0,0 +1,23 @@
+# --- Test 9. Use Label node. --- #
+import nodesnim
+
+
+Window("hello world")
+
+var
+  mainobj: SceneObj
+  main = Scene("Main", mainobj)
+
+  labelobj: LabelObj
+  label = Label(labelobj)
+
+main.addChild(label)
+
+label.text = "Hello, world!\nsecondline\nThis is a long sentence."  # Change label text.
+label.text_align = Anchor(0.2, 0.5, 0.2, 0.5)  # try to change it ^^.
+label.size_anchor = Vector2(1, 1)
+
+
+addScene(main)
+setMainScene("Main")
+windowLaunch()