-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwar_list_interface.py
289 lines (248 loc) · 13.8 KB
/
war_list_interface.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
283
284
285
286
287
288
289
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above
# copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
import pygame
import defines
import common_functions
import debug_functions
global prev_position, current_position, has_rendered
VALID_CHARS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'-', ' ', 'á', 'é', 'í', 'ó', 'ú', 'ä', 'ë', 'ï', 'ö', 'ü', 'â',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
LISTING_HEIGHT = 50 # Height of the bars
RECT_OFFSET = defines.PAD_DIST # Distance the bars are spaced from one another
FLAG_HEIGHT = int(LISTING_HEIGHT - LISTING_HEIGHT * 0.15)
FLAG_WIDTH = FLAG_HEIGHT # They're squares
SCROLL_SIZE = 2 # Amount each click of the scroll wheel moves the list
has_rendered = False # To trigger rendering of the list when the program is first initialized. Kind of lazy.
new_war_list = None
mode = "all" # "all" or "player" (types of nations shown)
sorting = "date" # "cas" for casualties, "date" for chronological
prev_position = 0
current_position = 0 # The topmost bar in the list
search_text = ""
def get_war_casualties(war) -> int: # Utility function
try:
return sum(war.a_loss_list) + sum(war.d_loss_list)
except:
return 0
def draw_list_controls(window, font, small_font):
global list_controls, search_text
button_bar_height = defines.NAV_BUTTON_HEIGHT
button_border_width = defines.NAV_BUTTON_BORDER_WIDTH
# Background bar the buttons are all sitting on
button_back_loc = (0, window.get_height() - button_bar_height, window.get_width(), button_bar_height)
button_back = pygame.draw.rect(window, defines.C_INTERFACE, button_back_loc)
button_back_rectangle = pygame.draw.rect(window, defines.C_GOLD, button_back_loc, button_border_width)
button_width = defines.NAV_BUTTON_HEIGHT + defines.PAD_DIST * 2
# Button to only show wars involving current player nations
# Text to the left of the button
sort_by_player_str = "Current player nations:"
sort_by_player = font.render(sort_by_player_str, True, defines.C_GOLD)
sort_by_player_loc = sort_by_player.get_rect()
sort_by_player_loc.midleft = (defines.PAD_DIST, button_back_rectangle.centery)
window.blit(sort_by_player, sort_by_player_loc)
# Button background and text
player_button_loc = (sort_by_player_loc.right + defines.PAD_DIST, button_back_rectangle.top + defines.PAD_DIST,
button_width, defines.NAV_BUTTON_HEIGHT - defines.PAD_DIST * 2)
if mode == "player":
player_button_backing = pygame.draw.rect(window, defines.C_LGRAY, player_button_loc)
list_controls.append(["disable", "playermode", player_button_backing])
player_button_toggle = pygame.draw.rect(window, defines.C_GOLD, player_button_loc, button_border_width)
else:
player_button_toggle = pygame.draw.rect(window, defines.C_GOLD, player_button_loc, button_border_width)
list_controls.append(["enable", "playermode", player_button_toggle])
player_button_label = small_font.render("PLAY", True, defines.C_GOLD)
player_button_label_loc = player_button_label.get_rect()
player_button_label_loc.center = player_button_toggle.center
window.blit(player_button_label, player_button_label_loc)
# Button to open a new savefile
open_button_loc = (
window.get_width() - defines.PAD_DIST - button_width, button_back_rectangle.top + defines.PAD_DIST,
button_width, defines.NAV_BUTTON_HEIGHT - defines.PAD_DIST * 2)
open_button = pygame.draw.rect(window, defines.C_GOLD, open_button_loc, button_border_width)
open_button_label = small_font.render("OPEN", True, defines.C_GOLD)
open_button_label_loc = open_button_label.get_rect()
open_button_label_loc.center = open_button.center
window.blit(open_button_label, open_button_label_loc)
list_controls.append(["open", "", open_button])
# Sort by date/casualties
sort_str = "Sort by casualties:"
sort_obj = font.render(sort_str, True, defines.C_GOLD)
sort_loc = sort_obj.get_rect()
sort_loc.midleft = (player_button_toggle.right + defines.PAD_DIST, button_back_rectangle.centery)
window.blit(sort_obj, sort_loc)
# Sort toggle button
sort_button_loc = (sort_loc.right + defines.PAD_DIST, button_back_rectangle.top + defines.PAD_DIST,
button_width, defines.NAV_BUTTON_HEIGHT - defines.PAD_DIST * 2)
if sorting == "cas":
sort_button_backing = pygame.draw.rect(window, defines.C_LGRAY, sort_button_loc)
list_controls.append(["date", "sorting", sort_button_backing])
sort_button_toggle = pygame.draw.rect(window, defines.C_GOLD, sort_button_loc, button_border_width)
else:
sort_button_toggle = pygame.draw.rect(window, defines.C_GOLD, sort_button_loc, button_border_width)
list_controls.append(["cas", "sorting", sort_button_toggle])
sort_button_label = small_font.render("SORT", True, defines.C_GOLD)
sort_button_label_loc = sort_button_label.get_rect()
sort_button_label_loc.center = sort_button_toggle.center
window.blit(sort_button_label, sort_button_label_loc)
# The search bar
search_bar_info_str = "Search nations:"
search_bar_info = font.render(search_bar_info_str, True, defines.C_GOLD)
search_bar_info_loc = search_bar_info.get_rect()
search_bar_info_loc.midleft = (sort_button_toggle.right + defines.PAD_DIST, sort_button_label_loc.centery)
window.blit(search_bar_info, search_bar_info_loc)
search_bar_x = search_bar_info_loc.right + defines.PAD_DIST
search_bar_loc = (search_bar_x, button_back.y + defines.PAD_DIST,
open_button_label_loc.x - search_bar_x - (defines.PAD_DIST * 2),
button_back.height - (defines.PAD_DIST * 2))
search_bar = pygame.draw.rect(window, defines.C_GOLD, search_bar_loc, button_border_width)
# Text in the search bar
if search_text == "":
search_text_rendered = small_font.render("(type)", True, defines.C_LGRAY)
else:
search_text_rendered = small_font.render(search_text, True, defines.C_WHITE)
search_text_loc = search_text_rendered.get_rect()
search_text_loc.midleft = (search_bar.x, search_bar.centery)
window.blit(search_text_rendered, search_text_loc)
def list_disp_update(window, font, small_font, war_list):
global prev_position, current_position, clickable_list, list_controls
clickable_list = []
list_controls = []
max_war_count = int(window.get_height() / (LISTING_HEIGHT + RECT_OFFSET + 0.0) + 1)
window.fill(defines.C_BLACK)
to_render = []
for i in range(current_position, min(len(war_list), current_position + max_war_count)):
if war_list[i].viable is True:
render_i = i - current_position
curr_loc_rect = (0, render_i * (LISTING_HEIGHT + RECT_OFFSET), window.get_width(), LISTING_HEIGHT)
curr_bar = pygame.draw.rect(window, defines.C_INTERFACE, curr_loc_rect)
# Render bars with the war title
curr_text = font.render(war_list[i].title, True, defines.C_WHITE)
text_loc = curr_text.get_rect()
text_loc.center = (
window.get_width() // 2, render_i * (LISTING_HEIGHT + RECT_OFFSET) + (LISTING_HEIGHT / 2))
to_render.append((curr_text, text_loc))
clickable_list.append(["war", war_list[i], curr_bar])
# Render the start date on the left
start_date_text = font.render(war_list[i].start_date, True, defines.C_LGRAY)
start_text_loc = start_date_text.get_rect()
start_text_loc.center = (0, render_i * (LISTING_HEIGHT + RECT_OFFSET) + (LISTING_HEIGHT / 2))
start_text_loc.x = defines.PAD_DIST
to_render.append((start_date_text, start_text_loc))
# Render the end date on the right
end_date_text = font.render(war_list[i].end_date, True, defines.C_LGRAY)
end_text_loc = end_date_text.get_rect()
end_text_loc.center = (0, render_i * (LISTING_HEIGHT + RECT_OFFSET) + (LISTING_HEIGHT / 2))
end_text_loc.right = window.get_width() - defines.PAD_DIST
to_render.append((end_date_text, end_text_loc))
# Draw flags: (primary) attacker's left of the war title, defender's right
curr_flag = common_functions.load_flag(war_list[i].primary_attacker, war_list[i])
first_flag = pygame.transform.scale(curr_flag, (FLAG_WIDTH, FLAG_HEIGHT))
to_render.append((first_flag, (text_loc.left - defines.PAD_DIST - FLAG_WIDTH, text_loc.y)))
curr_flag = common_functions.load_flag(war_list[i].primary_defender, war_list[i])
second_flag = pygame.transform.scale(curr_flag, (FLAG_WIDTH, FLAG_HEIGHT))
to_render.append((second_flag, (text_loc.right + defines.PAD_DIST, text_loc.y)))
window.blits(to_render)
draw_list_controls(window, font, small_font)
pygame.display.update()
def get_player_war_list(war_list):
new_list = []
for war in war_list:
if war.has_player:
new_list.append(war)
return new_list
def list_loop(window, font, small_font, war_list, event, force_update=False, force_reset_list=False, position=None):
global prev_position, current_position
global has_rendered
global mode, sorting
global new_war_list, search_text
if position is not None:
current_position = position
prev_position = current_position
changed_text = False
if new_war_list is None or force_reset_list:
new_war_list = war_list
if force_reset_list and mode == "player":
new_war_list = get_player_war_list(war_list)
if event is not None:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4: # 4 and 5 are the scroll wheel
if current_position > SCROLL_SIZE - 1:
current_position -= SCROLL_SIZE
elif event.button == 5:
if current_position < len(new_war_list) - SCROLL_SIZE - 1:
current_position += SCROLL_SIZE
elif event.button == 1:
mouse_pos = pygame.mouse.get_pos()
for button in list_controls:
if button[2].collidepoint(mouse_pos):
if button[0] == "enable":
if button[1] == "playermode":
mode = "player"
new_war_list = get_player_war_list(war_list)
changed_text = True
elif button[0] == "disable":
if button[1] == "playermode":
mode = "all"
new_war_list = war_list
changed_text = True
elif button[0] == "open":
current_position = 0
return "open"
elif button[1] == "sorting":
sorting = button[0]
changed_text = True
current_position = 0
force_update = True
break
if mouse_pos[1] < window.get_height() - defines.NAV_BUTTON_HEIGHT: # Off the button bar on the bottom
for button in clickable_list:
if button[2].collidepoint(mouse_pos):
if button[0] == "war":
return button[1], current_position
elif event.type == pygame.KEYDOWN:
if event.unicode in VALID_CHARS:
search_text += event.unicode.upper()
changed_text = True
elif event.key == 8: # Backspace
search_text = search_text[:-1]
changed_text = True
elif event.key == 127 or event.key == 27: # DEL or ESC (deletes everything)
if len(search_text) > 0:
changed_text = True
search_text = ""
force_update = True
# Update war list for search string
if changed_text:
current_position = 0
if mode == "player":
# Search only player nations
temp_war_list = get_player_war_list(war_list)
else:
temp_war_list = war_list
new_war_list = []
for war in temp_war_list:
if search_text in war.title.upper():
new_war_list.append(war)
else:
for participant in war.participants:
if search_text in war.participants[participant].longname.upper():
new_war_list.append(war)
break
else:
if search_text == war.participants[participant].name.upper():
new_war_list.append(war)
break
if sorting == "cas":
new_war_list = sorted(new_war_list, key=lambda x: get_war_casualties(x))
new_war_list.reverse()
if prev_position != current_position or force_update or not has_rendered:
# Only render if the position has changed (scrolled), main.py wants to force it to (when
# the window is resized), or the program has just initialized.
has_rendered = True
list_disp_update(window, font, small_font, new_war_list)
return None