OLD | NEW |
1 -- Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 -- Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 -- Use of this source code is governed by a BSD-style license that can be | 2 -- Use of this source code is governed by a BSD-style license that can be |
3 -- found in the LICENSE file. | 3 -- found in the LICENSE file. |
4 | 4 |
5 -- Touch handling code for game engine. | 5 -- Touch handling code for game engine. |
6 -- This module defines a singe global table touch_handler which contains | 6 -- This module defines a singe global table touch_handler which contains |
7 -- a single function called TouchHandler. This functions registered | 7 -- a single function called TouchHandler. This functions registered |
8 -- as the touch handler for CCLayer in which the gameplace takes place. | 8 -- as the touch handler for CCLayer in which the gameplace takes place. |
9 | 9 |
10 local util = require 'util' | 10 local util = require 'util' |
11 | 11 |
12 local touch_handler = {} | 12 local touch_handler = {} |
13 | 13 |
14 local touch_state = { | 14 local touch_state = { |
15 touchid = -1, | 15 touchid = -1, |
16 reciever = nil, | 16 reciever = nil, |
17 is_object = false, | 17 is_object = false, |
18 } | 18 } |
19 | 19 |
20 local function FindTaggedBodiesAt(x, y) | 20 local function FindTaggedBodiesAt(x, y) |
21 local b2pos = util.b2VecFromCocos(ccp(x, y)) | 21 local b2pos = util.b2VecFromCocos(ccp(x, y)) |
22 local found_bodies = {} | 22 local found_bodies = {} |
| 23 local found_something = false |
23 local function handler(body) | 24 local function handler(body) |
| 25 found_something = true |
24 local tag = body:GetUserData() | 26 local tag = body:GetUserData() |
25 if tag ~= 0 then | 27 if tag ~= 0 then |
26 -- FindBodiesAt can report the same bodies more than | 28 -- FindBodiesAt can report the same bodies more than |
27 -- once since it reports the body for each fixture that | 29 -- once since it reports the body for each fixture that |
28 -- exists at a given point, so filter out duplicates here. | 30 -- exists at a given point, so filter out duplicates here. |
29 if not found_bodies[tag] then | 31 if not found_bodies[tag] then |
30 util.Log("got body.. " .. tag) | 32 util.Log("found body.. " .. tag) |
31 assert(level_obj.object_map[tag]) | 33 assert(level_obj.object_map[tag]) |
32 found_bodies[tag] = level_obj.object_map[tag] | 34 found_bodies[tag] = level_obj.object_map[tag] |
33 end | 35 end |
34 end | 36 end |
35 end | 37 end |
36 | 38 |
37 level_obj.layer:FindBodiesAt(b2pos, handler) | 39 level_obj.layer:FindBodiesAt(b2pos, handler) |
| 40 if found_something and #found_bodies == 0 then |
| 41 util.Log("Found untagged bodies") |
| 42 end |
38 return found_bodies | 43 return found_bodies |
39 end | 44 end |
40 | 45 |
41 local lasttap_time = 0 | 46 local lasttap_time = 0 |
42 local lasttap_count = 1 | 47 local lasttap_count = 1 |
43 local lasttap_location = nil | 48 local lasttap_location = nil |
44 | 49 |
45 touch_handler.DOUBLE_CLICK_INTERVAL = 0.250 | 50 touch_handler.DOUBLE_CLICK_INTERVAL = 0.250 |
46 touch_handler.DOUBLE_CLICK_TOLERANCE = 5 | 51 touch_handler.DOUBLE_CLICK_TOLERANCE = 5 |
47 | 52 |
48 local function OnTouchBegan(x, y, touchid) | 53 local function OnTouchBegan(x, y, touchid) |
49 -- Do double tap detection based on taps that occur within | 54 -- Do double tap detection based on taps that occur within |
50 -- DOUBLE_CLICK_INTERVAL of each other and with a certain | 55 -- DOUBLE_CLICK_INTERVAL of each other and with a certain |
51 -- distance of each other. | 56 -- distance of each other. |
52 local now = CCTime:getTime() | 57 local now = CCTime:getTime() |
53 local distance = 0 | 58 local distance = 0 |
54 if lasttap_location then | 59 if lasttap_location then |
55 distance = ccpDistance(ccp(x, y), ccp(lasttap_location[1], lasttap_locat
ion[2])) | 60 distance = ccpDistance(ccp(x, y), ccp(lasttap_location[1], lasttap_locat
ion[2])) |
56 end | 61 end |
57 if (now - lasttap_time > touch_handler.DOUBLE_CLICK_INTERVAL | 62 if (now - lasttap_time > touch_handler.DOUBLE_CLICK_INTERVAL |
58 or distance > touch_handler.DOUBLE_CLICK_TOLERANCE) then | 63 or distance > touch_handler.DOUBLE_CLICK_TOLERANCE) then |
59 lasttap_count = 1 | 64 lasttap_count = 1 |
60 else | 65 else |
61 lasttap_count = lasttap_count + 1 | 66 lasttap_count = lasttap_count + 1 |
62 end | 67 end |
63 lasttap_time = now | 68 lasttap_time = now |
64 lasttap_location = { x, y } | 69 lasttap_location = { x, y } |
65 | 70 |
66 tapcount = lasttap_count | 71 tapcount = lasttap_count |
67 util.Log('tap ' .. tapcount) | 72 util.Log('tapcount ' .. tapcount) |
68 | 73 |
69 local objects = FindTaggedBodiesAt(x, y, 0x1) | 74 local objects = FindTaggedBodiesAt(x, y, 0x1) |
70 | 75 |
71 for _, obj_def in pairs(objects) do | 76 for _, obj_def in pairs(objects) do |
72 if obj_def.script and obj_def.script.OnTouchBegan then | 77 if obj_def.script and obj_def.script.OnTouchBegan then |
73 if obj_def.script.OnTouchBegan(obj_def, x, y, tapcount) then | 78 if obj_def.script.OnTouchBegan(obj_def, x, y, tapcount) then |
74 touch_state.touchid = touchid | 79 touch_state.touchid = touchid |
75 touch_state.receiver = obj_def | 80 touch_state.receiver = obj_def |
76 touch_state.is_object = true | 81 touch_state.is_object = true |
77 return true | 82 return true |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 if touch_type == 'began' then | 142 if touch_type == 'began' then |
138 return OnTouchBegan(x, y, touchid) | 143 return OnTouchBegan(x, y, touchid) |
139 elseif touch_type == 'moved' then | 144 elseif touch_type == 'moved' then |
140 return OnTouchMoved(x, y, touchid) | 145 return OnTouchMoved(x, y, touchid) |
141 elseif touch_type == 'ended' then | 146 elseif touch_type == 'ended' then |
142 return OnTouchEnded(x, y, touchid) | 147 return OnTouchEnded(x, y, touchid) |
143 end | 148 end |
144 end | 149 end |
145 | 150 |
146 return touch_handler | 151 return touch_handler |
OLD | NEW |