button.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. ../nodes/node,
  12. control,
  13. label
  14. type
  15. ButtonObj* = object of LabelObj
  16. button_mask*: cint ## Mask for handle clicks
  17. action_mask*: cint ## BUTTON_RELEASE or BUTTON_CLICK.
  18. normal_background_color*: ColorRef ## color, when button is not pressed and not hovered.
  19. hover_background_color*: ColorRef ## color, when button hovered.
  20. press_background_color*: ColorRef ## color, when button pressed.
  21. normal_color*: ColorRef ## text color, whenwhen button is not pressed and not hovered.
  22. hover_color*: ColorRef ## text color, when button hovered.
  23. press_color*: ColorRef ## text color, when button pressed.
  24. on_click*: proc(x, y: float): void ## This called, when user clicks on button.
  25. ButtonPtr* = ptr ButtonObj
  26. proc Button*(name: string, variable: var ButtonObj): ButtonPtr =
  27. ## Creates a new Button node pointer.
  28. ##
  29. ## Arguments:
  30. ## - `name` is a node name.
  31. ## - `variable` is a ButtonObj variable.
  32. runnableExamples:
  33. var
  34. my_button_obj: ButtonObj
  35. my_button = Button("Button", my_button_obj)
  36. nodepattern(ButtonObj)
  37. controlpattern()
  38. variable.rect_size.x = 40
  39. variable.rect_size.y = 40
  40. variable.text = ""
  41. variable.font = GLUT_BITMAP_HELVETICA_12
  42. variable.size = 12
  43. variable.spacing = 2
  44. variable.text_align = Anchor(0.5, 0.5, 0.5, 0.5)
  45. variable.color = Color(1f, 1f, 1f)
  46. variable.normal_color = Color(1f, 1f, 1f)
  47. variable.hover_color = Color(1f, 1f, 1f)
  48. variable.press_color = Color(1f, 1f, 1f)
  49. variable.button_mask = BUTTON_LEFT
  50. variable.action_mask = BUTTON_RELEASE
  51. variable.normal_background_color = Color(0x444444ff)
  52. variable.hover_background_color = Color(0x505050ff)
  53. variable.press_background_color = Color(0x595959ff)
  54. variable.on_click = proc(x, y: float) = discard
  55. proc Button*(obj: var ButtonObj): ButtonPtr {.inline.} =
  56. ## Creates a new Button node pointer with default node name "Button".
  57. ##
  58. ## Arguments:
  59. ## - `variable` is a ButtonObj variable.
  60. runnableExamples:
  61. var
  62. my_button_obj: ButtonObj
  63. my_button = Button(my_button_obj)
  64. Button("Button", obj)
  65. method draw*(self: ButtonPtr, w, h: GLfloat) =
  66. ## this method uses in the `window.nim`.
  67. self.calcGlobalPosition()
  68. let
  69. x = -w/2 + self.global_position.x
  70. y = h/2 - self.global_position.y
  71. color =
  72. if self.pressed:
  73. self.press_background_color
  74. elif self.hovered:
  75. self.hover_background_color
  76. else:
  77. self.normal_background_color
  78. self.color =
  79. if self.pressed:
  80. self.press_color
  81. elif self.hovered:
  82. self.hover_color
  83. else:
  84. self.normal_color
  85. glColor4f(color.r, color.g, color.b, color.a)
  86. glRectf(x, y, x + self.rect_size.x, y - self.rect_size.y)
  87. procCall self.LabelPtr.draw(w, h)
  88. method duplicate*(self: ButtonPtr, obj: var ButtonObj): ButtonPtr {.base.} =
  89. ## Duplicates Button object and creates a new Button node pointer.
  90. obj = self[]
  91. obj.addr
  92. method handle*(self: ButtonPtr, event: InputEvent, mouse_on: var NodePtr) =
  93. ## Handles user input. This uses in the `window.nim`.
  94. procCall self.ControlPtr.handle(event, mouse_on)
  95. if self.hovered:
  96. if event.kind == MOUSE and self.action_mask == 1 and mouse_pressed and self.button_mask == event.button_index:
  97. self.on_click(event.x, event.y)
  98. elif event.kind == MOUSE and self.action_mask == 0 and not mouse_pressed and self.button_mask == event.button_index:
  99. self.on_click(event.x, event.y)