comparison gamelib/gameboard.py @ 168:1014bfcddc4c

Fixed label alignment.
author Jeremy Thurgood <firxen@gmail.com>
date Thu, 03 Sep 2009 22:42:22 +0000
parents 4aa800354b7c
children 946f09ed37cd
comparison
equal deleted inserted replaced
167:1d4d2202ef9f 168:1014bfcddc4c
13 import equipment 13 import equipment
14 import sound 14 import sound
15 import cursors 15 import cursors
16 16
17 class OpaqueLabel(gui.Label): 17 class OpaqueLabel(gui.Label):
18 def __init__(self, value, **params):
19 gui.Label.__init__(self, value, **params)
20 if 'width' in params:
21 self._width = params['width']
22 if 'height' in params:
23 self._height = params['height']
24 self._set_size()
25
26 def _set_size(self):
27 width, height = self.font.size(self.value)
28 width = getattr(self, '_width', width)
29 height = getattr(self, '_height', height)
30 self.style.width, self.style.height = width, height
31
18 def paint(self, s): 32 def paint(self, s):
19 s.fill(self.style.background) 33 s.fill(self.style.background)
34 if self.style.align > 0:
35 r = s.get_rect()
36 w, _ = self.font.size(self.value)
37 s = s.subsurface(r.move((r.w-w, 0)).clip(r))
20 gui.Label.paint(self, s) 38 gui.Label.paint(self, s)
21 39
22 def update_value(self, value): 40 def update_value(self, value):
23 self.value = value 41 self.value = value
24 self.style.width, self.style.height = self.font.size(self.value) 42 self._set_size()
25 self.repaint() 43 self.repaint()
26 44
27 def mklabel(text=" ", color=constants.FG_COLOR): 45 def mklabel(text="", **params):
28 return OpaqueLabel(text, color=color) 46 params.setdefault('color', constants.FG_COLOR)
47 params.setdefault('width', GameBoard.TOOLBAR_WIDTH/2)
48 return OpaqueLabel(text, **params)
29 49
30 def mkcountupdate(counter): 50 def mkcountupdate(counter):
31 def update_counter(self, value): 51 def update_counter(self, value):
32 getattr(self, counter).update_value("%5s" % value) 52 getattr(self, counter).update_value("%s " % value)
33 self.repaint() 53 self.repaint()
34 return update_counter 54 return update_counter
35 55
36 class ToolBar(gui.Table): 56 class ToolBar(gui.Table):
37 def __init__(self, gameboard, **params): 57 def __init__(self, gameboard, **params):
38 gui.Table.__init__(self, **params) 58 gui.Table.__init__(self, **params)
39 self.gameboard = gameboard 59 self.gameboard = gameboard
40 self.cash_counter = mklabel() 60 self.cash_counter = mklabel(align=1)
41 self.chicken_counter = mklabel() 61 self.chicken_counter = mklabel(align=1)
42 self.egg_counter = mklabel() 62 self.egg_counter = mklabel(align=1)
43 self.day_counter = mklabel() 63 self.day_counter = mklabel(align=1)
44 self.killed_foxes = mklabel() 64 self.killed_foxes = mklabel(align=1)
45 self.rifle_counter = mklabel() 65
46 66 self.tr()
67 self.td(gui.Spacer(self.rect.w/2, 0))
68 self.td(gui.Spacer(self.rect.w/2, 0))
47 self.add_counter(mklabel("Day:"), self.day_counter) 69 self.add_counter(mklabel("Day:"), self.day_counter)
48 self.add_counter(mklabel("Groats:"), self.cash_counter) 70 self.add_counter(mklabel("Groats:"), self.cash_counter)
49 self.add_counter(mklabel("Eggs:"), self.egg_counter) 71 self.add_counter(mklabel("Eggs:"), self.egg_counter)
50 self.add_counter(icons.CHKN_ICON, self.chicken_counter) 72 self.add_counter(icons.CHKN_ICON, self.chicken_counter)
51 self.add_counter(icons.KILLED_FOX, self.killed_foxes) 73 self.add_counter(icons.KILLED_FOX, self.killed_foxes)
72 self.add_tool_button(building_cls.NAME.title(), building_cls, 94 self.add_tool_button(building_cls.NAME.title(), building_cls,
73 cursors.cursors.get('build', None)) 95 cursors.cursors.get('build', None))
74 for equipment_cls in equipment.EQUIPMENT: 96 for equipment_cls in equipment.EQUIPMENT:
75 self.add_tool_button(equipment_cls.NAME.title(), equipment_cls, 97 self.add_tool_button(equipment_cls.NAME.title(), equipment_cls,
76 cursors.cursors.get(equipment_cls.NAME, None)) 98 cursors.cursors.get(equipment_cls.NAME, None))
77 self.add_spacer() 99 self.add_spacer(30)
78 100
79 self.add_button("Finished Day", self.day_done) 101 self.add_button("Finished Day", self.day_done)
80 102
81 def day_done(self): 103 def day_done(self):
82 import engine 104 import engine
87 update_fox_counter = mkcountupdate('killed_foxes') 109 update_fox_counter = mkcountupdate('killed_foxes')
88 update_chicken_counter = mkcountupdate('chicken_counter') 110 update_chicken_counter = mkcountupdate('chicken_counter')
89 update_egg_counter = mkcountupdate('egg_counter') 111 update_egg_counter = mkcountupdate('egg_counter')
90 update_day_counter = mkcountupdate('day_counter') 112 update_day_counter = mkcountupdate('day_counter')
91 113
92 def add_spacer(self, height=30): 114 def add_spacer(self, height):
93 self.tr() 115 self.tr()
94 self.td(gui.Spacer(0, height), colspan=2) 116 self.td(gui.Spacer(0, height), colspan=2)
95 117
96 def add_heading(self, text): 118 def add_heading(self, text):
97 self.tr() 119 self.tr()
107 self.tr() 129 self.tr()
108 self.td(button, align=-1, colspan=2) 130 self.td(button, align=-1, colspan=2)
109 131
110 def add_counter(self, icon, label): 132 def add_counter(self, icon, label):
111 self.tr() 133 self.tr()
112 self.td(icon, align=-1, width=self.rect.w/2) 134 self.td(icon, width=self.rect.w/2)
113 self.td(label, align=-1, width=self.rect.w/2) 135 self.td(label, width=self.rect.w/2)
114 136
115 def resize(self, width=None, height=None): 137 def resize(self, width=None, height=None):
116 width, height = gui.Table.resize(self, width, height) 138 width, height = gui.Table.resize(self, width, height)
117 width = GameBoard.TOOLBAR_WIDTH 139 width = GameBoard.TOOLBAR_WIDTH
118 return width, height 140 return width, height