summaryrefslogtreecommitdiff
path: root/src/sliceitoff/display
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-03-16 12:35:07 +0200
committerViljami Ilola <+@hix.fi>2024-03-16 12:35:07 +0200
commita23b6647b0ca2cd9f2ccbbe12360c3fa0f96a554 (patch)
treebab154622f9767fd340278ab275babb76fc569d5 /src/sliceitoff/display
parent1ad8357f0039292b929e66cf703b4314660aded2 (diff)
scaling as class
Diffstat (limited to 'src/sliceitoff/display')
-rw-r--r--src/sliceitoff/display/__init__.py7
-rw-r--r--src/sliceitoff/display/display.py19
-rw-r--r--src/sliceitoff/display/scaling.py36
-rw-r--r--src/sliceitoff/display/static.py9
4 files changed, 40 insertions, 31 deletions
diff --git a/src/sliceitoff/display/__init__.py b/src/sliceitoff/display/__init__.py
index 296f519..25b6f18 100644
--- a/src/sliceitoff/display/__init__.py
+++ b/src/sliceitoff/display/__init__.py
@@ -1,8 +1,5 @@
+from .scaling import Scaling
from .display import Display
-from .static import (
- Scaling,
- INTERNAL_WIDTH,
- INTERNAL_HEIGHT,
- INTERNAL_ASPECT)
+from .static import INTERNAL_WIDTH, INTERNAL_HEIGHT
diff --git a/src/sliceitoff/display/display.py b/src/sliceitoff/display/display.py
index af61d49..ae98134 100644
--- a/src/sliceitoff/display/display.py
+++ b/src/sliceitoff/display/display.py
@@ -1,10 +1,6 @@
import pygame
-from .static import (
- Scaling,
- INTERNAL_WIDTH,
- INTERNAL_HEIGHT,
- INTERNAL_ASPECT)
+from .scaling import Scaling
class Display():
def __init__(self):
@@ -13,18 +9,7 @@ class Display():
self.screen = pygame.display.set_mode(
(mode_info.current_w, mode_info.current_h),
pygame.FULLSCREEN | pygame.SCALED )
-
- def get_scaling(self):
- w, h = self.screen.get_size()
- if w/h <= INTERNAL_ASPECT:
- scale = w / INTERNAL_WIDTH
- left = 0
- top = int((h - INTERNAL_HEIGHT * scale) / 2)
- else :
- scale = h / INTERNAL_HEIGHT
- left = int((w - INTERNAL_WIDTH * scale) / 2)
- top = 0
- return Scaling(scale, left, top)
+ Scaling.update_scaling(self.screen.get_size())
def update(self, groups = None):
""" Updates the screen: clear, blit gropus and flip """
diff --git a/src/sliceitoff/display/scaling.py b/src/sliceitoff/display/scaling.py
new file mode 100644
index 0000000..6be00da
--- /dev/null
+++ b/src/sliceitoff/display/scaling.py
@@ -0,0 +1,36 @@
+import pygame
+
+from .static import INTERNAL_WIDTH, INTERNAL_HEIGHT
+
+class Scaling():
+ factor = 0.02
+ left = 0
+ top = 0
+
+ @staticmethod
+ def area_to_rect(area: tuple) -> pygame.Rect:
+ """ converts area coordinates to pygame.Rect"""
+ return pygame.Rect(
+ area[0] * __class__.factor + __class__.left,
+ area[1] * __class__.factor + __class__.top,
+ area[2] * __class__.factor,
+ area[3] * __class__.factor)
+
+ @staticmethod
+ def scale_coordinates(coords: tuple ) -> tuple:
+ return (
+ coords[0] * __class__.factor + __class__.left,
+ coords[1] * __class__.factor + __class__.top)
+
+ @staticmethod
+ def update_scaling(size: tuple) -> None:
+ if size[0] / size[1] <= INTERNAL_WIDTH / INTERNAL_HEIGHT:
+ __class__.factor = size[0] / INTERNAL_WIDTH
+ __class__.left = 0
+ __class__.top = (size[1] - INTERNAL_HEIGHT * __class__.factor) // 2
+ else:
+ __class__.factor = size[1] / INTERNAL_HEIGHT
+ __class__.left = (size[0] - INTERNAL_WIDTH * __class__.factor) // 2
+ __class__.top = 0
+
+ \ No newline at end of file
diff --git a/src/sliceitoff/display/static.py b/src/sliceitoff/display/static.py
index 43dc385..6e5d0dc 100644
--- a/src/sliceitoff/display/static.py
+++ b/src/sliceitoff/display/static.py
@@ -1,11 +1,2 @@
-from dataclasses import dataclass
-
INTERNAL_WIDTH = 40000
INTERNAL_HEIGHT = 30000
-INTERNAL_ASPECT = INTERNAL_WIDTH / INTERNAL_HEIGHT
-
-@dataclass
-class Scaling():
- scale: float
- left: int
- top: int