-
Notifications
You must be signed in to change notification settings - Fork 0
/
DraggableContainer.js
84 lines (80 loc) · 1.92 KB
/
DraggableContainer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(function (window)
{
/** Construct boundaries for this object*/
var DraggableContainer = function()
{
this.initialize();
}
var p = DraggableContainer.prototype = new Container();
// public properties
p.mouseEventsEnabled = true;
p.Container_initialize = p.initialize;
p.Container_tick = p._tick;
p.initialize = function()
{
this.Container_initialize();
this.min_x = 0;
this.min_y = 0;
this.max_x = 0;
this.max_y = 0;
this.viewable_width = 0;
this.viewable_height = 0;
this.areBoundsSet = false;
}
p._tick = function ()
{
this.Container_tick();
}
/** Setup dragging bounds. Prerequisite for dragging */
p.setBounds = function (rect)
{
this.areBoundsSet = true;
this.min_x = rect.x;
this.min_y = rect.y;
this.max_x = rect.x+rect.width - this.viewable_width;
this.max_y = rect.y+rect.height - this.viewable_height;
(function(target)
{
target.onPress = function (evt)
{
this.setDefaultView();
releaseObjectFromContainer (this);
var offset = {x:this.x-evt.stageX, y:this.y-evt.stageY}
evt.onMouseMove = function (ev)
{
var newX = ev.stageX+offset.x;
var newY = ev.stageY+offset.y;
if (newX < this.target.min_x)
{
this.target.x = this.target.min_x;
} else if (newX > this.target.max_x)
{
this.target.x = this.target.max_x;
} else
{
this.target.x = newX;
}
if (newY < this.target.min_y)
{
this.target.y = this.target.min_y;
} else if (newY > this.target.max_y)
{
this.target.y = this.target.max_y;
} else
{
this.target.y = newY;
}
update = true;
}
evt.onMouseUp = function (ev)
{
placeObjectInContainer(this.target);
}
}
}(DraggableContainer.prototype));
}
/** Sub-classes need to have the switch view method */
p.switchView = function (){}
p.setDefaultView = function (){}
window.DraggableContainer = DraggableContainer;
}(window));