-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewPort.py
More file actions
138 lines (85 loc) · 3.34 KB
/
ViewPort.py
File metadata and controls
138 lines (85 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'''
* Viewport.py
*
* Created on: 13.11.2018
* last modified on: -
* Author: Andrew Jason Bishop
*
* General description:
* xxx
'''
from Enums import *
import tkinter as tk
import colors as col
import numpy as np
class ViewPort(tk.Canvas):
def __init__(self, _parent, _creator, _evaluator):
super().__init__( _parent )
self.parent = _parent
self.envirCreator = _creator
self.envirEvaluator = _evaluator
self.centerPortCoord = (0+0j)
self.halfPortCoordRange = (20+20j)
self.cellWidth_px = 24
self.cellMargin_px = 2
self.x_limit = 2 * int(self.halfPortCoordRange.real) + 1
self.y_limit = 2 * int(self.halfPortCoordRange.imag) + 1
self.setSize()
self.bind("<Button-3>", self.parent.switchState)
def setSize(self):
width_x = self.x_limit * (self.cellWidth_px + self.cellMargin_px)
width_y = self.y_limit * (self.cellWidth_px + self.cellMargin_px)
self.config(width=width_x, height=width_y)
def update_viewPort(self):
self.clear_viewPort()
self.scan_Port_and_draw_Cells()
self.update()
def clear_viewPort(self):
self.delete( tk.ALL )
def scan_Port_and_draw_Cells(self):
for y in range(0, self.y_limit):
for x in range(0, self.x_limit):
portCoord = complex(x, y)
self.update_portCell( portCoord )
def update_portCell(self, _portCoord):
cellCoord = self.portCoord_to_cellCoord( _portCoord )
cellType = self.envirEvaluator.getCellType( cellCoord )
if cellType:
self.draw_portCell( cellType, _portCoord )
def portCoord_to_cellCoord(self, _portCoord):
return ( self.centerPortCoord
- self.halfPortCoordRange
+ _portCoord )
def draw_portCell(self, _cellType, _portCoord):
fillCol = self.determine_fillColor( _cellType )
pxCoords = self.calculate_pxCoords( _portCoord )
self.create_rectangle( pxCoords,
fill = fillCol,
width=0) # no border
def determine_fillColor(self, _cellType):
if (CellType.DEAD == _cellType):
return col.gy2
else:
return col.gn
def calculate_pxCoords(self, _portCoord):
cw = self.cellWidth_px
cm = self.cellMargin_px
upperLeft_corner_px = _portCoord * (cw+cm) + (cm+cm*1j)
x0 = upperLeft_corner_px.real; y0 = upperLeft_corner_px.imag
x1 = x0+cw; y1 = y0+cw
return (x0, y0, x1, y1)
def new_lifeCell(self, _event): # TODO
cellCoord = self.coords_for_new_lifeCell( (_event.x, _event.y) )
self.envirCreator.create_initial_LiveCell( cellCoord )
self.update_viewPort()
def coords_for_new_lifeCell(self, _pxCoord ):
portCoord = self.pxCoord_to_portCoord( _pxCoord )
cellCoord = self.portCoord_to_cellCoord( portCoord )
return cellCoord
def pxCoord_to_portCoord(self, _pxCoord):
cw = self.cellWidth_px
cm = self.cellMargin_px
port_x = np.floor( _pxCoord[0] / (cw + cm) )
port_y = np.floor( _pxCoord[1] / (cw + cm) )
return complex(port_x, port_y)
''' END '''