From 4331f5c17901f46693dcb5c2df96276f6851be25 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Wed, 8 Oct 2008 18:28:21 +0000 Subject: libui updates, paint namespace, font and palette fixes, button sound --- src/render/Makefile.am | 4 +- src/render/primitives.cc | 19 ---------- src/render/primitives.h | 96 ----------------------------------------------- src/ui/Makefile.am | 8 ++-- src/ui/bitmap.cc | 8 ++-- src/ui/button.cc | 28 +++++++------- src/ui/font.cc | 57 ++++++++++++++++++++++++++++ src/ui/font.h | 51 +++++++++++++++++++++++++ src/ui/label.cc | 8 ++-- src/ui/paint.cc | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ src/ui/paint.h | 40 ++++++++++++++++++++ src/ui/ui.cc | 33 +++++++++++----- src/ui/ui.h | 6 ++- src/ui/widget.cc | 26 +++++++++---- src/ui/widget.h | 7 ++++ src/ui/window.cc | 7 ++-- 16 files changed, 330 insertions(+), 166 deletions(-) delete mode 100644 src/render/primitives.cc delete mode 100644 src/render/primitives.h create mode 100644 src/ui/font.cc create mode 100644 src/ui/font.h create mode 100644 src/ui/paint.cc create mode 100644 src/ui/paint.h (limited to 'src') diff --git a/src/render/Makefile.am b/src/render/Makefile.am index dd2f5f8..27ad8bb 100644 --- a/src/render/Makefile.am +++ b/src/render/Makefile.am @@ -10,6 +10,6 @@ endif librender_la_LDFLAGS = -avoid-version -no-undefined @GL_LIBS@ librender_la_LIBADD = $(top_builddir)/src/math/libmath.la librender_la_SOURCES = camera.cc draw.cc dust.cc gl.cc image.cc jpgfile.cc \ - pngfile.cc primitives.cc render.cc text.cc textures.cc tga.cc + pngfile.cc render.cc text.cc textures.cc tga.cc noinst_HEADERS = camera.h draw.h dust.h gl.h image.h render.h text.h textures.h \ - tga.h pngfile.h jpgfile.h primitives.h + tga.h pngfile.h jpgfile.h diff --git a/src/render/primitives.cc b/src/render/primitives.cc deleted file mode 100644 index b790c70..0000000 --- a/src/render/primitives.cc +++ /dev/null @@ -1,19 +0,0 @@ -/* - render/primitives.cc - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - - -#include "render/primitives.h" - -namespace render -{ - -namespace primitives -{ - -} - -} - diff --git a/src/render/primitives.h b/src/render/primitives.h deleted file mode 100644 index 679a76e..0000000 --- a/src/render/primitives.h +++ /dev/null @@ -1,96 +0,0 @@ -/* - render/primitives.h - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -#ifndef __INCLUDED_RENDER_PRIMITIVES_H__ -#define __INCLUDED_RENDER_PRIMITIVES_H__ - -#include "auxiliary/functions.h" -#include "math/vector2f.h" -#include "render/gl.h" -#include "render/text.h" -#include "render/textures.h" - -namespace render -{ - -/// drawing primitives for the user interface -namespace primitives -{ - -/// draw a border -inline void border(math::Vector2f const &location, math::Vector2f const &size) -{ - using namespace render::gl; - - begin(LineLoop); - vertex(location.x +1 , location.y); - vertex(location.x + size.x, location.y); - vertex(location.x + size.x, location.y + size.y -1); - vertex(location.x +1, location.y + size.y - 1); - end(); -} - -/// draw a rectangle -inline void rectangle(math::Vector2f const &location, math::Vector2f const &size) -{ - using namespace render::gl; - - begin(Quads); - vertex(location.x +1 , location.y); - vertex(location.x + size.x, location.y); - vertex(location.x + size.x, location.y + size.y -1); - vertex(location.x +1, location.y + size.y - 1); - end(); -} - -/// draw a rectangular bitmap -inline void bitmap(math::Vector2f const &location, math::Vector2f const &size, std::string const &texture) -{ - - using namespace render::gl; - - render::Textures::bind("bitmaps/" + texture); - gl::enable(GL_TEXTURE_2D); - - begin(Quads); - - glTexCoord2f(0.0f, 0.0f); - vertex(location.x +1 , location.y); - - glTexCoord2f(1.0f, 0.0f); - vertex(location.x + size.x, location.y); - - glTexCoord2f(1.0f, 1.0f); - vertex(location.x + size.x, location.y + size.y -1); - - glTexCoord2f(0.0f, 1.0f); - vertex(location.x +1, location.y + size.y - 1); - end(); - - gl::disable(GL_TEXTURE_2D); -} - -/// draw one line of centered text -inline void text_centered(math::Vector2f const &location, math::Vector2f const &size, std::string const &text) -{ - Text::setfont("gui", 14, 24); - gl::enable(GL_TEXTURE_2D); - - math::Vector2f v(location); - - v.x += (size.x - aux::text_strip(text).size() * Text::fontwidth()) /2.0f; - v.y += (size.y - Text::fontheight()) / 2.0f; - - Text::draw(v.x, v.y, text); - - gl::disable(GL_TEXTURE_2D); -} - -} - -} - -#endif // __INCLUDED_RENDER_PRIMITIVES_H__ diff --git a/src/ui/Makefile.am b/src/ui/Makefile.am index 730cdea..5d14589 100644 --- a/src/ui/Makefile.am +++ b/src/ui/Makefile.am @@ -7,9 +7,9 @@ else noinst_LTLIBRARIES = libui.la endif -noinst_HEADERS = bitmap.h button.h label.h menu.h palette.h ui.h widget.h \ - window.h +noinst_HEADERS = bitmap.h button.h font.h label.h menu.h paint.h \ + palette.h ui.h widget.h window.h -libui_la_SOURCES = bitmap.cc button.cc label.cc menu.cc palette.cc ui.cc \ - widget.cc window.cc +libui_la_SOURCES = bitmap.cc button.cc font.cc label.cc menu.cc paint.cc \ + palette.cc ui.cc widget.cc window.cc libui_la_LDFLAGS = -avoid-version -no-undefined diff --git a/src/ui/bitmap.cc b/src/ui/bitmap.cc index d848a52..c14ccff 100644 --- a/src/ui/bitmap.cc +++ b/src/ui/bitmap.cc @@ -4,9 +4,9 @@ the terms of the GNU General Public License version 2 */ -#include "ui/bitmap.h" #include "auxiliary/functions.h" -#include "render/primitives.h" +#include "ui/bitmap.h" +#include "ui/paint.h" #include "sys/sys.h" namespace ui @@ -46,8 +46,8 @@ void Bitmap::set_texture(const char *texture) void Bitmap::draw_background() { if (bitmap_texture.size()) { - render::gl::color(1.0f, 1.0f, 1.0f, 1.0f); - render::primitives::bitmap(global_location(), size(), bitmap_texture); + paint::color(1.0f, 1.0f, 1.0f, 1.0f); + paint::bitmap(global_location(), size(), bitmap_texture); } } diff --git a/src/ui/button.cc b/src/ui/button.cc index e06e883..d6384c9 100644 --- a/src/ui/button.cc +++ b/src/ui/button.cc @@ -4,8 +4,9 @@ the terms of the GNU General Public License version 2 */ +#include "audio/audio.h" #include "auxiliary/functions.h" -#include "render/primitives.h" +#include "ui/paint.h" #include "sys/sys.h" #include "ui/button.h" #include "core/commandbuffer.h" @@ -46,13 +47,11 @@ void Button::draw_border() if (!border()) return; - if (palette()) { - if (has_focus()) - render::gl::color(palette()->foreground()); - else - render::gl::color(palette()->border()); - } - render::primitives::border(global_location(), size()); + if (has_focus()) + paint::color(palette()->foreground()); + else + paint::color(palette()->border()); + paint::border(global_location(), size()); } void Button::draw_text() @@ -60,14 +59,12 @@ void Button::draw_text() if (!text().size()) return; - if (palette()) { - if (has_focus()) - render::gl::color(palette()->highlight()); - else - render::gl::color(palette()->foreground()); - } + if (has_focus()) + paint::color(palette()->highlight()); + else + paint::color(palette()->foreground()); - render::primitives::text_centered(global_location(), size(), text()); + paint::text_centered(global_location(), size(), text(), font()); } void Button::keypress(unsigned int key, unsigned int modifier) @@ -79,6 +76,7 @@ void Button::keyrelease(unsigned int key, unsigned int modifier) { if (key == 512 + SDL_BUTTON_LEFT) { core::cmd() << button_command << std::endl; + audio::play("ui/button"); } } diff --git a/src/ui/font.cc b/src/ui/font.cc new file mode 100644 index 0000000..26d4155 --- /dev/null +++ b/src/ui/font.cc @@ -0,0 +1,57 @@ +/* + ui/font.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "ui/font.h" + +namespace ui { + +Font::Font(const char *name, const float width, const float height) +{ + if (name) + font_name.assign(name); + font_size.assign(width, height); +} + +Font::~Font() +{} + +void Font::set_size(const float width, const float height) +{ + font_size.assign(width, height); +} + +void Font::set_size(math::Vector2f const &size) +{ + font_size.assign(size); +} + +void Font::set_width(float width) +{ + font_size.x = width; +} + +void Font::set_height(float height) +{ + font_size.y = height; +} + +void Font::set_name(const char *name) +{ + if (name) + font_name.assign(name); + else + font_name.clear(); +} + +void Font::set_name(std::string const & name) +{ + font_name.assign(name); +} + +} + + + diff --git a/src/ui/font.h b/src/ui/font.h new file mode 100644 index 0000000..a68dec8 --- /dev/null +++ b/src/ui/font.h @@ -0,0 +1,51 @@ +/* + ui/font.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_UI_FONT_H__ +#define __INCLUDED_UI_FONT_H__ + +#include +#include "math/vector2f.h" + +namespace ui { + +class Font +{ +public: + Font(const char *name, const float width, const float height); + ~Font(); + + inline std::string const & name() const { return font_name; } + + inline math::Vector2f const & size() const { return font_size; } + + inline float const width() const { return font_size.x; } + + inline float const height() const { return font_size.y; } + + void set_size(const float width, const float height); + + void set_size(math::Vector2f const &size); + + void set_width(float width); + + void set_height(float height); + + void set_name(const char *name); + + void set_name(std::string const & name); + +private: + + std::string font_name; + + math::Vector2f font_size; +}; + +} + +#endif // __INCLUDED_UI_FONT_H__ + diff --git a/src/ui/label.cc b/src/ui/label.cc index aee6a36..1b142c4 100644 --- a/src/ui/label.cc +++ b/src/ui/label.cc @@ -5,7 +5,7 @@ */ #include "math/vector2f.h" -#include "render/primitives.h" +#include "ui/paint.h" #include "ui/label.h" using math::Vector2f; @@ -53,10 +53,8 @@ void Label::draw_text() if (!label_text.size()) return; - if (palette()) - render::gl::color(palette()->foreground()); - - render::primitives::text_centered(global_location(), size(), label_text); + paint::color(palette()->foreground()); + paint::text_centered(global_location(), size(), text(), font()); } } diff --git a/src/ui/paint.cc b/src/ui/paint.cc new file mode 100644 index 0000000..935cf92 --- /dev/null +++ b/src/ui/paint.cc @@ -0,0 +1,98 @@ +/* + ui/paint.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "auxiliary/functions.h" +#include "math/vector2f.h" +#include "render/gl.h" +#include "render/text.h" +#include "render/textures.h" +#include "ui/paint.h" + +namespace ui { + +// contains the interface between the user interface and the render library +namespace paint { + +void color(float r, float g, float b, float a) +{ + render::gl::color(r, g, b, a); +} + +void color(math::Color const & color) +{ + render::gl::color(color); +} + +void border(math::Vector2f const &location, math::Vector2f const &size) +{ + using namespace render::gl; + + begin(LineLoop); + vertex(location.x +1 , location.y); + vertex(location.x + size.x, location.y); + vertex(location.x + size.x, location.y + size.y -1); + vertex(location.x +1, location.y + size.y - 1); + end(); +} + +void rectangle(math::Vector2f const &location, math::Vector2f const &size) +{ + using namespace render::gl; + + begin(Quads); + vertex(location.x +1 , location.y); + vertex(location.x + size.x, location.y); + vertex(location.x + size.x, location.y + size.y -1); + vertex(location.x +1, location.y + size.y - 1); + end(); +} + +void bitmap(math::Vector2f const &location, math::Vector2f const &size, std::string const &texture) +{ + + using namespace render::gl; + + render::Textures::bind("bitmaps/" + texture); + enable(GL_TEXTURE_2D); + + begin(Quads); + + glTexCoord2f(0.0f, 0.0f); + vertex(location.x +1 , location.y); + + glTexCoord2f(1.0f, 0.0f); + vertex(location.x + size.x, location.y); + + glTexCoord2f(1.0f, 1.0f); + vertex(location.x + size.x, location.y + size.y -1); + + glTexCoord2f(0.0f, 1.0f); + vertex(location.x +1, location.y + size.y - 1); + end(); + + disable(GL_TEXTURE_2D); +} + +void text_centered(math::Vector2f const &location, math::Vector2f const &size, std::string const &text, Font const *font) +{ + using namespace render::gl; + + render::Text::setfont(font->name().c_str(), font->width(), font->height()); + enable(GL_TEXTURE_2D); + + math::Vector2f v(location); + + v.x += (size.x - aux::text_strip(text).size() * font->width()) /2.0f; + v.y += (size.y - font->height()) / 2.0f; + + render::Text::draw(v.x, v.y, text); + + disable(GL_TEXTURE_2D); +} + +} + +} diff --git a/src/ui/paint.h b/src/ui/paint.h new file mode 100644 index 0000000..b75290d --- /dev/null +++ b/src/ui/paint.h @@ -0,0 +1,40 @@ +/* + ui/paint.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_UI_PAINT_H__ +#define __INCLUDED_UI_PAINT_H__ + +#include "ui/widget.h" + +namespace ui { + +/// low-level widget paint functions +namespace paint { + +/// set paint color +void color(float r=0.0f, float g=0.0f, float b=0.0f, float a=1.0f); + +/// set paint color +void color(math::Color const & color); + +/// draw a border +void border(math::Vector2f const &location, math::Vector2f const &size); + +/// draw a rectangle +void rectangle(math::Vector2f const &location, math::Vector2f const &size); + +/// draw a rectangular bitmap +void bitmap(math::Vector2f const &location, math::Vector2f const &size, std::string const &texture); + +/// draw one line of centered text +void text_centered(math::Vector2f const &location, math::Vector2f const &size, std::string const &text, Font const *font); + +} + +} + +#endif // __INCLUDED_UI_PAINT_H__ + diff --git a/src/ui/ui.cc b/src/ui/ui.cc index 94daf03..2b09938 100644 --- a/src/ui/ui.cc +++ b/src/ui/ui.cc @@ -168,14 +168,32 @@ void shutdown() UI::UI() : Window(0) { - set_palette(&ui_palette); set_label("root"); set_size(1024, 768); set_border(false); + // default palette + ui_palette = new Palette(); + set_palette(ui_palette); + + // default fonts + ui_font_small = new Font("gui", 12, 18); + ui_font_medium = new Font("gui", 14, 24); + ui_font_large = new Font("gui", 16, 30); + set_font(ui_font_small); + load(); } +UI::~UI() +{ + delete ui_palette; + + delete ui_font_small; + delete ui_font_medium; + delete ui_font_large; +} + void UI::load() { Windows::iterator it; @@ -237,16 +255,16 @@ void UI::load() } else if (ini.in_section("colors")) { if (ini.got_key_color("foreground", color)) { - ui_palette.set_foreground(color); + ui_palette->set_foreground(color); continue; } else if (ini.got_key_color("highlight", color)) { - ui_palette.set_highlight(color); + ui_palette->set_highlight(color); continue; } else if (ini.got_key_color("background", color)) { - ui_palette.set_background(color); + ui_palette->set_background(color); continue; } else if (ini.got_key_color("border", color)) { - ui_palette.set_border(color); + ui_palette->set_border(color); continue; } else { ini.unkown_key(); @@ -277,10 +295,6 @@ void UI::load() } } -UI::~UI() -{ -} - void UI::list() { size_t n = Widget::list(0); @@ -301,6 +315,7 @@ void UI::add_window(Window *window) { Window::add_window(window); window->hide(); + window->set_font(ui_font_medium); } void UI::remove_window(Window *window) diff --git a/src/ui/ui.h b/src/ui/ui.h index 5195cbd..b3b2259 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -7,6 +7,7 @@ #ifndef __INCLUDED_UI_H__ #define __INCLUDED_UI_H__ +#include "ui/font.h" #include "ui/palette.h" #include "ui/widget.h" #include "ui/window.h" @@ -59,7 +60,10 @@ protected: virtual void remove_window(Window *window); private: - Palette ui_palette; + Palette *ui_palette; + Font *ui_font_small; + Font *ui_font_medium; + Font *ui_font_large; Window *ui_active_window; Widget *ui_focus; diff --git a/src/ui/widget.cc b/src/ui/widget.cc index 11d7427..4ff577c 100644 --- a/src/ui/widget.cc +++ b/src/ui/widget.cc @@ -5,8 +5,8 @@ */ #include "auxiliary/functions.h" -#include "render/primitives.h" #include "sys/sys.h" +#include "ui/paint.h" #include "ui/ui.h" #include "ui/widget.h" @@ -17,6 +17,7 @@ Widget::Widget(Widget *parent) { widget_border = true; widget_background = false; widget_palette = 0; + widget_font = 0; widget_label.assign("widget"); if (!parent) { @@ -62,6 +63,14 @@ Palette const *Widget::palette() const { } } +Font const *Widget::font() const { + if (widget_font) { + return widget_font; + } else { + return parent()->font(); + } +} + void Widget::show() { widget_visible = true; @@ -99,6 +108,11 @@ void Widget::set_palette(Palette *palette) widget_palette = palette; } +void Widget::set_font(Font *font) +{ + widget_font = font; +} + void Widget::set_location(float const x, float const y) { widget_location.assign(x, y); } @@ -204,9 +218,8 @@ void Widget::draw_background() if (!widget_background) return; - if (palette()) - render::gl::color(palette()->background()); - render::primitives::rectangle(global_location(), size()); + paint::color(palette()->background()); + paint::rectangle(global_location(), size()); } void Widget::draw_border() @@ -214,9 +227,8 @@ void Widget::draw_border() if (!widget_border) return; - if (palette()) - render::gl::color(palette()->border()); - render::primitives::border(global_location(), size()); + paint::color(palette()->border()); + paint::border(global_location(), size()); } Widget *Widget::find_focus(math::Vector2f const & pos) diff --git a/src/ui/widget.h b/src/ui/widget.h index 26832c7..54c8d96 100644 --- a/src/ui/widget.h +++ b/src/ui/widget.h @@ -15,6 +15,7 @@ #include "auxiliary/functions.h" #include "math/color.h" #include "math/vector2f.h" +#include "ui/font.h" #include "ui/palette.h" #include "sys/sys.h" @@ -58,6 +59,8 @@ public: Palette const *palette() const; + Font const *font() const; + bool has_focus() const; @@ -84,6 +87,9 @@ public: /// set the widgets palette void set_palette(Palette *palette); + /// set the widgets font + void set_font(Font *font); + /// set the widgets label void set_label(std::string const &label); @@ -189,6 +195,7 @@ private: Children widget_children; Palette *widget_palette; + Font *widget_font; Widget *widget_parent; Children::iterator find_child(Widget *child); diff --git a/src/ui/window.cc b/src/ui/window.cc index 60fbf56..b18348b 100644 --- a/src/ui/window.cc +++ b/src/ui/window.cc @@ -4,8 +4,8 @@ the terms of the GNU General Public License version 2 */ +#include "ui/paint.h" #include "ui/window.h" -#include "render/primitives.h" namespace ui { @@ -29,9 +29,8 @@ void Window::draw_border() if (!border()) return; - if (palette()) - render::gl::color(palette()->foreground()); - render::primitives::border(global_location(), size()); + paint::color(palette()->foreground()); + paint::border(global_location(), size()); } Window::Windows::iterator Window::find_window(Window *window) -- cgit v1.2.3