Mercurial > rinkhals
comparison gamelib/gameboard.py @ 151:082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Thu, 03 Sep 2009 20:32:56 +0000 |
parents | 225be1220053 |
children | 702bc0eb2ac3 |
comparison
equal
deleted
inserted
replaced
150:89d2360d4350 | 151:082868bea873 |
---|---|
46 self.add_counter(mklabel("Day:"), self.day_counter) | 46 self.add_counter(mklabel("Day:"), self.day_counter) |
47 self.add_counter(mklabel("Groats:"), self.cash_counter) | 47 self.add_counter(mklabel("Groats:"), self.cash_counter) |
48 self.add_counter(mklabel("Eggs:"), self.egg_counter) | 48 self.add_counter(mklabel("Eggs:"), self.egg_counter) |
49 self.add_counter(icons.CHKN_ICON, self.chicken_counter) | 49 self.add_counter(icons.CHKN_ICON, self.chicken_counter) |
50 self.add_counter(icons.KILLED_FOX, self.killed_foxes) | 50 self.add_counter(icons.KILLED_FOX, self.killed_foxes) |
51 self.add_spacer() | |
51 | 52 |
52 self.add_tool_button("Move Animals", constants.TOOL_PLACE_ANIMALS) | 53 self.add_tool_button("Move Animals", constants.TOOL_PLACE_ANIMALS) |
53 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN) | 54 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN) |
54 self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG) | 55 self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG) |
55 self.add_tool_button("Sell building", constants.TOOL_SELL_BUILDING) | 56 self.add_tool_button("Sell building", constants.TOOL_SELL_BUILDING) |
58 for building_cls in buildings.BUILDINGS: | 59 for building_cls in buildings.BUILDINGS: |
59 self.add_tool_button("Buy %s" % (building_cls.NAME,), building_cls) | 60 self.add_tool_button("Buy %s" % (building_cls.NAME,), building_cls) |
60 for equipment_cls in equipment.EQUIPMENT: | 61 for equipment_cls in equipment.EQUIPMENT: |
61 self.add_tool_button("Buy %s" % (equipment_cls.NAME,), equipment_cls) | 62 self.add_tool_button("Buy %s" % (equipment_cls.NAME,), equipment_cls) |
62 self.add_spacer() | 63 self.add_spacer() |
64 | |
63 self.add_button("Finished Day", self.day_done) | 65 self.add_button("Finished Day", self.day_done) |
64 | 66 |
65 def day_done(self): | 67 def day_done(self): |
66 import engine | 68 import engine |
67 pygame.event.post(engine.START_NIGHT) | 69 pygame.event.post(engine.START_NIGHT) |
72 update_egg_counter = mkcountupdate('egg_counter') | 74 update_egg_counter = mkcountupdate('egg_counter') |
73 update_day_counter = mkcountupdate('day_counter') | 75 update_day_counter = mkcountupdate('day_counter') |
74 | 76 |
75 def add_spacer(self, height=30): | 77 def add_spacer(self, height=30): |
76 self.tr() | 78 self.tr() |
77 self.add(gui.Spacer(0, height)) | 79 self.td(gui.Spacer(0, height), colspan=2) |
78 | 80 |
79 def add_tool_button(self, text, tool): | 81 def add_tool_button(self, text, tool): |
80 self.add_button(text, lambda: self.gameboard.set_selected_tool(tool)) | 82 self.add_button(text, lambda: self.gameboard.set_selected_tool(tool)) |
81 | 83 |
82 def add_button(self, text, func): | 84 def add_button(self, text, func): |
83 button = gui.Button(text) | 85 button = gui.Button(text, width=self.rect.w) |
84 button.connect(gui.CLICK, func) | 86 button.connect(gui.CLICK, func) |
85 self.tr() | 87 self.tr() |
86 self.add(button, colspan=2) | 88 self.td(button, align=-1, colspan=2) |
87 | 89 |
88 def add_counter(self, icon, label): | 90 def add_counter(self, icon, label): |
89 self.tr() | 91 self.tr() |
90 if icon: | 92 self.td(icon, align=-1, width=self.rect.w/2) |
91 self.td(icon, align=-1, width=self.rect.w/2) | 93 self.td(label, align=-1, width=self.rect.w/2) |
92 self.add(label) | |
93 | 94 |
94 class VidWidget(gui.Widget): | 95 class VidWidget(gui.Widget): |
95 def __init__(self, gameboard, vid, **params): | 96 def __init__(self, gameboard, vid, **params): |
96 gui.Widget.__init__(self, **params) | 97 gui.Widget.__init__(self, **params) |
97 self.gameboard = gameboard | 98 self.gameboard = gameboard |
98 vid.bounds = pygame.Rect((0, 0), vid.tile_to_view(vid.size)) | |
99 self.vid = vid | 99 self.vid = vid |
100 self.width = params.get('width', 0) | 100 self.vid.bounds = pygame.Rect((0, 0), vid.tile_to_view(vid.size)) |
101 self.height = params.get('height', 0) | |
102 | 101 |
103 def paint(self, surface): | 102 def paint(self, surface): |
104 self.vid.paint(surface) | 103 self.vid.paint(surface) |
105 | 104 |
106 def update(self, surface): | 105 def update(self, surface): |
107 return self.vid.update(surface) | 106 return self.vid.update(surface) |
108 | |
109 def resize(self, width=0, height=0): | |
110 if width is not None: | |
111 self.width = width | |
112 if height is not None: | |
113 self.height = height | |
114 return self.width, self.height | |
115 | 107 |
116 def move_view(self, x, y): | 108 def move_view(self, x, y): |
117 self.vid.view.move_ip((x, y)) | 109 self.vid.view.move_ip((x, y)) |
118 | 110 |
119 def event(self, e): | 111 def event(self, e): |
128 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland'] | 120 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland'] |
129 FENCE = tiles.REVERSE_TILE_MAP['fence'] | 121 FENCE = tiles.REVERSE_TILE_MAP['fence'] |
130 WOODLAND = tiles.REVERSE_TILE_MAP['woodland'] | 122 WOODLAND = tiles.REVERSE_TILE_MAP['woodland'] |
131 BROKEN_FENCE = tiles.REVERSE_TILE_MAP['broken fence'] | 123 BROKEN_FENCE = tiles.REVERSE_TILE_MAP['broken fence'] |
132 | 124 |
133 def __init__(self): | 125 def __init__(self, main_app): |
126 self.disp = main_app | |
134 self.tv = tiles.FarmVid() | 127 self.tv = tiles.FarmVid() |
135 self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS) | 128 self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS) |
136 self.tv.png_folder_load_tiles(data.filepath('tiles')) | 129 self.tv.png_folder_load_tiles(data.filepath('tiles')) |
137 self.tv.tga_load_level(data.filepath('level1.tga')) | 130 self.tv.tga_load_level(data.filepath('level1.tga')) |
138 self.create_disp() | 131 self.create_display() |
139 | 132 |
140 self.selected_tool = None | 133 self.selected_tool = None |
141 self.animal_to_place = None | 134 self.animal_to_place = None |
142 self.chickens = set() | 135 self.chickens = set() |
143 self.foxes = set() | 136 self.foxes = set() |
150 | 143 |
151 self.fix_buildings() | 144 self.fix_buildings() |
152 | 145 |
153 self.add_some_chickens() | 146 self.add_some_chickens() |
154 | 147 |
155 def create_disp(self): | 148 def get_top_widget(self): |
156 width, height = pygame.display.get_surface().get_size() | 149 return self.top_widget |
150 | |
151 def create_display(self): | |
152 width, height = self.disp.rect.w, self.disp.rect.h | |
157 tbl = gui.Table() | 153 tbl = gui.Table() |
158 tbl.tr() | 154 tbl.tr() |
159 self.toolbar = ToolBar(self) | 155 self.toolbar = ToolBar(self, width=self.TOOLBAR_WIDTH) |
160 tbl.td(self.toolbar, width=self.TOOLBAR_WIDTH) | 156 tbl.td(self.toolbar, valign=-1) |
161 self.tvw = VidWidget(self, self.tv, width=width-self.TOOLBAR_WIDTH, height=height) | 157 self.tvw = VidWidget(self, self.tv, width=width-self.TOOLBAR_WIDTH-19, height=height) |
162 tbl.td(self.tvw) | 158 tbl.td(self.tvw) |
163 self.disp = gui.App() | 159 self.top_widget = tbl |
164 self.disp.init(tbl) | 160 |
165 | 161 def update(self): |
166 def paint(self, screen): | |
167 screen.fill(constants.BG_COLOR) | |
168 self.disp.paint(screen) | |
169 | |
170 def update(self, screen): | |
171 self.tvw.reupdate() | 162 self.tvw.reupdate() |
172 return self.disp.update(screen) | |
173 | 163 |
174 def loop(self): | 164 def loop(self): |
175 self.tv.loop() | 165 self.tv.loop() |
176 | 166 |
177 def set_selected_tool(self, tool): | 167 def set_selected_tool(self, tool): |