/src/WidgetLabel.cpp
C++ | 153 lines | 87 code | 27 blank | 39 comment | 33 complexity | ebbd1a3bc30cf0b90b2f280235d805fa MD5 | raw file
Possible License(s): GPL-3.0
1/* 2Copyright 2011 Clint Bellanger 3 4This file is part of FLARE. 5 6FLARE is free software: you can redistribute it and/or modify it under the terms 7of the GNU General Public License as published by the Free Software Foundation, 8either version 3 of the License, or (at your option) any later version. 9 10FLARE is distributed in the hope that it will be useful, but WITHOUT ANY 11WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12PARTICULAR PURPOSE. See the GNU General Public License for more details. 13 14You should have received a copy of the GNU General Public License along with 15FLARE. If not, see http://www.gnu.org/licenses/ 16*/ 17 18/** 19 * class WidgetLabel 20 * 21 * A simple text display for menus 22 */ 23 24#include "WidgetLabel.h" 25#include "SharedResources.h" 26 27using namespace std; 28 29 30WidgetLabel::WidgetLabel() { 31 32 text_buffer = NULL; 33 text = ""; 34 color = FONT_WHITE; 35 justify = JUSTIFY_LEFT; 36 valign = VALIGN_TOP; 37 38 bounds.x = bounds.y = 0; 39 bounds.w = bounds.h = 0; 40 41} 42 43/** 44 * Draw the buffered string surface to the screen 45 */ 46void WidgetLabel::render() { 47 48 SDL_Rect dest; 49 dest.x = bounds.x; 50 dest.y = bounds.y; 51 dest.w = bounds.w; 52 dest.h = bounds.h; 53 54 if (text_buffer != NULL) { 55 SDL_BlitSurface(text_buffer, NULL, screen, &dest); 56 } 57} 58 59 60/** 61 * A shortcut function to set all attributes simultaneously. 62 */ 63void WidgetLabel::set(int _x, int _y, int _justify, int _valign, const string& _text, int _color) { 64 65 bool changed = false; 66 67 if (justify != _justify) { 68 justify = _justify; 69 changed = true; 70 } 71 if (valign != _valign) { 72 valign = _valign; 73 changed = true; 74 } 75 if (text != _text) { 76 text = _text; 77 changed = true; 78 } 79 if (color != _color) { 80 color = _color; 81 changed = true; 82 } 83 if (x_origin != _x) { 84 x_origin = _x; 85 changed = true; 86 } 87 if (y_origin != _y) { 88 y_origin = _y; 89 changed = true; 90 } 91 92 if (changed) { 93 applyOffsets(); 94 refresh(); 95 } 96} 97 98/** 99 * Apply horizontal justify and vertical alignment to label position 100 */ 101void WidgetLabel::applyOffsets() { 102 103 bounds.w = font->calc_width(text); 104 bounds.h = font->getFontHeight(); 105 106 // apply JUSTIFY 107 if (justify == JUSTIFY_LEFT) 108 bounds.x = x_origin; 109 else if (justify == JUSTIFY_RIGHT) 110 bounds.x = x_origin - bounds.w; 111 else if (justify == JUSTIFY_CENTER) 112 bounds.x = x_origin - bounds.w/2; 113 114 // apply VALIGN 115 if (valign == VALIGN_TOP) { 116 bounds.y = y_origin; 117 } 118 else if (valign == VALIGN_BOTTOM) { 119 bounds.y = y_origin - bounds.h;; 120 } 121 else if (valign == VALIGN_CENTER) { 122 bounds.y = y_origin - bounds.h/2; 123 } 124 125} 126 127/** 128 * Update the label text only 129 */ 130void WidgetLabel::set(const string& _text) { 131 if (text != _text) { 132 this->text = _text; 133 applyOffsets(); 134 refresh(); 135 } 136} 137 138/** 139 * We buffer the rendered text instead of calculating it each frame 140 * This function refreshes the buffer. 141 */ 142void WidgetLabel::refresh() { 143 144 SDL_FreeSurface(text_buffer); 145 text_buffer = createSurface(bounds.w, bounds.h); 146 font->renderShadowed(text, 0, 0, JUSTIFY_LEFT, text_buffer, color); 147 148} 149 150 151WidgetLabel::~WidgetLabel() { 152 SDL_FreeSurface(text_buffer); 153}