Moving a texture around inside a container, aka fake "viewport"

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CalmTurtle

I think I may be missing something extremely basic, but can’t crack it.

I would like to move a texture around inside a container (MarginContainer in the example). The container stays static but the texture inside it can move about freely, (either by grabbing or scrollbars, or arrow keys). Despite my texture being much larger than the container. Imagine the container as the viewport for the texture. Any advice would be appreciated.

Thanks in advance.


:bust_in_silhouette: Reply From: Zylann

If you don’t want to use a Viewport, there are several other ways to do this:

  1. Use scissor rect. This feature visually clips the contents of your control, and you can enable it by checking clip contents. This should be the easiest, and that’s what controls like ItemList and ScrollView are doing with their contents as well.

  2. Use a texture region. This can either be done by manipulating texture coordinates (UVs) with an AtlasTexture, or use a Sprite with the region property.

Thanks for the answer. I appoligize, but I have a couple of questions of follow on questions:

What is a scissor rect? Search through the docs reveals nada.
Same goes for ScrollView (not in docs). I have tried playing with ScrollContainer but can’t get scroll bars to pop, nor is their much documentation on it.

I do like the texture region idea on an atlas texture, but how would I get scroll bars to pop.

Thanks again for your help.

CalmTurtle | 2019-03-29 14:16

See the details of my answer: “scissor rect” is the OpenGL name of the feature, in Godot controls expose it as a property named clip contents. Make it checked to clip any children of the node inside.

The atlas texture idea is more restrictive as it only works if you have only one texture to show.

In both cases, you need to handle scroll bars yourself, or use a ScrollView which comes with a bit of this functionality built-in: it will readjust if the controls inside it are larger. You could use a TextureRect node for example.

Zylann | 2019-03-29 20:23