texture_button.nim 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # author: Ethosa
  2. ## It is the convenient alternative of the Button node.
  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/image,
  12. ../core/color,
  13. ../nodes/node,
  14. control,
  15. label
  16. type
  17. TextureButtonObj* = object of LabelObj
  18. button_mask*: cint ## Mask for handle clicks
  19. action_mask*: cint ## BUTTON_RELEASE or BUTTON_CLICK.
  20. normal_background_texture*: GlTextureObj ## texture, when button is not pressed and not hovered.
  21. hover_background_texture*: GlTextureObj ## texture, when button hovered.
  22. press_background_texture*: GlTextureObj ## texture, when button pressed.
  23. normal_color*: ColorRef ## text color, whenwhen button is not pressed and not hovered.
  24. hover_color*: ColorRef ## text color, when button hovered.
  25. press_color*: ColorRef ## text color, when button pressed.
  26. on_touch*: proc(self: TextureButtonPtr, x, y: float): void ## This called, when user clicks on button.
  27. TextureButtonPtr* = ptr TextureButtonObj
  28. proc TextureButton*(name: string, variable: var TextureButtonObj): TextureButtonPtr =
  29. ## Creates a new TextureButton node pointer.
  30. ##
  31. ## Arguments:
  32. ## - `name` is a node name.
  33. ## - `variable` is a TextureButtonObj variable.
  34. runnableExamples:
  35. var
  36. my_button_obj: TextureButtonObj
  37. my_button = TextureButton("TextureButton", my_button_obj)
  38. nodepattern(TextureButtonObj)
  39. controlpattern()
  40. variable.rect_size.x = 40
  41. variable.rect_size.y = 40
  42. variable.text = ""
  43. variable.font = GLUT_BITMAP_HELVETICA_12
  44. variable.size = 12
  45. variable.spacing = 2
  46. variable.text_align = Anchor(0.5, 0.5, 0.5, 0.5)
  47. variable.color = Color(1f, 1f, 1f)
  48. variable.normal_color = Color(1f, 1f, 1f)
  49. variable.hover_color = Color(1f, 1f, 1f)
  50. variable.press_color = Color(1f, 1f, 1f)
  51. variable.button_mask = BUTTON_LEFT
  52. variable.action_mask = BUTTON_RELEASE
  53. variable.normal_background_texture = GlTextureObj()
  54. variable.hover_background_texture = GlTextureObj()
  55. variable.press_background_texture = GlTextureObj()
  56. variable.on_touch = proc(self: TextureButtonPtr, x, y: float) = discard
  57. variable.kind = TEXTURE_BUTTON_NODE
  58. proc TextureButton*(obj: var TextureButtonObj): TextureButtonPtr {.inline.} =
  59. ## Creates a new TextureButton node pointer with default node name "TextureButton".
  60. ##
  61. ## Arguments:
  62. ## - `variable` is a TextureButtonObj variable.
  63. runnableExamples:
  64. var
  65. my_button_obj: TextureButtonObj
  66. my_button = TextureButton(my_button_obj)
  67. TextureButton("TextureButton", obj)
  68. method draw*(self: TextureButtonPtr, w, h: GLfloat) =
  69. ## this method uses in the `window.nim`.
  70. let
  71. x = -w/2 + self.global_position.x
  72. y = h/2 - self.global_position.y
  73. texture =
  74. if self.pressed:
  75. self.press_background_texture
  76. elif self.hovered:
  77. self.hover_background_texture
  78. else:
  79. self.normal_background_texture
  80. self.color =
  81. if self.pressed:
  82. self.press_color
  83. elif self.hovered:
  84. self.hover_color
  85. else:
  86. self.normal_color
  87. # Texture
  88. if texture.texture > 0:
  89. glEnable(GL_TEXTURE_2D)
  90. glBindTexture(GL_TEXTURE_2D, texture.texture)
  91. glBegin(GL_QUADS)
  92. glTexCoord2f(0, 0)
  93. glVertex2f(x, y)
  94. glTexCoord2f(0, 1)
  95. glVertex2f(x, y - self.rect_size.y)
  96. glTexCoord2f(1, 1)
  97. glVertex2f(x + self.rect_size.x, y - self.rect_size.y)
  98. glTexCoord2f(1, 0)
  99. glVertex2f(x + self.rect_size.x, y)
  100. glEnd()
  101. glDisable(GL_TEXTURE_2D)
  102. procCall self.LabelPtr.draw(w, h)
  103. method duplicate*(self: TextureButtonPtr, obj: var TextureButtonObj): TextureButtonPtr {.base.} =
  104. ## Duplicates TextureButton object and creates a new TextureButton node pointer.
  105. obj = self[]
  106. obj.addr
  107. method handle*(self: TextureButtonPtr, event: InputEvent, mouse_on: var NodePtr) =
  108. ## Handles user input. This uses in the `window.nim`.
  109. procCall self.ControlPtr.handle(event, mouse_on)
  110. if self.hovered:
  111. if event.kind == MOUSE and self.action_mask == 1 and mouse_pressed and self.button_mask == event.button_index:
  112. self.on_touch(self, event.x, event.y)
  113. elif event.kind == MOUSE and self.action_mask == 0 and not mouse_pressed and self.button_mask == event.button_index:
  114. self.on_touch(self, event.x, event.y)
  115. method setNormalTexture*(self: TextureButtonPtr, texture: GlTextureObj) {.base.} =
  116. ## Changes button texture, when it not pressed and not hovered.
  117. self.normal_background_texture = texture
  118. method setHoverTexture*(self: TextureButtonPtr, texture: GlTextureObj) {.base.} =
  119. ## Changes button texture, when it hovered.
  120. self.hover_background_texture = texture
  121. method setPressTexture*(self: TextureButtonPtr, texture: GlTextureObj) {.base.} =
  122. ## Changes button texture, when it pressed.
  123. self.press_background_texture = texture