Info_edupage/TuGUI/Objects/Scene.py

110 lines
3.3 KiB
Python
Raw Normal View History

2021-09-01 11:26:29 +02:00
import pygame.transform
2021-09-13 20:32:50 +02:00
from TuGUI.Objects.Screen import Screen
2021-09-01 11:26:29 +02:00
from numpy import array as a
2021-09-13 20:03:31 +02:00
import numpy as np
2021-09-01 11:26:29 +02:00
class Scene(Screen):
2021-09-06 18:32:02 +02:00
object_type = "scene"
2021-09-01 22:22:18 +02:00
def __init__(self, width: float, height: float, scene_size: a, bg=(60, 60, 60)):
super().__init__(width, height, scene_size)
2021-09-01 11:26:29 +02:00
self.bg = bg
self.r_objects = [] # resizable Objects
2021-09-02 19:56:28 +02:00
self.nr_objects = [] # non-rescalable Objects
2021-09-06 18:32:02 +02:00
self.d_objects = [] # dynamic Objects
self.c_objects = [] # controllable Objects
2021-09-03 16:44:18 +02:00
self.i_objects = [] # interactive Objects
2021-09-10 19:36:15 +02:00
self.l_objects = [] # light Objects
2021-09-03 16:44:18 +02:00
2021-09-02 19:56:28 +02:00
self.position = a([0, 0])
self.multiscene = None
2021-09-01 11:26:29 +02:00
2021-09-02 22:25:45 +02:00
self.mouse_pos = a([0, 0])
self.clicked = False
2021-09-10 19:36:15 +02:00
def light_update(self):
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-01 11:26:29 +02:00
self.s_.fill(self.bg)
2021-09-10 19:36:15 +02:00
for object_ in self.l_objects:
object_.update()
2021-09-01 11:26:29 +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])
2021-09-01 17:32:53 +02:00
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-01 11:26:29 +02:00
pygame.display.update()
2021-09-02 22:25:45 +02:00
def update_mouse_events(self, mouse_pos, clicked):
self.mouse_pos = self.inverse_matrix @ a(mouse_pos)
self.clicked = clicked
2021-09-02 19:56:28 +02:00
def blit(self):
2021-09-02 22:25:45 +02:00
self.update()
2021-09-02 19:56:28 +02:00
self.multiscene.s.blit(self.s, self.multiscene.matrix @ self.position)
def progress(self):
pass
2021-09-03 16:44:18 +02:00
def get_i_objects(self):
2021-09-06 16:03:18 +02:00
return [[self.i_objects, self.mouse_pos]]
2021-09-06 18:32:02 +02:00
2021-09-13 20:03:31 +02:00
def get_info_for_light_objects(self, position):
s = self
positions = [position]
matrices = [self.matrix]
while s.multiscene is not None:
positions.append(s.position)
s = s.multiscene
matrices.append(s.matrix)
transformed_position = a([0., 0.])
for p, m in zip(positions, matrices):
transformed_position += a(m @ p)
return s.s, transformed_position
2021-09-06 18:32:02 +02:00
def sort_objects(self, *args):
for object_ in args:
for object_tag in object_.object_type.split("_"):
if object_tag == "r":
self.r_objects.append(object_)
elif object_tag == "nr":
self.nr_objects.append(object_)
elif object_tag == "d":
self.d_objects.append(object_)
elif object_tag == "c":
self.c_objects.append(object_)
elif object_tag == "i":
self.i_objects.append(object_)
2021-09-10 19:36:15 +02:00
elif object_tag == "l":
self.l_objects.append(object_)
2021-09-06 18:32:02 +02:00
elif object_tag in ["scene", "multiscene"]:
if self.object_type == "multiscene":
self.subscenes.append(object_)
else:
raise ValueError("Scene/Multiscene object encountered in scene.sort_objects()\n"
"Please use Multiscene for this purpose.")