9bafbc32fd40abc513c7dc3cb10daecc87df9a3e
1 # -*- coding: utf-8 -*-
3 Écran de sélection de l'instrument
10 from eventutils
import event_handler
, EventDispatcher
, EventHandlerMixin
11 from cursors
import WarpingCursor
12 from config
import FRAMERATE
13 from globals import BACKGROUND_LAYER
14 from globals import FOREGROUND_LAYER
15 from globals import CURSOR_LAYER
16 from globals import hls_to_rgba_8bits
19 class InstrumentSelector(pygame
.sprite
.LayeredDirty
, EventHandlerMixin
) :
23 instruments
= ['accordeon', 'celesta', 'flute', 'guitare', 'orgue', 'piano', 'tuba', 'violon', 'violoncelle']
26 super(InstrumentSelector
, self
).__init
__()
30 self
._inflatedTile
= None
32 def _initTiles(self
) :
33 screen
= pygame
.display
.get_surface()
34 tileWidth
= int(round(float(screen
.get_width()) / self
.cols
))
35 tileHeight
= int(round(float(screen
.get_height()) / self
.rows
))
38 instrus
= self
.instruments
[:]
39 for y
in range(self
.cols
) :
40 for x
in range(self
.rows
) :
41 upperLeftCorner
= (x
* tileWidth
, y
* tileHeight
)
42 rect
= pygame
.Rect(upperLeftCorner
, (tileWidth
, tileHeight
))
43 tile
= InstrumentTile(instrus
.pop(0), self
, rect
, (x
,y
))
44 self
.add(tile
, layer
=BACKGROUND_LAYER
)
45 self
.tiles
.append(tile
)
47 def _initCursor(self
) :
48 self
.cursor
= WarpingCursor(blinkMode
=True)
49 self
.add(self
.cursor
, layer
=CURSOR_LAYER
)
54 clock
= pygame
.time
.Clock()
56 pygame
.mouse
.set_visible(False)
58 EventDispatcher
.dispatchEvents()
59 dirty
= self
.draw(pygame
.display
.get_surface())
60 pygame
.display
.update(dirty
)
65 pygame
.mouse
.set_visible(True)
66 self
.cursor
._stopBlink
()
68 @event_handler(pygame
.KEYDOWN
)
69 def handleKeyDown(self
, event
) :
70 if event
.key
== pygame
.K_q
:
73 @event_handler(pygame
.MOUSEMOTION
)
74 #@event_handler(pygame.MOUSEBUTTONDOWN)
75 def onMouseMove(self
, event
) :
76 for tile
in reversed(self
.sprites()[:-1]) :
77 if tile
.rect
.collidepoint(*event
.pos
) :
78 self
.raiseTileOver(tile
)
81 def raiseTileOver(self
, tile
) :
82 if not tile
.inflated
:
83 self
.change_layer(tile
, FOREGROUND_LAYER
)
84 tile
.inflate(tile
.coords
)
86 if self
._inflatedTile
:
87 self
._inflatedTile
.deflate()
88 self
.change_layer(self
._inflatedTile
, BACKGROUND_LAYER
)
90 self
._inflatedTile
= tile
92 @event_handler(pygame
.MOUSEBUTTONDOWN
)
93 def selectInstrument(self
, event
) :
94 for tile
in reversed(self
.sprites()[:-1]) :
95 if tile
.rect
.collidepoint(*event
.pos
) :
96 self
.selectedInstrument
= tile
102 class InstrumentTile(pygame
.sprite
.DirtySprite
) :
105 def _get_instrument_image(name
) :
106 imagePath
= os
.path
.abspath(__file__
).split(os
.path
.sep
)[:-1]
107 imagePath
.extend(['data', 'instruments'])
108 name
, ext
= os
.path
.splitext(name
)
109 imagePath
.append('%s%s' % (name
, ext
or '.jpg'))
110 return os
.path
.sep
.join(imagePath
)
115 def __init__(self
, name
, group
, rect
, coords
) :
116 pygame
.sprite
.DirtySprite
.__init
__(self
, group
)
117 self
.inflated
= False
120 self
._baseRect
= rect
.copy()
122 imagePath
= InstrumentTile
._get
_instrument
_image
(name
)
123 self
._img
= pygame
.image
.load(imagePath
)
128 innerWidth
, innerHeight
= [l
-self
.BORDER
*2 for l
in self
.rect
.size
]
129 innerSize
= innerWidth
, innerHeight
131 border
= pygame
.Surface(self
.rect
.size
)
132 border
.fill((0,0,0,255))
134 bg
= pygame
.Surface(innerSize
)
135 bg
.fill((255,255,255,255))
136 bgRect
= pygame
.Rect((self
.BORDER
, self
.BORDER
), innerSize
)
139 iWidth
, iHeight
= img
.get_size()
140 imgRatio
= float(iWidth
) / iHeight
144 ih
= int(round(innerWidth
/ imgRatio
))
148 iw
= int(round(innerHeight
* imgRatio
))
150 imgPosition
= ((innerWidth
- iw
) / 2, (innerHeight
- ih
) / 2)
151 imgRect
= pygame
.Rect(imgPosition
, (iw
, ih
))
152 img
= pygame
.transform
.smoothscale(img
, (iw
, ih
))
154 bg
.blit(img
, imgRect
)
155 border
.blit(bg
, bgRect
)
159 def inflate(self
, refPoint
) :
162 for name
in REF_POINTS
[refPoint
] :
163 keep
[name
] = getattr(self
.rect
, name
)
165 self
.rect
.inflate_ip(*[l
*self
.INFLATE_ZOOM
for l
in self
.rect
.size
])
167 for k
, v
in keep
.items() :
168 setattr(self
.rect
, k
, v
)
175 self
.inflated
= False
176 self
.rect
= self
._baseRect
.copy()
183 (0, 0) : ['top', 'left'],
185 (2, 0) : ['top', 'right'],
191 (0, 2) : ['bottom', 'left'],
193 (2, 2) : ['bottom', 'right']