gameui

annotate src/widget.cc @ 4:e0916bb20b7f

changed the name to goatkit
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 21 Mar 2014 21:45:37 +0200
parents f1014234dece
children 5a84873185ff
rev   line source
nuclear@3 1 #include <stdio.h>
nuclear@3 2 #include <math.h>
nuclear@0 3 #include <string>
nuclear@2 4 #include <sstream>
nuclear@0 5 #include "widget.h"
nuclear@2 6 #include "boolanm.h"
nuclear@3 7 #include "theme.h"
nuclear@0 8
nuclear@4 9 namespace goatkit {
nuclear@0 10
nuclear@2 11 struct WidgetImpl {
nuclear@3 12 std::string type_str;
nuclear@0 13 std::string name;
nuclear@0 14 BBox box;
nuclear@0 15
nuclear@2 16 BoolAnim visible, active, press, hover;
nuclear@0 17 };
nuclear@0 18
nuclear@0 19
nuclear@0 20 Widget::Widget()
nuclear@0 21 {
nuclear@0 22 static int widget_count;
nuclear@0 23
nuclear@3 24 widget = new WidgetImpl;
nuclear@3 25 set_type_string("widget");
nuclear@3 26
nuclear@0 27 std::stringstream sstr;
nuclear@3 28 sstr << widget->type_str << widget_count++;
nuclear@3 29 widget->name = sstr.str();
nuclear@0 30
nuclear@3 31 widget->box.bmin = Vec2(0, 0);
nuclear@3 32 widget->box.bmax = Vec2(1, 1);
nuclear@0 33
nuclear@3 34 widget->visible.set(true);
nuclear@3 35 widget->active.set(true);
nuclear@0 36
nuclear@3 37 widget->hover.set_transition_duration(250);
nuclear@3 38 widget->press.set_transition_duration(50);
nuclear@0 39 }
nuclear@0 40
nuclear@0 41 Widget::~Widget()
nuclear@0 42 {
nuclear@3 43 delete widget;
nuclear@0 44 }
nuclear@0 45
nuclear@0 46 void Widget::show()
nuclear@0 47 {
nuclear@3 48 widget->visible.change(true);
nuclear@0 49 }
nuclear@0 50
nuclear@0 51 void Widget::hide()
nuclear@0 52 {
nuclear@3 53 widget->visible.change(false);
nuclear@0 54 }
nuclear@0 55
nuclear@0 56 float Widget::get_visibility() const
nuclear@0 57 {
nuclear@3 58 return widget->visible.get_value();
nuclear@3 59 }
nuclear@0 60
nuclear@3 61 bool Widget::is_visible() const
nuclear@3 62 {
nuclear@3 63 return widget->visible.get_state();
nuclear@0 64 }
nuclear@0 65
nuclear@1 66 void Widget::activate()
nuclear@1 67 {
nuclear@3 68 widget->active.change(true);
nuclear@1 69 }
nuclear@1 70
nuclear@3 71 void Widget::deactivate()
nuclear@0 72 {
nuclear@3 73 widget->active.change(false);
nuclear@0 74 }
nuclear@0 75
nuclear@3 76 float Widget::get_active() const
nuclear@0 77 {
nuclear@3 78 return widget->active.get_value();
nuclear@3 79 }
nuclear@0 80
nuclear@3 81 bool Widget::is_active() const
nuclear@3 82 {
nuclear@3 83 return widget->active.get_state();
nuclear@3 84 }
nuclear@3 85
nuclear@3 86 void Widget::press()
nuclear@3 87 {
nuclear@3 88 widget->press.change(true);
nuclear@3 89 }
nuclear@3 90
nuclear@3 91 void Widget::release()
nuclear@3 92 {
nuclear@3 93 widget->press.change(false);
nuclear@3 94 }
nuclear@3 95
nuclear@3 96 float Widget::get_pressed() const
nuclear@3 97 {
nuclear@3 98 return widget->press.get_value();
nuclear@3 99 }
nuclear@3 100
nuclear@3 101 bool Widget::is_pressed() const
nuclear@3 102 {
nuclear@3 103 return widget->press.get_state();
nuclear@3 104 }
nuclear@3 105
nuclear@3 106 void Widget::mousein()
nuclear@3 107 {
nuclear@3 108 widget->hover.change(true);
nuclear@3 109 }
nuclear@3 110
nuclear@3 111 void Widget::mouseout()
nuclear@3 112 {
nuclear@3 113 widget->hover.change(false);
nuclear@3 114 }
nuclear@3 115
nuclear@3 116 float Widget::get_under_mouse() const
nuclear@3 117 {
nuclear@3 118 return widget->hover.get_value();
nuclear@3 119 }
nuclear@3 120
nuclear@3 121 bool Widget::is_under_mouse() const
nuclear@3 122 {
nuclear@3 123 return widget->hover.get_state();
nuclear@3 124 }
nuclear@3 125
nuclear@3 126 void Widget::set_position(float x, float y)
nuclear@3 127 {
nuclear@3 128 set_position(Vec2(x, y));
nuclear@3 129 }
nuclear@3 130
nuclear@3 131 void Widget::set_position(const Vec2 &pos)
nuclear@3 132 {
nuclear@3 133 Vec2 sz = get_size();
nuclear@3 134
nuclear@3 135 widget->box.bmin = pos;
nuclear@3 136 widget->box.bmax.x = pos.x + sz.x;
nuclear@3 137 widget->box.bmax.y = pos.y + sz.y;
nuclear@3 138 }
nuclear@3 139
nuclear@3 140 const Vec2 &Widget::get_position() const
nuclear@3 141 {
nuclear@3 142 return widget->box.bmin;
nuclear@3 143 }
nuclear@3 144
nuclear@3 145 void Widget::set_size(float x, float y)
nuclear@3 146 {
nuclear@3 147 set_size(Vec2(x, y));
nuclear@3 148 }
nuclear@3 149
nuclear@3 150 void Widget::set_size(const Vec2 &sz)
nuclear@3 151 {
nuclear@3 152 widget->box.bmax.x = widget->box.bmin.x + sz.x;
nuclear@3 153 widget->box.bmax.y = widget->box.bmin.y + sz.y;
nuclear@3 154 }
nuclear@3 155
nuclear@3 156 const Vec2 Widget::get_size() const
nuclear@3 157 {
nuclear@3 158 return Vec2(widget->box.bmax.x - widget->box.bmin.x,
nuclear@3 159 widget->box.bmax.y - widget->box.bmin.y);
nuclear@3 160 }
nuclear@3 161
nuclear@3 162
nuclear@3 163 const BBox &Widget::get_box() const
nuclear@3 164 {
nuclear@3 165 return widget->box;
nuclear@3 166 }
nuclear@3 167
nuclear@3 168 bool Widget::hit_test(const Vec2 &pt) const
nuclear@3 169 {
nuclear@3 170 return pt.x >= widget->box.bmin.x && pt.x < widget->box.bmax.x &&
nuclear@3 171 pt.y >= widget->box.bmin.y && pt.y < widget->box.bmax.y;
nuclear@3 172 }
nuclear@3 173
nuclear@3 174 void Widget::draw() const
nuclear@3 175 {
nuclear@3 176 widget_draw_func draw_func = default_draw_func;
nuclear@3 177
nuclear@3 178 if(theme) {
nuclear@3 179 draw_func = theme->get_draw_func(widget->type_str.c_str());
nuclear@0 180 }
nuclear@3 181
nuclear@3 182 draw_func(this);
nuclear@0 183 }
nuclear@3 184
nuclear@3 185 // dummy event handlers
nuclear@3 186 void Widget::on_mouse_button(const ButtonEvent &ev)
nuclear@3 187 {
nuclear@3 188 }
nuclear@3 189
nuclear@3 190 void Widget::on_mouse_motion(const MotionEvent &ev)
nuclear@3 191 {
nuclear@3 192 }
nuclear@3 193
nuclear@3 194 void Widget::on_mouse_focus(const FocusEvent &ev)
nuclear@3 195 {
nuclear@3 196 }
nuclear@3 197
nuclear@3 198 void Widget::on_key(const KeyEvent &ev)
nuclear@3 199 {
nuclear@3 200 }
nuclear@3 201
nuclear@3 202 void Widget::on_click()
nuclear@3 203 {
nuclear@3 204 }
nuclear@3 205
nuclear@3 206 void Widget::on_double_click()
nuclear@3 207 {
nuclear@3 208 }
nuclear@3 209
nuclear@3 210 void Widget::on_change()
nuclear@3 211 {
nuclear@3 212 }
nuclear@3 213
nuclear@3 214
nuclear@3 215 void Widget::set_type_string(const char *type_str)
nuclear@3 216 {
nuclear@3 217 widget->type_str = type_str;
nuclear@3 218 }
nuclear@3 219
nuclear@3 220 /* the event dispatcher generates high-level events (click, etc)
nuclear@3 221 * and calls the on_whatever() functions for both low and high-level
nuclear@3 222 * events.
nuclear@3 223 * The on_whatever functions are called *after* any other actions performed
nuclear@3 224 * here, to give subclasses the opportunity to override them easily, by
nuclear@3 225 * overriding the on_ functions, without having to override handle_event itself
nuclear@3 226 */
nuclear@3 227 // TODO also call callbacks here I guess...
nuclear@3 228 void Widget::handle_event(const Event &ev)
nuclear@3 229 {
nuclear@3 230 switch(ev.type) {
nuclear@3 231 case EV_MOUSE_BUTTON:
nuclear@3 232 if(ev.button.press) {
nuclear@3 233 press();
nuclear@3 234 } else {
nuclear@3 235 if(is_pressed()) {
nuclear@3 236 on_click();
nuclear@3 237 }
nuclear@3 238 release();
nuclear@3 239 }
nuclear@3 240 on_mouse_button(ev.button);
nuclear@3 241 break;
nuclear@3 242
nuclear@3 243 case EV_MOUSE_MOTION:
nuclear@3 244 on_mouse_motion(ev.motion);
nuclear@3 245 break;
nuclear@3 246
nuclear@3 247 case EV_MOUSE_FOCUS:
nuclear@3 248 if(ev.focus.enter) {
nuclear@3 249 mousein();
nuclear@3 250 } else {
nuclear@3 251 mouseout();
nuclear@3 252 }
nuclear@3 253 on_mouse_focus(ev.focus);
nuclear@3 254 break;
nuclear@3 255
nuclear@3 256 case EV_KEY:
nuclear@3 257 on_key(ev.key);
nuclear@3 258 break;
nuclear@3 259
nuclear@3 260 default:
nuclear@3 261 fprintf(stderr, "%s: unknown event id: %d\n", __func__, ev.type);
nuclear@3 262 }
nuclear@3 263 }
nuclear@3 264
nuclear@3 265
nuclear@4 266 } // namespace goatkit