Ajout d'une image par défaut si jamais il n'existe pas d'illustration personnalisée.
[minwii.git] / src / minwii / widgets / home.py
1 # -*- coding: utf-8 -*-
2 """
3 Écran de démarrage minwii.
4
5 $Id$
6 $URL$
7 """
8 from pgu.gui import Table
9 from pgu.gui import Spacer
10 from pgu.gui import Label
11 from pgu.gui import Button
12 from pgu.gui import Switch
13 from pgu.gui import Radio
14 from pgu.gui import Group
15 from pgu.gui import Select
16 from pgu.gui import CLICK
17 from pgu.gui import QUIT
18 from pgu.gui import CHANGE
19 import pygame
20 from minwii.globals import PLAYING_MODES
21 from songfilebrowser import FileOpenDialog
22 import os.path
23
24 STYLE_LEFT_COL = {#'border_right' : 2,
25 #'border_left' : 2,
26 'align' : 1,
27 'padding_top' : 30,
28 'padding_right' : 10}
29
30 STYLE_RIGHT_COL = {#'border_right' : 2,
31 #'border_left' : 2,
32 'align' : -1,
33 'padding_top' : 30,
34 'padding_left' : 20}
35
36
37 class Home(object, Table) :
38 """
39 Écran de démarrage de minwii
40 """
41
42 def __init__(self,**params):
43 style = {'valign' : 0,
44 'width' : pygame.display.get_surface().get_width(),
45 'height' : pygame.display.get_surface().get_height()}
46 params['style'] = style
47 Table.__init__(self,**params)
48 self.songPath = params.get('songPath', '.')
49 self.nwiimotes = params.get('nwiimotes', 0)
50 self.songFile = params.get('songFile', '')
51 self.spaceSize = (100,100)
52 self.font = pygame.font.Font(None,70)
53 self._fill()
54 self.selectedPlayMode = params.get('playMode', 'NORMAL')
55 self.displayNotes = params.get('displayNotes', True)
56 self.selectedWiimoteIndex = params.get('wiimoteIndex', 0)
57 self._initLocalListeners()
58
59
60 def _fill(self):
61 # bouton pour browser de fichiers
62 self.tr()
63 st = STYLE_LEFT_COL.copy()
64 st['valign'] = -1
65 self.td(self.createLabel("Chanson :"), rowspan=2, style = st)
66 self.browseButton = Button(self.createLabel("Choisir..."))
67 self.td(self.browseButton,colspan=2, style = STYLE_RIGHT_COL)
68
69 self.tr()
70 caption = '' if not self.songFile else FileOpenDialog.getSongTitle(self.songFile)
71 self.songTitle = self.createLabel(caption)
72 st = STYLE_RIGHT_COL.copy()
73 del st['padding_top']
74 self.td(self.songTitle, style = st)
75
76 # choix du mode de jeu
77 self.tr()
78 self.td(self.createLabel("Mode :"), style=STYLE_LEFT_COL)
79 self.modeSelect = Select('NORMAL')
80 for k, caption in PLAYING_MODES :
81 self.modeSelect.add(self.createLabel(caption), k)
82 self.td(self.modeSelect,colspan=3, style=STYLE_RIGHT_COL)
83
84 # affichage / masquage des noms de notes
85 self.tr()
86 self.td(self.createLabel("Notes :"), style=STYLE_LEFT_COL)
87 self.displayNotesSelect = Select(True)
88 for k, caption in ((True, 'Oui'), (False, 'Non')) :
89 self.displayNotesSelect.add(self.createLabel(caption), k)
90 self.td(self.displayNotesSelect, style=STYLE_RIGHT_COL)
91
92
93 # choix des wiimotes
94 self.tr()
95 self.td(self.createLabel("Wiimote :", self.font), style=STYLE_LEFT_COL)
96 playerTable = Table()
97
98 self.selectedWiimote = Group(name='selectedWiimote', value=0)
99 for i in range(self.nwiimotes - 1):
100 r = Radio(self.selectedWiimote, i)
101 playerTable.td(r)
102 playerTable.td(self.createLabel(" %d " % (i+1)))
103 self.td(playerTable, style=STYLE_RIGHT_COL)
104
105 # boutons jouer / quitter
106 self.tr()
107 self.quitButton = Button(self.createLabel("Quitter"))
108 self.td(self.quitButton, style={'padding_top' : 50})
109 self.playButton = Button(self.createLabel("Jouer"))
110 self.td(self.playButton, style={'padding_top' : 50})
111
112 @property
113 def selectedPlayMode(self) :
114 return self.modeSelect.value
115
116 @selectedPlayMode.setter
117 def selectedPlayMode(self, value) :
118 self.modeSelect.value = value
119 if self.selectedPlayMode != 'IMPRO' and \
120 not self.songFile :
121 self.playButton.disabled = True
122 else :
123 self.playButton.disabled = False
124
125 @property
126 def displayNotes(self) :
127 return self.displayNotesSelect.value
128
129 @displayNotes.setter
130 def displayNotes(self, value) :
131 self.displayNotesSelect.value = value
132
133 @property
134 def selectedWiimoteIndex(self) :
135 return self.selectedWiimote.value
136
137 @selectedWiimoteIndex.setter
138 def selectedWiimoteIndex(self, value) :
139 self.selectedWiimote.value = value
140
141 def _initLocalListeners(self) :
142 self.browseButton.connect(CLICK, self.open_file_browser)
143 self.quitButton.connect(CLICK, self._exitApp)
144 self.playButton.connect(CLICK, self._exitHome)
145 self.modeSelect.connect(CHANGE, self._changeModeCB)
146
147 def _changeModeCB(self) :
148 if self.selectedPlayMode == 'IMPRO' :
149 self.playButton.disabled = False
150 elif not self.songFile :
151 self.playButton.disabled = True
152
153 def _exitApp(self, data=None) :
154 self.exitApp = True
155 self.send(QUIT)
156
157 def _exitHome(self, data=None) :
158 self.exitApp = False
159 self.send(QUIT)
160
161 def createLabel(self,text,font = None):
162 if font == None :
163 font = self.font
164 w,h = self.font.size(text)
165 label = Label(text,width=w,height=h,font = font)
166 return(label)
167
168 def open_file_browser(self):
169 dlg = FileOpenDialog(self.songPath)
170 dlg.connect(CHANGE, self.handle_file_browser_closed, dlg)
171 dlg.open()
172
173 def handle_file_browser_closed(self, dlg) :
174 if dlg.value and os.path.isfile(dlg.value):
175 self.remove(self.songTitle)
176 title = FileOpenDialog.getSongTitle(dlg.value)
177 self.songTitle = self.createLabel(title)
178 self.td(self.songTitle, col=1, row=1, style=STYLE_RIGHT_COL)
179 self.songFile = dlg.value
180 self.playButton.disabled = False
181
182
183