Repository navigation
Expand file tree
/
Copy pathdmc_dragdrop.lua
More file actions
447 lines (336 loc) · 11 KB
/
Copy pathdmc_dragdrop.lua
File metadata and controls
447 lines (336 loc) · 11 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
--====================================================================--
-- dmc_dragdrop.lua
--
-- by David McCuskey
-- Documentation: http://docs.davidmccuskey.com/display/docs/dmc_dragdrop.lua
--====================================================================--
--[[
Copyright (C) 2011 David McCuskey. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in the
Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
--]]
-- =========================================================
-- Imports
-- =========================================================
-- import DMC Objects file
local Objects = require( "dmc_objects" )
-- setup some aliases to make code cleaner
local inheritsFrom = Objects.inheritsFrom
local Object = Objects.Object
local coronaMetaTable = getmetatable( display.getCurrentStage() )
--- Returns true for any object returned from display.new*().
-- note that all Corona types seem to share the same metatable...
local isDisplayObject = function( object )
return type( object ) == "table" and getmetatable( object ) == coronaMetaTable
end
local color_blue = { 25, 100, 255 }
local color_lightblue = { 90, 170, 255 }
local color_green = { 50, 255, 50 }
local color_lightgreen = { 170, 225, 170 }
local color_red = { 255, 50, 50 }
local color_lightred = { 255, 120, 120 }
local color_grey = { 180, 180, 180 }
local color_lightgrey = { 200, 200, 200 }
-- createSquare()
--
-- function to help create shapes, useful for drag/drop target examples
--
local function createSquare( size, color )
s = display.newRect(0, 0, unpack( size ) )
s.strokeWidth = 3
s:setFillColor( unpack( color ) )
s:setStrokeColor( unpack( color_grey ) )
return s
end
--====================================================================--
-- Drag Drop class
--====================================================================--
local DragDrop = inheritsFrom( Object )
DragDrop.NAME = "Drag Drop"
DragDrop._registered = {} -- hash of all registered objects
DragDrop._onDragStart = {} -- list of registered objects with dragStart()
DragDrop._onDragStop = {} -- list of registered objects with dragStop()
-- property name holding Corona display object
DragDrop.DISPLAY_PROPERTY = "display"
DragDrop.ANIMATE_TIME_SLOW = 300
DragDrop.ANIMATE_TIME_FAST = 100
-- Drag Targets Info
-- a hash, indexed by drag target source
DragDrop._drag_targets = {}
-- Drop Target Info
-- drop_target: object being dropped on, Display Object
-- drop_target: dropped point is selected, Boolean
DragDrop.drop_target = nil
DragDrop.drop_target_accept = false
function DragDrop:setDisplayProperty( name )
self.DISPLAY_PROPERTY = name
end
function DragDrop:register( obj, params )
-- create our storage data structure for the object
local ds = {}
ds.obj = obj
ds.call_with_object = false -- whether we call using object
if not params then
ds.call_with_object = true
t = obj
else
ds.call_with_object = false
t = params
end
ds.dragStart = t.dragStart or nil
ds.dragEnter = t.dragEnter or nil
ds.dragOver = t.dragOver or nil
ds.dragDrop = t.dragDrop or nil
ds.dragExit = t.dragExit or nil
ds.dragStop = t.dragStop or nil
self._registered[ obj ] = ds
-- save for lookup optimization
if ds.dragStart then
table.insert( self._onDragStart, obj )
end
if ds.dragStop then
table.insert( self._onDragStop, obj )
end
end
function DragDrop:doDrag( drag_orgin, event, drag_op_info )
--== process Drag Target Info params
local drag_op_info = drag_op_info or {}
local size = {
drag_orgin.width,
drag_orgin.height
}
local drag_proxy = drag_op_info.proxy or createSquare( size, color_lightgrey );
--== Create data structure for this drag operation
--[[
source: dragged item, Display Object / dmc_object
origin: dragged item origin object, Dislay Object
format: dragged item data format, String
data: any data to be sent around, <any>
x_offset: dragged item x-offset from touch center, Integer
y_offset: dragged item y-offset from touch center, Integer
alpha: dragged item alpha, Number
--]]
local drag_info = {}
drag_info.proxy = drag_proxy
drag_info.origin = drag_orgin
drag_info.format = drag_op_info.format
drag_info.data = drag_op_info.data
drag_info.x_offset = drag_op_info.xOffset or 0
drag_info.y_offset = drag_op_info.yOffset or 0
drag_info.alpha = drag_op_info.alpha or 0.5
DragDrop._drag_targets[ drag_proxy ] = drag_info
--== Update the Drag Target visual item
drag_proxy.x = event.x + drag_info.x_offset
drag_proxy.y = event.y + drag_info.y_offset
drag_proxy.alpha = drag_info.alpha
--== Start our drag operation
drag_proxy.__is_dmc_drag = true
display.getCurrentStage():setFocus( drag_proxy )
self:_doDragStart( drag_proxy )
self:_startListening( drag_proxy )
end
function DragDrop:_createEventStructure( obj, drag_info )
local drag_info = drag_info or {}
local e = {
target = obj,
format = drag_info.format,
data = drag_info.data,
}
return e
end
function DragDrop:_doDragStart( drag_proxy )
local drag_info = DragDrop._drag_targets[ drag_proxy ]
local dragList = self._onDragStart
for i=1, #dragList do
local o = dragList[ i ]
local ds = self._registered[ o ]
local f = ds.dragStart
local e = self:_createEventStructure( o, drag_info )
if ds.call_with_object then
f( o, e )
else
f( e )
end
end
end
function DragDrop:_doDragStop( drag_proxy )
local drag_info = DragDrop._drag_targets[ drag_proxy ]
local dragList = self._onDragStop
for i=1, #dragList do
local o = dragList[ i ]
local ds = self._registered[ o ]
local f = ds.dragStop
local e = self:_createEventStructure( o, drag_info )
if ds.call_with_object then
f( o, e )
else
f( e )
end
end
end
function DragDrop:acceptDragDrop()
DragDrop.drop_target_accept = true
end
function DragDrop:touch( e )
local proxy = e.target
local phase = e.phase
local result = false
local drag_info = DragDrop._drag_targets[ proxy ]
if proxy.__is_dmc_drag then
if ( phase == "moved" ) then
-- keep the dragged item moving with the touch coordinates
proxy.x = e.x + drag_info.x_offset
proxy.y = e.y + drag_info.y_offset
-- see if we are over any drop targets
local newDropTarget = self:_searchDropTargets( e.x, e.y )
if DragDrop.drop_target == newDropTarget then
-- same object, so call dragOver()
if DragDrop.drop_target and DragDrop.drop_target_accept then
local o = newDropTarget
local ds = self._registered[ o ]
local f = ds.dragOver
if f then
local e = self:_createEventStructure( o, drag_info )
if ds.call_with_object then
result = f( o, e )
else
result = f( e )
end
end
end
elseif DragDrop.drop_target ~= newDropTarget then
-- new target is nil,
-- we exited, so call dragExit()
if DragDrop.drop_target and DragDrop.drop_target_accept then
local o = DragDrop.drop_target
local ds = self._registered[ o ]
local f = ds.dragExit
if f then
local e = self:_createEventStructure( o, drag_info )
if ds.call_with_object then
result = f( o, e )
else
result = f( e )
end
end
end
self.drop_target_accept = false
-- call dragEnter on newDropTarget
if newDropTarget then
local o = newDropTarget
local ds = self._registered[ newDropTarget ]
local f = ds.dragEnter
if f then
local e = self:_createEventStructure( o, drag_info )
if ds.call_with_object then
result = f( ds.obj, e )
else
result = f( e )
end
end
end
end
DragDrop.drop_target = newDropTarget
elseif phase == "ended" or phase == "cancelled" then
local func
if phase == "ended" then
if DragDrop.drop_target and DragDrop.drop_target_accept then
-- same object, so call dragDrop()
local o = DragDrop.drop_target
local ds = self._registered[ o ]
local f = ds.dragDrop
if f then
local e = self:_createEventStructure( o, drag_info )
if ds.call_with_object then
result = f( o, e )
else
result = f( e )
end
end
-- keep on Drop Target, and shrink
func = self:_createEndAnimation( { x=o.x, y=o.y,
time=DragDrop.ANIMATE_TIME_FAST,
resize=true, drag_proxy=proxy
})
else
-- drop not accepted, so move back to drag origin
func = self:_createEndAnimation({ x=drag_info.origin.x,
y=drag_info.origin.y, time=DragDrop.ANIMATE_TIME_SLOW,
resize=false, drag_proxy=proxy
})
end
end
self.drop_target = nil
self.drop_target_accept = false
self:_doDragStop( proxy )
self:_stopListening( proxy )
func()
proxy.__is_dmc_drag = nil
display.getCurrentStage():setFocus( nil )
end
end
return result
end
function DragDrop:_createEndAnimation( params )
local params = params or {}
local default_transition_time = 600
local fDelete, doFunc
local drag_proxy = params.drag_proxy
-- create final removal
fDelete = function( e )
local dt = drag_proxy
DragDrop._drag_targets[ dt ] = nil
dt:removeSelf()
end
-- move and/or shrink the object
local p = {}
p.onComplete = fDelete
p.time = params.time or default_transition_time
p.x = params.x
p.y = params.y
if params.resize then
p.width = 10 ; p.height = 10
end
doFunc = function( e )
transition.to( drag_proxy, p )
end
return doFunc
end
function DragDrop:_startListening( drag_proxy )
local drag_info = DragDrop._drag_targets[ drag_proxy ]
drag_info.proxy:addEventListener( "touch", self )
end
function DragDrop:_stopListening( drag_proxy )
local drag_info = DragDrop._drag_targets[ drag_proxy ]
drag_info.proxy:removeEventListener( "touch", self )
end
function DragDrop:_searchDropTargets( x, y )
local dropList = self._registered
for k, _ in pairs( dropList ) do
local o = k
if not isDisplayObject( k ) then
o = k[ DragDrop.DISPLAY_PROPERTY ]
if o == nil then
print( "\nWARNING: object not of type Corona Display nor does it have display property '" .. DragDrop.DISPLAY_PROPERTY .. "'\n" )
end
end
local bounds = o.contentBounds
local isWithinBounds =
bounds.xMin <= x and bounds.xMax >= x and bounds.yMin <= y and bounds.yMax >= y
if isWithinBounds then return k end
end
return nil
end
return DragDrop