Passage à MINDs 2.0.2.
[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,90)
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.exitApp = False
58 self._initLocalListeners()
59
60
61 def _fill(self):
62 # bouton pour browser de fichiers
63 self.tr()
64 st = STYLE_LEFT_COL.copy()
65 st['valign'] = -1
66 self.td(self.createLabel("Chanson :"), rowspan=2, style = st)
67 self.browseButton = Button(self.createLabel("Choisir..."))
68 self.td(self.browseButton,colspan=2, style = STYLE_RIGHT_COL)
69
70 self.tr()
71 caption = '' if not self.songFile else FileOpenDialog.getSongTitle(self.songFile)
72 self.songTitle = self.createLabel(caption, font=pygame.font.Font(None,60))
73 st = STYLE_RIGHT_COL.copy()
74 del st['padding_top']
75 self.td(self.songTitle, style = st)
76
77 # choix du mode de jeu
78 self.tr()
79 self.td(self.createLabel("Mode :"), style=STYLE_LEFT_COL)
80 self.modeSelect = Select('NORMAL')
81 for k, caption in PLAYING_MODES :
82 self.modeSelect.add(self.createLabel(caption), k)
83 self.td(self.modeSelect,colspan=3, style=STYLE_RIGHT_COL)
84
85 # affichage / masquage des noms de notes
86 self.tr()
87 self.td(self.createLabel("Notes :"), style=STYLE_LEFT_COL)
88 self.displayNotesSelect = Select(True)
89 for k, caption in ((True, 'Oui'), (False, 'Non')) :
90 self.displayNotesSelect.add(self.createLabel(caption), k)
91 self.td(self.displayNotesSelect, style=STYLE_RIGHT_COL)
92
93
94 # choix des wiimotes
95 self.tr()
96 self.td(self.createLabel("Wiimote :", self.font), style=STYLE_LEFT_COL)
97 playerTable = Table()
98
99 self.selectedWiimote = Group(name='selectedWiimote', value=0)
100 for i in range(self.nwiimotes - 1):
101 r = Radio(self.selectedWiimote, i)
102 playerTable.td(r)
103 playerTable.td(self.createLabel(" %d " % (i+1)))
104 self.td(playerTable, style=STYLE_RIGHT_COL)
105
106 # boutons jouer / quitter
107 self.tr()
108 self.quitButton = Button(self.createLabel("Quitter"))
109 self.td(self.quitButton, style={'padding_top' : 50})
110 self.playButton = Button(self.createLabel("Jouer"))
111 self.td(self.playButton, style={'padding_top' : 50})
112
113 @property
114 def selectedPlayMode(self) :
115 return self.modeSelect.value
116
117 @selectedPlayMode.setter
118 def selectedPlayMode(self, value) :
119 self.modeSelect.value = value
120 if self.selectedPlayMode != 'IMPRO' and \
121 not self.songFile :
122 self.playButton.disabled = True
123 else :
124 self.playButton.disabled = False
125
126 @property
127 def displayNotes(self) :
128 return self.displayNotesSelect.value
129
130 @displayNotes.setter
131 def displayNotes(self, value) :
132 self.displayNotesSelect.value = value
133
134 @property
135 def selectedWiimoteIndex(self) :
136 return self.selectedWiimote.value
137
138 @selectedWiimoteIndex.setter
139 def selectedWiimoteIndex(self, value) :
140 self.selectedWiimote.value = value
141
142 def _initLocalListeners(self) :
143 self.browseButton.connect(CLICK, self.open_file_browser)
144 self.quitButton.connect(CLICK, self._exitApp)
145 self.playButton.connect(CLICK, self._exitHome)
146 self.modeSelect.connect(CHANGE, self._changeModeCB)
147
148 def _changeModeCB(self) :
149 if self.selectedPlayMode == 'IMPRO' :
150 self.playButton.disabled = False
151 elif not self.songFile :
152 self.playButton.disabled = True
153
154 def _exitApp(self, data=None) :
155 self.exitApp = True
156 self.send(QUIT)
157
158 def _exitHome(self, data=None) :
159 self.exitApp = False
160 self.send(QUIT)
161
162 def createLabel(self,text,font = None):
163 if font == None :
164 font = self.font
165 w,h = self.font.size(text)
166 label = Label(text,width=w,height=h,font = font)
167 return(label)
168
169 def open_file_browser(self):
170 dlg = FileOpenDialog(self.songPath)
171 dlg.connect(CHANGE, self.handle_file_browser_closed, dlg)
172 dlg.open()
173
174 def handle_file_browser_closed(self, dlg) :
175 if dlg.value and os.path.isfile(dlg.value):
176 self.remove(self.songTitle)
177 title = FileOpenDialog.getSongTitle(dlg.value)
178 self.songTitle = self.createLabel(title, font= pygame.font.Font(None,60))
179 self.td(self.songTitle, col=1, row=1, style=STYLE_RIGHT_COL)
180 self.songFile = dlg.value
181 self.playButton.disabled = False
182
183
184