From 8861bf21e9821f7adcbbafcb9f72e0f1b8f59344 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 26 Apr 2009 15:51:50 +0000 Subject: added ui::IconButton class --- src/ui/iconbutton.cc | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/ui/iconbutton.cc (limited to 'src/ui/iconbutton.cc') diff --git a/src/ui/iconbutton.cc b/src/ui/iconbutton.cc new file mode 100644 index 0000000..1999072 --- /dev/null +++ b/src/ui/iconbutton.cc @@ -0,0 +1,115 @@ +/* + ui/iconbutton.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include + +#include "audio/audio.h" +#include "auxiliary/functions.h" +#include "core/application.h" +#include "core/commandbuffer.h" +#include "sys/sys.h" +#include "ui/paint.h" +#include "ui/iconbutton.h" + +namespace ui +{ + +IconButton::IconButton(Widget *parent, const char *icon, const char *command) : Widget(parent) +{ + set_label("iconbutton"); + set_background(false); + set_border(true); + set_command(command); + set_icon(icon); +} + +IconButton::~IconButton() +{ +} + +void IconButton::print(const size_t indent) const +{ + std::string marker(""); + con_print << aux::pad_left(marker, indent*2) << label() << " \"" << icon() << "\" \"" << command() << "\"" << std::endl; +} + +void IconButton::set_command(const char *command) +{ + if (command) + iconbutton_command.assign(command); + else + iconbutton_command.clear(); +} + +void IconButton::set_command(const std::string &command) +{ + iconbutton_command.assign(command); +} + +void IconButton::set_icon(const char *icon) +{ + if (icon) + iconbutton_icon.assign(icon); + else + iconbutton_icon.clear(); +} + +void IconButton::set_icon(const std::string &icon) +{ + iconbutton_icon.assign(icon); +} + +void IconButton::draw() +{ + if (!icon().size()) + return; + + if (has_mouse_focus()) + paint::color(palette()->highlight()); + else + paint::color(palette()->foreground()); + + paint::bitmap(location(), size(), icon()); +} + +void IconButton::draw_border() +{ + if (has_mouse_focus()) { + math::Color color(palette()->foreground()); + float t = core::application()->time(); + t = t - floorf(t); + if (t > 0.5) + t = 1 - t; + color.a = 0.5f + t; + paint::color(color); + paint::border(global_location(), size()); + } +} + +bool IconButton::on_keypress(const int key, const unsigned int modifier) +{ + if (key == 512 + SDL_BUTTON_LEFT) { + if (iconbutton_command.size()) { + core::cmd() << iconbutton_command << std::endl; + audio::play("ui/button"); + } + return true; + } + + return false; +} + +bool IconButton::on_keyrelease(const int key, const unsigned int modifier) +{ + return false; +} + +void IconButton::on_mouseover(const math::Vector2f &cursor) +{ + audio::play("ui/select"); +} + +} -- cgit v1.2.3