8f145b464b5fe8df89270795d052f54b1d3d8323
[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(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 = None
51 self.spaceSize = (100,100)
52 self.font = pygame.font.Font(None,70)
53 self._fill()
54 self._initLocalListeners()
55
56
57 def _fill(self):
58 #self.tr()
59 #self.td(Spacer(*self.spaceSize))
60
61
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 self.songTitle = self.createLabel(u'bonjour le monde !')
72 st = STYLE_RIGHT_COL.copy()
73 del st['padding_top']
74 self.td(self.songTitle, style = st)
75
76 # choix du niveau
77 self.tr()
78 self.td(self.createLabel("Niveau :"), 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 # choix des wiimotes
85 self.tr()
86 self.td(self.createLabel("Wiimote :", self.font), style=STYLE_LEFT_COL)
87 playerTable = Table()
88
89 self.selectedWiimote = Group(name='selectedWiimote', value=0)
90 for i in range(self.nwiimotes):
91 r = Radio(self.selectedWiimote, i)
92 playerTable.td(r)
93 playerTable.td(self.createLabel(" %d " % (i+1)))
94 self.td(playerTable, style=STYLE_RIGHT_COL)
95
96 # boutons jouer / quitter
97 self.tr()
98 self.quitButton = Button(self.createLabel("Quitter"))
99 self.td(self.quitButton, style={'padding_top' : 50})
100 self.playButton = Button(self.createLabel("Jouer"), disabled=True)
101 self.td(self.playButton, style={'padding_top' : 50})
102
103 @property
104 def selectedWiimoteIndex(self) :
105 return self.selectedWiimote.value
106
107 def _initLocalListeners(self) :
108 self.browseButton.connect(CLICK, self.open_file_browser)
109 self.quitButton.connect(CLICK, self._exitApp)
110 self.playButton.connect(CLICK, self._exitHome)
111
112 def _exitApp(self, data=None) :
113 self.exitApp = True
114 self.send(QUIT)
115
116 def _exitHome(self, data=None) :
117 self.exitApp = False
118 self.send(QUIT)
119
120 def createLabel(self,text,font = None):
121 if font == None :
122 font = self.font
123 w,h = self.font.size(text)
124 label = Label(text,width=w,height=h,font = font)
125 return(label)
126
127 def open_file_browser(self):
128 dlg = FileOpenDialog(self.songPath)
129 dlg.connect(CHANGE, self.handle_file_browser_closed, dlg)
130 dlg.open()
131
132 def handle_file_browser_closed(self, dlg) :
133 if dlg.value and os.path.isfile(dlg.value):
134 self.remove(self.songTitle)
135 self.songTitle = self.createLabel(os.path.basename(dlg.value))
136 self.td(self.songTitle, col=1, row=1, style=STYLE_RIGHT_COL)
137 self.songFile = dlg.value
138 self.playButton.disabled = False
139
140
141