Projekt_Sztuczna_Inteligencja/disarming/popup.py

597 lines
20 KiB
Python
Raw Normal View History

2021-06-06 15:51:08 +02:00
import pygame
import pygame_gui
2021-06-06 22:00:42 +02:00
2021-06-06 15:51:08 +02:00
from project_constants import SCREEN_WIDTH, SCREEN_HEIGHT, V_NAME_OF_WINDOW
2021-06-06 22:00:42 +02:00
from objects.mine_models.mine import Mine
from disarming.disarming_handler import DisarmingHandler
2021-06-06 15:51:08 +02:00
# =========== #
# == const == #
# =========== #
# main heights and lengths
gap_main = 20
gap_elem = 10
height_label = 24
height_cable_double_label = 2 * height_label + gap_elem
length_param_desc = 120
length_param_values = 250
length_cables = 250
# main heights and lengths
# frame, image and image labels
length_frame = length_param_desc + gap_elem + length_param_values + 3 * gap_main + length_cables
length_image = int((length_frame - 3 * gap_main) / 2)
height_image = length_image
height_frame = 6 * height_label + 5 * gap_elem + height_image + 2 * height_label + 2 * gap_elem + 3 * gap_main
screen_size = (int(length_frame), int(height_frame))
length_label_image = int((length_image - gap_elem) / 2)
# frame, image and image labels
# sizes
size_label_param_desc = (length_param_desc, height_label)
size_label_param_values = (length_param_values, height_label)
size_cable_label = (length_cables, height_label)
size_cable = (length_cables, height_cable_double_label)
size_image = (length_image, height_image)
size_light = (height_label, height_label)
size_label_image = (length_label_image, height_label)
# sizes
# y's
y_params1 = y_cables_label1 \
= gap_main
y_params2 = y_cable1 \
= y_params1 + gap_elem + height_label
y_params3 = y_params2 + gap_elem + height_label
y_params4 = y_cables_label2 \
= y_params3 + gap_elem + height_label
y_params5 = y_cable2 \
= y_params4 + gap_elem + height_label
y_params6 = y_params5 + gap_elem + height_label
y_image = y_params6 + gap_main + height_label
y_image_label1 = y_image + height_image + gap_elem
y_image_label2 = y_image_label1 + height_label + gap_elem
# y's
# x's
x_param_desc = x_image1 = x_image1_label1 \
= gap_main
x_image1_label2 = x_image1_label1 + length_label_image + gap_elem
x_param_values = x_param_desc + length_param_desc + gap_elem
x_param_values_after_light = x_param_values + gap_elem
x_cables = x_param_values + length_param_values + gap_main
x_image2 = x_image2_label1\
= x_image1 + length_image + gap_main
x_image2_label2 = x_image2_label1 + length_label_image + gap_elem
# x's
# =========== #
# == class == #
# =========== #
class SampleWindow:
def __init__(self):
# main attributes
self.running = True
self.clock = pygame.time.Clock()
2021-06-06 22:00:42 +02:00
self.manager = pygame_gui.UIManager(screen_size, 'disarming/theme.json') # TODO : change theme path
2021-06-06 15:51:08 +02:00
# main attributes
def gui():
def params_desc():
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_desc, y_params1),
size_label_param_desc
),
text="TYPE",
manager=self.manager,
object_id="description"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_desc, y_params2),
size_label_param_desc
),
text="DANGER CLASS",
manager=self.manager,
object_id="description"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_desc, y_params3),
size_label_param_desc
),
text="INDICATOR",
manager=self.manager,
object_id="description"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_desc, y_params4),
size_label_param_desc
),
text="WEIGHT",
manager=self.manager,
object_id="description"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_desc, y_params5),
size_label_param_desc
),
text="SERIES",
manager=self.manager,
object_id="description"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_desc, y_params6),
size_label_param_desc
),
text="SPECIFICITY",
manager=self.manager,
object_id="description"
)
def params_values():
# type value
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params1),
size_label_param_values
),
text="...",
manager=self.manager,
object_id="empty_field"
)
# danger class value
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params2),
size_label_param_values
),
text="...",
manager=self.manager,
object_id="empty_field"
)
# indicator value
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params3),
size_label_param_values
),
text="...",
manager=self.manager,
object_id="empty_field"
)
# weight value
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params4),
size_label_param_values
),
text="...",
manager=self.manager,
object_id="empty_field"
)
# series value
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params5),
size_label_param_values
),
text="...",
manager=self.manager,
object_id="empty_field"
)
# spec value
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params6),
size_label_param_values
),
text="...",
manager=self.manager,
object_id="empty_field"
)
def cables():
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_cables, y_cables_label1),
size_cable_label
),
text="CORRECT WIRE",
2021-06-06 15:51:08 +02:00
manager=self.manager,
object_id="description"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_cables, y_cables_label2),
size_cable_label
),
text="CHOSEN WIRE",
2021-06-06 15:51:08 +02:00
manager=self.manager,
object_id="description"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_cables, y_cable1),
size_cable
),
text="...",
manager=self.manager,
object_id="empty_field"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_cables, y_cable2),
size_cable
),
text="...",
manager=self.manager,
object_id="empty_field"
)
def image1():
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_image1_label1, y_image_label1),
size_label_image
),
text="CORRECT SERIES",
manager=self.manager,
object_id="description"
)
# correct series value
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_image1_label1, y_image_label2),
size_label_image
),
text="...",
manager=self.manager,
object_id="empty_field"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_image1_label2, y_image_label1),
size_label_image
),
text="RECOGNISED SERIES",
manager=self.manager,
object_id="description"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_image1_label2, y_image_label2),
size_label_image
),
text="...",
manager=self.manager,
object_id="empty_field"
)
surf = pygame.Surface(size_image)
surf.fill((20, 20, 20))
pygame_gui.elements.UIImage(
image_surface=surf,
relative_rect=pygame.Rect(
(x_image1, y_image),
size_image
),
manager=self.manager
)
2021-06-06 15:51:08 +02:00
def image2():
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_image2_label1, y_image_label1),
size_label_image
),
text="CORRECT SPEC",
manager=self.manager,
object_id="description"
)
# correct spec value
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_image2_label1, y_image_label2),
size_label_image
),
text="...",
manager=self.manager,
object_id="empty_field"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_image2_label2, y_image_label1),
size_label_image
),
text="RECOGNISED SPEC",
manager=self.manager,
object_id="description"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_image2_label2, y_image_label2),
size_label_image
),
text="...",
manager=self.manager,
object_id="empty_field"
)
surf = pygame.Surface(size_image)
surf.fill((20, 20, 20))
pygame_gui.elements.UIImage(
image_surface=surf,
relative_rect=pygame.Rect(
(x_image2, y_image),
size_image
),
manager=self.manager
)
2021-06-06 15:51:08 +02:00
params_desc()
params_values()
cables()
image1()
image2()
# frame basics
pygame.display.set_caption("disarming...")
self.window_surface = pygame.display.set_mode(screen_size)
self.background = pygame.Surface(screen_size)
self.background.fill(pygame.Color('#000000'))
# loading the gui
gui()
# ============= #
# == showing == #
# ============= #
def show_params(self, mine_type: str, weight: str, danger: str, indicator: str, series: str, spec: str):
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params1),
size_label_param_values
),
text=mine_type,
manager=self.manager,
object_id="field"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params4),
size_label_param_values
),
text=weight,
manager=self.manager,
object_id="field"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params2),
size_label_param_values
),
text=danger,
manager=self.manager,
object_id="field"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params3),
size_label_param_values
),
text=indicator,
manager=self.manager,
object_id="field"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_image1_label1, y_image_label2),
size_label_image
),
text=series,
manager=self.manager,
object_id="field"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_image2_label1, y_image_label2),
size_label_image
),
text=spec,
manager=self.manager,
object_id="field"
)
def blit_pic(self, x_image, pic):
# initializing surface with background
2021-06-06 15:51:08 +02:00
surf = pygame.Surface(size_image)
surf.fill((20, 20, 20))
# scaling size & coords
bigger = max(pic.get_size()[0], pic.get_size()[1])
scaling_factor = (height_image - 10) / bigger
length_pic = int(pic.get_size()[0]*scaling_factor)
height_pic = int(pic.get_size()[1]*scaling_factor)
size_pic = (length_pic, height_pic)
coords_pic = (
int((length_image-length_pic) / 2),
int((height_image-height_pic) / 2)
2021-06-06 15:51:08 +02:00
)
# blitting the image onto background
surf.blit(pygame.transform.scale(pic, size_pic), coords_pic)
2021-06-06 15:51:08 +02:00
pygame_gui.elements.UIImage(
image_surface=surf,
relative_rect=pygame.Rect(
(x_image, y_image),
2021-06-06 15:51:08 +02:00
size_image
),
manager=self.manager
)
def show_series(self, series: str, correct: bool):
colour = "param_values_correct" if correct else "param_values_wrong"
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params5),
size_label_param_values
),
text=series,
manager=self.manager,
object_id="field"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params5),
size_light
),
text="",
manager=self.manager,
object_id=colour
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_image1_label2, y_image_label2),
size_label_image
),
text=series,
manager=self.manager,
object_id=colour
)
def show_spec(self, spec: str, correct: bool):
colour = "param_values_correct" if correct else "param_values_wrong"
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params6),
size_label_param_values
),
text=spec,
manager=self.manager,
object_id="field"
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_param_values, y_params6),
size_light
),
text="",
manager=self.manager,
object_id=colour
)
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_image2_label2, y_image_label2),
size_label_image
),
text=spec,
manager=self.manager,
object_id=colour
)
def show_cable_calculated(self, colour: str):
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_cables, y_cable1),
size_cable
),
text=colour,
manager=self.manager,
object_id=colour
)
def show_cable_chosen(self, colour: str):
pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(
(x_cables, y_cable2),
size_cable
),
text=colour,
manager=self.manager,
object_id=colour
)
# ========= #
# == run == #
# ========= #
2021-06-06 22:00:42 +02:00
def run(self, mine: Mine):
timed_event = pygame.USEREVENT + 1
pygame.time.set_timer(timed_event, 1000)
2021-06-06 22:00:42 +02:00
step = 0
handler = DisarmingHandler(mine)
2021-06-06 15:51:08 +02:00
while self.running:
time_delta = self.clock.tick(60) / 1000.0
for event in pygame.event.get():
2021-06-06 15:51:08 +02:00
if event.type == pygame.QUIT:
self.running = False
2021-06-06 22:00:42 +02:00
if event.type == timed_event:
if step == 0: # just the params
2021-06-06 22:00:42 +02:00
params = handler.get_mine_params()
self.show_params(params[0], params[1], params[2], params[3], params[4], params[5])
elif step == 1: # correct cable
2021-06-06 22:00:42 +02:00
self.show_cable_calculated(handler.correct_wire)
elif step == 2: # loading the letter image
img = pygame.image.load(handler.pick_series_image())
self.blit_pic(x_image1, img)
elif step == 3: # recognising the letter
2021-06-06 22:00:42 +02:00
answer, is_correct = handler.recognize_series()
self.show_series(answer, is_correct)
elif step == 4: # loading spec image
img = pygame.image.load(handler.pick_specificity_image())
self.blit_pic(x_image2, img)
elif step == 5: # recognising spec image
2021-06-06 22:00:42 +02:00
answer, is_correct = handler.recognize_specificity()
self.show_spec(answer, is_correct)
elif step == 6: # showing the chosen cable
2021-06-06 22:00:42 +02:00
self.show_cable_chosen(handler.choose_wire())
else:
self.running = False
step += 1
2021-06-06 15:51:08 +02:00
self.manager.update(time_delta)
self.window_surface.blit(self.background, (0, 0))
self.manager.draw_ui(self.window_surface)
pygame.display.update()
2021-06-06 22:00:42 +02:00
return handler.defuse()
2021-06-06 15:51:08 +02:00
2021-06-06 22:00:42 +02:00
def disarming_popup(mine: Mine = None):
2021-06-06 15:51:08 +02:00
# run the pop-up
app = SampleWindow()
2021-06-06 22:00:42 +02:00
result = app.run(mine)
2021-06-06 15:51:08 +02:00
# bring display back to normal
pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption(V_NAME_OF_WINDOW)
2021-06-06 22:00:42 +02:00
return result
2021-06-06 15:51:08 +02:00
if __name__ == '__main__':
pygame.init()
disarming_popup()