-
Notifications
You must be signed in to change notification settings - Fork 26
Widget
Last revision: ver. 6.0 - 29 December 2014
Base class for GUI elements
Method(s) | Description |
---|---|
(v6.0) static Widget create(Container c) | Creates a new widget as a child of the specified container. (see Container) |
setVisible(bool visible) , bool isVisible()
|
Gets or sets the visibility for the widget |
setEnabled(bool enabled) , bool isEnabled()
|
When true, the widget is enabled, i.e. it can receive input events and takes part in navigation (can become active) |
setPosition(Vector2 position) , Vector2 getPosition()
|
Gets or sets the widget position in pixels (see Vector2). |
Vector2 getSize() , 'setSize(Vector2 size)
|
Gets or sets the size of the widget in pixels (see Vector2). |
float getScale() , setScale(float scale)
|
Sets the widget scale. Scale controls the visual appearance of a widget without changing its actual size or forcing a layout refresh of the widget container. Scale is indicated as a proportion of the current widget size. |
float getRotation() , setRotation(float degrees)
|
Gets or sets a widget rotation in degrees |
int getWidth() , 'setWidth(int value)
|
Gets or sets the widget width |
int getHeight() , 'setHeight(int value)
|
Gets or sets the widget height |
setAutosize(bool value) , bool getAutosize()
|
Enables / disables autosize mode. |
(v6.0) updateSize()
|
Forces a size recalculation for the widget. |
setName(stirng name) , string getName()
|
Gets or sets the widget name |
setStyle(string style) |
Sets one or more widget styles as key: value pairs |
(v6.0) setActiveStyle(string style) string getActiveStyle()
|
Gets or sets the style used when the widget is active. |
(v6.0) setInactiveStyle(string style) string getInactiveStyle()
|
Gets or sets the style used when the widget is inactive. |
setStyleValue(style, value) |
Sets the value for a single style key |
setFillEnabled(bool value) |
|
setFillColor(Color color) |
Sets the foll Color for the widget area. |
refresh() |
Forces a widget refresh |
hitTest(Vector2 position) |
Checks if the specified pixel position lies within the widget area. (see Vector2). |
(v6.0) Vector2 transformPoint(Vector2 point)
|
Transforms a 2D point from screen space to this widget's reference frame. |
WidgetLayer getLayer() , setLayer(WidgetLayer value)
|
Gets or sets the WidgetLayer for the widget. Layers influence the drawing order of widgets within the same container |
Vector2 transformPoint(Vector2 point) |
Transforms a point from global pixel coordinates to local pixel coordinates in the widget space. (see Vector2). |
setCenter(Vector2 point) , Vector2 getCenter()
|
Gets or Sets the widget position by it center. (see Vector2). |
setAlpha(float value) float getAlpha()
|
Gets or sets the transparency of the widget. Blending must be enabled through setBlendMode
|
setBlendMode(WidgetBlendMode value) WidgetBlendMode setBlendMode()
|
Gets or sets the blend mode for the widget |
bool isStereo() setStereo(bool value)
|
Gets or sets stereo mode for the Widget. Some widgets (like Image ) perform rendering differently when stereo mode is enabled. For instance, Image supports rendering of side-by-side stereo images. |
Layout | |
requestLayoutRefresh() |
Forces a layout recalculation for this widget and parent containers |
setDraggable(bool value) , bool isDraggable()
|
|
(v6.0) bool isDragging()
|
Returns true if this widget is being dragged |
setPinned(bool value) , bool isPinned()
|
When a widget is pinned, its position will remain fixed with respect to its container. Pinning widgets is useful to make container draggable only on a sub-section represented by the pinned widget. |
setSizeAnchorEnabled(bool) , bool isSizeAnchorEnabled()
|
Gets or sets size anchor mode. When size anchor is enabled, this widget size will follow its container size plus an offset specified by the setSizeAnchor method. |
setSizeAnchor(Vector2 size) , Vector2 getSizeAnchor()
|
gets or sets the widget size anchor. |
Navigation | |
Widget getHorizontalNextWidget() , setHorizontalNextWidget(Widget w)
|
Gets or sets the widget that will be selected when the user presses the right arrow on this widget. |
Widget getHorizontalPrevWidget() , setHorizontalPrevWidget(Widget w)
|
Gets or sets the widget that will be selected when the user presses the left arrow on this widget. |
Widget getVerticalNextWidget() , setVerticalNextWidget(Widget w)
|
Gets or sets the widget that will be selected when the user presses the down arrow on this widget. |
Widget getVerticalPrevWidget() , setVerticalPrevWidget(Widget w)
|
Gets or sets the widget that will be selected when the user presses the up arrow on this widget. |
bool isNavigationEnabled() , setNavigationEnabled(bool enabled)
|
Enables key-based widget navigation on this widget, or checks its state. |
Commands | |
setUIEventCommand(string command) |
Sets the command to be invoked when events happen in the widget area (like clicks or value changes) |
setUpdateCommand(string cmdstring) string getUpdateCommand()
|
Gets or specified an update command to be executed for each update cycle on the widget. Example: widget.setUpdateCommand("print('widget updated')")
|
(v6.0) setDragBeginCommand(string cmd) , string getDragBeginCommand()
|
Sets or gets a script command to be invoked when this widget starts dragging. |
(v6.0) setDragEndCommand(string cmd) , string getDragEndCommand()
|
Sets or gets a script command to be invoked when this widget ends dragging. |
(v6.0) setActivateCommand(string cmd) , string getActivateCommand()
|
Sets or gets a script command to be invoked when this widget gets active status. |
(v6.0) setDeactivateCommand(string cmd) , string getDeactivateCommand()
|
Sets or gets a script command to be invoked when this widget loses active status. |
(v6.0) Draw Callbacks | |
setPreDrawCallback(function callback) | Sets a python function to be called before rendering of this widget begins. The function must accept three arguments. A reference to this widget, the current Camera and a DrawInterface object. (see example at the bottom of the page) |
setPostDrawCallback(function callback) | Sets a python function to be called right before rendering of this widget terminates. The function must accept three arguments. A reference to this widget, the current Camera and a DrawInterface object. (see example at the bottom of the page) |
Note that event handlers can be attached to widgets or to widget containers - i.e. if you want to have a single event handler for all the widgets in a container, you can attach the event handler to the container itself.
widget events traverse the container hierarchy backwards, until they find an object that has a registered event handler. See https://github.com/uic-evl/omegalib/blob/master/src/omegaToolkit/ui/Widget.cpp#L403
The method getLayer()
returns a value from the WidgetLayer
enumeration. The WidgetLayer
enumeration contains the following values: Front
, Middle
, Back
The method getLayer()
returns a value from the WidgetBlendMode
enumeration. The WidgetLayer
enumeration:
-
BlendInherit
sets the blend mode to inherited from parent -
BlendNormal
sets the blend mode to standard blending -
BlendAdditive
sets the blend mode to additive blending
The methods setStyle
and setStyleValue
accept the following style keys:
-
fill
: sets the widget fill color. Example:widget.setStyleValue('fill', '#ff0000')
-
border
: sets the border style for all borders. widget fill color. Border style is a border width followed by a borer color. Example:widget.setStyleValue('border', '2 #ffff00')
-
border-top
,border-bottom
,border-left
,border-right
: sets each border side style separately - (v6.0)
alpha
: sets this widget transparency (between 0 and 1). same as thesetAlpha
method. - (v6.0)
scale
sets this widget scale. same as thesetScale
method. - (v6.0)
shader
sets the shader to use to render this widget. If namex
is specified, widget will loadx.vert
andx.frag
vertex and fragment shader files.
https://github.com/uic-evl/omegalib/blob/master/examples/python/drag.py
# Create a sub-menu that will contain the multiple columns
mm = MenuManager.createAndInitialize()
menu2 = mm.getMainMenu().addSubMenu("TestMenu")
# Get the menu container, set its layout to horizontal
mc = menu2.getContainer()
mc.setLayout(ContainerLayout.LayoutHorizontal)
# Create 3 vertical columns, add them to the menu
c1 = Container.create(ContainerLayout.LayoutVertical, mc)
c2 = Container.create(ContainerLayout.LayoutVertical, mc)
c3 = Container.create(ContainerLayout.LayoutVertical, mc)
# Add a bunch of buttons to the columns
# Column 1
b1c1 = Button.create(c1)
b2c1 = Button.create(c1)
# Column 2
b1c2 = Button.create(c2)
b2c2 = Button.create(c2)
b3c2 = Button.create(c2)
# Column 3
b1c3 = Button.create(c3)
b2c3 = Button.create(c3)
b3c3 = Button.create(c3)
# setup navigation
# vertical navigation for buttons will be set up automatically by the container.
# we set horizontal navigation manually
# Buttons on the first row
b1c1.setHorizontalNextWidget(b1c2)
b1c2.setHorizontalPrevWidget(b1c1)
b1c2.setHorizontalNextWidget(b1c3)
b1c3.setHorizontalPrevWidget(b1c2)
# Buttons on the second row
b2c1.setHorizontalNextWidget(b2c2)
b2c2.setHorizontalPrevWidget(b2c1)
b2c2.setHorizontalNextWidget(b2c3)
b2c3.setHorizontalPrevWidget(b2c2)
# Buttons on the third row
# since the first column does not have a third row, prev navigation
# from column 2 goes to the last button of column 1
b3c2.setHorizontalPrevWidget(b2c1)
b3c2.setHorizontalNextWidget(b3c3)
b3c3.setHorizontalPrevWidget(b3c2)
from omegaToolkit import *
uim = UiModule.createAndInitialize()
c = Widget.create(uim.getUi())
c.setDraggable(True)
c.setSize(Vector2(200,200))
firstTime = True
basicShader = 0
textShader = 0
def onDraw(w, camera, painter):
# Initialize the shaders on first run
global firstTime, basicShader, textShader
if(firstTime):
firstTime = False
basicShader = painter.getOrCreateProgram('base', 'ui/widget-base.vert', 'ui/widget-base.frag')
textShader = painter.getOrCreateProgram('text', 'ui/widget-label.vert', 'ui/widget-label.frag')
# Draw a rect
painter.program(basicShader)
a = painter.findUniform(basicShader, 'unif_Alpha')
# NOTE: uniformFloat needs to be called after program()
painter.uniformFloat(a, 1.0)
painter.drawRect(Vector2(0,0), Vector2(200,200), Color(0, 0.5, 0.5, 1))
# Draw Text
font = painter.getDefaultFont()
painter.program(textShader)
a = painter.findUniform(textShader, 'unif_Alpha')
painter.uniformFloat(a, 1.0)
painter.drawText("(Drag me): " + str(w.getPosition()), font, Vector2(5, 5), TextAlign.HALeft | TextAlign.VATop, Color(1, 1, 1, 1))
c.setPostDrawCallback(onDraw)