Mercurial > pyntnclick
comparison pyntnclick/gamescreen.py @ 672:55d5d384fc16 pyntnclick
Widget-based inventory view.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sun, 12 Feb 2012 18:39:16 +0200 |
parents | b27b5c6c54e8 |
children | f67bc162d69e |
comparison
equal
deleted
inserted
replaced
671:b27b5c6c54e8 | 672:55d5d384fc16 |
---|---|
1 # gamescreen.py | 1 # gamescreen.py |
2 # Copyright Boomslang team, 2010 (see COPYING File) | 2 # Copyright Boomslang team, 2010 (see COPYING File) |
3 # Main menu for the game | 3 # Main menu for the game |
4 | 4 |
5 import pygame.draw | |
5 from pygame import Rect, Surface | 6 from pygame import Rect, Surface |
6 from pygame.color import Color | 7 from pygame.color import Color |
7 from pygame.locals import MOUSEBUTTONDOWN, MOUSEMOTION, KEYDOWN, K_ESCAPE | 8 from pygame.locals import MOUSEBUTTONDOWN, MOUSEMOTION, KEYDOWN, K_ESCAPE |
8 | 9 |
9 from pyntnclick.cursor import CursorScreen | 10 from pyntnclick.cursor import CursorScreen |
10 from pyntnclick.engine import Screen | 11 from pyntnclick.engine import Screen |
11 from pyntnclick.state import handle_result | 12 from pyntnclick.state import handle_result |
12 from pyntnclick.widgets.base import Widget, Container | 13 from pyntnclick.widgets.base import Container |
13 from pyntnclick.widgets.text import TextButton | 14 from pyntnclick.widgets.text import TextButton |
14 from pyntnclick.widgets.imagebutton import ImageButtonWidget | 15 from pyntnclick.widgets.imagebutton import ImageButtonWidget |
15 | 16 |
16 # XXX: Need a way to get at the constants. | 17 # XXX: Need a way to get at the constants. |
17 from pyntnclick.constants import GameConstants | 18 from pyntnclick.constants import GameConstants |
18 constants = GameConstants() | 19 constants = GameConstants() |
19 SCREEN = constants.screen | 20 SCREEN = constants.screen |
20 LEAVE = constants.leave | 21 LEAVE = constants.leave |
21 | 22 |
22 | 23 |
23 class InventoryView(Widget): | 24 class InventorySlot(ImageButtonWidget): |
25 SELECTED_COLOR = Color("yellow") | |
26 SELECTED_WIDTH = 2 | |
27 | |
28 def __init__(self, rect, gd, game, state_widget): | |
29 self.item = None | |
30 super(InventorySlot, self).__init__(rect, gd, None) | |
31 self.game = game | |
32 self.state_widget = state_widget | |
33 self.add_callback(MOUSEBUTTONDOWN, self.mouse_down) | |
34 | |
35 def set_item(self, item): | |
36 self.item = item | |
37 | |
38 def draw(self, surface): | |
39 if self.item: | |
40 surface.blit(self.item.get_inventory_image(), self.rect) | |
41 if self.selected: | |
42 pygame.draw.rect(surface, self.SELECTED_COLOR, | |
43 self.rect, self.SELECTED_WIDTH) | |
44 | |
45 @property | |
46 def selected(self): | |
47 return self.game.tool is self.item | |
48 | |
49 def mouse_down(self, event, widget): | |
50 if event.button != 1: | |
51 return | |
52 if self.selected: | |
53 self.game.set_tool(None) | |
54 elif self.item.is_interactive(self.game.tool): | |
55 result = self.item.interact(self.game.tool) | |
56 handle_result(result, self.state_widget) | |
57 else: | |
58 self.game.set_tool(self.item) | |
59 | |
60 | |
61 class InventoryView(Container): | |
24 MIN_UPDOWN_WIDTH = 16 | 62 MIN_UPDOWN_WIDTH = 16 |
25 | |
26 sel_color = Color("yellow") | |
27 sel_width = 2 | |
28 | 63 |
29 def __init__(self, rect, gd, screen): | 64 def __init__(self, rect, gd, screen): |
30 self.bsize = gd.constants.button_size | 65 self.bsize = gd.constants.button_size |
31 super(InventoryView, self).__init__(rect, gd) | 66 super(InventoryView, self).__init__(rect, gd) |
32 self.screen = screen | 67 self.screen = screen |
33 self.game = screen.game | 68 self.game = screen.game |
34 self.state_widget = screen.state_widget | 69 self.state_widget = screen.state_widget |
35 | 70 |
36 self.inv_slots = (self.rect.width - self.MIN_UPDOWN_WIDTH) / self.bsize | 71 slots = (self.rect.width - self.MIN_UPDOWN_WIDTH) / self.bsize |
37 self.updown_width = self.rect.width - self.inv_slots * self.bsize | 72 self.slots = [self.add(self.make_slot(i)) for i in range(slots)] |
73 self.updown_width = self.rect.width - slots * self.bsize | |
38 self.inv_offset = 0 | 74 self.inv_offset = 0 |
39 | 75 |
40 self.add_callback(MOUSEBUTTONDOWN, self.mouse_down) | 76 self.add_callback(MOUSEBUTTONDOWN, self.mouse_down) |
41 self.update_surface() | 77 self.update_slots() |
42 | 78 |
43 def update_surface(self): | 79 def make_slot(self, slot): |
44 self.surface = Surface(self.rect.size) | 80 rect = Rect((self.rect.left + slot * self.bsize, self.rect.top), |
45 for slot in range(self.inv_slots): | 81 (self.bsize, self.rect.height)) |
46 self.draw_slot_item( | 82 return InventorySlot(rect, self.gd, self.game, self.state_widget) |
47 self.surface, slot) | 83 |
48 self.draw_updown(self.surface) | 84 def update_slots(self): |
49 | 85 items = (self.slot_items + [None] * len(self.slots))[:len(self.slots)] |
50 def draw(self, surface): | 86 for item, slot in zip(items, self.slots): |
51 self.update_surface() | 87 slot.set_item(item) |
52 surface.blit(self.surface, self.rect) | 88 |
89 def draw(self, surface): | |
90 self.update_slots() | |
91 super(InventoryView, self).draw(surface) | |
53 | 92 |
54 @property | 93 @property |
55 def slot_items(self): | 94 def slot_items(self): |
56 return self.game.inventory[self.inv_offset:][:self.inv_slots] | 95 return self.game.inventory[self.inv_offset:][:len(self.slots)] |
57 | |
58 def draw_slot_item(self, surface, slot): | |
59 if slot >= len(self.slot_items): | |
60 return | |
61 item = self.slot_items[slot] | |
62 rect = Rect((slot * self.bsize, 0), (self.bsize, self.bsize)) | |
63 surface.blit(item.get_inventory_image(), rect, None) | |
64 | 96 |
65 def draw_updown(self, surface): | 97 def draw_updown(self, surface): |
66 rect = Rect((self.rect.width - self.updown_width, 0), | 98 rect = Rect((self.rect.width - self.updown_width, 0), |
67 (self.updown_width, self.rect.height)) | 99 (self.updown_width, self.rect.height)) |
68 s = Surface(rect.size) | 100 s = Surface(rect.size) |
69 s.fill(Color("blue")) | 101 s.fill(Color("blue")) |
70 surface.blit(s, rect) | 102 surface.blit(s, rect) |
71 | 103 |
72 def click_slot(self, slot, event): | |
73 if slot >= len(self.slot_items): | |
74 return | |
75 item = self.slot_items[slot] | |
76 if self.item_is_selected(item): | |
77 self.unselect() | |
78 elif item.is_interactive(self.game.tool): | |
79 result = item.interact(self.game.tool) | |
80 handle_result(result, self.state_widget) | |
81 else: | |
82 self.game.set_tool(item) | |
83 | |
84 def mouse_down(self, event, widget): | 104 def mouse_down(self, event, widget): |
85 if event.button != 1: | 105 if event.button != 1: |
86 return self.game.cancel_doodah(self.screen) | 106 self.game.cancel_doodah(self.screen) |
87 x, y = self.global_to_local(event.pos) | |
88 slot = x / self.bsize | |
89 self.click_slot(slot, event) | |
90 | |
91 def item_is_selected(self, item): | |
92 return self.game.tool is item | |
93 | |
94 def unselect(self): | |
95 self.game.set_tool(None) | |
96 | 107 |
97 | 108 |
98 class StateWidget(Container): | 109 class StateWidget(Container): |
99 | 110 |
100 def __init__(self, rect, gd, screen): | 111 def __init__(self, rect, gd, screen): |