Mercurial > sypikslang
annotate gamelib/gui_base.py @ 42:47c7e96cf9c8
Change initial mission creation logic to look for leaf classes
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Mon, 07 May 2012 14:00:18 +0200 |
parents | d82d3e54a4ef |
children | 86d83dcb7d42 |
rev | line source |
---|---|
37 | 1 import pygame |
39
d82d3e54a4ef
fixed more pep8 issues
Rizmari Versfeld <rizziepit@gmail.com>
parents:
38
diff
changeset
|
2 from pygame import Surface, Rect |
37 | 3 from pygame.font import Font |
4 | |
5 from gamelib import data | |
6 | |
7 | |
8 # different font sizes | |
9 pygame.font.init() | |
10 font_small = Font(data.filepath('fonts/DejaVuSans.ttf'), 10) | |
11 font_medium = Font(data.filepath('fonts/DejaVuSans.ttf'), 14) | |
12 font_large = Font(data.filepath('fonts/DejaVuSans.ttf'), 18) | |
13 | |
14 | |
15 class Drawable(object): | |
38 | 16 |
17 def __init__(self, rect): | |
18 if isinstance(rect, Rect): | |
19 self.rect = rect | |
20 else: | |
21 self.rect = Rect(rect[0], rect[1], rect[2], rect[3]) | |
22 | |
23 def draw(self, surface): | |
24 pass | |
25 | |
26 | |
37 | 27 class Clickable(object): |
38 | 28 |
29 def on_mouse_down(self, pos): | |
30 pass | |
31 | |
32 def on_mouse_up(self, pos): | |
33 pass | |
34 | |
35 def on_mouse_move(self, pos): | |
36 pass | |
37 | |
38 def on_mouse_cancel(self): | |
39 pass | |
40 | |
41 def on_click(self): | |
42 pass | |
43 | |
37 | 44 |
45 class ContainerView(Drawable): | |
38 | 46 |
47 def __init__(self): | |
48 self.children = [] | |
49 | |
50 def add_child(self, child): | |
51 self.children.append(child) | |
52 | |
53 def remove_child(self, child): | |
54 self.children.remove(child) | |
55 | |
56 def get_child_by_pos(self, pos): | |
57 for child in self.children: | |
58 if child.rect.collidepoint(pos): | |
59 if isinstance(child, ContainerView): | |
60 # calculates position relative to child | |
61 x = pos[0] - child.rect[0] | |
62 y = pos[1] - child.rect[1] | |
63 return child.get_child_by_pos((x, y)) | |
64 else: | |
65 return child | |
66 | |
67 def draw_children(self, surface): | |
68 for child in self.children: | |
69 child.draw(surface) | |
70 | |
71 | |
37 | 72 class Window(Clickable, ContainerView): |
38 | 73 |
74 def __init__(self, screen): | |
75 super(Window, self).__init__() | |
76 self.surface = Surface((screen.get_width(), screen.get_height())) | |
77 self.background_colour = None | |
78 self.background_image = None | |
79 self.pressed_child = None | |
80 | |
81 def on_mouse_down(self, pos): | |
82 child = self.get_child_by_pos(pos) | |
83 if isinstance(child, Clickable): | |
84 # calculates position relative to child | |
85 x = pos[0] - child.rect[0] | |
86 y = pos[1] - child.rect[1] | |
87 child.on_mouse_down((x, y)) | |
88 self.pressed_child = child | |
89 | |
90 def on_mouse_up(self, pos): | |
91 if self.pressed_child: | |
92 child = self.pressed_child | |
93 x = pos[0] - child.rect[0] | |
94 y = pos[1] - child.rect[1] | |
95 child.on_mouse_up((x, y)) | |
96 self.pressed_child = None | |
97 | |
98 def on_mouse_move(self, pos): | |
99 if self.pressed_child: | |
100 if self.pressed_child != self.get_child_by_pos(pos): | |
101 self.pressed_child.on_mouse_cancel() | |
102 self.pressed_child = None | |
103 | |
104 def draw(self, screen): | |
105 if self.background_colour: | |
106 self.surface.fill(self.background_colour) | |
107 if self.background_image: | |
108 self.surface.blit(self.background_image, (0, 0)) | |
109 self.draw_children(self.surface) | |
110 screen.blit(self.surface, (0, 0)) | |
111 | |
37 | 112 |
113 class StateDrawable(Drawable): | |
38 | 114 |
115 def __init__(self, rect): | |
116 super(StateDrawable, self).__init__(rect) | |
117 self.state = -1 | |
118 self.states = {} | |
119 self.drawables = [] | |
120 self.surface = Surface((self.rect[2], self.rect[3])) | |
121 | |
122 def draw(self, surface): | |
123 if self.state != -1 and self.drawables[self.state]: | |
124 self.drawables[self.state].draw(surface) | |
125 | |
126 def add_state(self, state_name, drawable): | |
127 self.states[state_name] = len(self.drawables) | |
128 self.drawables.append(drawable) | |
129 | |
130 def set_state(self, state_name): | |
131 self.state = self.states[state_name] | |
132 | |
133 | |
37 | 134 class Button(Clickable, StateDrawable): |
38 | 135 |
136 def __init__(self, rect, normal_drawable, down_drawable): | |
137 super(Button, self).__init__(rect) | |
138 self.add_state('NORMAL', normal_drawable) | |
139 self.add_state('DOWN', down_drawable) | |
140 self.set_state('NORMAL') | |
141 | |
142 def on_mouse_down(self, pos): | |
143 self.set_state('DOWN') | |
144 | |
145 def on_mouse_up(self, pos): | |
146 self.set_state('NORMAL') | |
147 self.on_click() | |
148 | |
149 def on_mouse_cancel(self): | |
150 self.set_state('NORMAL') | |
151 | |
152 | |
37 | 153 class TextButton(Button): |
154 | |
38 | 155 def __init__(self, rect, normal_drawable, down_drawable, text): |
156 super(TextButton, self).__init__(rect, normal_drawable, down_drawable) | |
157 self.surface = Surface((rect[2], rect[3])) | |
158 self.text = text | |
159 font_large.set_bold(True) | |
160 self.text_surface = font_large.render(self.text, True, (128, 128, 128)) | |
161 shadow = font_large.render(self.text, True, (0, 0, 0)) | |
162 font_large.set_bold(False) | |
163 self.text_surface.blit(shadow, (-2, -2)) | |
164 size = font_large.size(self.text) | |
165 self.text_offset = ((rect[2] - size[0]) / 2, (rect[3] - size[1]) / 2) | |
37 | 166 |
38 | 167 def draw(self, surface): |
168 self.surface.fill((0, 0, 0, 0)) | |
169 super(TextButton, self).draw(self.surface) | |
170 self.surface.blit(self.text_surface, self.text_offset) | |
171 surface.blit(self.surface, self.rect) |