Fix and clean display

This commit is contained in:
timofej 2023-08-17 13:05:15 +02:00
parent 7c488d2892
commit 9287e2de10

View File

@ -33,7 +33,7 @@ class PygameEngine:
By initializing pygame lazy the dependency can be dropped. By initializing pygame lazy the dependency can be dropped.
""" """
def __init__(self, window_size, top, *args, **kwargs): def __init__(self, window_size, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
import pygame import pygame
self._pygame = pygame self._pygame = pygame
@ -43,7 +43,7 @@ class PygameEngine:
self.__font = pygame.font.SysFont("monospace", 15) self.__font = pygame.font.SysFont("monospace", 15)
self._width = window_size[0] self._width = window_size[0]
self._height = window_size[1] + top self._height = window_size[1]
def write_text(self, pos, text, color=(0, 255, 0)): def write_text(self, pos, text, color=(0, 255, 0)):
label = self.__font.render(text, True, color) label = self.__font.render(text, True, color)
@ -65,10 +65,10 @@ class PygameEngine:
class Simulate2Layers: class Simulate2Layers:
def __init__(self, def __init__(self,
cellular_automaton: Neuropercolation, dim,
window_size=(1000, 800), eps,
stretch_cells=False, res=10,
draw_engine=None, draw='pygame',
state_to_color_cb=None, state_to_color_cb=None,
*args, **kwargs): *args, **kwargs):
""" """
@ -81,10 +81,13 @@ class Simulate2Layers:
:param state_to_color_cb: A callback to define the draw color of CA states (default: red for states != 0) :param state_to_color_cb: A callback to define the draw color of CA states (default: red for states != 0)
""" """
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._cellular_automaton = cellular_automaton self._cellular_automaton = Neuropercolation(dim,eps)
self.__rect = _Rect(left=0, top=30, width=window_size[0], height=window_size[1]) self.__cell_size = [res,res]
self.__calculate_cell_display_size(stretch_cells) self.__gridside = dim*res
self.__draw_engine = PygameEngine(window_size, self.__rect.top) if draw_engine is None else draw_engine self.__samegap = 10
self.__othergap = 20
self.__rect = _Rect(left=0, top=30, width=dim*res, height=2*dim*res+self.__samegap)
self.__draw_engine = PygameEngine((self.__rect.width,self.__rect.height+self.__rect.top)) if draw == 'pygame' else draw
self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb
def run(self, def run(self,
@ -123,15 +126,6 @@ class Simulate2Layers:
def _not_at_the_end(self, last_evolution_step): def _not_at_the_end(self, last_evolution_step):
return (self._cellular_automaton.evolution_step < last_evolution_step or last_evolution_step <= 0) return (self._cellular_automaton.evolution_step < last_evolution_step or last_evolution_step <= 0)
def __calculate_cell_display_size(self, stretch_cells): # pragma: no cover
grid_dimension = self._cellular_automaton.dimension
if stretch_cells:
self.__cell_size = [(self.__rect.width) / (grid_dimension[0]),
(self.__rect.height) / (grid_dimension[1]*2)]
else:
self.__cell_size = [int((self.__rect.width) / (grid_dimension[0])),
int((self.__rect.height) / (grid_dimension[1]*2))]
def _redraw_dirty_cells(self): def _redraw_dirty_cells(self):
self.__draw_engine.update_rectangles(list(self.__redraw_dirty_cells())) self.__draw_engine.update_rectangles(list(self.__redraw_dirty_cells()))
@ -158,7 +152,7 @@ class Simulate2Layers:
map(operator.mul, map(operator.mul,
self.__cell_size, self.__cell_size,
coord[:2]), coord[:2]),
[0,(self.__rect.height/2+4)*(coord[2])])) [0,(self.__gridside+self.__samegap)*(coord[2])]))
def __calculate_cell_position_on_screen(self, cell_pos): def __calculate_cell_position_on_screen(self, cell_pos):
return [self.__rect.left + cell_pos[0], self.__rect.top + cell_pos[1]] return [self.__rect.left + cell_pos[0], self.__rect.top + cell_pos[1]]
@ -177,10 +171,10 @@ class Simulate2Layers:
class Simulate4Layers: class Simulate4Layers:
def __init__(self, def __init__(self,
cellular_automaton: NeuropercolationCoupled, dim,
window_size=(1000, 800), eps,
stretch_cells=False, res=10,
draw_engine=None, draw='pygame',
state_to_color_cb=None, state_to_color_cb=None,
*args, **kwargs): *args, **kwargs):
""" """
@ -193,10 +187,13 @@ class Simulate4Layers:
:param state_to_color_cb: A callback to define the draw color of CA states (default: red for states != 0) :param state_to_color_cb: A callback to define the draw color of CA states (default: red for states != 0)
""" """
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._cellular_automaton = cellular_automaton self._cellular_automaton = NeuropercolationCoupled(dim,eps)
self.__rect = _Rect(left=0, top=30, width=window_size[0], height=window_size[1]) self.__cell_size = [res,res]
self.__calculate_cell_display_size(stretch_cells) self.__gridside = dim*res
self.__draw_engine = PygameEngine(window_size, self.__rect.top) if draw_engine is None else draw_engine self.__samegap = 10
self.__othergap = 20
self.__rect = _Rect(left=0, top=30, width=2*dim*res+self.__othergap, height=2*dim*res+self.__samegap)
self.__draw_engine = PygameEngine((self.__rect.width,self.__rect.height+self.__rect.top)) if draw == 'pygame' else draw
self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb
def run(self, def run(self,
@ -235,15 +232,6 @@ class Simulate4Layers:
def _not_at_the_end(self, last_evolution_step): def _not_at_the_end(self, last_evolution_step):
return (self._cellular_automaton.evolution_step < last_evolution_step or last_evolution_step <= 0) return (self._cellular_automaton.evolution_step < last_evolution_step or last_evolution_step <= 0)
def __calculate_cell_display_size(self, stretch_cells): # pragma: no cover
grid_dimension = self._cellular_automaton.dimension
if stretch_cells:
self.__cell_size = [(self.__rect.width) / (grid_dimension[0]*2),
(self.__rect.height) / (grid_dimension[1]*2)]
else:
self.__cell_size = [int((self.__rect.width) / (grid_dimension[0]*2)),
int((self.__rect.height) / (grid_dimension[1]*2))]
def _redraw_dirty_cells(self): def _redraw_dirty_cells(self):
self.__draw_engine.update_rectangles(list(self.__redraw_dirty_cells())) self.__draw_engine.update_rectangles(list(self.__redraw_dirty_cells()))
@ -270,8 +258,8 @@ class Simulate4Layers:
map(operator.mul, map(operator.mul,
self.__cell_size, self.__cell_size,
coord[:2]), coord[:2]),
[(self.__rect.width/2)*(coord[3]), [(self.__gridside+self.__othergap)*(coord[3]),
(self.__rect.height/2)*(coord[2])])) (self.__gridside+self.__samegap)*(coord[2])]))
def __calculate_cell_position_on_screen(self, cell_pos): def __calculate_cell_position_on_screen(self, cell_pos):
return [self.__rect.left + cell_pos[0], self.__rect.top + cell_pos[1]] return [self.__rect.left + cell_pos[0], self.__rect.top + cell_pos[1]]