/* 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(false); set_command(command); set_icon(icon); iconbutton_enabled = true; } 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::enable(bool enabled) { iconbutton_enabled = enabled; } void IconButton::disable(bool disabled) { iconbutton_enabled = !disabled; } void IconButton::draw() { if (!icon().size()) return; if (disabled()) { paint::color(palette()->disabled()); } else if (has_mouse_focus()) { paint::color(palette()->highlight()); } else { paint::color(palette()->foreground()); } paint::bitmap(location(), size(), icon()); } void IconButton::draw_border() { if (enabled() && 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 (enabled() && 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) { if (enabled()) audio::play("ui/select"); } }