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