Info_edupage/TuGUI/Objects/Multiscene.py

125 lines
3.4 KiB
Python
Raw Normal View History

2021-09-03 16:44:18 +02:00
import copy
2021-09-02 19:56:28 +02:00
import math
import pygame
from numpy import array as a
2021-09-13 20:32:50 +02:00
from TuGUI.Objects.Scene import Scene
2021-09-02 19:56:28 +02:00
class Multiscene(Scene):
2021-09-06 18:32:02 +02:00
object_type = "multiscene"
2021-09-02 19:56:28 +02:00
def __init__(self, width: float, height: float, scene_size: a):
super().__init__(width, height, scene_size)
self.subscenes = []
self.subscenes_prepared = False
def prepare_subscenes(self):
for subscene in self.subscenes:
subscene.multiscene = self
subscene.subscene_size = subscene.screen_size
subscene.resize_screen(self.to_ints((self.matrix @ subscene.subscene_size).tolist()), True)
2021-09-10 21:08:42 +02:00
self.s = pygame.display.set_mode(self.screen_size)
2021-09-02 19:56:28 +02:00
self.subscenes_prepared = True
2021-09-10 19:36:15 +02:00
def light_update(self):
2021-09-13 20:03:31 +02:00
for subscene in self.subscenes:
subscene.light_update()
2021-09-10 19:36:15 +02:00
for object_ in self.l_objects:
object_.update()
2021-09-13 20:03:31 +02:00
object_.blit(True)
2021-09-10 19:36:15 +02:00
pygame.display.update()
2021-09-02 22:25:45 +02:00
def update(self):
2021-09-02 19:56:28 +02:00
if self.subscenes_prepared is False:
self.prepare_subscenes()
self.s_.fill(self.bg)
2021-09-10 19:36:15 +02:00
for object_ in self.l_objects:
object_.update()
2021-09-02 19:56:28 +02:00
for object_ in self.r_objects:
object_.blit()
rs = pygame.transform.scale(self.s_, self.screen_size)
self.s.blit(rs, [0, 0])
for scene in self.subscenes:
scene.blit()
for object_ in self.nr_objects:
object_.blit()
2021-09-06 18:32:02 +02:00
for object_ in self.d_objects:
2021-09-02 22:25:45 +02:00
object_.progress()
2021-09-06 18:32:02 +02:00
for object_ in self.c_objects:
2021-09-02 22:25:45 +02:00
object_.input(self.mouse_pos, self.clicked)
2021-09-02 19:56:28 +02:00
pygame.display.update()
def resize_screen(self, new_screen_size, subscene=False):
if self.subscenes_prepared is False:
self.prepare_subscenes()
self.screen_size = a(new_screen_size)
self.matrix = a([
[new_screen_size[0] / self.width, 0],
[0, new_screen_size[1] / self.height]
])
2021-09-02 22:25:45 +02:00
self.inverse_matrix = a([
[1 / self.matrix[0, 0], 0],
[0, 1 / self.matrix[1, 1]]
])
2021-09-02 19:56:28 +02:00
self.pd = abs(self.matrix[0, 0] * self.matrix[1, 1]) # positive determinant
self.pd_ = math.sqrt(self.pd)
if subscene is False:
self.s = pygame.display.set_mode(new_screen_size)
else:
self.s = pygame.Surface(new_screen_size)
self.resize_subscenes()
2021-09-03 16:44:18 +02:00
def update_mouse_events(self, mouse_pos, clicked):
mouse_pos = a(mouse_pos)
self.mouse_pos = self.inverse_matrix @ mouse_pos
self.clicked = clicked
for subscene in self.subscenes:
subscene.mouse_pos = (subscene.inverse_matrix @ (mouse_pos - (self.matrix @ subscene.position)))
subscene.clicked = clicked
2021-09-02 19:56:28 +02:00
def progress(self):
self.progress_subscenes()
def resize_subscenes(self):
for subscene in self.subscenes:
subscene.resize_screen(self.to_ints((self.matrix @ subscene.subscene_size).tolist()), True)
def progress_subscenes(self):
for subscene in self.subscenes:
subscene.progress()
2021-09-03 16:44:18 +02:00
def get_i_objects(self):
2021-09-06 16:03:18 +02:00
i_objects = [[copy.copy(self.i_objects), self.mouse_pos]]
2021-09-03 16:44:18 +02:00
for subscene in self.subscenes:
i_objects += subscene.get_i_objects()
return i_objects
2021-09-02 19:56:28 +02:00
@staticmethod
def to_ints(iterable):
for i in range(len(iterable)):
iterable[i] = int(iterable[i])
return iterable