Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ScrollViewport action #4973

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions renpy/common/00action_other.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,70 @@ init -1500 python:
adjustment.animate(amount, self.delay, _warper.ease)


@renpy.pure
class ScrollViewport(Action, DictEquality):
"""
:doc: other_action

Causes a Viewport to scroll to a specific position.

`id`
The id of the viewport in the current screen.

`position`
x and y coordinates for the viewport to scroll to.

`delay`
If non-zero, the scroll will be animated for this many seconds.
"""

delay = 0.0

def __init__(self, id, position, delay=0.0):
self.id = id
self.position = position
self.delay = delay

def __call__(self):

d = renpy.get_widget(None, self.id)

if d is None:
raise Exception("There is no displayable with the id {}.".format(self.id))

if not isinstance(d, Viewport):
raise Exception("The displayable with the id {} is not a viewport. SetViewportPosition only takes Viewports as arguments.".format(self.id))

if not isinstance(self.position, tuple):
raise Exception("The position must be a tuple.")

xadjustment = d.xadjustment
yadjustment = d.yadjustment
adjustment = (xadjustment, yadjustment) # tuple of x and y adjustments to use in the loop below

xyadjustment_change = [] # x and y relative adjustment changes for the animate call based on the position and the current adjustment values

for i, pos in enumerate(self.position):
# a loop for x and y
# using a loop here to avoid code duplication and allow mixing and matching of absolute and relative values
if isinstance(pos, float) and not isinstance(pos, absolute):
# If x/y is a float, use it as a relative position
xyadjustment_change.append(pos * adjustment[i].range - adjustment[i].value)

else:
# otherwise, use it as an absolute position
xyadjustment_change.append(pos - adjustment[i].value)

if self.delay == 0.0:
# if there is no delay, call change and sum the x and y adjustment changes to arrive at the final position
xadjustment.change(xadjustment.value + xyadjustment_change[0])
yadjustment.change(yadjustment.value + xyadjustment_change[1])
else:
# otherwise, animate the x and y adjustment changes
xadjustment.animate(xyadjustment_change[0], self.delay, _warper.ease)
yadjustment.animate(xyadjustment_change[1], self.delay, _warper.ease)
Comment on lines +749 to +773
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section can be simplified significantly by leveraging changes merged earlier in the 8,2,0 cycle.

Suggested change
xadjustment = d.xadjustment
yadjustment = d.yadjustment
adjustment = (xadjustment, yadjustment) # tuple of x and y adjustments to use in the loop below
xyadjustment_change = [] # x and y relative adjustment changes for the animate call based on the position and the current adjustment values
for i, pos in enumerate(self.position):
# a loop for x and y
# using a loop here to avoid code duplication and allow mixing and matching of absolute and relative values
if isinstance(pos, float) and not isinstance(pos, absolute):
# If x/y is a float, use it as a relative position
xyadjustment_change.append(pos * adjustment[i].range - adjustment[i].value)
else:
# otherwise, use it as an absolute position
xyadjustment_change.append(pos - adjustment[i].value)
if self.delay == 0.0:
# if there is no delay, call change and sum the x and y adjustment changes to arrive at the final position
xadjustment.change(xadjustment.value + xyadjustment_change[0])
yadjustment.change(yadjustment.value + xyadjustment_change[1])
else:
# otherwise, animate the x and y adjustment changes
xadjustment.animate(xyadjustment_change[0], self.delay, _warper.ease)
yadjustment.animate(xyadjustment_change[1], self.delay, _warper.ease)
px, py = self.position
x = absolute.compute_raw(px, d.xadjustment.range)
y = absolute.compute_raw(py, d.yadjustment.range)
if self.delay:
d.xadjustment.animate(x - d.xadjustment.value, self.delay, _warper.ease)
d.yadjustment.animate(y - d.yadjustment.value, self.delay, _warper.ease)
else:
d.xadjustment.change(x)
d.yadjustment.change(y)



@renpy.pure
class OpenDirectory(Action, DictEquality):
"""
Expand Down