Browse Source

add text rendering in canvas node

Ethosa 4 years ago
parent
commit
feddeed555
4 changed files with 100 additions and 74 deletions
  1. 6 1
      src/nodesnim/core/font.nim
  2. 93 73
      src/nodesnim/nodes/canvas.nim
  3. BIN
      tests/assets/canvas.png
  4. 1 0
      tests/test2.nim

+ 6 - 1
src/nodesnim/core/font.nim

@@ -183,7 +183,7 @@ proc getCaretPos*(text: StyleText, pos: uint32): tuple[a: Vector2Obj, b: uint16]
         return result
     result[0].y -= text.spacing
 
-proc render*(text: StyleText, size: Vector2Obj, anchor: AnchorObj) =
+proc renderSurface*(text: StyleText, anchor: AnchorObj): SurfacePtr =
   when defined(debug):
     if text.font.isNil():
       error("Font is not loaded!")
@@ -215,7 +215,12 @@ proc render*(text: StyleText, size: Vector2Obj, anchor: AnchorObj) =
         freeSurface(rendered)
         x += w
       y += h + text.spacing.cint
+    return surface
+
+proc render*(text: StyleText, size: Vector2Obj, anchor: AnchorObj) =
+  var surface = renderSurface(text, anchor)
 
+  if not surface.isNil():
     text.texture.size.x = surface.w.float
     text.texture.size.y = surface.h.float
 

+ 93 - 73
src/nodesnim/nodes/canvas.nim

@@ -12,6 +12,7 @@ import
   ../core/color,
   ../core/anchor,
   ../core/enums,
+  ../core/font,
   ../core/tools,
 
   node
@@ -56,21 +57,21 @@ proc Canvas*(name: string = "Canvas"): CanvasRef =
   result.renderer = result.surface.createSoftwareRenderer()
   result.kind = CANVAS_NODE
   result.type_of_node = NODE_TYPE_CONTROL
-  glGenTextures(1, result.canvas_texture.addr)
 
 
 # --- Private --- #
 template loadColor(color_argument_name: untyped): untyped =
   let clr = toUint32Tuple(`color_argument_name`)
-  echo clr
   canvas.renderer.setDrawColor(clr.r.uint8, clr.g.uint8, clr.b.uint8, clr.a.uint8)
 
 template loadGL(canvas: untyped): untyped =
   `canvas`.renderer.present()
   discard `canvas`.renderer.readPixels(nil, 0, `canvas`.surface.pixels, 0)
+  if `canvas`.canvas_texture == 0:
+    glGenTextures(1, `canvas`.canvas_texture.addr)
   glBindTexture(GL_TEXTURE_2D, `canvas`.canvas_texture)
-  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
-  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
+  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
+  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA.GLint, `canvas`.surface.w,  `canvas`.surface.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, `canvas`.surface.pixels)
@@ -133,21 +134,67 @@ method move*(self: CanvasRef, x, y: float) {.base, inline.} =
   self.position += Vector2(x, y)
   self.anchor.clear()
 
-method bezier*(canvas: CanvasRef, x1, y1, x2, y2, x3, y3: GLfloat, color: ColorRef) {.base.} =
-  ## Draws a quadric bezier curve at 3 points.
-  loadColor(color)
-  for pnt in bezier_iter(0.001, Vector2(x1, y1), Vector2(x2, y2), Vector2(x3, y3)):
-    canvas.renderer.drawPoint(pnt.x.cint, pnt.y.cint)
-  loadGL(canvas)
+method resize*(self: CanvasRef, w, h: GLfloat, save_anchor: bool = false) {.base.} =
+  ## Resizes canvas.
+  ##
+  ## Arguments:
+  ## - `w` is a new width.
+  ## - `h` is a new height.
+  if w > self.rect_min_size.x:
+    self.rect_size.x = w
+    if not save_anchor:
+      self.size_anchor.x = 0.0
+  else:
+    self.rect_size.x = self.rect_min_size.x
+  if h > self.rect_min_size.y:
+    self.rect_size.y = h
+    if not save_anchor:
+      self.size_anchor.y = 0.0
+  else:
+    self.rect_size.y = self.rect_min_size.y
+  if self.kind == CANVAS_NODE:
+    var new_surface = createRGBSurface(
+      0, w.cint, h.cint, 32,
+      0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000'u32)
+    self.surface.blitSurface(nil, new_surface, nil)
+    self.renderer.destroy()
+    self.surface.freeSurface()
+    self.surface = new_surface
+    self.renderer = self.surface.createSoftwareRenderer()
+    loadGL(self)
 
-method cubic_bezier*(canvas: CanvasRef, x1, y1, x2, y2, x3, y3, x4, y4: GLfloat, color: ColorRef) {.base.} =
+method setAnchor*(self: CanvasRef, anchor: AnchorObj) {.base.} =
+  ## Changes node anchor.
+  ##
+  ## Arguments:
+  ## - `anchor` - AnchorObj object.
+  self.anchor = anchor
+
+method setAnchor*(self: CanvasRef, x1, y1, x2, y2: float) {.base.} =
+  ## Changes node anchor.
+  ##
+  ## Arguments:
+  ## - `x1` and `y1` - anchor relative to the parent node.
+  ## - `x2` and `y2` - anchor relative to this node.
+  self.anchor = Anchor(x1, y1, x2, y2)
+
+
+method setSizeAnchor*(self: CanvasRef, anchor: Vector2Obj) {.base.} =
+  self.size_anchor = anchor
+
+method setSizeAnchor*(self: CanvasRef, x, y: float) {.base.} =
+  self.size_anchor = Vector2(x, y)
+
+
+# --- Draw functions --- #
+proc bezier*(canvas: CanvasRef, x1, y1, x2, y2, x3, y3: GLfloat, color: ColorRef) =
   ## Draws a quadric bezier curve at 3 points.
   loadColor(color)
-  for pnt in cubic_bezier_iter(0.001, Vector2(x1, y1), Vector2(x2, y2), Vector2(x3, y3), Vector2(x4, y4)):
+  for pnt in bezier_iter(0.001, Vector2(x1, y1), Vector2(x2, y2), Vector2(x3, y3)):
     canvas.renderer.drawPoint(pnt.x.cint, pnt.y.cint)
   loadGL(canvas)
 
-method circle*(canvas: CanvasRef, x, y, radius: GLfloat, color: ColorRef, quality: int = 100) {.base.} =
+proc circle*(canvas: CanvasRef, x, y, radius: GLfloat, color: ColorRef, quality: int = 100) =
   ## Draws a circle in the canvas.
   ##
   ## Arguments:
@@ -162,18 +209,20 @@ method circle*(canvas: CanvasRef, x, y, radius: GLfloat, color: ColorRef, qualit
     canvas.renderer.drawPoint((x + radius*cos(angle)).cint, (y + radius*sin(angle)).cint)
   loadGL(canvas)
 
-method point*(canvas: CanvasRef, x, y: GLfloat, color: ColorRef) {.base.} =
-  ## Draws a point in the canvas.
-  ##
-  ## Arguments:
-  ## - `x` - point position at X axis.
-  ## - `y` - point position at Y axis.
-  ## - `color` - point color.
+proc cubic_bezier*(canvas: CanvasRef, x1, y1, x2, y2, x3, y3, x4, y4: GLfloat, color: ColorRef) =
+  ## Draws a quadric bezier curve at 3 points.
   loadColor(color)
-  canvas.renderer.drawPoint(x.cint, y.cint)
+  for pnt in cubic_bezier_iter(0.001, Vector2(x1, y1), Vector2(x2, y2), Vector2(x3, y3), Vector2(x4, y4)):
+    canvas.renderer.drawPoint(pnt.x.cint, pnt.y.cint)
   loadGL(canvas)
 
-method line*(canvas: CanvasRef, x1, y1, x2, y2: GLfloat, color: ColorRef) {.base.} =
+proc fill*(canvas: CanvasRef, color: ColorRef) =
+  ## Fills canvas.
+  loadColor(color)
+  canvas.renderer.clear()
+  loadGL(canvas)
+
+proc line*(canvas: CanvasRef, x1, y1, x2, y2: GLfloat, color: ColorRef) =
   ## Draws a line in the canvas.
   ##
   ## Arguments:
@@ -186,7 +235,7 @@ method line*(canvas: CanvasRef, x1, y1, x2, y2: GLfloat, color: ColorRef) {.base
   canvas.renderer.drawLine(x1.cint, y1.cint, x2.cint, y2.cint)
   loadGL(canvas)
 
-method rect*(canvas: CanvasRef, x1, y1, x2, y2: GLfloat, color: ColorRef) {.base.} =
+proc rect*(canvas: CanvasRef, x1, y1, x2, y2: GLfloat, color: ColorRef) =
   ## Draws a line in the canvas.
   ##
   ## Arguments:
@@ -200,63 +249,34 @@ method rect*(canvas: CanvasRef, x1, y1, x2, y2: GLfloat, color: ColorRef) {.base
   canvas.renderer.drawRect(rectangle)
   loadGL(canvas)
 
-method fill*(canvas: CanvasRef, color: ColorRef) {.base.} =
-  ## Fills canvas.
+proc point*(canvas: CanvasRef, x, y: GLfloat, color: ColorRef) =
+  ## Draws a point in the canvas.
+  ##
+  ## Arguments:
+  ## - `x` - point position at X axis.
+  ## - `y` - point position at Y axis.
+  ## - `color` - point color.
   loadColor(color)
-  canvas.renderer.clear()
+  canvas.renderer.drawPoint(x.cint, y.cint)
   loadGL(canvas)
 
-method resize*(self: CanvasRef, w, h: GLfloat, save_anchor: bool = false) {.base.} =
-  ## Resizes canvas.
+proc text*(canvas: CanvasRef, text: StyleText | string, x, y: Glfloat, align: Vector2Obj = Vector2()) =
+  ## Draws multiline text.
   ##
   ## Arguments:
-  ## - `w` is a new width.
-  ## - `h` is a new height.
-  if w > self.rect_min_size.x:
-    self.rect_size.x = w
-    if not save_anchor:
-      self.size_anchor.x = 0.0
+  ## - `text` - multiline colored text.
+  ## - `align` - horizontal text alignment.
+  when text is StyleText:
+    var
+      surface = text.renderSurface(Anchor(align.x, 0, align.y, 0))
+      rectangle = rect(x.cint, y.cint, x.cint+surface.w, y.cint+surface.h)
   else:
-    self.rect_size.x = self.rect_min_size.x
-  if h > self.rect_min_size.y:
-    self.rect_size.y = h
-    if not save_anchor:
-      self.size_anchor.y = 0.0
-  else:
-    self.rect_size.y = self.rect_min_size.y
-  if self.kind == CANVAS_NODE:
-    var new_surface = createRGBSurface(
-      0, w.cint, h.cint, 32,
-      0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000'u32)
-    self.surface.blitSurface(nil, new_surface, nil)
-    self.renderer.destroy()
-    self.surface.freeSurface()
-    self.surface = new_surface
-    self.renderer = self.surface.createSoftwareRenderer()
-    loadGL(self)
+    var
+      surface = stext(text).renderSurface(Anchor(align.x, 0, align.y, 0))
+      rectangle = rect(x.cint, y.cint, x.cint+surface.w, y.cint+surface.h)
+  surface.blitSurface(nil, canvas.surface, rectangle.addr)
+  loadGL(canvas)
 
 proc saveAs*(self: CanvasRef, filename: cstring) =
   ## Saves canvas as image file.
   discard self.surface.savePNG(filename)
-
-method setAnchor*(self: CanvasRef, anchor: AnchorObj) {.base.} =
-  ## Changes node anchor.
-  ##
-  ## Arguments:
-  ## - `anchor` - AnchorObj object.
-  self.anchor = anchor
-
-method setAnchor*(self: CanvasRef, x1, y1, x2, y2: float) {.base.} =
-  ## Changes node anchor.
-  ##
-  ## Arguments:
-  ## - `x1` and `y1` - anchor relative to the parent node.
-  ## - `x2` and `y2` - anchor relative to this node.
-  self.anchor = Anchor(x1, y1, x2, y2)
-
-
-method setSizeAnchor*(self: CanvasRef, anchor: Vector2Obj) {.base.} =
-  self.size_anchor = anchor
-
-method setSizeAnchor*(self: CanvasRef, x, y: float) {.base.} =
-  self.size_anchor = Vector2(x, y)

BIN
tests/assets/canvas.png


+ 1 - 0
tests/test2.nim

@@ -20,6 +20,7 @@ canvas.line(200, -150, 0, 256, Color("#0e1317ff"))
 canvas.bezier(0, 0, 256, 0, 256, 256, Color("#227"))
 canvas.cubic_bezier(0, 0, 256, 0, 0, 256, 256, 256, Color("#272"))
 canvas.move(74.4, 89.4)
+canvas.text("hello!,\nworld!", 64, 64, Vector2(1, 1))
 canvas.saveAs("assets/canvas.png")  # save result in file.