Skip to content

Commit

Permalink
fix screen size issues, allow empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Conengmo committed Mar 17, 2024
1 parent 5cc3956 commit d7ef828
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
15 changes: 9 additions & 6 deletions battle_map_tv/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(
self,
scene: QGraphicsScene,
screen_size_px: tuple[int, int],
screen_size_mm: tuple[int, int],
screen_size_mm: Optional[tuple[int, int]],
window_size_px: tuple[int, int],
opacity: int,
):
Expand All @@ -34,8 +34,8 @@ def update_window_px(self, window_px: Tuple[int, int]):
self.window_size_px = window_px
self.reset()

def update_screen_mm(self, width_mm: int, height_mm: int):
self.screen_size_mm = (width_mm, height_mm)
def update_screen_mm(self, screen_size_mm: Optional[Tuple[int, int]]):
self.screen_size_mm = screen_size_mm
self.reset()

def update_opacity(self, opacity: int):
Expand All @@ -53,12 +53,15 @@ def reset(self):
self.group.setZValue(1)
self.scene.addItem(self.group)

pixels_per_inch_x = self.screen_size_px[0] / self.screen_size_mm[0] / mm_to_inch
pixels_per_inch_y = self.screen_size_px[1] / self.screen_size_mm[1] / mm_to_inch
if self.screen_size_mm is not None:
pixels_per_inch_x = self.screen_size_px[0] / self.screen_size_mm[0] / mm_to_inch
pixels_per_inch_y = self.screen_size_px[1] / self.screen_size_mm[1] / mm_to_inch
else:
pixels_per_inch_x, pixels_per_inch_y = 60, 60
n_lines_vertical = math.ceil(self.window_size_px[0] / pixels_per_inch_x)
n_lines_horizontal = math.ceil(self.window_size_px[1] / pixels_per_inch_y)
offset_x = (self.window_size_px[0] - ((n_lines_vertical - 1) * pixels_per_inch_x)) / 2
offset_y = (self.window_size_px[1] - ((n_lines_horizontal - 1) * pixels_per_inch_x)) / 2
offset_y = (self.window_size_px[1] - ((n_lines_horizontal - 1) * pixels_per_inch_y)) / 2

pen = QPen()
pen.setWidth(1)
Expand Down
23 changes: 16 additions & 7 deletions battle_map_tv/window_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from battle_map_tv.aoe import area_of_effect_shapes_to_class
from battle_map_tv.events import global_event_dispatcher, EventKeys
from battle_map_tv.storage import set_in_storage, StorageKeys, get_from_storage
from battle_map_tv.storage import set_in_storage, StorageKeys, get_from_storage, remove_from_storage
from battle_map_tv.ui_elements import (
StyledButton,
StyledLineEdit,
Expand Down Expand Up @@ -47,7 +47,7 @@ def __init__(
"""
)

self.screen_size_mm: Tuple[int, int] = (100, 100)
self.screen_size_mm: Optional[Tuple[int, int]] = None

self._superlayout = QHBoxLayout(self)
self._superlayout.setAlignment(Qt.AlignVCenter) # type: ignore[attr-defined]
Expand Down Expand Up @@ -179,19 +179,28 @@ def add_row_screen_size(self):
except KeyError:
pass
else:
assert self.screen_size_mm
screen_width_input.setText(str(self.screen_size_mm[0]))
screen_height_input.setText(str(self.screen_size_mm[1]))

def set_screen_size_callback():
width_str = screen_width_input.text()
height_str = screen_height_input.text()
if not width_str and not height_str:
remove_from_storage(StorageKeys.screen_size_mm)
self.screen_size_mm = None
self.image_window.update_screen_size_mm(self.screen_size_mm)
return

try:
width_mm = int(screen_width_input.text())
height_mm = int(screen_height_input.text())
width_mm = int(width_str)
height_mm = int(height_str)
except ValueError:
pass
else:
set_in_storage(StorageKeys.screen_size_mm, (width_mm, height_mm))
if self.image_window.grid is not None:
self.image_window.grid.update_screen_mm(width_mm, height_mm)
self.screen_size_mm = (width_mm, height_mm)
set_in_storage(StorageKeys.screen_size_mm, self.screen_size_mm)
self.image_window.update_screen_size_mm(self.screen_size_mm)

button = StyledButton("Set")
button.clicked.connect(set_screen_size_callback)
Expand Down
6 changes: 5 additions & 1 deletion battle_map_tv/window_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def restore_image(self):
self.remove_image()
self.add_image(image_path=previous_image)

def add_grid(self, screen_size_mm: Tuple[int, int], opacity: int):
def add_grid(self, screen_size_mm: Optional[tuple[int, int]], opacity: int):
if self.grid is not None:
self.remove_grid()
self.grid = Grid(
Expand All @@ -75,6 +75,10 @@ def add_grid(self, screen_size_mm: Tuple[int, int], opacity: int):
opacity=opacity,
)

def update_screen_size_mm(self, screen_size_mm: Optional[Tuple[int, int]]):
if self.grid is not None:
self.grid.update_screen_mm(screen_size_mm)

def remove_grid(self):
if self.grid is not None:
self.grid.delete()
Expand Down

0 comments on commit d7ef828

Please sign in to comment.