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 | function setupTips(params)
local targetNode = params.target_node
local tips = params.tips
local excludeNodes = params.exclude_nodes or {}
local DEFAULT_TIPS_DIST = 10
local TIPS_ZORDER = 1000
if tolua.isnull(targetNode) or tolua.isnull(tips) then
return
end
tips:setVisible(false)
targetNode:setTouchEnabled(false)
display.getRunningScene():addChild(tips, TIPS_ZORDER)
targetNode:addNodeEventListener(cc.NODE_EVENT, function(event)
if event.name == "exit" then
local scene = display.getRunningScene()
scene:performWithDelay(MyPackage.callbackWrapper({scene}, function()
if not tolua.isnull(tips) then
tips:setVisible(false)
end
end), 0)
local eventDispatcher = display.getRunningScene():getEventDispatcher()
eventDispatcher:removeEventListenersForTarget(targetNode)
end
end)
------------------------------------------------------------
local function setTipsPosition()
local leftBottomPos = MyPackage.getPositionOfNode(targetNode, display.LEFT_BOTTOM)
local targetNodePos = display.getRunningScene():convertToNodeSpace(targetNode:getParent():convertToWorldSpace(leftBottomPos))
local targetNodeAnchorPoint = targetNode:getAnchorPoint()
local tipsPos = targetNodePos
local tipsAnchorPoint = cc.p(0, 0)
local director = cc.Director:getInstance()
local glView = director:getOpenGLView()
local frameSize = glView:getFrameSize()
local viewSize = director:getVisibleSize()
if targetNodePos.x <= frameSize.width * 0.5 / glView:getScaleX() then
-- show tips on the right of the targetNode if the targetNode is on the left screen
tipsPos.x = tipsPos.x + targetNode:getContentSize().width + DEFAULT_TIPS_DIST
else
-- show tips on the left of the targetNode otherwises
tipsPos.x = tipsPos.x - DEFAULT_TIPS_DIST
tipsAnchorPoint.x = 1
end
if targetNodePos.y + tips:getContentSize().height > viewSize.height then
tipsPos.y = viewSize.height
tipsAnchorPoint.y = 1
end
if targetNodePos.y < 0 then
targetNodePos.y = 0
end
tips:ignoreAnchorPointForPosition(false)
tips:setAnchorPoint(tipsAnchorPoint)
tips:setPosition(tipsPos)
end
------------------------------------------------------------
local function activeFunc()
local scene = display.getRunningScene()
-- NOTE: delay util the next frame in order to get the correct WorldSpace position
scene:performWithDelay(MyPackage.callbackWrapper({scene, tips}, function()
setTipsPosition()
tips:setVisible(true)
end), 0)
end
local function inactiveFunc()
if not tolua.isnull(tips) then
tips:setVisible(false)
end
end
local function isTouchInNode(touch, node)
if tolua.isnull(node) or tolua.isnull(touch) then
return false
end
local localLocation = node:convertToNodeSpace(touch:getLocation())
local width = node:getContentSize().width
local height = node:getContentSize().height
local rect = cc.rect(0, 0, width, height)
return getCascadeVisibility(node) and node:isRunning() and cc.rectContainsPoint(rect, localLocation)
end
local function isActive(touch)
local isExcluded = false
for _, excludeNode in ipairs(excludeNodes) do
isExcluded = isExcluded or isTouchInNode(touch, excludeNode)
end
return isTouchInNode(touch, targetNode) and not isExcluded
end
local function onTouchBegan(touch, event)
if isActive(touch) then
activeFunc()
return true
else
return false
end
end
local function onTouchMoved(touch, event)
local scene = display.getRunningScene()
scene:performWithDelay(MyPackage.callbackWrapper({scene}, function()
if isActive(touch) then
activeFunc()
else
inactiveFunc()
end
end), 0)
end
local function onTouchEnded(touch, event)
local scene = display.getRunningScene()
scene:performWithDelay(MyPackage.callbackWrapper({scene}, function()
inactiveFunc()
end), 0)
end
local listener = cc.EventListenerTouchOneByOne:create()
listener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
listener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED)
listener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED)
listener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_CANCELLED)
local eventDispatcher = display.getRunningScene():getEventDispatcher()
eventDispatcher:removeEventListenersForTarget(targetNode)
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, targetNode)
end
|