Back to index

freevial  1.2
dialog_question.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002  
00003 #
00004 # Freevial
00005 # Question Message
00006 #
00007 # Copyright (C) 2007, 2008 The Freevial Team
00008 #
00009 # By Carles Oriol i Margarit <carles@kumbaworld.com>
00010 # By Siegfried-Angel Gevatter Pujals <siggi.gevatter@gmail.com>
00011 #
00012 # This program is free software: you can redistribute it and/or modify
00013 # it under the terms of the GNU General Public License as published by
00014 # the Free Software Foundation, either version 3 of the License, or
00015 # (at your option) any later version.
00016 #
00017 # This program is distributed in the hope that it will be useful,
00018 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 # GNU General Public License for more details.
00021 #
00022 # You should have received a copy of the GNU General Public License
00023 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00024 #
00025 
00026 import sys
00027 import os
00028 import pygame
00029 
00030 from freevialglob import *
00031 from events import EventHandle
00032 
00033 class Question:
00034 
00035        def __init__(self):
00036        
00037               self.color = (255, 0, 0)
00038        
00039        def setSelColor ( self, color ):
00040 
00041               self.color = color
00042 
00043 
00044        def ask( self, screen, pregunta, respostes, predeterminat = 0, cancel = -1 ):
00045 
00046               frate = frameRate( 40 )
00047 
00048               seleccio = predeterminat
00049 
00050               sfc_copiascreen = pygame.Surface( ( screen.get_width(), screen.get_height()), pygame.SRCALPHA, 32 )
00051               sfc_screen2 = pygame.Surface( ( screen.get_width(), screen.get_height()), pygame.SRCALPHA, 32 )
00052 
00053               sfc_copiascreen.blit( screen, (0,0) )
00054 
00055               total_duracio_fadeout = duracio_fadeout = 10
00056               intensitat_fadeout = 20
00057 
00058               sfc_pregunta = render_text( pregunta, (255,255,255), 50, 1, '', 600 )
00059               sfc_respostes = []
00060 
00061               espai_entre_respostes = 32
00062               respostes_ample = 0
00063               for compta in range(0, len(respostes) ):
00064                      sfc = render_text( respostes[compta], (255,255,255), 50, 1, '', 500 )
00065                      respostes_ample += sfc.get_width()
00066                      if compta: respostes_ample += espai_entre_respostes
00067 
00068                      sfc_respostes.append( sfc )
00069 
00070               while 1:
00071 
00072                      for event in pygame.event.get():
00073 
00074                             eventhandle = EventHandle(event)
00075                             
00076                             if event.type == pygame.JOYBUTTONDOWN:
00077                                    translateJoystickEvent(event)
00078                             
00079                             if eventhandle.isQuit():
00080                                    sys.exit()
00081                             
00082                             if eventhandle.keyUp('ESCAPE', 'q'):
00083                                    return cancel
00084                             
00085                             if eventhandle.keyDown('PRINT'):
00086                                    screenshot(self.joc.screen)
00087                             
00088                             if eventhandle.keyUp('f', 'F11'):
00089                                    pygame.display.toggle_fullscreen()
00090                             
00091                             if eventhandle.keyUp('RIGHT'):     
00092                                    seleccio += 1
00093                                    seleccio %= len(respostes)
00094                             
00095                             if eventhandle.keyUp('LEFT'):      
00096                                    seleccio -= 1
00097                                    seleccio %= len(respostes)
00098                             
00099                             if eventhandle.isRelease('primary') or eventhandle.keyUp('RETURN', 'SPACE', 'KP_ENTER'):
00100                                    return seleccio
00101                             
00102                      if duracio_fadeout:
00103                             sfc_screen2.blit( sfc_copiascreen, (0,0) )
00104                             sfc_screen2.fill( (0,0,0, total_duracio_fadeout * intensitat_fadeout - duracio_fadeout * intensitat_fadeout) ) 
00105        
00106                             duracio_fadeout -= 1
00107                             
00108                      screen.blit( sfc_copiascreen, (0,0) )
00109                      screen.blit( sfc_screen2, (0,0) )
00110 
00111                      screen.blit( sfc_pregunta, (screen.get_width() / 2 -sfc_pregunta.get_width() / 2, 300) )
00112 
00113                      posx = (screen.get_width() / 2 - respostes_ample / 2 )
00114 
00115                      for compta in range(0, len(respostes) ):
00116 
00117                             if seleccio == compta:
00118                                    for salt in range(0, 25):
00119                                           screen.fill( (self.color[0] * salt / 25, self.color[1]* salt / 25, self.color[2]* salt / 25), (posx - 25 + salt, 450, sfc_respostes[compta].get_width() + 25*2-salt*2, sfc_respostes[compta].get_height()  ))
00120 
00121                             screen.blit( sfc_respostes[compta], (posx, 450) )
00122                             posx += sfc_respostes[compta].get_width() + espai_entre_respostes
00123 
00124                      frate.next( screen )
00125 
00126                      pygame.display.flip()
00127 
00128 
00129 def fesPregunta( screen, pregunta, respostes, predeterminat = 0, cancel = -1, color = (255,0,0) ):
00130 
00131        question = Question()
00132 
00133        question.setSelColor( color )
00134 
00135        return question.ask( screen, pregunta, respostes, predeterminat = 0, cancel = -1 )