-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1759 lines (1350 loc) · 41 KB
/
main.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
# -*- coding: utf-8 -*-
# --------------------------------------------------------
# 2D Action Game using Pygame
# doc100@gmail.com
# --------------------------------------------------------
import os
import sys
import random
import string
import fpformat
import copy
import profile
import math
import new
import pygame
import yaml
from pygame.locals import *
# Global
VERSION = ""
CS = 16 # Charactor chip Size (Pixel)
DL0 = 0 # Debug Level Low
DL1 = 1 # Debug Level
DL2 = 2 # Debug Level High
BLACK = 0, 0, 0
WHITE = 255, 255, 255
IMAGE_DIRECTORY = "image"
DATA_DIRECTORY = "data"
UP = K_UP
RIGHT= K_RIGHT
DOWN = K_DOWN
LEFT = K_LEFT
ENTER = K_x
CANCEL = K_z
class Config:
def __init__(self):
self.configData = yaml.load( open(os.path.join(DATA_DIRECTORY, 'config.yml') ))
self.win_alpha = self.get("win_alpha", 255)
self.disp_bpp = self.get("disp_bpp", 0)
self.fps = self.get("fps", 30)
self.mouse_support = self.get("mouse_support", 0)
self.music_freq = self.get("music_freq", 44100)
self.music_bits = self.get("music_bits", 16)
self.music_volume = self.getFloat("music_volume", 1.0)
self.fontsize = self.get("fontsize", 12)
self.fontpath = self.getStr("fontpath", os.path.join("font", "ipag\\ipag.otf"))
self.fonttype = self.get("fonttype", 1)
self.lineheight = self.getFloat("lineheight", 1.5)
self.gDebugLevel = self.get("debuglevel", 0)
self.doMouseView = self.get("domouseview", 0)
self.flipKey = self.get("flipkey", 0)
self.disp_flags = 0
if self.get("fullscreen", 0):
self.disp_flags |= FULLSCREEN
if self.get("hwsurface", 0):
self.disp_flags |= HWSURFACE
self.disp_flags |= DOUBLEBUF
if self.get("asyncblit", 0):
self.disp_flags |= ASYNCBLIT
def get(self, key, default):
if self.configData.has_key(key):
return int(self.configData[key])
return default
def getFloat(self, key, default):
if self.configData.has_key(key):
return float(self.configData[key])
return float(default)
def getStr(self, key, default):
if self.configData.has_key(key):
return self.configData[key]
return default
def display(self):
print self.configData
class State:
def __init__(self):
self.fps = 0
self.playerLife = 100
self.gamePauseFlag = False
def writeToFile(self):
pass
class Pos:
def __init__(self, x, y):
self.x = x
self.y = y
self.map = {0:self.x, 1:self.y}
def __getitem__(self, index):
if index == 0:
return self.x
elif index == 1:
return self.y
def __setitem__(self, index, value):
if index == 0:
self.x = value
elif index == 1:
self.y = value
def __len__(self):
return len(self.__dict__)
class Size:
def __init__(self, w, h):
self.w = w
self.h = h
def __getitem__(self, index):
if index == 0:
return self.w
elif index == 1:
return self.h
def __len__(self):
return len(self.__dict__)
class Dir:
def __init__(self, top, right, bottom, left):
self.top = top
self.right = right
self.bottom = bottom
self.left = left
class ControlInterface:
def __init__(self):
self.key = {
UP: False, RIGHT: False, DOWN: False, LEFT: False,
ENTER: False, CANCEL: False,
}
self.keyOnce = {
UP: False, RIGHT: False, DOWN: False, LEFT: False,
ENTER: False, CANCEL: False,
}
self.connection = False
self.scrollAmount = 0
def connect(self):
self.connection = True
def disconnect(self):
self.connection = False
class Controller(ControlInterface):
def __init__(self):
self.state = { K_UP:False, K_RIGHT:False, K_DOWN:False, K_LEFT:False,\
K_z:False, K_x:False }
self.newstate = []
self.link = None
self.connection = False
self.ctllist = []
self.memMousePos = None
self.gestureMargin = 32
self.scrollAmount = 0
def call(self):
if self.connection == True:
new = pygame.key.get_pressed()
newstate = {
UP:new[UP],
RIGHT:new[RIGHT],
DOWN:new[DOWN],
LEFT:new[LEFT],
ENTER:new[ENTER],
CANCEL:new[CANCEL],
}
for dictkey in self.link.keyOnce.keys():
self.link.keyOnce[dictkey] = False
self.scrollAmount = 0
if g.joypad != None:
x = g.joypad.get_axis(0)
y = g.joypad.get_axis(1)
if abs(x) < 0.1:
x = 0.0
if abs(y) < 0.1:
y = 0.0
if x > 0:
newstate[RIGHT] = True
elif x < 0:
newstate[LEFT] = True
if y < 0:
newstate[UP] = True
elif y > 0:
newstate[DOWN] = True
if joypad.get_button(1):
newstate[ENTER] = True
if joypad.get_button(0):
newstate[CANCEL] = True
for e in g.state.event:
if e.type == KEYDOWN:
if e.dict['key'] == ENTER:
self.link.keyOnce[ENTER] = True
if e.dict['key'] == CANCEL:
self.link.keyOnce[CANCEL] = True
if e.dict['key'] == UP:
self.link.keyOnce[UP] = True
elif e.dict['key'] == DOWN:
self.link.keyOnce[DOWN] = True
if e.dict['key'] == RIGHT:
self.link.keyOnce[RIGHT] = True
elif e.dict['key'] == LEFT:
self.link.keyOnce[LEFT] = True
for dictkey in self.link.key.keys():
self.link.key[dictkey] = False
if newstate[UP] == True:
self.link.key[UP] = True
if newstate[RIGHT]:
self.link.key[RIGHT] = True
if newstate[DOWN]:
self.link.key[DOWN] = True
if newstate[LEFT]:
self.link.key[LEFT] = True
if newstate[ENTER]:
self.link.key[ENTER] = True
if newstate[CANCEL]:
self.link.key[CANCEL] = True
for dictkey in self.link.keyOnce.keys():
self.link.keyOnce[dictkey] = False
if (newstate[UP] == True) & (newstate[UP] <> self.state[UP]):
self.link.keyOnce[UP] = True
if (newstate[RIGHT] == True) & (newstate[RIGHT] <> self.state[RIGHT]):
self.link.keyOnce[RIGHT] = True
if (newstate[DOWN] == True) & (newstate[DOWN] <> self.state[DOWN]):
self.link.keyOnce[DOWN] = True
if (newstate[LEFT] == True) & (newstate[LEFT] <> self.state[LEFT]):
self.link.keyOnce[LEFT] = True
if (newstate[ENTER] == True) & (newstate[ENTER] <> self.state[ENTER]):
self.link.keyOnce[ENTER] = True
if (newstate[CANCEL] == True) & (newstate[CANCEL] <> self.state[CANCEL]):
self.link.keyOnce[CANCEL] = True
if self.scrollAmount == 0:
self.scrollAmount = 1
self.link.scrollAmount = self.scrollAmount
flag = False
for s in newstate:
if s:
flag = True
if flag == False:
pass
self.state[K_UP] = newstate[K_UP]
self.state[K_RIGHT] = newstate[K_RIGHT]
self.state[K_DOWN] = newstate[K_DOWN]
self.state[K_LEFT] = newstate[K_LEFT]
self.state[K_x] = newstate[K_x]
self.state[K_z] = newstate[K_z]
self.key = self.link.key
self.keyOnce = self.link.keyOnce
def connect(self, link):
self.ctllist.append(link)
if self.link != None:
self.link.disconnect()
for dictkey in self.link.key.keys():
self.link.key[dictkey] = False
for dictkey in self.link.keyOnce.keys():
self.link.keyOnce[dictkey] = False
self.link = link
self.connection = True
self.link.connect()
def disconnect(self):
if len(self.ctllist) >= 2:
self.ctllist.pop()
self.connect(self.ctllist.pop())
else:
if len(self.ctllist) == 1:
self.ctllist.pop()
self.connection = False
self.link = None
def free(self):
while(len(self.ctllist) > 0):
self.disconnect()
class Object:
def __init__(self):
self.id = 0
self.count = 0
self.locking = False
def draw(self):
pass
def call(self):
pass
def __del__(self):
pass
def lock(self):
self.locking = True
def unlock(self):
self.locking = False
class VisibleObject(Object):
def __init__(self):
Object.__init__(self)
self.pos = Pos(0,0)
self.old_pos = Pos(0,0)
self.zpos = 0
self.zindex = 0
self.useCol = False
self.type = ""
self.battle = False
def call(self):
pass
def draw(self, screen, campos=(0,0)):
pass
class Window(Object, ControlInterface):
def __init__(self, pos, size, mode=0):
padding=Dir(g.config.fontsize, int(g.config.fontsize/2), int(g.config.fontsize/2), g.config.fontsize)
self.pos = pos
self.size = Size(0,0)
self.boxpos = Pos(0,0)
self.boxsize = Size(0,0)
self.frameSrcPosX = 32
self.frameSrcPosY = 16
self.padding = padding
self.border = Dir(8, 8, 8, 8)
if mode == 1:
self.forceWinAlpha = True
else:
self.forceWinAlpha = False
self.bgcolor_A = [25,28,51]
self.bgcolor_B = [57,86,115]
self.highlight_bgcolor_A = [14,28,115]
self.highlight_bgcolor_B = [91,121,242]
self.bordercolor = [0,0,0]
self.count = 0
self.mode = mode
self.setSize(size)
self.setPos(pos)
self.drawBuffer()
def setSize(self, size):
self.size = Size( int(size.w), int(size.h) )
self.boxsize = Size( self.size.w + (self.padding.left + self.padding.right) + (self.border.left + self.border.right), \
self.size.h + (self.padding.top + self.padding.bottom) + (self.border.top + self.border.bottom))
self.scrbuf = pygame.Surface((self.boxsize.w, self.boxsize.h)).convert()
self.scrbuf.fill((0,0,0))
self.scrbuf.set_colorkey((0,0,0))
if g.config.win_alpha != 255:
if g.config.disp_flags & HWSURFACE:
pass
else:
self.scrbuf.set_alpha(g.config.win_alpha)
else:
if self.mode == 1:
self.scrbuf.set_alpha(g.config.win_alpha)
if self.forceWinAlpha:
self.scrbuf.set_alpha(214)
self.drawBuffer()
def setPos(self, pos):
self.pos = pos
self.boxpos = Pos( self.pos.x - (self.padding.left + self.border.left), \
self.pos.y - (self.padding.top + self.border.top) )
def call(self):
count+=1
def setMode(self, mode):
oldmode = self.mode
self.mode = mode
if self.mode != oldmode:
self.drawBuffer()
def drawBuffer(self):
bgcolor_A = self.bgcolor_A
bgcolor_B = self.bgcolor_B
if self.mode == 1:
bgcolor_A = self.highlight_bgcolor_A
bgcolor_B = self.highlight_bgcolor_B
winDeco = True
if winDeco:
fx = self.frameSrcPosX
fy = self.frameSrcPosY
drawRect( self.scrbuf,
Rect( self.border.left, self.border.top, \
self.boxsize.w - (self.border.left + self.border.right), \
self.boxsize.h - (self.border.top + self.border.bottom) ), \
bgcolor_A, bgcolor_B )
for i in range(self.border.left, self.boxsize.w-self.border.right):
self.scrbuf.blit(g.img["parts"], (i,0), (fx+8, fy, 1, 8))
self.scrbuf.blit(g.img["parts"], (self.boxsize.w-self.border.right, 0), (fx+16, fy, 8, 8))
for i in range(self.border.top, self.boxsize.h-self.border.bottom):
self.scrbuf.blit(g.img["parts"], (self.boxsize.w-self.border.right,i), (fx+16, fy+8, 8, 1))
self.scrbuf.blit(g.img["parts"], (self.boxsize.w-self.border.right, self.boxsize.h-self.border.bottom), (fx+16, fy+16, 8, 8))
for i in range(self.border.left, self.boxsize.w-self.border.right):
self.scrbuf.blit(g.img["parts"], (i,self.boxsize.h-self.border.bottom), (fx+8, fy+16, 1, 8))
self.scrbuf.blit(g.img["parts"], (0,self.boxsize.h-self.border.bottom), (fx, fy+16, 8, 8))
for i in range(self.border.top, self.boxsize.h-self.border.bottom):
self.scrbuf.blit(g.img["parts"], (0,i), (fx, fy+8, 8, 1))
self.scrbuf.blit(g.img["parts"], (0,0), (fx, fy, 8, 8))
else:
rect = Rect(0,0,self.boxsize.w, self.boxsize.h)
color = [0,0,0]
color_A = bgcolor_A
color_B = bgcolor_B
rezo = 8
for i in range(rect.h/rezo):
for j in range(3):
color[j] = (color_A[j] * (rect.h-i*rezo) + color_B[j] * i*rezo) / rect.h
self.scrbuf.fill(color, (rect.x, rect.y + i*rezo, rect.w, rezo))
pygame.draw.rect(self.scrbuf, (240,240,240), (0,0,self.boxsize.w, self.boxsize.h/rezo*rezo), 1)
def draw(self, screen, campos=(0,0)):
screen.blit(self.scrbuf, (self.boxpos.x, self.boxpos.y))
class Map:
def __init__(self): # コンストラクタ
self.filepath = ""
self.size = Size(0,0)
self.pos = Pos(0,0)
self.zindex = -1
def load(self, filePath):
self.filePath = fileOath
# マップ画像読み込み
try:
trace(DL2, "map.setMap().filepath: "+`filepath`)
block = string.split(filepath, '.')
name = block[0]
extend = block[1]
self.src, self.size = loadImage(os.path.join(IMAGE_DIRECTORY, name+'.'+extend))
except Exception, message:
trace(DL1, "map.setMap() - Error occured on map image load. set temporary map image.")
self.src = pygame.Surface((640,480)).convert()
self.size = Size(640,480)
class MenuItem:
def __init__(self, name, cmd, obj=None):
self.name = name
self.cmd = cmd
self.obj = obj
class Menu(ControlInterface, VisibleObject):
ITEM_CANTSELECT = 10
ITEM_NORMAL = None
def __init__(self, **prm):
VisibleObject.__init__(self)
ControlInterface.__init__(self)
self.connected = False
self.idtag = None
self.zindex = 0
self.screen = g.screen
self.pos = Pos(0, 0)
self.size = Size(0, 0)
self.rect = Rect(0,0,0,0)
self.help = ""
self.itemsel = 0
self.lines = 1
self.olditemsel = self.itemsel
self.itemlist = []
self.itemnum = 0
self.disp_itemnum = 0
self.scroll_pos = 0
self.old_scroll_pos = 0
if prm.has_key('forceWinAlpha'):
self.forceWinAlpha = 1
else:
self.forceWinAlpha = 0
self.fontcolor = [255,255,255]
self.selcolor = [255,192,0]
self.helpbgcolor = [72,80,88]
self.lineheight = 1.5
self.padding = Dir(g.config.fontsize, int(g.config.fontsize/2), int(g.config.fontsize/2), g.config.fontsize)
self.border = Dir(2, 6, 4, 2)
self.count = 0
self.keycount = 0
self.anchorL = False
self.anchorR = False
self.doDispCursor = False
self.win = None
self.helpfontsize = 16
self.helpfont = pygame.font.Font(g.config.fontpath, self.helpfontsize)
self.itemHeight = int(g.config.fontsize * self.lineheight)
self.history = []
self.oldkey = self.key.copy()
self.doDraw = True
for menu in g.menuMgr.menulist:
self.history.append(menu)
self.load()
def getMenu(self):
pass
def resetFromSize(self):
max_itemnum = int(math.floor(self.size.h / (g.config.fontsize * self.lineheight)))
if max_itemnum > self.itemnum:
self.disp_itemnum = self.itemnum*self.lines
else:
self.disp_itemnum = max_itemnum*self.lines
def setPos(self, pos):
self.pos = pos
self.win.setPos(pos)
def setSize(self, size):
self.size = size
self.win.setSize(size)
def load(self):
self.getMenu()
self.itemnum = len(self.itemlist)
self.rect.w = self.rect.w*self.lines + ((self.lines-1)*2*g.config.fontsize)
self.pos = Pos(self.rect.x, self.rect.y)
self.size = Size(self.rect.w, self.rect.h)
if self.size.w < 0:
maxlen = 0
for item in self.itemlist:
if maxlen < len(item.name):
maxlen = len(item.name)
self.size.w = int(maxlen * fontsize)
if self.size.h < 0:
self.size.h = int(len(self.itemlist) * (g.config.fontsize *g.config.lineheight))
self.win = Window(self.pos, self.size, self.forceWinAlpha)
self.resetFromSize()
self._cashRender()
self.drawBuffer()
if self.help != '':
ren = self.helpfont.render(self.help, g.config.fonttype, (255,255,255))
self.ren_help = pygame.Surface(ren.get_size()).convert()
color = self.helpbgcolor
#if colCtrSw:
# color = colCtrChg(color, 1)
self.ren_help.fill(color)
self.ren_help.blit( ren, (0,0), (0, 0, ren.get_width(), ren.get_height()) )
def _cashRender(self):
self.buf = pygame.Surface((self.size.w, self.size.h)).convert()
self.buf.set_colorkey((0,0,0))
self.ren_upanchor = pygame.Surface((32, 16)).convert()
self.ren_upanchor.blit( g.img["parts"], (0,0), (11*16, 0, 32, 16) )
self.ren_upanchor.set_colorkey((0,0,0))
self.ren_downanchor = pygame.Surface((32, 16)).convert()
self.ren_downanchor.blit( g.img["parts"], (0,0), (13*16, 0, 32, 16) )
self.ren_downanchor.set_colorkey((0,0,0))
self.ren_leftanchor = pygame.Surface((16, 32)).convert()
self.ren_leftanchor.blit( g.img["parts"], (0,0), (15*16, 0, 16, 32) )
self.ren_leftanchor.set_colorkey((0,0,0))
self.ren_rightanchor = pygame.Surface((16, 32)).convert()
self.ren_rightanchor.blit( g.img["parts"], (0,0), (15*16, 2*16, 16, 32) )
self.ren_rightanchor.set_colorkey((0,0,0))
self.ren_cursor = pygame.Surface((32, 32)).convert()
self.ren_cursor.blit( g.img["parts"], (0,0), (0, 16, 32, 32) )
self.ren_cursor.set_colorkey((0,0,0))
self._cashRenderText()
def _cashRenderText(self):
self.ren_iname = []
for i in range(0, self.disp_itemnum):
inum = i + self.scroll_pos
try:
item = self.itemlist[inum]
except IndexError, message:
trace(DL2, "Index Error. TeamItemMenu.drawBuffer().self.itemlist[inum]")
continue
ren = self._renderText(item, None, i)
self.ren_iname.append(ren)
def _renderText(self, menuItem, apColor=None, i=0):
item = menuItem
if item.obj == False:
t = float(i) / float(self.disp_itemnum)
r = self.win.highlight_bgcolor_A[0]*(1-t) + self.win.highlight_bgcolor_B[0]*(t)
g = self.win.highlight_bgcolor_A[1]*(1-t) + self.win.highlight_bgcolor_B[1]*(t)
b = self.win.highlight_bgcolor_A[2]*(1-t) + self.win.highlight_bgcolor_B[2]*(t)
color = (r,g,b)
markcolor = 0
elif item.obj == True:
color = (176,176,176)
markcolor = 0
elif item.obj == -1:
color = (96,240,96)
markcolor = 0
elif item.obj == self.ITEM_CANTSELECT:
color = (96,96,96)
markcolor = 0
else:
color = self.fontcolor
markcolor = 0
if apColor != None:
color = apColor
"""
elif self.itemsel == inum:
color = self.selcolor
markcolor = 1
"""
#if colCtrSw:
# color = colCtrChg(color, 1)
ren = render(g.font, item.name, 0, color).convert()
return ren
def reload(self):
self.keyClear()
self.load()
self.call()
def setFontsize(self, fontsize):
pass
def receive(self, message):
pass
def reRender(self):
self._cashRenderText()
self.drawBuffer()
def drawBuffer(self):
d = self.scroll_pos - self.old_scroll_pos
if len(self.itemlist) > 0:
self.ren_iname_select = self._renderText(self.itemlist[self.itemsel], self.selcolor)
if d != 0:
temp = []
for i in range(0,abs(d)):
if d > 0:
index = i + self.scroll_pos + self.disp_itemnum-self.lines
else:
index = i + self.scroll_pos
item = self.itemlist[index]
temp.append(self._renderText(item))
for i in range(0, self.disp_itemnum):
shift = d / abs(d)
if d > 0:
targetIndex = i - abs(d)
if targetIndex >= 0:
self.ren_iname[targetIndex] = self.ren_iname[i]
else:
i = (self.disp_itemnum-1) - i
targetIndex = i + abs(d)
if targetIndex < self.disp_itemnum:
self.ren_iname[targetIndex] = self.ren_iname[i]
for i in range(0,abs(d)):
if d > 0:
index = (i - abs(d)) + (self.disp_itemnum)
self.ren_iname[index] = temp[i]
else:
index = i
self.ren_iname[index] = temp[i]
self.buf.fill((0,0,0,255))
for i in range(0, self.disp_itemnum):
self.buf.blit(self.ren_iname[i], self._getTextPos(i) )
def _getTextPos(self, index):
i = index
return ((i%self.lines)*14*g.config.fontsize, 0 + (i/self.lines * g.config.fontsize * self.lineheight) )
def _drawText(self, index):
pass
def draw(self, screen, campos=(0,0)):
if not self.doDraw:
return
self.win.draw(screen)
screen.blit(self.buf, (self.win.pos.x, self.win.pos.y))
if hasattr(self, "ren_iname_select"):
tPos = self._getTextPos(self.itemsel-self.scroll_pos)
tPos = (tPos[0] + self.pos.x, tPos[1] + self.pos.y)
screen.blit(self.ren_iname_select, tPos )
if self.help != '':
pos = self.helpPos
screen.fill( (8,8,8), (pos.x, pos.y, (len(self.help)*self.helpfontsize)+6, self.helpfontsize*lineheight) )
color = self.helpbgcolor
if colCtrSw:
color = colCtrChg(color, 1)
screen.fill( color, (pos.x+1, pos.y+1, (len(self.help)*self.helpfontsize)+4, self.helpfontsize*lineheight-2) )
self.screen.blit(self.ren_help, (pos.x+4, pos.y+4) )
if len(self.itemlist) >= 1:
if self.disp_itemnum < self.itemnum:
barpos = Pos( self.win.boxpos.x + (self.win.boxsize.w - self.border.right+1),
self.win.boxpos.y + self.win.border.top + (self.scroll_pos * (self.win.boxsize.h - (self.win.border.top+self.win.border.bottom)) / self.itemnum ))
barsize = Size( self.win.border.right-2,
(self.win.boxsize.h - (self.win.border.top+self.win.border.bottom)) / self.itemnum * self.disp_itemnum )
screen.fill((0,0,0), (barpos.x-2, barpos.y, barsize.w+4, barsize.h))
screen.fill((255,255,255), (barpos.x+1, barpos.y+2, barsize.w-2, barsize.h-4))
if (self.scroll_pos) > 0:
screen.blit(self.ren_upanchor, (self.win.pos.x+self.win.size.w/2 - self.ren_upanchor.get_width()/2,
self.win.boxpos.y) )
if (self.scroll_pos+self.disp_itemnum) < self.itemnum:
screen.blit(self.ren_downanchor, (self.win.pos.x+self.win.size.w/2 - self.ren_upanchor.get_width()/2,
self.win.pos.y+self.win.size.h) )
if self.anchorL:
screen.blit(self.ren_leftanchor, (self.win.pos.x - 26 - self.ren_leftanchor.get_width()/2,
self.win.boxpos.y+self.win.size.h/2) )
if self.anchorR:
screen.blit(self.ren_rightanchor, (self.win.pos.x + 16 + self.win.size.w - self.ren_rightanchor.get_width()/2,
self.win.boxpos.y+self.win.size.h/2) )
if self.connection:
curpos = Pos( (self.itemsel%(self.lines)*(self.size.w/self.lines+fontsize))+self.win.boxpos.x + 4 - 16 + resoSin(3, def_fps/3, self.count), self.pos.y - 3 + ((self.itemsel/self.lines - self.scroll_pos/self.lines) * self.itemHeight))
screen.blit(self.ren_cursor, (curpos.x, curpos.y))
def calcScrollPos(self, itemsel):
return (itemsel / self.disp_itemnum) * self.disp_itemnum
def call(self):
self.olditemsel = self.itemsel
self.old_scroll_pos = copy(self.scroll_pos)
firstinterval = 8
interval = 2
if self.key != self.oldkey and self.key[ENTER] == False:
self.keycount = 0
self.oldkey = self.key.copy()
if (self.keycount == 0) or ( (((self.keycount) % interval) == 0) & (self.keycount > firstinterval) ):
if self.key[UP]:
self.itemsel -= self.lines
if self.itemsel < self.scroll_pos:
if self.scroll_pos > 0:
self.scroll_pos -= self.lines
if self.key[DOWN]:
self.itemsel += self.lines
if self.itemsel/self.lines > (self.scroll_pos + self.disp_itemnum)/self.lines-1:
if self.scroll_pos < (self.itemnum - self.disp_itemnum):
self.scroll_pos += self.lines
self.keycount += 1
if self.keyOnce[LEFT]:
if (self.itemsel%self.lines) > 0:
self.itemsel -= 1
self.back()
if self.keyOnce[RIGHT]:
if (self.itemsel%self.lines) < (self.lines-1):
self.itemsel += 1
self.forward()
if self.disp_itemnum == self.itemnum:
isScrollMenu = True
else:
isScrollMenu = False
if self.itemsel < 0:
if isScrollMenu:
self.itemsel += self.itemnum
self.scroll_pos = self.itemnum - self.disp_itemnum
else:
self.itemsel = copy(self.olditemsel)
elif self.itemsel >= self.itemnum:
if isScrollMenu:
self.itemsel -= self.itemnum
self.scroll_pos = 0
else:
self.itemsel = copy(self.olditemsel)
if self.keyOnce[ENTER]:
if self.itemlist[self.itemsel].obj != self.ITEM_CANTSELECT:
self.action()
else:
sound.playSound(3)
if self.keyOnce[CANCEL]:
self.cancel()
if self.itemsel != self.olditemsel:
self.onSelChange()
if self.connection:
self.count += 1
def onSelChange(self, reRenderFlag=False):
sound.playSound(0)
if reRenderFlag:
self.reRender()
else:
self.drawBuffer()
def action(self):
sound.playSound(1)
def cancel(self):
sound.playSound(2)
menuMgr.pop()
def forward(self):
pass
def back(self):
pass
def connect(self):
ControlInterface.connect(self)
self.doDispCursor = True
self.doDraw = True
self.connected = True
def disconnect(self):
ControlInterface.disconnect(self)
self.doDispCursor = False
def keyClear(self):
self.key = ControlInterface().key.copy()
self.oldkey = ControlInterface().key.copy()
self.keyOnce = ControlInterface().keyOnce.copy()
class ExitMenu(Menu):
def __init__(self):
global doCharCall
doCharCall = False
Menu.__init__(self)
self.zindex = 100
def getMenu(self):
self.itemlist = []
self.rect = Rect(240, 240, 160, -1)
self.help = u"Exit game?"
self.itemlist.append( MenuItem(u"exit", "end") )
self.itemlist.append( MenuItem(u"continue", "continue") )
def action(self):
Menu.action(self)
cmd = self.itemlist[self.itemsel].cmd
if cmd == 'continue':
menuMgr.pop()
elif cmd == 'end':
exit()
def __del__(self):
global doCharCall
doCharCall = True
class MenuManager:
def __init__(self):
self.menulist = []
self.menuHideList = [-1]
self.memItemsel = {}
self.memScroll_pos = {}
self.session = {}
self.gNaviList = []
self.helpfontsize = 16
self.helpfont = pygame.font.Font(g.config.fontpath, self.helpfontsize)
def add(self, menu, doConnect=False):
if menu.__class__ is tuple or menu.__class__ is list:
self.menulist.append(menu)
else:
listmenu = []
listmenu.append(menu)
self.menulist.append(listmenu)
if doConnect:
self.post(self, ["connect", menu.__class__.__name__])
self.menuHideList.append(self.menuHideList[-1])
def disconnect(self):
controller.disconnect()
def getAllMenu(self):
menulist = []
for menublock in self.menulist:
for menu in menublock:
menulist.append(menu)
return menulist
def getMenuByName(self, className):
for menu in self.getAllMenu():
trace(DL2, 'getMenuByName: ' + `menu`)
if isclass(menu, className):
return menu
return None
def pop(self):
menu = self.menulist.pop()