Mercurial > pyntnclick
comparison pyntnclick/scenewidgets.py @ 824:9f542ef6e498 pyntnclick
Reorganise code
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Tue, 29 Jan 2013 11:41:03 +0200 |
parents | e42d19c2237b |
children | 3a00c9337731 |
comparison
equal
deleted
inserted
replaced
823:1bf088e7865b | 824:9f542ef6e498 |
---|---|
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 | |
7 from pygame.colordict import THECOLORS | 6 from pygame.colordict import THECOLORS |
8 from pygame.surface import Surface | 7 from pygame.surface import Surface |
9 | 8 |
10 from pyntnclick.state import Thing | 9 from pyntnclick.state import Thing |
10 from pyntnclick.utils import convert_color, render_text | |
11 from pyntnclick.widgets.text import LabelWidget | 11 from pyntnclick.widgets.text import LabelWidget |
12 | 12 |
13 | 13 |
14 class Interact(object): | 14 class Interact(object): |
15 | 15 |
58 | 58 |
59 Used so we can easily include translatable strings in the scenes""" | 59 Used so we can easily include translatable strings in the scenes""" |
60 | 60 |
61 def __init__(self, x, y, w, h, text, color, max_font_size, font=None): | 61 def __init__(self, x, y, w, h, text, color, max_font_size, font=None): |
62 self._text = text | 62 self._text = text |
63 self._color = Color(color) | 63 self._color = convert_color(color) |
64 self._max_font_size = max_font_size | 64 self._max_font_size = max_font_size |
65 self._font = font | 65 self._font = font |
66 rect = Rect((x, y), (w, h)) | 66 rect = Rect((x, y), (w, h)) |
67 super(InteractText, self).__init__(None, rect, rect) | 67 super(InteractText, self).__init__(None, rect, rect) |
68 | 68 |
69 def set_thing(self, thing): | 69 def set_thing(self, thing): |
70 done = False | |
71 font_size = self._max_font_size | 70 font_size = self._max_font_size |
72 bg_color = Color(0, 0, 0, 0) # transparent background | |
73 if not self._font: | 71 if not self._font: |
74 # Pull the default font out of constants | 72 # Pull the default font out of constants |
75 self._font = thing.gd.constants.font | 73 self._font = thing.gd.constants.font |
76 surface = Surface(self.rect.size, SRCALPHA).convert_alpha() | 74 bg_color = Color(0, 0, 0, 0) # transparent background |
77 while not done and font_size > 0: | 75 self.image = render_text(self._text, self._font, font_size, |
78 font = thing.resource.get_font(self._font, font_size) | 76 self._color, bg_color, thing.resource, self.rect.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 | 77 |
92 | 78 |
93 class InteractRectUnion(Interact): | 79 class InteractRectUnion(Interact): |
94 | 80 |
95 def __init__(self, rect_list): | 81 def __init__(self, rect_list): |