2 GoatKit - a themable/animated widget toolkit for games
3 Copyright (C) 2014-2015 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 std::string name, text;
35 BoolAnim visible, active, press, hover, focus;
46 static int widget_count;
48 widget = new WidgetImpl;
51 std::stringstream sstr;
52 sstr << get_type_name() << widget_count++;
53 widget->name = sstr.str();
55 widget->box.bmin = Vec2(0, 0);
56 widget->box.bmax = Vec2(1, 1);
58 widget->visible.set(true);
59 widget->active.set(true);
61 widget->hover.set_transition_duration(250);
62 widget->press.set_transition_duration(50);
64 memset(widget->cb, 0, sizeof widget->cb);
72 void Widget::set_screen(Screen *scr)
77 Screen *Widget::get_screen() const
82 const char *Widget::get_type_name() const
87 void Widget::set_name(const char *name)
89 widget->name = std::string(name);
92 const char *Widget::get_name() const
94 return widget->name.c_str();
97 void Widget::set_text(const char *text)
99 widget->text = std::string(text);
102 const char *Widget::get_text() const
104 return widget->text.c_str();
109 widget->visible.change(true);
114 widget->visible.change(false);
117 float Widget::get_visibility() const
119 return widget->visible.get_value();
122 bool Widget::is_visible() const
124 return widget->visible.get_state();
127 void Widget::set_visibility_transition(long msec)
129 widget->visible.set_transition_duration(msec);
132 long Widget::get_visibility_transition() const
134 return widget->visible.get_transition_duration();
137 void Widget::activate()
139 widget->active.change(true);
142 void Widget::deactivate()
144 widget->active.change(false);
147 float Widget::get_active() const
149 return widget->active.get_value();
152 bool Widget::is_active() const
154 return widget->active.get_state();
157 void Widget::set_active_transition(long msec)
159 widget->active.set_transition_duration(msec);
162 long Widget::get_active_transition() const
164 return widget->active.get_transition_duration();
169 widget->press.change(true);
172 void Widget::release()
174 widget->press.change(false);
177 float Widget::get_pressed() const
179 return widget->press.get_value();
182 bool Widget::is_pressed() const
184 return widget->press.get_state();
187 void Widget::set_press_transition(long msec)
189 widget->press.set_transition_duration(msec);
192 long Widget::get_press_transition() const
194 return widget->press.get_transition_duration();
197 void Widget::mousein()
199 widget->hover.change(true);
202 void Widget::mouseout()
204 widget->hover.change(false);
206 widget->press.change(false);
210 float Widget::get_under_mouse() const
212 return widget->hover.get_value();
215 bool Widget::is_under_mouse() const
217 return widget->hover.get_state();
220 void Widget::set_hover_transition(long msec)
222 widget->hover.set_transition_duration(msec);
225 long Widget::get_hover_transition() const
227 return widget->hover.get_transition_duration();
230 bool Widget::can_focus() const
235 void Widget::focusin()
237 widget->focus.change(true);
240 void Widget::focusout()
242 widget->focus.change(false);
245 float Widget::get_focus() const
247 return widget->focus.get_value();
250 bool Widget::is_focused() const
252 return widget->focus.get_state();
255 void Widget::set_focus_transition(long msec)
257 widget->focus.set_transition_duration(msec);
260 long Widget::get_focus_transition() const
262 return widget->focus.get_transition_duration();
265 void Widget::set_position(float x, float y)
267 set_position(Vec2(x, y));
270 void Widget::set_position(const Vec2 &pos)
272 Vec2 sz = get_size();
274 widget->box.bmin = pos;
275 widget->box.bmax.x = pos.x + sz.x;
276 widget->box.bmax.y = pos.y + sz.y;
279 const Vec2 &Widget::get_position() const
281 return widget->box.bmin;
284 void Widget::set_size(float x, float y)
286 set_size(Vec2(x, y));
289 void Widget::set_size(const Vec2 &sz)
291 widget->box.bmax.x = widget->box.bmin.x + sz.x;
292 widget->box.bmax.y = widget->box.bmin.y + sz.y;
295 const Vec2 Widget::get_size() const
297 return Vec2(widget->box.bmax.x - widget->box.bmin.x,
298 widget->box.bmax.y - widget->box.bmin.y);
302 const BBox &Widget::get_box() const
307 bool Widget::hit_test(const Vec2 &pt) const
309 return pt.x >= widget->box.bmin.x && pt.x < widget->box.bmax.x &&
310 pt.y >= widget->box.bmin.y && pt.y < widget->box.bmax.y;
313 void Widget::draw() const
315 WidgetDrawFunc draw_func = default_draw_func;
318 draw_func = theme->get_draw_func(get_type_name());
324 // dummy event handlers
325 void Widget::on_mouse_button(const ButtonEvent &ev)
329 void Widget::on_mouse_motion(const MotionEvent &ev)
333 void Widget::on_mouse_focus(const FocusEvent &ev)
337 void Widget::on_key(const KeyEvent &ev)
341 void Widget::on_click()
345 void Widget::on_double_click()
349 void Widget::on_change()
354 #define CALL_CB(w, ev) \
356 if((w)->widget->cb[ev.type].func) { \
357 (w)->widget->cb[ev.type].func((w), ev, (w)->widget->cb[ev.type].cls); \
361 #define CALL_CB_TYPE(w, t) \
368 /* the event dispatcher generates high-level events (click, etc)
369 * and calls the on_whatever() functions for both low and high-level
371 * The on_whatever functions are called *after* any other actions performed
372 * here, to give subclasses the opportunity to override them easily, by
373 * overriding the on_ functions, without having to override handle_event itself
375 // TODO also call callbacks here I guess...
376 void Widget::handle_event(const Event &ev)
379 case EV_MOUSE_BUTTON:
380 if(ev.button.press) {
384 CALL_CB_TYPE(this, EV_CLICK);
390 on_mouse_button(ev.button);
393 case EV_MOUSE_MOTION:
394 on_mouse_motion(ev.motion);
403 on_mouse_focus(ev.focus);
422 void Widget::set_callback(EventType evtype, EventCallback func, void *cls)
424 widget->cb[evtype].func = func;
425 widget->cb[evtype].cls = cls;
429 } // namespace goatkit