lua - add basic clipboard bindings

This commit is contained in:
Cody Glassman 2025-08-01 15:26:57 -07:00
parent b160cee0b7
commit 624df21a9c
11 changed files with 97 additions and 1 deletions

View File

@ -63,7 +63,7 @@ add_openmw_dir (mwlua
context menuscripts globalscripts localscripts playerscripts luabindings objectbindings cellbindings coremwscriptbindings
mwscriptbindings camerabindings vfsbindings uibindings soundbindings inputbindings nearbybindings dialoguebindings
postprocessingbindings stats recordstore debugbindings corebindings worldbindings worker landbindings magicbindings factionbindings
classbindings itemdata inputprocessor animationbindings birthsignbindings racebindings markupbindings weatherbindings
classbindings itemdata inputprocessor animationbindings birthsignbindings racebindings markupbindings weatherbindings clipboardbindings
types/types types/door types/item types/actor types/container types/lockable types/weapon types/npc
types/creature types/player types/activator types/book types/lockpick types/probe types/apparatus
types/potion types/ingredient types/misc types/repair types/armor types/light types/static

View File

@ -0,0 +1,46 @@
#include "clipboardbindings.hpp"
#include <SDL.h>
#include <SDL_clipboard.h>
#include <components/debug/debuglog.hpp>
#include "context.hpp"
namespace MWLua
{
sol::table initClipboardPackage(const Context& context)
{
sol::state_view lua = context.sol();
sol::table api(lua, sol::create);
api["get"] = []() {
std::string text;
if (char* raw = SDL_GetClipboardText())
{
text = raw;
SDL_free(raw);
}
else
{
Log(Debug::Error) << "Failed to get clipboard content: " << SDL_GetError();
}
return text;
};
auto set = [](const char* data) {
if (SDL_SetClipboardText(data) != 0)
{
Log(Debug::Error) << "Failed to set clipboard content: " << SDL_GetError();
}
};
api["set"] = [set](const std::string_view text) { set(text.data()); };
api["clear"] = [set]() { set(nullptr); };
return LuaUtil::makeReadOnly(api);
}
}

View File

@ -0,0 +1,13 @@
#ifndef MWLUA_CLIPBOARD_H
#define MWLUA_CLIPBOARD_H
#include <sol/forward.hpp>
#include "context.hpp"
namespace MWLua
{
sol::table initClipboardPackage(const Context&);
}
#endif // MWLUA_CLIPBOARD_H

View File

@ -10,6 +10,7 @@
#include "animationbindings.hpp"
#include "camerabindings.hpp"
#include "cellbindings.hpp"
#include "clipboardbindings.hpp"
#include "corebindings.hpp"
#include "debugbindings.hpp"
#include "inputbindings.hpp"
@ -62,6 +63,7 @@ namespace MWLua
{ "openmw.core", initCorePackage(context) },
{ "openmw.types", initTypesPackage(context) },
{ "openmw.nearby", initNearbyPackage(context) },
{ "openmw.clipboard", initClipboardPackage(context) },
};
}
@ -85,6 +87,7 @@ namespace MWLua
{ "openmw.ui", initUserInterfacePackage(context) },
{ "openmw.menu", initMenuPackage(context) },
{ "openmw.input", initInputPackage(context) },
{ "openmw.clipboard", initClipboardPackage(context) },
};
}
}

View File

@ -12,6 +12,7 @@ Packages
async <openmw_async>
camera <openmw_camera>
core <openmw_core>
clipboard <openmw_clipboard>
debug <openmw_debug>
input <openmw_input>
markup <openmw_markup>

View File

@ -0,0 +1,7 @@
Package openmw.clipboard
========================
.. include:: version.rst
.. raw:: html
:file: generated_html/openmw_clipboard.html

View File

@ -20,6 +20,9 @@
* - :doc:`core </reference/lua-scripting/openmw_core>`
- |bdg-ctx-all|
- Functions that are common for both global and local scripts
* - :doc:`clipboard </reference/lua-scripting/openmw_clipboard>`
- |bdg-ctx-local| |bdg-ctx-menu|
- Read and write access to the systems clipboard
* - :doc:`debug </reference/lua-scripting/openmw_debug>`
- |bdg-ctx-player|
- Collection of debug utils.

View File

@ -23,6 +23,7 @@ local env = {
util = require('openmw.util'),
storage = require('openmw.storage'),
core = require('openmw.core'),
clipboard = require('openmw.clipboard'),
types = require('openmw.types'),
vfs = require('openmw.vfs'),
markup = require('openmw.markup'),

View File

@ -45,6 +45,7 @@ local env = {
menu = require('openmw.menu'),
util = require('openmw.util'),
core = require('openmw.core'),
clipboard = require('openmw.clipboard'),
storage = require('openmw.storage'),
vfs = require('openmw.vfs'),
markup = require('openmw.markup'),

View File

@ -70,6 +70,7 @@ local env = {
util = require('openmw.util'),
storage = require('openmw.storage'),
core = require('openmw.core'),
clipboard = require('openmw.clipboard'),
types = require('openmw.types'),
vfs = require('openmw.vfs'),
markup = require('openmw.markup'),

View File

@ -0,0 +1,20 @@
---
-- Provides an interface to the systems clipboard.
-- @context local|menu
-- @module clipboard
-- @usage local clipboard = require('openmw.clipboard')
---
-- Sets the clipboard from a UTF-8 encoded string
-- @function [parent=#clipboard] set()
-- @param #string text The text to set for the clipboard.
---
-- Gets the clipboard content as a UTF-8 encoded string
-- @function [parent=#clipboard] get()
---
-- Clears the systems clipboard
-- @function [parent=#clipboard] clear()
return nil