-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_svgdraw.lua
315 lines (267 loc) · 7.12 KB
/
cmd_svgdraw.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
function widget:GetInfo()
return {
name = "SVG Draw",
desc = "v0.003 Draw SVG on the map - /luaui svgdraw",
author = "CarRepairer",
date = "2013-08-10",
license = "GNU GPL, v2 or later",
layer = 0,
enabled = false,
}
end
include("keysym.h.lua")
local imageLists = {}
local drawMode
local keypadNumKeys = {}
local picScale = 0.5
local picWidth = 0
local picHeight = 0
local echo = Spring.Echo
local function explode(div,str)
if (div=='') then return false end
local pos,arr = 0,{}
-- for each divider found
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
pos = sp + 1 -- Jump past current divider
end
table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
return arr
end
function string:findlast(str)
local i
local j = 0
repeat
i = j
j = self:find(str,i+1,true)
until (not j)
return i
end
function string:GetExt()
local i = self:findlast('.')
if (i) then
return self:sub(i)
end
end
function table:ifind(element)
for i=1, #self do
if self[i] == element then
return i
end
end
return false
end
--code from cmd_emotes by TheFatController
local function linePoints(x1, y1, x2, y2)
return { x1 = x1, y1 = y1, x2 = x2, y2 = y2 }
end
local function addline(lineList,x1, y1, x2, y2)
table.insert(lineList,linePoints((x1*picScale),(y1*picScale),(x2*picScale),(y2*picScale)))
end
local function getHW(lineList)
if (table.getn(lineList) == 0) then
return false
end
local bx1 = lineList[1].x1
local by1 = lineList[1].y1
local bx2 = lineList[1].x2
local by2 = lineList[1].y2
for _,lineInfo in ipairs(lineList) do
if (lineInfo.x1 < bx1) then
bx1 = lineInfo.x1
end
if (lineInfo.y1 < by1) then
by1 = lineInfo.y1
end
if (lineInfo.x2 > bx2) then
bx2 = lineInfo.x2
end
if (lineInfo.y2 > by2) then
by2 = lineInfo.y2
end
end
picWidth = math.abs(bx1 - bx2)
picHeight = math.abs(by1 - by2)
end
local function drawList(lineList)
local x,y = Spring.GetMouseState()
local getOver,getCo = Spring.TraceScreenRay(x,y, true) --param3 = onlycoords
getHW(lineList)
--if (getOver == "ground") then
x = (getCo[1] - (picWidth / 2))
y = (getCo[3] - (picHeight / 2))
for _,l in ipairs(lineList) do
Spring.MarkerAddLine((x+l.x1),0,(y+l.y1),(x+l.x2),0,(y+l.y2))
end
--end
end
--end code from cmd_emotes
--convert svg path to a list of line segments. assumes no bezier curves
local function PathToList(data)
local list = {}
data = data:gsub('([%a,])', ' %1 ')
--echo(data)
local dataBreakdown = explode( ' ', data )
local x,y, firstX,firstY
local cx,cy
local firstCmd
local onFirstCmd
local abs
local gotX, gotY
local nx, ny
local gotCoord
local curCmd
local curCmdAbs
for i,v in ipairs(dataBreakdown) do
gotCoord = false
badElem = false
if v:sub(1,1):find( '[%d%-]' ) then
if not gotX then
x = v
gotX = true
elseif not gotY then
y = v
gotX = false
gotCoord = true
if not firstX then
firstX = x
firstY = y
end
else --got coord
--echo('store coord', x, y)
end
elseif ({M=1, m=1})[v:sub(1,1)] then
cmd = 'move'
abs = v:sub(1,1):find( '%u' )
curCmd = cmd
curCmdAbs = abs
elseif ({L=1, l=1})[v:sub(1,1)] then
cmd = 'line'
abs = v:sub(1,1):find( '%u' )
curCmd = cmd
curCmdAbs = abs
elseif v:sub(1,1) == 'z' then
cmd = 'line'
abs = true
curCmdAbs = abs
x = firstX
y = firstY
gotCoord = true
curCmd = cmd
else
badElem = true
end
if not badElem then
abs = curCmdAbs
if not firstCmd then
firstCmd = cmd
onFirstCmd = true
end
cmd = curCmd
if gotCoord then
if onFirstCmd then
abs = true
end
if cmd == 'move' or cmd == 'line' then
if abs then
nx = x
ny = y
else
nx = cx + x
ny = cy + y
end
end
if cmd == 'line' then
list[#list+1] = {cx,cy, nx,ny}
end
cx = nx
cy = ny
if cmd == 'move' then
curCmd = 'line'
end
cmd = nil
abs = false
onFirstCmd = false
end
end
end
return list
end
local function AddPaths( pic, paths )
imageLists[pic] = {}
for i,path in ipairs(paths) do
local list = PathToList(path)
for i2,v in ipairs(list) do
addline(imageLists[pic],unpack(v))
end
end
end
local function AddImage(file)
local imageName = file:match( '([^/\\]+)%.svg' )
--echo(imageName )
local VFSMODE = VFS.ZIP_FIRST
local data = VFS.LoadFile(file, VFSMODE)
local lines = explode('\n', data)
local lists = {}
for _,line in ipairs(lines) do
--echo(line)
local match = line:match('%sd="([^"]*)"')
if match then
lists[#lists+1] = match
end
end
AddPaths( imageName, lists )
end
local function ScanDir()
local files = VFS.DirList('LuaUI/images/svgdraw')
local imageFiles = {}
for i=1,#files do
local fileName = files[i]
local ext = (fileName:GetExt() or ""):lower()
if (table.ifind({'.svg'},ext))then
imageFiles[#imageFiles+1]=fileName
AddImage(fileName)
end
end
end
local function EnterDrawMode()
drawMode = true
local out = ''
local index = 1
for pic, v in pairs(imageLists) do
out = out .. '(' .. index .. ') ' .. pic .. '. '
index = index + 1
end
echo(out)
end
-------------
--callins
function widget:Initialize()
ScanDir()
for i=0,9 do
keypadNumKeys[ KEYSYMS['KP' .. i] ] = i
end
end
function widget:KeyPress(key, mods, isRepeat, label, unicode)
if not drawMode then
return
end
if keypadNumKeys[key] then
--drawList(imageLists['drawing'])
local index = 1
for pic, imageList in pairs(imageLists) do
if index == keypadNumKeys[key] then
drawList(imageList)
drawMode = false
return
end
index = index + 1
end
end
drawMode = false
end
function widget:TextCommand(command)
if (string.find(command, 'svgdraw') == 1) then
EnterDrawMode()
end
end