comparison gamelib/gameboard.py @ 36:5569430fd82e

Display cleanup and rationalisation.
author Jeremy Thurgood <firxen@gmail.com>
date Mon, 31 Aug 2009 12:55:00 +0000
parents 8f6c6a54a099
children 497b53b69280
comparison
equal deleted inserted replaced
35:8f6c6a54a099 36:5569430fd82e
3 from pgu import gui 3 from pgu import gui
4 4
5 import data 5 import data
6 import tiles 6 import tiles
7 7
8 # FIXME: These should probably be proper events of some kind.
8 SELL_CHICKEN = None 9 SELL_CHICKEN = None
9 SELL_EGG = None 10 SELL_EGG = None
10 BUY_FENCE = 2 11 BUY_FENCE = 2
11 BUY_HENHOUSE = 3 12 BUY_HENHOUSE = 3
12 13
19 self.add_tool_button("Sell egg", SELL_EGG) 20 self.add_tool_button("Sell egg", SELL_EGG)
20 self.add_tool_button("Buy fence", BUY_FENCE) 21 self.add_tool_button("Buy fence", BUY_FENCE)
21 self.add_tool_button("Buy henhouse", BUY_HENHOUSE) 22 self.add_tool_button("Buy henhouse", BUY_HENHOUSE)
22 23
23 def add_tool_button(self, text, tool): 24 def add_tool_button(self, text, tool):
24 style = {
25 "padding_bottom": 15,
26 }
27 td_kwargs = {
28 "style": style,
29 "width": self.gameboard.TOOLBAR_WIDTH,
30 }
31 button = gui.Button(text) 25 button = gui.Button(text)
32 button.connect(gui.CLICK, lambda: self.gameboard.set_selected_tool(tool)) 26 button.connect(gui.CLICK, lambda: self.gameboard.set_selected_tool(tool))
33 self.tr() 27 self.tr()
34 self.td(button, **td_kwargs) 28 self.add(button)
35 29
36 30
37 class VidWidget(gui.Widget): 31 class VidWidget(gui.Widget):
38 def __init__(self, gameboard, vid, **params): 32 def __init__(self, gameboard, vid, **params):
39 gui.Widget.__init__(self, **params) 33 gui.Widget.__init__(self, **params)
44 38
45 def paint(self, surface): 39 def paint(self, surface):
46 self.vid.paint(surface) 40 self.vid.paint(surface)
47 41
48 def update(self, surface): 42 def update(self, surface):
49 offset = surface.get_offset() 43 return self.vid.update(surface)
50 return [r.move(offset) for r in self.vid.update(surface)]
51 44
52 def resize(self, width=0, height=0): 45 def resize(self, width=0, height=0):
53 if width is not None: 46 if width is not None:
54 self.width = width 47 self.width = width
55 if height is not None: 48 if height is not None:
57 return self.width, self.height 50 return self.width, self.height
58 51
59 def event(self, e): 52 def event(self, e):
60 if e.type == MOUSEBUTTONDOWN: 53 if e.type == MOUSEBUTTONDOWN:
61 self.gameboard.use_tool(e) 54 self.gameboard.use_tool(e)
62
63
64 class DispTable(gui.Table):
65 def __init__(self, gameboard, **params):
66 gui.Table.__init__(self, **params)
67 self.gameboard = gameboard
68 self.tr()
69 self.td(self.gameboard.tools, width=self.gameboard.TOOLBAR_WIDTH)
70 self.td(self.gameboard.vidwidget)
71 55
72 56
73 class GameBoard(object): 57 class GameBoard(object):
74 TILE_DIMENSIONS = (20, 20) 58 TILE_DIMENSIONS = (20, 20)
75 TOOLBAR_WIDTH = 140 59 TOOLBAR_WIDTH = 140
84 self.foxes = [] 68 self.foxes = []
85 self.create_disp() 69 self.create_disp()
86 70
87 def create_disp(self): 71 def create_disp(self):
88 width, height = pygame.display.get_surface().get_size() 72 width, height = pygame.display.get_surface().get_size()
89 self.tools = ToolBar(self) 73 tbl = gui.Table()
90 self.vidwidget = VidWidget(self, self.tv, width=width-self.TOOLBAR_WIDTH, height=height) 74 tbl.tr()
75 tbl.td(ToolBar(self), width=self.TOOLBAR_WIDTH)
76 tbl.td(VidWidget(self, self.tv, width=width-self.TOOLBAR_WIDTH, height=height))
91 self.disp = gui.App() 77 self.disp = gui.App()
92 c = gui.Container(align=0, valign=0) 78 self.disp.init(tbl)
93 tbl = DispTable(self)
94 c.add(tbl, 0, 0)
95 self.disp.init(c)
96
97 def split_screen(self, screen):
98 leftbar_rect = screen.get_rect()
99 leftbar_rect.width = self.TOOLBAR_WIDTH
100 main_rect = screen.get_rect()
101 main_rect.width -= leftbar_rect.width
102 main_rect.left += leftbar_rect.width
103 return screen.subsurface(leftbar_rect), screen.subsurface(main_rect)
104 79
105 def paint(self, screen): 80 def paint(self, screen):
106 self.disp.paint(screen) 81 self.disp.paint(screen)
107
108 def update_subscreen(self, vid, subsurface):
109 offset = subsurface.get_offset()
110 return [r.move(offset) for r in vid.update(subsurface)]
111 82
112 def update(self, screen): 83 def update(self, screen):
113 return self.disp.update(screen) 84 return self.disp.update(screen)
114 85
115 def loop(self): 86 def loop(self):
119 self.selected_tool = tool 90 self.selected_tool = tool
120 91
121 def use_tool(self, e): 92 def use_tool(self, e):
122 if self.selected_tool is None: 93 if self.selected_tool is None:
123 return 94 return
124 # pos = self.tv.screen_to_tile((e.pos[0] - self.TOOLBAR_WIDTH, e.pos[1]))
125 pos = self.tv.screen_to_tile(e.pos) 95 pos = self.tv.screen_to_tile(e.pos)
126 self.tv.set(pos, self.selected_tool) 96 self.tv.set(pos, self.selected_tool)
127 97
128 def event(self, e): 98 def event(self, e):
129 self.disp.event(e) 99 self.disp.event(e)