Advertisement
Chronos_Ouroboros

Single-texture button

Nov 25th, 2017
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. class S7_ZF_Button : S7_ZF_Element {
  2. enum ButtonStates {
  3. B_INACTIVE,
  4. B_HOVER,
  5. B_CLICK,
  6. B_DISABLED
  7. }
  8.  
  9. S7_ZF_Handler handler;
  10. string command;
  11.  
  12. Font fnt;
  13. string text;
  14. double textScale;
  15.  
  16. S7_ZF_BoxTextures textures[4];
  17. string btnTextures [4];
  18. bool singleTex;
  19.  
  20. int buttonState;
  21.  
  22. Vector2 mousePos;
  23.  
  24. void setTexture(string inactive, string hover, string click) {
  25. self.btnTextures[B_INACTIVE] = inactive;
  26. self.btnTextures[B_HOVER] = hover;
  27. self.btnTextures[B_CLICK] = click;
  28. self.singleTex = true;
  29. }
  30.  
  31. void config(string text = "", S7_ZF_Handler handler = NULL, string command = "",
  32. S7_ZF_BoxTextures inactive = NULL, S7_ZF_BoxTextures hover = NULL,
  33. S7_ZF_BoxTextures click = NULL, Font fnt = NULL, double textScale = 1) {
  34. if (fnt == NULL) {
  35. self.fnt = smallfont;
  36. }
  37. else {
  38. self.fnt = fnt;
  39. }
  40. self.handler = handler;
  41. self.command = command;
  42. self.text = text;
  43. self.textScale = textScale;
  44. self.textures[B_INACTIVE] = inactive;
  45. self.textures[B_HOVER] = hover;
  46. self.textures[B_CLICK] = click;
  47. self.singleTex = false;
  48. }
  49.  
  50. S7_ZF_Button init(Vector2 pos, Vector2 size, string text = "", S7_ZF_Handler handler = NULL, string command = "",
  51. S7_ZF_BoxTextures inactive = NULL, S7_ZF_BoxTextures hover = NULL, S7_ZF_BoxTextures click = NULL,
  52. Font fnt = NULL, double textScale = 1) {
  53. self.config(text, handler, command, inactive, hover, click, fnt, textScale);
  54. self.setBox(pos, size);
  55.  
  56. return self;
  57. }
  58.  
  59. override void ticker() {
  60. }
  61.  
  62. override void drawer() {
  63. if (singleTex) {
  64. string texture = btnTextures[buttonState];
  65. drawImage((0, 0), texture, true);
  66. } else {
  67. S7_ZF_BoxTextures textures = textures[buttonState];
  68. drawBox((0, 0), box.size, textures);
  69. }
  70.  
  71. // draw the text in the middle of the button
  72. Vector2 textSize = (fnt.stringWidth(text), fnt.getHeight()) * textScale;
  73. Vector2 textPos = (box.size - textSize) / 2;
  74. drawText(textPos, fnt, text, Font.CR_WHITE, textScale);
  75. }
  76.  
  77. override void onUIEvent(UIEvent ev) {
  78. // if the player's clicked, and their mouse is in the right place, set the state accordingly
  79. if (ev.type == UIEvent.Type_LButtonDown) {
  80. if (box.pointCollides(master.screenToRel(mousePos))) {
  81. buttonState = B_CLICK;
  82. }
  83. }
  84. // if the player's releasing, check if their mouse is still in the correct range and trigger method if it was
  85. else if (ev.type == UIEvent.Type_LButtonUp) {
  86. if (box.pointCollides(master.screenToRel(mousePos)) && buttonState == B_CLICK) {
  87. buttonState = B_HOVER;
  88. handler.buttonCommand(command);
  89. }
  90. else {
  91. buttonState = B_INACTIVE;
  92. }
  93. }
  94. // if the player's mouse has moved, update the tracked position and do a quick hover check
  95. else if (ev.type == UIEvent.Type_MouseMove) {
  96. mousePos = (ev.mouseX, ev.mouseY);
  97. if (buttonState != B_CLICK) {
  98. if (box.pointCollides(master.screenToRel(mousePos))) {
  99. buttonState = B_HOVER;
  100. }
  101. else {
  102. buttonState = B_INACTIVE;
  103. }
  104. }
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement