Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
path: root/src/ui
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/Makefile.am4
-rw-r--r--src/ui/listitem.h2
-rw-r--r--src/ui/listview.cc3
-rw-r--r--src/ui/slider.cc111
-rw-r--r--src/ui/slider.h101
-rw-r--r--src/ui/widget.cc28
-rw-r--r--src/ui/widget.h32
7 files changed, 267 insertions, 14 deletions
diff --git a/src/ui/Makefile.am b/src/ui/Makefile.am
index e62a5dd..a98e964 100644
--- a/src/ui/Makefile.am
+++ b/src/ui/Makefile.am
@@ -10,12 +10,12 @@ endif
noinst_HEADERS = bitmap.h button.h console.h container.h definitions.h font.h \
iconbutton.h inputbox.h label.h listitem.h listview.h \
menu.h modelview.h paint.h palette.h plaintext.h scrollpane.h \
- toolbar.h ui.h widget.h window.h
+ slider.h toolbar.h ui.h widget.h window.h
libui_la_SOURCES = bitmap.cc button.cc console.cc container.cc font.cc \
iconbutton.cc inputbox.cc label.cc listitem.cc listview.cc \
menu.cc modelview.cc paint.cc palette.cc plaintext.cc scrollpane.cc \
- toolbar.cc ui.cc widget.cc window.cc
+ slider.cc toolbar.cc ui.cc widget.cc window.cc
libui_la_LDFLAGS = -avoid-version -no-undefined
diff --git a/src/ui/listitem.h b/src/ui/listitem.h
index 1d4cd33..6fad3e9 100644
--- a/src/ui/listitem.h
+++ b/src/ui/listitem.h
@@ -43,6 +43,8 @@ public:
listitem_item = item;
}
+ void select();
+
protected:
/// keypress event handler
virtual bool on_keypress(const int key, const unsigned int modifier);
diff --git a/src/ui/listview.cc b/src/ui/listview.cc
index e503807..ced0dd6 100644
--- a/src/ui/listview.cc
+++ b/src/ui/listview.cc
@@ -90,7 +90,8 @@ bool ListView::on_emit(Widget *sender, const Event event, void *data)
{
if ((sender->parent() == this) && (event == Widget::EventListItemClicked)) {
listview_selecteditem = static_cast<ListItem *>(sender);
- return false; // return false because parent widgets might want to handle this event
+ emit(EventListViewChanged);
+ return true;
}
return ui::Widget::on_emit(sender, event, data);
diff --git a/src/ui/slider.cc b/src/ui/slider.cc
new file mode 100644
index 0000000..5de82b2
--- /dev/null
+++ b/src/ui/slider.cc
@@ -0,0 +1,111 @@
+/*
+ ui/slider.cc
+ This file is part of the Osirion project and is distributed under
+ the terms of the GNU General Public License version 2
+*/
+
+#include "math/functions.h"
+#include "ui/paint.h"
+#include "ui/slider.h"
+
+namespace ui {
+
+Slider::Slider(Widget *parent, const float minimum, const float maximum) : Widget(parent)
+{
+ slider_minimum = minimum;
+ slider_maximum = maximum;
+ slider_value = slider_minimum;
+
+ slider_minbutton = new ui::Button(this, "<");
+ slider_minbutton->set_border(false);
+
+ slider_maxbutton = new ui::Button(this, ">>");
+ slider_maxbutton->set_border(false);
+
+ validate();
+}
+
+Slider::~Slider() {
+}
+
+void Slider::set_value(const float value)
+{
+ slider_value = value;
+ validate();
+}
+
+void Slider::set_minimum(const float minimum)
+{
+ slider_minimum = minimum;
+ validate();
+}
+
+void Slider::set_maximum(const float maximum)
+{
+ slider_maximum = maximum;
+ validate();
+}
+
+void Slider::set_range(const float minimum, const float maximum)
+{
+ slider_minimum = minimum;
+ slider_maximum = maximum;
+ validate();
+}
+
+void Slider::validate()
+{
+ if (slider_minimum > slider_maximum) {
+ math::swap(slider_minimum, slider_maximum);
+ }
+
+ math::clamp(slider_value, slider_minimum, slider_maximum);
+}
+
+void Slider::print(const size_t indent) const
+{
+ std::string marker("");
+ con_print << aux::pad_left(marker, indent*2) << label() << " \"" << minimum() << "-" << maximum() << "\"" << std::endl;
+}
+
+bool Slider::on_emit(Widget *sender, const Event event, void *data)
+{
+ if (event == EventButtonClicked) {
+ if (sender == slider_minbutton) {
+ slider_value = slider_minimum;
+ emit(EventSliderChanged, this);
+ return true;
+ } else if (sender == slider_maxbutton) {
+ slider_value = slider_maximum;
+ emit(EventSliderChanged, this);
+ return true;
+ }
+ }
+
+ return false;
+}
+
+
+void Slider::resize()
+{
+ // note: slider expects width > height
+ slider_minbutton->set_size(height(), height());
+ slider_minbutton->set_location(0, 0);
+
+ slider_maxbutton->set_size(height(), height());
+ slider_maxbutton->set_location(width() - slider_maxbutton->width(), 0);
+}
+
+void Slider::draw()
+{
+ if (slider_maximum > slider_minimum) {
+ const float range = (slider_value - slider_minimum) / (slider_maximum - slider_minimum);
+ const float x = (width() - 3.0f * height()) * range;
+ Paint::set_color(palette()->foreground());
+ Paint::draw_rectangle(math::Vector2f(global_location().x() + height() + x, global_location().y()), math::Vector2f(height(), height()));
+ }
+
+ Widget::draw();
+}
+
+} // namespace ui
diff --git a/src/ui/slider.h b/src/ui/slider.h
new file mode 100644
index 0000000..8bcb427
--- /dev/null
+++ b/src/ui/slider.h
@@ -0,0 +1,101 @@
+/*
+ ui/slider.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_SLIDER_H__
+#define __INCLUDED_UI_SLIDER_H__
+
+#include <string>
+#include "ui/button.h"
+#include "ui/widget.h"
+
+namespace ui
+{
+
+/**
+ * @brief a slider widget
+ */
+class Slider : public Widget
+{
+public:
+ Slider(Widget *parent, const float minimum = 0.0f, const float maximum = 1.0f);
+ ~Slider();
+
+ /**
+ * @brief the current slider value
+ */
+ inline const float value() const {
+ return slider_value;
+ }
+
+ /**
+ * @brief the minimum value
+ */
+ inline const float minimum() const {
+ return slider_minimum;
+ }
+
+ /**
+ * @brief the maximum value
+ */
+ inline const float maximum() const {
+ return slider_maximum;
+ }
+
+ /**
+ * @brief print widget description to console
+ */
+ virtual void print(const size_t indent) const;
+
+ /**
+ * @brief set the current slider value
+ * @see value
+ */
+ void set_value(const float value);
+
+ /**
+ * @brief set the minimum slider value
+ * @see minimum
+ */
+ void set_minimum(const float minimum);
+
+ /**
+ * @brief set the maximum slider value
+ * @see maximum
+ */
+ void set_maximum(const float maximum);
+
+ /**
+ * @brief set the minimum and maximum slider values
+ * @see minimum
+ * @see maximum
+ */
+ void set_range(const float minimum, const float maximum);
+
+protected:
+ /// resize event handler
+ virtual void resize();
+
+ /// draw event handler
+ virtual void draw();
+
+ virtual bool on_emit(Widget *sender, const Event event, void *data=0);
+
+private:
+ /// validate slider value
+ void validate();
+
+ float slider_minimum;
+ float slider_maximum;
+ float slider_value;
+
+ Button *slider_minbutton;
+ Button *slider_maxbutton;
+};
+
+}
+
+#endif // __INCLUDED_UI_SLIDER_H__
+
diff --git a/src/ui/widget.cc b/src/ui/widget.cc
index b3aa27c..2f35185 100644
--- a/src/ui/widget.cc
+++ b/src/ui/widget.cc
@@ -19,6 +19,7 @@ Widget::Widget(Widget *parent)
widget_visible = true;
widget_border = true;
widget_background = false;
+ widget_enabled = true;
widget_palette = 0;
widget_font = 0;
widget_label.assign("widget");
@@ -162,7 +163,7 @@ void Widget::hide()
}
-void Widget::set_visible(bool visible)
+void Widget::set_visible(const bool visible)
{
if (visible)
show();
@@ -170,24 +171,41 @@ void Widget::set_visible(bool visible)
hide();
}
-void Widget::set_border(bool border)
+void Widget::enable()
+{
+ widget_enabled = true;
+}
+
+void Widget::disable()
+{
+ widget_enabled = false;
+}
+
+void Widget::set_enabled(const bool enabled)
+{
+ if (enabled)
+ enable();
+ else
+ disable();
+}
+void Widget::set_border(const bool border)
{
widget_border = border;
}
-void Widget::set_background(bool background)
+void Widget::set_background(const bool background)
{
widget_background = background;
}
-void Widget::set_label(std::string const & label)
+void Widget::set_label(const std::string & label)
{
widget_label.assign(label);
aux::to_label(widget_label);
}
-void Widget::set_label(char const *label)
+void Widget::set_label(const char *label)
{
if (label) {
widget_label.assign(label);
diff --git a/src/ui/widget.h b/src/ui/widget.h
index 3ae75d2..8b61bc1 100644
--- a/src/ui/widget.h
+++ b/src/ui/widget.h
@@ -28,7 +28,7 @@ class Widget
public:
/// types of custom events a widget can emit
- enum Event {EventNone = 0, EventButtonClicked, EventListItemClicked};
+ enum Event {EventNone = 0, EventButtonClicked, EventListItemClicked, EventListViewChanged, EventSliderChanged};
/// create a new widget
Widget(Widget *parent = 0);
@@ -128,6 +128,16 @@ public:
return !widget_visible;
}
+ /// true if the widget is enabled
+ inline bool enabled() const {
+ return widget_enabled;
+ }
+
+ /// true if the widget is disabled
+ inline bool disabled() const {
+ return !widget_enabled;
+ }
+
/// the palette used to draw this widget
const Palette *palette() const;
@@ -156,9 +166,18 @@ public:
/// hide the widget
virtual void hide();
-
+
/// set visibility
- void set_visible(bool visible = true);
+ void set_visible(const bool visible = true);
+
+ /// disable the widget
+ virtual void disable();
+
+ /// enable the widget
+ virtual void enable();
+
+ ///set enabled or disabled state
+ void set_enabled(const bool enabled = true);
/// set input focus
void set_focus();
@@ -194,16 +213,16 @@ public:
void set_font(const Font *font);
/// set the widgets label
- void set_label(std::string const &label);
+ void set_label(const std::string &label);
/// set the widgets label
void set_label(const char *label);
/// enable or disable widget border
- void set_border(bool border = true);
+ void set_border(const bool border = true);
/// enable or disable widget background
- void set_background(bool background = true);
+ void set_background(const bool background = true);
/* -- event distributors --------------------------------------- */
@@ -354,6 +373,7 @@ private:
bool widget_background;
bool widget_border;
bool widget_focus;
+ bool widget_enabled;
math::Vector2f widget_location;
math::Vector2f widget_size;