rich_label.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # author: Ethosa
  2. import
  3. ../thirdparty/opengl,
  4. ../thirdparty/opengl/glut,
  5. ../core/vector2,
  6. ../core/rect2,
  7. ../core/anchor,
  8. ../core/input,
  9. ../core/enums,
  10. ../core/color,
  11. ../core/color_text,
  12. ../nodes/node,
  13. control
  14. type
  15. RichLabelObj* = object of ControlPtr
  16. font*: pointer ## Glut font data.
  17. spacing*: float ## Font spacing.
  18. size*: float ## Font size.
  19. text*: ColorTextRef ## RichLabel text.
  20. text_align*: AnchorRef ## Text align.
  21. RichLabelPtr* = ptr RichLabelObj
  22. proc RichLabel*(name: string, variable: var RichLabelObj): RichLabelPtr =
  23. nodepattern(RichLabelObj)
  24. controlpattern()
  25. variable.rect_size.x = 40
  26. variable.rect_size.y = 40
  27. variable.text = clrtext""
  28. variable.font = GLUT_BITMAP_HELVETICA_12
  29. variable.size = 12
  30. variable.spacing = 2
  31. variable.text_align = Anchor(0, 0, 0, 0)
  32. proc RichLabel*(obj: var RichLabelObj): RichLabelPtr {.inline.} =
  33. RichLabel("RichLabel", obj)
  34. method draw*(self: RichLabelPtr, w, h: GLfloat) =
  35. self.calcGlobalPosition()
  36. let
  37. x = -w/2 + self.global_position.x
  38. y = h/2 - self.global_position.y
  39. glColor4f(self.background_color.r, self.background_color.g, self.background_color.b, self.background_color.a)
  40. glRectf(x, y, x+self.rect_size.x, y-self.rect_size.y)
  41. var th = 0f
  42. for line in self.text.splitLines(): # get text height
  43. th += self.spacing + self.size
  44. if th != 0:
  45. th -= self.spacing
  46. var ty = y - self.rect_size.y*self.text_align.y1 + th*self.text_align.y2 - self.size
  47. for line in self.text.splitLines():
  48. var tw = self.font.glutBitmapLength($line).float
  49. # Draw text:
  50. var tx = x + self.rect_size.x*self.text_align.x1 - tw * self.text_align.x2
  51. for c in line.chars:
  52. let
  53. cw = self.font.glutBitmapWidth(c.c.int).float
  54. right =
  55. if self.text_align.x2 > 0.9 and self.text_align.x1 > 0.9:
  56. 1f
  57. else:
  58. 0f
  59. bottom =
  60. if self.text_align.y2 > 0.9 and self.text_align.y1 > 0.9:
  61. 1f
  62. else:
  63. 0f
  64. if tx >= x and tx < x + self.rect_size.x+right and ty <= y and ty > y - self.rect_size.y+bottom:
  65. glColor4f(c.color.r, c.color.g, c.color.b, c.color.a)
  66. glRasterPos2f(tx, ty) # set char position
  67. self.font.glutBitmapCharacter(c.c.int) # render char
  68. tx += cw
  69. ty -= self.spacing + self.size
  70. # Press
  71. if self.pressed:
  72. self.press(last_event.x, last_event.y)
  73. method dublicate*(self: RichLabelPtr, obj: var RichLabelObj): RichLabelPtr {.base.} =
  74. obj = self[]
  75. obj.addr
  76. method setTextAlign*(self: RichLabelPtr, align: AnchorRef) {.base.} =
  77. self.text_align = align
  78. method setTextAlign*(self: RichLabelPtr, x1, y1, x2, y2: float) {.base.} =
  79. self.text_align = Anchor(x1, y1, x2, y2)
  80. method setText*(self: RichLabelPtr, value: ColorTextRef) {.base.} =
  81. self.text = value