2013-01-22 16:47:51 +00:00
|
|
|
var Window = {
|
2012-08-01 23:14:18 +00:00
|
|
|
# Constructor
|
|
|
|
#
|
2013-01-22 16:47:51 +00:00
|
|
|
# @param size ([width, height])
|
2013-06-06 20:46:37 +00:00
|
|
|
new: func(size, type = nil, id = nil)
|
2012-08-01 23:14:18 +00:00
|
|
|
{
|
2013-06-06 20:46:37 +00:00
|
|
|
var ghost = _newWindowGhost(id);
|
2012-08-01 23:14:18 +00:00
|
|
|
var m = {
|
2013-06-06 20:46:37 +00:00
|
|
|
parents: [Window, PropertyElement, ghost],
|
|
|
|
_node: props.wrapNode(ghost._node_ghost)
|
2012-08-01 23:14:18 +00:00
|
|
|
};
|
2013-06-06 20:46:37 +00:00
|
|
|
|
2013-01-22 16:47:51 +00:00
|
|
|
m.setInt("size[0]", size[0]);
|
|
|
|
m.setInt("size[1]", size[1]);
|
2012-08-01 23:14:18 +00:00
|
|
|
|
2013-06-06 20:46:37 +00:00
|
|
|
# TODO better default position
|
|
|
|
m.move(0,0);
|
|
|
|
|
2012-12-14 16:25:04 +00:00
|
|
|
# arg = [child, listener_node, mode, is_child_event]
|
|
|
|
setlistener(m._node, func m._propCallback(arg[0], arg[2]), 0, 2);
|
2013-06-06 20:46:37 +00:00
|
|
|
if( type )
|
|
|
|
m.set("type", type);
|
2012-12-14 16:25:04 +00:00
|
|
|
|
2012-08-01 23:14:18 +00:00
|
|
|
return m;
|
|
|
|
},
|
2013-01-01 12:16:19 +00:00
|
|
|
# Destructor
|
|
|
|
del: func
|
|
|
|
{
|
2013-06-06 20:46:37 +00:00
|
|
|
me._node.remove();
|
2013-06-08 11:07:16 +00:00
|
|
|
me._node = nil;
|
|
|
|
|
2013-01-01 12:16:19 +00:00
|
|
|
if( me["_canvas"] != nil )
|
2013-06-08 11:07:16 +00:00
|
|
|
{
|
2013-01-01 12:16:19 +00:00
|
|
|
me._canvas.del();
|
2013-06-08 11:07:16 +00:00
|
|
|
me._canvas = nil;
|
|
|
|
}
|
2013-01-01 12:16:19 +00:00
|
|
|
},
|
2013-01-22 16:47:51 +00:00
|
|
|
# Create the canvas to be used for this Window
|
2012-08-01 23:14:18 +00:00
|
|
|
#
|
|
|
|
# @return The new canvas
|
|
|
|
createCanvas: func()
|
|
|
|
{
|
2013-01-22 16:47:51 +00:00
|
|
|
var size = [
|
2012-08-01 23:14:18 +00:00
|
|
|
me.get("size[0]"),
|
|
|
|
me.get("size[1]")
|
|
|
|
];
|
|
|
|
|
|
|
|
me._canvas = new({
|
2013-01-22 16:47:51 +00:00
|
|
|
size: [2 * size[0], 2 * size[1]],
|
|
|
|
view: size,
|
2012-08-01 23:14:18 +00:00
|
|
|
placement: {
|
|
|
|
type: "window",
|
2013-06-14 18:29:39 +00:00
|
|
|
id: me.get("id")
|
2013-07-13 10:52:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
# Standard alpha blending
|
|
|
|
"blend-source-rgb": "src-alpha",
|
|
|
|
"blend-destination-rgb": "one-minus-src-alpha",
|
|
|
|
|
|
|
|
# Just keep current alpha (TODO allow using rgb textures instead of rgba?)
|
|
|
|
"blend-source-alpha": "zero",
|
|
|
|
"blend-destination-alpha": "one"
|
2012-08-01 23:14:18 +00:00
|
|
|
});
|
2013-01-22 16:47:51 +00:00
|
|
|
|
|
|
|
me._canvas.addEventListener("mousedown", func me.raise());
|
|
|
|
return me._canvas;
|
2012-08-01 23:14:18 +00:00
|
|
|
},
|
2013-01-22 16:47:51 +00:00
|
|
|
# Set an existing canvas to be used for this Window
|
2012-08-01 23:14:18 +00:00
|
|
|
setCanvas: func(canvas_)
|
|
|
|
{
|
|
|
|
if( !isa(canvas_, canvas.Canvas) )
|
|
|
|
return debug.warn("Not a canvas.Canvas");
|
|
|
|
|
|
|
|
canvas_.addPlacement({type: "window", index: me._node.getIndex()});
|
|
|
|
me['_canvas'] = canvas_;
|
|
|
|
},
|
|
|
|
# Get the displayed canvas
|
|
|
|
getCanvas: func()
|
|
|
|
{
|
|
|
|
return me['_canvas'];
|
|
|
|
},
|
2013-06-06 20:46:37 +00:00
|
|
|
getCanvasDecoration: func()
|
|
|
|
{
|
|
|
|
return wrapCanvas(me._getCanvasDecoration());
|
|
|
|
},
|
2012-08-01 23:14:18 +00:00
|
|
|
setPosition: func(x, y)
|
|
|
|
{
|
2013-06-06 20:46:37 +00:00
|
|
|
me.setInt("tf/t[0]", x);
|
|
|
|
me.setInt("tf/t[1]", y);
|
2012-08-01 23:14:18 +00:00
|
|
|
},
|
|
|
|
move: func(x, y)
|
|
|
|
{
|
2013-06-06 20:46:37 +00:00
|
|
|
me.setInt("tf/t[0]", me.get("tf/t[0]", 10) + x);
|
|
|
|
me.setInt("tf/t[1]", me.get("tf/t[1]", 30) + y);
|
2012-08-01 23:14:18 +00:00
|
|
|
},
|
|
|
|
# Raise to top of window stack
|
|
|
|
raise: func()
|
|
|
|
{
|
2013-06-14 18:29:39 +00:00
|
|
|
# on writing the z-index the window always is moved to the top of all other
|
|
|
|
# windows with the same z-index.
|
|
|
|
me.setInt("z-index", me.get("z-index", 0));
|
2012-12-14 16:25:04 +00:00
|
|
|
},
|
|
|
|
# private:
|
|
|
|
_propCallback: func(child, mode)
|
|
|
|
{
|
2013-06-06 20:46:37 +00:00
|
|
|
if( !me._node.equals(child.getParent()) )
|
|
|
|
return;
|
|
|
|
var name = child.getName();
|
|
|
|
|
2012-12-14 16:25:04 +00:00
|
|
|
# support for CSS like position: absolute; with right and/or bottom margin
|
2013-06-06 20:46:37 +00:00
|
|
|
if( name == "right" )
|
|
|
|
me._handlePositionAbsolute(child, mode, name, 0);
|
|
|
|
else if( name == "bottom" )
|
|
|
|
me._handlePositionAbsolute(child, mode, name, 1);
|
|
|
|
|
|
|
|
# update decoration on type change
|
|
|
|
else if( name == "type" )
|
|
|
|
{
|
|
|
|
if( mode == 0 )
|
|
|
|
settimer(func me._updateDecoration(), 0);
|
|
|
|
}
|
2012-12-14 16:25:04 +00:00
|
|
|
},
|
|
|
|
_handlePositionAbsolute: func(child, mode, name, index)
|
|
|
|
{
|
|
|
|
# mode
|
|
|
|
# -1 child removed
|
|
|
|
# 0 value changed
|
|
|
|
# 1 child added
|
|
|
|
|
|
|
|
if( mode == 0 )
|
|
|
|
me._updatePos(index, name);
|
|
|
|
else if( mode == 1 )
|
|
|
|
me["_listener_" ~ name] = [
|
|
|
|
setlistener
|
|
|
|
(
|
|
|
|
"/sim/gui/canvas/size[" ~ index ~ "]",
|
|
|
|
func me._updatePos(index, name)
|
|
|
|
),
|
|
|
|
setlistener
|
|
|
|
(
|
|
|
|
me._node.getNode("size[" ~ index ~ "]"),
|
|
|
|
func me._updatePos(index, name)
|
|
|
|
)
|
|
|
|
];
|
|
|
|
else if( mode == -1 )
|
|
|
|
for(var i = 0; i < 2; i += 1)
|
|
|
|
removelistener(me["_listener_" ~ name][i]);
|
|
|
|
},
|
|
|
|
_updatePos: func(index, name)
|
|
|
|
{
|
|
|
|
me.setInt
|
|
|
|
(
|
2013-06-06 20:46:37 +00:00
|
|
|
"tf/t[" ~ index ~ "]",
|
2012-12-14 16:25:04 +00:00
|
|
|
getprop("/sim/gui/canvas/size[" ~ index ~ "]")
|
|
|
|
- me.get(name)
|
|
|
|
- me.get("size[" ~ index ~ "]")
|
|
|
|
);
|
2013-06-06 20:46:37 +00:00
|
|
|
},
|
|
|
|
_updateDecoration: func()
|
|
|
|
{
|
|
|
|
var border_radius = 9;
|
|
|
|
me.set("decoration-border", "25 1 1");
|
|
|
|
me.set("shadow-inset", int((1 - math.cos(45 * D2R)) * border_radius + 0.5));
|
|
|
|
me.set("shadow-radius", 5);
|
2013-06-08 11:07:16 +00:00
|
|
|
me.setBool("update", 1);
|
2013-06-06 20:46:37 +00:00
|
|
|
|
|
|
|
var canvas_deco = me.getCanvasDecoration();
|
|
|
|
canvas_deco.addEventListener("mousedown", func me.raise());
|
|
|
|
canvas_deco.set("blend-source-rgb", "src-alpha");
|
|
|
|
canvas_deco.set("blend-destination-rgb", "one-minus-src-alpha");
|
|
|
|
canvas_deco.set("blend-source-alpha", "one");
|
|
|
|
canvas_deco.set("blend-destination-alpha", "one");
|
|
|
|
|
|
|
|
var group_deco = canvas_deco.getGroup("decoration");
|
|
|
|
var title_bar = group_deco.createChild("group", "title_bar");
|
|
|
|
title_bar
|
|
|
|
.rect( 0, 0,
|
|
|
|
me.get("size[0]"),
|
|
|
|
me.get("size[1]"), #25,
|
|
|
|
{"border-top-radius": border_radius} )
|
|
|
|
.setColorFill(0.25,0.24,0.22)
|
|
|
|
.setStrokeLineWidth(0);
|
|
|
|
|
|
|
|
var style_dir = "gui/styles/AmbianceClassic/";
|
|
|
|
|
|
|
|
# close icon
|
|
|
|
var x = 10;
|
|
|
|
var y = 3;
|
|
|
|
var w = 19;
|
|
|
|
var h = 19;
|
|
|
|
var ico = title_bar.createChild("image", "icon-close")
|
|
|
|
.set("file", style_dir ~ "close_focused_normal.png")
|
|
|
|
.setTranslation(x,y);
|
|
|
|
ico.addEventListener("click", func me.del());
|
|
|
|
ico.addEventListener("mouseover", func ico.set("file", style_dir ~ "close_focused_prelight.png"));
|
|
|
|
ico.addEventListener("mousedown", func ico.set("file", style_dir ~ "close_focused_pressed.png"));
|
|
|
|
ico.addEventListener("mouseout", func ico.set("file", style_dir ~ "close_focused_normal.png"));
|
|
|
|
|
|
|
|
# title
|
|
|
|
me._title = title_bar.createChild("text", "title")
|
|
|
|
.set("alignment", "left-center")
|
|
|
|
.set("character-size", 14)
|
|
|
|
.set("font", "LiberationFonts/LiberationSans-Bold.ttf")
|
2013-06-06 22:20:09 +00:00
|
|
|
.setTranslation( int(x + 1.5 * w + 0.5),
|
|
|
|
int(y + 0.5 * h + 0.5) );
|
2013-06-06 20:46:37 +00:00
|
|
|
|
|
|
|
var title = me.get("title", "Canvas Dialog");
|
|
|
|
me._node.getNode("title", 1).alias(me._title._node.getPath() ~ "/text");
|
|
|
|
me.set("title", title);
|
2013-06-29 12:43:06 +00:00
|
|
|
|
|
|
|
title_bar.addEventListener("drag", func(e) {
|
|
|
|
if( !ico.equals(e.target) )
|
|
|
|
me.move(e.deltaX, e.deltaY);
|
|
|
|
});
|
2012-08-01 23:14:18 +00:00
|
|
|
}
|
|
|
|
};
|