File: leveledit.py

package info (click to toggle)
seahorse-adventures 1.1+dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,936 kB
  • sloc: python: 8,640; makefile: 8; sh: 3
file content (1393 lines) | stat: -rwxr-xr-x 41,664 bytes parent folder | download | duplicates (4)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
#!/usr/bin/python
"""<title>a simple level editor for pygame</title>
<pre>
usage: leveledit level.tga [tiles.tga] [codes.tga] [tile_w] [tile_h]
windows: python leveledit level.tga [tiles.tga] [codes.tga] [tile_w] [tile_h]

options:
  -h, --help            show this help message and exit
  -tTILES, --tiles=TILES
                        filename of the tiles image
  -cCODES, --codes=CODES
                        file name of the codes image
  --tw=TILE_W           tile width
  --th=TILE_H           tile height
  --vw=VIEW_W           view width
  --vh=VIEW_H           view height
  --sw=SCREEN_W         screen width
  --sh=SCREEN_H         screen height
  --c=CLASS             class (e.g. pgu.tilevid.Tilevid)
  --tile                use pgu.tilevid.Tilevid
  --iso                 use pgu.isovid.Isovid
  --hex                 use pgu.hexvid.Hexvid
  -a, --app             set application level defaults

example:
leveledit level.tga tiles.tga codes.tga 16 16

note:
the editor can only edit tga files.  the output files will
have the "tile" layer in the red channel, the "bkgr" layer
in the green channel, and the "code" layer in the blue channel.

you may edit default options in leveledit.ini

interface:
- menus for common commands
- toolbox
- tile edit area
    left click to use current tool
    right click to select a tile
    middle drag to move around the level
- tile select area
    click to select a tile
- code select area
    click to select a code

keys:
l - load
s - save
p - preview

a - select all
z - undo
c - copy selection to clipboard
v - paste clipboard at selection origin
delete - delete selection
f - fill selection

t - switch tile & bkgr layers

arrows - change tile
shift+arrows - scroll screen by 1/8 screen size jumps
ctrl+arrows - scroll screen by full screen size jumps
return - toggle fullscreen
</pre>
"""

import os,sys    
from optparse import OptionParser
from ConfigParser import ConfigParser
import pygame
from pygame.locals import *

# the following line is not needed if pgu is installed
import os
import sys; sys.path.insert(0, os.path.join('lib'))

from pgu import gui, html, tilevid, isovid, hexvid



#whatever...
ini_fname = "leveledit.ini"
ini = ConfigParser()
cfg = {}

class _app(gui.Container):
    def __init__(self):
        gui.Container.__init__(self)
        #self.cls = "desktop"
        #self.background = gui.Box(self.style.background)

        self.screen_w = cfg['screen_w']
        self.screen_h = cfg['screen_h']
        self.screen = pygame.display.set_mode((self.screen_w,
            self.screen_h), SWSURFACE)

        self.fname = cfg['fname']
        
        k = cfg['class']
        parts = k.split(".")
        n = ".".join(parts[:-1])
        m = __import__(n,globals(),locals(),parts[-1])
        c = getattr(m,parts[-1])
        self.level = c()

        #self.level = pygame.image.load(self.level_fname)
        #self.level = tilevid.Tilevid()
        #g = self.level = isovid.Isovid()
        
    
        if self.fname != None:
            self.level.tga_load_level(self.fname,1)
        else:
            self.level.resize((cfg['width'],cfg['height']),1)
        #self.level_w, self.level_h = (self.level.get_width(), self.level.get_height())
        self.level_w, self.level_h = len(self.level.tlayer[0]),len(self.level.tlayer)

        self.tiles_last_ctime = None
        self.codes_last_ctime = None

        self.load_tiles_and_codes()
        



        
        
        self.tile = 0
        self.code = 0
        
        self.mode = 'tile'
        self.clipboard = None
        self.history = []
        #self.modrect = pygame.Rect(0xffff,0xffff,-0xffff,-0xffff)
        
        self.changes = []
        self.dirty = 0
        

    def load_tiles_and_codes(self):
        #
        #

        self.tile_w, self.tile_h = cfg['tile_w'], cfg['tile_h']

        self.tiles_fname = cfg['tiles']
        if os.path.isfile(self.tiles_fname):
            # we check to see if the ctime is the same.

            newctime = os.stat(self.tiles_fname)[9]
            if newctime <= self.tiles_last_ctime:
                #nothing to do, so we return.
                return

            self.tiles_last_ctime = newctime

            self.tiles = pygame.image.load(self.tiles_fname)
        else:
            self.tiles = hex_image(self)
        self.tiles_w, self.tiles_h = (self.tiles.get_width(),
            self.tiles.get_height())
        self.level.tga_load_tiles(self.tiles,(self.tile_w,self.tile_h))

        self.codes_fname = cfg['codes']
        if os.path.isfile(self.codes_fname):
            newctime = os.stat(self.codes_fname)[9]
            if newctime <= self.codes_last_ctime:
                #nothing to do, so we return.
                return
            self.codes_last_ctime = newctime

            self.codes = pygame.image.load(self.codes_fname)
        else:
            self.codes = hex_image(self)
                        
        self.codes_w, self.codes_h = (self.codes.get_width(),
            self.codes.get_height())

        
        tmp = self.level.tiles
        self.level.tiles = [None for i in xrange(0,256)]
        self.level.tga_load_tiles(self.codes,(self.tile_w,self.tile_h))
        self.level.codes = self.level.tiles
        self.level.tiles = tmp






    def mod(self,rect):
        self.dirty = 1
        self.changes.append((pygame.Rect(rect),self.copy(rect)))
    

    def view_init(self,dw,dh):        
        
        self.view_w = dw #/ self.tile_w
        self.view_h = dh #/ self.tile_h
        
        if 'view_w' in cfg and cfg['view_w'] != 0:
            self.view_w = min(self.view_w, cfg['view_w'])
        if 'view_h' in cfg and cfg['view_h'] != 0:
            self.view_h = min(self.view_h, cfg['view_h'])
        
        #self.view_w = min(self.level.size[0],self.view_w)
        #self.view_h = min(self.level.size[1],self.view_h)
        
        #print self.view_w,self.view_h
        
        #self.view = self.level.subsurface((0,0,self.view_w,self.view_h))
        self.select = Rect(0,0,self.level.size[0],self.level.size[1]) #self.view_w,self.view_h)
        
    def fill(self,rect,v):
        lvl = self.level
        w,h = lvl.size
        
        for layer,n in [ (lvl.tlayer,0), (lvl.blayer,1), (lvl.clayer,2) ]:
            for y in range(0,rect.h):
                for x in range(0,rect.w):
                    tx,ty = x+rect.x,y+rect.y
                    if tx >= 0 and tx < w and ty >= 0 and ty < h: layer[ty][tx] = v[n]
        
    def copy(self,rect):
        data = [[[None for x in range(0,rect.w)] for y in range(0,rect.h)] for l in range(0,4)] 

        lvl = self.level
        w,h = lvl.size
        
        for layer,n in [ (lvl.tlayer,0), (lvl.blayer,1), (lvl.clayer,2) ]:
            for y in range(0,rect.h):
                for x in range(0,rect.w):
                    tx,ty = x+rect.x,y+rect.y
                    if tx >= 0 and tx < w and ty >= 0 and ty < h: data[n][y][x] = layer[ty][tx]
        return data
                
    def paste(self,rect,data):
        lvl = self.level
        w,h = lvl.size
        
        for layer,n in [ (lvl.tlayer,0), (lvl.blayer,1), (lvl.clayer,2) ]:
            for y in range(0,rect.h):
                for x in range(0,rect.w):
                    tx,ty = x+rect.x,y+rect.y
                    v = data[n][y][x]
                    if v != None and tx >= 0 and tx < w and ty >= 0 and ty < h: layer[ty][tx] = v
        
        
    def archive(self):
        if not len(self.changes): return
        
        self.dirty = 1
        h = self.history
        if len(h) >= 32:
            del h[0]
        #c = pygame.Surface((self.view_w,self.view_h),SWSURFACE,self.view)
        #c.fill((0,0,0,0))
        #c.blit(self.view,(0,0))
        
        lvl = self.level
        #ox,oy = lvl.screen_to_tile((0,0))
        #bx,by = lvl.screen_to_tile((self.vdraw.rect.w,self.vdraw.rect.h))
        
        #rect = pygame.Rect(ox,oy,bx-ox,by-oy)
        #print self.modrect
        
        h.append(self.changes)
        self.changes = []
        
    def undo(self):
        if len(self.changes): self.archive()
            
        if len(self.history) == 0: return
        
        self.dirty = 1
        changes = self.history.pop()
        
        changes.reverse()
        for rect,data in changes:
            self.paste(rect,data)
            
        self.vdraw.repaint()
        self.tpicker.repaint() #huh?
        
        self.changes = []
        return
        
        self.level.fill((0,0,0,0),(off[0],off[1],self.view_w,self.view_h))
        self.level.blit(c,off)
        self.vdraw.repaint()
        self.tpicker.repaint()
        
    def __setattr__(self,k,v):
        self.__dict__[k] = v
        
        if k == 'view':
            if hasattr(self,'vdraw'): self.vdraw.repaint()
        
        if k == 'tile':
            if hasattr(self,'tpicker'): self.tpicker.repaint()
                
        if k == 'code':
            if hasattr(self,'cpicker'): self.cpicker.repaint()
        
            
    def event(self,e):
        if e.type is KEYDOWN:
            for key,cmd,value in keys:
                if e.key == key:
                    cmd(value)
                    return
        return gui.Container.event(self,e)
    

def hex_image(self):
    if not hasattr(self,'tiles_w'): self.tiles_w = 256
    if not hasattr(self,'tiles_h'): self.tiles_h = 256
    rimg = pygame.Surface((self.tiles_w,self.tiles_h)).convert_alpha()
    rimg.fill((0,0,0,0))
    w,h = self.tiles_w / self.tile_w, self.tiles_h / self.tile_h
    n = 0
    fnt = pygame.font.SysFont("helvetica",self.tile_h-1)
    for y in range(0,h):
        for x in range(0,w):
            n = x+y*w
            if n != 0:
                xx,yy = x*self.tile_w,y*self.tile_h
                img = fnt.render("%02X"%n,0,(0,0,0))
                img = pygame.transform.scale(img,(self.tile_w-1,self.tile_h-1))
                rimg.blit(img,(xx+1,yy+1))
                img = fnt.render("%02X"%n,0,(255,255,255))
                img = pygame.transform.scale(img,(self.tile_w-1,self.tile_h-1))
                rimg.blit(img,(xx,yy))
    return rimg


class tpicker(gui.Widget):
    def __init__(self):
        gui.Widget.__init__(self)
        self.style.width = app.tiles_w
        self.style.height = app.tiles_h
        
    def paint(self,s):
        s.fill((128,128,128))
        s.blit(app.tiles,(0,0))
        w = app.tiles_w/app.tile_w
        x,y = app.tile%w,app.tile/w
        off = x*app.tile_w,y*app.tile_h
        pygame.draw.rect(s,(255,255,255),(off[0],off[1],app.tile_w,app.tile_h),2)
        
    def event(self,e):
        if (e.type is MOUSEBUTTONDOWN and e.button == 1) or (e.type is MOUSEMOTION and e.buttons[0] == 1 and self.container.myfocus == self):
            w = app.tiles_w/app.tile_w
            x,y = e.pos[0]/app.tile_w,e.pos[1]/app.tile_h
            n = x+y*w
            self.set(n)
            if app.mode not in ('tile','bkgr'):
                app.tools['tile'].click()
    
    def set(self,n):
        if n < 0 or n >= len(app.level.tiles) or app.level.tiles[n] == None: return
        app.tile = n


class cpicker(gui.Widget):
    def __init__(self):
        gui.Widget.__init__(self)
        self.style.width = app.codes_w
        self.style.height = app.codes_h
        
    def paint(self,s):
        s.fill((128,128,128))
        s.blit(app.codes,(0,0))
        w = app.codes_w/app.tile_w
        x,y = app.code%w,app.code/w
        off = x*app.tile_w,y*app.tile_h
        pygame.draw.rect(s,(255,255,255),(off[0],off[1],app.tile_w,app.tile_h),2)
        
    def event(self,e):
        if (e.type is MOUSEBUTTONDOWN and e.button == 1) or (e.type is MOUSEMOTION and e.buttons[0] == 1 and self.container.myfocus == self):
            w = app.codes_w/app.tile_w
            x,y = e.pos[0]/app.tile_w,e.pos[1]/app.tile_h
            n = x+y*w
            self.set(n)
            app.tools['code'].click()
    
    def set(self,n):
        if n < 0 or n >= len(app.level.codes) or app.level.codes[n] == None: return
        app.code = n



class vwrap(gui.Table):
    def __init__(self,**params):
        gui.Table.__init__(self,**params)
        self.style.width = app.view_w #* app.tile_w
        self.style.height = app.view_h #* app.tile_h
        w,h = self.rect.w,self.rect.h = self.style.width,self.style.height
        
        sw = 16
        
        self.vdraw = e = vdraw(width=w-sw,height=h-sw)
        self.add(e,0,0)
        
        rect = pygame.Rect(0,0,app.level.size[0],app.level.size[1])
        tcorners = [rect.topleft,rect.topright,rect.bottomright,rect.bottomleft] 
        corners = [app.level.tile_to_view(tcorners[n]) for n in range(0,4)]
        
        minx,miny,maxx,maxy = 0xffff,0xffff,-0xffff,-0xffff
        for x,y in corners:
            minx,miny,maxx,maxy = min(minx,x),min(miny,y),max(maxx,x),max(maxy,y)

        minx -= w/2
        maxx -= w/2
        miny -= h/2
        maxy -= h/2
        
        self.vs = e = gui.VSlider(0,miny,maxy,sw*4,width=sw,height=h-sw)
        self.add(e,1,0)
        e.connect(gui.CHANGE,self.move_y,e)
        
        self.hs = e = gui.HSlider(0,minx,maxx,sw*4,width=w-sw,height=sw)
        self.add(e,0,1)
        e.connect(gui.CHANGE,self.move_x,e)
        
    def move_x(self,value):
        v = value.value
        if app.level.view.x != v:
            app.level.view.x = v
            app.vdraw.repaint()
    
    def move_y(self,value):
        v = value.value
        if app.level.view.y != v:
            app.level.view.y = v
            app.vdraw.repaint()
        
    def adjust(self):
        self.vs.value = app.level.view.y
        self.hs.value = app.level.view.x
        

class vdraw(gui.Widget):
    def repaint(self):
        self.container.adjust()
        gui.Widget.repaint(self)
        
    def __init__(self,**params):
        gui.Widget.__init__(self,**params)
        #self.style.width = app.view_w #* app.tile_w
        #self.style.height = app.view_h #* app.tile_h
        self.rect.w,self.rect.h = self.style.width,self.style.height
        
        s = pygame.Surface((self.rect.w,self.rect.h))
        clrs = [(148,148,148),(108,108,108)]
        inc = 7
        for y in range(0,self.rect.w/inc):
            for x in range(0,self.rect.h/inc):
                s.fill(clrs[(x+y)%2],(x*inc,y*inc,inc,inc))
        self.bg = s

        s = pygame.Surface((self.rect.w,self.rect.h)).convert_alpha()
        s.fill((0,0,0,0))
        for x in range(0,app.view_w):
            pygame.draw.line(s,(0,0,0),(self.rect.w*x/app.view_w,0),(self.rect.w*x/app.view_w,self.rect.h))
        for y in range(0,app.view_h):
            pygame.draw.line(s,(0,0,0),(0,self.rect.h*y/app.view_h),(self.rect.w,self.rect.h*y/app.view_h))
        self.grid = s
        
        self.pos = 0,0

        
    def paint(self,s):
        #print s
        #print s.get_width(),s.get_height(),s.get_clip()
        #s.blit(self.bg,(0,0))
        s.fill((128,128,128))

        #make sure to clamp the bounds
        if app.level.bounds != None:
            app.level.view.clamp_ip(app.level.bounds)
        
        #draw border        
        rect = pygame.Rect(0,0,app.level.size[0],app.level.size[1])
        tcorners = [rect.topleft,rect.topright,rect.bottomright,rect.bottomleft]
        corners = [app.level.tile_to_screen(tcorners[n]) for n in range(0,4)]
        pygame.draw.lines(s,(255,255,0),1,corners,2)

        
        #s.fill((0,0,0))
        #0/0
        app.level.paint(s)
        
        tmp_tiles = app.level.tiles
        tmp_tlayer = app.level.tlayer
        tmp_blayer = app.level.blayer
        
        app.level.tiles = app.level.codes
        app.level.tlayer = app.level.clayer
        app.level.blayer = None
        
        app.level.paint(s)
        
        app.level.tiles = tmp_tiles
        app.level.tlayer = tmp_tlayer
        app.level.blayer = tmp_blayer
        
        rect = pygame.Rect(self.pos[0],self.pos[1],1,1)
        tcorners = [rect.topleft,rect.topright,rect.bottomright,rect.bottomleft]
        corners = [app.level.tile_to_screen(tcorners[n]) for n in range(0,4)]
        pygame.draw.lines(s,(196,196,196),1,corners,2)

        rect = pygame.Rect(app.select.x,app.select.y,app.select.w,app.select.h) 
        tcorners = [rect.topleft,rect.topright,rect.bottomright,rect.bottomleft]
        corners = [app.level.tile_to_screen(tcorners[n]) for n in range(0,4)]
        pygame.draw.lines(s,(255,255,255),1,corners,2)


                
        
        #s.blit(self.grid,(0,0))
        #r = app.select
        #pygame.draw.rect(s,(255,255,255,128),Rect(r.x*self.rect.w/app.view_w,r.y*self.rect.h/app.view_h,r.w*self.rect.w/app.view_w,r.h*self.rect.h/app.view_h),4)
        
    def event(self,e):
        if e.type is MOUSEMOTION:
            self.getpos(e)
        if (e.type is MOUSEBUTTONDOWN and e.button == 3) or (e.type is MOUSEMOTION and e.buttons[2]==1 and self.container.myfocus == self):
            self.picker_down(e)
        if e.type is MOUSEBUTTONDOWN and e.button == 1:
            self.getpos(e)
            a = '%s_down'%app.mode
            if hasattr(self,a): getattr(self,a)(e)
        if e.type is MOUSEMOTION and e.buttons[0] and self.container.myfocus == self:
            a = '%s_drag'%app.mode
            if hasattr(self,a): getattr(self,a)(e)
        if e.type is MOUSEBUTTONUP and e.button == 1:
            a = '%s_up'%app.mode
            if hasattr(self,a): getattr(self,a)(e)
        if e.type is MOUSEBUTTONDOWN and e.button == 2:
            self.move_down(e)
        if e.type is MOUSEMOTION and e.buttons[1] and self.container.myfocus == self:
            self.move_drag(e)
    
    #move
    def move_down(self,e):
        self.moff = app.level.view.x,app.level.view.y
        self.m1 = e.pos
        
    def move_drag(self,e):
        m1 = self.m1
        m2 = e.pos
        #app.view = app.level.subsurface((x,y,app.view_w,app.view_h))
        app.level.view.x,app.level.view.y = self.moff[0] + m1[0]-m2[0], self.moff[1]+m1[1]-m2[1]
        self.repaint()
            
    #picker
    def picker_down(self,e):
        pos = self.getpos(e)
        #tx,ty = app.level.screen_to_tile(e.pos)
        #r,g,b,a = app.view.get_at(pos)
        if pos == None: return 
        tx,ty = pos
        
        if app.mode == 'tile':
            app.tile = app.level.tlayer[ty][tx]
        if app.mode == 'bkgr':
            app.tile = app.level.blayer[ty][tx]
        app.code = app.level.clayer[ty][tx]
        
    
    
    #tile
    def tile_down(self,e):
        app.archive()
        self.tile_drag(e)
    
    def tile_drag(self,e):
        pos = self.getpos(e)
        #r,g,b,a = app.view.get_at(pos)
        #r = app.tile
        #app.view.set_at(pos,(r,g,b))
        
        if pos == None: return
        tx,ty = pos
        app.mod(pygame.Rect(tx,ty,1,1))
        app.level.tlayer[ty][tx] = app.tile
        self.repaint()
        
    #bkgr
    def bkgr_down(self,e):
        app.archive()
        self.bkgr_drag(e)
    
    def bkgr_drag(self,e):
        pos = self.getpos(e)
        #r,g,b,a = app.view.get_at(pos)
        #g = app.tile
        #app.view.set_at(pos,(r,g,b))
        if pos == None: return
        tx,ty = pos
        app.mod(pygame.Rect(tx,ty,1,1))
        app.level.blayer[ty][tx] = app.tile
        self.repaint()
        
        
    #code
    def code_down(self,e):
        app.archive()
        self.code_drag(e)
    
    def code_drag(self,e):
        pos = self.getpos(e)
        #r,g,b,a = app.view.get_at(pos)
        #b = app.code
        #app.view.set_at(pos,(r,g,b))
        if pos == None: return
        tx,ty =  pos
        app.mod(pygame.Rect(tx,ty,1,1))
        app.level.clayer[ty][tx] = app.code
        self.repaint()
        
    #eraser
    def eraser_down(self,e):
        app.archive()
        self.eraser_drag(e)
    
    def eraser_drag(self,e):
        pos = self.getpos(e)
        if pos == None: return
        tx,ty = pos
        app.mod(pygame.Rect(tx,ty,1,1))
        app.level.tlayer[ty][tx] = 0
        app.level.blayer[ty][tx] = 0
        app.level.clayer[ty][tx] = 0
        #app.view.set_at(pos,(0,0,0))
        self.repaint()
        
    def getpos(self,e):
        tx,ty = app.level.screen_to_tile(e.pos)
        
        if tx < 0 or ty < 0 or tx >= app.level.size[0] or ty >= app.level.size[1]: return None
        
        if (tx,ty) != self.pos:
            self.pos = tx,ty
            self.repaint()
        return tx,ty
        
        x,y = e.pos[0]/app.tile_w,e.pos[1]/app.tile_h
        x = min(max(0,x),app.view_w-1)
        y = min(max(0,y),app.view_h-1)
        return x,y
    
    def getpos2(self,e):
        tx,ty = app.level.screen_to_tile(e.pos)
        
        return tx+1,ty+1
        
        w = app.tile_w
        h = app.tile_h
        x,y = (e.pos[0]+w/2)/app.tile_w,(e.pos[1]+h/2)/app.tile_h
        x = min(max(0,x),app.view_w)
        y = min(max(0,y),app.view_h)
        return x,y
    
    #select
    def select_down(self,e):
        pos = self.getpos2(e)
        pos = pos[0]-1,pos[1]-1
        app.select = Rect(pos[0],pos[1],1,1)
        self.repaint()
        
    def select_drag(self,e):
        pos = self.getpos2(e)
        app.select = Rect(app.select.x,app.select.y,pos[0]-app.select.x,pos[1]-app.select.y)
        app.select.w = max(1,app.select.w)
        app.select.h = max(1,app.select.h)

        self.repaint()
        

def cmd_all(value):
    app.select = Rect(0,0,app.level.size[0],app.level.size[1])
    app.vdraw.repaint()
    
    #print 'deprecated in v0.5'
    
def cmd_shrink(value):    
    if app.select.w <= 2 or app.select.h <= 2: return
    app.select.x += 1
    app.select.y += 1
    app.select.w -= 2
    app.select.h -= 2

def cmd_undo(value):
    app.undo()
    
def cmd_redo(value):
    pass
    
def cmd_copy(value):
    #next version of pygame?
    #app.clipboard = app.tile.subsurface(app.select).copy()
    
    data = app.copy(app.select)
    app.clipboard = pygame.Rect(app.select),data
    return
    
    #s = app.view.subsurface(app.select)
    #app.clipboard = pygame.Surface((app.select.w,app.select.h),SWSURFACE,s)
    #app.clipboard.fill((0,0,0,0))
    #app.clipboard.blit(s,(0,0))
    
    print app.clipboard.get_at((0,0))
    
def cmd_paste(value):
    if app.clipboard != None:
        app.archive()
        #app.view.fill((0,0,0,0),(app.select[0],app.select[1],app.clipboard.get_width(),app.clipboard.get_height()))
        #app.view.blit(app.clipboard,app.select)
        #app.vdraw.repaint()
        
        rect,data = app.clipboard
        rect = pygame.Rect(app.select.x,app.select.y,rect.w,rect.h)
        
        app.mod(rect)
        app.paste(rect,data)
        app.vdraw.repaint()

class Restart(Exception):
    pass
    
def _dirty(fnc,v):
    dialog = DirtyDialog()
    def onchange(value):
        value.close()
        return fnc(v)
    dialog.connect(gui.CHANGE,onchange,dialog)
    dialog.open()

def cmd_new(value):
    if app.dirty: _dirty(_cmd_new,value)
    else: _cmd_new(value)
    
def _cmd_new(value):
    dialog = NewDialog()
    
    def onchange(value):
        value.close()
        vv = value.value
        ok = 0
        try:        
            width,height,tile_w,tile_h,codes,tiles,klass = int(vv['width'].value),int(vv['height'].value),int(vv['tile_w'].value),int(vv['tile_h'].value),vv['codes'].value,vv['tiles'].value,vv['class'].value
            global cfg
            cfg['fname'] = None
            cfg['width'] = width
            cfg['height'] = height
            cfg['tile_w'] = tile_w
            cfg['tile_h'] = tile_h
            cfg['codes'] = codes
            cfg['tiles'] = tiles
            cfg['class'] = klass
            ok = 1
        except Exception, v:
            ErrorDialog("New failed.",v).open()
        if ok:
            raise Restart()
    
    dialog.connect(gui.CHANGE,onchange,dialog)
    dialog.open()

    
def cmd_open(value):
    if app.dirty: _dirty(_cmd_open,value)
    else: _cmd_open(value)

def _cmd_open(value):
    dialog = OpenDialog()
    
    def onchange(value):
        value.close()
        vv = value.value
        ok = 0
        
        try:        
            
            fname,tile_w,tile_h,codes,tiles,klass = vv['fname'].value,int(vv['tile_w'].value),int(vv['tile_h'].value),vv['codes'].value,vv['tiles'].value,vv['class'].value
            global cfg
            cfg['fname'] = fname
            cfg['tile_w'] = tile_w
            cfg['tile_h'] = tile_h
            cfg['codes'] = codes
            cfg['tiles'] = tiles
            cfg['class'] = klass

            
            ok = 1
        except Exception,v:
            ErrorDialog("Open failed.",v).open()
            
        if ok: raise Restart()

    
    dialog.connect(gui.CHANGE,onchange,dialog)
    dialog.open()

def cmd_saveas(value):
    dialog = SaveAsDialog()
    
    def onchange(value):
        value.close()
        vv = value.value
        fname = vv['fname'].value
        if len(fname) == 0:
            ErrorDialog("Save As failed.","File Name too short!").open()
            return
        global cfg
        app.fname = cfg['fname'] = fname
        return cmd_save(None)
        
    dialog.connect(gui.CHANGE,onchange,dialog)
    dialog.open()
        
def cmd_cut(value):
    cmd_copy(value)
    cmd_delete(value)

def cmd_fullscreen(value):
    pygame.display.toggle_fullscreen()
    
def cmd_delete(value):
    app.archive()
    #app.view.fill((0,0,0,0),app.select)
    app.mod(app.select)
    app.fill(app.select,(0,0,0,0))
    app.vdraw.repaint()
        
#NOTE: this function is a temporary HACK, to be replaced
#with layer editing in the future, maybe.
def cmd_tswitch(value):
    blayer = app.level.blayer
    tlayer = app.level.tlayer
    for ty in xrange(0,app.level.size[1]):
        for tx in xrange(0,app.level.size[0]):
            tmp = blayer[ty][tx]
            blayer[ty][tx] = tlayer[ty][tx]
            tlayer[ty][tx] = tmp
    app.vdraw.repaint()
    

def cmd_fill(value):
    pass

def cmd_pick(value):
    dx,dy = value
    
    mods = pygame.key.get_mods()
    
    
    if (mods&KMOD_SHIFT) != 0:
        app.level.view.x += dx*app.vdraw.rect.w/8
        app.level.view.y += dy*app.vdraw.rect.h/8
        app.vdraw.repaint()
        #x,y = app.view.get_offset()
        #x = x + 1*dx
        #y = y + 1*dy
        #x = min(max(x,0),app.level_w-app.view_w)
        #y = min(max(y,0),app.level_h-app.view_h)
        #app.view = app.level.subsurface((x,y,app.view_w,app.view_h))
        
    elif (mods&KMOD_CTRL) != 0:
        app.level.view.x += dx*app.vdraw.rect.w
        app.level.view.y += dy*app.vdraw.rect.h
        app.vdraw.repaint()
        #x,y = app.view.get_offset()
        #x = x + app.view_w*dx
        #y = y + app.view_h*dy
        #x = min(max(x,0),app.level_w-app.view_w)
        #y = min(max(y,0),app.level_h-app.view_h)
        #app.view = app.level.subsurface((x,y,app.view_w,app.view_h))
        
    
    else:
        w = app.tiles_w/app.tile_w
        if app.mode == 'code':
            n = app.code + dx + dy*w
            app.cpicker.set(n)
        else:
            n = app.tile + dx + dy*w
            app.tpicker.set(n)
        
def cmd_mode(value):
    mode = value
    app.mode = mode

def cmd_load(value):
    if app.dirty: _dirty(_cmd_load,value)
    else: _cmd_load(value)

def _cmd_load(value):
    if app.fname == None:
        ErrorDialog("Load failed","Image is untitled.").open()
        return
    raise Restart()

    
def cmd_save(value):
    if app.fname == None:
        return cmd_saveas(value)
    try:
        app.level.tga_save_level(app.fname)
        cfg_to_ini(['class','codes','tiles','tile_w','tile_h'],app.fname)
        ini_save()
        app.dirty = 0
    except Exception, v:
        ErrorDialog("Save failed.",v).open()
        return

    
import os
def cmd_preview(value):
    app.level.tga_save_level("_preview.tga")
    cmd = "python preview.py _preview.tga"
    # The Windows version of Python does not put itself in the PATH, but
    # this should work instead:
    if sys.platform.lower() == 'win32':
	cmd = "preview.py _preview.tga"
    print cmd
    os.system(cmd)
    
def cmd_quit(value):
    if app.dirty: _dirty(_cmd_quit,value)
    else: _cmd_quit(value)

def _cmd_quit(value):
    app.top.quit()    
    
def cmd_refreshtiles(value):
    app.load_tiles_and_codes()




menus = [
    ('File/New',cmd_new,None),
    ('File/Open',cmd_open,None),
    ('File/Save',cmd_save,None),
    ('File/Save As',cmd_saveas,None),
    ('File/Reload',cmd_load,None),
    ('File/Preview',cmd_preview,None),
    ('File/Quit',cmd_quit,None),

    ('Edit/Undo',cmd_undo,None),
    ('Edit/Cut',cmd_cut,None),
    ('Edit/Copy',cmd_copy,None),
    ('Edit/Paste',cmd_paste,None),
    ('Edit/Delete',cmd_delete,None),
    ('Edit/Select All',cmd_all,None),
    ('Edit/Shrink',cmd_shrink,None),
    #('Edit/Redo',None,None,None),
    #('Edit/Cut',None,None,None),
    #('Edit/Fill',cmd_fill,None),
    
    ]

keys = [
    (K_s,cmd_save,None),
    (K_d,cmd_load,None),
    (K_p,cmd_preview,None),

    (K_a,cmd_all,None),
    (K_z,cmd_undo,None),
    #('Edit/Redo',None,None,None),
    (K_x,cmd_cut,None),
    (K_c,cmd_copy,None),
    (K_v,cmd_paste,None),
    #('Edit/Cut',None,None,None),
    (K_DELETE,cmd_delete,None),
    #(K_f,cmd_fill,None),
        
        (K_t,cmd_tswitch,None),
    
    (K_F10,cmd_fullscreen,None),
    
    (K_UP,cmd_pick,(0,-1)),
    (K_DOWN,cmd_pick,(0,1)),
    (K_LEFT,cmd_pick,(-1,0)),
    (K_RIGHT,cmd_pick,(1,0)),
    ]
    

tools = [
    ('tile','tile'),
    ('bkgr','bkgr'),
    ('code','code'),
    ('select','select'),
    ('eraser','eraser'),
    ]

    
    

class NewDialog(gui.Dialog):
    def __init__(self,**params):
        title = gui.Label("New...")
        
        doc = html.HTML(globals={'gui':gui,'dialog':self},data="""
<form id='form'>

<table>

<tr><td colspan=2>

<table>
<tr><td>Level<br>Type:
<td align=left><input type=radio name='class' value='pgu.tilevid.Tilevid' checked> Tile<br>
<input type=radio name='class' value='pgu.isovid.Isovid'> Isometric<br>
<input type=radio name='class' value='pgu.hexvid.Hexvid'> Hexoganol

<tr><td >Tiles: <td><input type='text' size=20 name='tiles' value='%(tiles)s'>
<tr><td>Codes: <td><input type='text' size=20 name='codes' value='%(codes)s'>
</table>

<tr>
<td align=center>Level Size
<td align=center>Tile Size


<tr><td colspan='1' align='center' style='padding-right:8px;'>
<table>
<tr><td align=right>Width: <td><input type='text' size='4' value='%(width)s' name='width'>
<tr><td align=right>Height: <td><input type='text' size='4' value='%(height)s' name='height'>
</table>

<td colspan='1' align='center'>
<table>
<tr><td align=right>Width: <td><input type='text' size='4' value='%(tile_w)s' name='tile_w'>
<tr><td align=right>Height: <td><input type='text' size='4' value='%(tile_h)s' name='tile_h'>
</table>

<tr><td>&nbsp;

<tr><td colspan=2><input type='button' value='Okay' onclick='dialog.send(gui.CHANGE)'> <input type='button' value='Cancel' onclick='dialog.close()'>

</table>"""%ini_to_dict('None'))
        gui.Dialog.__init__(self,title,doc)
        
        self.value = doc['form']

class SaveAsDialog(gui.Dialog):
    def __init__(self,**params):
        title = gui.Label("Save As...")
        
        doc = html.HTML(globals={'gui':gui,'dialog':self},data="""
<form id='form'>

<table>

<tr><td colspan=2>File Name: <input type='file' size=20 name='fname' value=''>

<tr><td>&nbsp;

<tr><td colspan=2><input type='button' value='Okay' onclick='dialog.send(gui.CHANGE)'> <input type='button' value='Cancel' onclick='dialog.close()'>

</table>""")
        gui.Dialog.__init__(self,title,doc)
        
        self.value = doc['form']

class OpenDialog(gui.Dialog):
    def __init__(self,**params):
        title = gui.Label("Open...")
        
        def load_vals(fname,form):
            if not ini.has_section(fname): return
            
            for k,v in ini.items(fname):
                if k in form:
                    form[k].value = v

        doc = html.HTML(globals={'load_vals':load_vals,'ini':ini,'gui':gui,'dialog':self},data="""<form id='form'><table>
        
        <tr><td align=right>File Name:&nbsp;<td  align=left><input type='file' size=20 name='fname' value='' onchange='load_vals(self.value,form)'>

        <tr><td align=right>Level&nbsp;<br>Type:&nbsp;
        <td align=left><input type=radio name='class' value='pgu.tilevid.Tilevid' checked> Tile<br><input type=radio name='class' value='pgu.isovid.Isovid'> Isometric<br><input type=radio name='class' value='pgu.hexvid.Hexvid'> Hexoganol
        
        <tr><td align=right>Tiles:&nbsp;<td align=left><input type='text' size=20 name='tiles' value='%(tiles)s'>
        <tr><td align=right>Codes:&nbsp;<td align=left><input type='text' size=20 name='codes' value='%(codes)s'>
        
        <tr><td align=right>Tile Width:&nbsp;<td align=left><input type='text' size='4' value='%(tile_w)s' name='tile_w'>
        <tr><td align=right>Tile Height:&nbsp;<td align=left><input type='text' size='4' value='%(tile_h)s' name='tile_h'>


<tr><td>&nbsp;

<tr><td colspan=2><input type='button' value='Okay' onclick='dialog.send(gui.CHANGE)'> <input type='button' value='Cancel' onclick='dialog.close()'>

</table>"""%ini_to_dict('None'))
        gui.Dialog.__init__(self,title,doc)
        
        self.value = doc['form']

class ErrorDialog(gui.Dialog):
    def __init__(self,tt,data,**params):
        title = gui.Label("Error: "+tt)
        data = str(data)
        
        doc = html.HTML(globals={'gui':gui,'dialog':self},data="""
<form id='form'>

<table>
<tr><td><h1>&lt;!&gt;&nbsp;</h1>
<td>"""+data+"""
<tr><td>&nbsp;
<tr><td colspan=2><input type='button' value='Okay' onclick='dialog.send(gui.CHANGE);dialog.close()'>
</table>""")
        gui.Dialog.__init__(self,title,doc)
        
        self.value = doc['form']

class DirtyDialog(gui.Dialog):
    def __init__(self,**params):
        title = gui.Label("File not yet saved...")
        data = "Your file is not yet saved.<br>Are you sure you want to continue?"
        
        doc = html.HTML(globals={'gui':gui,'dialog':self},data="""
<form id='form'>

<table>
<tr><td><h1>&lt;!&gt;&nbsp;</h1>
<td>"""+data+"""
<tr><td>&nbsp;
<tr><td colspan=2><input type='button' value='Okay' onclick='dialog.send(gui.CHANGE)'> <input type='button' value='Cancel' onclick='dialog.close()'>
</table>""")
        gui.Dialog.__init__(self,title,doc)
        
        self.value = doc['form']


    
        
def init_ini():
    ini.read([ini_fname])

def ini_save():
    f = open(ini_fname,"wb")
    ini.write(f)
    f.close()


def init_opts():
    ini.read([ini_fname])
    
    usage = "usage: %prog level.tga [tiles.tga] [codes.tga] [tile_w] [tile_h]"
    
    parser = OptionParser(usage)
    
    parser.add_option("-t", "--tiles", dest="tiles",
        help="filename of the tiles image (level)")
    parser.add_option("-c", "--codes", dest="codes",
        help="file name of the codes image (level)")

    parser.add_option("--tw", dest="tile_w", help="tile width (level)", type='int')
    parser.add_option("--th", dest="tile_h", help="tile height (level)", type='int')

    parser.add_option("--vw", dest="view_w", help="view width (level)", type='int')
    parser.add_option("--vh", dest="view_h", help="view height (level)", type='int')
        
    parser.add_option("--sw", dest="screen_w", help="screen width (app)", type='int')
    parser.add_option("--sh", dest="screen_h", help="screen height (app)", type='int')
    parser.add_option("--class",dest="class",help="class (e.g. pgu.tilevid.Tilevid) (level)")
    parser.add_option("--tile",action="store_const",const="pgu.tilevid.Tilevid",dest="class",help="use pgu.tilevid.Tilevid")
    parser.add_option("--iso",action="store_const",const="pgu.isovid.Isovid",dest="class",help="use pgu.isovid.Isovid")
    parser.add_option("--hex",action="store_const",const="pgu.hexvid.Hexvid",dest="class",help="use pgu.hexvid.Hexvid")
    
    parser.add_option("--width",dest="width",help="new width (level)",type='int')
    parser.add_option("--height",dest="height",help="new height (level)",type='int')

    parser.add_option("-d","--defaults",dest="defaults",help="set default settings (image)",action="store_true")

    #parser.add_option("-a", "--app", dest="app",
    #    help="set application level defaults", action="store_true")
    
    (opts, args) = parser.parse_args()
    
    if len(args) not in (0,1,2,3,4,5):
        parser.error("incorrect number of arguments")
    
    #parse arguments
    if len(args) == 0:
        opts.fname = "None"
    if len(args) > 0:
        opts.fname = args[0]
    if len(args) > 1:
        opts.tiles = args[1]
    if len(args) in (3,5):
        opts.codes = args[2]
    if len(args) in (4,5):
        n = len(args)-2
        try: opts.tile_w,opts.tile_h = int(args[n]),int(args[n+1])
        except: parser.error("width and height must be integers")
        if opts.tile_w < 1 or opts.tile_h < 1: parser.error("width and height must be greater than 0")
        
    fname = opts.fname
    
    #create all sections
    for k in [fname,"None","app"]:
        if not ini.has_section(k):
            ini.add_section(k)
    
    #set app level defaults
    for k,v in [('screen_w',800),('screen_h',600)]:
        if not ini.has_option('app',k):
            ini.set('app',k,str(v))
    
    #set app level values
    for k in ['screen_w','screen_h']:
        if hasattr(opts,k):
            v = getattr(opts,k)
            if v != None: ini.set('app',k,str(v))

    #set default defaults
    for k,v in [('width',40),('height',30),('tile_w',32),('tile_h',32),('tiles','tiles.tga'),('codes','codes.tga'),('class','pgu.tilevid.Tilevid')]:
        if not ini.has_option('None',k):
            ini.set('None',k,str(v))
    
    #name of keys for normal stuff
    file_ks = ['class','tiles','codes','width','height','tile_w','tile_h']
            
    #set default values
    if opts.defaults:
        for k in file_ks:
            if hasattr(opts,k):
                v = getattr(opts,k)
                if v != None: ini.set('None',k,str(v))
    
    #set fname values
    for k in file_ks:
        if hasattr(opts,k):
            v = getattr(opts,k)
            if v != None: ini.set(fname,k,str(v))
    
    #save the ini
    ini_save()
            
    #convert ini to cfg stuff...
    ini_to_cfg(['app','None',fname])
    if fname == 'None': fname = None
    cfg['fname'] = fname

def ini_to_cfg(sections):
    global cfg
    ik = ['screen_w','screen_h','tile_w','tile_h','width','height']
    for s in sections:
        for k,v in ini.items(s):
            if k in ik: v = int(v)
            cfg[k] = v

def ini_to_dict(section):
    cfg = {}
    ik = ['screen_w','screen_h','tile_w','tile_h','width','height']
    for s in [section]:
        for k,v in ini.items(s):
            if k in ik: v = int(v)
            cfg[k] = v
    return cfg

def cfg_to_ini(ks,section):
    if not ini.has_section(section): ini.add_section(section)
    for k in ks:
        v = cfg[k]
        ini.set(section,k,str(v))

        
def init_gui():
    #themes = cfg['theme'].split(",")
#    themes2 = []
#    for t in themes:
#        if t[0] == "/" or t[0] == ".": themes2.append(t)
#        else: themes2.append(dname+"/"+t)
    #gui.theme.load(themes)
    #gui.theme.load(['default','tools'])
    global top
    top = gui.Desktop(theme=gui.Theme([os.path.join('data','themes','default'),os.path.join('data','themes','tools')]))
    #top.theme.load(['default','tools'])


    pass

def init_app():
    global app
    app = _app()
    
    #
    ss = 8
        
    #--- top
    x,y,h = 0,0,0
        
    #menus
    e = gui.Menus(menus)
    e.rect.w,e.rect.h = e.resize()
    app.add(e,x,y)
    x,h = x+e.rect.w,max(h,e.rect.h)
    menus_height = e.rect.h
        
    #--- row
    x,y,h = 0,y+h,0
    
    #--- vspace
    y += ss
    
    #--- hspace
    x += ss
    
    #tools
    e = gui.Toolbox(tools,1,0,value='tile')#,"icons48")
    e.rect.w,e.rect.h = e.resize()
    def _set_mode(value): cmd_mode(value.value)
    e.connect(gui.CHANGE,_set_mode,e)
    app.add(e,x,y)
    app.tools = e.tools
    x,h = x+e.rect.w,max(h,e.rect.h)
    toolbox_width = e.rect.w
    
    #--- hspace
    x += ss
    
    #vdraw
    dw = app.screen_w - (toolbox_width+app.tiles.get_width()+ss*4)
    dh = app.screen_h - (menus_height+ss*2)
    app.view_init(dw,dh)
    
    
    e = app.vwrap = vwrap()
    app.vdraw = e.vdraw
    e.rect.w,e.rect.h = e.resize()
    app.add(e,x,y)
    x,h = x+e.rect.w,max(h,e.rect.h)
    
    #--- hspace
    x += ss
    
    #tpicker
    e = app.tpicker = tpicker()
    e.rect.w,e.rect.h = e.resize()
    #--- right
    x = app.screen_w-e.rect.w-ss
    app.add(e,x,y)
    x,h = x+e.rect.w,max(h,e.rect.h)
    tpicker_height = e.rect.h
    
    #cpicker
    e = app.cpicker = cpicker()
    e.rect.w,e.rect.h = e.resize()
    #--- right
    x = app.screen_w-e.rect.w-ss
    app.add(e,x,y+tpicker_height+ss)
    x,h = x+e.rect.w,max(h,e.rect.h)
    



    pygame.key.set_repeat(500,30)
    
    app.screen.fill((255,255,255,255))


def run():
    top.connect(gui.QUIT,cmd_quit,None)
    top.connect(pygame.ACTIVEEVENT, cmd_refreshtiles,None)

    top.init(app,app.screen)
    app.top=top
    top.run()

def main():
    init_ini()
    init_opts()
    init_gui()
    
    restart = 1
    while restart:
        restart = 0
        try:
            init_app()
            run()
        except Restart: restart = 1

main()
# vim: set filetype=python sts=4 sw=4 noet si :