peng3d.gui.widgets - 2D GUI Widget Base classes

class peng3d.gui.widgets.BasicWidget(name, submenu, window, peng, pos=None, size=None)[source]

Basic Widget class.

Every widget must be registered with their appropriate sub-menus to work properly.

pos may be either a list or 2-tuple of (x,y) for static positions or a function with the signature window_width,window_height,widget_width,widget_height returning a tuple.

size is similar to pos but will only get window_width,window_height as its arguments.

Commonly, the lambda lambda sw,sh,bw,bh: (sw/2.-bw/2.,sh/2.-bh/2.) is used to center the widget.

Additionally, an instance of a subclass of LayoutCell may be passed as pos. Note that this will automatically override size as well, unless size is also supplied.

The actions available may differ from widget to widget, by default these are used:

  • press is called upon starting to click on the widget
  • click is called if the mouse is released on the widget while also having been pressed on it before, recommended for typical button callbacks
  • context is called upon right-clicking on the widget and may be used to display a context menu
  • hover_start signals that the cursor is now hovering over the widget
  • hover is called every time the cursor moves while still being over the widget
  • hover_end is called after the cursor leaves the widget
  • statechanged is called every time the visual state of the widget should change
IS_CLICKABLE = False

Class attribute used to signal if widgets of this class are usually clickable.

This attribute is used to fill the initial value of enabled and can therefore be overridden on a widget-by-widget basis.

Note that leaving this set to False will prevent most mouse-related actions from being triggered. This is due to internal optimization and the main benefit of leaving this option off.

clickable

Property used for determining if the widget should be clickable by the user.

This is only true if the submenu of this widget is active and this widget is enabled.

The widget may be either disabled by setting this property or the enabled attribute.

delete()[source]

Deletes resources of this widget that require manual cleanup.

Currently removes all actions, event handlers and the background.

The background itself should automatically remove all vertex lists to avoid visual artifacts.

Note that this method is currently experimental, as it seems to have a memory leak.

draw()[source]

Draws all vertex lists associated with this widget.

enabled

Property used for storing whether or not this widget is enabled.

May influence rendering and behavior.

Note that the widget will be immediately redrawn if this property is changed.

getState()[source]

Returns the current state of the widget.

One of "pressed", "hover", "disabled" or "idle". Note that some information may be lost by getting this state, for example it is not possible to know if the widget is hovered or not if "pressed" is returned. However, this should not be a problem for most intended uses of this method.

on_redraw()[source]

Callback to be overridden by subclasses called if redrawing the widget seems necessary.

Note that this method should not be called manually, see redraw() instead.

pos

Property that will always be a 2-tuple representing the position of the widget.

Note that this method may call the method given as pos in the initializer.

The returned object will actually be an instance of a helper class to allow for setting only the x/y coordinate.

This property also respects any Container set as its parent, any offset will be added automatically.

Note that setting this property will override any callable set permanently.

redraw()[source]

Triggers a redraw of the widget.

Note that the redraw may not be executed instantly, but rather batched together on the next frame. If an instant and synchronous redraw is needed, use on_redraw() instead.

registerEventHandlers()[source]

Registers event handlers used by this widget, e.g. mouse click/motion and window resize.

This will allow the widget to redraw itself upon resizing of the window in case the position needs to be adjusted.

size

Similar to pos but for the size instead.

visible

Property used for storing whether or not this widget is enabled.

May influence rendering and behavior.

Note that the widget will be immediately redrawn if this property is changed.

class peng3d.gui.widgets.Background(widget)[source]

Class representing the background of a widget.

Note that if a background is used as the background of a SubMenu, the SubMenu instance itself should be passed as the widget.

This base class does not do anything.

init_bg()[source]

Called just before the background will be drawn the first time.

Commonly used to initialize vertex lists.

It is recommended to add all vertex lists to the submenu.batch2d Batch to speed up rendering and preventing glitches with grouping.

is_hovering

Read-only helper property for easier access.

Equivalent to widget.is_hovering.

peng

Property for accessing the parent widget’s instance of peng3d.peng.Peng.

pressed

Read-only helper property for easier access.

Equivalent to widget.pressed.

redraw_bg()[source]

Method called by the parent widget every time its Widget.redraw() method is called.

reg_vlist(vlist)[source]

Registers a vertex list to the internal list.

This allows the class to clean itself up properly during deletion, as the background would still be visible after deletion otherwise.

submenu

Property for accessing the parent widget’s submenu.

window

Property for accessing the parent widget’s window.

class peng3d.gui.widgets.Widget(name, submenu, window, peng, pos=None, size=None, bg=None, min_size=None)[source]

Subclass of BasicWidget adding support for changing the Background.

If no background is given, an EmptyBackground will be used instead.

on_redraw()[source]

Draws the background and the widget itself.

Subclasses should use super() to call this method, or rendering may glitch out.

setBackground(bg)[source]

Sets the background of the widget.

This may cause the background to be initialized.

class peng3d.gui.widgets.EmptyBackground(widget)[source]

Background that draws simply nothing.

Can be used as a placeholder.