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 -- Functions for drawing new sprites using a brush. | 5 -- Functions for drawing new sprites using a brush. |
6 -- This module defines a single 'drawing' global containing the following | 6 -- This module defines a single 'drawing' global containing the following |
7 -- functions: | 7 -- functions: |
8 -- - SetBrush | 8 -- - SetBrush |
9 -- - CreateShape | 9 -- - CreateShape |
10 -- - CreateSprite | 10 -- - CreateSprite |
(...skipping 10 matching lines...) Expand all Loading... | |
21 MODE_SELECT = 1, | 21 MODE_SELECT = 1, |
22 MODE_FREEHAND = 2, | 22 MODE_FREEHAND = 2, |
23 MODE_LINE = 3, | 23 MODE_LINE = 3, |
24 MODE_RECT = 4, | 24 MODE_RECT = 4, |
25 MODE_CIRCLE = 5, | 25 MODE_CIRCLE = 5, |
26 } | 26 } |
27 | 27 |
28 drawing.mode = drawing.MODE_FREEHAND | 28 drawing.mode = drawing.MODE_FREEHAND |
29 | 29 |
30 -- Brush information (set by SetBrush) | 30 -- Brush information (set by SetBrush) |
31 local brush_node | 31 local brush_tex |
32 local brush_thickness | 32 local brush_thickness |
33 | 33 |
34 -- Constant for grouping physics bodies | 34 -- Constant for grouping physics bodies |
35 local MAIN_CATEGORY = 0x1 | 35 local MAIN_CATEGORY = 0x1 |
36 local DRAWING_CATEGORY = 0x2 | 36 local DRAWING_CATEGORY = 0x2 |
37 | 37 |
38 -- Constants for tagging cococs nodes | |
binji
2013/05/10 16:59:01
sp: cocos
Sam Clegg
2013/05/15 20:18:43
Done.
| |
39 local TAG_BATCH_NODE = 0x1 | |
binji
2013/05/10 16:59:01
Isn't this tag already used? Not sure where but it
| |
40 | |
38 -- Local state for default touch handlers | 41 -- Local state for default touch handlers |
39 local current_shape = nil | 42 local current_shape = nil |
40 local current_tag = 99 -- util.tags.TAG_DYNAMIC_START | 43 local current_tag = 99 -- util.tags.TAG_DYNAMIC_START |
41 local start_pos = nil | 44 local start_pos = nil |
42 local last_pos = nil | 45 local last_pos = nil |
43 local brush_color = ccc3(255, 100, 100) | 46 local brush_color = ccc3(255, 100, 100) |
44 | 47 |
45 -- Callbacks that are registered for drawn objects. The game | 48 -- Callbacks that are registered for drawn objects. The game |
46 -- can register its own callbacks here to add behavior for | 49 -- can register its own callbacks here to add behavior for |
47 -- drawn objects. | 50 -- drawn objects. |
48 drawing.handlers = {} | 51 drawing.handlers = {} |
49 | 52 |
50 --- Create a b2Vec from a lua list containing 2 elements. | 53 --- Create a b2Vec from a lua list containing 2 elements. |
51 -- This is used to convert point data from .def files directly | 54 -- This is used to convert point data from .def files directly |
52 -- to the box2dx coordinate system | 55 -- to the box2dx coordinate system |
53 local function b2VecFromLua(point) | 56 local function b2VecFromLua(point) |
54 return util.b2VecFromCocos(util.PointFromLua(point)) | 57 return util.b2VecFromCocos(util.PointFromLua(point)) |
55 end | 58 end |
56 | 59 |
60 local function CreateBrushBatch(parent) | |
61 local node = CCSpriteBatchNode:createWithTexture(brush_tex) | |
62 assert(node) | |
63 parent:addChild(node, 1, TAG_BATCH_NODE) | |
64 return node | |
65 end | |
66 | |
57 --- Create a fixed pivot point between the world and the given body. | 67 --- Create a fixed pivot point between the world and the given body. |
58 local function CreatePivot(anchor, body) | 68 local function CreatePivot(anchor, body) |
59 local anchor_point = util.b2VecFromCocos(anchor) | 69 local anchor_point = util.b2VecFromCocos(anchor) |
60 | 70 |
61 -- create a new fixed body to pivot against | 71 -- create a new fixed body to pivot against |
62 local ground_def = b2BodyDef:new_local() | 72 local ground_def = b2BodyDef:new_local() |
63 ground_def.position = anchor_point | 73 ground_def.position = anchor_point |
64 local ground_body = level_obj.world:CreateBody(ground_def); | 74 local ground_body = level_obj.world:CreateBody(ground_def); |
65 | 75 |
66 -- create the pivot joint | 76 -- create the pivot joint |
67 local joint_def = b2RevoluteJointDef:new_local() | 77 local joint_def = b2RevoluteJointDef:new_local() |
68 joint_def:Initialize(ground_body, body, anchor_point) | 78 joint_def:Initialize(ground_body, body, anchor_point) |
69 local joint = level_obj.world:CreateJoint(joint_def) | 79 local joint = level_obj.world:CreateJoint(joint_def) |
70 end | 80 end |
71 | 81 |
72 local function AddShapeToBody(body, shape, sensor) | 82 local function AddShapeToBody(body, shape, sensor) |
73 local fixture_def = b2FixtureDef:new_local() | 83 local fixture_def = b2FixtureDef:new_local() |
74 fixture_def.shape = shape | 84 fixture_def.shape = shape |
75 fixture_def.density = 1.0 | 85 fixture_def.density = 1.0 |
76 fixture_def.friction = 0.5 | 86 fixture_def.friction = 0.5 |
77 fixture_def.restitution = 0.3 | 87 fixture_def.restitution = 0.3 |
78 fixture_def.isSensor = sensor | 88 fixture_def.isSensor = sensor |
79 return body:CreateFixture(fixture_def) | 89 return body:CreateFixture(fixture_def) |
80 end | 90 end |
81 | 91 |
82 local function InitPhysicsSprite(sprite, location, dynamic) | 92 local function InitPhysicsNode(node, location, dynamic, tag) |
83 local body_def = b2BodyDef:new_local() | 93 local body_def = b2BodyDef:new_local() |
84 if dynamic == true then | 94 if dynamic == true then |
85 body_def.type = b2_dynamicBody | 95 body_def.type = b2_dynamicBody |
86 end | 96 end |
87 local body = level_obj.world:CreateBody(body_def) | 97 local body = level_obj.world:CreateBody(body_def) |
88 sprite:setB2Body(body) | 98 node:setB2Body(body) |
89 sprite:setPTMRatio(util.PTM_RATIO) | 99 node:setPTMRatio(util.PTM_RATIO) |
90 sprite:setPosition(location) | 100 node:setPosition(location) |
101 node:setTag(tag) | |
102 body:SetUserData(tag) | |
103 level_obj.layer:addChild(node, 1, tag) | |
91 return body | 104 return body |
92 end | 105 end |
93 | 106 |
107 -- Create and initialise a new invisible physics node. | |
108 local function CreatePhysicsNode(location, dynamic, tag) | |
109 local node = CCPhysicsNode:create() | |
110 InitPhysicsNode(node, location, dynamic, tag) | |
111 return node | |
112 end | |
113 | |
94 local function DrawBrush(parent, location, color) | 114 local function DrawBrush(parent, location, color) |
95 local child_sprite = CCSprite:createWithTexture(brush_tex) | 115 local child_sprite = CCSprite:createWithTexture(brush_tex) |
96 child_sprite:setPosition(location) | 116 child_sprite:setPosition(location) |
97 child_sprite:setColor(color) | 117 child_sprite:setColor(color) |
98 parent:addChild(child_sprite) | 118 parent:addChild(child_sprite) |
99 end | 119 end |
100 | 120 |
101 function DrawPhysicsBrush(location, color, tag, dynamic) | |
102 local sprite = CCPhysicsSprite:createWithTexture(brush_tex) | |
103 local body = InitPhysicsSprite(sprite, location, dynamic) | |
104 sprite:setColor(color) | |
105 sprite:setTag(tag) | |
106 body:SetUserData(tag) | |
107 brush_node:addChild(sprite) | |
108 return sprite | |
109 end | |
110 | |
111 -- Add a new circle/sphere fixture to a body and return the new fixture | 121 -- Add a new circle/sphere fixture to a body and return the new fixture |
112 local function AddSphereToBody(body, location, radius, sensor) | 122 local function AddSphereToBody(body, location, radius, sensor) |
113 local sphere = b2CircleShape:new_local() | 123 local sphere = b2CircleShape:new_local() |
114 sphere.m_radius = util.ScreenToWorld(radius) | 124 sphere.m_radius = util.ScreenToWorld(radius) |
115 sphere.m_p.x = util.ScreenToWorld(location.x) - body:GetPosition().x | 125 sphere.m_p.x = util.ScreenToWorld(location.x) - body:GetPosition().x |
116 sphere.m_p.y = util.ScreenToWorld(location.y) - body:GetPosition().y | 126 sphere.m_p.y = util.ScreenToWorld(location.y) - body:GetPosition().y |
117 return AddShapeToBody(body, sphere, sensor) | 127 return AddShapeToBody(body, sphere, sensor) |
118 end | 128 end |
119 | 129 |
120 -- Add a new line/box fixture to a body and return the new fixture | 130 -- Add a new line/box fixture to a body and return the new fixture |
121 local function AddLineToShape(sprite, from, to, color) | 131 local function AddLineToShape(node, from, to, color, absolute) |
122 -- calculate length and angle of line based on start and end points | 132 -- calculate length and angle of line based on start and end points |
123 local body = sprite:getB2Body() | 133 local body = node:getB2Body() |
124 local length = ccpDistance(from, to); | 134 local length = ccpDistance(from, to); |
125 local dist_x = to.x - from.x | 135 local dist_x = to.x - from.x |
126 local dist_y = to.y - from.y | 136 local dist_y = to.y - from.y |
127 | 137 |
128 -- create fixture | 138 -- create fixture |
129 local relative_start_x = from.x - sprite:getPositionX() | 139 local start_x = from.x |
130 local relative_start_y = from.y - sprite:getPositionY() | 140 local start_y = from.y |
131 local center = b2Vec2:new_local(util.ScreenToWorld(relative_start_x + dist_x /2), | 141 if absolute then |
132 util.ScreenToWorld(relative_start_y + dist_y /2)) | 142 start_x = start_x - node:getPositionX() |
143 start_y = start_y - node:getPositionY() | |
144 end | |
145 local center = b2Vec2:new_local(util.ScreenToWorld(start_x + dist_x/2), | |
146 util.ScreenToWorld(start_y + dist_y/2)) | |
133 local shape = b2PolygonShape:new_local() | 147 local shape = b2PolygonShape:new_local() |
134 local angle = math.atan2(dist_y, dist_x) | 148 local angle = math.atan2(dist_y, dist_x) |
135 shape:SetAsBox(util.ScreenToWorld(length/2), util.ScreenToWorld(brush_thickn ess), | 149 shape:SetAsBox(util.ScreenToWorld(length/2), util.ScreenToWorld(brush_thickn ess), |
136 center, angle) | 150 center, angle) |
137 local fixture = AddShapeToBody(body, shape, false) | 151 local fixture = AddShapeToBody(body, shape, false) |
138 | 152 |
139 -- Now create the visible CCPhysicsSprite that the body is attached to | 153 -- Create sequence of sprite nodes as children |
140 sprite:setColor(color) | |
141 sprite:setPTMRatio(util.PTM_RATIO) | |
142 | |
143 -- And add a sequence of non-physics sprites as children of the first | |
144 local dist = CCPointMake(dist_x, dist_y) | 154 local dist = CCPointMake(dist_x, dist_y) |
145 local num_children = math.ceil(length / brush_step) | 155 local num_children = math.ceil(length / brush_step) |
146 local inc_x = dist_x / num_children | 156 local inc_x = dist_x / num_children |
147 local inc_y = dist_y / num_children | 157 local inc_y = dist_y / num_children |
148 local child_location = ccp(relative_start_x + brush_thickness, relative_star t_y + brush_thickness) | 158 local child_location = ccp(start_x, start_y) |
159 --if absolute then | |
binji
2013/05/10 16:59:01
remove
Sam Clegg
2013/05/15 20:18:43
Done.
| |
160 --child_location = ccp(child_location.x + brush_thickness, child_locatio n.y + brush_thickness) | |
161 --end | |
149 | 162 |
150 util.Log('Create line at: ' .. util.PointToString(from) .. ' len=' .. length .. ' num=' .. num_children) | 163 util.Log('Create line at: ' .. util.PointToString(from) .. ' len=' .. length .. ' num=' .. num_children) |
164 util.Log(util.PointToString(ccp(start_x, start_y))) | |
165 | |
166 local batch_node = node:getChildByTag(TAG_BATCH_NODE) | |
167 assert(batch_node) | |
151 for i = 1,num_children do | 168 for i = 1,num_children do |
152 child_location.x = child_location.x + inc_x | 169 child_location.x = child_location.x + inc_x |
153 child_location.y = child_location.y + inc_y | 170 child_location.y = child_location.y + inc_y |
154 DrawBrush(sprite, child_location, color) | 171 DrawBrush(batch_node, child_location, color) |
155 end | 172 end |
156 | 173 |
157 return fixture | 174 return fixture |
158 end | 175 end |
159 | 176 |
160 --- Create a line between two points. | |
161 -- Uses a sequence of brush sprites an a single box2d rect. | |
162 local function CreateLine(from, to, objdef) | |
163 -- create body | |
164 util.Log("Creating line with tag " .. objdef.tag) | |
165 | |
166 if objdef.color then | |
167 color = ccc3(objdef.color[1], objdef.color[2], objdef.color[3]) | |
168 else | |
169 color = ccc3(255, 255, 255) | |
170 end | |
171 | |
172 local sprite = drawing.DrawStartPoint(from, color, objdef.tag, objdef.dynami c) | |
173 if objdef.anchor then | |
174 CreatePivot(objdef.anchor, sprite:getB2Body()) | |
175 end | |
176 AddLineToShape(sprite, from, to, color) | |
177 return sprite | |
178 end | |
179 | |
180 -- Set the collision group for a fixture | 177 -- Set the collision group for a fixture |
181 local function SetCategory(fixture, category) | 178 local function SetCategory(fixture, category) |
182 local filter = fixture:GetFilterData() | 179 local filter = fixture:GetFilterData() |
183 filter.categoryBits = category | 180 filter.categoryBits = category |
184 filter.maskBits = category | 181 filter.maskBits = category |
185 fixture:SetFilterData(filter) | 182 fixture:SetFilterData(filter) |
186 end | 183 end |
187 | 184 |
188 --- Make the a body dynamic and put it in the default collision group | 185 --- Make the a body dynamic and put it in the default collision group |
189 local function MakeBodyDynamic(body) | 186 local function MakeBodyDynamic(body) |
190 body:SetType(b2_dynamicBody) | 187 body:SetType(b2_dynamicBody) |
191 local fixture = body:GetFixtureList() | 188 local fixture = body:GetFixtureList() |
192 while fixture do | 189 while fixture do |
193 SetCategory(fixture, MAIN_CATEGORY) | 190 SetCategory(fixture, MAIN_CATEGORY) |
194 fixture = fixture:GetNext() | 191 fixture = fixture:GetNext() |
195 end | 192 end |
196 end | 193 end |
197 | 194 |
198 --- Set brush texture for subsequent draw operations | 195 --- Set brush texture for subsequent draw operations |
199 function drawing.SetBrush(brush) | 196 function drawing.SetBrush(brush) |
200 -- calculate thickness based on brush sprite size | 197 -- calculate thickness based on brush sprite size |
201 brush_node = brush | |
202 brush_tex = brush:getTexture() | 198 brush_tex = brush:getTexture() |
203 local brush_size = brush_tex:getContentSizeInPixels() | 199 local brush_size = brush_tex:getContentSizeInPixels() |
204 brush_thickness = math.max(brush_size.height/2, brush_size.width/2) | 200 brush_thickness = math.max(brush_size.height/2, brush_size.width/2) |
205 brush_step = brush_thickness * 1.5 | 201 brush_step = brush_thickness * 1.5 |
206 end | 202 end |
207 | 203 |
204 local function AddChildShape(shape, child_def, absolute) | |
205 if child_def.color then | |
206 color = ccc3(child_def.color[1], child_def.color[2], child_def.color[3]) | |
207 else | |
208 color = ccc3(255, 255, 255) | |
209 end | |
210 | |
211 if child_def.type == 'line' then | |
212 local start = util.PointFromLua(child_def.start, not absolute) | |
213 local finish = util.PointFromLua(child_def.finish, not absolute) | |
214 if child_def.anchor then | |
215 child_def.anchor = util.PointFromLua(child_def.anchor) | |
216 end | |
217 AddLineToShape(shape, start, finish, color, absolute) | |
218 else | |
219 assert(false) | |
220 end | |
221 end | |
222 | |
208 --- Draw a shape described by a given shape def. | 223 --- Draw a shape described by a given shape def. |
209 -- This creates physics sprites and accosiated box2d bodies for | 224 -- This creates physics sprites and accosiated box2d bodies for |
210 -- the shape. | 225 -- the shape. |
211 function drawing.CreateShape(shape_def) | 226 function drawing.CreateShape(shape_def) |
212 if shape_def.type == 'line' then | 227 local shape = nil |
213 local start = util.PointFromLua(shape_def.start) | 228 |
214 local finish = util.PointFromLua(shape_def.finish) | 229 if shape_def.type == 'compound' then |
215 if shape_def.anchor then | 230 local pos = util.PointFromLua(shape_def.pos) |
216 shape_def.anchor = util.PointFromLua(shape_def.anchor) | 231 shape = CreatePhysicsNode(pos, false, shape_def.tag) |
232 CreateBrushBatch(shape) | |
233 if shape_def.children then | |
234 for _, child_def in ipairs(shape_def.children) do | |
235 child_def.tag = shape_def.tag | |
236 child = AddChildShape(shape, child_def, false) | |
237 end | |
217 end | 238 end |
218 return CreateLine(start, finish, shape_def) | 239 elseif shape_def.type == 'line' then |
240 local pos = util.PointFromLua(shape_def.start) | |
241 shape = CreatePhysicsNode(pos, false, shape_def.tag) | |
242 CreateBrushBatch(shape) | |
243 AddChildShape(shape, shape_def, true) | |
219 elseif shape_def.type == 'edge' then | 244 elseif shape_def.type == 'edge' then |
220 local body_def = b2BodyDef:new_local() | 245 local body_def = b2BodyDef:new_local() |
221 local body = level_obj.world:CreateBody(body_def) | 246 local body = level_obj.world:CreateBody(body_def) |
222 local b2shape = b2EdgeShape:new_local() | 247 local b2shape = b2EdgeShape:new_local() |
223 b2shape:Set(b2VecFromLua(shape_def.start), b2VecFromLua(shape_def.finish )) | 248 b2shape:Set(b2VecFromLua(shape_def.start), b2VecFromLua(shape_def.finish )) |
224 body:CreateFixture(b2shape, 0) | 249 body:CreateFixture(b2shape, 0) |
250 return | |
225 else | 251 else |
226 assert(false) | 252 assert(false) |
227 end | 253 end |
254 | |
255 return shape | |
228 end | 256 end |
229 | 257 |
230 --- Create a physics sprite at a fiven location with a given image | 258 --- Create a physics sprite at a fiven location with a given image |
231 function drawing.CreateSprite(sprite_def) | 259 function drawing.CreateSprite(sprite_def) |
232 local pos = util.PointFromLua(sprite_def.pos) | 260 local pos = util.PointFromLua(sprite_def.pos) |
233 -- util.Log('Create sprite [tag=' .. sprite_def.tag .. ' image=' .. sprite_d ef.image .. ']: ' .. | 261 -- util.Log('Create sprite [tag=' .. sprite_def.tag .. ' image=' .. sprite_d ef.image .. ']: ' .. |
234 -- util.PointToString(pos)) | 262 -- util.PointToString(pos)) |
235 local image = game_obj.assets[sprite_def.image] | 263 local image = game_obj.assets[sprite_def.image] |
236 local sprite = CCPhysicsSprite:create(image) | 264 local sprite = CCPhysicsSprite:create(image) |
237 local dynamic = not sprite_def.sensor | 265 local dynamic = not sprite_def.sensor |
238 local body = InitPhysicsSprite(sprite, pos, dynamic) | 266 local body = InitPhysicsNode(sprite, pos, dynamic, sprite_def.tag) |
239 body:SetUserData(sprite_def.tag) | |
240 | 267 |
241 AddSphereToBody(body, pos, sprite:boundingBox().size.height/2, sprite_def.se nsor) | 268 AddSphereToBody(body, pos, sprite:boundingBox().size.height/2, sprite_def.se nsor) |
242 return sprite | 269 return sprite |
243 end | 270 end |
244 | 271 |
245 --- Create a single circlular point with the brush. | 272 --- Create a single circlular point with the brush. |
246 -- This is used to start shapes that the use draws. The starting | 273 -- This is used to start shapes that the user draws. The returned |
247 -- point contains the box2d body for the shape. | 274 -- node is the an invisible node that acts as the physics objects. |
275 -- Sprite nodes are then attached to this as the user draws. | |
248 function drawing.DrawStartPoint(location, color, tag, dynamic) | 276 function drawing.DrawStartPoint(location, color, tag, dynamic) |
249 -- Add visible sprite | 277 -- Add invisibe physics node |
250 local sprite = DrawPhysicsBrush(location, color, tag, dynamic) | 278 local node = CreatePhysicsNode(location, dynamic, tag) |
279 CreateBrushBatch(node) | |
280 | |
281 -- Add visiable sprite | |
binji
2013/05/10 16:59:01
sp: visible
Sam Clegg
2013/05/15 20:18:43
Done.
binji
2013/05/21 21:11:36
or is it...?
Sam Clegg
2013/05/21 22:51:45
Done.
| |
282 local sprite = CCSprite:createWithTexture(brush_tex) | |
283 sprite:setColor(color) | |
284 node:addChild(sprite) | |
251 | 285 |
252 -- Add collision info | 286 -- Add collision info |
253 local fixture = AddSphereToBody(sprite:getB2Body(), location, brush_thicknes s, false) | 287 local fixture = AddSphereToBody(node:getB2Body(), location, brush_thickness, false) |
254 SetCategory(fixture, DRAWING_CATEGORY) | 288 SetCategory(fixture, DRAWING_CATEGORY) |
255 | 289 |
256 return sprite | 290 return node |
257 end | 291 end |
258 | 292 |
259 --- Create a circle composed of brush sprites backed by a single box2d | 293 --- Create a circle composed of brush sprites backed by a single box2d |
260 -- circle fixture. | 294 -- circle fixture. |
261 function drawing.DrawCircle(center, radius, color, tag) | 295 function drawing.DrawCircle(center, radius, color, tag) |
262 -- Create an initial, invisble sprite at the center, to which we | 296 -- Create the initial (invidible) node at the center |
binji
2013/05/10 16:59:01
sp: invisible
Sam Clegg
2013/05/15 20:18:43
Done.
| |
263 -- attach a sequence of visible child sprites | 297 -- and then attach a sequence of visible child sprites |
298 local node = CreatePhysicsNode(center, true, tag) | |
299 local batch_node = CreateBrushBatch(node) | |
264 | 300 |
265 local inner_radius = math.max(radius - brush_thickness, 1) | 301 local inner_radius = math.max(radius - brush_thickness, 1) |
266 local circumference = 2 * math.pi * inner_radius | 302 local circumference = 2 * math.pi * inner_radius |
267 local num_sprites = math.max(circumference / brush_step, 1) | 303 local num_sprites = math.max(circumference / brush_step, 1) |
268 local angle_delta = 2 * math.pi / num_sprites | 304 local angle_delta = 2 * math.pi / num_sprites |
269 | 305 |
270 -- The firsh brush draw is the origin of the physics object. | 306 util.Log('drawing circle: radius=' .. math.floor(radius) .. ' sprites=' .. m ath.floor(num_sprites)) |
271 local start_point = ccp(center.x + inner_radius, center.y) | 307 for angle = 0, 2 * math.pi, angle_delta do |
272 local sprite = DrawPhysicsBrush(start_point, color, tag) | |
273 | |
274 local start_offset = ccp(start_point.x - center.x, start_point.y - center.y) | |
275 | |
276 local anchor = sprite:getAnchorPointInPoints() | |
277 util.Log('drawing circle: radius=' .. math.floor(radius) .. ' sprites=' .. n um_sprites) | |
278 util.Log('drawing circle: anchor=' .. util.PointToString(anchor)) | |
279 for angle = angle_delta, 2 * math.pi, angle_delta do | |
280 x = inner_radius * math.cos(angle) | 308 x = inner_radius * math.cos(angle) |
281 y = inner_radius * math.sin(angle) | 309 y = inner_radius * math.sin(angle) |
282 local pos = ccp(x - start_offset.x + anchor.x, y - start_offset.y + anch or.y) | 310 DrawBrush(batch_node, ccp(x, y), color) |
283 DrawBrush(sprite, pos, color) | |
284 end | 311 end |
285 | 312 |
286 -- Create the box2d physics body to match the sphere. | 313 -- Create the box2d physics body to match the sphere. |
287 local fixture = AddSphereToBody(sprite:getB2Body(), center, radius, false) | 314 local fixture = AddSphereToBody(node:getB2Body(), center, radius, false) |
288 SetCategory(fixture, DRAWING_CATEGORY) | 315 SetCategory(fixture, DRAWING_CATEGORY) |
289 | 316 return node |
290 return sprite | |
291 end | 317 end |
292 | 318 |
293 function drawing.DrawEndPoint(sprite, location, color) | 319 function drawing.DrawEndPoint(node, location, color) |
294 -- Add visible sprite | 320 -- Add visible sprite |
295 local child_sprite = CCSprite:createWithTexture(brush_tex) | 321 local child_sprite = CCSprite:createWithTexture(brush_tex) |
296 local relative_x = location.x - sprite:getPositionX() | 322 local relative_x = location.x - node:getPositionX() |
297 local relative_y = location.y - sprite:getPositionY() | 323 local relative_y = location.y - node:getPositionY() |
298 local brush_size = brush_tex:getContentSizeInPixels() | 324 child_sprite:setPosition(ccp(relative_x, relative_y)) |
299 child_sprite:setPosition(ccp(relative_x + brush_size.width/2, relative_y + b rush_size.height/2)) | |
300 child_sprite:setColor(color) | 325 child_sprite:setColor(color) |
301 sprite:addChild(child_sprite) | 326 node:addChild(child_sprite) |
302 | 327 |
303 -- Add collision info | 328 -- Add collision info |
304 local body = sprite:getB2Body() | 329 local body = node:getB2Body() |
305 local fixture = AddSphereToBody(body, location, sprite:boundingBox().size.he ight/2, false) | 330 local fixture = AddSphereToBody(body, location, brush_thickness, false) |
306 SetCategory(fixture, DRAWING_CATEGORY) | 331 SetCategory(fixture, DRAWING_CATEGORY) |
307 end | 332 end |
308 | 333 |
309 function drawing.AddLineToShape(sprite, from, to, color) | 334 function drawing.AddLineToShape(sprite, from, to, color) |
310 fixture = AddLineToShape(sprite, from, to, color) | 335 fixture = AddLineToShape(sprite, from, to, color, true) |
binji
2013/05/10 16:59:01
might be clearer to use a string or constant here
| |
311 SetCategory(fixture, DRAWING_CATEGORY) | 336 SetCategory(fixture, DRAWING_CATEGORY) |
312 end | 337 end |
313 | 338 |
314 function drawing.IsDrawing() | 339 function drawing.IsDrawing() |
315 return current_shape ~= nil | 340 return current_shape ~= nil |
316 end | 341 end |
317 | 342 |
318 --- Sample OnTouchBegan for drawing-based games. For bespoke drawing behaviour | 343 --- Sample OnTouchBegan for drawing-based games. For bespoke drawing behaviour |
319 -- clone and modify this code. | 344 -- clone and modify this code. |
320 function drawing.OnTouchBegan(x, y) | 345 function drawing.OnTouchBegan(x, y) |
321 --- ignore touches if we are already drawing something | 346 --- ignore touches if we are already drawing something |
322 if drawing.IsDrawing() then | 347 if drawing.IsDrawing() then |
323 return false | 348 return false |
324 end | 349 end |
325 | 350 |
351 if drawing.mode == drawing.MODE_SELECT then | |
352 return false | |
353 end | |
354 | |
326 start_pos = ccp(x, y) | 355 start_pos = ccp(x, y) |
327 last_pos = start_pos | 356 last_pos = start_pos |
328 | 357 |
329 -- New shape def | 358 -- New shape def |
330 local shape = { | 359 local shape = { |
331 tag = current_tag, | 360 tag = current_tag, |
332 tag_str = 'drawn_shape_' .. current_tag, | 361 tag_str = 'drawn_shape_' .. current_tag, |
333 script = drawing.handlers, | 362 script = drawing.handlers, |
334 } | 363 } |
335 | 364 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
368 if length > brush_thickness * 2 then | 397 if length > brush_thickness * 2 then |
369 drawing.AddLineToShape(current_shape.node, last_pos, new_pos, brush_ color) | 398 drawing.AddLineToShape(current_shape.node, last_pos, new_pos, brush_ color) |
370 last_pos = new_pos | 399 last_pos = new_pos |
371 end | 400 end |
372 elseif drawing.mode == drawing.MODE_LINE then | 401 elseif drawing.mode == drawing.MODE_LINE then |
373 local tag = current_shape.node:getTag() | 402 local tag = current_shape.node:getTag() |
374 drawing.DestroySprite(current_shape.node) | 403 drawing.DestroySprite(current_shape.node) |
375 | 404 |
376 current_shape.node = drawing.DrawStartPoint(start_pos, brush_color, tag) | 405 current_shape.node = drawing.DrawStartPoint(start_pos, brush_color, tag) |
377 drawing.AddLineToShape(current_shape.node, start_pos, new_pos, brush_col or) | 406 drawing.AddLineToShape(current_shape.node, start_pos, new_pos, brush_col or) |
378 | |
379 elseif drawing.mode == drawing.MODE_CIRCLE then | 407 elseif drawing.mode == drawing.MODE_CIRCLE then |
380 local tag = current_shape.node:getTag() | 408 local tag = current_shape.node:getTag() |
381 drawing.DestroySprite(current_shape.node) | 409 drawing.DestroySprite(current_shape.node) |
382 radius = ccpDistance(start_pos, new_pos) | 410 radius = ccpDistance(start_pos, new_pos) |
383 current_shape.node = drawing.DrawCircle(start_pos, radius, brush_color, tag) | 411 current_shape.node = drawing.DrawCircle(start_pos, radius, brush_color, tag) |
384 else | 412 else |
385 error('invalid drawing mode: ' .. tostring(drawing.mode)) | 413 error('invalid drawing mode: ' .. tostring(drawing.mode)) |
386 end | 414 end |
387 end | 415 end |
388 | 416 |
(...skipping 18 matching lines...) Expand all Loading... | |
407 MakeBodyDynamic(current_shape.node:getB2Body()) | 435 MakeBodyDynamic(current_shape.node:getB2Body()) |
408 | 436 |
409 local rtn = current_shape | 437 local rtn = current_shape |
410 last_pos = nil | 438 last_pos = nil |
411 start_pos = nil | 439 start_pos = nil |
412 current_shape = nil | 440 current_shape = nil |
413 return rtn | 441 return rtn |
414 end | 442 end |
415 | 443 |
416 return drawing | 444 return drawing |
OLD | NEW |