From b1e75ebdc87675a1eabf55b5d4932b587246cfe9 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 24 Oct 2009 19:47:48 +0100 Subject: [PATCH] More work on the new GUI --- include/gui/IFont.hpp | 3 +- include/gui2/Button.hpp | 2 +- include/gui2/Colour.hpp | 32 +++++++++++ include/gui2/ContainerWidget.hpp | 5 ++ include/gui2/Label.hpp | 2 +- include/gui2/RenderBuffer.hpp | 43 +++++++++++++++ include/gui2/RenderContext.hpp | 57 ++++++++++++++++++++ include/gui2/Theme.hpp | 46 ++++++++++++++++ include/gui2/Widget.hpp | 9 +++- include/gui2/Window.hpp | 2 +- layouts/demo.xml | 6 ++- schemas/layout.xsd | 4 ++ src/gui2/Button.cpp | 8 ++- src/gui2/ContainerWidget.cpp | 13 +++++ src/gui2/Label.cpp | 2 +- src/gui2/Layout.cpp | 52 +++++++++++------- src/gui2/RenderContext.cpp | 91 ++++++++++++++++++++++++++++++++ src/gui2/Theme.cpp | 35 ++++++++++++ src/gui2/Widget.cpp | 8 ++- src/gui2/Window.cpp | 14 ++++- 20 files changed, 404 insertions(+), 30 deletions(-) create mode 100644 include/gui2/Colour.hpp create mode 100644 include/gui2/RenderBuffer.hpp create mode 100644 include/gui2/RenderContext.hpp create mode 100644 include/gui2/Theme.hpp create mode 100644 src/gui2/RenderContext.cpp create mode 100644 src/gui2/Theme.cpp diff --git a/include/gui/IFont.hpp b/include/gui/IFont.hpp index c8ba454..fbf3dbc 100644 --- a/include/gui/IFont.hpp +++ b/include/gui/IFont.hpp @@ -37,7 +37,8 @@ namespace gui { typedef std::tr1::shared_ptr IFontPtr; - IFontPtr loadFont(const std::string& aFile, int aHeight, bool shadow=true); + IFontPtr loadFont(const std::string& aFile, int aHeight, + bool shadow=true); } #endif diff --git a/include/gui2/Button.hpp b/include/gui2/Button.hpp index 9207372..0d774e7 100644 --- a/include/gui2/Button.hpp +++ b/include/gui2/Button.hpp @@ -34,7 +34,7 @@ namespace gui { const string& label() const { return label_; } void label(const string& t) { label_ = t; } - void render() const; + void render(RenderContext& rc) const; private: string label_; }; diff --git a/include/gui2/Colour.hpp b/include/gui2/Colour.hpp new file mode 100644 index 0000000..6d427cf --- /dev/null +++ b/include/gui2/Colour.hpp @@ -0,0 +1,32 @@ +// +// Copyright (C) 2006-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_GUI2_COLOUR_HPP +#define INC_GUI2_COLOUR_HPP + +#include "Platform.hpp" + +namespace gui { + typedef tuple Colour; + + inline Colour make_colour(float r, float g, float b, float a=1.0f) + { + return make_tuple(r, g, b, a); + } +} + +#endif diff --git a/include/gui2/ContainerWidget.hpp b/include/gui2/ContainerWidget.hpp index e530535..0db91ce 100644 --- a/include/gui2/ContainerWidget.hpp +++ b/include/gui2/ContainerWidget.hpp @@ -29,6 +29,10 @@ namespace gui { class ContainerWidget : public Widget { public: ContainerWidget(const AttributeSet& attrs); + + virtual void render(RenderContext& rc) const; + + void add_child(Widget* w); protected: virtual void child_added(Widget* w) {}; @@ -42,6 +46,7 @@ namespace gui { { return children.begin(); } ChildList::const_iterator const_end() const { return children.end(); } + private: ChildList children; }; diff --git a/include/gui2/Label.hpp b/include/gui2/Label.hpp index c70bce7..5c5f1de 100644 --- a/include/gui2/Label.hpp +++ b/include/gui2/Label.hpp @@ -34,7 +34,7 @@ namespace gui { const string& text() const { return text_; } void text(const string& t) { text_ = t; } - void render() const; + void render(RenderContext& rc) const; private: string text_; }; diff --git a/include/gui2/RenderBuffer.hpp b/include/gui2/RenderBuffer.hpp new file mode 100644 index 0000000..e05cd51 --- /dev/null +++ b/include/gui2/RenderBuffer.hpp @@ -0,0 +1,43 @@ +// +// 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_GUI_RENDERBUFFER_HPP +#define INC_GUI_RENDERBUFFER_HPP + +// Internal header: do not include this file directly + +#include "Platform.hpp" + +#include + +namespace gui { + + class Window : public ContainerWidget { + public: + Window(const AttributeSet& attrs); + + const string& title() const { return title_; } + void title(const string& t) { title_ = t; } + + void render() const; + private: + string title_; + }; + +} + +#endif diff --git a/include/gui2/RenderContext.hpp b/include/gui2/RenderContext.hpp new file mode 100644 index 0000000..47415c6 --- /dev/null +++ b/include/gui2/RenderContext.hpp @@ -0,0 +1,57 @@ +// +// 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_GUI_RENDERCONTEXT_HPP +#define INC_GUI_RENDERCONTEXT_HPP + +// Internal header: do not include this file directly + +#include "Platform.hpp" +#include "gui2/Colour.hpp" +#include "gui2/Theme.hpp" + +#include +#include + +namespace gui { + + class RenderContext { + public: + RenderContext(); + ~RenderContext(); + + void push_origin(int x, int y); + void pop_origin(); + + void rectangle(int x, int y, int w, int h, Colour c); + void border(int x, int y, int w, int h, Colour c); + + void print(IFontPtr font, int x, int y, const string& s); + + const Theme& theme() const { return theme_; } + private: + void offset(int& x, int& y) const; + + Theme theme_; + int origin_x, origin_y; + + stack > origin_stack; + }; + +} + +#endif diff --git a/include/gui2/Theme.hpp b/include/gui2/Theme.hpp new file mode 100644 index 0000000..bdfe1b1 --- /dev/null +++ b/include/gui2/Theme.hpp @@ -0,0 +1,46 @@ +// +// 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_GUI_THEME_HPP +#define INC_GUI_THEME_HPP + +// Internal header: do not include this file directly + +#include "Platform.hpp" +#include "gui2/Colour.hpp" +#include "gui/IFont.hpp" + +namespace gui { + + class Theme { + public: + Theme(); + + // Colours + Colour background() const; + Colour border() const; + + // Fonts + IFontPtr normal_font() const { return normal_font_; } + + private: + IFontPtr normal_font_; + }; + +} + +#endif diff --git a/include/gui2/Widget.hpp b/include/gui2/Widget.hpp index f68b1fd..cce1161 100644 --- a/include/gui2/Widget.hpp +++ b/include/gui2/Widget.hpp @@ -22,6 +22,7 @@ #include "Platform.hpp" #include "gui2/ILayout.hpp" +#include "gui2/RenderContext.hpp" #include "IXMLParser.hpp" #include @@ -36,11 +37,15 @@ namespace gui { Widget(const AttributeSet& attrs); const string& name() const { return name_; } + int x() const { return x_; } + int y() const { return y_; } + int width() const { return width_; } + int height() const { return height_; } boost::any get_property(const string& key) const; void set_property(const string& key, boost::any value); - virtual void render() const = 0; + virtual void render(RenderContext& rc) const = 0; protected: template @@ -62,6 +67,8 @@ namespace gui { static string unique_name(); string name_; + int x_, y_, width_, height_; + const AttributeSet& tmp_attrs; // Do not use after initialisation typedef map PropertyMap; diff --git a/include/gui2/Window.hpp b/include/gui2/Window.hpp index f983ed9..a9f5d50 100644 --- a/include/gui2/Window.hpp +++ b/include/gui2/Window.hpp @@ -35,7 +35,7 @@ namespace gui { const string& title() const { return title_; } void title(const string& t) { title_ = t; } - void render() const; + void render(RenderContext& rc) const; private: string title_; }; diff --git a/layouts/demo.xml b/layouts/demo.xml index b7e2451..5d45643 100644 --- a/layouts/demo.xml +++ b/layouts/demo.xml @@ -1,7 +1,9 @@ - -