button.nim 2.8 KB

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