Capricorn 76
Gui controls example.
GUI controls can be added to the engine using the IGraphics:create...() functions.
Groups of control can be saved/loaded to files using the IGraphics:loadGui() and IGraphics:saveGui() functions.
All the gui controls loaded by this demo are defined in the example.e76gui file.

---------------------------
---------------------------
-- Gui controls example
-- Copyright (c) 2007-09 Capricorn 76 Pty. Ltd.
--
-- GUI controls can be added to the engine using the IGraphics:create...() functions.
-- Groups of control can be saved/loaded to files using the IGraphics:loadGui() and IGraphics:saveGui() functions.
-- All the gui controls loaded by this demo are defined in the example.e76gui file.
--
-- NOTE:
-- The ./Demos/runDemo.e76script is a utility script used to run all the examples.
-- It loads the world, calls the OnWorldLoad(), OnWorldUnload() functions, and runs the graphics engine loop, waiting for the user to press escape.
---------------------------
---------------------------

local ID_HIGHLIGHT = nil;   -- We create an 2D sprite image to highlight the focussed control.
local subGui;

-- Load modules required by this demo
require('glob');

---------------------------
-- runDemo.e76script calls this function when the world is loaded
---------------------------
function OnWorldLoad(worldName)
    --print('OnWorldLoad');

    ---------------------------
    -- The SDK world editor creates a worldIds.e76script when the world is saved. It contains all the entity ids.
    -- We load the IDs so that we can refer to the world entities using friendly names.
    ---------------------------
    IApp:loadScript(IWorld:getNameLong() .. '/worldIds');
    IWorld:setActiveCameraId(idCamera_cam1);

    ---------------------------
    -- Let's show some help text
    ---------------------------
    OnWorldLoadGui();

    ---------------------------
    -- Subscribe to the 'gui' events.
    -- The graphics engine will run our callback function when any gui events occur.
    -- We can handle the events to perform specific processing.
    ---------------------------
    subGui = IEvents:subscribeGui('OnGui');

    ---------------------------
    -- To control how multiple handlers get prioritised, you can set the handler priority.
    -- All event subscribers have this capability.
    -- In this example, our handler must be secondary to the runDemo.e76script handler, so the main menu will be processed before our gui handler.
    ---------------------------
    IEvents:setPriority(subGui, -1000); -- priority is anything less than the default (which is what the runDemo.e76script handler uses)

    ---------------------------
    -- Load utility scripts/constants
    ---------------------------
    IApp:loadScript(IApp:getExeFilePath() .. './Constants/constantsGuiEventTypes');

    ---------------------------
    -- Set an animation for agent
    ---------------------------
    IGraphics:setEntityAnimationEx(idAgent_Mech, 0, 14, true, 10);

    ---------------------------
    -- Change the background color
    ---------------------------
    IGraphics:setBackColor('0.9 0.25 0.25 1');
end

---------------------------
-- Load the example's specific gui
---------------------------
function OnWorldLoadGui()
    ---------------------------
    -- Let's show some help text
    ---------------------------
    IGraphics:loadGui('example');

    ---------------------------
    -- Add an item to the listbox.
    -- Set some tagged data. This can be recalled from the listbox
    ---------------------------
    idx = IGraphics:addListboxItem('list', 'Item 1', 'ITEM1');
    idx = IGraphics:addListboxItem('list', 'Item 2', 'ITEM2');
    idx = IGraphics:addListboxItem('list', 'Item 3', 'ITEM 3');
    idx = IGraphics:addListboxItem('list', 'Item 4', 'ITEM 4');
    idx = IGraphics:addListboxItem('list', 'Item 5');
    idx = IGraphics:insertListboxItem('list', 'Inserted 6', nil, nil, 3);   -- Inserting into list
    idx = IGraphics:addListboxItem('list', 'Item 7', 'ITEM7');

    IGraphics:addComboboxItem('combo', 'aaa');
    IGraphics:addComboboxItem('combo', 'bbb');
    IGraphics:addComboboxItem('combo', 'ccc');
    IGraphics:addComboboxItem('combo', 'ddd');

    local filenames = glob.match('./*.*');
    for i, v in pairs(filenames) do
        IGraphics:addListboxItem('listDir', v);
    end

    ---------------------------
    -- Disable the tabpage clipping that's holding the combobox.
    -- This will allow the combobox's drop-down list to be displayed outside the tabpage
    ---------------------------
    IGraphics:setControlClipped('tabPage1', false);

    ---------------------------
    -- Create a small 2D sprite image we will use to highlight the focussed control
    ---------------------------
    --local spriteBankId = IGraphics:addSpriteBankEntry('focusHighlight.png', nil, nil);    -- This creates a static focus highlight image

    local positions = {'0 0 15 15', '0 16 15 31', '0 32 15 47', '0 48 15 63'}; -- Texture positions defining each frame
    local spriteBankId = IGraphics:addSpriteBankEntry('focusHighlight2.png', positions, 500); --This creates a moving focus highlight image

    ID_HIGHLIGHT = IGraphics:addSpriteToScreen(spriteBankId, '0 0', 0, true);

end

---------------------------
-- runDemo.e76script calls this function when the world is unloaded
---------------------------
function OnWorldUnload(worldName)
    --print('OnWorldUnload');

    ---------------------------
    -- Remove our sprite
    ---------------------------
    IGraphics:clearSpriteBank();

    ---------------------------
    -- Unsubscribe to events.
    -- We will no longer be informed of the events.
    ---------------------------
    IEvents:unsubscribe(subGui);
end

------------------------
-- Gui callback function.
-- The graphics engine will call this function whenever a gui event occurs.
--
-- We can test the event type, and the control identifier
-- to determine what happened and act accordingly.
-- Or we can use the utility functions like IGraphics:buttonClicked() ...
--
-- The control identifiers were defined in the example.e76gui
--
-- Return 'true' from the event handler to stop any default processing.
-- Return 'false'/'nil' (or don't return anything at all) to allow default processing to continue.
------------------------
function OnGui(idControl, eventType)
--print('OnGui:', idControl, eventType);

    if (IGraphics:buttonClicked('toolBarButton1', idControl, eventType)) then
        print('Toolbar Button clicked:', idControl, eventType);
        if (not(IGraphics:controlExists('window'))) then
            if (IGraphics:createWindow('window', nil, 'New window', '0.2 0.2 0.6 0.6', false, true, true, true)) then
                oldParent = IGraphics:getGuiParent();
                IGraphics:setGuiParent('window');
                IGraphics:createText('windowText', 'Text', '0.1 0.1 0.3 0.15', true, false, true, '0 0 0 1');
                IGraphics:createButton('windowButton', 'Button', '0.1 0.17 0.3 0.22');
                IGraphics:setControlFocus('windowText');
                IGraphics:setGuiParent(oldParent);
            end
        end
        return true;

    elseif (IGraphics:buttonClicked('toolBarButton2', idControl, eventType)) then
        print('Toolbar Button clicked:', idControl, eventType);
        return true;

    elseif (IGraphics:buttonClicked('buttonRemoveTab', idControl, eventType)) then
        print('Button clicked:', idControl, eventType);
        if (IGraphics:controlExists('tabPage')) then
            IGraphics:removeControl('tabPage');
        end
        return true;    -- We handled it

    elseif (IGraphics:buttonClicked('buttonChangeList', idControl, eventType)) then
        print('Changing list items:', idControl, eventType);

        for i=1, IGraphics:getListboxCount('list') do
            IGraphics:setListboxText('list', i-1, 'New ' .. tostring(i));
            IGraphics:setListboxData('list', i-1, 'DATA' .. tostring(i));
            IGraphics:setListboxIcon('list', i-1, '[' .. tostring(i) .. ']');
        end

        for i=1, IGraphics:getComboboxCount('combo2') do
            IGraphics:setComboboxText('combo2', i-1, 'New ' .. tostring(i));
            IGraphics:setComboboxData('combo2', i-1, 'DATA' .. tostring(i));
        end

        return true;    -- We handled it

    elseif (IGraphics:listChanged('list', idControl, eventType)) then
        print('Listbox changed:', IGraphics:getListboxSelectedText('list'), IGraphics:getListboxSelectedData('list'));
        return true;

    elseif (IGraphics:listChanged('listDir', idControl, eventType)) then
        print('DirListbox changed:', IGraphics:getListboxSelectedText('listDir'));
        return true;

    elseif (IGraphics:checkboxClicked('checkbox', idControl, eventType)) then
        print('Checkbox clicked:', IGraphics:getCheckbox('checkbox'));
        return true;

    elseif (IGraphics:tabControlChanged('tabControl', idControl, eventType)) then
        print('Tab changed:', IGraphics:getActiveTab('tabControl'));
        return true;

    elseif (IGraphics:comboChanged('combo', idControl, eventType)) then
        print('Combobox changed:', IGraphics:getComboboxSelectedText('combo'), IGraphics:getComboboxSelectedData('combo'));
        return true;

    elseif (IGraphics:comboChanged('combo2', idControl, eventType)) then
        print('Combobox changed:', IGraphics:getComboboxSelectedText('combo2'), IGraphics:getComboboxSelectedData('combo2'));
        return true;

    elseif (IGraphics:scrollBarChanged('scrollBar', idControl, eventType)) then
        print('Scrollbar changed:', IGraphics:getScrollBarPos('scrollBar'));
        return true;

    -- Did the control focus change?
    -- Test the event type directly, ignoring the control identifier.
    elseif (eventType == GUI_ELEMENT_FOCUS) then
        -- Did a control grab focus?
        if (IGraphics:controlExists(idControl)) then
            local controlPos = IGraphics:getControlPosAbsolute(idControl);
            local spriteRect = IGraphics:getSpriteScreenPosition(ID_HIGHLIGHT);
            local spriteWidth = IMisc:rectGetWidth(spriteRect);
            local spriteHeight = IMisc:rectGetHeight(spriteRect);
            print('Focus changed:', idControl, controlPos);

            IGraphics:setSpriteScreenPosition(  ID_HIGHLIGHT,
                                    IMisc:rectCreate(   controlPos:getX() - spriteWidth,
                                                controlPos:getY() - spriteHeight,
                                                controlPos:getX(),
                                                controlPos:getY())
                                    );
        end
        -- Dont return 'true' here, because we still want default processing to occur
    end
end

Copyright © 2006-23 Sep 2009 Capricorn 76 Pty. Ltd. (created on Wed Sep 23 16:49:12 2009)