2013-07-27 09:54:41 +00:00
|
|
|
gui.Widget = {
|
|
|
|
# enum FocusPolicy:
|
|
|
|
NoFocus: 0,
|
|
|
|
TabFocus: 1,
|
|
|
|
ClickFocus: 2,
|
|
|
|
StrongFocus: 1 + 2,
|
|
|
|
|
|
|
|
#
|
|
|
|
new: func(derived)
|
|
|
|
{
|
2014-06-01 10:25:34 +00:00
|
|
|
return canvas.Widget.new({
|
2013-07-27 09:54:41 +00:00
|
|
|
parents: [derived, gui.Widget],
|
|
|
|
_focused: 0,
|
|
|
|
_focus_policy: gui.Widget.NoFocus,
|
|
|
|
_hover: 0,
|
2014-03-19 22:20:09 +00:00
|
|
|
_root: nil,
|
|
|
|
_size: [64, 64]
|
2014-06-01 10:25:34 +00:00
|
|
|
});
|
2013-07-27 09:54:41 +00:00
|
|
|
},
|
|
|
|
# Move the widget to the given position (relative to its parent)
|
|
|
|
move: func(x, y)
|
|
|
|
{
|
|
|
|
me._root.setTranslation(x, y);
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
#
|
2014-03-19 22:20:09 +00:00
|
|
|
setSize: func(w, h)
|
|
|
|
{
|
|
|
|
me._size[0] = w;
|
|
|
|
me._size[1] = h;
|
|
|
|
},
|
|
|
|
#
|
2013-07-27 09:54:41 +00:00
|
|
|
setFocus: func
|
|
|
|
{
|
|
|
|
if( me._focused )
|
|
|
|
return me;
|
|
|
|
|
2014-06-01 10:25:34 +00:00
|
|
|
var canvas = me.getCanvas();
|
|
|
|
if( canvas._focused_widget != nil )
|
|
|
|
canvas._focused_widget.clearFocus();
|
2013-07-27 09:54:41 +00:00
|
|
|
|
|
|
|
me._focused = 1;
|
2014-06-01 10:25:34 +00:00
|
|
|
canvas._focused_widget = me;
|
2013-07-27 09:54:41 +00:00
|
|
|
|
|
|
|
me.onFocusIn();
|
|
|
|
me._onStateChange();
|
|
|
|
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
#
|
|
|
|
clearFocus: func
|
|
|
|
{
|
|
|
|
if( !me._focused )
|
|
|
|
return me;
|
|
|
|
|
|
|
|
me._focused = 0;
|
2014-06-01 10:25:34 +00:00
|
|
|
me.getCanvas()._focused_widget = nil;
|
2013-07-27 09:54:41 +00:00
|
|
|
|
|
|
|
me.onFocusOut();
|
|
|
|
me._onStateChange();
|
|
|
|
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
onFocusIn: func {},
|
|
|
|
onFocusOut: func {},
|
|
|
|
onMouseEnter: func {},
|
|
|
|
onMouseLeave: func {},
|
|
|
|
# protected:
|
2014-06-04 15:52:48 +00:00
|
|
|
_MAX_SIZE: 32768, # size for "no size-limit"
|
2013-07-27 09:54:41 +00:00
|
|
|
_onStateChange: func {},
|
|
|
|
_setRoot: func(el)
|
|
|
|
{
|
|
|
|
me._root = el;
|
2014-06-01 10:25:34 +00:00
|
|
|
|
|
|
|
var canvas = el.getCanvas();
|
|
|
|
me.setCanvas(canvas);
|
|
|
|
|
|
|
|
canvas.addEventListener("wm.focus-in", func {
|
|
|
|
me._onStateChange();
|
|
|
|
});
|
|
|
|
canvas.addEventListener("wm.focus-out", func {
|
|
|
|
me._onStateChange();
|
|
|
|
});
|
|
|
|
|
2013-07-27 09:54:41 +00:00
|
|
|
el.addEventListener("mouseenter", func {
|
|
|
|
me._hover = 1;
|
|
|
|
me.onMouseEnter();
|
|
|
|
me._onStateChange();
|
|
|
|
});
|
|
|
|
el.addEventListener("mousedown", func {
|
|
|
|
if( bits.test(me._focus_policy, me.ClickFocus / 2) )
|
|
|
|
me.setFocus();
|
|
|
|
});
|
|
|
|
el.addEventListener("mouseleave", func {
|
|
|
|
me._hover = 0;
|
|
|
|
me.onMouseLeave();
|
|
|
|
me._onStateChange();
|
|
|
|
});
|
2014-06-01 10:25:34 +00:00
|
|
|
},
|
|
|
|
_windowFocus: func
|
|
|
|
{
|
|
|
|
var canvas = me.getCanvas();
|
|
|
|
return canvas != nil ? canvas.data("focused") : 0;
|
2013-07-27 09:54:41 +00:00
|
|
|
}
|
|
|
|
};
|