/src/WidgetLabel.cpp

http://github.com/clintbellanger/flare · C++ · 153 lines · 87 code · 27 blank · 39 comment · 33 complexity · ebbd1a3bc30cf0b90b2f280235d805fa 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 WidgetLabel
  15. *
  16. * A simple text display for menus
  17. */
  18. #include "WidgetLabel.h"
  19. #include "SharedResources.h"
  20. using namespace std;
  21. WidgetLabel::WidgetLabel() {
  22. text_buffer = NULL;
  23. text = "";
  24. color = FONT_WHITE;
  25. justify = JUSTIFY_LEFT;
  26. valign = VALIGN_TOP;
  27. bounds.x = bounds.y = 0;
  28. bounds.w = bounds.h = 0;
  29. }
  30. /**
  31. * Draw the buffered string surface to the screen
  32. */
  33. void WidgetLabel::render() {
  34. SDL_Rect dest;
  35. dest.x = bounds.x;
  36. dest.y = bounds.y;
  37. dest.w = bounds.w;
  38. dest.h = bounds.h;
  39. if (text_buffer != NULL) {
  40. SDL_BlitSurface(text_buffer, NULL, screen, &dest);
  41. }
  42. }
  43. /**
  44. * A shortcut function to set all attributes simultaneously.
  45. */
  46. void WidgetLabel::set(int _x, int _y, int _justify, int _valign, const string& _text, int _color) {
  47. bool changed = false;
  48. if (justify != _justify) {
  49. justify = _justify;
  50. changed = true;
  51. }
  52. if (valign != _valign) {
  53. valign = _valign;
  54. changed = true;
  55. }
  56. if (text != _text) {
  57. text = _text;
  58. changed = true;
  59. }
  60. if (color != _color) {
  61. color = _color;
  62. changed = true;
  63. }
  64. if (x_origin != _x) {
  65. x_origin = _x;
  66. changed = true;
  67. }
  68. if (y_origin != _y) {
  69. y_origin = _y;
  70. changed = true;
  71. }
  72. if (changed) {
  73. applyOffsets();
  74. refresh();
  75. }
  76. }
  77. /**
  78. * Apply horizontal justify and vertical alignment to label position
  79. */
  80. void WidgetLabel::applyOffsets() {
  81. bounds.w = font->calc_width(text);
  82. bounds.h = font->getFontHeight();
  83. // apply JUSTIFY
  84. if (justify == JUSTIFY_LEFT)
  85. bounds.x = x_origin;
  86. else if (justify == JUSTIFY_RIGHT)
  87. bounds.x = x_origin - bounds.w;
  88. else if (justify == JUSTIFY_CENTER)
  89. bounds.x = x_origin - bounds.w/2;
  90. // apply VALIGN
  91. if (valign == VALIGN_TOP) {
  92. bounds.y = y_origin;
  93. }
  94. else if (valign == VALIGN_BOTTOM) {
  95. bounds.y = y_origin - bounds.h;;
  96. }
  97. else if (valign == VALIGN_CENTER) {
  98. bounds.y = y_origin - bounds.h/2;
  99. }
  100. }
  101. /**
  102. * Update the label text only
  103. */
  104. void WidgetLabel::set(const string& _text) {
  105. if (text != _text) {
  106. this->text = _text;
  107. applyOffsets();
  108. refresh();
  109. }
  110. }
  111. /**
  112. * We buffer the rendered text instead of calculating it each frame
  113. * This function refreshes the buffer.
  114. */
  115. void WidgetLabel::refresh() {
  116. SDL_FreeSurface(text_buffer);
  117. text_buffer = createSurface(bounds.w, bounds.h);
  118. font->renderShadowed(text, 0, 0, JUSTIFY_LEFT, text_buffer, color);
  119. }
  120. WidgetLabel::~WidgetLabel() {
  121. SDL_FreeSurface(text_buffer);
  122. }