forked from lassefolkersen/highfrontier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
3897 lines (2868 loc) · 187 KB
/
gui.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from enum import Enum
import os
import global_variables
import sys
import string
import pygame
import datetime
import math
import company
import primitives
import gui_components
import random
import time
from solarsystem import solarsystem
from display import Display, raise_not_implemented_display, Direction
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
class gui():
"""
This class holds all the top-level gui stuff, such as functions to distribute clicks and the commandbox buttons on the right side and such
"""
def __init__(self,right_side_surface, message_surface, action_surface, solar_system_object: solarsystem):
"""
Here the commandbox is started initialized. In addition all the other GUI elements is started up, to keep it at one place
"""
# defining
command_box_left = right_side_surface.get_offset()[0]
command_width = right_side_surface.get_size()[0]
infobox_top = 0
command_top = 70
subcommand_top = 470
self.action_rect = pygame.Rect(0,0, action_surface.get_size()[0], action_surface.get_size()[1])
self.infobox_rect = pygame.Rect(command_box_left, infobox_top, command_width, command_top)
self.command_rect = pygame.Rect(command_box_left, command_top, command_width, subcommand_top-command_top)
self.subcommand_rect = pygame.Rect(command_box_left, subcommand_top, command_width, global_variables.window_size[1] -subcommand_top)
self.action_surface = action_surface
self.infobox_surface = right_side_surface.subsurface(pygame.Rect(0, infobox_top, command_width, command_top))
self.command_surface = right_side_surface.subsurface(pygame.Rect(0, command_top, command_width, subcommand_top-command_top))
self.subcommand_surface = right_side_surface.subsurface(pygame.Rect(0, subcommand_top, command_width, global_variables.window_size[1] -subcommand_top))
self.solar_system_object_link = solar_system_object
self.active_window = None
self.all_windows = {}
self.all_windows["Messages"] = message_bar(solar_system_object, action_surface, message_surface)
self.all_windows["Company menu"] = company_window(solar_system_object, action_surface)
self.all_windows["Trade menu"] = trade_window(solar_system_object, action_surface)
self.all_windows["Base overview"] = base_window(solar_system_object, action_surface)
self.all_windows["Map overlays"] = overlay_window(solar_system_object, action_surface)
self.all_windows["Planet shortcuts"] = planet_jump_window(solar_system_object, action_surface)
# self.all_windows["Navigation"] = navigation_window(solar_system_object, action_surface)
self.all_windows["File menu"] = file_window(solar_system_object, action_surface)
self.all_windows["Technology"] = tech_window(solar_system_object, action_surface)
self.all_windows["base_population_info"] = base_population_info(solar_system_object, action_surface)
self.all_windows["base_list_of_companies"] = base_list_of_companies(solar_system_object, action_surface)
self.all_windows["base_list_of_firms"] = base_list_of_firms(solar_system_object, action_surface)
self.all_windows["base_and_firm_market_window"] = base_and_firm_market_window(solar_system_object, action_surface)
self.all_windows["base_build_menu"] = base_build_menu(solar_system_object, action_surface)
self.all_windows["company_ownership_info"] = company_ownership_info(solar_system_object, action_surface)
self.all_windows["company_financial_info"] = company_financial_info(solar_system_object, action_surface)
self.all_windows["company_list_of_firms"] = company_list_of_firms(solar_system_object, action_surface)
self.all_windows["firm_trade_partners_info"] = firm_trade_partners_info(solar_system_object, action_surface)
self.all_windows["firm_process_info"] = firm_process_info(solar_system_object, action_surface)
self.all_windows["firm_process_info"] = firm_process_info(solar_system_object, action_surface)
self.all_windows["construct_base_menu"] = construct_base_menu(solar_system_object, action_surface)
self.create_infobox()
self.create_commandbox()
self.create_subcommandbox()
def receive_click(self,event):
"""
Function that distributes clicks where necessary
"""
#Checking where the click is located
if self.command_rect.collidepoint(event.pos) == 1:
for button in list(self.command_buttons.values()):
if button.rect.collidepoint((event.pos[0] - global_variables.window_size[0] + self.command_rect[2], event.pos[1] - self.command_rect[1])) == 1:
button.activate(None)
return
if self.subcommand_rect.collidepoint(event.pos) == 1:
for button in list(self.subcommand_buttons.values()):
if button.rect.collidepoint((event.pos[0] - global_variables.window_size[0] + self.subcommand_rect[2], event.pos[1] - self.subcommand_rect[1])) == 1:
button.activate(None)
return
elif self.action_rect.collidepoint(event.pos) == 1:
if self.active_window is not None:
if self.active_window.rect.collidepoint(event.pos) == 1:
return_value = self.active_window.receive_click(event)
if return_value is not None:
if return_value == "clear":
self.clear_screen()
elif return_value == "population transfer":
self.solar_system_object_link.display_mode = Display.PLANETARY
self.solar_system_object_link.build_base_mode = True
self.solar_system_object_link.building_base = self.solar_system_object_link.current_planet.current_base
print_dict = {"text":"DEBUGGING: unknown display mode passed to infobox","type":"debugging"}
self.solar_system_object_link.messages.append(print_dict)
pygame.mouse.set_cursor(*pygame.cursors.diamond)
print_dict = {"text":"Select destination for population transfer: new or existing base","type":"general gameplay info"}
self.clear_screen()
self.active_window = None
pygame.display.flip()
else:
raise Exception("Receive click got this, return value: " + str(return_value))
return
self.click_in_action_window(event)
#updating the infobox in any case
self.create_infobox()
self.all_windows["Messages"].create()
pygame.display.flip()
def clear_screen(self):
"""
Function that takes care of clearing screen, by drawing whatever planet or solar system or base window that is supposed
to be on it, thus overwriting what gui-box might be there
"""
self.active_window = None
sol = self.solar_system_object_link
match sol.display_mode:
case Display.SOLAR_SYSTEM:
self.action_surface.blit(sol.draw_solar_system(zoom_level=sol.solar_system_zoom,date_variable=sol.current_date,center_object=sol.current_planet.planet_name),(0,0))
case Display.PLANETARY:
self.action_surface.blit(sol.current_planet.draw_entire_planet(sol.current_planet.eastern_inclination,sol.current_planet.northern_inclination,sol.current_planet.projection_scaling),(0,0))
case Display.BASE:
if sol.current_planet.current_base is not None:
self.going_to_base_mode_event(sol.current_planet.current_base)
case Display.FIRM:
if sol.firm_selected is not None:
self.going_to_firm_window_event(sol.firm_selected)
case Display.COMPANY:
if sol.company_selected is not None:
self.going_to_company_window_event(sol.company_selected)
case Display.TECHTREE:
pass
case _:
raise NotImplementedError(f"The mode: {sol.display_mode} is unknown")
self.create_subcommandbox()
pygame.display.flip()
def draw_display(self, display: Display) -> pygame.Surface:
"""Draw the display according to the given mode."""
sol = self.solar_system_object_link
surface = None
logger.debug(f"Drawing display {display=}")
match display:
case Display.SOLAR_SYSTEM:
surface = sol.draw_solar_system(
zoom_level=sol.solar_system_zoom,
date_variable=sol.current_date,
center_object=sol.current_planet.planet_name
)
case Display.PLANETARY:
surface = sol.current_planet.draw_entire_planet(
sol.current_planet.eastern_inclination,
sol.current_planet.northern_inclination,
sol.current_planet.projection_scaling
)
case Display.TECHTREE:
surface = sol.technology_tree.draw()
case _:
raise_not_implemented_display(display)
if surface is None:
raise RuntimeError("No surface was created")
return surface
def zoom(self, out: bool = False):
"""Zoom in or out."""
self.clear_screen()
sol = self.solar_system_object_link
logger.debug(f"Zooming {out=}, {sol.display_mode=}")
match sol.display_mode, out:
case Display.SOLAR_SYSTEM, True:
if sol.solar_system_zoom >= 2:
# Zoom out if not already on max
sol.solar_system_zoom = int(sol.solar_system_zoom / 2)
else:
# No update
return
case Display.SOLAR_SYSTEM, False:
if sol.solar_system_zoom > 2000000:
# Go to planet
sol.display_mode = Display.PLANETARY
else:
# Zoom
sol.solar_system_zoom = int(sol.solar_system_zoom * 2)
case Display.PLANETARY, True:
if sol.current_planet.projection_scaling >= 90:
sol.current_planet.projection_scaling = int(sol.current_planet.projection_scaling / 2)
else:
sol.solar_system_zoom = 300000000 / max(sol.current_planet.planet_diameter_km, 2000)
sol.current_planet.unload_from_drawing()
sol.display_mode = Display.SOLAR_SYSTEM
case Display.PLANETARY, False:
if sol.current_planet.projection_scaling < 720:
sol.current_planet.projection_scaling = int(sol.current_planet.projection_scaling * 2)
else:
if sol.current_planet.current_base is not None:
# if a base is selected on this planet, we'll zoom in on it
sol.display_mode = Display.BASE
self.going_to_base_mode_event(sol.current_planet.current_base)
return
case Display.FIRM | Display.COMPANY | Display.BASE, True:
# Use the planetary surface
sol.display_mode = Display.PLANETARY
self.create_subcommandbox()
case Display.TECHTREE, _:
sol.technology_tree.zoom(out=out)
case _:
# No update
return
surface = self.draw_display(sol.display_mode)
self.action_surface.blit(surface,(0,0))
pygame.display.flip()
def click_in_action_window(self,event):
sol = self.solar_system_object_link
position = event.pos
button = event.button
click_spot = pygame.Rect(position[0]-3,position[1]-3,6,6)
logger.debug(f"Click in action window {sol.display_mode=}")
surface = None
match sol.display_mode:
case Display.SOLAR_SYSTEM:
collision_test_result = click_spot.collidedict(sol.areas_of_interest)
if collision_test_result != None:
sol.current_planet = sol.planets[collision_test_result[1]]
else:
return
case Display.PLANETARY:
if sol.build_base_mode:
#if we are in the special build base mode, there should be a base creation instead.
sphere_coordinates = sol.current_planet.check_base_position(position)
if sphere_coordinates[0:19] == "transfer population" or isinstance(sphere_coordinates, tuple) or sphere_coordinates == "space base": #if the selection was correctly verified by check_base_position we send it back to the GUI for further processing
sol.build_base_mode = False
pygame.mouse.set_cursor(*pygame.cursors.arrow)
self.all_windows["construct_base_menu"].new_base_ask_for_name(sphere_coordinates)
self.active_window = self.all_windows["construct_base_menu"]
return
else:
#if we are not in build_base_mode we work as normally
indexes = (sol.current_planet.northern_inclination,sol.current_planet.eastern_inclination, int(sol.current_planet.projection_scaling))
try:
areas_of_interest = sol.current_planet.areas_of_interest[indexes]
except (IndexError, KeyError):
logger.error(f'Problem with the area of interest for index {indexes}')
return
collision_test_result = click_spot.collidedict(areas_of_interest)
if collision_test_result != None:
current_base = sol.current_planet.bases[collision_test_result[1]]
logger.debug(f"{current_base=}")
sol.current_planet.current_base = current_base
if button == 1:
surface = sol.current_planet.draw_entire_planet(sol.current_planet.eastern_inclination,sol.current_planet.northern_inclination,sol.current_planet.projection_scaling)
elif button == 3:
self.going_to_base_mode_event(current_base)
return
else:
logger.error(f"Unkonw button action {button}")
return
else:
return
case Display.TECHTREE:
surface = sol.technology_tree.receive_click(event)
case _:
# No update
return
if surface is None:
logger.error(f"No surface received to update after click in action window")
return
self.action_surface.blit(surface,(0,0))
pygame.display.flip()
def go_any_direction(self, direction: Direction):
"""Move the display to the requested direction."""
self.clear_screen()
sol = self.solar_system_object_link
logger.debug(f"Moving {direction=}, {sol.display_mode=}")
match sol.display_mode:
case Display.PLANETARY:
self._go_planet_direction(direction)
case Display.TECHTREE:
sol.technology_tree.move(direction)
case _:
return
surface = self.draw_display(sol.display_mode)
self.action_surface.blit(surface,(0,0))
pygame.display.flip()
def _go_planet_direction(self, direction: Direction):
"""Move the planet accordint to the direction."""
sol = self.solar_system_object_link
match direction:
case Direction.LEFT:
sol.current_planet.eastern_inclination -= 30
if sol.current_planet.eastern_inclination <= -180:
sol.current_planet.eastern_inclination += 360
case Direction.RIGHT:
sol.current_planet.eastern_inclination += 30
if sol.current_planet.eastern_inclination > 180:
sol.current_planet.eastern_inclination -= 360
case Direction.UP:
sol.current_planet.northern_inclination += 30
if sol.current_planet.northern_inclination > 90:
sol.current_planet.northern_inclination = 90
case Direction.DOWN:
sol.current_planet.northern_inclination -= 30
if sol.current_planet.northern_inclination < -90:
sol.current_planet.northern_inclination = -90
case _:
raise ValueError(f"Unknown direction {direction}")
def going_to_company_window_event(self,company_selected):
sol = self.solar_system_object_link
# company_selected = event.data
# mode_before_change = sol.display_mode
sol.display_mode = Display.COMPANY
surface = company_selected.draw_company_window()
self.action_surface.blit(surface,(0,0))
pygame.display.flip()
def going_to_firm_window_event(self,firm_selected):
sol = self.solar_system_object_link
sol.display_mode = Display.FIRM
surface = firm_selected.draw_firm_window()
self.action_surface.blit(surface,(0,0))
pygame.display.flip()
def going_to_base_mode_event(self,base_selected):
sol = self.solar_system_object_link
# mode_before_change = sol.display_mode
sol.current_planet.current_base = base_selected
sol.current_planet = base_selected.home_planet
sol.display_mode = Display.BASE
surface = base_selected.draw_base_window()
self.create_subcommandbox()
self.action_surface.blit(surface,(0,0))
pygame.display.flip()
#
def create_infobox(self):
self.infobox_surface.fill((150,150,150))
# creating the date string
date_string = str(self.solar_system_object_link.current_date)
rendered_date_string = global_variables.standard_font.render(date_string,True,(0,0,0))
self.infobox_surface.blit(rendered_date_string, (10,10))
# creating the env string
env_string = ""
match self.solar_system_object_link.display_mode:
case Display.SOLAR_SYSTEM:
env_string = "Solar system -" + self.solar_system_object_link.current_planet.planet_name.upper()
case Display.PLANETARY:
if self.solar_system_object_link.current_planet.current_base == None:
env_string = self.solar_system_object_link.current_planet.planet_name
else:
env_string = self.solar_system_object_link.current_planet.planet_name + " - " + self.solar_system_object_link.current_planet.current_base.name
case Display.COMPANY:
if self.solar_system_object_link.company_selected is not None:
env_string = self.solar_system_object_link.company_selected.name
case Display.FIRM:
if self.solar_system_object_link.firm_selected is not None:
env_string = self.solar_system_object_link.firm_selected.name
case Display.BASE:
if self.solar_system_object_link.current_planet.current_base is not None:
env_string = self.solar_system_object_link.current_planet.current_base.name
case Display.TECHTREE:
env_string = "technology tree"
case _:
raise_not_implemented_display(self.solar_system_object_link.display_mode)
rendered_env_string = global_variables.standard_font.render(env_string,True,(0,0,0))
self.infobox_surface.blit(rendered_env_string, (10,30))
#creating the capital string
if self.solar_system_object_link.current_player is not None:
capital_string = str(primitives.nicefy_numbers(int(self.solar_system_object_link.current_player.capital))) + " $"
rendered_capital_string = global_variables.standard_font.render(capital_string,True,(0,0,0))
self.infobox_surface.blit(rendered_capital_string, (10,50))
def commandbox_button_activate(self,label, function_parameter):
"""
Function that decides what to do if a commandbox button is pressed
"""
if label == "Technology":
if self.solar_system_object_link.display_mode == Display.TECHTREE:
self.solar_system_object_link.display_mode = self.all_windows["Technology"].display_mode_before
self.clear_screen()
return
else:
self.all_windows["Technology"].display_mode_before = self.solar_system_object_link.display_mode
self.clear_screen()
self.active_window = self.all_windows[label]
self.all_windows[function_parameter].create()
def create_commandbox(self):
"""
Creates the right-side menu command box
"""
self.command_surface.fill((150,150,150))
# pygame.draw.rect(self.command_surface, (150,150,150), self.command_rect)
labels = ["Map overlays","Planet shortcuts","Company menu","Base overview","Technology","Trade menu","File menu"]
self.command_buttons = {}
for i, label in enumerate(labels):
self.command_buttons[label] = gui_components.button(label,
self.command_surface,
self.commandbox_button_activate,
function_parameter = label,
fixed_size = (self.command_rect[2]-20,35), topleft = (10, i * 40 + 10))
def subcommandbox_button_activate(self, nicelabel, label):
"""
Activates the right-side menu lower subcommand box. This is specific for the navigation window context
such as for example base information and such
"""
self.all_windows[label].create()
self.active_window = self.all_windows[label]
def create_subcommandbox(self):
"""
Creates the right-side menu lower subcommand box. This is specific for the navigation window context
such as for example base information and such
"""
self.subcommand_surface.fill((150,150,150))
self.subcommand_buttons = {}
# pygame.draw.rect(self.subcommand_surface, (150,150,150), self.subcommand_rect)
if self.solar_system_object_link.display_mode == Display.BASE:
self.buttonlinks = ["base_population_info","base_list_of_companies","base_list_of_firms","base_and_firm_market_window","base_build_menu"]
self.buttonnicenames = ["Population","Companies","Firms","Market","Build"]
elif self.solar_system_object_link.display_mode == Display.FIRM:
self.buttonlinks = ["firm_process_info","base_and_firm_market_window","firm_trade_partners_info"]
self.buttonnicenames = ["Production","Market","Trade partners"]
elif self.solar_system_object_link.display_mode == Display.COMPANY:
self.buttonlinks = ["company_ownership_info","company_financial_info","company_list_of_firms"]
self.buttonnicenames = ["Ownership info","Financial info","Owned firms"]
else:
pygame.display.flip()
return
self.subcommand_buttons = {}
for i, label in enumerate(self.buttonlinks):
self.subcommand_buttons[label] = gui_components.button(self.buttonnicenames[i], self.subcommand_surface,
self.subcommandbox_button_activate, function_parameter = label,
fixed_size = (self.subcommand_surface.get_width()-20,35),
topleft = (10, i * 40 + 10))
pygame.display.flip()
class message_bar():
"""
Class that receives messages for the player and prints them.
It will show the message depending on the type. Types are:
general gameplay info
company_generation
and more
The message bar is visible at all times in the bottom of the screen.
"""
def __init__(self,solar_system_object,action_surface,message_surface):
self.solar_system_object_link = solar_system_object
self.action_surface = action_surface
self.message_surface = message_surface
self.messages = []
self.max_print_length = 6 #how many lines of text to print in standard viewing of the window
self.max_save_length = 500 #how many lines of text to save in memory
self.max_string_length = 140#how many letters is maximally allowed to be printed in the message window
self.create()
def create(self):
"""
Function that will update the text field
"""
self.message_surface.fill((212,212,212))
pygame.draw.line(self.message_surface, (255,255,255), (0, 0), (self.message_surface.get_size()[0],0),2)
pygame.draw.line(self.message_surface, (255,255,255), (0, 0), (0,self.message_surface.get_size()[1]),2)
#first trim the message list down to the number indicated to be max
if len(self.messages) > self.max_save_length:
surplus = len(self.messages) - self.max_save_length
del self.messages[0:surplus]
messages = []
range_here = list(range(0,len(self.solar_system_object_link.messages)))
range_here.reverse()
for i in range_here:
message = self.solar_system_object_link.messages[i]
if self.solar_system_object_link.message_printing[message["type"]]:
messages.append(message)
if len(messages) >= self.max_print_length:
break
messages.reverse()
i = 0
for message in messages:
if self.solar_system_object_link.message_printing[message["type"]]:
if len(message["text"]) > self.max_string_length:
message_text = message["text"][0:self.max_string_length]
else:
message_text = message["text"]
rendered_message_string = global_variables.standard_font_small.render(message_text,True,(0,0,0))
self.message_surface.blit(rendered_message_string, (10,10 + i * 15))
i = i + 1
class navigation_window():
"""
The navigation window. Is controlled by a togglebutton in the commandbox. When visible it can be used for zooming and rotating.
"""
def __init__(self,solar_system_object,action_surface):
self.solar_system_object_link = solar_system_object
self.rect = pygame.Rect(500,50,190,170)
self.action_surface = action_surface
def zoom_in(self,label,function_parameter):
return "zoom_in"
def zoom_out(self,label,function_parameter):
return "zoom_out"
def rotate_west(self,label,function_parameter):
return "go_west"
def rotate_east(self,label,function_parameter):
return "go_east"
def rotate_south(self,label,function_parameter):
return "go_south"
def rotate_north(self,label,function_parameter):
return "go_north"
def receive_click(self,event):
if pygame.Rect(self.rect[0] + 70, self.rect[1] + 10, 50, 30).collidepoint(event.pos) == 1:
return self.button_north.activate(event.pos)
if pygame.Rect(self.rect[0] + 20, self.rect[1] + 40, 50, 30).collidepoint(event.pos) == 1:
return self.button_west.activate(event.pos)
if pygame.Rect(self.rect[0] + 70, self.rect[1] + 70, 50, 30).collidepoint(event.pos) == 1:
return self.button_south.activate(event.pos)
if pygame.Rect(self.rect[0] + 120, self.rect[1] + 40, 50, 30).collidepoint(event.pos) == 1:
return self.button_east.activate(event.pos)
if pygame.Rect(self.rect[0] + 10, self.rect[1] + 120, 80, 30).collidepoint(event.pos) == 1:
return self.button_zoom_in.activate(event.pos)
if pygame.Rect(self.rect[0] + 100, self.rect[1] + 120, 80, 30).collidepoint(event.pos) == 1:
return self.button_zoom_out.activate(event.pos)
def create(self):
"""
The creation function.
"""
pygame.draw.rect(self.action_surface, (212,212,212), self.rect)
pygame.draw.rect(self.action_surface, (0,0,0), self.rect, 2)
pygame.draw.line(self.action_surface, (255,255,255), (self.rect[0], self.rect[1]), (self.rect[0] + self.rect[2], self.rect[1]))
pygame.draw.line(self.action_surface, (255,255,255), (self.rect[0], self.rect[1]), (self.rect[0], self.rect[1] + self.rect[3]))
self.button_north = gui_components.button("N", self.action_surface, self.rotate_north, topleft = (self.rect[0] + 70, self.rect[1] + 10),fixed_size = (50,30))
self.button_west = gui_components.button("W", self.action_surface, self.rotate_west, topleft = (self.rect[0] + 20, self.rect[1] + 40),fixed_size = (50,30))
self.button_south = gui_components.button("S", self.action_surface, self.rotate_south, topleft = (self.rect[0] + 70, self.rect[1] + 70),fixed_size = (50,30))
self.button_east = gui_components.button("E", self.action_surface, self.rotate_east, topleft = (self.rect[0] + 120, self.rect[1] + 40),fixed_size = (50,30))
self.button_zoom_in = gui_components.button("Zoom in", self.action_surface, self.zoom_in, topleft = (self.rect[0] + 10, self.rect[1] + 120),fixed_size = (80,30))
self.button_zoom_out = gui_components.button("Zoom out", self.action_surface, self.zoom_out, topleft = (self.rect[0] + 100, self.rect[1] + 120),fixed_size = (80,30))
class overlay_window():
"""
The overlay control window. Can be toggled from commandbox. When visible it can be used to control which visual overlays
that can be seen in planet mode (topographical maps, resource maps etc)
"""
def __init__(self,solar_system_object,action_surface):
self.solar_system_object_link = solar_system_object
self.rect = pygame.Rect(500,50,200,250)
self.action_surface = action_surface
def overlay_set(self,type_of_overlay, function_parameter):
sol = self.solar_system_object_link
sol.current_planet.planet_display_mode = type_of_overlay
surface = sol.current_planet.draw_entire_planet(sol.current_planet.eastern_inclination,sol.current_planet.northern_inclination,sol.current_planet.projection_scaling)
self.action_surface.blit(surface,(0,0))
self.create()
pygame.display.flip()
# def (self,button_name,function_parameter):
# print "emitted display overlay with " + str(button_name)
def receive_click(self,event):
self.radiobuttons.activate(event.pos)
def create(self):
"""
The creation function. Doesn't return anything.
"""
pygame.draw.rect(self.action_surface, (212,212,212), self.rect)
pygame.draw.rect(self.action_surface, (0,0,0), self.rect, 2)
pygame.draw.line(self.action_surface, (255,255,255), (self.rect[0], self.rect[1]), (self.rect[0] + self.rect[2], self.rect[1]))
pygame.draw.line(self.action_surface, (255,255,255), (self.rect[0], self.rect[1]), (self.rect[0], self.rect[1] + self.rect[3]))
labels = ["visible light","trade network","topographical"] + self.solar_system_object_link.mineral_resources
self.radiobuttons = gui_components.radiobuttons(
labels,
self.action_surface,
self.overlay_set,
function_parameter = None,
topleft = (self.rect[0] + 10 , self.rect[1] + 10),
selected = self.solar_system_object_link.current_planet.planet_display_mode)
class planet_jump_window():
"""
The planet jump window. Can be toggled from commandbox. When visible it can be used as shortcut to planet view
for the different planets
"""
def __init__(self,solar_system_object,action_surface):
self.solar_system_object_link = solar_system_object
self.rect = pygame.Rect(500,50,100,250)
self.action_surface = action_surface
def planet_jump(self,planet_name,function_parameter):
planet = self.solar_system_object_link.planets[planet_name]
self.solar_system_object_link.current_planet = planet
planet.load_for_drawing()
self.solar_system_object_link.display_mode = Display.PLANETARY
surface = planet.draw_entire_planet(planet.eastern_inclination,planet.northern_inclination,planet.projection_scaling)
self.action_surface.blit(surface,(0,0))
pygame.display.flip()
def receive_click(self,event):
offset = event.pos[1] - self.rect[1]
index = (offset - 5) // 30
if 0 <= index < len(self.button_labels):
selection = self.buttons[self.button_labels[index]].activate(event.pos)
if selection in list(self.solar_system_object_link.planets.keys()):
return self.solar_system_object_link.planets[selection]
else:
if self.solar_system_object_link.message_printing["debugging"]:
print_dict = {"text":"DEBUGGING: The planet jump function asked to go to a non-recognised planet","type":"debugging"}
self.solar_system_object_link.messages.append(print_dict)
def create(self):
"""
The creation function.
"""
pygame.draw.rect(self.action_surface, (212,212,212), self.rect)
pygame.draw.rect(self.action_surface, (0,0,0), self.rect, 2)
pygame.draw.line(self.action_surface, (255,255,255), (self.rect[0], self.rect[1]), (self.rect[0] + self.rect[2], self.rect[1]))
pygame.draw.line(self.action_surface, (255,255,255), (self.rect[0], self.rect[1]), (self.rect[0], self.rect[1] + self.rect[3]))
self.button_labels = ["mercury","venus","earth","mars","jupiter","saturn","uranus","neptune"]
self.buttons = {}
for i, button_label in enumerate(self.button_labels):
self.buttons[button_label] = gui_components.button(button_label, self.action_surface, self.planet_jump, topleft = (self.rect[0] + 5, self.rect[1] + 5 + i * 30),fixed_size = (self.rect[2] - 10, 25))
class tech_window():
"""
Class for the tech tree. Most of the actual algorithms are in techtree.py - this is just a shell for holding
the notify system in the same structure as other GUI elements
"""
def __init__(self,solar_system_object, action_surface):
self.solar_system_object_link = solar_system_object
self.action_surface = action_surface
self.display_mode_before = Display.PLANETARY
self.rect = pygame.Rect(0,0,0,0)
def create(self):
sol = self.solar_system_object_link
sol.display_mode = Display.TECHTREE
surface = sol.technology_tree.plot_total_tree(sol.technology_tree.vertex_dict,sol.technology_tree.zoomlevel,center = sol.technology_tree.center)
self.action_surface.blit(surface,(0,0))
pygame.display.flip()
def receive_click(self,event):
if self.solar_system_object_link.message_printing["debugging"]:
print_dict = {"text":"DEBUGGING: tech window received a direct click. This should not be possible","type":"debugging"}
self.solar_system_object_link.messages.append(print_dict)
class base_window():
"""
This window shows an overview of all bases with options for fast jumping.
"""
def __init__(self,solar_system_object,action_surface):
self.solar_system_object_link = solar_system_object
self.rect = pygame.Rect(50,50,700,500)
self.action_surface = action_surface
def create(self):
"""
The creation function. '
"""
base_data = {}
for planet_instance in list(self.solar_system_object_link.planets.values()):
for base_instance in list(planet_instance.bases.values()):
if base_instance.for_sale:
for_sale = "For sale"
else:
for_sale = ""
data_here = {"Location":planet_instance.name,"Population":base_instance.population,"For sale":for_sale}
base_data[base_instance.name] = data_here
column_order = ["rownames","Location","Population","For sale"]
self.fast_list = gui_components.fast_list(self.action_surface, base_data, self.rect, column_order)
def receive_click(self,event):
self.fast_list.receive_click(event)
if event.button == 3:
base_selected = None
for planet_instance in list(self.solar_system_object_link.planets.values()):
for base_instance in list(planet_instance.bases.values()):
if base_instance.name == self.fast_list.selected_name:
base_selected = base_instance
if base_selected is None:
raise Exception("The base sought after (" + str(self.fast_list.selected_name) + ") was not found in the base list of the solar_system_object_link")
self.solar_system_object_link.current_planet.current_base = base_selected
self.solar_system_object_link.display_mode = Display.BASE
return "clear"
class trade_window():
"""
This windows shows an overview of all assets (bases and firms) and tech that is for sale, ie. all non-location specific offers.
"""
def __init__(self,solar_system_object,action_surface):
self.solar_system_object_link = solar_system_object
self.rect = pygame.Rect(25,50,825,500)
self.action_surface = action_surface
self.text_receiver = None
self.menu_position = "root"
self.selections = {}
def create(self):
"""
The creation function. Doesn't return anything.
"""
self.text_receiver = None
self.menu_position = "root"
self.selections = {}
asset_and_tech_data = {}
for planet_instance in list(self.solar_system_object_link.planets.values()):
for base_instance in list(planet_instance.bases.values()):
if base_instance.for_sale:
data_here = {"Type":"base","Best price":"for auction (pop: " + str(base_instance.population) + ")","For sale by":base_instance.owner.name,"for_sale_by_link":[base_instance.owner],"object":base_instance}
asset_and_tech_data[base_instance.name] = data_here
#for company_instance in self.solar_system_object_link.companies.values():
#pass #FIXME add firms for sale here, whenever that is implemented
for technology in list(self.solar_system_object_link.technology_tree.vertex_dict.values()):
if len(technology["for_sale_by"]) > 0:
prices = list(technology["for_sale_by"].values())
prices.sort()
best_price = prices[0]
if len(technology["for_sale_by"]) == 1:
for_sale_by = str(list(technology["for_sale_by"].keys())[0].name)
else:
for_sale_by = str(len(technology["for_sale_by"])) + " companies"
for_sale_by_link = list(technology["for_sale_by"].keys())
check_result = self.solar_system_object_link.technology_tree.check_technology_bid(self.solar_system_object_link.current_player.known_technologies,technology)
if check_result != "already known": #only include if we don't already know it
if not check_result == "ok": #include it as a sales-piece if too advaned, but not possible to buy
type = "advanced tech."
else:
type = "technology"
data_here = {"Type":type,"Best price":best_price,"For sale by":for_sale_by,"for_sale_by_link":for_sale_by_link,"object":technology}
asset_and_tech_data[technology["technology_name"]] = data_here
if len(asset_and_tech_data) == 0:
pygame.draw.rect(self.action_surface, (224,218,213), self.rect)
pygame.draw.rect(self.action_surface, (0,0,0), self.rect, 2)
pygame.draw.line(self.action_surface, (255,255,255), (self.rect[0], self.rect[1]), (self.rect[0] + self.rect[2], self.rect[1]))
pygame.draw.line(self.action_surface, (255,255,255), (self.rect[0], self.rect[1]), (self.rect[0], self.rect[1] + self.rect[3]))
warning = global_variables.standard_font.render("Nothing is for sale on the tech and asset market.",True,(0,0,0))
self.action_surface.blit(warning, (self.rect[0] + self.rect[2] /2 - warning.get_width() / 2, self.rect[1] + self.rect[3] / 2 - warning.get_height() /2))
else:
column_order = ["rownames","Type","Best price","For sale by"]
self.fast_list = gui_components.fast_list(self.action_surface, asset_and_tech_data, rect = self.rect, column_order = column_order)
def receive_click(self,event):
#if event.button == 1:
if self.menu_position == "root":
self.fast_list.receive_click(event)
elif self.menu_position == "pick_seller":
self.fast_list.receive_click(event)
elif self.menu_position == "base bidding":
if self.bid_button.rect.collidepoint(event.pos) == 1:
return self.bid_button.activate(event)
else:
raise Exception("Unknown menu_position: " + str(self.menu_position))
if event.button == 3:
if self.fast_list.selected_name is not None:
if self.menu_position == "root":
return self.process_bid(self.fast_list.selected_name)
elif self.menu_position == "pick_seller":
# print self.fast_list.selected_name
return self.perform_bid(self.fast_list.selected_name)
else:
raise Exception("Unknown menu_position: " + str(self.menu_position))
def process_bid(self, bid_on):
"""
Where clicks from the initial menu should be send. Bid_on is the name given in this first menu
"""
self.selections["for_sale_by"] = self.fast_list.original_tabular_data[bid_on]["for_sale_by_link"]
self.selections["sale_object"] = self.fast_list.original_tabular_data[bid_on]["object"]
self.selections["type"] = self.fast_list.original_tabular_data[bid_on]["Type"]
self.selections["price"] = self.fast_list.original_tabular_data[bid_on]["Best price"]
self.selections["bid_on"] = bid_on
if self.selections["type"] in ["technology","advanced tech."]:
current_player_tech = self.solar_system_object_link.current_player.known_technologies
check_result = self.solar_system_object_link.technology_tree.check_technology_bid(current_player_tech,self.selections["sale_object"] )