This commit is contained in:
Benjamín 2021-09-02 19:56:28 +02:00
parent 9a1c6d95b8
commit 319b85c552
8 changed files with 244 additions and 5 deletions

78
UI/Objects/Multiscene.py Normal file

@ -0,0 +1,78 @@
import math
import pygame
from numpy import array as a
from Objects.Scene import Scene
class Multiscene(Scene):
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)
self.subscenes_prepared = True
def redraw(self):
if self.subscenes_prepared is False:
self.prepare_subscenes()
self.s_.fill(self.bg)
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()
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]
])
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()
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()
@staticmethod
def to_ints(iterable):
for i in range(len(iterable)):
iterable[i] = int(iterable[i])
return iterable

@ -9,7 +9,10 @@ class Scene(Screen):
self.bg = bg self.bg = bg
self.r_objects = [] # resizable Objects self.r_objects = [] # resizable Objects
self.nr_objects = [] # non-resizable Objects self.nr_objects = [] # non-rescalable Objects
self.position = a([0, 0])
self.multiscene = None
def redraw(self): def redraw(self):
self.s_.fill(self.bg) self.s_.fill(self.bg)
@ -25,3 +28,9 @@ class Scene(Screen):
pygame.display.update() pygame.display.update()
def blit(self):
self.redraw()
self.multiscene.s.blit(self.s, self.multiscene.matrix @ self.position)
def progress(self):
pass

@ -23,6 +23,20 @@ class Screen:
self.center = a([width, height]) / 2 self.center = a([width, height]) / 2
def resize_screen(self, new_screen_size, subscene=False):
self.screen_size = a(new_screen_size)
self.matrix = a([
[new_screen_size[0] / self.width, 0],
[0, new_screen_size[1] / self.height]
])
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)
def save(self, path, size=None): def save(self, path, size=None):
if size is None: if size is None:
pygame.image.save(self.s, path) pygame.image.save(self.s, path)

@ -0,0 +1,49 @@
from numpy import array as a
import pygame
import math
class EquilateralTriangle:
def __init__(self, position, side_length, rotation, color, scene, width=0):
self.position = position
self.side_length = side_length
self.rotation = rotation
self.color = color
self.scene = scene
self.width = width
self.rotation_point = complex(math.cos(rotation), math.sin(rotation))
self.pd_ = self.scene.pd_
self.points = a([
complex(0, self.side_length),
complex(self.side_length, self.side_length),
complex(self.side_length * 0.5,
self.side_length - math.sqrt(self.side_length ** 2 - (self.side_length * 0.5) ** 2))
])
self.points -= complex(self.side_length * 0.5, self.side_length * 0.5)
self.points *= self.rotation_point * self.pd_
self.points += complex(self.side_length * 0.5, self.side_length * 0.5)
rescaled_position = self.scene.matrix @ self.position
self.points += complex(rescaled_position[0], rescaled_position[1])
self.points = [[point.real, point.imag] for point in self.points]
def blit(self):
if self.pd_ != self.scene.pd_:
self.pd_ = self.scene.pd_
self.points = a([
complex(0, self.side_length),
complex(self.side_length, self.side_length),
complex(self.side_length * 0.5,
self.side_length - math.sqrt(self.side_length ** 2 - (self.side_length * 0.5) ** 2))
])
self.points -= complex(self.side_length * 0.5, self.side_length * 0.5)
self.points *= self.rotation_point * self.pd_
self.points += complex(self.side_length * 0.5, self.side_length * 0.5)
rescaled_position = self.scene.matrix @ self.position
self.points += complex(rescaled_position[0], rescaled_position[1])
self.points = [[point.real, point.imag] for point in self.points]
pygame.draw.polygon(self.scene.s, self.color, self.points, self.width)

@ -0,0 +1,40 @@
from Objects.Multiscene import Multiscene
from Scenes.BasicScene import BasicScene
from Objects.r_objects import *
from numpy import array as a
from UI.Objects.nr_objects.Line import Line
class BasicMultiscene(Multiscene):
def __init__(self, scene_size):
self.s__size = [600, 400]
super().__init__(self.s__size[0], self.s__size[1], scene_size)
s0 = BasicScene([200, 400])
self.subscenes.append(s0)
s1 = BasicScene([400, 400])
s1.position = a([200, 0])
self.subscenes.append(s1)
l0 = Line(a([200, 0]), a([200, 400]), 10, (255, 100, 100), self)
self.nr_objects.append(l0)
self.i = 0
def progress(self):
if self.i == 1:
self.subscenes[0].subscene_size[0] += 1
self.subscenes[1].subscene_size[0] -= 1
self.subscenes[1].position[0] += 1
self.nr_objects[0].a[0] += 1
self.nr_objects[0].b[0] += 1
self.resize_subscenes()
self.i = 0
else:
self.i += 1

@ -1,10 +1,10 @@
from Objects.Scene import Scene from Objects.Scene import Scene
from Objects.r_objects import * from Objects.r_objects import *
from numpy import array as a from numpy import array as a
import numpy as np
from UI.Objects.nr_objects.Ellipse import Ellipse from UI.Objects.nr_objects.Ellipse import Ellipse
from UI.Objects.nr_objects.Circle import Circle from UI.Objects.nr_objects.Circle import Circle
from UI.Objects.nr_objects.EquilateralTriangle import EquilateralTriangle
from UI.Objects.nr_objects.Image import Image from UI.Objects.nr_objects.Image import Image
from UI.Objects.nr_objects.Line import Line from UI.Objects.nr_objects.Line import Line
from UI.Objects.nr_objects.Text import Text from UI.Objects.nr_objects.Text import Text
@ -14,9 +14,9 @@ from UI.Objects.r_objects.Rect import Rect
class BasicScene(Scene): class BasicScene(Scene):
def __init__(self, scene_size: a, bg=(60, 60, 60)): def __init__(self, scene_size: a, bg=(60, 60, 60)):
s__size = [600, 400] self.s__size = [600, 400]
super().__init__(s__size[0], s__size[1], scene_size, bg) super().__init__(self.s__size[0], self.s__size[1], scene_size, bg)
r0 = Rect(a([0, 0]), a([200, 200]), (160, 160, 160), self) r0 = Rect(a([0, 0]), a([200, 200]), (160, 160, 160), self)
self.r_objects.append(r0) self.r_objects.append(r0)
@ -35,3 +35,6 @@ class BasicScene(Scene):
i0 = Image(a([200, 0]), 0.8, "test.png", self) i0 = Image(a([200, 0]), 0.8, "test.png", self)
self.nr_objects.append(i0) self.nr_objects.append(i0)
t0 = EquilateralTriangle(a([500, 300]), 30, 1, (250, 250, 150), self)
self.nr_objects.append(t0)

@ -0,0 +1,42 @@
from Objects.Multiscene import Multiscene
from Scenes.BasicMultiscene import BasicMultiscene
from numpy import array as a
from UI.Objects.nr_objects.Line import Line
class MultisceneInMultiscene(Multiscene):
def __init__(self, scene_size):
self.s__size = [600, 800]
super().__init__(self.s__size[0], self.s__size[1], scene_size)
ms0 = BasicMultiscene([600, 400])
self.subscenes.append(ms0)
ms1 = BasicMultiscene([600, 400])
ms1.position = a([0, 400])
self.subscenes.append(ms1)
l0 = Line(a([0, 400]), a([600, 400]), 12, (100, 100, 255), self)
self.nr_objects.append(l0)
self.i = 0
def progress(self):
self.progress_subscenes()
if self.i == 1:
self.subscenes[0].subscene_size[1] += 1
self.subscenes[1].subscene_size[1] -= 1
self.subscenes[1].position[1] += 1
self.nr_objects[0].a[1] += 1
self.nr_objects[0].b[1] += 1
self.resize_subscenes()
self.i = 0
else:
self.i += 1

@ -1,14 +1,18 @@
from Scenes.BasicScene import BasicScene from Scenes.BasicScene import BasicScene
from Scenes.BasicMultiscene import BasicMultiscene
from Scenes.MultisceneInMultiscene import MultisceneInMultiscene
from numpy import array as a from numpy import array as a
import time import time
screen_size = a([200, 500]) screen_size = a([200, 500])
scene = MultisceneInMultiscene(screen_size)
for i in range(400): for i in range(400):
scene = BasicScene(screen_size) scene.resize_screen(screen_size)
scene.redraw() scene.redraw()
scene.progress()
# scene.save(f"Render/{i}.png", [800, 800]) # scene.save(f"Render/{i}.png", [800, 800])
if i < 150: if i < 150: