From da9beb729c58ca2d91f67ab85a6728b628c27cf2 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 5 Oct 2008 19:03:25 +0000 Subject: user interface library --- src/ui/widget.cc | 190 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 src/ui/widget.cc (limited to 'src/ui/widget.cc') diff --git a/src/ui/widget.cc b/src/ui/widget.cc new file mode 100644 index 0000000..94260cb --- /dev/null +++ b/src/ui/widget.cc @@ -0,0 +1,190 @@ +/* + ui/widget.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 "render/primitives.h" +#include "sys/sys.h" +#include "ui/widget.h" + +namespace ui { + +Widget::Widget(Widget *parent) { + widget_parent = parent; + widget_visible = true; + widget_border = true; + widget_background = false; + widget_label.assign("widget"); + + if (widget_parent) { + widget_palette = parent->widget_palette; + parent->add_child(this); + } else { + widget_palette = 0; + } +} + +Widget::~Widget() +{ + if (widget_parent) { + widget_parent->remove_child(this); + } + + for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) { + (*it)->widget_parent = 0; + delete (*it); + } + + widget_children.clear(); +} + +size_t Widget::list(size_t indent) +{ + size_t n = 1; + std::string marker(""); + con_print << aux::pad_left(marker, indent*2) << label() << " " << (size_t)(this) << std::endl; + + for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) { + n += (*it)->list(indent+1); + } + return n; +} + +void Widget::show() +{ + widget_visible = true; +} + +void Widget::hide() +{ + widget_visible = false; +} + +void Widget::set_border(bool border) +{ + widget_border = border; +} + + +void Widget::set_background(bool background) +{ + widget_background = background; +} + +void Widget::set_label(std::string const & label) +{ + set_label(label.c_str()); +} + +void Widget::set_label(char const *label) +{ + widget_label.assign(label); + aux::to_label(widget_label); +} + +void Widget::set_palette(Palette *palette) +{ + widget_palette = palette; +} + +void Widget::set_location(float const x, float const y) { + widget_location.assign(x, y); +} + +void Widget::set_size(float const w, float const h) +{ + widget_size.assign(w, h); +} + +void Widget::set_width(float const w) +{ + widget_size.x = w; +} +void Widget::set_height(float const h) +{ + widget_size.y = h; +} + +Widget::Children::iterator Widget::find_child(Widget *child) +{ + Children::iterator it; + for (it = widget_children.begin(); it != widget_children.end(); it++) { + if ((*it) == child) + return it; + } + + return it; +} + +void Widget::add_child(Widget *child) +{ + Children::iterator it = find_child(child); + if (it == widget_children.end()) { + widget_children.push_back(child); + } +} + +void Widget::remove_child(Widget *child) +{ + + Children::iterator it = find_child(child); + if (it != widget_children.end()) { + widget_children.erase(it); + } +} + +void Widget::resize_event() +{ + resize(); + for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) { + (*it)->resize_event(); + } +} + +void Widget::resize() +{ +} + +void Widget::draw_event() +{ + if (!widget_visible) + return; + + draw(); + for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) { + if ((*it)->visible()) + (*it)->draw_event(); + } +} + +void Widget::draw() +{ + draw_background(); + draw_border(); +} + +void Widget::draw_background() +{ + if (!widget_background) + return; + + if (palette()) + render::gl::color(palette()->background()); + math::Vector2f v(to_global(location())); + render::primitives::rectangle(v, size()); +} + +void Widget::draw_border() +{ + if (!widget_border) + return; + + if (palette()) + render::gl::color(palette()->border()); + math::Vector2f v(to_global(location())); + render::primitives::border(v, size()); +} + +} -- cgit v1.2.3