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/Makefile.am | 5 ++- src/ui/iconbutton.cc | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/ui/iconbutton.h | 71 +++++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 src/ui/iconbutton.cc create mode 100644 src/ui/iconbutton.h (limited to 'src/ui') diff --git a/src/ui/Makefile.am b/src/ui/Makefile.am index 695f2db..3b4bcd1 100644 --- a/src/ui/Makefile.am +++ b/src/ui/Makefile.am @@ -8,10 +8,11 @@ noinst_LTLIBRARIES = libui.la endif noinst_HEADERS = bitmap.h button.h console.h container.h definitions.h font.h \ - inputbox.h label.h menu.h modelview.h paint.h palette.h scrollpane.h toolbar.h ui.h widget.h \ + iconbutton.h inputbox.h label.h menu.h modelview.h paint.h palette.h scrollpane.h toolbar.h ui.h widget.h \ window.h libui_la_SOURCES = bitmap.cc button.cc console.cc console.h container.cc \ - font.cc inputbox.cc label.cc menu.cc modelview.cc paint.cc palette.cc scrollpane.cc \ + font.cc iconbutton.cc inputbox.cc label.cc menu.cc modelview.cc paint.cc palette.cc scrollpane.cc \ toolbar.cc ui.cc widget.cc window.cc libui_la_LDFLAGS = -avoid-version -no-undefined + 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"); +} + +} diff --git a/src/ui/iconbutton.h b/src/ui/iconbutton.h new file mode 100644 index 0000000..d545697 --- /dev/null +++ b/src/ui/iconbutton.h @@ -0,0 +1,71 @@ +/* + ui/iconbutton.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_ICONBUTTON_H__ +#define __INCLUDED_UI_ICONBUTTON_H__ + +#include "ui/widget.h" + +namespace ui +{ + +class IconButton : public Widget +{ +public: + IconButton(Widget *parent, const char *icon=0, const char *command=0); + ~IconButton(); + + /// set the command this button will execute + void set_command(const std::string &command); + + /// set the command this button will execute + void set_command(const char *command); + + /// the command this button executes + inline const std::string & command() const { + return iconbutton_command; + } + + + /// set the icon texture + void set_icon(const std::string & icon); + + /// set the icon texture + void set_icon(const char *icon); + + /// the icon texture + inline const std::string & icon() const { + return iconbutton_icon; + } + + /// print button description + virtual void print(const size_t indent) const; + + /// called when the mouse enters the widget + virtual void on_mouseover(const math::Vector2f &cursor); + + /// called when the widget receives a key press + virtual bool on_keypress(const int key, const unsigned int modifier); + + /// called when the widget receives a key release + virtual bool on_keyrelease(const int key, const unsigned int modifier); + +protected: + /// draw the button border + virtual void draw_border(); + + /// draw the button + virtual void draw(); + +private: + std::string iconbutton_command; + std::string iconbutton_icon; +}; + +} + +#endif // __INCLUDED_UI_ICONBUTTON_H__ + -- cgit v1.2.3