button.dd 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. (include "button.ddh")
  2. # create tile facing front
  3. (class_function Button void create (group)
  4. (group
  5. (dd_matrix_identity this)
  6. (= this.width 0)
  7. (= this.height 0)
  8. (= this.x 0)
  9. (= this.y 0)
  10. (= this.z 0)
  11. # idle animation
  12. (= this.idleRot 0)
  13. (= this.idleRotTarget 7)
  14. (= this.idleCol 0)
  15. (= this.pX 0)
  16. (= this.pY 0)
  17. (= this.pW 0.1)
  18. (= this.pSize -5)
  19. )
  20. )
  21. (class_function Button void setSize (group float sizeX float sizeY)
  22. (group
  23. (= this.width sizeX)
  24. (= this.height sizeY)
  25. )
  26. )
  27. (class_function Button void setPosition (group float posX float posY float posZ)
  28. (group
  29. (= this.x posX)
  30. (= this.y posY)
  31. (= this.z posZ)
  32. )
  33. )
  34. (class_function Button void setProportionalPosition (group float propX float propY)
  35. (group
  36. (= this.pX propX)
  37. (= this.pY propY)
  38. (this.resize)
  39. )
  40. )
  41. (class_function Button void setProportionalWidth (group float propW)
  42. (group
  43. (= this.pW propW)
  44. (this.resize)
  45. )
  46. )
  47. (class_function Button void setProportionalSize (group float propSize)
  48. (group
  49. (= this.pSize (/ -5 propSize))
  50. (this.resize)
  51. )
  52. )
  53. # animate the tiles to the current side
  54. (class_function Button void update (group int isSelected int isClicked)
  55. (group
  56. # animate to target if clicked
  57. (if isClicked
  58. (group
  59. (= this.idleRot (dd_math_ease_linear 0.2 this.idleRot this.idleRotTarget))
  60. (= this.idleCol (dd_math_ease_linear 0.2 this.idleCol 2))
  61. )
  62. # animate to target if selected
  63. isSelected
  64. (group
  65. (= this.idleRot (dd_math_ease_linear 0.2 this.idleRot this.idleRotTarget))
  66. (= this.idleCol (dd_math_ease_linear 0.2 this.idleCol 1))
  67. )
  68. # animate to idle
  69. (group
  70. (= this.idleRot (dd_math_ease_linear 0.2 this.idleRot 0))
  71. (= this.idleCol (dd_math_ease_linear 0.2 this.idleCol 0))
  72. )
  73. )
  74. # flip animation when reached the target
  75. (if (< (dd_math_abs (- this.idleRot this.idleRotTarget)) 0.15)
  76. (= this.idleRotTarget (* this.idleRotTarget -1))
  77. )
  78. )
  79. ) # update
  80. # draw tile based on its side
  81. (class_function Button void applyTransform (group)
  82. (group
  83. (dd_translatef this.x this.y this.z)
  84. )
  85. )
  86. # draw tile based on its side
  87. (class_function Button void applyMatrixTransform (group)
  88. (group
  89. (dd_multMatrixf this)
  90. (dd_rotatef this.idleRot 0 0 1)
  91. (dd_scalef
  92. (+ 1.0 (* this.idleCol 0.2))
  93. (+ 1.0 (* this.idleCol 0.2))
  94. (+ 1.0 (* this.idleCol 0.2))
  95. )
  96. )
  97. )
  98. # draw tile based on its side
  99. (class_function Button void drawRaw (group)
  100. (group
  101. (this.mesh.draw)
  102. )
  103. )
  104. # draw tile based on its side
  105. (class_function Button void draw (group)
  106. (group
  107. (dd_pushMatrix)
  108. (this.applyTransform)
  109. (this.applyMatrixTransform)
  110. (this.drawRaw)
  111. (dd_popMatrix)
  112. )
  113. )
  114. (class_function Button void clean (group)
  115. (group
  116. )
  117. )
  118. (class_function Button int hasMouseCollided (group)
  119. (group
  120. # get mouse's proportioned position
  121. (def float screenProportionX)
  122. (= screenProportionX (- (dd_mouse_xProportion) 0.5))
  123. (def float screenProportionY)
  124. (= screenProportionY (- (dd_mouse_yProportion) 0.5))
  125. # get x and y of the plane where the button is
  126. (def float planeX)
  127. (= planeX (* screenProportionX (dd_screen_width_get (* this.z -1))))
  128. (def float planeY)
  129. (= planeY (* screenProportionY (dd_screen_height_get this.z)))
  130. # check collision
  131. (if (&&
  132. (>= planeX (- this.x (/ this.width 2)))
  133. (<= planeX (+ this.x (/ this.width 2)))
  134. (>= planeY (- this.y (/ this.height 2)))
  135. (<= planeY (+ this.y (/ this.height 2)))
  136. )
  137. (return 1)
  138. (return 0)
  139. )
  140. )
  141. )
  142. (class_function Button void resize (group)
  143. (group
  144. # figure out if button should be smaller
  145. (def float objScale (dd_math_minf (* (dd_screen_width_get 5) this.pW) 1.0))
  146. (= this.z (/ this.pSize objScale))
  147. # place the button on the screen based on the proportions
  148. (= this.x (* (dd_screen_width_get (* this.z -1)) (- this.pX 0.5)))
  149. (= this.y (* (dd_screen_height_get (* this.z -1)) (- this.pY 0.5)))
  150. )
  151. )