comparison gamelib/gui_base.py @ 37:9c4bf1f15431

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