Skip to content

Commit

Permalink
Merge pull request #125 from naraesk/master
Browse files Browse the repository at this point in the history
#51 Positioning relative to other slide
  • Loading branch information
regebro committed May 2, 2017
2 parents 71210c1 + 27f2a2b commit 98ecc42
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ Changes
2.4 (unreleased)
----------------

- #51: Positioning relative to other slide [naraesk]

- Removed the code that uses pkg_util to access included templates. We don't
support installing Hovercraft as a ZIP file anyway, so it only complicates
things for no good reason.



2.3 (2017-04-12)
----------------

Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ Other contributors (see CHANGES.txt for details):

* Adam Johnson [adamchainz]

* David Baum [naraesk]

.. _impress.js: http://github.com/bartaz/impress.js
.. _demo: http://regebro.github.com/hovercraft
.. _readthedocs.org: https://hovercraft.readthedocs.io/
Expand Down
21 changes: 18 additions & 3 deletions docs/examples/positions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ So here we rotated 90 degrees and zoomed out five times.
:data-scale: 1
:data-x: 4000
:data-y: 2000
:id: positions_last_slide

Relative positions
==================
Relative positions to last slide
================================

One thing that *is* a problem is the absolute positioning. All the positions
we used so far above are in relation to the start of the coordinate system.
Expand All @@ -105,6 +106,8 @@ relative coordinate to the last slide.

:data-x: r1000

----

Like this
=========

Expand All @@ -121,12 +124,23 @@ to use relative positioning.

----

:data-y: positions_last_slide+1000

Relative positions to any slide
===============================

You can reference any *previous* slide by its id and specify the position relative to it.
This will work for all fields.
However, you should not use ``r`` as a slide id since the positioning might not behave as you expect.

----

:data-rotate: r15

Automatic positioning
=====================

Every field will retain it's last value if you don't specify a new one.
Every field will retain its last value if you don't specify a new one.
In this case, we keep a r1000 value for data-x and introduce a new
r15 value for data-rotate. This and the next slide will therefore
move right 1000 pixels and rotate 15 degrees more for each slide.
Expand All @@ -135,6 +149,7 @@ It looks like it moves "up" because we are already rotated 90 degrees.

----

:data-x: r1000
:data-scale: 0.15

**A warning!**
Expand Down
13 changes: 9 additions & 4 deletions docs/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ There are four ways to position slides:
#. Absolute positioning: You simply add X and Y coordinates to a slide,
in pixels. Doing only this will not be fun, but someone might need it.

#. Relative positioning: By specifying x and/or y with with a starting r,
you specify the distance from the previous slide. By using this form of
positioning you can insert a slide, and the other slides will just move
to make space for the new slide.
#. Relative positioning to last slide: By specifying x and/or y with with
a starting r,you specify the distance from the previous slide. By using
this form of positioning you can insert a slide, and the other slides
will just move to make space for the new slide.

#. Relative positiong to any slide: You can reference any *previous* slide
by its id and specify the position relative to it. This will work for
all positioning fields. However, you should not use ``r`` as a slide id
since the positioning might not behave as you expect.

#. Automatically: If you don’t specify any position the slide will have the
same settings as the previous slide. With a relative positioning, this
Expand Down
33 changes: 27 additions & 6 deletions hovercraft/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,21 @@ def _update_position(pos1, pos2):
for key in POSITION_ATTRIBS:
val = pos2.get(key)
if val is not None:
if val[0] == 'r':
# Relative movement
newval = pos1[key] + num(val[1:])
plus = val.find("+")
minus = val.find("-")
if plus > -1:
newval = num(val[plus+1:])
pos1[key + "-rel"] = val[0:plus]
elif minus > -1 and not val.startswith("r-"):
newval = num(val[minus:])
pos1[key + "-rel"] = val[0:minus]
else:
newval = num(val)
if val[0] == 'r':
# Relative movement
newval = pos1[key] + num(val[1:])
else:
newval = num(val)
pos1.pop(key+"-rel", None)
pos1[key] = newval


Expand Down Expand Up @@ -211,15 +221,26 @@ def update_positions(tree, positions):

for step, pos in zip(tree.findall('step'), positions):
for key in sorted(pos):
step.attrib[key] = str(pos[key])
value = pos.get(key)
if key.endswith("-rel"):
abs_key = key[:key.index("-rel")]
if value is not None:
els = tree.findall(".//*[@id='" + value + "']")
for el in els :
pos[abs_key] = num(el.get(abs_key)) + pos.get(abs_key)
print(num(el.get(abs_key)))
print(pos.get(abs_key))
step.attrib[abs_key] = str(pos.get(abs_key))
else:
step.attrib[key] = str(pos[key])

if 'hovercraft-path' in step.attrib:
del step.attrib['hovercraft-path']


def position_slides(tree):
"""Position the slides in the tree"""

positions = gather_positions(tree)
positions = calculate_positions(positions)
update_positions(tree, positions)
13 changes: 13 additions & 0 deletions hovercraft/tests/test_data/positioning.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,18 @@ Move in Z and rotate. Path continues to be used.

:data-x: 3000
:data-y: 1000
:id: firstID

Explicit position


----

:id: secondID
:data-x: firstID+1000
:data-y: firstID-500

----

:data-x: secondID+800
:data-y: 200
14 changes: 14 additions & 0 deletions hovercraft/tests/test_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ def test_gathering(self):
{'data-x': '3000', 'data-y': '1000', 'data-z': 'r0',
'data-rotate-x': 'r0', 'data-rotate-y': 'r0',
'data-rotate-z': 'r0', 'data-scale': 'r0', 'is_path': False},
{'data-x': 'firstID+1000', 'data-y': 'firstID-500', 'data-z': 'r0',
'data-rotate-x': 'r0', 'data-rotate-y': 'r0',
'data-rotate-z': 'r0', 'data-scale': 'r0', 'is_path': False},
{'data-x': 'secondID+800', 'data-y': '200', 'data-z': 'r0',
'data-rotate-x': 'r0', 'data-rotate-y': 'r0',
'data-rotate-z': 'r0', 'data-scale': 'r0', 'is_path': False}
])


Expand Down Expand Up @@ -390,6 +396,14 @@ def test_complete(self):
{'data-x': '3000', 'data-y': '1000', 'data-z': '1000',
'data-rotate-x': '180', 'data-rotate-y': '0',
'data-rotate-z': '90.0', 'data-scale': '1'},
# Positioning relative to other slide by id
{'data-x': '4000', 'data-y': '500', 'data-z': '1000',
'data-rotate-x': '180', 'data-rotate-y': '0',
'data-rotate-z': '90.0', 'data-scale': '1'},
# Positioning x relative to other slide by id, Explicit y
{'data-x': '4800', 'data-y': '200', 'data-z': '1000',
'data-rotate-x': '180', 'data-rotate-y': '0',
'data-rotate-z': '90.0', 'data-scale': '1'},
])


Expand Down

0 comments on commit 98ecc42

Please sign in to comment.