Multiple Tool Window Instances

If your editor tool can have multiple instances (for example de Inspector tool window) you can use a helper class in api/guiInstancer.lua. Example:

Multiple tool instances
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- we use a small helper to keep our instances array
local guiInstancer = require("editor/api/guiInstancer")

-- when the user chose the Tool menu item for your tool, will create a new window instance
local function onEditorGuiToolsMenu()
    if imgui.MenuItem1("New My Tool", nil, imgui_false, imgui_true) then
        -- lets add a new window instance, the returned value is the index in the .instances field
        local index = guiInstancer.addInstance()
        guiInstancer.instances[index].locked = true -- will lock the window to a particular edited object
        guiInstancer.instances[index].objectId = someIdHere -- we want this object to be edited in this window
    end
end

local function renderMyWindow(instance)
    --   Render your widgets and logic in the window instance here
    -- the instance table can have various members pertaining to that instance.
    --   It can hold a instanceId for example, so you can identify the edited
    -- object or resource for that particular window instance.
    --   It also holds locked boolean, if true then the window is locked to that
    -- particular instance id.
end

local function onEditorGui()
    -- we go over all window instances
    for index, instance in pairs(guiInstancer.instances) do
        -- force visible true, we'll check after
        showWindow[0] = true
        -- we start a window here, we must make this window title unique (due to imgui logic),
        -- thus we add the index number to the window name
        if imgui.Begin("MyWindow #" .. tostring(index), showWindow) then
            -- it seems the user closed the window instance, so remove it
            if not showWindow[0] then
                guiInstancer.removeInstance(index)
            else
                -- render this window instance widgets/gui
                renderMyWindow(instance)
            end
        end
    end
end

local function onEditorInitialized()
    -- optional, you can spawn a default window instance on editor start up
    guiInstancer.addInstance()
    -- no need to set objectId or locked, this window instance will be global for now
    -- it all depends on your logic, for example the Scene Tree window instances will
    -- retain their scroll position, but the scene tree will be common to all instances
end

M.onEditorInitialized = onEditorInitialized
M.onEditorGui = onEditorGui
M.onEditorGuiToolsMenu = onEditorGuiToolsMenu

return M

For further reference on multiple instance tools you can check the code of the lua/ge/extensions/editor/sceneTree.lua

../../../_images/multi_tool_instances.png