comparison gamelib/gamescreen.py @ 114:13d8cb1d5962

Better cursor handling using draw_all() and mouse_delta().
author Jeremy Thurgood <firxen@gmail.com>
date Tue, 24 Aug 2010 14:51:22 +0200
parents ab11689aec36
children d5f7cccfdb6c
comparison
equal deleted inserted replaced
113:1a55d10c0469 114:13d8cb1d5962
18 from popupmenu import PopupMenu, PopupMenuButton 18 from popupmenu import PopupMenu, PopupMenuButton
19 from state import initial_state, Item 19 from state import initial_state, Item
20 from widgets import BoomLabel 20 from widgets import BoomLabel
21 21
22 22
23 class InventoryView(PaletteView, CursorWidget): 23 class InventoryView(PaletteView):
24 24
25 sel_color = Color("yellow") 25 sel_color = Color("yellow")
26 sel_width = 2 26 sel_width = 2
27 27
28 def __init__(self, state, handbutton): 28 def __init__(self, state, handbutton):
29 CursorWidget.__init__(self)
30 PaletteView.__init__(self, (BUTTON_SIZE, BUTTON_SIZE), 1, 6, scrolling=True) 29 PaletteView.__init__(self, (BUTTON_SIZE, BUTTON_SIZE), 1, 6, scrolling=True)
31 self.state = state 30 self.state = state
32 self.handbutton = handbutton 31 self.handbutton = handbutton
33 32
34 def num_items(self): 33 def num_items(self):
35 return len(self.state.inventory) 34 return len(self.state.inventory)
36 35
37 def draw_item(self, surface, item_no, rect): 36 def draw_item(self, surface, item_no, rect):
38 item_image = self.state.inventory[item_no].get_inventory_image() 37 item_image = self.state.inventory[item_no].get_inventory_image()
39 surface.blit(item_image, rect, None, BLEND_ADD) 38 surface.blit(item_image, rect, None, BLEND_ADD)
40
41 def draw_over(self, surface):
42 PaletteView.draw_over(self, surface)
43 CursorWidget.draw_over(self, surface)
44 39
45 def click_item(self, item_no, event): 40 def click_item(self, item_no, event):
46 self.state.set_tool(self.state.inventory[item_no]) 41 self.state.set_tool(self.state.inventory[item_no])
47 self.handbutton.unselect() 42 self.handbutton.unselect()
48 43
64 self.border_width = 1 59 self.border_width = 1
65 self.border_color = (0, 0, 0) 60 self.border_color = (0, 0, 0)
66 self.bg_color = (127, 127, 127) 61 self.bg_color = (127, 127, 127)
67 self.fg_color = (0, 0, 0) 62 self.fg_color = (0, 0, 0)
68 63
69 def draw_over(self, surface):
70 CursorWidget.draw_over(self, surface)
71
72 def mouse_down(self, event): 64 def mouse_down(self, event):
73 self.dismiss() 65 self.dismiss()
74 66
75 67
76 class StateWidget(CursorWidget): 68 class StateWidget(Widget):
77 69
78 def __init__(self, state): 70 def __init__(self, state):
79 CursorWidget.__init__(self, Rect(0, 0, SCENE_SIZE[0], SCENE_SIZE[1])) 71 Widget.__init__(self, Rect(0, 0, SCENE_SIZE[0], SCENE_SIZE[1]))
80 self.state = state 72 self.state = state
81 73
82 def draw(self, surface): 74 def draw(self, surface):
83 self.state.draw(surface) 75 self.state.draw(surface)
84 76
99 self.invalidate() 91 self.invalidate()
100 92
101 def mouse_move(self, event): 93 def mouse_move(self, event):
102 if not self.subwidgets: 94 if not self.subwidgets:
103 self.state.mouse_move(event.pos) 95 self.state.mouse_move(event.pos)
104 CursorWidget.mouse_move(self, event)
105 96
106 97
107 class DetailWindow(CursorWidget): 98 class DetailWindow(Widget):
108 def __init__(self, state): 99 def __init__(self, state):
109 CursorWidget.__init__(self, Rect(0, 0, SCENE_SIZE[0], SCENE_SIZE[1])) 100 Widget.__init__(self, Rect(0, 0, SCENE_SIZE[0], SCENE_SIZE[1]))
110 self.state = state 101 self.state = state
111 self.draw_area = Rect(0, 0, 300, 300) 102 self.draw_area = Rect(0, 0, 300, 300)
112 self.draw_area.center = self.center 103 self.draw_area.center = self.center
113 104
114 def draw(self, surface): 105 def draw(self, surface):
123 114
124 def mouse_move(self, event): 115 def mouse_move(self, event):
125 if self.draw_area.collidepoint(event.pos): 116 if self.draw_area.collidepoint(event.pos):
126 # TODO: mouse_move stuff 117 # TODO: mouse_move stuff
127 pass 118 pass
128 CursorWidget.mouse_move(self, event)
129 119
130 120
131 class ToolBar(Row, CursorWidget): 121 class ToolBar(Row):
132 def __init__(self, items): 122 def __init__(self, items):
133 CursorWidget.__init__(self)
134 Row.__init__(self, items, spacing=0, width=SCREEN[0]) 123 Row.__init__(self, items, spacing=0, width=SCREEN[0])
135 124
136 125
137 class GameScreen(Screen): 126 class GameScreen(Screen, CursorWidget):
138 def __init__(self, shell): 127 def __init__(self, shell):
128 CursorWidget.__init__(self)
139 Screen.__init__(self, shell) 129 Screen.__init__(self, shell)
140 self.running = False 130 self.running = False
141 131
142 def _clear_all(self): 132 def _clear_all(self):
143 for widget in self.subwidgets[:]: 133 for widget in self.subwidgets[:]:
198 self.inventory.unselect() 188 self.inventory.unselect()
199 189
200 def begin_frame(self): 190 def begin_frame(self):
201 if self.running: 191 if self.running:
202 self.state_widget.animate() 192 self.state_widget.animate()
203
204 def mouse_delta(self, event):
205 w = self.shell.find_widget(event.pos)
206 if not isinstance(w, CursorWidget):
207 mouse.set_visible(1)