Mercurial > boomslang
comparison pyntnclick/scenewidgets.py @ 820:e42d19c2237b pyntnclick
Add InteractText and InteractUnion
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Mon, 28 Jan 2013 18:19:44 +0200 |
parents | 80ff3c9187bc |
children | 9f542ef6e498 |
comparison
equal
deleted
inserted
replaced
819:80ff3c9187bc | 820:e42d19c2237b |
---|---|
1 """Interactive elements within a Scene.""" | 1 """Interactive elements within a Scene.""" |
2 | 2 |
3 | 3 |
4 from pygame import Rect | 4 from pygame import Rect |
5 from pygame.color import Color | 5 from pygame.color import Color |
6 from pygame.locals import SRCALPHA | |
6 from pygame.colordict import THECOLORS | 7 from pygame.colordict import THECOLORS |
7 from pygame.surface import Surface | 8 from pygame.surface import Surface |
8 | 9 |
9 from pyntnclick.state import Thing | 10 from pyntnclick.state import Thing |
10 from pyntnclick.widgets.text import LabelWidget | 11 from pyntnclick.widgets.text import LabelWidget |
50 rect = Rect((x, y), label.size) | 51 rect = Rect((x, y), label.size) |
51 label.draw_all(image) | 52 label.draw_all(image) |
52 super(InteractDebugText, self).__init__(image, rect, rect) | 53 super(InteractDebugText, self).__init__(image, rect, rect) |
53 | 54 |
54 | 55 |
56 class InteractText(Interact): | |
57 """Display a text string on a transparent background. | |
58 | |
59 Used so we can easily include translatable strings in the scenes""" | |
60 | |
61 def __init__(self, x, y, w, h, text, color, max_font_size, font=None): | |
62 self._text = text | |
63 self._color = Color(color) | |
64 self._max_font_size = max_font_size | |
65 self._font = font | |
66 rect = Rect((x, y), (w, h)) | |
67 super(InteractText, self).__init__(None, rect, rect) | |
68 | |
69 def set_thing(self, thing): | |
70 done = False | |
71 font_size = self._max_font_size | |
72 bg_color = Color(0, 0, 0, 0) # transparent background | |
73 if not self._font: | |
74 # Pull the default font out of constants | |
75 self._font = thing.gd.constants.font | |
76 surface = Surface(self.rect.size, SRCALPHA).convert_alpha() | |
77 while not done and font_size > 0: | |
78 font = thing.resource.get_font(self._font, font_size) | |
79 text_surf = font.render(self._text, True, self._color) | |
80 if (text_surf.get_width() > self.rect.width or | |
81 text_surf.get_height() > self.rect.height): | |
82 font_size -= 1 | |
83 else: | |
84 done = True | |
85 surface.fill(bg_color) | |
86 # Centre the text in the rect | |
87 x = max(0, (self.rect.width - text_surf.get_width()) / 2) | |
88 y = max(0, (self.rect.height - text_surf.get_height()) / 2) | |
89 surface.blit(text_surf, (x, y)) | |
90 self.image = surface | |
91 | |
92 | |
55 class InteractRectUnion(Interact): | 93 class InteractRectUnion(Interact): |
56 | 94 |
57 def __init__(self, rect_list): | 95 def __init__(self, rect_list): |
58 super(InteractRectUnion, self).__init__(None, None, None) | 96 super(InteractRectUnion, self).__init__(None, None, None) |
59 rect_list = [Rect(x) for x in rect_list] | 97 rect_list = [Rect(x) for x in rect_list] |
60 self.interact_rect = rect_list | 98 self.interact_rect = rect_list |
99 | |
100 | |
101 class InteractUnion(Interact): | |
102 """An interact made out of other interacts""" | |
103 | |
104 def __init__(self, interact_list): | |
105 super(InteractUnion, self).__init__(None, None, None) | |
106 self._interact_list = interact_list | |
107 | |
108 def set_thing(self, thing): | |
109 interact_list = [] | |
110 for sub_interact in self._interact_list: | |
111 sub_interact.set_thing(thing) | |
112 sub_rect = sub_interact.interact_rect | |
113 if hasattr(sub_rect, 'collidepoint'): | |
114 interact_list.append(sub_interact.interact_rect) | |
115 else: | |
116 interact_list.extend(sub_interact.interact_rect) | |
117 self.interact_rect = interact_list | |
118 | |
119 def draw(self, surface): | |
120 for sub_interact in self._interact_list: | |
121 sub_interact.draw(surface) | |
122 | |
123 def animate(self): | |
124 for sub_interact in self._interact_list: | |
125 sub_interact.animate() | |
61 | 126 |
62 | 127 |
63 class InteractImage(Interact): | 128 class InteractImage(Interact): |
64 | 129 |
65 def __init__(self, x, y, image_name): | 130 def __init__(self, x, y, image_name): |