Skip to content

Commit a84999b

Browse files
committed
introducing screen_to_world and world_to_screen in scrolling
1 parent 2c5b7d4 commit a84999b

6 files changed

Lines changed: 53 additions & 46 deletions

File tree

cocos/layer/scrolling.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565

6666
__docformat__ = 'restructuredtext'
6767

68+
import warnings
69+
6870
from cocos.director import director
6971
from .base_layers import Layer
7072
import pyglet
@@ -74,10 +76,12 @@
7476
class ScrollableLayer(Layer):
7577
"""Layer that supports scrolling.
7678
77-
If ``px_width`` (resp ``px_height``) is defined, scrolling will be limited
78-
to only show areas with origin_x <= x < = px_width (resp
79+
If ``px_width`` is defined, then ``px_height`` must also be defined; scrolling
80+
will be limited to only show areas with origin_x <= x < = px_width and
7981
origin_y <= y <= px_height).
8082
83+
If ``px_width`` is not defined, then the layer will not limit the scrolling.
84+
8185
A layer may have a ``parallax`` value which is used to scale the position
8286
(and not the dimensions) of the view for the layer - the layer's view
8387
(x, y) coordinates are calculated as::
@@ -291,7 +295,14 @@ def add(self, child, z=0, name=None):
291295
self.set_focus(self.fx, self.fy, force=True)
292296

293297
def pixel_from_screen(self, x, y):
294-
"""Look up the Layer-space pixel matching the screen-space pixel.
298+
"""deprecated, was renamed as screen_to_world"""
299+
warnings.warn("Cocos Deprecation Warning: ScrollingManager.pixel_from_screen "
300+
"was renamed to Scrolling Manager.screen_to_world; the"
301+
" former will disappear in future cocos releases")
302+
return self.screen_to_world(x, y)
303+
304+
def screen_to_world(self, x, y):
305+
"""Translates screen coordinates to world coordinates.
295306
296307
Account for viewport, layer and screen transformations.
297308
@@ -300,7 +311,7 @@ def pixel_from_screen(self, x, y):
300311
y (int): y coordinate in screen space
301312
302313
Returns:
303-
tuple[int, int]: coordinates in map-space
314+
tuple[int, int]: coordinates in world-space
304315
"""
305316
# director display scaling
306317
if director.autoscale:
@@ -322,13 +333,20 @@ def pixel_from_screen(self, x, y):
322333
return int(vx + sx * w), int(vy + sy * h)
323334

324335
def pixel_to_screen(self, x, y):
325-
"""Look up the screen-space pixel matching the Layer-space pixel.
336+
"""deprecated, was renamed as world_to_screen"""
337+
warnings.warn("Cocos Deprecation Warning: ScrollingManager.pixel_to_screen "
338+
"was renamed to Scrolling Manager.world_to_screen; the"
339+
" former will disappear in future cocos releases")
340+
return self.world_to_screen(x, y)
341+
342+
def world_to_screen(self, x, y):
343+
"""Translates world coordinates to screen coordinates.
326344
327345
Account for viewport, layer and screen transformations.
328346
329347
Arguments:
330-
x (int): x coordinate in map space
331-
y (int): y coordinate in map space
348+
x (int): x coordinate in world space
349+
y (int): y coordinate in world space
332350
333351
Returns:
334352
tuple[int, int]: coordinates in screen space
@@ -338,12 +356,11 @@ def pixel_to_screen(self, x, y):
338356
return int(screen_x), int(screen_y)
339357

340358
def set_focus(self, fx, fy, force=False):
341-
"""Determine the viewport based on a desired focus pixel in the
342-
Layer space (fx, fy) and honoring any bounding restrictions of
343-
child layers.
359+
"""Makes the point (fx, fy) show as near the view's center as possible.
344360
345-
The focus will always be shifted to ensure no child layers display
346-
out-of-bounds data, as defined by their dimensions ``px_width`` and ``px_height``.
361+
Changes his children so that the point (fx, fy) in world coordinates
362+
will be seen as near the view center as possible, while at the
363+
same time not displaying out-of-bounds areas in the children.
347364
348365
Args:
349366
fx (int): the focus point x coordinate

samples/mouse_elastic_box_selection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# -*- coding: cp1252 -*-
1+
# -*- coding: utf-8 -*-
22
"""
33
Interactive test for CollisionManager.objs_into_box
44
Implements click selection plus elastic box selection
@@ -601,8 +601,8 @@ def on_enter(self):
601601
scene.add(scrolling_manager)
602602
playview = Worldview(**consts['world'])
603603
scrolling_manager.add(playview, z=0)
604-
world_to_screen = scrolling_manager.pixel_to_screen
605-
screen_to_world = scrolling_manager.pixel_from_screen
604+
world_to_screen = scrolling_manager.world_to_screen
605+
screen_to_world = scrolling_manager.screen_to_world
606606
editor = EditLayer(scrolling_manager, playview, **consts['edit'])
607607
scene.add(editor)
608608
director.run(scene)

test/test_scrolling_manager_without_tiles.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ def on_key_release(self, k, m ):
137137
def on_mouse_press(self,x,y,button,modifiers):
138138
# test from screen coords
139139
print('on_mouse_press:')
140-
vx, vy = self.scroller.pixel_from_screen(x,y)
141-
print('\tpixel_from_screen(x, y):', vx, vy)
140+
vx, vy = self.scroller.screen_to_world(x,y)
141+
print('\tscreen_to_world(x, y):', vx, vy)
142142

143143
def clamp(self, actor, new_pos):
144144
x,y = new_pos
@@ -182,7 +182,7 @@ def update_after_change(self):
182182

183183
def refresh_marks(self):
184184
for mark, position in zip(self.scene.marks, self.marks_positions):
185-
screen_pos = self.scroller.pixel_to_screen(*position)
185+
screen_pos = self.scroller.world_to_screen(*position)
186186
mark.position = screen_pos
187187

188188
def teleport_player(self, x, y):
@@ -220,11 +220,6 @@ def show_common_text():
220220
use arrows to move, +- in the main keyboard (not the keypad) for zoom,
221221
ctrl-f to toggle fullscreen status.
222222
223-
You will need grossinis_sister1.png , to be found in the test directory.
224-
225-
You will need cocos r966+ ( for 'cocos_on_resize' event and supresion of
226-
NotImplementedError in pixel_to_screen)
227-
228223
For clarity set view_width, view_height to respect your desktop aspect
229224
ratio; look near the script begin
230225
@@ -253,17 +248,17 @@ def show_mode_1_text():
253248
zoom in or out using key '+' or '-', not so much that all the world shows
254249
in a single screen, then repeat 1), 2)
255250
256-
4. consistency screen to world coordinates conversion(aka pixel_from_screen):
251+
4. consistency screen to world coordinates conversion:
257252
a. restart the script, mouse click the inner bottom left corner, do ctrl-f
258253
click again over the inner bottom left corner, zoom a little, click again
259254
over the the inner bottom left corner.
260-
Now look at the console: the pixel_from_screen values must all be near a
255+
Now look at the console: the screen_to_world values must all be near a
261256
common value.
262257
263258
b. restart the script, scroll to the top right corner, repeat a. replacing
264259
'botton left' by 'top left'
265260
266-
5. world to screen coordinate changes (aka pixel_to_screen) correctness:
261+
5. world to screen coordinate changes correctness:
267262
restart the script; move a bit. Look at the lower left corners in the lower
268263
row of squares; you should see a small square in the shades of green.
269264
moving, zomming, resizing (ctrl-f) should not alter the relative position of
@@ -303,17 +298,17 @@ def show_mode_2_text():
303298
zoom in or out using key '+' or '-', not so much that all the world shows
304299
in a single screen, then repeat 1), 2)
305300
306-
4. consistency screen to world coordinates conversion(aka pixel_from_screen):
301+
4. consistency screen to world coordinates conversion:
307302
a. restart the script, mouse click the inner bottom left corner, do ctrl-f
308303
click again over the inner bottom left corner, zoom a litle, click again
309304
over the the inner bottom left corner.
310-
Now look at the console: the pixel_from_screen values must all be near a
305+
Now look at the console: the screen_to_world values must all be near a
311306
common value.
312307
313308
b. restart the script, scroll to the top right corner, repeat a. replacing
314309
'botton left' by 'top left'
315310
316-
5. world to screen coordinate changes (aka pixel_to_screen) correctness:
311+
5. world to screen coordinate changes correctness:
317312
restart the script; move a bit. Look at the lower left corners in the lower
318313
row of squares; you should see a small square in the shades of green.
319314
moving, zomming, resizing (ctrl-f) should not alter the relative position of

test/test_scrolling_manager_without_tiles_autoscale.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ def on_key_release(self, k, m ):
136136
def on_mouse_press(self,x,y,button,modifiers):
137137
# test from screen coords
138138
print('on_mouse_press:')
139-
vx, vy = self.scroller.pixel_from_screen(x,y)
140-
print('\tpixel_from_screen(x, y):', vx, vy)
139+
vx, vy = self.scroller.screen_to_world(x,y)
140+
print('\tscreen_to_world(x, y):', vx, vy)
141141

142142
def clamp(self, actor, new_pos):
143143
x,y = new_pos
@@ -181,7 +181,7 @@ def update_after_change(self):
181181

182182
def refresh_marks(self):
183183
for mark, position in zip(self.scene.marks, self.marks_positions):
184-
screen_pos = self.scroller.pixel_to_screen(*position)
184+
screen_pos = self.scroller.world_to_screen(*position)
185185
mark.position = screen_pos
186186

187187
def teleport_player(self, x, y):
@@ -219,11 +219,6 @@ def show_common_text():
219219
use arrows to move, +- in the main keyboard (not the keypad) for zoom,
220220
ctrl-f to toggle fullscreen status.
221221
222-
You will need grossinis_sister1.png , to be found in the test directory.
223-
224-
You will need cocos r966+ ( for 'cocos_on_resize' event and supresion of
225-
NotImplementedError in pixel_to_screen)
226-
227222
For clarity set view_width, view_height to respect your desktop aspect
228223
ratio; look near the script begin
229224
@@ -252,17 +247,17 @@ def show_mode_1_text():
252247
zoom in or out using key '+' or '-', not so much that all the world shows
253248
in a single screen, then repeat 1), 2)
254249
255-
4. consistency screen to world coordinates conversion(aka pixel_from_screen):
250+
4. consistency screen to world coordinates conversion:
256251
a. restart the script, mouse click the inner bottom left corner, do ctrl-f
257252
click again over the inner bottom left corner, zoom a little, click again
258253
over the the inner bottom left corner.
259-
Now look at the console: the pixel_from_screen values must all be near a
254+
Now look at the console: the screen_to_world values must all be near a
260255
common value.
261256
262257
b. restart the script, scroll to the top right corner, repeat a. replacing
263258
'botton left' by 'top left'
264259
265-
5. world to screen coordinate changes (aka pixel_to_screen) correctness:
260+
5. world to screen coordinate changes correctness:
266261
restart the script; move a bit. Look at the lower left corners in the lower
267262
row of squares; you should see a small square in the shades of green.
268263
moving, zomming, resizing (ctrl-f) should not alter the relative position of
@@ -302,17 +297,17 @@ def show_mode_2_text():
302297
zoom in or out using key '+' or '-', not so much that all the world shows
303298
in a single screen, then repeat 1), 2)
304299
305-
4. consistency screen to world coordinates conversion(aka pixel_from_screen):
300+
4. consistency screen to world coordinates conversion:
306301
a. restart the script, mouse click the inner bottom left corner, do ctrl-f
307302
click again over the inner bottom left corner, zoom a litle, click again
308303
over the the inner bottom left corner.
309-
Now look at the console: the pixel_from_screen values must all be near a
304+
Now look at the console: the screen_to_world values must all be near a
310305
common value.
311306
312307
b. restart the script, scroll to the top right corner, repeat a. replacing
313308
'botton left' by 'top left'
314309
315-
5. world to screen coordinate changes (aka pixel_to_screen) correctness:
310+
5. world to screen coordinate changes correctness:
316311
restart the script; move a bit. Look at the lower left corners in the lower
317312
row of squares; you should see a small square in the shades of green.
318313
moving, zomming, resizing (ctrl-f) should not alter the relative position of

test/test_tmx_hexmap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def on_key_press(key, modifier):
8383
def on_mouse_motion(x, y, dx, dy ):
8484
global scroller, old_ij, old_cell, old_highlighted_color
8585
#vh, vy = director.get_virtual_coordinates(x, y)
86-
vx, vy = scroller.pixel_from_screen(x,y)
86+
vx, vy = scroller.screen_to_world(x,y)
8787
ij = test_layer.get_key_at_pixel(vx, vy)
8888
if ij == old_ij:
8989
return

tools/editor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def on_mouse_release(self, x, y, buttons, modifiers):
104104
m = self.selector.map_layer
105105

106106
# get the cell and cell coordinate
107-
x, y = self.manager.pixel_from_screen(x, y)
107+
x, y = self.manager.screen_to_world(x, y)
108108
cell = m.get_at_pixel(x, y)
109109
if not cell:
110110
# click not in map
@@ -121,7 +121,7 @@ def on_mouse_release(self, x, y, buttons, modifiers):
121121

122122
def on_mouse_motion(self, x, y, dx, dy):
123123
m = self.selector.map_layer
124-
cell = m.get_at_pixel(*self.manager.pixel_from_screen(x, y))
124+
cell = m.get_at_pixel(*self.manager.screen_to_world(x, y))
125125
if not cell:
126126
self.highlight = None
127127
return True

0 commit comments

Comments
 (0)