-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.lua
296 lines (249 loc) · 10 KB
/
service.lua
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
local M = {}
M.GetSystemInfo = function()
local mySystem = system.getInfo( "platform" )
print("system is " .. mySystem)
return mySystem
end
M.GetSystemInfo()
local function doesFileExistSB( fname, path )
local results = false
-- Path for the file
local filePath = system.pathForFile( fname, path )
print("file path is "..filePath)
if ( filePath ) then
local file, errorString = io.open( filePath, "r" )
if not file then
-- Error occurred; output the cause
print( "File error: " .. errorString )
else
-- File exists!
print( "File found: " .. fname )
results = true
-- Close the file handle
file:close()
end
end
return results
end
local function doesFileExist( fname, path )
local results = false
-- Path for the file
local filePath = path..fname
--print("file path is "..filePath)
if ( filePath ) then
local file, errorString = io.open( filePath, "r" )
if not file then
-- Error occurred; output the cause
print( "File error: " .. errorString )
else
-- File exists!
print( "File found: " .. fname )
results = true
-- Close the file handle
file:close()
end
end
return results
end
M.copyFileToSB = function( srcName, srcPath, dstName, dstPath, overwrite )
local results = false
print("file name" .. srcName )
local fileExists = doesFileExist( srcName, srcPath )
if ( fileExists == false ) then
return nil -- nil = Source file not found
end
-- Check to see if destination file already exists
if not ( overwrite ) then
if ( doesFileExistSB( dstName, dstPath ) ) then
return 1 -- 1 = File already exists (don't overwrite)
end
end
-- Copy the source file to the destination file
local rFilePath = srcPath..srcName
local wFilePath = system.pathForFile( dstName, dstPath )
local rfh = io.open( rFilePath, "rb" )
local wfh, errorString = io.open( wFilePath, "wb" )
if not ( wfh ) then
-- Error occurred; output the cause
native.showAlert("ERROR", "File error: " .. errorString)
print( "File error: " .. errorString )
return false
else
-- Read the file and write to the destination directory
local data = rfh:read( "*a" )
if not ( data ) then
print( "Read error!" )
return false
else
if not ( wfh:write( data ) ) then
print( "Write error!" )
return false
end
end
end
results = 2 -- 2 = File copied successfully!
-- Close file handles
rfh:close()
wfh:close()
return results
end
M.copyFileFromSB = function( srcName, srcPath, dstName, dstPath, overwrite )
local results = false
local fileExists = doesFileExistSB( srcName, srcPath )
if ( fileExists == false ) then
return nil -- nil = Source file not found
end
-- Check to see if destination file already exists
if not ( overwrite ) then
if ( doesFileExist( dstName, dstPath ) ) then
return 1 -- 1 = File already exists (don't overwrite)
end
end
-- Copy the source file to the destination file
local rFilePath = system.pathForFile( srcName, srcPath )
local wFilePath = dstPath..dstName
local rfh = io.open( rFilePath, "rb" )
local wfh, errorString = io.open( wFilePath, "wb" )
if not ( wfh ) then
-- Error occurred; output the cause
print( "File error: " .. errorString )
native.showAlert("ERROR", "File error: " .. errorString)
return false
else
-- Read the file and write to the destination directory
local data = rfh:read( "*a" )
if not ( data ) then
print( "Read error!" )
return false
else
if not ( wfh:write( data ) ) then
print( "Write error!" )
return false
end
end
end
results = 2 -- 2 = File copied successfully!
-- Close file handles
rfh:close()
wfh:close()
return results
end
M.getPath = function(str, sep)
sep = sep or package.config:sub(1,1) -- Get the default separator for the OS
local pattern = "(.*" .. sep .. ")"
return str:match(pattern)
end
M.get_file_name = function(file)
return file:match("^.+[\\/](.+)$")
end
M.get_file_name_no_extension = function(file)
-- Match the filename with or without path separators (handles both / and \)
local filename = file:match("^.+[\\/](.+)$")
-- Remove the extension
return filename and filename:match("(.+)%..+$") or file:match("(.+)%..+$")
end
-- Touch listener for handles to resize the image
-- Function to update the size and position of the selected image based on the handle and movement
M.updateImageSizeAndPosition = function(selectedImage, resizeHandles, handle, dx, dy, proportion, shiftPressed, controlPressed, zoomFactor)
-- Get the rotation angle in radians
local angle = math.rad(selectedImage.rotation)
local cosAngle = math.cos(angle)
local sinAngle = math.sin(angle)
-- Calculate the local dx and dy in the rotated coordinate system
local localDx = dx/ zoomFactor * cosAngle + dy/ zoomFactor * sinAngle
local localDy = dy/ zoomFactor * cosAngle - dx/ zoomFactor * sinAngle
local newWidth, newHeight
if controlPressed then
-- Calculate the distance from the center of the image to the mouse cursor
local centerX, centerY = selectedImage.x, selectedImage.y
local distanceX = (handle.startX + dx) - centerX
local distanceY = (handle.startY + dy) - centerY
-- Calculate the initial distances of the handle from the center
local initialDistanceX = handle.startX - centerX
local initialDistanceY = handle.startY - centerY
-- Calculate the scaling factors
local scaleX = (initialDistanceX ~= 0) and (distanceX / initialDistanceX) or 1
local scaleY = (initialDistanceY ~= 0) and (distanceY / initialDistanceY) or 1
-- Apply the scaling factors to calculate new width and height
newWidth = handle.startWidth * scaleX
newHeight = handle.startHeight * scaleY
-- Apply proportional scaling if shift is pressed
if shiftPressed then
if math.abs(scaleX) > math.abs(scaleY) then
newWidth = handle.startWidth * scaleX
newHeight = newWidth / proportion
else
newHeight = handle.startHeight * scaleY
newWidth = newHeight * proportion
end
end
-- Update the position of the handle being manipulated to stay under the cursor
handle.x = handle.startX + dx
handle.y = handle.startY + dy
-- Ensure consistent scaling factors
if scaleX < 0 then
selectedImage.xScale = -math.abs(selectedImage.xScale)
else
selectedImage.xScale = math.abs(selectedImage.xScale)
end
if scaleY < 0 then
selectedImage.yScale = -math.abs(selectedImage.yScale)
else
selectedImage.yScale = math.abs(selectedImage.yScale)
end
else
if handle == resizeHandles.topLeft then
if shiftPressed then
newWidth = handle.startWidth - localDx
newHeight = newWidth / proportion
localDx = handle.startWidth - newWidth
localDy = handle.startHeight - newHeight
else
newWidth = handle.startWidth - localDx
newHeight = handle.startHeight - localDy
end
selectedImage.x = handle.startImageX + (localDx * cosAngle - localDy * sinAngle) / 2
selectedImage.y = handle.startImageY + (localDx * sinAngle + localDy * cosAngle) / 2
elseif handle == resizeHandles.topRight then
if shiftPressed then
newWidth = handle.startWidth + localDx
newHeight = newWidth / proportion
localDx = newWidth - handle.startWidth
localDy = handle.startHeight - newHeight
else
newWidth = handle.startWidth + localDx
newHeight = handle.startHeight - localDy
end
selectedImage.x = handle.startImageX + (localDx * cosAngle - localDy * sinAngle) / 2
selectedImage.y = handle.startImageY + (localDx * sinAngle + localDy * cosAngle) / 2
elseif handle == resizeHandles.bottomLeft then
if shiftPressed then
newHeight = handle.startHeight + localDy
newWidth = newHeight * proportion
localDx = handle.startWidth - newWidth
localDy = newHeight - handle.startHeight
else
newWidth = handle.startWidth - localDx
newHeight = handle.startHeight + localDy
end
selectedImage.x = handle.startImageX + (localDx * cosAngle - localDy * sinAngle) / 2
selectedImage.y = handle.startImageY + (localDx * sinAngle + localDy * cosAngle) / 2
elseif handle == resizeHandles.bottomRight then
if shiftPressed then
newWidth = handle.startWidth + localDx
newHeight = newWidth / proportion
localDx = newWidth - handle.startWidth
localDy = newHeight - handle.startHeight
else
newWidth = handle.startWidth + localDx
newHeight = handle.startHeight + localDy
end
selectedImage.x = handle.startImageX + (localDx * cosAngle - localDy * sinAngle) / 2
selectedImage.y = handle.startImageY + (localDx * sinAngle + localDy * cosAngle) / 2
end
end
-- Apply the new dimensions
selectedImage.width = math.abs(newWidth)
selectedImage.height = math.abs(newHeight)
end
return M