/src/WidgetButton.cpp

http://github.com/clintbellanger/flare · C++ · 140 lines · 71 code · 33 blank · 36 comment · 16 complexity · 6f7b3e5c9d07d73fbab7ea0f8d206fda MD5 · raw file

  1. /*
  2. Copyright 2011 Clint Bellanger
  3. This file is part of FLARE.
  4. FLARE is free software: you can redistribute it and/or modify it under the terms
  5. of the GNU General Public License as published by the Free Software Foundation,
  6. either version 3 of the License, or (at your option) any later version.
  7. FLARE is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  9. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. FLARE. If not, see http://www.gnu.org/licenses/
  12. */
  13. /**
  14. * class WidgetButton
  15. */
  16. #include "WidgetButton.h"
  17. #include "SharedResources.h"
  18. using namespace std;
  19. WidgetButton::WidgetButton(const std::string& _fileName)
  20. : fileName(_fileName) {
  21. buttons = NULL;
  22. click = NULL;
  23. label = "";
  24. pos.x = pos.y = pos.w = pos.y = 0;
  25. enabled = true;
  26. pressed = false;
  27. loadArt();
  28. pos.w = buttons->w;
  29. pos.h = (buttons->h / 4); //height of one button
  30. }
  31. void WidgetButton::loadArt() {
  32. // load button images
  33. buttons = IMG_Load(fileName.c_str());
  34. if(!buttons) {
  35. fprintf(stderr, "Couldn't load image: %s\n", IMG_GetError());
  36. SDL_Quit();
  37. }
  38. // optimize
  39. SDL_Surface *cleanup = buttons;
  40. buttons = SDL_DisplayFormatAlpha(buttons);
  41. SDL_FreeSurface(cleanup);
  42. }
  43. /**
  44. * Sets and releases the "pressed" visual state of the button
  45. * If press and release, activate (return true)
  46. */
  47. bool WidgetButton::checkClick() {
  48. // disabled buttons can't be clicked;
  49. if (!enabled) return false;
  50. // main button already in use, new click not allowed
  51. if (inp->lock[MAIN1]) return false;
  52. // main click released, so the button state goes back to unpressed
  53. if (pressed && !inp->lock[MAIN1]) {
  54. pressed = false;
  55. if (isWithin(pos, inp->mouse)) {
  56. // activate upon release
  57. return true;
  58. }
  59. }
  60. pressed = false;
  61. // detect new click
  62. if (inp->pressing[MAIN1]) {
  63. if (isWithin(pos, inp->mouse)) {
  64. inp->lock[MAIN1] = true;
  65. pressed = true;
  66. }
  67. }
  68. return false;
  69. }
  70. void WidgetButton::render() {
  71. SDL_Rect src;
  72. src.x = 0;
  73. src.w = pos.w;
  74. src.h = pos.h;
  75. // the "button" surface contains button variations.
  76. // choose which variation to display.
  77. if (!enabled)
  78. src.y = BUTTON_GFX_DISABLED * pos.h;
  79. else if (pressed)
  80. src.y = BUTTON_GFX_PRESSED * pos.h;
  81. else if (isWithin(pos, inp->mouse))
  82. src.y = BUTTON_GFX_HOVER * pos.h;
  83. else
  84. src.y = BUTTON_GFX_NORMAL * pos.h;
  85. SDL_BlitSurface(buttons, &src, screen, &pos);
  86. wlabel.render();
  87. }
  88. /**
  89. * Create the text buffer
  90. */
  91. void WidgetButton::refresh() {
  92. if (label != "") {
  93. // render text
  94. int font_color = FONT_WHITE;
  95. if (!enabled) font_color = FONT_GRAY;
  96. int font_x = pos.x + (pos.w/2);
  97. int font_y = pos.y + (pos.h/2);
  98. wlabel.set(font_x, font_y, JUSTIFY_CENTER, VALIGN_CENTER, label, font_color);
  99. }
  100. }
  101. WidgetButton::~WidgetButton() {
  102. SDL_FreeSurface(buttons);
  103. }