There are so many ways to approach this, but it sounds like the overlay should be over the template canvas instead of over the templates in the canvas. If the overlay were moved to over the canvas and the overlay over the template was done away with, the only difference would be getting the viewport. This could be done with a library script or a custom method on the overlay itself.
Here is how the viewport could be obtained from using a custom method on the canvas overlay:
# def getViewportFromCanvas(self, canvas):
for component in canvas.components:
if component.__class__.__name__ == 'TemplateCanvas$1':
return component.viewport
To move the scrollbars:
On the mousePressed event:
# get the template canvas
canvas = event.source.parent.getComponent('Template Canvas')
# Capture and set the initial values of the mouse event on the overlay's custom properties
viewPosition = event.source.getViewportFromCanvas(canvas).viewPosition
event.source.initialMouseX = event.x
event.source.initialMouseY = event.y
event.source.oldX = event.x
event.source.oldY = event.y
event.source.initialViewX = viewPosition.x
event.source.initialViewY = viewPosition.y
On the mouseDragged event:
from java.awt import Point
# Get the canvas and its viewport
canvas = event.source.parent.getComponent('Template Canvas')
viewport = event.source.getViewportFromCanvas(canvas)
# Calculate the event coordinates by averaging the current position and the previous position
# During initial testing, the scroll movements were jittery, but
# ...adding this smoothed the movements out
eventX = (event.x + event.source.oldX)/2
eventY = (event.y + event.source.oldY)/2
# Calulate each coordinate by adding the difference between the current coordinate position and the initial coordinate position to the initial coordinate position
# ...Make sure the new position is not out of bounds using max and min
newX = max(0, min(event.source.initialViewX + (event.source.initialMouseX - eventX), viewport.view.width - viewport.viewRect.width))
newY = max(0, min(event.source.initialViewY + (event.source.initialMouseY - eventY), viewport.view.height - viewport.viewRect.height))
# Set the new view postion
viewport.setViewPosition(Point(newX, newY))
# Update the oldX and oldY properties with this iterations averaged value
event.source.oldX = eventX
event.source.oldY = eventY
