button.nim 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # author: Ethosa
  2. ## Handles mouse clicks.
  3. import
  4. ../thirdparty/opengl,
  5. ../thirdparty/sdl2,
  6. ../core/vector2,
  7. ../core/rect2,
  8. ../core/anchor,
  9. ../core/input,
  10. ../core/enums,
  11. ../core/color,
  12. ../core/font,
  13. ../nodes/node,
  14. ../graphics/drawable,
  15. control,
  16. label
  17. type
  18. ButtonObj* = object of LabelObj
  19. button_mask*: cint ## Mask for handle clicks
  20. action_mask*: cint ## BUTTON_RELEASE or BUTTON_CLICK.
  21. normal_background*: DrawableRef ## color, when button is not pressed and not hovered.
  22. hover_background*: DrawableRef ## color, when button hovered.
  23. press_background*: DrawableRef ## color, when button pressed.
  24. on_touch*: proc(self: ButtonRef, x, y: float): void ## This called, when user clicks on button.
  25. ButtonRef* = ref ButtonObj
  26. proc Button*(name: string = "Button"): ButtonRef =
  27. ## Creates a new Button node.
  28. ##
  29. ## Arguments:
  30. ## - `name` is a node name.
  31. runnableExamples:
  32. var my_button = Button("Button")
  33. nodepattern(ButtonRef)
  34. controlpattern()
  35. result.rect_size.x = 160
  36. result.rect_size.y = 40
  37. result.text = stext""
  38. result.text_align = Anchor(0.5, 0.5, 0.5, 0.5)
  39. result.button_mask = BUTTON_LEFT.cint
  40. result.action_mask = BUTTON_RELEASE
  41. result.normal_background = Drawable()
  42. result.hover_background = Drawable()
  43. result.press_background = Drawable()
  44. result.normal_background.setColor(Color(0x444444ff))
  45. result.hover_background.setColor(Color(0x505050ff))
  46. result.press_background.setColor(Color(0x595959ff))
  47. result.on_touch = proc(self: ButtonRef, x, y: float) = discard
  48. result.kind = BUTTON_NODE
  49. method draw*(self: ButtonRef, w, h: GLfloat) =
  50. ## this method uses in the `window.nim`.
  51. self.background =
  52. if self.pressed and self.focused:
  53. self.press_background
  54. elif self.hovered and not mouse_pressed:
  55. self.hover_background
  56. else:
  57. self.normal_background
  58. procCall self.LabelRef.draw(w, h)
  59. method duplicate*(self: ButtonRef, obj: var ButtonObj): ButtonRef {.base.} =
  60. ## Duplicates Button object and creates a new Button node pointer.
  61. self.deepCopy()
  62. method handle*(self: ButtonRef, event: InputEvent, mouse_on: var NodeRef) =
  63. ## Handles user input. This uses in the `window.nim`.
  64. procCall self.ControlRef.handle(event, mouse_on)
  65. if self.hovered and self.focused:
  66. if event.kind == MOUSE and self.action_mask == 1 and mouse_pressed and self.button_mask == event.button_index:
  67. self.on_touch(self, event.x, event.y)
  68. elif event.kind == MOUSE and self.action_mask == 0 and not mouse_pressed and self.button_mask == event.button_index:
  69. self.on_touch(self, event.x, event.y)