990f27565ba71af94d9aeef2ce3156b5e467a649
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 config
import INSTRUMENTS
14 from globals import BACKGROUND_LAYER
15 from globals import FOREGROUND_LAYER
16 from globals import CURSOR_LAYER
17 from globals import hls_to_rgba_8bits
20 class InstrumentSelector(pygame
.sprite
.LayeredDirty
, EventHandlerMixin
) :
24 instruments
= INSTRUMENTS
27 super(InstrumentSelector
, self
).__init
__()
31 self
._inflatedTile
= None
33 def _initTiles(self
) :
34 screen
= pygame
.display
.get_surface()
35 tileWidth
= int(round(float(screen
.get_width()) / self
.cols
))
36 tileHeight
= int(round(float(screen
.get_height()) / self
.rows
))
39 instrus
= list(self
.instruments
[:])
40 for y
in range(self
.cols
) :
41 for x
in range(self
.rows
) :
42 upperLeftCorner
= (x
* tileWidth
, y
* tileHeight
)
43 rect
= pygame
.Rect(upperLeftCorner
, (tileWidth
, tileHeight
))
44 # !!! s'il y avait plus de 3x3 tuiles !!!, il faudrait alors
45 # changer le tuple (x,y) qui concerne le point d'application de l'homotétie.
46 # Cf. InstrumentTile.inflate
47 tile
= InstrumentTile(instrus
.pop(0), self
, rect
, (x
,y
))
48 self
.add(tile
, layer
=BACKGROUND_LAYER
)
49 self
.tiles
.append(tile
)
51 def _initCursor(self
) :
52 self
.cursor
= WarpingCursor(blinkMode
=True)
53 self
.add(self
.cursor
, layer
=CURSOR_LAYER
)
58 clock
= pygame
.time
.Clock()
60 pygame
.mouse
.set_visible(False)
62 EventDispatcher
.dispatchEvents()
63 dirty
= self
.draw(pygame
.display
.get_surface())
64 pygame
.display
.update(dirty
)
69 pygame
.mouse
.set_visible(True)
70 self
.cursor
._stopBlink
()
72 @event_handler(pygame
.KEYDOWN
)
73 def handleKeyDown(self
, event
) :
74 if event
.key
== pygame
.K_q
:
77 @event_handler(pygame
.MOUSEMOTION
)
78 def onMouseMove(self
, event
) :
79 for tile
in reversed(self
.sprites()[:-1]) :
80 if tile
.rect
.collidepoint(*event
.pos
) :
81 self
.raiseTileOver(tile
)
84 def raiseTileOver(self
, tile
) :
85 if not tile
.inflated
:
86 self
.change_layer(tile
, FOREGROUND_LAYER
)
87 tile
.inflate(tile
.coords
)
89 if self
._inflatedTile
:
90 self
._inflatedTile
.deflate()
91 self
.change_layer(self
._inflatedTile
, BACKGROUND_LAYER
)
93 self
._inflatedTile
= tile
95 @event_handler(pygame
.MOUSEBUTTONDOWN
)
96 def selectInstrument(self
, event
) :
97 for tile
in reversed(self
.sprites()[:-1]) :
98 if tile
.rect
.collidepoint(*event
.pos
) :
99 self
.selectedInstrument
= tile
.instrumentDescription
105 class InstrumentTile(pygame
.sprite
.DirtySprite
) :
108 def _get_instrument_image(name
) :
109 imagePath
= os
.path
.abspath(__file__
).split(os
.path
.sep
)[:-1]
110 imagePath
.extend(['data', 'instruments'])
111 name
, ext
= os
.path
.splitext(name
)
112 imagePath
.append('%s%s' % (name
, ext
or '.jpg'))
113 return os
.path
.sep
.join(imagePath
)
118 def __init__(self
, instrumentDescription
, group
, rect
, coords
) :
119 pygame
.sprite
.DirtySprite
.__init
__(self
, group
)
120 self
.inflated
= False
121 self
.instrumentDescription
= instrumentDescription
123 self
._baseRect
= rect
.copy()
125 imagePath
= InstrumentTile
._get
_instrument
_image
(instrumentDescription
['name'])
126 self
._img
= pygame
.image
.load(imagePath
)
131 innerWidth
, innerHeight
= [l
-self
.BORDER
*2 for l
in self
.rect
.size
]
132 innerSize
= innerWidth
, innerHeight
134 border
= pygame
.Surface(self
.rect
.size
)
135 border
.fill((0xdd,0xdd,0xdd,255))
137 bg
= pygame
.Surface(innerSize
)
138 bg
.fill((255,255,255,255))
139 bgRect
= pygame
.Rect((self
.BORDER
, self
.BORDER
), innerSize
)
142 iWidth
, iHeight
= img
.get_size()
143 imgRatio
= float(iWidth
) / iHeight
147 ih
= int(round(innerWidth
/ imgRatio
))
151 iw
= int(round(innerHeight
* imgRatio
))
153 imgPosition
= ((innerWidth
- iw
) / 2, (innerHeight
- ih
) / 2)
154 imgRect
= pygame
.Rect(imgPosition
, (iw
, ih
))
155 img
= pygame
.transform
.smoothscale(img
, (iw
, ih
))
157 bg
.blit(img
, imgRect
)
158 border
.blit(bg
, bgRect
)
162 def inflate(self
, refPoint
) :
165 for name
in REF_POINTS
[refPoint
] :
166 keep
[name
] = getattr(self
.rect
, name
)
168 self
.rect
.inflate_ip(*[l
*self
.INFLATE_ZOOM
for l
in self
.rect
.size
])
170 for k
, v
in keep
.items() :
171 setattr(self
.rect
, k
, v
)
178 self
.inflated
= False
179 self
.rect
= self
._baseRect
.copy()
186 (0, 0) : ['top', 'left'],
188 (2, 0) : ['top', 'right'],
194 (0, 2) : ['bottom', 'left'],
196 (2, 2) : ['bottom', 'right']