1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# -*- coding: utf-8 -*-
import pygame
from window import Window
from utilities import Text, TextBlock
"""
Clase encargada del control de ventanas
"""
class WindowsController:
def __init__(self, screen):
internal_size = 1200, 780 # The game is meant to run in this resolution
self.scaled_game = ScaledGame(screen, internal_size)
self.screen = self.scaled_game.get_internal_screen()
self.windows = {} # Diccionario de ventanas. Aca se encuentran todas las ventanas del programa
self.windows_stack = [] # Stack de ventanas para el control de venta activa. Aca se enceuntra el stack de ventanas abiertas
self.reload_main = True
self.next_update_list = [] # Keeps track of updates done to the screen
# Tooltips
self.showing_tooltip = False
self.active_tooltip_bg = None
self.active_tooltip = None
def get_screen(self):
return self.screen
# Windows
def close_active_window(self):
self.windows_stack[-1].repaint = True
# Solo puede ser llamado por la ventana activa e implica
# hacer un pop del stack
self.windows_stack.pop()
self.show_window_hierarchy(self.windows_stack[-1])
if self.windows_stack[-1].get_register_id() == "main_window":
self.game_man.continue_game()
self.reload_main = True
for win in self.windows_stack[-1].windows:
if isinstance(win, Window):
win.enable_repaint()
def set_active_window(self, window_key):
if window_key <> "main_window":
self.game_man.pause_game()
self.windows_stack.append(self.windows[window_key])
self.show_window_hierarchy(self.windows_stack[-1])
def register_new_window(self, id, window):
self.windows[id] = window
def unregister_window(self, window):
try:
del self.windows[window.register_id]
except:
pass
def show_window_hierarchy(self, window):
print window.get_register_id()
W = []
for win in window.windows:
W.append(win.register_id)
print(" (%s)" % (W))
# Events handlers
def handle_mouse_down(self, (x, y)):
x, y = self.scaled_game.scale_coordinates((x, y))
self.windows_stack[-1].handle_mouse_down((x, y))
def handle_mouse_up(self, pos):
self.windows_stack[-1].handle_mouse_up(pos)
def handle_mouse_over(self, (x, y)):
x, y = self.scaled_game.scale_coordinates((x, y))
self.windows_stack[-1].handle_mouse_over((x, y))
def handle_mouse_motion(self, (x, y)):
x, y = self.scaled_game.scale_coordinates((x, y))
self.windows_stack[-1].handle_mouse_motion((x, y))
# Tooltips
def show_tooltip(self, tooltip):
x, y = self.scaled_game.scale_coordinates(pygame.mouse.get_pos())
self.active_tooltip = Text(self.screen.get_rect(), x, y, 1, tooltip, 18, pygame.Color('red'), "tooltip")
# Necesitamos guardar lo que esta atras del tooltip para cuando lo querramos esconder
self.active_tooltip_bg = (self.screen.subsurface(self.active_tooltip.rect_absolute).copy(), self.active_tooltip.rect_absolute)
self.showing_tooltip = True
def show_super_tooltip(self, tooltip):
x, y = self.scaled_game.scale_coordinates(pygame.mouse.get_pos())
self.active_tooltip = TextBlock(self.screen.get_rect(), x, y, 1, tooltip, 18, pygame.Color('red'), "tooltip")
self.active_tooltip_bg = (self.screen.subsurface(self.active_tooltip.rect_absolute).copy(), self.active_tooltip.rect_absolute)
self.showing_tooltip = True
def hide_active_tooltip(self):
# Solo se ejecuta si se esta mostrando algun tooltip en la pantalla
if self.showing_tooltip:
# Hacemos un blit con lo que tenia atras el tooltip
self.screen.blit(self.active_tooltip_bg[0], self.active_tooltip_bg[1])
# Lo guardamos en la lista de las proximas actualizaciones
self.next_update(self.active_tooltip_bg[1])
self.showing_tooltip = False
# Updates to the screen
def next_update(self, rect):
"""
Add a rect that must be updated at next update
"""
self.next_update_list.append(rect)
def update(self, frames):
"""
Updates GUI
"""
# Cada vez que "volvamos" a la ventana principal es necesario
# repintar el fondo para que no queden rastros de la ventana anterior
if self.reload_main:
pygame.display.flip() # Actualizamos el screen para hacer visibles los efectos
self.reload_main = False
self.windows_stack[-1].update(frames)
changes = []
if frames % self.windows_stack[-1].frame_rate == 0:
changes.extend(self.windows_stack[-1].draw(self.screen, frames))
if changes:
if self.next_update_list:
changes.extend(self.next_update_list)
self.next_update_list = [] # Vaciamos la lista
# Tooltips
if self.showing_tooltip:
if isinstance(self.active_tooltip, Text):
self.screen.fill((255, 255, 255), self.active_tooltip.rect_in_container)
# Le decimos al tooltip (widget) que se dibuje
self.active_tooltip.draw(self.screen)
changes.append(self.active_tooltip_bg[1])
self.scaled_game.update_screen(changes)
#self.scaled_game.flip()
class ScaledGame:
def __init__(self, pygame_screen, internal_size):
self.screen = pygame_screen
pygame_screen_size = pygame_screen.get_size()
self.scale_factor = pygame_screen_size[0] / float(internal_size[0]), pygame_screen_size[1] / float(internal_size[1])
if self.scale_factor == (1, 1):
self.internal_screen = self.screen
else:
self.internal_screen = pygame.Surface(internal_size)
def get_internal_screen(self):
""" Returns the screen where everything should be drawn.
If using scalation its a virtual surface if not is the real display surface.
"""
return self.internal_screen
def flip(self):
if self.scale_factor == (1, 1):
pygame.display.flip()
else:
pygame.transform.scale(self.internal_screen, self.screen.get_size(), self.screen)
pygame.display.flip()
def update_screen(self, rect_list):
if self.scale_factor == (1, 1):
pygame.display.update(rect_list)
else:
pygame.transform.scale(self.internal_screen, self.screen.get_size(), self.screen)
pygame.display.update(self.scale_rect_list(rect_list))
def scale_rect_list(self, rect_list):
return [self.scale_rect(rect) for rect in rect_list if rect]
def scale_rect(self, rect):
if rect:
left = int(rect.left * self.scale_factor[0])
top = int(rect.top * self.scale_factor[1])
width = int(rect.width * self.scale_factor[0])
height = int(rect.height * self.scale_factor[1])
return pygame.Rect(left, top, width, height)
else:
return None
def scale_coordinates(self, display_coordinates):
""" Retruns the internal coordinates corresponding to the display coordinates """
if self.scale_factor == (1, 1):
return display_coordinates
else:
x = int(display_coordinates[0] / self.scale_factor[0])
y = int(display_coordinates[1] / self.scale_factor[0])
return x, y
|