Shiny Language Reference

Language Reference > User Interface > Static Images


setstaticimagedrawfunc

setstaticimagedrawfunc(int ID, string FuncName);

 

Sets a function for "callback" to draw a custom static image control

The SETSTATICIMAGEDRAWFUNC is an extremely powerful enhancement of the static image user interface element. This statement allows the plugin to draw its own control, meaning that it can have any appearance required including multiple GIF/JPG/BMP/PNG images combined together.

This control can be used like a canvas for drawing on using the custom draw system. The advantage to this is it allows you to draw complex images as the plugin runs (rather than stored on disk as a pre-created image), and the resulting image is used for your static control. This in turn is enabled for the 100% Configuration System, meaning that all of your custom drawing is moveable and resizable simply by clicking on the display panel when Design Mode is enabled.

Note: True per-pixel alphablending in PNG images is supported. This allows for smooth, blended edges of buttons. Note that fastdraw must be disabled for PNG alphablending as true blending can only be achieved by recalculating the panel contents.

The SETSTATICIMAGEDRAWFUNC is called with the name of a specific function as the second parameter. The function is prototyped as follows:

bool function MyButtonDrawing(int iState, int iWidth, int iHeight)

The name of the function can be anything required, this of course allows for more than one image-drawing functions.

When SETSTATICIMAGEDRAWFUNC is called, it immediately prepares Brass for drawing functions. It then calls the function provided as the second parameter. Inside this function you may use any Brass drawing function, exactly as you would in the Render handler.

The iWidth and iHeight parameters contain the width and height of the control. These values are exactly the same as the dimensions you specified for the createstaticimage statement, they are simply passed as parameters here for convenience, to save you storing the values as global variables. If you loaded an image with the createstaticimage statement and did not supply a width and height parameter, the width and height are automatically set to the dimensions of the image you specified.

When you have drawn a state you must return boolean TRUE. Brass checks the return value of your custom draw handler to decide what actions need to be taken. This is to ensure future compatibility. Brass knows what state it asked the plugin to draw, and checks the return value of the handler. If the return value is boolean TRUE, Brass notes that the plugin has successfully drawn the requested state and caches the output. If the return value is boolean FALSE, Brass assumes that the plugin did not draw the requested state. In this case Brass will simply draw a grey square to alert you that your custom draw handler did not respond correctly.

There is a detailed explanation of how the custom draw system works and why you must return boolean True in the Custom Draw Button reference page. The principle is exactly the same for all controls.

Implementation note: Controls may be drawn as complex as you require. Brass calls the drawing function only when SETSTATICIMAGEDRAWFUNC is called, and caches the image you draw. Therefore when the display panel must be redrawn Brass already has the custom image stored in memory and available to draw.

Because of this smart-management system, custom drawn controls are no slower for use than standard, flat controls.

By using static image controls (optionally with the custom draw system) you are enabling your image to be manipulated using the 100% Configuration System.

 

Parameters

ID

The control ID of the control you wish to draw manually

FuncName

The name of the function within the plugin that will be called to draw the control

 

Return Value

None

 

Example Code

The following code is a complete, working plugin. You may paste it directly into SSEdit, compile and run it to demonstrate custom-drawn static image controls. This sample assumes "sample.png" exists; it loads it, then sets up a custom draw handler which draws a simple black border around the image. For a complete sample plugin including sample image, a download is available on the main Static Image Controls page.


function Init()
{
    // This code assumes sample.png exists and is valid
    int FirstImageID = createstaticimage(1, 10, 1, "sample.png");
    setstaticimagedrawfunc(FirstImageID, "ImageDrawFunc");
}

bool function ImageDrawFunc(int iState, int iWidth, int iHeight)
{
    // Check the state we are being asked to draw
    if(iState == IMAGE_NORMAL)
    {
        // Draw a simple black border around the control. We could do as much
        // complex drawing as we wanted here.
        drawrect(0, 0, iWidth, iHeight, RGB(0, 0, 0));

        // Return boolean True because we drew this state
        return True;
    }
    
    
// Return boolean False for future compatibility.
    return False;
}