button.nim 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # author: Ethosa
  2. ## Handles mouse clicks.
  3. import
  4. ../thirdparty/opengl,
  5. ../thirdparty/opengl/glut,
  6. ../core/vector2,
  7. ../core/rect2,
  8. ../core/anchor,
  9. ../core/input,
  10. ../core/enums,
  11. ../core/color,
  12. ../nodes/node,
  13. control,
  14. label
  15. type
  16. ButtonObj* = object of LabelObj
  17. button_mask*: cint ## Mask for handle clicks
  18. action_mask*: cint ## BUTTON_RELEASE or BUTTON_CLICK.
  19. normal_background_color*: ColorRef ## color, when button is not pressed and not hovered.
  20. hover_background_color*: ColorRef ## color, when button hovered.
  21. press_background_color*: ColorRef ## color, when button pressed.
  22. normal_color*: ColorRef ## text color, whenwhen button is not pressed and not hovered.
  23. hover_color*: ColorRef ## text color, when button hovered.
  24. press_color*: ColorRef ## text color, when button pressed.
  25. on_click*: proc(x, y: float): void ## This called, when user clicks on button.
  26. ButtonPtr* = ptr ButtonObj
  27. var buttons: seq[ButtonObj] = @[]
  28. proc Button*(name: string = "Button"): ButtonPtr =
  29. ## Creates a new Button node pointer.
  30. ##
  31. ## Arguments:
  32. ## - `name` is a node name.
  33. runnableExamples:
  34. var my_button = Button("Button")
  35. var variable: ButtonObj
  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. variable.kind = BUTTON_NODE
  56. buttons.add(variable)
  57. return addr buttons[^1]
  58. method draw*(self: ButtonPtr, w, h: GLfloat) =
  59. ## this method uses in the `window.nim`.
  60. self.calcGlobalPosition()
  61. let
  62. x = -w/2 + self.global_position.x
  63. y = h/2 - self.global_position.y
  64. color =
  65. if self.pressed:
  66. self.press_background_color
  67. elif self.hovered:
  68. self.hover_background_color
  69. else:
  70. self.normal_background_color
  71. self.color =
  72. if self.pressed:
  73. self.press_color
  74. elif self.hovered:
  75. self.hover_color
  76. else:
  77. self.normal_color
  78. glColor4f(color.r, color.g, color.b, color.a)
  79. glRectf(x, y, x + self.rect_size.x, y - self.rect_size.y)
  80. procCall self.LabelPtr.draw(w, h)
  81. method duplicate*(self: ButtonPtr): ButtonPtr {.base.} =
  82. ## Duplicates Button object and creates a new Button node pointer.
  83. var obj = self[]
  84. buttons.add(obj)
  85. return addr buttons[^1]
  86. method handle*(self: ButtonPtr, event: InputEvent, mouse_on: var NodePtr) =
  87. ## Handles user input. This uses in the `window.nim`.
  88. procCall self.ControlPtr.handle(event, mouse_on)
  89. if self.hovered:
  90. if event.kind == MOUSE and self.action_mask == 1 and mouse_pressed and self.button_mask == event.button_index:
  91. self.on_click(event.x, event.y)
  92. elif event.kind == MOUSE and self.action_mask == 0 and not mouse_pressed and self.button_mask == event.button_index:
  93. self.on_click(event.x, event.y)