-
Notifications
You must be signed in to change notification settings - Fork 3
/
ui.py
282 lines (208 loc) · 8.18 KB
/
ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# coding=utf-8
from sdl2 import SDL_ClearError, \
SDL_CreateTextureFromSurface, \
SDL_CreateRGBSurface, \
SDL_FreeSurface, \
SDL_RenderCopy, \
SDL_Rect, \
SDL_Color, \
SDL_DestroyTexture, \
SDL_SWSURFACE, \
SDL_BlitSurface
from sdl2.sdlttf import TTF_Init, \
TTF_RenderText_Blended, \
TTF_OpenFont, \
TTF_CloseFont, \
TTF_GetError, \
TTF_RenderText_Shaded
from sdl2.ext import Resources, \
TextureSpriteRenderSystem, \
TextureSprite, \
SoftwareSprite, \
SoftwareSpriteRenderSystem, \
SDLError, \
FontManager, \
load_image, \
subsurface
from const import Colors
from utils import count_chars
RESOURCES = Resources(__file__, 'resources', 'ui')
FONTS = Resources(__file__, 'resources', 'fonts')
class SoftSprite(SoftwareSprite):
def __init__(self, renderer, font=None, text="", font_size=16,
text_color=SDL_Color(255, 255, 255),
background_color=SDL_Color(0, 0, 0)):
self.renderer = renderer
if font is None:
font = FONTS.get_path("04B_20__.TTF")
else:
font = FONTS.get_path(font)
self.font_manager = FontManager(font, bg_color=background_color)
self._text = text
self.font_size = font_size
self.text_color = text_color
self.background_color = background_color
surface = self._create_surface()
super(SoftSprite, self).__init__(surface, True)
def _create_surface(self):
surface = self.font_manager.render(self._text)
if surface is None:
raise TTF_GetError()
return surface
def _update_surface(self):
surface = self._create_surface()
super(SoftSprite, self).__init__(surface, None)
@property
def text(self):
return self._text
@text.setter
def text(self, value):
if self._text == value:
return
self._text = value
self._update_surface()
class TextSprite(TextureSprite):
def __init__(self, renderer, font=None, text="", font_size=16,
text_color=SDL_Color(255, 255, 255),
background_color=SDL_Color(0, 0, 0)):
self.renderer = renderer
if font is None:
font = FONTS.get_path("04B_20__.TTF")
else:
font = FONTS.get_path(font)
self.font = TTF_OpenFont(font.encode("UTF-8"), font_size)
if self.font is None:
raise TTF_GetError()
self._text = text
self.font_size = font_size
self.text_color = text_color
self.background_color = background_color
texture = self._create_texture()
super(TextSprite, self).__init__(texture)
def _create_texture(self):
text_surface = TTF_RenderText_Shaded(self.font, self._text.encode("UTF-8"), self.text_color,
self.background_color)
if text_surface is None:
raise TTF_GetError()
texture = SDL_CreateTextureFromSurface(self.renderer, text_surface)
if texture is None:
raise SDLError()
SDL_FreeSurface(text_surface)
return texture
def _update_texture(self):
texture_to_delete = self.texture
texture = self._create_texture()
super(TextSprite, self).__init__(texture)
SDL_DestroyTexture(texture_to_delete)
@property
def text(self):
return self._text
@text.setter
def text(self, value):
if self._text == value:
return
self._text = value
self._update_texture()
class Dialog:
def __init__(self, factory, *args, **kwargs):
self.position = kwargs['position']
self.dialog_box = DialogBox(factory, *args, **kwargs)
self.lines = self.dialog_box.lines
self.font_size = self.dialog_box.font_size
self.text = self.dialog_box.get_sprites()
self.decoration = DialogDecorations(self.lines, self.font_size)
self.decoration.position = self.position[0] - 32, self.position[1] - 32
def get_decoration_sprites(self):
return self.decoration
def get_text_sprites(self):
return self.text
class DialogDecorations(SoftwareSprite):
def __init__(self, lines, font_size):
self.free = True
border_sprite_sheet_path = RESOURCES.get_path("dialog_border.png")
self.border_sprite_sheet = load_image(border_sprite_sheet_path)
self.surface = self.create_surface(lines, font_size)
super(DialogDecorations, self).__init__(self.surface.contents, self.free)
def create_surface(self, lines, font_size):
max_chars = 8
tile_size = 16
offset = tile_size * 4
width = font_size * max_chars
height = font_size * lines
surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
width + offset,
height + offset,
32,
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000)
cols = int(width / tile_size) + 3
rows = int(height / tile_size) + 3
sprite_crop = [0, 0, tile_size, tile_size]
for i in range(cols + 1):
for j in range(rows + 1):
if (i == 0) and (j == 0):
sprite_crop[0] = 0
sprite_crop[1] = 0
elif (i < cols) and (j == 0):
sprite_crop[0] = 16
sprite_crop[1] = 0
elif (i == cols) and (j == 0):
sprite_crop[0] = 32
sprite_crop[1] = 0
elif (i == cols) and (j < rows):
sprite_crop[0] = 32
sprite_crop[1] = 16
elif (i == 0) and (j < rows):
sprite_crop[0] = 0
sprite_crop[1] = 16
elif (i == 0) and (j == rows):
sprite_crop[0] = 0
sprite_crop[1] = 32
elif (i < cols) and (j == rows):
sprite_crop[0] = 16
sprite_crop[1] = 32
elif (i == cols) and (j == rows):
sprite_crop[0] = 32
sprite_crop[1] = 32
else:
sprite_crop[0] = 16
sprite_crop[1] = 16
rect = SDL_Rect((16 * i), (16 * j))
tile = subsurface(self.border_sprite_sheet, sprite_crop)
SDL_BlitSurface(tile, None, surface, rect)
return surface
class DialogBox:
def __init__(self, factory, *args, **kwargs):
self.factory = factory
self.font_size = kwargs['font_size']
self.fg_color = kwargs['fg_color']
self.bg_color = kwargs['bg_color']
self.font_name = kwargs['font_name']
self.text = kwargs['text']
self.position = kwargs['position']
self.renderer = kwargs['renderer']
self.sprites = []
self.surface = None
self.lines = len(self.text.items())
for i in range(self.lines):
self.create_text_sprites(i)
super(DialogBox, self).__init__()
def create_text_sprites(self, line):
text_sprite = None
if isinstance(self.renderer, SoftwareSpriteRenderSystem):
text_sprite = SoftSprite(self.renderer, self.font_name, self.text[line],
font_size=self.font_size,
text_color=self.fg_color,
background_color=self.bg_color)
elif isinstance(self.renderer, TextureSpriteRenderSystem):
text_sprite = TextSprite(self.renderer, self.font_name, self.text[line],
font_size=self.font_size,
text_color=self.fg_color,
background_color=self.bg_color)
text_sprite.x = self.position[0]
text_sprite.y = self.position[1] + (self.font_size * line)
self.sprites.append(text_sprite)
def get_sprites(self):
return self.sprites