-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgif2spr-gui.tcl
executable file
·411 lines (358 loc) · 11.5 KB
/
gif2spr-gui.tcl
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
#!/usr/bin/env wish
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# 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 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.
#
# For more information, please refer to <http://unlicense.org/>
package require Tk 8.6
package require platform
# Boilerplate Section
proc followLinks {path} {
while 1 {
try {
set dir [file dirname $path]
set path [file join $dir [file readlink $path]]
} on error {} {
break
}
}
return $path
}
set OS [regsub -- {-.*$} [platform::identify] ""]
set DISABLED_COLOR [ttk::style lookup TLabel -foreground disabled]
set SCRIPT_PATH [file normalize [followLinks [info script]]]
if {$OS == {win32}} {
set WIN 1
set MAC 0
set NIX 0
} elseif {$OS == {cygwin}} {
set WIN 1
set MAC 0
set NIX 1
} elseif {$OS == {linux}} {
set WIN 0
set MAC 0
set NIX 1
} elseif {[string first macosx $OS] == 0} {
set WIN 0
set MAC 1
set NIX 1
} else {
set WIN 0
set MAC 0
set NIX 1
}
if {$MAC} {
set APP 1
} else {
set APP 0
}
if {$NIX && !$APP} { ;# Linux, Cygwin, cmd-line Mac
if {[string first /usr/local [file normalize [info script]]] == 0} {
set DATA_PATH /usr/local/share/gif2spr
} else {
set DATA_PATH /usr/share/gif2spr
}
set USER_DATA_PATH [file join $env(HOME) .local share gif2spr]
set GIF2SPR gif2spr
} elseif {$MAC} { ;# Mac application
# ???
throw UNIMPLEMENTED "Ouch! Mac stuff unimplemented"
set USER_DATA_PATH [file join $env(HOME) Library "Application Support"\
gif2spr]
} else { ;# Assume Windows
if {[info exists ::vfs::tclkit_version]} {
set DATA_PATH [file dirname [info nameofexecutable]]
} else {
set DATA_PATH [file dirname $SCRIPT_PATH]
}
set USER_DATA_PATH [file join $env(USERPROFILE) AppData Local gif2spr]
set GIF2SPR [file join $DATA_PATH gif2spr.exe]
}
try {
file mkdir $USER_DATA_PATH
} on error {} {
puts stderr "Could not get/set user data path"
}
try {
set initDictChan [open [file join $USER_DATA_PATH init.dict]]
set initDict [read $initDictChan]
} on error {} {
set initDict {
dirs {}
game quake
}
} finally {
if {[info exists initDictChan]} {
close $initDictChan
}
}
# UI Section
if {[dict exists $initDict game]} {
set game [dict get $initDict game]
} else {
set game quake
}
if {$argc > 0} {
set gifPath [lindex $argv 0]
} else {
set gifPath ""
}
set origin 0.5,0.5
set alignment vp-parallel
set palPath ""
set blendMode normal
set color #ffffff
set useDummy 1
set extend 0
proc onClose {} {
try {
set initDictChan [open [file join $::USER_DATA_PATH init.dict] w]
puts $initDictChan $::initDict
} on error {err} {
puts stderr "Could not save init directories"
puts $err
} finally {
if {[info exists initDirDictChan]} {
close $initDictChan
}
destroy .
}
}
proc colorButtonFlat {btn clr} {
$btn configure -background $clr -highlightbackground $clr\
-activebackground $clr
}
proc onSetBlendModeOrGame {} {
if {$::blendMode == "index-alpha" && $::game == "hl"} {
.l.colorlbl state !disabled
colorButtonFlat .l.colorborder.btn $::color
.l.colorborder.btn configure -state normal
} else {
.l.colorlbl state disabled
colorButtonFlat .l.colorborder.btn $::DISABLED_COLOR
.l.colorborder.btn configure -state disabled
}
}
proc onSetBlendMode {args} {
onSetBlendModeOrGame
}
proc onSetGame {args} {
if {$::game == "hl"} {
.l.blendlbl state !disabled
.l.blendcombo state !disabled
.r.dummy state !disabled
.l.pallbl state disabled
.l.palframe.entry state disabled
.l.palframe.selbtn state disabled
.l.palframe.clrbtn state disabled
} else {
.l.blendlbl state disabled
.l.blendcombo state disabled
.r.dummy state disabled
.l.pallbl state !disabled
.l.palframe.entry state !disabled
.l.palframe.selbtn state !disabled
.l.palframe.clrbtn state !disabled
}
dict set ::initDict game $::game
onSetBlendModeOrGame
}
proc onSetColor {args} {
colorButtonFlat .l.colorborder.btn $::color
}
proc onSetGifPath {args} {
if {[string trim $::gifPath] == ""} {
.bot.write state disabled
} else {
.bot.write state !disabled
}
}
proc writeSpr {} {
set saveFileScript tk_getSaveFile
lappend saveFileScript -filetypes {
{"Sprite" .spr}
{"All Files" *}
}
if {[dict exists $::initDict dirs spr]} {
lappend saveFileScript -initialdir [dict get $::initDict dirs spr]
}
set sprFile [eval $saveFileScript]
if {[string trim $sprFile] == ""} {
return
} else {
dict set ::initDict dirs spr [file dirname $sprFile]
}
if {![regexp -nocase -- {\.spr$} $sprFile] && $::WIN} {
set sprFile $sprFile.spr
}
set cmd [list exec $::GIF2SPR -$::game -origin $::origin]
lappend cmd -alignment $::alignment
if {$::game == "hl"} {
lappend cmd -blendmode $::blendMode
if {$::useDummy} {
lappend cmd -dummy
}
} else {
if {$::palPath != ""} {
lappend cmd -palette "$::palPath"
}
}
if {$::game == "hl" && $::blendMode == "index-alpha"} {
lappend cmd -color $::color
}
if {$::extend} {
lappend cmd -extend
}
lappend cmd "$::gifPath" "$sprFile"
try {
eval $cmd
} trap CHILDSTATUS {err _} {
tk_messageBox -type ok -title Error -message $err -icon error
}
}
proc setColor args {
set newColor [tk_chooseColor -initialcolor $::color]
if {$newColor != ""} {
set ::color $newColor
}
}
proc selectFile {type pathVar} {
upvar $pathVar myPathVar
set openFileScript tk_getOpenFile
set path ""
if {$type == {gif}} {
set gifTypes {
{"GIF Files" .gif}
{"All Files" *}
}
if {[file exists $::gifPath]} {
lappend openFileScript -initialdir [file dirname $::gifPath]
} elseif {[dict exists $::initDict dirs gif]} {
lappend openFileScript -initialdir [dict get $::initDict dirs gif]
}
lappend openFileScript -filetypes $gifTypes
} elseif {$type == {pal}} {
if {[dict exists $::initDict dirs pal]} {
lappend openFileScript -initialdir [dict get $::initDict dirs pal]
}
} else {
throw {} "Unsupported file type \"$type\""
}
set path [eval $openFileScript]
if {$path != {}} {
dict set ::initDict dirs $type [file dirname $path]
set $pathVar $path
}
}
trace add variable game write onSetGame
trace add variable blendMode write onSetBlendMode
trace add variable color write onSetColor
trace add variable gifPath write onSetGifPath
grid [ttk::frame .l] [ttk::frame .r]
grid [ttk::frame .bot] -columnspan 2 -sticky e
grid [ttk::label .l.gamelbl -text Game]\
[ttk::frame .l.gameframe]
grid [ttk::radiobutton .l.gameframe.quake -variable game -text Quake\
-value quake] [ttk::radiobutton .l.gameframe.hl -variable game\
-text Half-Life -value hl]
grid configure .l.gameframe.hl -padx 18
grid [ttk::label .l.giflbl -text "Input GIF"]\
[ttk::frame .l.gifframe]
grid [ttk::entry .l.gifframe.entry -textvariable gifPath -width 30]\
[ttk::button .l.gifframe.btn -text Select -command {
selectFile gif ::gifPath
}]
grid [ttk::label .l.alignlbl -text Alignment]\
[ttk::combobox .l.aligncombo -state readonly -textvariable alignment\
-values {
vp-parallel-upright
upright
vp-parallel
oriented
vp-parallel-oriented
}]
grid [ttk::label .l.pallbl -text "Override Palette\n(Blank = Use Default)"\
-justify right]\
[ttk::frame .l.palframe]
grid [ttk::entry .l.palframe.entry -textvariable palPath -width 30]\
[ttk::button .l.palframe.selbtn -text Select -command {
selectFile pal ::palPath
}]\
[ttk::button .l.palframe.clrbtn -text Clear -command {
set ::palPath ""
}]
grid [ttk::label .l.blendlbl -text "Blend Mode"]\
[ttk::combobox .l.blendcombo -state readonly -textvariable blendMode\
-values {
normal
additive
index-alpha
alpha-test
}]
grid [ttk::label .l.colorlbl -text "Color"]\
[ttk::frame .l.colorborder -borderwidth 1 -relief sunken]
grid [button .l.colorborder.btn -width 8 -command setColor -state disabled\
-borderwidth 0] -sticky nesw
grid columnconfigure .l.colorborder all -uniform c -weight 1
grid rowconfigure .l.colorborder all -uniform r -weight 1
foreach widget [grid slaves .l -column 0] {
grid configure $widget -sticky e
}
foreach widget [grid slaves .l -column 1] {
grid configure $widget -sticky ew
}
grid configure .l.colorborder -sticky nsw
foreach widget [grid slave .l] {
grid configure $widget -padx 4 -pady 4
}
grid [ttk::labelframe .r.origin -text Origin] -padx 4 -pady 4
grid [ttk::radiobutton .r.origin.nw -variable origin -text NW -value 0.0,1.0]\
[ttk::radiobutton .r.origin.n -variable origin -text N -value 0.5,1.0]\
[ttk::radiobutton .r.origin.ne -variable origin -text NE -value 1.0,1.0]
grid [ttk::radiobutton .r.origin.w -variable origin -text W -value 0.0,0.5]\
[ttk::radiobutton .r.origin.c -variable origin -text Center -value 0.5,0.5]\
[ttk::radiobutton .r.origin.e -variable origin -text E -value 1.0,0.5]
grid [ttk::radiobutton .r.origin.sw -variable origin -text SW -value 0.0,0.0]\
[ttk::radiobutton .r.origin.s -variable origin -text S -value 0.5,0.0]\
[ttk::radiobutton .r.origin.se -variable origin -text SE -value 1.0,0.0]
grid [ttk::checkbutton .r.dummy -text "Append dummy frame" -variable useDummy]\
-sticky w
grid [ttk::checkbutton .r.extend -text "Extend frames to image dimensions"\
-variable extend] -sticky w
foreach radio [grid slave .r.origin] {
grid configure $radio -padx 4 -pady 4 -sticky w
}
grid columnconfigure .r.origin all -uniform c -weight 1 -pad 4
grid [ttk::button .bot.write -text Write -command writeSpr]\
[ttk::button .bot.quit -text Quit -command onClose]\
-padx 4 -pady 4
.bot.write state disabled
wm protocol . WM_DELETE_WINDOW {
onClose
}
onSetGame
. configure -padx 4 -pady 4
# set root background color to themed frame background color
. configure -background [ttk::style lookup TFrame -background]
wm resizable . 0 0
wm title . "gif2spr GUI Front-End"