Mercurial > rinkhals
annotate gamelib/misc.py @ 602:1b77bcbfb9e3 default tip
update tags
author | convert-repo |
---|---|
date | Thu, 19 May 2011 15:58:03 +0000 |
parents | 95c71a13468c |
children |
rev | line source |
---|---|
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
1 # Holder for misc useful classes |
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
2 |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
3 import random |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
4 |
336 | 5 from pygame.locals import KEYDOWN, K_ESCAPE |
6 from pgu import gui | |
534
d16ed2a8a33e
Make price and control reference dialogs a little shinier.
Simon Cross <hodgestar@gmail.com>
parents:
519
diff
changeset
|
7 from pgu import html |
431
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
8 from pgu.algo import getline |
336 | 9 |
427
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
10 import serializer |
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
11 |
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
12 class Position(serializer.Simplifiable): |
456
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
13 """2D/3D position / vector. Assumed immutable.""" |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
14 |
427
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
15 SIMPLIFY = ['x', 'y', 'z'] |
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
16 |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
17 def __init__(self, x, y, z=0): |
100 | 18 self.x = x |
19 self.y = y | |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
20 self.z = z |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
21 |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
22 def to_tile_tuple(self): |
100 | 23 return self.x, self.y |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
24 |
456
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
25 def to_3d_tuple(self): |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
26 return self.x, self.y, self.z |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
27 |
100 | 28 def dist(self, b): |
29 """Gives the distance to another position""" | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
30 |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
31 return max(abs(self.x - b.x), abs(self.y - b.y), abs(self.z - b.z)) |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
32 |
100 | 33 def __sub__(self, b): |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
34 return Position(self.x - b.x, self.y - b.y, self.z - b.z) |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
35 |
100 | 36 def __add__(self, b): |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
37 return Position(self.x + b.x, self.y + b.y, self.z + b.z) |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
38 |
100 | 39 def left_of(self, b): |
40 return self.x < b.x | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
41 |
100 | 42 def right_of(self, b): |
43 return self.x > b.x | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
44 |
456
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
45 def __hash__(self): |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
46 return hash(self.to_3d_tuple()) |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
47 |
100 | 48 def __eq__(self, b): |
456
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
49 return self.to_3d_tuple() == b.to_3d_tuple() |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
50 |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
51 def __str__(self): |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
52 return "<Position: %s>" % (self.to_3d_tuple(),) |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
53 |
431
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
54 def intermediate_positions(self, b): |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
55 """Only operates in two dimensions.""" |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
56 if max(abs(self.x - b.x), abs(self.y - b.y)) <= 1: |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
57 # pgu gets this case wrong on occasion. |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
58 return [b] |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
59 start = self.to_tile_tuple() |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
60 end = b.to_tile_tuple() |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
61 points = getline(start, end) |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
62 points.remove(start) # exclude start_pos |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
63 if end not in points: |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
64 # Rounding errors in getline cause this |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
65 points.append(end) |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
66 return [Position(p[0], p[1]) for p in points] |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
67 |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
68 class WeightedSelection(object): |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
69 def __init__(self, weightings=None): |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
70 self.weightings = [] |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
71 self.total = 0 |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
72 if weightings: |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
73 for item, weight in weightings: |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
74 self.weightings.append((item, weight)) |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
75 self.total += weight |
456
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
76 |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
77 def choose(self): |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
78 roll = random.uniform(0, self.total) |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
79 for item, weight in self.weightings: |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
80 if roll < weight: |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
81 return item |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
82 roll -= weight |
336 | 83 |
84 class CheckDialog(gui.Dialog): | |
553
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
85 def __init__(self, sure_func, message, yes_choice, no_choice, |
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
86 maybe_choice, **params): |
548
27c09c58d89d
Remove gameboard and dialog event flow hacks.
Simon Cross <hodgestar@gmail.com>
parents:
534
diff
changeset
|
87 self._sure_func = sure_func |
347
35f09e0ccd16
Make 'Esc' cancel the 'Are You Sure?' dialog. Fix dialog title
Neil Muller <drnlmuller@gmail.com>
parents:
336
diff
changeset
|
88 title = gui.Label('Are You Sure?') |
336 | 89 tbl = gui.Table() |
90 tbl.tr() | |
553
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
91 tbl.td(gui.Label(message), colspan=3) |
336 | 92 tbl.tr() |
553
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
93 tbl.td(gui.Spacer(0, 15), colspan=3) |
336 | 94 tbl.tr() |
553
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
95 no_button = gui.Button(no_choice) |
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
96 no_button.connect(gui.CLICK, self.clicked, 0) |
336 | 97 tbl.td(no_button, align=-1) |
553
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
98 if maybe_choice: |
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
99 maybe_button = gui.Button(maybe_choice) |
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
100 maybe_button.connect(gui.CLICK, self.clicked, 2) |
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
101 tbl.td(maybe_button, align=1) |
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
102 else: |
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
103 tbl.td(gui.Spacer(0, 15)) |
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
104 yes_button = gui.Button(yes_choice) |
7963fc09fac2
Make checkdialog take strings to show as input. Add possiblity of 3rd button on dialog
Neil Muller <drnlmuller@gmail.com>
parents:
548
diff
changeset
|
105 yes_button.connect(gui.CLICK, self.clicked, 1) |
336 | 106 tbl.td(yes_button, align=1) |
107 gui.Dialog.__init__(self, title, tbl, **params) | |
108 | |
109 def clicked(self, val): | |
548
27c09c58d89d
Remove gameboard and dialog event flow hacks.
Simon Cross <hodgestar@gmail.com>
parents:
534
diff
changeset
|
110 self._sure_func(val) |
336 | 111 self.close() |
112 | |
113 def event(self, e): | |
114 if e.type == KEYDOWN and e.key == K_ESCAPE: | |
347
35f09e0ccd16
Make 'Esc' cancel the 'Are You Sure?' dialog. Fix dialog title
Neil Muller <drnlmuller@gmail.com>
parents:
336
diff
changeset
|
115 self.clicked(False) |
336 | 116 return True |
117 return gui.Dialog.event(self, e) | |
118 | |
119 | |
575
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
120 class WarnDialog(gui.Dialog): |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
121 def __init__(self, title, message, **params): |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
122 title = gui.Label(title) |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
123 |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
124 body = gui.Table() |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
125 body.tr() |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
126 body.td(gui.Label(message), colspan=3) |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
127 body.tr() |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
128 body.td(gui.Spacer(0, 15), colspan=3) |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
129 |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
130 ok_button = gui.Button("Ok") |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
131 ok_button.connect(gui.CLICK, self.clicked) |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
132 |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
133 body.tr() |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
134 body.td(gui.Spacer(0, 0), colspan=2) |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
135 body.td(ok_button, align=-1) |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
136 |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
137 gui.Dialog.__init__(self, title, body, **params) |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
138 |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
139 def clicked(self): |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
140 self.close() |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
141 |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
142 def event(self, e): |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
143 if e.type == KEYDOWN and e.key == K_ESCAPE: |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
144 self.clicked() |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
145 return True |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
146 return gui.Dialog.event(self, e) |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
147 |
95c71a13468c
Convert failed save/restore print messages to warning dialogs.
Simon Cross <hodgestar@gmail.com>
parents:
553
diff
changeset
|
148 |
519
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
149 # Utility layout functions |
336 | 150 |
534
d16ed2a8a33e
Make price and control reference dialogs a little shinier.
Simon Cross <hodgestar@gmail.com>
parents:
519
diff
changeset
|
151 def make_box(text, markup=False): |
519
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
152 style = { |
534
d16ed2a8a33e
Make price and control reference dialogs a little shinier.
Simon Cross <hodgestar@gmail.com>
parents:
519
diff
changeset
|
153 'border' : 1, |
d16ed2a8a33e
Make price and control reference dialogs a little shinier.
Simon Cross <hodgestar@gmail.com>
parents:
519
diff
changeset
|
154 'padding_left': 2, |
d16ed2a8a33e
Make price and control reference dialogs a little shinier.
Simon Cross <hodgestar@gmail.com>
parents:
519
diff
changeset
|
155 'padding_right': 2, |
d16ed2a8a33e
Make price and control reference dialogs a little shinier.
Simon Cross <hodgestar@gmail.com>
parents:
519
diff
changeset
|
156 'padding_top': 2, |
d16ed2a8a33e
Make price and control reference dialogs a little shinier.
Simon Cross <hodgestar@gmail.com>
parents:
519
diff
changeset
|
157 'padding_bottom': 2, |
d16ed2a8a33e
Make price and control reference dialogs a little shinier.
Simon Cross <hodgestar@gmail.com>
parents:
519
diff
changeset
|
158 } |
d16ed2a8a33e
Make price and control reference dialogs a little shinier.
Simon Cross <hodgestar@gmail.com>
parents:
519
diff
changeset
|
159 if markup: |
d16ed2a8a33e
Make price and control reference dialogs a little shinier.
Simon Cross <hodgestar@gmail.com>
parents:
519
diff
changeset
|
160 word = html.HTML(text, style=style) |
d16ed2a8a33e
Make price and control reference dialogs a little shinier.
Simon Cross <hodgestar@gmail.com>
parents:
519
diff
changeset
|
161 else: |
d16ed2a8a33e
Make price and control reference dialogs a little shinier.
Simon Cross <hodgestar@gmail.com>
parents:
519
diff
changeset
|
162 word = gui.Label(text, style=style) |
519
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
163 return word |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
164 |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
165 def fix_widths(doc): |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
166 """Loop through all the widgets in the doc, and set the |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
167 width of the labels to max + 10""" |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
168 # We need to do this because of possible font issues |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
169 max_width = 0 |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
170 for thing in doc.widgets: |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
171 if hasattr(thing, 'style'): |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
172 # A label |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
173 if thing.style.width > max_width: |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
174 max_width = thing.style.width |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
175 for thing in doc.widgets: |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
176 if hasattr(thing, 'style'): |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
177 thing.style.width = max_width + 10 |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
178 |