forked from robterrell/cocos2d-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.js
150 lines (114 loc) · 3.3 KB
/
Menu.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict'
var util = require('util'),
Layer = require('./Layer').Layer,
Director = require('../Director').Director,
MenuItem = require('./MenuItem').MenuItem,
geom = require('geometry'), ccp = geom.ccp
/**
* @private
* @constant
*/
var kMenuStateWaiting = 0
/**
* @private
* @constant
*/
var kMenuStateTrackingTouch = 1
/**
* @class
* A fullscreen node used to render a selection of menu options
*
* @memberOf cocos.nodes
* @extends cocos.nodes.Layer
*
* @opt {cocos.nodes.MenuItem[]} items An array of MenuItems to draw on the menu
*/
function Menu (opts) {
Menu.superclass.constructor.call(this, opts)
var items = opts.items
this.isMouseEnabled = true
var s = Director.sharedDirector.winSize
this.isRelativeAnchorPoint = false
this.anchorPoint = ccp(0.5, 0.5)
this.contentSize = s
this.position = ccp(s.width / 2, s.height / 2)
if (items) {
var z = 0
items.forEach(function (item) {
this.addChild({child: item, z: z++})
}.bind(this))
}
}
Menu.inherit(Layer, /** @lends cocos.nodes.Menu# */ {
mouseDelegatePriority: (-Number.MAX_VALUE + 1),
state: kMenuStateWaiting,
selectedItem: null,
color: null,
addChild: function (opts) {
if (!opts.child instanceof MenuItem) {
throw "Menu only supports MenuItem objects as children"
}
Menu.superclass.addChild.call(this, opts)
},
itemForMouseEvent: function (event) {
var location = event.locationInCanvas
var children = this.children
for (var i = 0, len = children.length; i < len; i++) {
var item = children[i]
if (item.visible && item.isEnabled) {
var local = item.convertToNodeSpace(location)
var r = item.rect
r.origin = ccp(0, 0)
if (geom.rectContainsPoint(r, local)) {
return item
}
}
}
return null
},
mouseUp: function (event) {
var selItem = this.selectedItem
if (selItem) {
selItem.unselected()
selItem.activate()
}
if (this.state != kMenuStateWaiting) {
this.state = kMenuStateWaiting
}
if (selItem) {
return true
}
return false
},
mouseDown: function (event) {
if (this.state != kMenuStateWaiting || !this.visible) {
return false
}
var selectedItem = this.itemForMouseEvent(event)
this.selectedItem = selectedItem
if (selectedItem) {
selectedItem.selected()
this.state = kMenuStateTrackingTouch
return true
}
return false
},
mouseDragged: function (event) {
var currentItem = this.itemForMouseEvent(event)
if (currentItem != this.selectedItem) {
if (this.selectedItem) {
this.selectedItem.unselected()
}
this.selectedItem = currentItem
if (this.selectedItem) {
this.selectedItem.selected()
}
}
if (currentItem && this.state == kMenuStateTrackingTouch) {
return true
}
return false
}
})
exports.Menu = Menu
// vim:et:st=4:fdm=marker:fdl=0:fdc=1