PageRenderTime 34ms CodeModel.GetById 2ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/WidgetButton.cpp

http://github.com/clintbellanger/flare
C++ | 140 lines | 71 code | 33 blank | 36 comment | 16 complexity | 6f7b3e5c9d07d73fbab7ea0f8d206fda MD5 | raw file
  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 WidgetButton
 20 */
 21
 22#include "WidgetButton.h"
 23#include "SharedResources.h"
 24
 25using namespace std;
 26
 27WidgetButton::WidgetButton(const std::string& _fileName)
 28	: fileName(_fileName) {
 29
 30	buttons = NULL;
 31	click = NULL;
 32	label = "";
 33	pos.x = pos.y = pos.w = pos.y = 0;
 34	enabled = true;
 35	pressed = false;
 36	
 37	loadArt();
 38
 39	pos.w = buttons->w;
 40	pos.h = (buttons->h / 4); //height of one button
 41
 42}
 43
 44void WidgetButton::loadArt() {
 45
 46	// load button images
 47	buttons = IMG_Load(fileName.c_str());
 48
 49	if(!buttons) {
 50		fprintf(stderr, "Couldn't load image: %s\n", IMG_GetError());
 51		SDL_Quit();
 52	}
 53	
 54	// optimize
 55	SDL_Surface *cleanup = buttons;
 56	buttons = SDL_DisplayFormatAlpha(buttons);
 57	SDL_FreeSurface(cleanup);
 58}
 59
 60/**
 61 * Sets and releases the "pressed" visual state of the button
 62 * If press and release, activate (return true)
 63 */
 64bool WidgetButton::checkClick() {
 65
 66	// disabled buttons can't be clicked;
 67	if (!enabled) return false;
 68
 69	// main button already in use, new click not allowed
 70	if (inp->lock[MAIN1]) return false;
 71
 72	// main click released, so the button state goes back to unpressed
 73	if (pressed && !inp->lock[MAIN1]) {
 74		pressed = false;
 75		
 76		if (isWithin(pos, inp->mouse)) {
 77		
 78			// activate upon release
 79			return true;
 80		}
 81	}
 82
 83	pressed = false;
 84	
 85	// detect new click
 86	if (inp->pressing[MAIN1]) {
 87		if (isWithin(pos, inp->mouse)) {
 88		
 89			inp->lock[MAIN1] = true;
 90			pressed = true;
 91
 92		}
 93	}
 94	return false;
 95	
 96}
 97
 98void WidgetButton::render() {
 99	SDL_Rect src;
100	src.x = 0;
101	src.w = pos.w;
102	src.h = pos.h;
103	
104	// the "button" surface contains button variations.
105	// choose which variation to display.
106	if (!enabled)
107		src.y = BUTTON_GFX_DISABLED * pos.h;
108	else if (pressed)
109		src.y = BUTTON_GFX_PRESSED * pos.h;
110	else if (isWithin(pos, inp->mouse))
111		src.y = BUTTON_GFX_HOVER * pos.h;
112	else
113		src.y = BUTTON_GFX_NORMAL * pos.h;
114	
115	SDL_BlitSurface(buttons, &src, screen, &pos);
116
117	wlabel.render();
118}
119
120/**
121 * Create the text buffer
122 */
123void WidgetButton::refresh() {
124	if (label != "") {
125	
126		// render text
127		int font_color = FONT_WHITE;
128		if (!enabled) font_color = FONT_GRAY;
129
130		int font_x = pos.x + (pos.w/2);
131		int font_y = pos.y + (pos.h/2);
132
133		wlabel.set(font_x, font_y, JUSTIFY_CENTER, VALIGN_CENTER, label, font_color);
134	}
135}
136
137WidgetButton::~WidgetButton() {
138	SDL_FreeSurface(buttons);
139}
140