Login
  • Username:

    Password:
  • Remember my login on this computer
  • Register

R3 GUI actors

Contents

[edit] R3 GUI Actors

First author: Carl Sassenrath, RMA, 22-Apr-2011

[edit] Concept

An actor implements a style function.


If you think of a style as an object class, its actors are like the methods, and they operate on the instance of that class, the face object. Essentially, they are functions that are reused for each instance of a style, each face.


We call them actors because they are are not true methods and are similar to the actor functions used in schemes.


Note

This document is is for advanced users who plan to implement their own styles and need to provide new functions for rendering, input, or other control. Casual GUI users do not need to know about actors.


[edit] Defining Actors

GUI styles define actors to provide functions that handle events, process input, modify attributes, and setup graphics rendering.

Within a style an actor block holds the actor definitions. The block is a collection of actors names followed by their function body blocks. No function creators or argument specifications are needed, the make-style function will create those.

The naming convention for actors begins with on- followed by a verb or verb-noun that best describes the action. Examples are: on-draw or on-view. Although not strictly required, using this convention helps make GUI code more clear.

Here’s an example actors block as it would look within a style definintion:

actors: [
        on-make: [
                ...
        ]
        on-update: [
                ...
        ]
        on-resize: [
                ...
        ]
        on-scroll: [
                ...
        ]
        on-over: [
                ...
        ]
]

Here is a complete style definition of clicker, the base-style that implements buttons.

clicker: [

        about: "Single-action button without text. Basis of other styles."

        tags: [internal]

        facets: [
                init-size: 28x28
                bg-color: 80.100.120
                border-color: 0.0.0.128

                pen-color: ; set by on-draw
                area-fill: ; set by on-draw
                material: 'chrome
                focus-color: red
                draw-mode: 'normal
                materials: none
                face-width: none
        ]

        options: [
                face-width: [integer!]
                init-size: [pair!]
                bg-color: [tuple!]
        ]

        state: [
                validity: none
        ]

        draw: [
                normal: [
                        pen pen-color
                        line-width 1
                        grad-pen linear 1x1 0 (viewport-box/bottom-right/y) 90 area-fill
                        box 1x1 (viewport-box/bottom-right - 2) 1
                ]
                focus: [
                        fill-pen focus-color
                        box -1x-1 viewport-box/bottom-right 5
                        pen pen-color
                        line-width 1
                        grad-pen linear 1x1 0 (viewport-box/bottom-right/y) 90 area-fill
                        box 1x1 (viewport-box/bottom-right - 2) 1
                ]
        ]
        actors: [
                on-make: [
                        if face/facets/face-width [
                                face/facets/init-size/x:
                                face/facets/min-size/x:
                                face/facets/max-size/x: face/facets/face-width
                        ]
                ]

                on-init: [
                        set-facet face 'materials make-material face get-facet face 'material
                ]

                on-attach: [
                        ; Called when some face auto-attaches:
                        extend-face face 'attached arg
                        if r: get-facet arg 'default-reactor [
                                append-face-act face reduce bind r 'arg
                        ]
                ]

                on-draw: [
                        set-material face face/state/mode
                        color: get-facet face 'border-color
                        if face/state/mode = 'over [color: color / 2]
                        face/facets/pen-color: color
                        arg ; return draw block
                ]

                on-over: [; arg: offset or none
                        face/state/mode: pick [over up] face/state/over: not not arg
                        draw-face face
                ]

                on-click: [; arg: event
                        face/state/mode: arg/type
                        if 'up = face/state/mode [face/state/mode: 'over]
                        draw-face face
                        if arg/type = 'up [focus face do-face face]
                        none
                ]

                on-focus: [
                        set-facet face 'draw-mode either arg ['focus] ['normal]
                        set-facet face 'focus-color either arg [red] [255.255.255.255]
                        draw-face face
                ]

                on-key: [; arg: event
                        if arg/type = 'key [
                                switch arg/key [
                                        #" " [do-face face]
                                ]
                        ]
                ]

                on-validate: [
                        face/state/validity: validate-face face
                ]
        ]
]

[edit] Actor Arguments

Actor arguments are fixed and standardized. The arguments are:

face

The face upon which the actor acts.

args

A single value or block of multiple values.

When an actor is called, those values are passed to the body block. Local variables can be defined using set-words (same way as in FUNCT method).

[edit] Inherited Actors

When a style is derived from another style its actors are inherited.

It should be the derived actors are not bound to any particular style, but are simple functions. This allows actors to be efficiently reused in all instances of a face object.

For example, button uses the clicker actors here:

button: clicker [

        about: "Single action button with text."

        tags: [action tab]

        facets: [
                init-size: 130x24
                text: "Button"
                text-style: 'button
                max-size: 260x24
                min-size: 24x24
                text-size-pad: 20x0
        ]

        options: [
                text: [string! block!]
                bg-color: [tuple!]
                init-size: [pair!]
                wide: [percent!]
                face-width: [integer!]
        ]

        actors: [
                on-set: [
                        if arg/1 = 'value [
                                face/facets/text: form any [arg/2 ""]
                                show-later face
                        ]
                ]
                on-get: [
                        if arg = 'value [
                                face/facets/text
                        ]
                ]
                on-draw: [
                        t: get-facet face 'text
                        ; limit-text-size modifies, so we need to copy
                        ; size is made 20px smaller to incorporate "..." (see text-size-pad)
                        l: limit-text-size copy t face/gob/size - face/facets/text-size-pad face-font? face
                        set-facet face 'text-body either equal? length? t length? l [t] [join l "..."]
                        do-style/style face 'on-draw arg 'clicker
                ]
        ]
]

Notice, no actor definitions from clicker are needed to be duplicated.

[edit] Accessing Facets

For any given style a facet value may be stored in either the style object itself, or within the face instance. The location depends on whether the facet is static for all face instances, or changes for each instance.

Because you don’t know and shouldn’t care where it is stored, the get-facet function is provided to get the value and the set-facet function to set it.

For example, if the on-resize actor needs to know the size facet, it would use:

size: get-facet face 'size

If the size is found in the face/facets object, that will be used. Otherwise, the style/facets object will be used.

</div>

[edit] Standard Actor Names

A number of actor names are predefined for standard actions, and we recommend that you use these for their equivalent actors within your GUI styles:

Actor

Description

on-make

when face is first created to initialize special values

on-click

when mouse button clicked on face

on-drag

when dragging inside a face

on-drag-over

when dragging and are over a target face

on-drop

when drag has stopped or when a file is dropped

on-focus

when we have been given or are losing focus

on-get

to fetch state values

on-set

when state values are set

on-clear

when the state values are cleared

on-key

when key has been pressed (for our focus)

on-move

when mouse has moved

on-over

when mouse passes over or away

on-reset

when reset is needed

on-resize

when size values have changed

on-update

when face contents have changed

on-draw

when system starts to draw the face (create DRAW block)

on-scroll

when scrolling is needed

on-scroll-event

when the mouse wheel is used

on-init

when face and its parent pane are ready (including initial sizing)

on-attach

when a face is attached to another face

[edit] Default Actors

A small number of actors are defined by default to work for all styles. They are:

Style

Description

on-resize

Recompute the area-size facet.

on-get

Return the face/state of a given name.

locate

A special actor to map an event to an offset position.

Style definitions are allowed to override these actors with their own definition specific to their operation.

[edit] Calling an Actor

There will be times when one actor function will call another. This is done with the do-style function. Actors are always called this way, never directly.

For example:

do-style face 'on-click true

It should be noted that the call will be ignored if the face has no on-click actor.

Sometimes you can reuse actor code from another style to avoid duplicate code in multiple styles.

For example:

on-resize: [
        ;call the standard on-resize code from FACE style
        do-style/style face 'on-resize arg 'face

        ;here follows specific on-resize code for this style
        ...
]

[edit] Actors for Initialization

[edit] on-make

The on-make actor is called when the face is created within the layout engine.

args

none

return

none

This actor can be used to setup face facet values such as blocks of data, colors, face orientation, etc. and anything else unique to the face. If you are defining a compound style, this is a good actor to set-up any sub-faces.

It is not necessary to compute the face size within on-make because on-resize will be called later to do that.

Example that sets colors unique to the new face:

on-make: [
        face/facets/colors: copy [255.255.255 128.128.128]
]

[edit] on-init

The on-init actor is called when a new layout is created.

args

none

return

none

When a new layout is created, the on-init actor will be called for each face within the layout. At this point the face objects exist and the layout has been initialized, including any special bindings.

It is also called for any trigger faces (special faces that act on when layout events.)

Resetting the face on initialization:

on-init: [
        do-style face 'on-reset
]

[edit] on-attach

The on-attach actor is called when this face gets attached to another face.

args

the face requesting attachment

return

none

For example, the on-init actor for a scroll bar will call the on-attach for a scrollable face that was defined earlier.

Here’s how a layout uses it:

on-attach: [; arg: scroller
        ; Called when a face auto-attaches:
        extend-face face 'attached arg
        set-face arg face/state/value
]

For the scroller, the code to attach sets TARGET facet through its on-init:

on-init: [
        ; Find a prior face to attach the scroll bar to:
        if all [
                not get-facet face 'attached? ; check if face is already attached, so we don't add anoter target
                target: find-face-actor/reverse face 'on-scroll
        ] [
                do-style target 'on-attach face
        ]
        set-facet face 'target target
]

[edit] Actors for Events

[edit] on-click

The on-click is called each time a mouse button press or release occurs.

args

the event object for the mouse click.

return

on down, can be none or the drag object. On up, none.

This example prints the button event that occurred:

on-click: [
        probe arg/type
]

on-click is also the precursor to dragging (holding down a mouse button, while moving the mouse), so you can create a drag object in on-click using the init-drag function. If you return the drag object the on-drag actor will be invoked.

on-click: [
        if arg/type = 'down [
                return init-drag face arg/offset
        ]
        none ; return value
]

[edit] on-drag

The on-drag actor is called when the drag object is created or when the mouse is moving inside its target face.

args

drag object (created earlier)

return

none

on-drag usually comes after on-click and ceases to be used when the drag object is destroyed, which happens right after on-drop.

Note that when a drag object exists, the on-move and on-over actors are not called.

Here is an example used by a slider that changes a value constantly during drag, updates its graphics, and calls its reactors (if any).

on-drag: [; arg: drag
        do-style face 'on-offset arg/delta   arg/base
        draw-face face
        do-attached face
        do-face face
]

[edit] on-drag-over

The on-drag-over actor is called when the mouse is moving over a foreign face (not the target of the drag.)

args

Block of values related to the drag [drag-object offset ???]

return

none

Examples

on-drag-over: [
        ; write this example
]

[edit] on-drop

The on-drop actor is called when the drag operation is released.

args

the drag object created earlier.

return

none

At the end of a drag operation, when one of the mouse buttons is released, the on-click action is called with the event, then on-drop is called.

After on-drop, the drag object is automatically destroyed. The on-drop actor is caleld regardless of whether the drag operation was started over this face or a different face.

The on-drop actor is also called if a file is dragged and dropped from the system desktop over the face. This is a special case.

Example:

on-drop: [
        ; write an example here
]

[edit] on-focus

The on-focus actor is called every time the face gains focus using the focus or next-focus function or loses focus with the unfocus function.

args

true for focus and none for unfocus.

return

none

Nothing is called after on-focus, so if the face is changing appearance, a show-later should be called within the actor.

This example makes the background color yello (selection variation of yellow) when the face is focused and makes it white, when the face is unfocused:

on-focus: [
        face/facets/bg-color: pick reduce [yello white] arg
        draw-face face
]

[edit] on-key

The on-key actor is called when a face has focus and a key is pressed.

args

the input event

return

the same event

The on-key actor has a default value for all faces, to return the event argument.

Simple keyboard navigation in a street map face:

on-key: [
        if arg/type = 'key [;detect only "key-down" event types (use 'key-up for up events)
                dx: dy: 0
                switch arg/key [
                        right [dx: 1]
                        left [dx: -1]
                        up [dy: 1]
                        down [dy: -1]
                ]
                if find arg/flags 'shift [
                        dx: dx * 3
                        dy: dy * 3
                ]
                move-map as-pair dx dy
        ]
        arg ; return same event
]

[edit] on-move

The on-move actor is called every time the mouse moves.

args

the event value (with face-relative positions)

return

none

Important notes:

  • Not required for most styles.
  • Will be called often, so don’t define it unless you need it.
  • The on-over actor will be called after this actor.
  • Not be called during a drag operation.

[edit] on-over

The on-over actor is called every time the mouse moves over or away from the face.

args

the face relative position or none

return

none

If the face has the all-over value specified as true, this actor will be run continuously as long as the mouse is over the face. If all-over is not true, it does not report continuously, only on enter and exit.

[edit] on-resize

The on-resize actor is called every time the layout is resized.

args

new size (pair! value)

return

none

Normally, you use this actor to modify the size fields of the draw block and any GOBs used within it. If your style is a compound style, the faces inside may also need to be resized.

Resizing a single face:

on-resize: [
        face/gob/size: arg
        set-facet face 'size arg
]

Resizing a compound face (of panel layout type):

on-resize: [
        do-style/style face 'on-resize arg 'hpanel
        ;style specific code follows
        ....
]

[edit] on-scroll

The on-scroll actor is called from one face to scroll another face.

args

the face object of the scroller

return

true if the attach was successful

When a scroll-bar is moved it will call this function to tell its target face to scroll. This is how a scroll-bar informs the other face that it’s time to update. So, this actor must be defined for styles that are scrollable.

When a face is scrolled, this actor can be called directly by the scroll reactor or by a prior attachment that occurs when a scrollable face is defined before a scrolling style.

Do not confuse this with on-scroll-event which takes an event to perform.

Scroll a text area face:

on-scroll: [; arg: scroller
        gob: sub-gob? face
        size: gob/size - gob/text/scroll
        tsize: size-text gob
        gob/text/scroll/y: min 0 arg/state/value * negate tsize/y - gob/size/y   5
        show-later face
]

[edit] on-scroll-event

The on-scroll-event actor is called when a scroll event occurs.


args

the event

args

then same event


Scroll events can be caused by the mouse-wheel or other such devices.


Do not confuse this actor with on-scroll which is used to scroll a face from another face, such as a scroller style.


Here is an example:


on-scroll-event: [
        dy: none
        switch arg/type [
                scroll-line [dy: arg/offset/y / -30]
                scroll-page [dy: negate arg/offset/y]
        ]
        if dy [bump-scroll face dy]
        none
]


Note that bump-scroll is a function specific to this style.

[edit] Actors for Value Control

[edit] on-get

The on-get actor returns values stored in the face object and is normally called by get-face. Normally, the default actor is all you need.

args

the name of the variable to fetch (default is value)

return

The value fetched.

The default definition of on-get is:

on-get: [; arg: the field to get
        select face/state arg
]

[edit] on-set

The on-set actor is called by set-face for setting the state value of a face or any other value in the face.


args

A block containing the name and its value.

return

none


The default name is value, which means set the primary value of the face.


Here is an example of on-set used by a clock style to set its time:


on-set: [
        if arg/1 = 'value [
                if date? time: arg/2 [time: time/time]
                face/state/value: time
                face/facets/clock/set-clock time
                show-later face
        ]
]

[edit] on-clear

The on-clear actor is called when a face needs to be cleared.

args

none

return

none

This actor is called by the clear reactor. It is also called when a layout that contains input fields is cleared with the clear-layout function.

on-clear: [
        clear face/facets/text-edit
        show-later face
]

[edit] on-reset

The on-reset actor is called when the face needs to be reset to a predefined initial value.

args

none

return

none

This actor is mainly used for the reset reactor.

on-reset: [
        do-style face 'on-set 0
]

[edit] Other Actors

[edit] on-draw

The on-draw actor allows you modify a draw block immediately before it is rendered.

args

the current draw block for the face

return

the modified draw block

This function may be called often, every time a draw-face is needed. You should make this actor as efficient as possible. If it is possible for computations can be performed in other actors, such as on-click, that should be done.

Here is an example of the on-draw used by buttons (from the clicker style):

on-draw: [
        set-material face face/state/mode
        color: get-facet face 'border-color
        if face/state/mode = 'over [color: color / 2]
        face/facets/pen-color: color
        arg ; return draw block
]