Viewport

class glumpy.app.Viewport(size=(800, 600), position=(0, 0), anchor=(0, 0), aspect=None)

Bases: glumpy.app.window.event.EventDispatcher

A Viewport represents a rectangular area on a window.

Parameters:
  • size – Requested size as (width, height)
  • position – Requested position as (x,y)
  • anchor – Anchor point as (x,y)
  • aspect (float) – Aspect (= width/height).

The size and the position are always relative to the parent viewport. They may be given in pixels (int) or as a percentage (float) of parent viewport size. Positive or negative values are accepted.

Important

The viewport class works in conjunction with the Viewport transform that ensure actual positioning and sizing within a shader program.

Let’s consider a root viewport of size 400x400 and a child viewport:

Absolute size

viewport = Viewport(400,400)
child = Viewport(100,100)
viewport.add(child)

# Child size is 100x100 (pixels)
viewport = Viewport(400,400)
child = Viewport(-100, -100)
viewport.add(child)

# Child size is (400-100) x (400-100) = 300 x 300 (pixels)

Relative size

viewport = Viewport(400,400)
child = Viewport(0.5, 0.5)
viewport.add(child)

# Child size is 400*0.5 x 400*0.5 = 200 x 200 (pixels)

# Child size is 200x200 pixels.
viewport = Viewport(400,400)
child = Viewport(-0.125, -0.125)
viewport.add(child)

# Child size is (400*(1-0.125)) x (400*(1-0.125)) = 50 x 50 (pixels)

Note

It is also possible to define an aspect (width/height) that will be enforced anytime.

Positioning the viewport inside the parent viewport is also made using absolute or relative coordinates.

Absolute position

viewport = Viewport(size=(400,400), position=(0,0))
child = Viewport(size=(100,100), position=(10,10))
viewport.add(child)

# Child position is +10+10 (pixels)
viewport = Viewport(size=(400,400), position=(0,0))
child = Viewport(size=(100,100), position=(-10,-10))
viewport.add(child)

# Child position is +(400-10)+(400-10) = +390+390 (pixels)

Relative position

viewport = Viewport(size=(400,400), position=(0,0))
child = Viewport(size=(100,100), position=(0.25,0.25))
viewport.add(child)

# Child position is +(400*0.25)+(400*0.25) = +100+100 (pixels)
viewport = Viewport(size=(400,400), position=(0,0))
child = Viewport(size=(100,100), position=(-0.25,-0.25))
viewport.add(child)

# Child position is +(400*(1-0.25))+(400*(1-0.25)) = +300+300 (pixels)

Note

The final position of the viewport relates to the anchor point which can be also set in absolute or relative coordinates.

The order of rendering is done according to the order of the viewport hierarchy, starting from the root viewport.

active

Whether viewport is active

add(child)

Add a new child to the viewport

attach(*args, **kwargs)

Push a level onto the top of the handler stack, then attach zero or more event handlers.

If keyword arguments are given, they name the event type to attach. Otherwise, a callable’s __name__ attribute will be used. Any other object may also be specified, in which case it will be searched for callables with event names.

dispatcher

Event dispatcher

event(*args)

Function decorator for an event handler.

Usage:

win = window.Window()

@win.event
def on_resize(self, width, height):
    # ...

or:

@win.event('on_resize')
def foo(self, width, height):
    # ...
extents

Actual position and size of the viewport

name

Viewport name

on_key_press(key, modifiers)

Default key handler that close window on escape

parent

Parent viewport

position

Actual position of the viewport

root

Root viewport

scissor

Actual position and size of the scissor

size

Actual size of the viewport