Skip to content

GraphView

Since 2.2.1

GraphView is a pannable, zoomable canvas for displaying node graphs or any arbitrary 2D content. Children are placed on a contentRoot element that can be translated and scaled. The view renders a repeating grid background and supports:

  • Pan — click and drag (left-button on the view background, or middle-button anywhere).
  • Zoom — mouse wheel, clamped to [min-scale, max-scale].
  • Fit — helpers to centre and scale the view to fit all children or a given bounding box.

INFO

Everything documented on UIElement (layout, styles, events, data bindings, etc.) applies here too.


Usage

java
var graph = new GraphView();
graph.graphViewStyle(style -> style
    .allowZoom(true)
    .allowPan(true)
    .minScale(0.2f)
    .maxScale(5f)
);

var node = new UIElement();
graph.addContentChild(node);
parent.addChild(graph);

// Fit the view after adding children:
graph.fitToChildren(20f, 0.1f);

Internal Structure

FieldCSS classDescription
contentRoot.__graph-view_content-root__Absolutely-positioned element that holds all user-added children and receives the pan/zoom transform.

GraphView Style

INFO

allow-zoom

Whether scrolling the mouse wheel changes the zoom level.

Default: true

java
graph.graphViewStyle(style -> style.allowZoom(false));

INFO

allow-pan

Whether clicking and dragging pans the view.

Default: true

java
graph.graphViewStyle(style -> style.allowPan(false));

INFO

min-scale / max-scale

Zoom level bounds.

Defaults: 0.1 / 10

java
graph.graphViewStyle(style -> style.minScale(0.25f).maxScale(4f));

INFO

grid-background

Texture tiled across the background. The default is a built-in grid sprite with REPEAT wrap mode.

Default: ldlib2:textures/gui/grid_bg.png (repeat)

java
graph.graphViewStyle(style -> style.gridTexture(myGridTexture));

INFO

grid-size

Size (in world-space units) of each grid cell.

Default: 64

java
graph.graphViewStyle(style -> style.gridSize(32f));

Fields

NameTypeAccessDescription
contentRootUIElementpublic finalThe transformed canvas holding all content.
graphViewStyleGraphViewStyleprivate (getter)Current style.
offsetXfloatgetter/setterCurrent horizontal world-space offset.
offsetYfloatgetter/setterCurrent vertical world-space offset.
scalefloatprivate (getter)Current zoom level.

Methods

MethodReturnsDescription
addContentChild(UIElement)GraphViewAdds a child to contentRoot.
removeContentChild(UIElement)GraphViewRemoves a child from contentRoot.
clearAllContentChildren()GraphViewRemoves all children from contentRoot.
graphViewStyle(Consumer<GraphViewStyle>)GraphViewConfigures style fluently.
contentRoot(Consumer<UIElement>)UIElementConfigures contentRoot.
fitToChildren(float padding, float minScaleBound)voidAdjusts offset and scale to fit all visible children, with the given padding and minimum scale.
fit(float minX, float minY, float maxX, float maxY, float minScaleBound)voidAdjusts offset and scale to fit the given bounding box.

Released under the MIT License.