-
Notifications
You must be signed in to change notification settings - Fork 0
/
sf2tool.py
executable file
·1619 lines (1289 loc) · 50.4 KB
/
sf2tool.py
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/local/bin/python
import wx
import jsonpickle
import os.path
import glob
import enum
import sys
from wx.lib.anchors import LayoutAnchors
from numpy import array
from sf2tool import dlgAnimFrames
# -----------------------------------------
# MY SETTINGS-RELATED CLASSES
# -----------------------------------------
class Settings:
def __init__(self, projpath=''):
if projpath=='':
return
self.projpath = projpath
self.groups = {}
# add in groups
for dir in os.listdir(self.projpath):
if not os.path.isdir(os.path.join(self.projpath,dir)):
continue
if dir[0] == '.':
continue
self.groups[dir] = Group(self.projpath, dir)
def update(self):
for dir in os.listdir(self.projpath):
if not os.path.isdir(os.path.join(self.projpath, dir)):
continue
if dir[0] == '.':
continue
if dir in self.groups:
self.groups[dir].update(self.projpath)
else:
self.groups[dir] = Group(self.projpath, dir)
# -----------------------------------------
class Group:
def __init__(self, projpath, name):
self.name = name
groupPath = os.path.join(projpath, self.name)
self.PNGs = self._find_pngs(groupPath)
self.anims = { }
def update(self, projpath):
groupPath = os.path.join(projpath, self.name)
self._find_pngs(groupPath, refresh=True)
# - - - - - - - - - - - - - - - - - - -
def _find_pngs(self, path, refresh=False):
pngs = {}
files = glob.glob(path + "/*.png")
short_files = [f[len(path)+1:-4] for f in files]
for f in short_files:
# are we just refreshing existing list?
if refresh:
if not f in self.PNGs:
self.PNGs[f] = PNG(f)
# nope, we're creating a completely new list
else:
pngs[f] = PNG(f)
return pngs
# -----------------------------------------
class PNG:
def __init__(self, name):
self.name=name
self.hitboxes= { 'Head': [0,0,0,0], 'Torso': [0,0,0,0], 'Feet': [0,0,0,0], 'Attack': [0,0,0,0] }
self.crop = [ ]
self.adjust = [ ]
# -----------------------------------------
class Anim:
def __init__(self, name):
self.name=""
self.relx=[]
self.rely=[]
self.frame=[]
self.animlength=0
self.pngs=[]
self.pingpong=0
self.cols=0
self.rows=0
# -----------------------------------------
# MYFRAME GUI CLASS
# -----------------------------------------
class MyFrame(wx.Frame):
def __init__(self):
# Had to pass in these weird arguments for super() in python2.7 (not needed in python3?)
# https://stackoverflow.com/questions/38963018/typeerror-super-takes-at-least-1-argument-0-given-error-is-specific-to-any
super(MyFrame, self).__init__(parent=None, title='SF2 Animation Tool', pos=(100,50), size=(1100,700))
self.Bind(wx.EVT_CLOSE, self.OnFrameClose)
self.mode = None
panel = wx.Panel(self, size=(self.GetSize()[0]-10, self.GetSize()[1]-10))
panel.SetAutoLayout(True)
_, self.lstGroups = self.create_labeled_list_box(panel, label='GROUPS', pos=(5,0), size=(100,360), choices=[])
self.lstGroups.Bind(wx.EVT_LISTBOX, self.OnLstGroupSelectionChanged)
self.lstGroups.Bind(wx.EVT_SET_FOCUS, self.OnLstGroupSelectionChanged)
self.lblPngs, self.lstPngs = self.create_labeled_list_box(panel, label='PNGS', pos=(105,0), size=(150,360), choices=[])
self.lstPngs.Bind(wx.EVT_LISTBOX, self.OnLstPngsSelectionChanged)
self.lblAnims, self.lstAnims = self.create_labeled_list_box(panel, label='ANIMS', pos=(255,0), size=(100,360), choices=[])
self.lstAnims.Bind(wx.EVT_LISTBOX, self.OnLstAnimsSelectionChanged)
self.lstAnims.Bind(wx.EVT_CONTEXT_MENU, self.OnLstAnimsContextMenu)
# I think these two should be panes that I turn on/off based on whether a PNGS or ANIMS item is selected
self.pnlHitboxes = self.create_hitboxes_panel(panel, pos=(355,0), size=(215,300))
self.pnlAnimDetails = self.create_animdetails_panel(panel, pos=(355,0), size=(200,320))
self.pnlHitboxes.Hide()
self.pnlAnimDetails.Hide()
self.txtDebug = wx.TextCtrl(panel, pos=(5,385), size=(555, 300))
self.txtDebug.SetConstraints(LayoutAnchors(self.txtDebug, left=True, top=True, right=False, bottom=True))
self.sheetscale = 1
self.scale = 4
self.bmp = self.create_bitmap_area(panel, pos=(575,0), size=(510,680))
self.create_menu()
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimerUpdate, self.timer)
self.speed = 200
self.timer.Start(self.speed)
self.animateFlag = False
self.animateDir = True
self.animidx = 0
self.Show()
# - - - - - - - - - - - - - - - - - - -
def UpdateRelxRely(self):
if self.selectedAnimObj.relx:
self.relx += self.selectedAnimObj.relx[self.animidx]
if self.selectedAnimObj.rely:
self.rely += self.selectedAnimObj.rely[self.animidx]
# - - - - - - - - - - - - - - - - - - -
def CalculateMaxColRow(self):
pngPath = os.path.join(settings.projpath, self.selectedGroup, self.selectedAnimObj.pngs[0] + ".png")
img = wx.Image(pngPath, wx.BITMAP_TYPE_ANY)
width = img.GetWidth()
height = img.GetHeight()
self.maxcol = width / 8
self.maxrow = height / 8
self.relx = 0
for relx in self.selectedAnimObj.relx:
self.maxcol += abs(relx)
if relx < 0:
self.relx -= relx
for rely in self.selectedAnimObj.rely:
if rely < 0:
self.maxrow -= rely
self.rely = self.maxrow - height / 8
print("mc={},mr={},rx={},ry={}".format(self.maxcol,self.maxrow,self.relx,self.rely))
# - - - - - - - - - - - - - - - - - - -
def OnTimerUpdate(self, event):
if self.animateFlag:
print(str(self.animidx) + ", " + str(self.animateDir))
# load the index image and draw it
if self.selectedAnimObj.frame:
pngidx = self.selectedAnimObj.frame[self.animidx]
png = self.selectedAnimObj.pngs[pngidx]
else:
png = self.selectedAnimObj.pngs[self.animidx]
if self.animidx == 0:
self.CalculateMaxColRow()
print(png)
pngPath = os.path.join(settings.projpath, self.selectedGroup, png + ".png")
self.load_image(pngPath)
self.UpdateRelxRely()
if self.selectedAnimObj.frame:
self.UpdateAnimIdx(len(self.selectedAnimObj.frame))
else:
self.UpdateAnimIdx(len(self.selectedAnimObj.pngs))
# - - - - - - - - - - - - - - - - - - -
def UpdateAnimIdx(self, framecnt):
if self.selectedAnimObj.pingpong:
if self.animateDir:
self.animidx += 1
if self.animidx >= framecnt:
self.animidx = framecnt-2
self.animateDir = False
else:
if self.animidx == 0:
self.animateDir = True
self.animidx += 1
else:
self.animidx -= 1
else:
self.animidx += 1
if self.animidx >= framecnt:
self.animidx = 0
# - - - - - - - - - - - - - - - - - - -
class Mode(enum.Enum):
Group = 0
Png = 1
# - - - - - - - - - - - - - - - - - - -
class Hitbox(enum.Enum):
Head = 0
Torso = 1
Feet = 2
Attack = 3
# - - - - - - - - - - - - - - - - - - -
def create_hitboxes_panel(self, panel, pos, size):
hbpanel = wx.Panel(panel, pos=pos, size=size)
itempos=[5, 20]
self.lblPngName = self.create_static_field(hbpanel, tuple(itempos), "Name:", "???")
itempos[1] += 30
self.txtCrop = self.create_text_field(hbpanel, tuple(itempos), "Crop Dim:", "", self.OnBtnCutClicked)
self.txtCrop.Bind(wx.EVT_KEY_DOWN, self.OnTxtCropKeyDown)
self.btnCut = wx.Button(hbpanel, label='Cut', pos=(itempos[0]+170,itempos[1]), size=(40,25))
self.btnCut.Bind(event=wx.EVT_BUTTON, handler=self.OnBtnCutClicked)
itempos[1] += 30
self.lblPngSize = self.create_static_field(hbpanel, tuple(itempos), "Size:", "???")
itempos[1] += 30
self.txtAdjust = self.create_text_field(hbpanel, tuple(itempos), "Adjust:", "", self.OnBtnCutClicked)
self.txtAdjust.Bind(wx.EVT_KEY_DOWN, self.OnTxtAdjustKeyDown)
initialvalue="0, 0, 0, 0"
self.lstTxtHboxes = []
for hb in self.Hitbox:
itempos[1] += 30
self.lstTxtHboxes.append(self.create_text_field(hbpanel, tuple(itempos), hb.name + ":",
initialvalue, self.OnTxtHboxEnter))
return hbpanel
# - - - - - - - - - - - - - - - - - - -
def OnTxtCropKeyDown(self, event):
if event.ControlDown():
[self.sx, self.sy, self.sw, self.sh] = [int(i) for i in self.txtCrop.GetValue().split(',')]
c = event.GetKeyCode()
# go to next png?
if event.ShiftDown():
self.MovePngSelectionOnKeyPress(c)
self.OnLstPngsSelectionChanged(None)
return
# move the overlay?
elif c == wx.WXK_UP:
self.sy -= 1
elif c == wx.WXK_DOWN:
self.sy += 1
elif c == wx.WXK_LEFT:
self.sx -= 1
elif c == wx.WXK_RIGHT:
self.sx += 1
else:
event.Skip()
return
else:
event.Skip()
return
s = "{}, {}, {}, {}".format(self.sx, self.sy, self.sw, self.sh)
self.txtCrop.SetValue(s)
self.OnBtnCutClicked(None)
# - - - - - - - - - - - - - - - - - - -
def OnTxtAdjustKeyDown(self, event):
if event.ControlDown():
c = event.GetKeyCode()
# go to next png?
if event.ShiftDown():
self.MovePngSelectionOnKeyPress(c)
self.OnLstPngsSelectionChanged(None)
return
# move the adjustment?
else:
try:
[self.sx, self.sy, self.sw, self.sh] = [int(i) for i in self.txtCrop.GetValue().split(',')]
[x, y, w, h] = [int(i) for i in self.txtAdjust.GetValue().split(',')]
if event.AltDown():
if c == wx.WXK_UP:
h -= 8
elif c == wx.WXK_DOWN:
h += 8
elif c == wx.WXK_LEFT:
w -= 8
elif c == wx.WXK_RIGHT:
w += 8
else:
event.Skip()
return
else:
if c == wx.WXK_UP:
y -= 1
elif c == wx.WXK_DOWN:
y += 1
elif c == wx.WXK_LEFT:
x -= 1
elif c == wx.WXK_RIGHT:
x += 1
else:
event.Skip()
return
except:
event.Skip()
return
else:
event.Skip()
return
s = "{}, {}, {}, {}".format(x, y, w, h)
self.txtAdjust.SetValue(s)
self.OnBtnCutClicked(None)
# - - - - - - - - - - - - - - - - - - -
def OnBtnCutClicked(self, event):
print('clicked')
# cut the image from the sprite-sheet at the coords specified by crop-dim field
cropvals = [int(i) for i in self.txtCrop.GetValue().split(',')]
adjustvals = None
if len(self.txtAdjust.GetValue()) > 0:
adjustvals = [int(i) for i in self.txtAdjust.GetValue().split(',')]
[self.sx, self.sy, self.sw, self.sh] = cropvals
self.update_cropdim(adjustvals)
self.SaveOutCrop(self.sx, self.sy, self.sw, self.sh, self.pngPath, adjustvals)
# redraw png
self.OnLstPngsSelectionChanged(None)
# - - - - - - - - - - - - - - - - - - -
def SaveOutCrop(self, x, y, w, h, path, adjust=None):
if not adjust:
adjust = [ 0, 0, ((w+7)/8)*8, ((h+7)/8)*8 ]
dbmp = wx.Bitmap(adjust[2], adjust[3], depth=16)
sheetbmp = self.sprsheet.ConvertToBitmap()
sourcedc = wx.MemoryDC(sheetbmp)
pixels = self.sprsheet.GetData()
pos=(self.sprsheet.GetWidth()+1)*3 # avoid any initial white frame (e.g. in Zangief spritesheet)
transparent = (pixels[pos], pixels[pos+1], pixels[pos+2])
destdc=wx.MemoryDC(dbmp)
destdc.SetBrush(wx.Brush(transparent))
destdc.SetPen(wx.Pen(transparent, 1))
destdc.DrawRectangle(0, 0, adjust[2], adjust[3])
destdc.Blit(adjust[0], adjust[1], w, h, sourcedc, x, y, wx.COPY)
# save out the new bitmap
dbmp.SaveFile(path, wx.BITMAP_TYPE_PNG)
# - - - - - - - - - - - - - - - - - - -
def OnTxtHboxEnter(self, event):
print(event.GetEventObject())
self.update_image()
self.CopyHitboxesFromGuiToSettings()
projectNotSaved = True
# - - - - - - - - - - - - - - - - - - -
def create_animdetails_panel(self, panel, pos, size):
adpanel = wx.Panel(panel, pos=pos, size=size)
itempos=[5,20]
self.lblAnimName = self.create_static_field(adpanel, tuple(itempos), "Name:", "???")
itempos[1] += 30
self.txtRelx = self.create_text_field(adpanel, tuple(itempos), "relx[]:", "", self.OnTxtRelxEnter)
itempos[1] += 30
self.txtRely = self.create_text_field(adpanel, tuple(itempos), "rely[]:", "", self.OnTxtRelyEnter)
itempos[1] += 30
self.txtFrame = self.create_text_field(adpanel, tuple(itempos), "frame[]:", "", self.OnTxtFrameEnter)
itempos[1] += 30
self.lblAnimlen = self.create_static_field(adpanel, tuple(itempos), "anim_len:", "<implied>")
itempos[1] += 30
self.lblAnimPngs = self.create_static_field(adpanel, tuple(itempos), "pngs[]:", "<not set>")
self.lblAnimPngs.Bind(wx.EVT_LEFT_DOWN, self.OnLblAnimPngsClicked)
itempos[1] += 30
self.txtPingPong = self.create_text_field(adpanel, tuple(itempos), "pingpong:", "0", self.OnTxtPingPongEnter)
itempos[1] += 30
self.lblCols = self.create_static_field(adpanel, tuple(itempos), "cols:", "<implied>")
itempos[1] += 30
self.lblCols = self.create_static_field(adpanel, tuple(itempos), "rows:", "<implied>")
# media controls
itempos[1] += 30
self.btnPlay = wx.Button(adpanel, label='>', pos=tuple(itempos), size=(25,25))
self.btnPlay.Bind(event=wx.EVT_BUTTON, handler=self.OnBtnPlayClicked)
itempos[0] += 30
self.btnStop = wx.Button(adpanel, label='o', pos=tuple(itempos), size=(25,25))
self.btnStop.Bind(event=wx.EVT_BUTTON, handler=self.OnBtnStopClicked)
return adpanel
# - - - - - - - - - - - - - - - - - - -
def OnTxtRelxEnter(self, event):
global projectNotSaved
vals = [int(i) for i in self.txtRelx.GetValue().split(',')]
self.selectedAnimObj.relx = vals
projectNotSaved = True
# - - - - - - - - - - - - - - - - - - -
def OnTxtRelyEnter(self, event):
global projectNotSaved
vals = [int(i) for i in self.txtRely.GetValue().split(',')]
self.selectedAnimObj.rely = vals
projectNotSaved = True
# - - - - - - - - - - - - - - - - - - -
def OnTxtFrameEnter(self, event):
global projectNotSaved
vals = [int(i) for i in self.txtFrame.GetValue().split(',')]
self.selectedAnimObj.frame = vals
projectNotSaved = True
# - - - - - - - - - - - - - - - - - - -
def OnTxtPingPongEnter(self, event):
global projectNotSaved
print('OnTxtPingPongEnter')
self.selectedAnimObj.pingpong = int(self.txtPingPong.GetValue())
projectNotSaved = True
# - - - - - - - - - - - - - - - - - - -
def OnBtnPlayClicked(self, event):
self.animateFlag = True
self.animateDir = True
self.animidx = 0
self.selectedAnimObj.pngs
print('play')
# - - - - - - - - - - - - - - - - - - -
def OnBtnStopClicked(self, event):
self.animateFlag = False
print('stop')
# - - - - - - - - - - - - - - - - - - -
def OnLblAnimPngsClicked(self, event):
print('clicked')
files = self.selectedGroupObj.PNGs
rslt = dlgAnimFrames.ShowDialog(self.selectedGroupObj.PNGs.keys(), self.selectedAnimObj.pngs)
if rslt == wx.ID_OK:
print('results are ' + str(dlgAnimFrames.GetSelection()))
self.selectedAnimObj.pngs = dlgAnimFrames.GetSelection()
self.UpdateLblAnimPngs()
# - - - - - - - - - - - - - - - - - - -
def create_static_field(self, panel, pos, name, value):
relpos = list(pos)
wx.StaticText(panel, pos=tuple(relpos), label=name)
relpos[0] += 70
field = wx.StaticText(panel, pos=tuple(relpos), label=value)
return field
# - - - - - - - - - - - - - - - - - - -
def create_text_field(self, panel, pos, name, value, on_enter=None):
relpos = list(pos)
wx.StaticText(panel, pos=tuple(relpos), label=name)
relpos[0] += 70
style = wx.TE_PROCESS_ENTER if on_enter != None else wx.TE_LEFT
field = wx.TextCtrl(panel, style=style, pos=tuple(relpos), size=(100,25), value=value)
if on_enter != None:
field.Bind(event=wx.EVT_TEXT_ENTER, handler=on_enter, source=field)
return field
# - - - - - - - - - - - - - - - - - - -
def OnLstGroupSelectionChanged(self, event):
self.movingPngFlag = False
self.animateFlag = False
if type(event) == wx.FocusEvent:
sel = self.lstGroups.GetSelection()
if sel == -1:
return
self.selectedGroup = self.lstGroups.GetString(sel)
else:
self.selectedGroup = self.lstGroups.GetString(event.GetSelection())
self.selectedGroupObj = settings.groups[self.selectedGroup]
# show pngs within this sub-folder
groupPath = os.path.join(settings.projpath, self.selectedGroup)
if type(event) != wx.FocusEvent:
self.ShowPngList()
self.ShowAnimsList()
self.mode = self.Mode.Group
# if there is a .gif within the Graphics/ folder with this group-name,
# then let's assume it is the sprite-sheet and we can preview it
if self.lstPngs.GetSelection() != -1 and hasattr(self.selectedPngObj, 'crop') and len(self.selectedPngObj.crop) > 0:
c = self.selectedPngObj.crop
self.sx = c[0]
self.sy = c[1]
else:
self.sx = 0
self.sy = 0
self.DrawSpriteSheet()
# - - - - - - - - - - - - - - - - - - -
def ShowAnimsList(self):
frame.lblAnims.SetLabel('ANIMS')
frame.lstAnims.Clear()
anims = self.selectedGroupObj.anims
if len(anims) != 0:
items=anims.keys()
items.sort()
self.lstAnims.InsertItems(items, 0)
self.lblAnims.SetLabel('ANIMS ({})'.format(len(items)))
# - - - - - - - - - - - - - - - - - - -
def ShowPngList(self):
self.lstPngs.Clear()
self.lblPngs.SetLabel('PNGs')
files = self.selectedGroupObj.PNGs
if len(files) != 0:
items=files.keys()
items.sort()
self.lstPngs.InsertItems(items, 0)
self.lblPngs.SetLabel('PNGs ({})'.format(len(items)))
# - - - - - - - - - - - - - - - - - - -
def DrawSpriteSheet(self):
sprsheet = "{}.gif".format(self.selectedGroup)
sprsheet = os.path.join(settings.projpath, sprsheet)
if os.path.exists(sprsheet):
self.load_plain_image(sprsheet)
self.sprsheet = self.img
self.OverlayPng()
else:
self.sprsheet = None
# - - - - - - - - - - - - - - - - - - -
def OverlayPng(self):
bmp = self.img.ConvertToBitmap()
dc=wx.MemoryDC(bmp)
dc.SetPen(wx.Pen(wx.RED, 1, wx.PENSTYLE_SHORT_DASH))
dc.SetBrush(wx.Brush(wx.RED, wx.BRUSHSTYLE_TRANSPARENT))
sel = self.lstPngs.GetSelection()
if sel != -1:
pngPath = os.path.join(settings.projpath, self.selectedGroup, self.selectedPng + ".png")
img = wx.Image(pngPath, wx.BITMAP_TYPE_ANY)
sbmp = img.ConvertToBitmap()
dt = img.GetData()
sbmp.SetMaskColour((dt[0], dt[1], dt[2]))
sourcedc = wx.MemoryDC(sbmp)
self.sw = img.GetWidth()
self.sh = img.GetHeight()
dc.Blit(self.sx, self.sy, self.sw, self.sh, sourcedc, 0, 0, wx.COPY, useMask=True)
dc.DrawRectangle(self.sx, self.sy, self.sw, self.sh)
if hasattr(self, 'selNewPngFlag') and self.selNewPngFlag:
dc.DrawRectangle(self.pt1[0] / self.sheetscale, self.pt1[1] / self.sheetscale,
(self.pt2[0] - self.pt1[0]) / self.sheetscale,
(self.pt2[1] - self.pt1[1]) / self.sheetscale)
img = bmp.ConvertToImage()
width = img.GetWidth() * self.sheetscale
height = img.GetHeight() * self.sheetscale
simg = img.Scale(width, height, wx.IMAGE_QUALITY_NORMAL)
self.bmp.SetBitmap(simg.ConvertToBitmap())
self.update_scrollbars()
# - - - - - - - - - - - - - - - - - - -
def _UpdatePngDetails(self):
# import pdb; pdb.set_trace()
self.selectedPng = self.lstPngs.GetString(self.lstPngs.GetSelection())
self.lblPngName.SetLabel(self.selectedPng)
self.selectedPngObj = self.selectedGroupObj.PNGs[self.selectedPng]
self.CopyHitboxesFromSettingsToGui()
if hasattr(self.selectedPngObj, 'crop') and len(self.selectedPngObj.crop) > 0:
c = self.selectedPngObj.crop
self.sx = c[0]
self.sy = c[1]
self.sw = c[2]
self.sh = c[3]
if len(c) > 0:
self.txtCrop.SetValue('{}, {}, {}, {}'.format(c[0], c[1], c[2], c[3]))
else:
self.txtCrop.SetValue('')
else:
self.txtCrop.SetValue('')
pngPath = os.path.join(settings.projpath, self.selectedGroup, self.selectedPng + ".png")
img = wx.Image(pngPath, wx.BITMAP_TYPE_ANY)
self.lblPngSize.SetLabel("{}x{}".format(img.GetWidth(), img.GetHeight()))
if hasattr(self.selectedPngObj, 'adjust'):
a = self.selectedPngObj.adjust
self.txtAdjust.SetValue("{}, {}, {}, {}".format(a[0], a[1], a[2], a[3]))
else:
self.txtAdjust.SetValue("")
# - - - - - - - - - - - - - - - - - - -
def OnLstPngsSelectionChanged(self, event):
print('selchange')
self.animateFlag = False
self.mode = self.Mode.Png
self._UpdatePngDetails()
self.pngPath = os.path.join(settings.projpath, self.selectedGroup, self.selectedPng + ".png")
self.pnlHitboxes.Show()
self.pnlAnimDetails.Hide()
self.lstAnims.SetSelection(wx.NOT_FOUND)
self.load_image(self.pngPath)
# debug info
dbg="ROWSEGS({}) = \n".format(len(self.rowsegs))
for rowseg in self.rowsegs:
dbg += str(rowseg) + "\n"
dbg += "\n\nREPAIRS({}) = \n".format(len(self.repairs))
for repair in self.repairs:
dbg += str(repair) + "\n"
self.txtDebug.SetValue(dbg)
# - - - - - - - - - - - - - - - - - - -
def CopyHitboxesFromSettingsToGui(self):
hbidx = 0
hbnames = [e.name for e in self.Hitbox]
for txtHb in self.lstTxtHboxes:
svals = [str(i) for i in self.selectedPngObj.hitboxes[hbnames[hbidx]]]
ss = ', '.join(svals)
txtHb.SetValue(ss)
hbidx += 1
# - - - - - - - - - - - - - - - - - - -
def CopyHitboxesFromGuiToSettings(self):
hbidx = 0
hbnames = [e.name for e in self.Hitbox]
for txtHb in self.lstTxtHboxes:
vals = [int(i) for i in txtHb.GetValue().split(',')]
self.selectedPngObj.hitboxes[hbnames[hbidx]] = vals
hbidx += 1
# - - - - - - - - - - - - - - - - - - -
def OnLstAnimsSelectionChanged(self, event):
print('OnLstAnimsSelectionChanged')
for txtHbox in self.lstTxtHboxes:
txtHbox.SetValue('0, 0, 0, 0')
self.animidx = 0
self.animateDir = True
self.SetAnimSelection(event.GetSelection())
# - - - - - - - - - - - - - - - - - - -
def SetAnimSelection(self, idx):
self.selectedAnim = self.lstAnims.GetString(idx)
if self.selectedAnim in self.selectedGroupObj.anims:
self.selectedAnimObj = self.selectedGroupObj.anims[self.selectedAnim]
else:
self.selectedAnimObj = None
self.UpdatePnlAnimDetails()
self.pnlHitboxes.Hide()
self.lstPngs.SetSelection(wx.NOT_FOUND)
self.pnlAnimDetails.Show()
# - - - - - - - - - - - - - - - - - - -
def UpdatePnlAnimDetails(self):
if self.selectedAnimObj == None:
self.lblAnimName.SetLabel("<none>")
self.lblAnimPngs.SetLabel("<none selected>")
return
self.lblAnimName.SetLabel(self.selectedAnim)
self.txtPingPong.SetValue(str(self.selectedAnimObj.pingpong))
self.txtRelx.SetValue(', '.join(map(str, self.selectedAnimObj.relx)))
self.txtRely.SetValue(', '.join(map(str, self.selectedAnimObj.rely)))
self.txtFrame.SetValue(', '.join(map(str, self.selectedAnimObj.frame)))
self.UpdateLblAnimPngs()
# - - - - - - - - - - - - - - - - - - -
def UpdateLblAnimPngs(self):
if len(self.selectedAnimObj.pngs) == 0:
self.lblAnimPngs.SetLabel("<none selected>")
else:
self.lblAnimPngs.SetLabel(str(self.selectedAnimObj.pngs))
# - - - - - - - - - - - - - - - - - - -
def OnLstAnimsContextMenu(self, event):
if not hasattr(self, 'popupAnims'):
self.popupAnims = wx.Menu()
mnuitmAddAnim = self.popupAnims.Append(wx.ID_ANY, 'Add anim', 'Add anim')
self.Bind(event=wx.EVT_MENU, handler=self.OnAddAnim, source=mnuitmAddAnim)
if not hasattr(self, 'selectedGroupObj'):
return
self.PopupMenu(self.popupAnims)
# - - - - - - - - - - - - - - - - - - -
def OnAddAnim(self, event):
global projectNotSaved
print('OnAddAnim')
# ask user what to call the anim
dlg = wx.TextEntryDialog(self, 'Name of new anim:', value='')
dlg.ShowModal()
rslt = dlg.GetValue()
if not (len(rslt) > 0):
return
# add to self.selectedGroupObj.anims dict
if not hasattr(self.selectedGroupObj, 'anims'):
self.selectedGroupObj.anims = { }
anim = Anim(rslt)
self.selectedGroupObj.anims[rslt] = anim
self.selectedAnimObj = anim
# add it as an item in the lstAnims gui object
idx = self.lstAnims.Append(rslt)
self.lstAnims.SetSelection(idx)
self.SetAnimSelection(idx)
projectNotSaved = True
# - - - - - - - - - - - - - - - - - - -
def AddGroup(self, name):
self.lstGroups.InsertItems([str(name)],self.lstGroups.GetCount())
# - - - - - - - - - - - - - - - - - - -
def OnFrameClose(self, event):
global projectNotSaved
if (type(event) == wx.CommandEvent or (type(event) == wx.CloseEvent and event.CanVeto())) and projectNotSaved:
if wx.MessageBox("The project has not been saved... Continue to save and close?",
"Please confirm",
wx.ICON_QUESTION | wx.YES_NO) != wx.YES:
if type(event) == wx.CloseEvent:
event.Veto()
return
SaveSettings()
return
SaveSettings()
self.Destroy()
# - - - - - - - - - - - - - - - - - - -
def create_labeled_list_box(self, panel, label, pos, size, choices):
lbl = wx.StaticText(panel, pos=pos, label=label)
pos = (pos[0], pos[1] + 18)
return lbl, wx.ListBox(panel, pos=pos, size=size, choices=choices, style=0)
# - - - - - - - - - - - - - - - - - - -
def create_menu(self):
menu_bar = wx.MenuBar()
file_menu = wx.Menu()
options_menu = wx.Menu()
mnuitmFileOpenFolder = file_menu.Append(wx.ID_ANY, 'Open Folder\tCTRL-O', 'Open a folder with MP3s')
mnuitmFileScanForNewItems = file_menu.Append(wx.ID_ANY, 'Scan for New Items\tF5')
mnuitmFileSaveHitboxes = file_menu.Append(wx.ID_ANY, 'Save Hitboxes\tCTRL-S', "Save hitboxes to 'hitboxes.h'")
self.toggle_view = options_menu.AppendCheckItem(wx.ID_ANY, 'Toggle View\tCTRL-t', 'Toggle between png and c64 view')
self.toggle_colours = options_menu.AppendCheckItem(wx.ID_ANY, 'Toggle Colours\tCTRL-R', 'Toggle colours on/off')
self.toggle_grid = options_menu.AppendCheckItem(wx.ID_ANY, 'Toggle Grid\tCTRL-G', 'Toggle grid')
self.zoom_in = options_menu.Append(wx.ID_ANY, 'Zoom in\tCtrl-.', 'Zoom in')
self.zoom_out = options_menu.Append(wx.ID_ANY, 'Zoom out\tCtrl-,', 'Zoom out')
self.faster = options_menu.Append(wx.ID_ANY, 'Anim faster\tCtrl-=', 'Anim faster')
self.slower = options_menu.Append(wx.ID_ANY, 'Anim slower\tCtrl--', 'Anim slower')
menu_bar.Append(file_menu, 'File')
self.Bind(event=wx.EVT_MENU, handler=self.OnOpenFolder, source=mnuitmFileOpenFolder)
self.Bind(event=wx.EVT_MENU, handler=self.OnScanForNewItems, source=mnuitmFileScanForNewItems)
self.Bind(event=wx.EVT_MENU, handler=self.OnSaveHitboxes, source=mnuitmFileSaveHitboxes)
menu_bar.Append(options_menu, 'Options')
self.Bind(event=wx.EVT_MENU, handler=self.OnToggleView, source=self.toggle_view)
self.Bind(event=wx.EVT_MENU, handler=self.OnToggleColour, source=self.toggle_colours)
self.Bind(event=wx.EVT_MENU, handler=self.OnToggleGrid, source=self.toggle_grid)
self.Bind(event=wx.EVT_MENU, handler=self.OnZoomIn, source=self.zoom_in)
self.Bind(event=wx.EVT_MENU, handler=self.OnZoomOut, source=self.zoom_out)
self.Bind(event=wx.EVT_MENU, handler=self.OnFaster, source=self.faster)
self.Bind(event=wx.EVT_MENU, handler=self.OnSlower, source=self.slower)
# Check for unique mac-osx 'apple menu'
apple_menu = menu_bar.OSXGetAppleMenu()
if apple_menu:
quit_menu_item = apple_menu.FindItemByPosition(apple_menu.GetMenuItemCount()-1)
self.Bind(wx.EVT_MENU, self.OnFrameClose, quit_menu_item)
self.SetMenuBar(menu_bar)
# - - - - - - - - - - - - - - - - - - -
def OnFaster(self, event):
if self.speed > 100:
self.speed -= 100
self.timer.Start(self.speed)
# - - - - - - - - - - - - - - - - - - -
def OnSlower(self, event):
if self.speed < 500:
self.speed += 100
self.timer.Start(self.speed)
# - - - - - - - - - - - - - - - - - - -
def OnZoomIn(self, event):
if self.mode == self.Mode.Png or self.animateFlag:
self.scale += 1
self.load_image()
elif self.mode == self.Mode.Group:
self.sheetscale += 1
self.DrawSpriteSheet()
# - - - - - - - - - - - - - - - - - - -
def OnZoomOut(self, event):
if self.mode == self.Mode.Png or self.animateFlag:
if self.scale > 1:
self.scale -= 1
self.load_image()
elif self.mode == self.Mode.Group:
if self.sheetscale > 1:
self.sheetscale -= 1
self.DrawSpriteSheet()
# - - - - - - - - - - - - - - - - - - -
def OnToggleGrid(self, event):
self.ConvertToC64()
self.update_image()
# - - - - - - - - - - - - - - - - - - -
def OnToggleView(self, event):
self.update_image()
def OnToggleColour(self, event):
self.ConvertToC64()
self.update_image()
# - - - - - - - - - - - - - - - - - - -
def OnOpenFolder(self, event):
title = "Choose a directory:"
dlg = wx.DirDialog(self, title, style=wx.DD_DEFAULT_STYLE)
if dlg.ShowModal() == wx.ID_OK:
SetGraphicsDirectory(dlg.GetPath())
# - - - - - - - - - - - - - - - - - - -
def OnScanForNewItems(self, event):
global settings, projectNotSaved
settings.update()
projectNotSaved = True
UpdateLists()
# - - - - - - - - - - - - - - - - - - -
def OnSaveHitboxes(self, event):
global settings
# read out all hitbox values for each group, and write them to a file
outfile = open('hitboxes.h', 'wt')
outfile.write("""typedef struct
{
char name[64];
unsigned short boxes[4][4];
} type_hitbox;
type_hitbox lstHitBoxes[] =
{\n""")
for group in settings.groups:
for png in settings.groups[group].PNGs:
validflag = False
hbs = settings.groups[group].PNGs[png].hitboxes
for hb in hbs.values():
if hb != [0, 0, 0, 0]:
validflag = True
if validflag:
# write this one to file
outfile.write(" {\n")
outfile.write(' \"Graphics/{}/{}.png\",\n'.format(group, png))
outfile.write(' ' + str(hbs['Head'])[1:-1] + ',\n')
outfile.write(' ' + str(hbs['Torso'])[1:-1] + ',\n')
outfile.write(' ' + str(hbs['Feet'])[1:-1] + ',\n')
outfile.write(' ' + str(hbs['Attack'])[1:-1] + '\n')
outfile.write(' },\n')
print group
outfile.write(""" { 0 }
};""")
# - - - - - - - - - - - - - - - - - - -
def create_bitmap_area(self, panel, pos, size):
self.spnl = wx.ScrolledWindow(panel, pos=pos, size=size)
bmp = wx.StaticBitmap(parent=self.spnl)
self.spnl.SetConstraints(LayoutAnchors(self.spnl, left=True, top=True, right=True, bottom=True))
# mouse-related events
bmp.Bind(wx.EVT_MOUSE_EVENTS, self.OnBmpMouseEvents)
bmp.Bind(wx.EVT_MOTION, self.OnBmpMouseMove)
self.spnl.Bind(wx.EVT_KEY_DOWN, self.OnSpnlKeyDown)
self.spnl.Bind(wx.EVT_MOUSE_EVENTS, self.OnSpnlMouseEvents)
return bmp
# - - - - - - - - - - - - - - - - - - -
def OnSpnlMouseEvents(self, event):
if event.LeftUp() and self.movingPngFlag:
self.movingPngFlag = False
self.sx = int(self.sx)
self.sy = int(self.sy)
self.update_cropdim()
event.Skip()
# - - - - - - - - - - - - - - - - - - -
def MovePngSelectionOnKeyPress(self, c):
idx = self.lstPngs.GetSelection()
if c == wx.WXK_UP:
if idx > 0:
idx -= 1