-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguicanvas.cfg
392 lines (358 loc) · 14.1 KB
/
guicanvas.cfg
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
// GUICANVAS by @SalatielSauer (01/09/2023)
// github.com/CubeScript/Sauer-GUICANVAS
// the screen consists of a model with dozens of meshes, each mesh has an index that is used as a pixel;
// the pixel color/texture is set using objskin <index>;
// the model can be loaded in game with `mmodel guicanvas` or in a menu with `guimodelpreview "guicanvas"`;
// this file must be executed (/exec guicanvas.cfg) at least once to enable the guicanvas.* commands.
guicanvas.previewMode = 0
_guicanvasEditor = [
notepadfile = $arg1
newgui _ [ guistayopen [
guinoautotab [
guieditor $notepadfile -80 20
textinit $notepadfile $notepadfile
guilist [
guilist [
guifield notepadfile -25
guilist [
guibutton "^f8save file" [textfocus $notepadfile; textsave $notepadfile] 0
guibar
guibutton "^f1exec file" [textfocus $notepadfile; textexec $notepadfile] 0
guibar
guibutton "^f8load file" [textfocus $notepadfile; textload $notepadfile] 0
]
]
guibar
guilist [
guibutton "^f1copy all" [textfocus $notepadfile; textselectall; textcopy] 0
guibutton "^f1paste" [textfocus $notepadfile; textpaste] 0
]
guibar
guilist [
guibutton "^f3clear all" [textfocus $notepadfile; textclear] 0
]
guibar
guilist [
guicheckbox "^f8show preview" guicanvas.previewMode
guibutton "^f1exec & preview" [textfocus $notepadfile; textexec $notepadfile; sleep 0 [guicanvas.refresh]; guicanvas.previewMode = 1] 0
]
]
guistrut -2
guialign 1 [guitextbox "GUICANVAS Editor 1.0" 0 0 0xccaacc]
if (guicanvas.previewMode) [
guistrut -28
guialign 1 [
guistrut 10
guimodelpreview "../../packages/models/guicanvas" [] 1 10
guistrut -8
]
guistrut 8
]
]
]] 0
showgui _
]
_guicanvas = [
if (|| (! (getalias guicanvas)) (=s (getalias guicanvas) "")) [
guicanvas = [
if (=s $arg1 "notepad") [_guicanvasEditor] [
local canvas
canvas = []
loopfiles cfg "guicanvas" "cfg" [append canvas $cfg]
inputcommand $arg1 [
local isNum
isNum = (&& (>= (strcode $commandbuf) 48) (<= (strcode $commandbuf) 57))
if (! $isNum) [
local newCanvas saveCanvas
newCanvas = (concatword "guicanvas/" (strreplace $commandbuf ".cfg" "") ".cfg")
saveCanvas = [
_filesystem.save @newCanvas (escape "exec ^"guicanvas.cfg^"^n^nguicanvas.size 32 32 // this will try to use an existing screen or generate a new one^nguicanvas.background = ^"packages/textures/default.png^"^n^nguicanvas.refreshRate = 0^nguicanvas.play [^n^t// your render commands go here^n^t// they will run every millisecond set by $guicanvas.refreshRate^n^n] [^n^t// this is the interface body^n^tguititle ^"my canvas^"^n] 0 0^n^nguicanvas.refresh // this will draw the background and the pixels set with guicanvas.setPixel") [
_guicanvasEditor @@newCanvas
textfocus @@newCanvas
textload @@newCanvas
]
]
if (findfile $newCanvas) [
inputcommand "0" [
if (= $commandbuf 0) [
_guicanvasEditor @@newCanvas
] [
@@saveCanvas
]
] (format "^f8[GUICANVAS] ^f3warning^n^f7there is already a canvas with the same name in ^f5%1^f7^nuse ^f50 ^f~to load it or ^f51 ^f~to overwrite with preset.^n^f8>^f5" @newCanvas)
] [
@saveCanvas
]
] [
exec (concatword "guicanvas/" (at (concat @@canvas) @commandbuf) ".cfg")
]
] (concat "^f8[GUICANVAS]^n^f7type a name to create a canvas, or a number to load:^n" (loopconcat i (listlen $canvas) [result (format "^f1%1^f7: ^f8%2^n^f5" $i (at $canvas $i))]))
]
]
guicanvas.width = 32
guicanvas.height = 32
guicanvas.pixels = []
guicanvas.background = 1
guicanvas.keepPixels = 0
guicanvas.opacity = 1
guicanvas.refreshRate = -1
// most of these are used only once and shouldn't be changed accidentally,
// but for the sake of variability options:
_guicanvas.screenDir = "packages/models/guicanvas"
_guicanvas.baseTextureBlack = "packages/skyboxes/black_up.jpg"
_guicanvas.baseTextureWhite = "packages/skyboxes/white_ft.jpg"
_guicanvas.baseTextureOpacity = "<grey>packages/skyboxes/cloud_ring.png"
_guicanvas.usedIndexes = []
_guicanvas.lastRefresh = (getmillis)
guicanvas.resetPixels = [
guicanvas.pixels = []
_guicanvas.usedIndexes = []
]
guicanvas.setPixel = [
local pixel_index pixel_texture
pixel_index = (+ (* $arg2 $guicanvas.width) $arg1)
if (= (indexof $_guicanvas.usedIndexes $pixel_index) -1) [
// if arg3 is -1: arg3 is baseTextureOpacity, else: if arg4 is defined: arg3 arg4 arg5 are colors (baseTextureWhite), else: arg3 is custom texture path.
pixel_texture = (? (= $arg3 -1) $_guicanvas.baseTextureOpacity (? (!=s $arg4 "") (concatword "<colorify:" $arg3 "/" $arg4 "/" $arg5 ">" $_guicanvas.baseTextureWhite) $arg3))
guicanvas.pixels = (concatword $guicanvas.pixels "objskin " $pixel_index " ^"" $pixel_texture "^";")
append _guicanvas.usedIndexes $pixel_index // prevents duplicate items in pixels list
]
]
guicanvas.refresh = [
clearmodel (format "../../%1" $_guicanvas.screenDir)
]
guicanvas.fillRect = [
local x y width height r g b
x = $arg1
y = $arg2
width = $arg3
height = $arg4
r = $arg5
g = $arg6
b = $arg7
loop i $width [
loop j $height [
guicanvas.setPixel (+ $x $i) (+ $y $j) $r $g $b
]
]
]
guicanvas.perspective = [
modelpreviewpitch $arg1
modelpreviewfov (max 10 $arg2)
]
guicanvas.onupdate = [
if (!= $guicanvas.refreshRate -1) [
if (>= (getmillis) $_guicanvas.lastRefresh) [
_guicanvas.lastRefresh = (+ (getmillis) $guicanvas.refreshRate)
do $arg1
]
]
]
guicanvas.play = [
local onupdate action actionX actionY
if (= $numargs 0) [
arg1 = (getalias _guicanvas.playarg1)
arg2 = (getalias _guicanvas.playarg2)
arg3 = (getalias _guicanvas.playarg3)
arg4 = (getalias _guicanvas.playarg4)
]
onupdate = $arg1; _guicanvas.playarg1 = $arg1
action = $arg2; _guicanvas.playarg2 = $arg2
actionX = $arg3; _guicanvas.playarg3 = $arg3
actionY = $arg4; _guicanvas.playarg4 = $arg4
_guicanvas.lastRefresh = (getmillis)
newgui guicanvas [
guistrut -30
guialign 0 [
guistrut -35
guialign 0 [
guistrut 22
guimodelpreview (format "../../%1" $_guicanvas.screenDir) "mapmodel" [] 15 0
guistrut -10
]
guistrut -35
]
guialign -1 [
guilist [
guistrut (+ -12 @@@actionY)
guilist [
guistrut @@@@actionX
@@@@action
guistrut (* @@@@actionX -1)
]
guistrut (- 13 @@@actionY)
]
guistrut 20
]
guicanvas.onupdate [
@@onupdate
]
] 0
showgui guicanvas
]
guicanvas.size = [
if (= $numargs 0) [
inputcommand (concat $guicanvas.width $guicanvas.height) [
guicanvas.size (at $commandbuf 0) (at $commandbuf 1)
sleep 0 [echo (concat "^f8[GUICANVAS] ^f1screen size set to" $guicanvas.width $guicanvas.height)]
] "^f8[GUICANVAS] ^f7screen size:^f5"
] [
guicanvas.width = @arg1
guicanvas.height = (? (=s @arg2 "") $guicanvas.width @arg2)
]
if (! (findfile (format "%1/canvas_%2x%3.obj" $_guicanvas.screenDir $guicanvas.width $guicanvas.height))) [
sleep 0 [cleargui]
inputcommand " " [
echo (format "^f8[GUICANVAS] ^f5generating file for screen %1x%2, this may take a while.." $guicanvas.width $guicanvas.height)
sleep 0 [
_guicanvas.genScreen $guicanvas.width $guicanvas.height 1 1 [
local restart
restart = [
if (!=s (getalias _guicanvas.playarg1) "") [
echo "^f8[GUICANVAS] ^f7restarting canvas"
guicanvas.play $_guicanvas.playarg1 $_guicanvas.playarg2 $_guicanvas.playarg3 $_guicanvas.playarg4
if (getalias guicanvas.onstart) [do $guicanvas.onstart; guicanvas.onstart = []]
]
]
if (! (findfile (format "%1/obj.cfg" $_guicanvas.screenDir))) [
_filesystem.save (format "%1/obj.cfg" $_guicanvas.screenDir) (escape $_guicanvas.screenCFG) [
restart
]
] [restart]
]
]
] (format "^f8[GUICANVAS]^t^n> ^f7The screen size ^f5%1^f7x^f5%2 ^f7is not yet available^npress ^f0enter ^f~to generate it or ^f3esc ^f~to cancel." $guicanvas.width $guicanvas.height)
] [if (getalias guicanvas.onstart) [do $guicanvas.onstart; guicanvas.onstart = []]]
]
guicanvas.genScreen = [
local _obj warning checkobj
_gscw = (? (=s $arg1 "") $guicanvas.width $arg1)
_gsch = (? (=s $arg2 "") $guicanvas.height $arg2)
_gscl = 1
_gsct = 1
_obj = (format "canvas_%1x%2.obj" $_gscw $_gsch)
checkobj = [
if (findfile (concatword $_guicanvas.screenDir "/" $_obj)) [
warning = (concat "^f~The^f8" $_obj "^f~file ^f2is already in place^f~, are you sure you want to rewrite it? ")
] [
warning = (format "You are about to ^f2create^f~ a ^f5%1^f~x^f5%2 ^f~screen, " $_gscw $_gsch)
]
]
checkobj
newgui _ [ guistayopen [
guistrut -3
guititle (format "^f8GUICANVAS screen generator^n^f5%1^f~x^f5%2 ^f~contains ^f5%3 ^f~pixels" $_gscw $_gsch (* $_gscw $_gsch))
guistrut 2
guialign 0 [
guitext "^f5width: " 0
guifield _gscw -5 [sleep 500 [guifadein @guifadein]; guifadein 0; guicanvas.genScreen $_gscw $_gsch]
guistrut 1
guitext "^f5height: " 0
guifield _gsch -5 [sleep 500 [guifadein @guifadein]; guifadein 0; guicanvas.genScreen $_gscw $_gsch]
guistrut 1
guitext "^f5layers: " 0
guifield _gscl -5
]
guistrut 0.3
guialign 0 [
guicheckbox "textures support" _gsct 1 0
]
guistrut 0.3
guitextbox (concat @@warning "this operation may take a while depending on the screen size..") 45 0 0xffffff
guialign 0 [
guibutton (concat "^f8continue anyway") [
_guicanvas.genScreen $_gscw $_gsch $_gscl $_gsct
]
guistrut 1
guibutton "^f3cancel" [cleargui] "exit"
]
guistrut 1
]] 0
showgui _
]
_guicanvas.genScreen = [
local width height layers textures index x_offset y_offset buffer callback
width = $arg1
height = $arg2
layers = $arg3
textures = $arg4
callback = $arg5
// offsets for centering
x_offset = (*f (+f (divf $width 2.0) 0.5) -1)
y_offset = (-f (divf $height 2.0) 0.5)
index = 0
buffer = "# generated with GUICANVAS Screen Generator by @SalatielSauer^n"
loop layer $layers [
loop ri $height [
local i
i = (- $height $ri) // start from top row
loop j $width [
local v1 v2 v3 v4 uv start_vertex
// define the 4 vertices for the current plane, considering offsets for centering
v1 = (format "v %1 %2 0.%3" (+f $j $x_offset) (+f $i $y_offset) $layer)
v2 = (format "v %1 %2 0.%3" (+f (+f $j 1) $x_offset) (+f $i $y_offset) $layer)
v3 = (format "v %1 %2 0.%3" (+f (+f $j 1) $x_offset) (+f (+f $i 1) $y_offset) $layer)
v4 = (format "v %1 %2 0.%3" (+f $j $x_offset) (+f (+f $i 1) $y_offset) $layer)
// UV coordinates (standard plane unwrapping)
uv = "vt 0 0^nvt 1 0^nvt 1 1^nvt 0 1^n"
// write the face (polygon) and UV using the above vertices
// 'g' is equivalent to "export as groups" in Blender
buffer = (format "%7g %1 ^n%2 ^n%3 ^n%4 ^n%5^n%6" $index $v1 $v2 $v3 $v4 (? $textures $uv "") $buffer)
start_vertex = (+ (* $index 4) 1)
index = (+ $index 1)
buffer = (format "%5f %1/1 %2/2 %3/3 %4/4^n" $start_vertex (+ $start_vertex 1) (+ $start_vertex 2) (+ $start_vertex 3) $buffer)
]
]
]
_filesystem.save (format "%1/canvas_%2x%3.obj" $_guicanvas.screenDir $width $height) (escape $buffer) [
if (findfile (format "%1/canvas_%2x%3.obj" $_guicanvas.screenDir @width @height)) [
echo (format "^f8[GUICANVAS] ^f1screen saved successfully at ^f7%1/canvas_%2x%3.obj" $_guicanvas.screenDir @@width @@height)
@@callback
] [
error "^f8[GUICANVAS] ^f3could not save screen, try again"
]
]
]
_filesystem.write = [if (!=s $arg1 "") [append _filesystem.linesbuff (concatword "[" $arg1 "]")]]
_filesystem.open = [_filesystem.linesbuff = []; newgui _ [guieditor @arg1 -1 1; textfocus @arg1; textload @arg1; cleargui]; showgui _; if $arg2 [sleep 0 [arg1 = (escape $textshow); arg2 = @arg1; nodebug [do (substr (strreplace (strreplace (strreplace (strreplace (concatword "^n" $textshow) ";" "^n") "//" "^^/^^/") "^n" "]^n _filesystem.write [") "[ ]" "^^n") 1)]; @arg2]]]
_filesystem.save = [newgui _ [local content; content = @arg2; guifield content 1; textfocus content; textsave @arg1; cleargui];showgui _; sleep 0 [@arg3]]
_guicanvas.loadScreen = 1
_guicanvas.screenCFG = [
// the actual model configuration:
objload (concatword "canvas_" $guicanvas.width "x" $guicanvas.height ".obj")
mdlscale 100
mdlspin -36
mdlyaw -90
mdlfullbright 1
mdlalphatest $guicanvas.opacity
objcullface * 0
objdir "../.." // start at home/installation folder
if (|| (!= $guicanvas.background 0) (!=s $guicanvas.background 0)) [
objskin * (? (= $guicanvas.background 1) $_guicanvas.baseTextureBlack $guicanvas.background) // use black background as default (1)
] [
objskin * $_guicanvas.baseTextureOpacity
]
do $guicanvas.pixels
if (! $guicanvas.keepPixels) [
guicanvas.pixels = []
_guicanvas.usedIndexes = []
]
]
] [
if (_guicanvas.loadScreen) [
do $_guicanvas.screenCFG
] [_guicanvas.loadScreen = 1]
]
]
_guicanvas.loadScreen = 0
_guicanvas
if (! (findfile (format "%1/obj.cfg" $_guicanvas.screenDir))) [
// create model configuration file if not available
_filesystem.save (format "%1/obj.cfg" $_guicanvas.screenDir) (escape $_guicanvas.screenCFG) [
if (findfile (format "%1/obj.cfg" $_guicanvas.screenDir)) [
echo "^f8[GUICANVAS] ^f7loaded successfully"
] [
error (format "^f8[GUICANVAS] ^f3could not write ^f7%1/obj.cfg" $_guicanvas.screenDir)
]
]
] [echo "^f7GUICANVAS version: 1.0"]