From dd5589b0c469dee54f94bd965f01f984a91858ca Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 5 Dec 2009 12:58:49 +0000 Subject: [PATCH] Add new ToggleBar and ToggleButton widgets --- include/gui/Button.hpp | 2 -- include/gui/RenderContext.hpp | 4 +-- include/gui/ToggleBar.hpp | 41 ++++++++++++++++++++++++++++ include/gui/ToggleButton.hpp | 35 ++++++++++++++++++++++++ include/gui/Widget.hpp | 6 ++++- layouts/editor.xml | 14 ++++++++++ schemas/layout.xsd | 27 +++++++++++++++++++ src/Editor.cpp | 12 ++++++--- src/Main.cpp | 3 ++- src/gui/Layout.cpp | 6 +++++ src/gui/RenderContext.cpp | 27 +++++++++++++------ src/gui/ToggleBar.cpp | 51 +++++++++++++++++++++++++++++++++++ src/gui/ToggleButton.cpp | 32 ++++++++++++++++++++++ src/gui/Widget.cpp | 6 +++++ src/gui/Window.cpp | 4 +-- 15 files changed, 251 insertions(+), 19 deletions(-) create mode 100644 include/gui/ToggleBar.hpp create mode 100644 include/gui/ToggleButton.hpp create mode 100644 layouts/editor.xml create mode 100644 src/gui/ToggleBar.cpp create mode 100644 src/gui/ToggleButton.cpp diff --git a/include/gui/Button.hpp b/include/gui/Button.hpp index d14587e..4c9cdc9 100644 --- a/include/gui/Button.hpp +++ b/include/gui/Button.hpp @@ -18,8 +18,6 @@ #ifndef INC_GUI_BUTTON_HPP #define INC_GUI_BUTTON_HPP -// Internal header: do not include this file directly - #include "Platform.hpp" #include "gui/Widget.hpp" diff --git a/include/gui/RenderContext.hpp b/include/gui/RenderContext.hpp index 65ca72b..3fce9d7 100644 --- a/include/gui/RenderContext.hpp +++ b/include/gui/RenderContext.hpp @@ -36,8 +36,8 @@ namespace gui { RenderContext(const Theme& theme); ~RenderContext(); - void push_origin(const Widget* w); - void pop_origin(); + void pushOrigin(const Widget* w); + void popOrigin(); void scissor(Widget* w); diff --git a/include/gui/ToggleBar.hpp b/include/gui/ToggleBar.hpp new file mode 100644 index 0000000..ee27ee0 --- /dev/null +++ b/include/gui/ToggleBar.hpp @@ -0,0 +1,41 @@ +// +// Copyright (C) 2009 Nick Gasson +// +// This program 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. +// +// This program 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 this program. If not, see . +// + +#ifndef INC_TOGGLEBAR_HPP +#define INC_TOGGLEBAR_HPP + +#include "Platform.hpp" +#include "gui/ContainerWidget.hpp" + +namespace gui { + + // Like a toolbar but one item is always shown selected + class ToggleBar : public ContainerWidget { + public: + ToggleBar(const AttributeSet& attrs); + + void render(RenderContext& rc) const; + private: + void childAdded(Widget* w); + + int nextX; + int buttonWidth, buttonHeight; + }; + +} + +#endif diff --git a/include/gui/ToggleButton.hpp b/include/gui/ToggleButton.hpp new file mode 100644 index 0000000..0772dc2 --- /dev/null +++ b/include/gui/ToggleButton.hpp @@ -0,0 +1,35 @@ +// +// Copyright (C) 2009 Nick Gasson +// +// This program 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. +// +// This program 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 this program. If not, see . +// + +#ifndef INC_TOGGLE_BUTTON_HPP +#define INC_TOGGLE_BUTTON_HPP + +#include "Platform.hpp" +#include "gui/Widget.hpp" + +namespace gui { + + // A special sort of button that only appears in toggle bars + class ToggleButton : public Widget { + public: + ToggleButton(const AttributeSet& attrs); + + void render(RenderContext& rc) const; + }; +} + +#endif diff --git a/include/gui/Widget.hpp b/include/gui/Widget.hpp index 0126285..86a981d 100644 --- a/include/gui/Widget.hpp +++ b/include/gui/Widget.hpp @@ -39,7 +39,9 @@ namespace gui { int width() const { return width_; } int height() const { return height_; } bool visible() const { return visible_; } - + + void x(int x) { x_ = x; } + void y(int y) { y_ = y; } void width(int w) { width_ = w; } void height(int h) { height_ = h; } void visible(bool v) { visible_ = v; } @@ -56,6 +58,8 @@ namespace gui { virtual void adjustForTheme(const Theme& theme) {} virtual void handleClick(int x, int y); + + void dumpLocation() const; protected: void raise(Signal sig); diff --git a/layouts/editor.xml b/layouts/editor.xml new file mode 100644 index 0000000..2d33e5c --- /dev/null +++ b/layouts/editor.xml @@ -0,0 +1,14 @@ + + + + + + diff --git a/schemas/layout.xsd b/schemas/layout.xsd index 95cd2ae..57d56e3 100644 --- a/schemas/layout.xsd +++ b/schemas/layout.xsd @@ -27,6 +27,7 @@ + @@ -70,6 +71,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Editor.cpp b/src/Editor.cpp index 7de7584..3cb8ce6 100644 --- a/src/Editor.cpp +++ b/src/Editor.cpp @@ -24,6 +24,7 @@ #include "ModelViewer.hpp" #include "IBuilding.hpp" #include "NewMapDialog.hpp" +#include "gui/ILayout.hpp" #include #include @@ -58,7 +59,7 @@ public: // Different tools the user can be using enum Tool { TRACK_TOOL, RAISE_TOOL, LOWER_TOOL, DELETE_TOOL, - LEVEL_TOOL, START_TOOL, STATION_TOOL, BUILDING_TOOL + LEVEL_TOOL, START_TOOL, STATION_TOOL, BUILDING_TOOL, }; void setTool(Tool aTool) { myTool = aTool; } @@ -87,6 +88,9 @@ private: // Variables for dragging track segments Point dragBegin, dragEnd; bool amDragging; + + // GUI elements + gui::ILayoutPtr layout; }; // The FLTK toolbox @@ -248,6 +252,8 @@ Editor::Editor(IMapPtr aMap) theEditor = this; + layout = gui::makeLayout("layouts/editor.xml"); + if (map) { map->setGrid(true); @@ -305,7 +311,7 @@ void Editor::display(IGraphicsPtr aContext) const // Render the overlay void Editor::overlay() const { - + layout->render(); } // Prepare the next frame @@ -674,7 +680,7 @@ void Editor::onKeyUp(SDLKey aKey) } void Editor::onKeyDown(SDLKey aKey) -{ +{ switch (aKey) { case SDLK_g: // Toggle grid diff --git a/src/Main.cpp b/src/Main.cpp index da1071f..6044b77 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -68,7 +68,8 @@ int main(int argc, char** argv) IScreenPtr screen; if (cmd == "edit") { - ::window = makeFLTKWindow("Train Game Editor", addEditorGUI); + // ::window = makeFLTKWindow("Train Game Editor", addEditorGUI); + ::window = makeSDLWindow(); if (resourceExists(mapFile, "maps")) screen = makeEditorScreen(loadMap(mapFile)); else { diff --git a/src/gui/Layout.cpp b/src/gui/Layout.cpp index 37d67c1..37acec9 100644 --- a/src/gui/Layout.cpp +++ b/src/gui/Layout.cpp @@ -25,6 +25,8 @@ #include "gui/Button.hpp" #include "gui/Label.hpp" #include "gui/ThrottleMeter.hpp" +#include "gui/ToggleBar.hpp" +#include "gui/ToggleButton.hpp" #include #include @@ -120,6 +122,10 @@ void Layout::startElement(const string& localName, w = new Label(attrs); else if (localName == "throttleMeter") w = new ThrottleMeter(attrs); + else if (localName == "toggleBar") + w = new ToggleBar(attrs); + else if (localName == "toggleButton") + w = new ToggleButton(attrs); else throw runtime_error("Unexpected " + localName); diff --git a/src/gui/RenderContext.cpp b/src/gui/RenderContext.cpp index 4d084a1..edd6cc8 100644 --- a/src/gui/RenderContext.cpp +++ b/src/gui/RenderContext.cpp @@ -42,17 +42,17 @@ RenderContext::~RenderContext() glScissor(0, 0, wnd->width(), wnd->height()); } -void RenderContext::push_origin(const Widget* w) +void RenderContext::pushOrigin(const Widget* w) { - origin_x = w->x(); - origin_y = w->y(); + origin_x += w->x(); + origin_y += w->y(); origin_stack.push(w); } -void RenderContext::pop_origin() +void RenderContext::popOrigin() { - origin_x = origin_stack.top()->x(); - origin_y = origin_stack.top()->y(); + origin_x -= origin_stack.top()->x(); + origin_y -= origin_stack.top()->y(); origin_stack.pop(); } @@ -73,6 +73,8 @@ void RenderContext::rectangle(int x, int y, int w, int h, Colour c) glVertex2i(x + w, y + h); glVertex2i(x, y + h); glEnd(); + + assert(glGetError() == GL_NO_ERROR); } void RenderContext::border(int x, int y, int w, int h, Colour c) @@ -116,6 +118,15 @@ void RenderContext::scissor(Widget* w) int width = min(w->width() + 1, max_w); int height = min(w->height(), max_h); - - glScissor(x, y, width, height); + + if (x < 0 || y < 0 || width <= 0 || height <= 0) { + static bool haveWarned = false; + + if (!haveWarned) { + warn() << "Widget " << w->name() << " is out of bounds"; + haveWarned = true; + } + } + else + glScissor(x, y, width, height); } diff --git a/src/gui/ToggleBar.cpp b/src/gui/ToggleBar.cpp new file mode 100644 index 0000000..750b744 --- /dev/null +++ b/src/gui/ToggleBar.cpp @@ -0,0 +1,51 @@ +// +// Copyright (C) 2009 Nick Gasson +// +// This program 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. +// +// This program 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 this program. If not, see . +// + +#include "gui/ToggleBar.hpp" +#include "ILogger.hpp" + +using namespace gui; + +ToggleBar::ToggleBar(const AttributeSet& attrs) + : ContainerWidget(attrs), + nextX(0), + buttonWidth(32), buttonHeight(32) +{ + width(0); + height(buttonHeight); +} + +void ToggleBar::render(RenderContext& rc) const +{ + rc.pushOrigin(this); + ContainerWidget::render(rc); + rc.popOrigin(); +} + +void ToggleBar::childAdded(Widget* w) +{ + debug() << "Added " << w->name() << " to toggle bar"; + + w->x(nextX); + w->y(0); + w->width(buttonWidth); + w->height(buttonHeight); + + nextX += buttonWidth; + + width(width() + buttonWidth); +} diff --git a/src/gui/ToggleButton.cpp b/src/gui/ToggleButton.cpp new file mode 100644 index 0000000..7094760 --- /dev/null +++ b/src/gui/ToggleButton.cpp @@ -0,0 +1,32 @@ +// +// Copyright (C) 2009 Nick Gasson +// +// This program 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. +// +// This program 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 this program. If not, see . +// + +#include "gui/ToggleButton.hpp" +#include "ILogger.hpp" + +using namespace gui; + +ToggleButton::ToggleButton(const AttributeSet& attrs) + : Widget(attrs) +{ + +} + +void ToggleButton::render(RenderContext& rc) const +{ + rc.rectangle(x(), y(), width(), height(), colour::WHITE); +} diff --git a/src/gui/Widget.cpp b/src/gui/Widget.cpp index 72fb173..74c7a2d 100644 --- a/src/gui/Widget.cpp +++ b/src/gui/Widget.cpp @@ -57,3 +57,9 @@ void Widget::handleClick(int x, int y) raise(SIG_CLICK); } +void Widget::dumpLocation() const +{ + debug() << name() << ": x=" << x() + << " y=" << y() << " width=" << width() + << " height=" << height(); +} diff --git a/src/gui/Window.cpp b/src/gui/Window.cpp index 67e93bf..e7cae7e 100644 --- a/src/gui/Window.cpp +++ b/src/gui/Window.cpp @@ -34,10 +34,10 @@ void Window::render(RenderContext& rc) const rc.border(x(), y(), width(), height(), rc.theme().border()); - rc.push_origin(this); + rc.pushOrigin(this); ContainerWidget::render(rc); - rc.pop_origin(); + rc.popOrigin(); } -- 2.39.2