Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Saludame.activity/menu.py
blob: 1e7025b9cd1b156c510bcbf09b10816cbc47dd33 (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# -*- coding: utf-8 -*-

# This module offers a menu.
# Items are displayed in a circle with an exit button as a center.
# The diameter of the circle will vary with the quantity of items
# Items will be a concatenation of an icon and a name
# When mouse over an item a description will be shown

import pygame
import os
import math
import gui
import utilities
import effects
import random
from gettext import gettext as _

SIZE = 700, 300
EXP_SPEED = 15.0 #expansion speed, in pixels per frame
MAX_ITEMS = 8 #max items quantity per selection
RADIUS = 90.0

#fonts
LARGE_TEXT = 10 # larger than this will use large button

#buttons
SMALL_BUTTON = "assets/menu/A.png"
LARGE_BUTTON = "assets/menu/B.png"
CENTER_BUTTON = "assets/menu/center.png"
HELP_BUTTON = "assets/menu/menu_help.png"

CLOSE_MENU = "close_menu"
BACK_MENU = "back_menu"

class Menu(gui.Window):
    
    def __init__(self, frame_rate, container, windows_controller, item_list, center, radius, game_manager, font):
        
        rect = pygame.Rect((0, 0), SIZE)
        rect.center = center
        self.game_manager = game_manager
        
        gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "menu_window")

        self.depth = 0 #it means we are in the root of the menu, mayor values means we are not.
        
        self.center = center # center of the menu's circle
        
        self.frame_rate = frame_rate
        self.item_list = item_list # item's list that going to be displayed, root items
        self.previous_items = []
        
        self.exit = Item(container, frame_rate, _("exit"), CENTER_BUTTON, CLOSE_MENU, [], self, font, None, None, True)
        self.exit.rect_in_container.center = center
        self.exit.set_rect_in_container(self.exit.rect_in_container)
        
        self.back = Item(container, frame_rate, _("back"), CENTER_BUTTON, BACK_MENU, [], self, font, None, None, True)
        self.back.rect_in_container.center = center
        self.back.set_rect_in_container(self.back.rect_in_container)
        
        self.current_selection = self.item_list  #list of current subitems selection
        
        self.radius = RADIUS
        self.hide()

        self.on_expansion = False
        self.calculate()
        
    
    def add_item(self, item):
        self.item_list.append(item)
    
    def set_items(self, items_list):
        self.item_list = items_list
    
    
    def update(self, frames):
        if self.visible and self.on_expansion:
            if self.radius < 90:
                self.radius += EXP_SPEED
                self.__calculate_items_position(self.current_selection)
            else:
                self.on_expansion = False
        
        if self.visible:
            self.set_dirty_background() # Sets the background as dirty, cause the menĂº is animated
    
    def send_action(self, action_id):
        """
        Send an action to the game_manager. The action was selected
        in one of the sub-items
        """
        if action_id == CLOSE_MENU:
            self.close()
        elif action_id == BACK_MENU:
            self.back()
        else:
            self.game_manager.execute_action(action_id)
            self.close()
    
    def set_current_selection(self, items_list):
        """
        Set the current items selection.
        """
        if not self.on_expansion:
            current_selection = self.get_allowed_items(items_list) # gets some allowed items, fewer than nine
            if len(current_selection) > MAX_ITEMS:
                current_selection = random.sample(current_selection, MAX_ITEMS)
            
            self.current_selection = current_selection
            #
            self.on_expansion = True #if the selection changes, display the animation
            self.radius = 0
            
            self.clear_childs()
            map(self.add_button, self.current_selection)

            if self.depth == 0:
                self.add_button(self.exit)
            else:
                self.add_button(self.back)

    
    def get_allowed_items(self, items_list):
        """
        Verifies wich items are allowed to perform its actions
        for currently character's properties.
        """
        allowed_items = []
        for item in items_list:
            #verifies item conditions
            if self.verify_item(item, self.game_manager):
                #verifies item's action conditions
                if item.action_id: #the item hasn't sub items
                    action = self.game_manager.get_action(item.action_id)
                    if self.verify_action(action, self.game_manager):
                        allowed_items.append(item)
                else:
                    allowed_items.append(item)
        return allowed_items
    
    def verify_item(self, item, game_manager):
        #verify place
        if item.allowed_places:
            allowed = False
            current_place = game_manager.get_current_place()
            for place in item.allowed_places:
                if current_place == place:
                    allowed = True
                    break
            if not allowed:
                return False
        #verify hour
        if item.allowed_hours:
            allowed = False
            current_hour = game_manager.get_current_hour()
            for hour in item.allowed_hours:
                if current_hour == hour:
                    allowed = True
                    break
            if not allowed:
                return False

        return True
    
    def verify_action(self, action, game_manager):
        
        #verify place
        allowed = False
        if action.allowed_places:
            current_place = game_manager.get_current_place()
            for place in action.allowed_places:
                if current_place == place:
                    allowed = True
                    break
            if not allowed:
                return False
            
        #verify hour
        if action.allowed_hours:
            allowed = False
            current_hour = game_manager.get_current_hour()
            for hour in action.allowed_hours:
                if current_hour == hour:
                    allowed = True
                    break
            if not allowed:
                return False
            
        #verify event
        if action.allowed_events:
            allowed = False
            active_events = game_manager.get_active_events()
            for evt_name in action.allowed_events:
                for active_evt in active_events:
                    if evt_name == active_evt.name:
                        allowed = True
                        break
            if not allowed:
                return False

        if action.level > self.game_manager.get_level():
            return False
        #verify path
        if not utilities.verify_path(action, self.game_manager):
            return False
              
        return True

        
    def close(self):
        """
        Close the Menu Window
        """
        self.hide()
        self.depth = 0
        self.previous_items = []
        self.current_selection = []
        self.clear_childs()
        self.game_manager.menu_active = False

    def back_to_previous_selection(self):
        """
        comes back to a previous items selection.
        """
        self.depth -= 1
        self.set_current_selection(self.previous_items[self.depth])
        if self.depth == 0:
            self.previous_items = []

    def show_items(self, subitems_list):
        """
        shows the recive items
        """
        self.previous_items.append(self.current_selection)
        self.depth += 1
        self.set_current_selection(subitems_list)

    
    #handlers
    def handle_mouse_down(self, coord):
        if self.visible and not self.on_expansion:
            if self.exit.rect_absolute.collidepoint(coord) and self.depth == 0:
                self.close()
            elif self.exit.rect_absolute.collidepoint(coord) and self.depth > 0: #click on back item, it's in the same position of exit item
                self.back_to_previous_selection()
            else:
                for item in self.current_selection:
                    if item.rect_absolute.collidepoint(coord):
                        item.on_mouse_click()
                        break

        else:
            self.set_current_selection(self.item_list)
            self.show()
            self.game_manager.menu_active = True

    #privates
    
    def calculate(self):
        """
            Calculate the position for each menu's current selection.
        """
        self.__calculate_items_position(self.current_selection)
        
    def __calculate_items_position(self, item_list):
        if len(item_list) > 0:
            angle = (2.0 * math.pi) / len(item_list)
        else:
            angle = 0.0
        current_angle = - math.pi / 2.0 - angle / 2.0
        for item in item_list:
            self.__calculate_item_position(item, current_angle) #calculate the position for each item
            current_angle += angle
      
    def __calculate_item_position(self, item, angle):
        """
        Calculates the position in the display for each menu item.
        """
        coord = int(self.center[0] + math.cos(angle) * self.radius), int(self.center[1] + math.sin(angle) * self.radius)
        rect = item.rect_in_container
        if coord[0] < self.center[0]:
            if coord[1] > self.center[1]: #third quadrant
                rect.topright = coord
            elif coord[1] < self.center[1]: #second quadrant
                rect.bottomright = coord
            else:
                rect.midright = coord
        elif coord[0] > self.center[0]:
            if coord[1] > self.center[1]: #fourth quadrant
                rect.topleft = coord
            elif coord[1] < self.center[1]: #first quadrant
                rect.bottomleft = coord
            else:
                rect.midleft = coord
        else:
            if coord[1] > self.center[1]:
                rect.midbottom = coord[0], coord[1] + 25
            else:
                rect.midtop = coord[0], coord[1] - 25
        item.set_rect_in_container(rect)   # Recalculates the absolute coordinates
        

class Item(gui.Button):
    """
    Entity that represent an item
    """
    def __init__(self, container, frame_rate, name, icon_path, action_id, subitems_list, menu, font, allowed_places = None, allowed_hours = None,  center_item=False):
        
        self.name = name
        self.subitems_list = subitems_list
        self.action_id = action_id
        self.menu = menu
        self.allowed_places = allowed_places
        self.allowed_hours = allowed_hours
        
        self.help_image = None
        self.bg_image = None
        if center_item:
            self.bg_image = pygame.image.load(CENTER_BUTTON).convert()
        else:
            if len(self.name) > LARGE_TEXT:
                self.bg_image = pygame.image.load(LARGE_BUTTON).convert()
            else:
                self.bg_image = pygame.image.load(SMALL_BUTTON).convert()
        self.bg_rect = self.bg_image.get_rect()
        
        action = self.menu.game_manager.get_action(self.action_id)
        if action:
            if action.link: #has to show help button
                self.help_image = pygame.image.load(HELP_BUTTON).convert()
                self.help_rect = self.help_image.get_rect()
                
        size, surface = self.get_surface(20, self.name, self.bg_image, self.help_image)
        
        self.rect = pygame.Rect((0, 0), size)
        
        gui.Button.__init__(self, container, self.rect, frame_rate, surface)

    def get_surface(self, font_size, text, bg_image, help_image):
        font = utilities.get_font(font_size)
        render = font.render(text, True, (255, 255, 255))
        
        if help_image:
            full_size = self.help_rect.width + self.bg_rect.width - 4, self.help_rect.height
            surface = pygame.Surface(full_size)
            surface.fill(bg_image.get_at(bg_image.get_rect().center))
            surface.blit(bg_image, bg_image.get_rect(left=self.bg_image.get_rect().left))
            surface.blit(help_image, help_image.get_rect(left=surface.get_rect().left + self.bg_rect.width - 4))
        else:
            full_size = bg_image.get_rect().size
            surface = bg_image.copy()
        surface.blit(render, render.get_rect(center=self.bg_rect.center))
        return full_size, surface
        
    def add_subitem(self, item):
        """
        Append a subitem to the item list
        """
        self.subitems_list.append(item)
    
    def on_mouse_click(self):
        """
        Handle mouse click
        """
        if len(self.subitems_list) > 0:
            self.menu.show_items(self.subitems_list)
        else:
            if self.action_id != None:
                self.menu.send_action(self.action_id)
            else:
                self.menu.send_action(CLOSE_MENU) # if the item have not children and have not an action_id, close the menu