From d877d937968b7ce91a7ace5324faf712b2faf76e Mon Sep 17 00:00:00 2001 From: Justin Jacobs Date: Thu, 10 May 2012 18:46:43 -0400 Subject: [PATCH] Add WidgetComboBox class --- CMakeLists.txt | 1 + src/WidgetComboBox.cpp | 201 +++++++++++++++++++++++++++++++++++++++++ src/WidgetComboBox.h | 76 ++++++++++++++++ 3 files changed, 278 insertions(+) create mode 100644 src/WidgetComboBox.cpp create mode 100644 src/WidgetComboBox.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d9517e8..e4454a31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -120,6 +120,7 @@ Set (FLARE_SOURCES ./src/UtilsFileSystem.cpp ./src/UtilsParsing.cpp ./src/WidgetCheckBox.cpp + ./src/WidgetComboBox.cpp ./src/WidgetButton.cpp ./src/WidgetInput.cpp ./src/WidgetLabel.cpp diff --git a/src/WidgetComboBox.cpp b/src/WidgetComboBox.cpp new file mode 100644 index 00000000..d2ab9f15 --- /dev/null +++ b/src/WidgetComboBox.cpp @@ -0,0 +1,201 @@ +/* +Copyright © 2011-2012 Clint Bellanger + +This file is part of FLARE. + +FLARE is free software: you can redistribute it and/or modify it under the terms +of the GNU General Public License as published by the Free Software Foundation, +either version 3 of the License, or (at your option) any later version. + +FLARE is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +FLARE. If not, see http://www.gnu.org/licenses/ +*/ + +/** + * class WidgetComboBox + */ + +#include "WidgetComboBox.h" +#include "SharedResources.h" + +using namespace std; + +WidgetComboBox::WidgetComboBox(int amount, const std::string& _fileName) + : fileName(_fileName) { + + cmbAmount = amount+1; + values = new std::string[cmbAmount]; + vlabels = new WidgetLabel[cmbAmount]; + rows = new SDL_Rect[cmbAmount]; + + comboboxs = NULL; + click = NULL; + label = ""; + pos.x = pos.y = pos.w = pos.h = 0; + enabled = true; + pressed = false; + selected = 0; + + loadArt(); + + pos.w = (comboboxs->w / 2); + pos.h = (comboboxs->h / 4); //height of one ComboBox + +} + +void WidgetComboBox::loadArt() { + + // load ComboBox images + comboboxs = IMG_Load(fileName.c_str()); + + if(!comboboxs) { + fprintf(stderr, "Couldn't load image: %s\n", IMG_GetError()); + SDL_Quit(); + exit(1); // or abort ?? + } + + // optimize + SDL_Surface *cleanup = comboboxs; + comboboxs = SDL_DisplayFormatAlpha(comboboxs); + SDL_FreeSurface(cleanup); +} + +/** + * Sets and releases the "pressed" visual state of the ComboBox + * If press and release, activate (return true) + */ +bool WidgetComboBox::checkClick() { + + // disabled ComboBoxs can't be clicked; + if (!enabled) return false; + + // main ComboBox already in use, new click not allowed + if (inp->lock[MAIN1]) return false; + + // main click released, so the ComboBox state goes back to unpressed + if (pressed && !inp->lock[MAIN1]) { + pressed = false; + + for(int i=0;imouse)) { + // activate upon release + selected = i; + refresh(); + return true; + } + } + } + + pressed = false; + + // detect new click + if (inp->pressing[MAIN1]) { + if (isWithin(pos, inp->mouse)) { + + inp->lock[MAIN1] = true; + pressed = true; + + } + } + return false; + +} + +void WidgetComboBox::set(int index, std::string value) { + values[index] = value; +} + +int WidgetComboBox::getSelected() { + return selected; +} + +void WidgetComboBox::render() { + SDL_Rect src; + src.x = 0; + src.w = pos.w; + src.h = pos.h; + + // the "ComboBox" surface contains ComboBox variations. + // choose which variation to display. + if (!enabled) + src.y = COMBOBOX_GFX_DISABLED * pos.h; + else if (pressed) + src.y = COMBOBOX_GFX_PRESSED * pos.h; + else if (isWithin(pos, inp->mouse)) + src.y = COMBOBOX_GFX_HOVER * pos.h; + else + src.y = COMBOBOX_GFX_NORMAL * pos.h; + + SDL_BlitSurface(comboboxs, &src, screen, &pos); + + wlabel.render(); + + if(pressed) + { + src.x = pos.w; + for(int i=0;imouse)) + font_color = FONT_WHITE; + else + font_color = FONT_GRAY; + + int font_x = rows[i].x + (rows[i].w/2); + int font_y = rows[i].y + (rows[i].h/2); + + vlabels[i].set(font_x, font_y, JUSTIFY_CENTER, VALIGN_CENTER, values[i], font_color); + } + } +} + +WidgetComboBox::~WidgetComboBox() { + SDL_FreeSurface(comboboxs); + delete[] values; + delete[] vlabels; + delete[] rows; +} + diff --git a/src/WidgetComboBox.h b/src/WidgetComboBox.h new file mode 100644 index 00000000..ed7fc222 --- /dev/null +++ b/src/WidgetComboBox.h @@ -0,0 +1,76 @@ +/* +Copyright © 2011-2012 Clint Bellanger + +This file is part of FLARE. + +FLARE is free software: you can redistribute it and/or modify it under the terms +of the GNU General Public License as published by the Free Software Foundation, +either version 3 of the License, or (at your option) any later version. + +FLARE is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +FLARE. If not, see http://www.gnu.org/licenses/ +*/ + +/** + * class WidgetComboBox + */ + +#ifndef WIDGET_ComboBox_H +#define WIDGET_ComboBox_H + +#include "Utils.h" +#include "FontEngine.h" +#include "InputState.h" +#include "Widget.h" +#include "WidgetLabel.h" + +#include +#include +#include + +#include + + +const int COMBOBOX_GFX_NORMAL = 0; +const int COMBOBOX_GFX_PRESSED = 1; +const int COMBOBOX_GFX_HOVER = 2; +const int COMBOBOX_GFX_DISABLED = 3; + +class WidgetComboBox : public Widget { +private: + + std::string fileName; // the path to the ComboBoxs background image + + SDL_Surface *comboboxs; + Mix_Chunk *click; + + WidgetLabel wlabel; + + int cmbAmount; + std::string *values; + WidgetLabel *vlabels; + SDL_Rect *rows; + +public: + WidgetComboBox(int amount, const std::string& _fileName); + ~WidgetComboBox(); + + void loadArt(); + bool checkClick(); + void set(int index, std::string value); + int getSelected(); + void render(); + void refresh(); + + std::string label; + SDL_Rect pos; + bool enabled; + bool pressed; + int selected; +}; + +#endif