forked from pythonarcade/arcade
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_orthographic_projector.py
More file actions
186 lines (142 loc) · 5.74 KB
/
Copy pathtest_orthographic_projector.py
File metadata and controls
186 lines (142 loc) · 5.74 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import pytest as pytest
from arcade import camera, Window
def test_orthographic_projector_use(window: Window):
# Given
ortho_camera = camera.OrthographicProjector()
view_matrix = ortho_camera._generate_view_matrix()
proj_matrix = ortho_camera._generate_projection_matrix()
# When
ortho_camera.use()
# Then
assert window.current_camera is ortho_camera
assert window.ctx.view_matrix == view_matrix
assert window.ctx.projection_matrix == proj_matrix
# Reset the window for later tests
window.default_camera.use()
def test_orthographic_projector_activate(window: Window):
# Given
ortho_camera: camera.OrthographicProjector = camera.OrthographicProjector()
view_matrix = ortho_camera._generate_view_matrix()
proj_matrix = ortho_camera._generate_projection_matrix()
# When
with ortho_camera.activate() as cam:
# Initially
assert window.current_camera is cam is ortho_camera
assert window.ctx.view_matrix == view_matrix
assert window.ctx.projection_matrix == proj_matrix
# Finally
assert window.current_camera is window.default_camera
# Reset the window for later tests
window.default_camera.use()
@pytest.mark.parametrize("width, height", [(800, 600), (1280, 720), (500, 500)])
def test_orthographic_projector_map_coordinates(window: Window, width, height):
# Given
window.set_size(width, height)
ortho_camera = camera.OrthographicProjector()
# When
mouse_pos_a = (100.0, 100.0)
mouse_pos_b = (100.0, 0.0)
mouse_pos_c = (230.0, 800.0)
# Then
assert ortho_camera.map_screen_to_world_coordinate(mouse_pos_a) == pytest.approx((100.0, 100.0, 0.0))
assert ortho_camera.map_screen_to_world_coordinate(mouse_pos_b) == pytest.approx((100.0, 0.0, 0.0))
assert ortho_camera.map_screen_to_world_coordinate(mouse_pos_c) == pytest.approx((230.0, 800.0, 0.0))
@pytest.mark.parametrize("width, height", [(800, 600), (1280, 720), (500, 500)])
def test_orthographic_projector_map_coordinates_move(window: Window, width, height):
# Given
window.set_size(width, height)
ortho_camera = camera.OrthographicProjector()
default_view = ortho_camera.view
half_width, half_height = window.width//2, window.height//2
mouse_pos_a = (half_width, half_height)
mouse_pos_b = (100.0, 100.0)
# When
default_view.position = (0.0, 0.0, 0.0)
# Then
assert ortho_camera.map_screen_to_world_coordinate(mouse_pos_a) == pytest.approx((0.0, 0.0, 0.0))
assert (
ortho_camera.map_screen_to_world_coordinate(mouse_pos_b)
==
pytest.approx((-half_width+100.0, -half_height+100, 0.0))
)
# And
# When
default_view.position = (100.0, 100.0, 0.0)
# Then
assert ortho_camera.map_screen_to_world_coordinate(mouse_pos_a) == pytest.approx((100.0, 100.0, 0.0))
assert (
ortho_camera.map_screen_to_world_coordinate(mouse_pos_b)
==
pytest.approx((-half_width+200.0, -half_height+200.0, 0.0))
)
@pytest.mark.parametrize("width, height", [(800, 600), (1280, 720), (500, 500)])
def test_orthographic_projector_map_coordinates_rotate(window: Window, width, height):
# Given
window.set_size(width, height)
ortho_camera = camera.OrthographicProjector()
default_view = ortho_camera.view
half_width, half_height = window.width//2, window.height//2
mouse_pos_a = (half_width, half_height)
mouse_pos_b = (100.0, 100.0)
# When
default_view.up = (1.0, 0.0, 0.0)
default_view.position = (0.0, 0.0, 0.0)
# Then
assert ortho_camera.map_screen_to_world_coordinate(mouse_pos_a) == pytest.approx((0.0, 0.0, 0.0))
assert (
ortho_camera.map_screen_to_world_coordinate(mouse_pos_b)
==
pytest.approx((-half_height+100.0, half_width-100.0, 0.0))
)
# And
# When
default_view.up = (2.0**-0.5, 2.0**-0.5, 0.0)
default_view.position = (100.0, 100.0, 0.0)
b_shift_x = -half_width + 100.0
b_shift_y = -half_height + 100.0
b_rotated_x = b_shift_x / (2.0**0.5) + b_shift_y / (2.0**0.5) + 100
b_rotated_y = -b_shift_x / (2.0**0.5) + b_shift_y / (2.0**0.5) + 100
# Then
assert ortho_camera.map_screen_to_world_coordinate(mouse_pos_a) == pytest.approx((100.0, 100.0, 0.0))
assert (
ortho_camera.map_screen_to_world_coordinate(mouse_pos_b)
==
pytest.approx((b_rotated_x, b_rotated_y, 0.0))
)
@pytest.mark.parametrize("width, height", [(800, 600), (1280, 720), (500, 500)])
def test_orthographic_projector_map_coordinates_zoom(window: Window, width, height):
# Given
window.set_size(width, height)
ortho_camera = camera.OrthographicProjector()
default_view = ortho_camera.view
half_width, half_height = window.width//2, window.height//2
mouse_pos_a = (window.width, window.height)
mouse_pos_b = (100.0, 100.0)
# When
default_view.zoom = 2.0
# Then
assert (
ortho_camera.map_screen_to_world_coordinate(mouse_pos_a)
==
pytest.approx((window.width*0.75, window.height*0.75, 0.0))
)
assert (
ortho_camera.map_screen_to_world_coordinate(mouse_pos_b)
==
pytest.approx((half_width + (100 - half_width)*0.5, half_height + (100 - half_height)*0.5, 0.0))
)
# And
# When
default_view.position = (0.0, 0.0, 0.0)
default_view.zoom = 0.25
# Then
assert (
ortho_camera.map_screen_to_world_coordinate(mouse_pos_a)
==
pytest.approx((window.width*2.0, window.height*2.0, 0.0))
)
assert (
ortho_camera.map_screen_to_world_coordinate(mouse_pos_b)
==
pytest.approx(((100 - half_width)*4.0, (100 - half_height)*4.0, 0.0))
)