Jump to content

Lua Module in Editor


Josh
 Share

Recommended Posts

I put together the first editor extension using a Lua module.

LuaModule.zip

Your main.c file needs one important function. The name of the module is "LuaZenMode", so the DLL needs to be called "LuaZenMode.dll" and the function needs to be called "luaopen_LuaZenMode". You will get an error if you call both the script and the module the same thing, because Lua will think the script is trying to load itself.

__declspec(dllexport) int luaopen_LuaZenMode(lua_State* L) {
    lua_pushcfunction(L, SetWindowZenMode);
    return 1;
}

In this case I am just assigning a function to the return value, but in most cases you probably want to create a table and add the function pointers to the table.

Here is our one function the lib uses:

static int SetWindowZenMode(lua_State* L)
{
    HWND hwnd = (HWND)luaL_checkinteger(L, 1);
    int state = luaL_checkint(L, 2);
    if (state == 1)
    {
        SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~(WS_CAPTION | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_THICKFRAME));
    }
    else
    {
        SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) | WS_CAPTION | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_THICKFRAME);
    }
    return 0;
}

It's okay to treat the HWND as a Lua number because even 64-bit Windows uses 32-bit window handles.

https://learn.microsoft.com/en-us/windows/win32/winprog64/interprocess-communication?redirectedfrom=MSDN

Here is how I load it in my script named ZenMode.lua:

local extension = {}
extension.func = require "LuaZenMode"

The Lua code then creates a menu divider and a new menu item:

local menu = program.menu:FindChild("View", false)
if menu == nil then
    Print("Error: ZenMode extension cannot find View menu")
    return
end

--Add divider
CreateMenu("", menu)

--Add menu item
extension.menu = CreateMenu("Zen Mode", menu)

And then it declares a function and uses that as an event listener. Note that the extension table is being passed in the extra function parameter:

function extension.ProcessEvent(event, extra)
    Print("Calling ProcessEvent...")
    local hwnd = program.window:GetHandle()
    if extra.menu:GetState() == 0 then
        Print("Enabling Zen mode...")
        extra.menu:SetState(1)
        extra.windowposition = {}
        extra.windowsize = {}
        extra.windowposition.x = program.window.position.x
        extra.windowposition.y = program.window.position.y
        extra.windowsize.x = program.window.size.x
        extra.windowsize.y = program.window.size.y
        program.window:SetShape(program.window.display.position.x, program.window.display.position.y, program.window.display.size.x, program.window.display.size.y)
        extra.func(hwnd, 1)
        Print("Zen mode enabled")
    else
        Print("Disabling Zen mode...")
        extra.menu:SetState(0)
        extra.func(hwnd, 0)
        program.window:SetShape(extra.windowposition.x, extra.windowposition.y, extra.windowsize.x, extra.windowsize.y)
        Print("Zen mode disabled")
    end
end

ListenEvent(EVENT_WIDGETACTION, extension.menu, extension.ProcessEvent, extension)

And now the program has new functionality added entirely by the extension.

image.thumb.jpeg.d470a4e4454c6630928ba2e6dc248584.jpeg

  • Like 2

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...