From f64c935ffeb39fae01e4a223b26a98ca03ff6b6a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 11 Oct 2013 00:30:31 +1100 Subject: [PATCH] Imported revamped GUI classes, moved map classes --- common/button.cpp | 81 ++++++++++++++++ common/button.hpp | 94 +++++++++++++++++++ common/menu_bar.cpp | 139 ++++++++++++++++++++++++++++ common/menu_bar.hpp | 91 ++++++++++++++++++ common/raster_font.cpp | 60 ++++++++++++ common/raster_font.hpp | 54 +++++++++++ {editor => common}/region.cpp | 0 {editor => common}/region.hpp | 0 {editor => common}/region_pager.cpp | 0 {editor => common}/region_pager.hpp | 0 {editor => common}/tile.cpp | 0 {editor => common}/tile.hpp | 0 {editor => common}/tile_sheet.cpp | 0 {editor => common}/tile_sheet.hpp | 0 14 files changed, 519 insertions(+) create mode 100644 common/button.cpp create mode 100644 common/button.hpp create mode 100644 common/menu_bar.cpp create mode 100644 common/menu_bar.hpp create mode 100644 common/raster_font.cpp create mode 100644 common/raster_font.hpp rename {editor => common}/region.cpp (100%) rename {editor => common}/region.hpp (100%) rename {editor => common}/region_pager.cpp (100%) rename {editor => common}/region_pager.hpp (100%) rename {editor => common}/tile.cpp (100%) rename {editor => common}/tile.hpp (100%) rename {editor => common}/tile_sheet.cpp (100%) rename {editor => common}/tile_sheet.hpp (100%) diff --git a/common/button.cpp b/common/button.cpp new file mode 100644 index 0000000..8d7eff9 --- /dev/null +++ b/common/button.cpp @@ -0,0 +1,81 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "button.hpp" + +#include + +Button::State Button::MouseMotion(SDL_MouseMotionEvent const& motion) { + return CalcState(motion.x, motion.y, motion.state & SDL_BUTTON_LMASK); +} + +Button::State Button::MouseButtonDown(SDL_MouseButtonEvent const& button) { + if (button.button == SDL_BUTTON_LEFT) { + return CalcState(button.x, button.y, true); + } + return state; +} + +Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& button) { + if (button.button == SDL_BUTTON_LEFT) { + return CalcState(button.x, button.y, false); + } + return state; +} + +void Button::DrawTo(SDL_Surface* const dest) { + if (!image || !font) { + throw(std::runtime_error("Surface not set for Button")); + } + image->SetClipY(state * image->GetClipH()); + image->DrawTo(dest, x, y); + font->DrawStringTo(text, dest, textX + x, textY + y); +} + +std::string Button::SetText(std::string t) { + if (!image || !font) { + throw(std::runtime_error("Surface not set for Button")); + } + //one line, cache the position + text = t; + textX = (image->GetClipW() / 2) - (font->GetCharW() * text.size() / 2); + textY = (image->GetClipH() / 2) - (font->GetCharH() / 2); + return text; +} + +Button::State Button::CalcState(Sint16 i, Sint16 j, bool leftPressed) { + if (!image || !font) { + throw(std::runtime_error("Surface not set for Button")); + } + //if out of bounds + if (i < x || i >= (x + image->GetClipW()) || + j < y || j >= (y + image->GetClipH()) + ) { + return state = State::NORMAL; + } + + if (leftPressed) { + return state = State::PRESSED; + } + else { + return state = State::HOVER; + } +} diff --git a/common/button.hpp b/common/button.hpp new file mode 100644 index 0000000..ae69ec1 --- /dev/null +++ b/common/button.hpp @@ -0,0 +1,94 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef BUTTON_HPP_ +#define BUTTON_HPP_ + +#include "image.hpp" +#include "raster_font.hpp" + +#include + +/* 3-phases, no toggle, centred text + * This class uses the size of the provided image as its bounds. Also, + * The provided image should be formatted correctly. + * + * The button's image should be divided into 3 sections virtucally, + * which act as the different button images. The clip width & height of the + * Image should be set manually, and the height should be 1/3 of the total + * graphical data. +*/ +class Button { +public: + enum State { + NORMAL = 0, HOVER = 1, PRESSED = 2 + }; + + Button() = default; + ~Button() = default; + + //handle input + State MouseMotion(SDL_MouseMotionEvent const&); + State MouseButtonDown(SDL_MouseButtonEvent const&); + State MouseButtonUp(SDL_MouseButtonEvent const&); + + //yet another draw function + void DrawTo(SDL_Surface* const); + + //accessors and mutators + Image* SetImage(Image* const ptr) { return image = ptr; } + Image* GetImage() { return image; } + RasterFont* SetFont(RasterFont* const ptr) { return font = ptr; } + RasterFont* GetFont() { return font; } + + Sint16 SetX(Sint16 i) { return x = i; } + Sint16 SetY(Sint16 i) { return y = i; } + Sint16 GetX() const { return x; } + Sint16 GetY() const { return y; } + + Sint16 SetTextX(Sint16 i) { return textX = i; } + Sint16 SetTextY(Sint16 i) { return textY = i; } + Sint16 GetTextX() const { return textX; } + Sint16 GetTextY() const { return textY; } + + State SetState(State s) { return state = s; } + State GetState() const { return state; } + + std::string SetText(std::string); + std::string GetText() const { return text; } + +private: + State CalcState(Sint16 x, Sint16 y, bool leftPressed); + + //point to the provided external objects + Image* image = nullptr; + RasterFont* font = nullptr; + + //positions + Sint16 x = 0, y = 0; + Sint16 textX = 0, textY = 0; + + // + State state = State::NORMAL; + std::string text; +}; + +#endif diff --git a/common/menu_bar.cpp b/common/menu_bar.cpp new file mode 100644 index 0000000..a6af861 --- /dev/null +++ b/common/menu_bar.cpp @@ -0,0 +1,139 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "menu_bar.hpp" + +#include +#include + +void MenuBar::DrawTo(SDL_Surface* const dest) { + for (auto& i : entries) { + i.DrawTo(dest); + } +} + +void MenuBar::MouseMotion(SDL_MouseMotionEvent const& motion) { + for (auto& i : entries) { + i.MouseMotion(motion); + } +} + +void MenuBar::MouseButtonDown(SDL_MouseButtonEvent const& button) { + for (auto& i : entries) { + i.MouseButtonDown(button); + } +} + +void MenuBar::MouseButtonUp(SDL_MouseButtonEvent const& button, int* entry, int* butt) { + *entry = *butt = -1; + int ret = -1; + for (auto& i : entries) { + ret = i.MouseButtonUp(button); + + if (ret != -1) { + *entry = (&i - entries.data()); + *butt = ret; + } + } +} + +void MenuBar::SetEntries(std::vector> info) { + if (!image || !font) { + throw(std::runtime_error("Surfaces not loaded into the menu bar")); + } + + entries.clear(); + for (int i = 0; i < info.size(); i++) { + //create the entry & the main button + entries.push_back(MenuBarEntry()); + entries[i].mainButton.SetImage(image); + entries[i].mainButton.SetFont(font); + entries[i].mainButton.SetText(info[i][0]); + entries[i].mainButton.SetX(i * image->GetClipW()); + entries[i].mainButton.SetY(0); + for (int j = 0; j < info[i].size()-1; j++) { + //create each drop button in this entry + entries[i].dropButtons.push_back(Button()); + entries[i].dropButtons[j].SetImage(image); + entries[i].dropButtons[j].SetFont(font); + entries[i].dropButtons[j].SetText(info[i][j+1]); + entries[i].dropButtons[j].SetX(i * image->GetClipW()); + entries[i].dropButtons[j].SetY((j+1) * image->GetClipH()); + } + } +} + +void MenuBar::MenuBarEntry::DrawTo(SDL_Surface* const dest) { + //only draw the dropButtons in the user has this menu open + mainButton.DrawTo(dest); + + if (!open) { + return; + } + + for (auto& i : dropButtons) { + i.DrawTo(dest); + } +} + +void MenuBar::MenuBarEntry::MouseMotion(SDL_MouseMotionEvent const& motion) { + //open the menu + bool o = mainButton.MouseMotion(motion) == Button::State::PRESSED; + + if (!(open |= o)) { + return; + } + + for (auto& i : dropButtons) { + //dragging down the menu + o |= i.MouseMotion(motion) == Button::State::PRESSED; + } + + open = o; +} + +void MenuBar::MenuBarEntry::MouseButtonDown(SDL_MouseButtonEvent const& button) { + //open the menu + if (!(open = mainButton.MouseButtonDown(button) == Button::State::PRESSED)) { + return; + } + + //update the others anyway + for (auto& i : dropButtons) { + i.MouseButtonDown(button); + } +} + +int MenuBar::MenuBarEntry::MouseButtonUp(SDL_MouseButtonEvent const& button) { + int ret = -1; + mainButton.MouseButtonUp(button); + + for (auto& i : dropButtons) { + //the user just released this button + if (i.GetState() != i.MouseButtonUp(button) && i.GetState() == Button::State::HOVER && open) { + //get this button's index + ret = (&i - dropButtons.data()); + } + } + + open = false; + return ret; +} diff --git a/common/menu_bar.hpp b/common/menu_bar.hpp new file mode 100644 index 0000000..81fbe4a --- /dev/null +++ b/common/menu_bar.hpp @@ -0,0 +1,91 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef MENUBAR_HPP_ +#define MENUBAR_HPP_ + +#include "image.hpp" +#include "raster_font.hpp" +#include "button.hpp" + +#include +#include + +/* I've redesigned this so that the contents of the menu bar can't change during run time. + * This is more restrictive but I'm focusing on getting this working first. + * The Image and Font pointers must be set before the text data is entered. + * + * This class needs a rewrite. +*/ + +class MenuBar { +public: + MenuBar() = default; + ~MenuBar() = default; + + //yet another draw function + void DrawTo(SDL_Surface* const dest); + + //user inputs + void MouseMotion(SDL_MouseMotionEvent const&); + void MouseButtonDown(SDL_MouseButtonEvent const&); + void MouseButtonUp(SDL_MouseButtonEvent const&, int* entry, int* button); + + //manage the entries & buttons + void SetEntries(std::vector> info); + void ClearEntries() { entries.clear(); } + + //Accessors and mutators + Image* SetImage(Image* const ptr) { return image = ptr; } + Image* GetImage() { return image; } + RasterFont* SetFont(RasterFont* const ptr) { return font = ptr; } + RasterFont* GetFont() { return font; } + +private: + class MenuBarEntry; + + std::vector entries; + + Image* image = nullptr; + RasterFont* font = nullptr; +}; + +class MenuBar::MenuBarEntry { +public: + MenuBarEntry() = default; + ~MenuBarEntry() = default; + + void DrawTo(SDL_Surface* const dest); + + void MouseMotion(SDL_MouseMotionEvent const&); + void MouseButtonDown(SDL_MouseButtonEvent const&); + int MouseButtonUp(SDL_MouseButtonEvent const&); + +private: + Button mainButton; + + std::vector