Compare commits
15 commits
8f68ae45ce
...
c5c8b0f56d
Author | SHA1 | Date | |
---|---|---|---|
c5c8b0f56d | |||
88829b1660 | |||
e15db6f82f | |||
b4e3c1a425 | |||
9438d9d0b2 | |||
e5c67be41a | |||
c7e5a21be0 | |||
08738ed0cc | |||
2a2130f42f | |||
c794d7b8ca | |||
ef961265a5 | |||
e44f70bbac | |||
1c896b521d | |||
3116bceb66 | |||
8aba15baa2 |
31 changed files with 166 additions and 464 deletions
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 413 B |
|
@ -3,10 +3,10 @@
|
|||
// I/O
|
||||
|
||||
out vec4 fColor;
|
||||
uniform vec4 aColor;
|
||||
uniform vec3 aColor;
|
||||
|
||||
// Main
|
||||
|
||||
void main() {
|
||||
fColor = aColor;
|
||||
fColor = vec4(aColor, 1.0);
|
||||
}
|
|
@ -77,7 +77,7 @@ int main() {
|
|||
}
|
||||
|
||||
void errorCatcher(int id, const char* str) {
|
||||
Logger::fatalErrorf("GLFW Error: [{}] {}", id, str);
|
||||
Logger::fatalError(std::format("GLFW Error: [{}] {}", id, str));
|
||||
}
|
||||
|
||||
float lastTime;
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
extern const char* WRAPPER_SRC; // TODO: Move this to a shared header
|
||||
int script_errhandler(lua_State*); // extern
|
||||
|
||||
SignalSource::SignalSource() : std::shared_ptr<Signal>(std::make_shared<Signal>()) {}
|
||||
|
@ -38,11 +39,30 @@ LuaSignalConnection::~LuaSignalConnection() {
|
|||
luaL_unref(state, LUA_REGISTRYINDEX, thread);
|
||||
}
|
||||
|
||||
#if 0
|
||||
static void stackdump(lua_State* L) {
|
||||
printf("%d\n", lua_gettop(L));
|
||||
fflush(stdout);
|
||||
lua_getfield(L, LUA_GLOBALSINDEX, "tostring");
|
||||
for (int i = lua_gettop(L)-1; i >= 1; i--) {
|
||||
lua_pushvalue(L, -1);
|
||||
lua_pushvalue(L, i);
|
||||
lua_call(L, 1, 1);
|
||||
const char* str = lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
printf("%s: %s\n", lua_typename(L, lua_type(L, i)), str);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
printf("\n\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
#endif
|
||||
|
||||
void LuaSignalConnection::Call(std::vector<Variant> args) {
|
||||
lua_State* thread = lua_newthread(state);
|
||||
|
||||
// Push wrapepr as thread function
|
||||
lua_getfield(thread, LUA_REGISTRYINDEX, "LuaPCallWrapper");
|
||||
luaL_loadbuffer(thread, WRAPPER_SRC, strlen(WRAPPER_SRC), "=PCALL_WRAPPER");
|
||||
|
||||
// Push function as upvalue for wrapper
|
||||
lua_rawgeti(thread, LUA_REGISTRYINDEX, function);
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
static std::ofstream logStream;
|
||||
static std::vector<Logger::LogListener> logListeners;
|
||||
static std::vector<Logger::TraceLogListener> traceLogListeners;
|
||||
std::string Logger::currentLogDir = "NULL";
|
||||
|
||||
void Logger::init() {
|
||||
|
@ -27,7 +28,7 @@ void Logger::finish() {
|
|||
logStream.close();
|
||||
}
|
||||
|
||||
void Logger::log(std::string message, Logger::LogLevel logLevel, ScriptSource source) {
|
||||
void Logger::log(std::string message, Logger::LogLevel logLevel) {
|
||||
std::string logLevelStr = logLevel == Logger::LogLevel::INFO ? "INFO" :
|
||||
logLevel == Logger::LogLevel::DEBUG ? "DEBUG" :
|
||||
logLevel == Logger::LogLevel::TRACE ? "TRACE" :
|
||||
|
@ -43,7 +44,7 @@ void Logger::log(std::string message, Logger::LogLevel logLevel, ScriptSource so
|
|||
printf("%s\n", formattedLogLine.c_str());
|
||||
|
||||
for (Logger::LogListener listener : logListeners) {
|
||||
listener(logLevel, message, source);
|
||||
listener(logLevel, message);
|
||||
}
|
||||
|
||||
if (logLevel == Logger::LogLevel::FATAL_ERROR) {
|
||||
|
@ -51,6 +52,20 @@ void Logger::log(std::string message, Logger::LogLevel logLevel, ScriptSource so
|
|||
}
|
||||
}
|
||||
|
||||
void Logger::trace(std::string source, int line, void* userData) {
|
||||
std::string message = "'" + source + "' Line " + std::to_string(line);
|
||||
|
||||
log(message, Logger::LogLevel::TRACE);
|
||||
|
||||
for (Logger::TraceLogListener listener : traceLogListeners) {
|
||||
listener(message, source, line, userData);
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::addLogListener(Logger::LogListener listener) {
|
||||
logListeners.push_back(listener);
|
||||
}
|
||||
|
||||
void Logger::addLogListener(Logger::TraceLogListener listener) {
|
||||
traceLogListeners.push_back(listener);
|
||||
}
|
|
@ -1,11 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include <format>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class Script;
|
||||
|
||||
namespace Logger {
|
||||
enum class LogLevel {
|
||||
INFO,
|
||||
|
@ -16,39 +14,34 @@ namespace Logger {
|
|||
FATAL_ERROR,
|
||||
};
|
||||
|
||||
struct ScriptSource {
|
||||
std::shared_ptr<Script> script;
|
||||
int line;
|
||||
};
|
||||
|
||||
typedef std::function<void(LogLevel logLevel, std::string message, ScriptSource source)> LogListener;
|
||||
typedef std::function<void(LogLevel logLevel, std::string message)> LogListener;
|
||||
typedef std::function<void(std::string message, std::string source, int line, void* userData)> TraceLogListener;
|
||||
|
||||
extern std::string currentLogDir;
|
||||
|
||||
void init();
|
||||
void finish();
|
||||
void addLogListener(LogListener);
|
||||
void addLogListener(TraceLogListener);
|
||||
|
||||
void log(std::string message, LogLevel logLevel, ScriptSource source = {});
|
||||
void log(std::string message, LogLevel logLevel);
|
||||
inline void info(std::string message) { log(message, LogLevel::INFO); }
|
||||
inline void debug(std::string message) { log(message, LogLevel::DEBUG); }
|
||||
inline void warning(std::string message) { log(message, LogLevel::WARNING); }
|
||||
inline void error(std::string message) { log(message, LogLevel::ERROR); }
|
||||
inline void fatalError(std::string message) { log(message, LogLevel::FATAL_ERROR); }
|
||||
inline void trace(std::string message) { log(message, LogLevel::TRACE); };
|
||||
|
||||
template <typename ...Args>
|
||||
void scriptLogf(std::string format, LogLevel logLevel, ScriptSource source, Args&&... args) {
|
||||
char message[200];
|
||||
sprintf(message, format.c_str(), args...);
|
||||
log(message, logLevel, source);
|
||||
}
|
||||
inline void traceStart() { log("Stack start", LogLevel::TRACE); }
|
||||
inline void traceEnd() { log("Stack end", LogLevel::TRACE); }
|
||||
void trace(std::string source, int line, void* userData = nullptr);
|
||||
|
||||
template <typename ...Args>
|
||||
void logf(std::string format, LogLevel logLevel, Args&&... args) {
|
||||
scriptLogf(format, logLevel, {}, args...);
|
||||
char message[200];
|
||||
sprintf(message, format.c_str(), args...);
|
||||
log(message, logLevel);
|
||||
}
|
||||
|
||||
|
||||
template <typename ...Args> inline void infof(std::string format, Args&&... args) { logf(format, LogLevel::INFO, args...); }
|
||||
template <typename ...Args> inline void debugf(std::string format, Args&&... args) { logf(format, LogLevel::DEBUG, args...); }
|
||||
template <typename ...Args> inline void warningf(std::string format, Args&&... args) { logf(format, LogLevel::WARNING, args...); }
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include "panic.h"
|
||||
#include <memory>
|
||||
|
||||
Service::Service(const InstanceType* type) : Instance(type) {}
|
||||
Service::Service(const InstanceType* type) : Instance(type){}
|
||||
|
||||
// Fail if parented to non-datamodel, otherwise lock parent
|
||||
void Service::OnParentUpdated(std::optional<std::shared_ptr<Instance>> oldParent, std::optional<std::shared_ptr<Instance>> newParent) {
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
#include "hint.h"
|
||||
|
||||
Hint::Hint(): Message(&TYPE) {}
|
||||
Hint::~Hint() = default;
|
|
@ -1,18 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "objects/annotation.h"
|
||||
#include "objects/base/instance.h"
|
||||
#include "objects/message.h"
|
||||
#include <memory>
|
||||
|
||||
// Dims the player's screen and displays some centered text
|
||||
class DEF_INST_(explorer_icon="message") Hint : public Message {
|
||||
AUTOGEN_PREAMBLE
|
||||
|
||||
public:
|
||||
Hint();
|
||||
~Hint();
|
||||
|
||||
static inline std::shared_ptr<Hint> New() { return std::make_shared<Hint>(); };
|
||||
static inline std::shared_ptr<Instance> Create() { return std::make_shared<Hint>(); };
|
||||
};
|
|
@ -1,5 +0,0 @@
|
|||
#include "message.h"
|
||||
|
||||
Message::Message(const InstanceType* type) : Instance(type) {}
|
||||
Message::Message(): Instance(&TYPE) {}
|
||||
Message::~Message() = default;
|
|
@ -1,21 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "objects/annotation.h"
|
||||
#include "objects/base/instance.h"
|
||||
#include <memory>
|
||||
|
||||
// Dims the player's screen and displays some centered text
|
||||
class DEF_INST_(explorer_icon="message") Message : public Instance {
|
||||
AUTOGEN_PREAMBLE
|
||||
|
||||
protected:
|
||||
Message(const InstanceType* type);
|
||||
public:
|
||||
Message();
|
||||
~Message();
|
||||
|
||||
DEF_PROP std::string text;
|
||||
|
||||
static inline std::shared_ptr<Message> New() { return std::make_shared<Message>(); };
|
||||
static inline std::shared_ptr<Instance> Create() { return std::make_shared<Message>(); };
|
||||
};
|
|
@ -1,11 +1,9 @@
|
|||
#include "meta.h"
|
||||
#include "objects/folder.h"
|
||||
#include "objects/hint.h"
|
||||
#include "objects/joint/jointinstance.h"
|
||||
#include "objects/joint/rotate.h"
|
||||
#include "objects/joint/rotatev.h"
|
||||
#include "objects/joint/weld.h"
|
||||
#include "objects/message.h"
|
||||
#include "objects/service/jointsservice.h"
|
||||
#include "objects/model.h"
|
||||
#include "objects/part.h"
|
||||
|
@ -29,8 +27,6 @@ std::map<std::string, const InstanceType*> INSTANCE_MAP = {
|
|||
{ "JointInstance", &JointInstance::TYPE },
|
||||
{ "Script", &Script::TYPE },
|
||||
{ "Model", &Model::TYPE },
|
||||
{ "Message", &Message::TYPE },
|
||||
{ "Hint", &Hint::TYPE },
|
||||
// { "Folder", &Folder::TYPE },
|
||||
|
||||
// Services
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#include "script.h"
|
||||
#include "common.h"
|
||||
#include "datatypes/variant.h"
|
||||
#include "lauxlib.h"
|
||||
#include "logger.h"
|
||||
#include "objects/base/instance.h"
|
||||
|
@ -13,8 +12,13 @@
|
|||
#include <algorithm>
|
||||
#include <memory>
|
||||
|
||||
int script_wait(lua_State*);
|
||||
int script_delay(lua_State*);
|
||||
int script_errhandler(lua_State*);
|
||||
|
||||
// TODO: Move this to a shared header
|
||||
const char* WRAPPER_SRC = "local func, errhandler = ... return function(...) local args = {...} xpcall(function() func(unpack(args)) end, errhandler) end";
|
||||
|
||||
Script::Script(): Instance(&TYPE) {
|
||||
source = "print(\"Hello, world!\")";
|
||||
}
|
||||
|
@ -32,8 +36,30 @@ void Script::Run() {
|
|||
this->thread = lua_newthread(L);
|
||||
lua_State* Lt = thread;
|
||||
|
||||
// Initialize script globals
|
||||
lua_getglobal(Lt, "_G");
|
||||
|
||||
InstanceRef(shared_from_this()).PushLuaValue(Lt);
|
||||
lua_setfield(Lt, -2, "script");
|
||||
|
||||
InstanceRef(dataModel().value()).PushLuaValue(Lt);
|
||||
lua_setfield(Lt, -2, "game");
|
||||
|
||||
InstanceRef(dataModel().value()->GetService<Workspace>()).PushLuaValue(Lt);
|
||||
lua_setfield(Lt, -2, "workspace");
|
||||
|
||||
lua_pushlightuserdata(Lt, scriptContext.get());
|
||||
lua_pushcclosure(Lt, script_wait, 1);
|
||||
lua_setfield(Lt, -2, "wait");
|
||||
|
||||
lua_pushlightuserdata(Lt, scriptContext.get());
|
||||
lua_pushcclosure(Lt, script_delay, 1);
|
||||
lua_setfield(Lt, -2, "delay");
|
||||
|
||||
lua_pop(Lt, 1); // _G
|
||||
|
||||
// Push wrapper as thread function
|
||||
lua_getfield(Lt, LUA_REGISTRYINDEX, "LuaPCallWrapper");
|
||||
luaL_loadbuffer(Lt, WRAPPER_SRC, strlen(WRAPPER_SRC), "=PCALL_WRAPPER");
|
||||
|
||||
// Load source code and push onto thread as upvalue for wrapper
|
||||
int status = luaL_loadbuffer(Lt, source.c_str(), source.size(), this->GetFullName().c_str());
|
||||
|
@ -45,21 +71,6 @@ void Script::Run() {
|
|||
return;
|
||||
}
|
||||
|
||||
// Initialize script globals
|
||||
scriptContext->NewEnvironment(Lt); // Pushes envtable, metatable
|
||||
|
||||
// Set script in metatable source
|
||||
InstanceRef(shared_from_this()).PushLuaValue(Lt);
|
||||
lua_setfield(Lt, -2, "source");
|
||||
|
||||
lua_pop(Lt, 1); // Pop metatable
|
||||
|
||||
// Set script in environment
|
||||
InstanceRef(shared_from_this()).PushLuaValue(Lt);
|
||||
lua_setfield(Lt, -2, "script");
|
||||
|
||||
lua_setfenv(Lt, -2); // Set env of loaded function
|
||||
|
||||
// Push our error handler and then generate the wrapped function
|
||||
lua_pushcfunction(Lt, script_errhandler);
|
||||
lua_call(Lt, 2, 1);
|
||||
|
@ -75,32 +86,33 @@ void Script::Stop() {
|
|||
// TODO:
|
||||
}
|
||||
|
||||
static std::shared_ptr<Script> getfsource(lua_State* L, lua_Debug* dbg) {
|
||||
int top = lua_gettop(L);
|
||||
int script_wait(lua_State* L) {
|
||||
ScriptContext* scriptContext = (ScriptContext*)lua_touserdata(L, lua_upvalueindex(1));
|
||||
float secs = lua_gettop(L) == 0 ? 0.03 : std::max(luaL_checknumber(L, 1), 0.03);
|
||||
if (lua_gettop(L) > 0) lua_pop(L, 1); // pop secs
|
||||
|
||||
lua_getinfo(L, "f", dbg);
|
||||
lua_getfenv(L, -1); // Get fenv of stack pos
|
||||
if (lua_isnil(L, -1)) { // No env could be found
|
||||
lua_settop(L, top);
|
||||
return nullptr;
|
||||
}
|
||||
scriptContext->PushThreadSleep(L, secs);
|
||||
|
||||
// Get source from metatable
|
||||
lua_getmetatable(L, -1);
|
||||
lua_getfield(L, -1, "source");
|
||||
// Yield
|
||||
return lua_yield(L, 0);
|
||||
}
|
||||
|
||||
auto result = InstanceRef::FromLuaValue(L, -1);
|
||||
if (!result) {
|
||||
lua_settop(L, top);
|
||||
return nullptr;
|
||||
}
|
||||
int script_delay(lua_State* L) {
|
||||
ScriptContext* scriptContext = (ScriptContext*)lua_touserdata(L, lua_upvalueindex(1));
|
||||
float secs = std::max(luaL_checknumber(L, 1), 0.03);
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
|
||||
lua_settop(L, top);
|
||||
lua_State* Lt = lua_newthread(L); // Create a new thread
|
||||
// I think this is memory abuse??
|
||||
// Wouldn't popping the thread in this case make it eligible for garbage collection?
|
||||
lua_pop(L, 1); // pop the newly created thread so that xmove moves func instead of it into itself
|
||||
lua_xmove(L, Lt, 1); // move func
|
||||
lua_pop(L, 1); // pop secs
|
||||
|
||||
std::shared_ptr<Instance> ref = result.expect().get<InstanceRef>();
|
||||
if (!ref->IsA<Script>()) return nullptr;
|
||||
// Schedule next run
|
||||
scriptContext->PushThreadSleep(Lt, secs);
|
||||
|
||||
return ref->CastTo<Script>().expect();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int script_errhandler(lua_State* L) {
|
||||
|
@ -109,7 +121,7 @@ int script_errhandler(lua_State* L) {
|
|||
|
||||
// Traceback
|
||||
|
||||
Logger::trace("Stack start");
|
||||
Logger::traceStart();
|
||||
|
||||
lua_Debug dbg;
|
||||
int stack = 1;
|
||||
|
@ -119,13 +131,10 @@ int script_errhandler(lua_State* L) {
|
|||
if (strcmp(dbg.what, "C") == 0 || strcmp(dbg.source, "=PCALL_WRAPPER") == 0)
|
||||
continue;
|
||||
|
||||
// Find script source
|
||||
std::shared_ptr<Script> source = getfsource(L, &dbg);
|
||||
|
||||
Logger::scriptLogf("'%s', Line %d", Logger::LogLevel::TRACE, {source, dbg.currentline}, dbg.source, dbg.currentline);
|
||||
Logger::trace(dbg.source, dbg.currentline);
|
||||
}
|
||||
|
||||
Logger::trace("Stack end");
|
||||
Logger::traceEnd();
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,30 +1,22 @@
|
|||
#include "scriptcontext.h"
|
||||
#include "datatypes/cframe.h"
|
||||
#include "datatypes/color3.h"
|
||||
#include "datatypes/ref.h"
|
||||
#include "datatypes/vector.h"
|
||||
#include "logger.h"
|
||||
#include "objects/datamodel.h"
|
||||
#include "objects/service/workspace.h"
|
||||
#include "timeutil.h"
|
||||
#include <ctime>
|
||||
#include <string>
|
||||
#include "luaapis.h" // IWYU pragma: keep
|
||||
|
||||
const char* WRAPPER_SRC = "local func, errhandler = ... return function(...) local args = {...} xpcall(function() func(unpack(args)) end, errhandler) end";
|
||||
|
||||
int g_wait(lua_State*);
|
||||
int g_delay(lua_State*);
|
||||
static int g_print(lua_State*);
|
||||
static int g_require(lua_State*);
|
||||
static const luaL_Reg luaglobals [] = {
|
||||
static const struct luaL_Reg luaglobals [] = {
|
||||
{"print", g_print},
|
||||
{"require", g_require},
|
||||
{NULL, NULL} /* end of array */
|
||||
};
|
||||
|
||||
std::string unsafe_globals[] = {
|
||||
// Todo implement our own "safe" setfenv/getfenv
|
||||
"loadfile", "loadstring", "load", "dofile", "getfenv", "setfenv"
|
||||
};
|
||||
|
||||
|
@ -56,29 +48,6 @@ void ScriptContext::InitService() {
|
|||
Color3::PushLuaLibrary(state);
|
||||
Instance::PushLuaLibrary(state);
|
||||
|
||||
// Add other globals
|
||||
lua_getglobal(state, "_G");
|
||||
|
||||
InstanceRef(dataModel().value()).PushLuaValue(state);
|
||||
lua_setfield(state, -2, "game");
|
||||
|
||||
InstanceRef(dataModel().value()->GetService<Workspace>()).PushLuaValue(state);
|
||||
lua_setfield(state, -2, "workspace");
|
||||
|
||||
lua_pushlightuserdata(state, this);
|
||||
lua_pushcclosure(state, g_wait, 1);
|
||||
lua_setfield(state, -2, "wait");
|
||||
|
||||
lua_pushlightuserdata(state, this);
|
||||
lua_pushcclosure(state, g_delay, 1);
|
||||
lua_setfield(state, -2, "delay");
|
||||
|
||||
lua_pop(state, 1); // _G
|
||||
|
||||
// Add wrapper function
|
||||
luaL_loadbuffer(state, WRAPPER_SRC, strlen(WRAPPER_SRC), "=PCALL_WRAPPER");
|
||||
lua_setfield(state, LUA_REGISTRYINDEX, "LuaPCallWrapper");
|
||||
|
||||
// TODO: custom os library
|
||||
|
||||
// Override print
|
||||
|
@ -162,27 +131,6 @@ void ScriptContext::RunSleepingThreads() {
|
|||
schedTime = tu_clock_micros() - startTime;
|
||||
}
|
||||
|
||||
void ScriptContext::NewEnvironment(lua_State* L) {
|
||||
lua_newtable(L); // Env table
|
||||
lua_newtable(L); // Metatable
|
||||
|
||||
// Push __index
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
lua_setfield(L, -2, "__index");
|
||||
|
||||
// Push __metatable
|
||||
lua_pushstring(L, "metatable is locked");
|
||||
lua_setfield(L, -2, "__metatable");
|
||||
|
||||
// Copy metatable and set the env table's metatable
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setmetatable(L, -3);
|
||||
|
||||
// Remainder on stack:
|
||||
// 1. Env table
|
||||
// 2. Metatable
|
||||
}
|
||||
|
||||
// https://www.lua.org/source/5.1/lbaselib.c.html
|
||||
static int g_print(lua_State* L) {
|
||||
std::string buf;
|
||||
|
@ -213,33 +161,4 @@ static int g_require(lua_State* L) {
|
|||
if (nargs < 1) return luaL_error(L, "expected argument module");
|
||||
|
||||
return luaL_error(L, "require is not yet implemented");
|
||||
}
|
||||
|
||||
int g_wait(lua_State* L) {
|
||||
ScriptContext* scriptContext = (ScriptContext*)lua_touserdata(L, lua_upvalueindex(1));
|
||||
float secs = lua_gettop(L) == 0 ? 0.03 : std::max(luaL_checknumber(L, 1), 0.03);
|
||||
if (lua_gettop(L) > 0) lua_pop(L, 1); // pop secs
|
||||
|
||||
scriptContext->PushThreadSleep(L, secs);
|
||||
|
||||
// Yield
|
||||
return lua_yield(L, 0);
|
||||
}
|
||||
|
||||
int g_delay(lua_State* L) {
|
||||
ScriptContext* scriptContext = (ScriptContext*)lua_touserdata(L, lua_upvalueindex(1));
|
||||
float secs = std::max(luaL_checknumber(L, 1), 0.03);
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
|
||||
lua_State* Lt = lua_newthread(L); // Create a new thread
|
||||
// I think this is memory abuse??
|
||||
// Wouldn't popping the thread in this case make it eligible for garbage collection?
|
||||
lua_pop(L, 1); // pop the newly created thread so that xmove moves func instead of it into itself
|
||||
lua_xmove(L, Lt, 1); // move func
|
||||
lua_pop(L, 1); // pop secs
|
||||
|
||||
// Schedule next run
|
||||
scriptContext->PushThreadSleep(Lt, secs);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -32,8 +32,5 @@ public:
|
|||
void PushThreadSleep(lua_State* thread, float delay);
|
||||
void RunSleepingThreads();
|
||||
|
||||
// Generates an environment with a metatable and pushes it both the env table and metatable in order onto the stack
|
||||
void NewEnvironment(lua_State* state);
|
||||
|
||||
static inline std::shared_ptr<Instance> Create() { return std::make_shared<ScriptContext>(); };
|
||||
};
|
|
@ -11,8 +11,6 @@ extern Texture* debugFontTexture;
|
|||
extern Shader* debugFontShader;
|
||||
extern Shader* identityShader;
|
||||
|
||||
void drawRect(int x, int y, int width, int height, glm::vec4 color);
|
||||
|
||||
void drawChar(char c, int x, int y, float scale=1.f) {
|
||||
debugFontShader->use();
|
||||
debugFontTexture->activate(1);
|
||||
|
@ -43,6 +41,22 @@ void drawString(std::string str, int x, int y, float scale=1.f) {
|
|||
}
|
||||
}
|
||||
|
||||
void drawRect(int x, int y, int w, int h, glm::vec4 color) {
|
||||
identityShader->use();
|
||||
identityShader->set("aColor", color);
|
||||
|
||||
float x0 = 2*float(x)/viewportWidth-1, y0 = 2*float(y)/viewportHeight-1, x1 = 2*float(x + w)/viewportWidth-1, y1 = 2*float(y + h)/viewportHeight-1;
|
||||
float tmp;
|
||||
tmp = -y0, y0 = -y1, y1 = tmp;
|
||||
|
||||
glBegin(GL_QUADS);
|
||||
glVertex3f(x0, y0, 0);
|
||||
glVertex3f(x1, y0, 0);
|
||||
glVertex3f(x1, y1, 0);
|
||||
glVertex3f(x0, y1, 0);
|
||||
glEnd();
|
||||
}
|
||||
|
||||
static tu_time_t lastTime;
|
||||
extern tu_time_t renderTime;
|
||||
extern tu_time_t physTime;
|
||||
|
|
|
@ -63,8 +63,7 @@ std::shared_ptr<Font> loadFont(std::string fontName) {
|
|||
|
||||
std::shared_ptr<Font> font = std::make_shared<Font>();
|
||||
|
||||
FT_Set_Pixel_Sizes(face, 0, 16);
|
||||
font->height = face->size->metrics.y_ppem;
|
||||
FT_Set_Pixel_Sizes(face, 0, 48);
|
||||
|
||||
// Load each glyph
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
@ -132,7 +131,7 @@ void drawText(std::shared_ptr<Font> font, std::string text, float x, float y, fl
|
|||
Character ch = font->characters[c];
|
||||
|
||||
float xpos = x + ch.bearing.x * scale;
|
||||
float ypos = viewportHeight - y - font->height - (ch.size.y - ch.bearing.y) * scale;
|
||||
float ypos = y - (ch.size.y - ch.bearing.y) * scale;
|
||||
|
||||
float w = ch.size.x * scale;
|
||||
float h = ch.size.y * scale;
|
||||
|
@ -160,17 +159,4 @@ void drawText(std::shared_ptr<Font> font, std::string text, float x, float y, fl
|
|||
x += (ch.advance >> 6) * scale; // bitshift by 6 to get value in pixels (2^6 = 64)
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
float calcTextWidth(std::shared_ptr<Font> font, std::string text, float scale) {
|
||||
float x = 0;
|
||||
// iterate through all characters
|
||||
for (size_t i = 0; i < text.size(); i++) {
|
||||
unsigned char c = text[i];
|
||||
Character ch = font->characters[c];
|
||||
|
||||
x += (ch.advance >> 6) * scale;
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
|
@ -14,12 +14,10 @@ struct Character {
|
|||
};
|
||||
|
||||
struct Font {
|
||||
unsigned int height;
|
||||
Character characters[128];
|
||||
};
|
||||
|
||||
void fontInit();
|
||||
void fontFinish();
|
||||
std::shared_ptr<Font> loadFont(std::string fontName);
|
||||
void drawText(std::shared_ptr<Font> font, std::string text, float x, float y, float scale=1.f, glm::vec3 color = glm::vec3(1,1,1));
|
||||
float calcTextWidth(std::shared_ptr<Font> font, std::string text, float scale = 1.f);
|
||||
void drawText(std::shared_ptr<Font> font, std::string text, float x, float y, float scale=1.f, glm::vec3 color = glm::vec3(1,1,1));
|
|
@ -20,8 +20,6 @@
|
|||
#include "datatypes/vector.h"
|
||||
#include "handles.h"
|
||||
#include "math_helper.h"
|
||||
#include "objects/hint.h"
|
||||
#include "objects/message.h"
|
||||
#include "objects/service/selection.h"
|
||||
#include "partassembly.h"
|
||||
#include "rendering/font.h"
|
||||
|
@ -64,8 +62,7 @@ bool wireframeRendering = false;
|
|||
int viewportWidth, viewportHeight;
|
||||
|
||||
void renderDebugInfo();
|
||||
void drawRect(int x, int y, int width, int height, glm::vec4 color);
|
||||
inline void drawRect(int x, int y, int width, int height, glm::vec3 color) { return drawRect(x, y, width, height, glm::vec4(color, 1)); };
|
||||
void drawRect(int x, int y, int width, int height, glm::vec3 color);
|
||||
|
||||
void renderInit(int width, int height) {
|
||||
viewportWidth = width, viewportHeight = height;
|
||||
|
@ -652,33 +649,6 @@ void addDebugRenderCFrame(CFrame frame, Color3 color) {
|
|||
DEBUG_CFRAMES.push_back(std::make_pair(frame, color));
|
||||
}
|
||||
|
||||
void renderMessages() {
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
// glEnable(GL_DEPTH_TEST);
|
||||
glDisable(GL_CULL_FACE);
|
||||
// glEnable(GL_BLEND);
|
||||
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
for (auto it = gWorkspace()->GetDescendantsStart(); it != gWorkspace()->GetDescendantsEnd(); it++) {
|
||||
if (!it->IsA<Message>()) continue;
|
||||
std::shared_ptr<Message> message = it->CastTo<Message>().expect();
|
||||
|
||||
float textWidth = calcTextWidth(sansSerif, message->text);
|
||||
|
||||
// Render hint
|
||||
if (message->GetClass() == &Hint::TYPE) {
|
||||
drawRect(0, 0, viewportWidth, 20, glm::vec4(0,0,0,1));
|
||||
drawText(sansSerif, message->text, (viewportWidth - textWidth) / 2, 0);
|
||||
} else {
|
||||
// Don't draw if text is empty
|
||||
if (message->text == "") continue;
|
||||
|
||||
drawRect(0, 0, viewportWidth, viewportHeight, glm::vec4(0.5));
|
||||
drawText(sansSerif, message->text, ((float)viewportWidth - textWidth) / 2, ((float)viewportHeight - sansSerif->height) / 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tu_time_t renderTime;
|
||||
void render() {
|
||||
tu_time_t startTime = tu_clock_micros();
|
||||
|
@ -698,14 +668,13 @@ void render() {
|
|||
renderWireframe();
|
||||
if (debugRendererEnabled)
|
||||
renderDebugInfo();
|
||||
renderMessages();
|
||||
// TODO: Make this a debug flag
|
||||
// renderAABB();
|
||||
|
||||
renderTime = tu_clock_micros() - startTime;
|
||||
}
|
||||
|
||||
void drawRect(int x, int y, int width, int height, glm::vec4 color) {
|
||||
void drawRect(int x, int y, int width, int height, glm::vec3 color) {
|
||||
// GL_CULL_FACE has to be disabled as we are flipping the order of the vertices here, besides we don't really care about it
|
||||
glDisable(GL_CULL_FACE);
|
||||
glm::mat4 model(1.0f); // Same applies to this VV
|
||||
|
|
|
@ -38,8 +38,6 @@ set(PROJECT_SOURCES
|
|||
panes/outputtextview.cpp
|
||||
script/scriptdocument.h
|
||||
script/scriptdocument.cpp
|
||||
script/commandedit.h
|
||||
script/commandedit.cpp
|
||||
aboutdialog.ui
|
||||
aboutdialog.h
|
||||
aboutdialog.cpp
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#include "mainwindow.h"
|
||||
#include "./ui_mainwindow.h"
|
||||
#include "script/commandedit.h"
|
||||
#include "common.h"
|
||||
#include "aboutdialog.h"
|
||||
#include "logger.h"
|
||||
|
@ -15,7 +14,6 @@
|
|||
#include <qevent.h>
|
||||
#include <qglobal.h>
|
||||
#include <qkeysequence.h>
|
||||
#include <qlabel.h>
|
||||
#include <qmessagebox.h>
|
||||
#include <qmimedata.h>
|
||||
#include <qnamespace.h>
|
||||
|
@ -119,8 +117,6 @@ MainWindow::MainWindow(QWidget *parent)
|
|||
undoManager.SetUndoStateListener([&]() {
|
||||
updateToolbars();
|
||||
});
|
||||
|
||||
setUpCommandBar();
|
||||
}
|
||||
|
||||
void MainWindow::closeEvent(QCloseEvent* evt) {
|
||||
|
@ -146,14 +142,6 @@ void MainWindow::closeEvent(QCloseEvent* evt) {
|
|||
#endif
|
||||
}
|
||||
|
||||
void MainWindow::setUpCommandBar() {
|
||||
CommandEdit* commandEdit;
|
||||
QToolBar* commandBar = ui->commandBar;
|
||||
commandBar->layout()->setSpacing(5);
|
||||
commandBar->addWidget(new QLabel(tr("Command ")));
|
||||
commandBar->addWidget(commandEdit = new CommandEdit());
|
||||
}
|
||||
|
||||
void MainWindow::connectActionHandlers() {
|
||||
connect(ui->actionToolSelect, &QAction::triggered, this, [&]() { selectedTool = TOOL_SELECT; updateToolbars(); });
|
||||
connect(ui->actionToolMove, &QAction::triggered, this, [&](bool state) { selectedTool = state ? TOOL_MOVE : TOOL_SELECT; updateToolbars(); });
|
||||
|
@ -610,23 +598,19 @@ ScriptDocument* MainWindow::findScriptWindow(std::shared_ptr<Script> script) {
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
void MainWindow::openScriptDocument(std::shared_ptr<Script> script, int line) {
|
||||
void MainWindow::openScriptDocument(std::shared_ptr<Script> script) {
|
||||
// Document already exists, don't open it
|
||||
ScriptDocument* doc = findScriptWindow(script);
|
||||
if (doc != nullptr) {
|
||||
ui->mdiArea->setActiveSubWindow(doc);
|
||||
doc->setFocus();
|
||||
if (line > -1) doc->moveCursor(line);
|
||||
return;
|
||||
}
|
||||
|
||||
doc = new ScriptDocument(script);
|
||||
doc->setAttribute(Qt::WA_DeleteOnClose, true);
|
||||
if (line > -1) doc->moveCursor(line);
|
||||
ui->mdiArea->addSubWindow(doc);
|
||||
ui->mdiArea->setActiveSubWindow(doc);
|
||||
doc->showMaximized();
|
||||
doc->setFocus();
|
||||
}
|
||||
|
||||
void MainWindow::closeScriptDocument(std::shared_ptr<Script> script) {
|
||||
|
|
|
@ -58,7 +58,7 @@ public:
|
|||
GridSnappingMode snappingMode;
|
||||
bool editSoundEffects = true;
|
||||
|
||||
void openScriptDocument(std::shared_ptr<Script>, int line = -1);
|
||||
void openScriptDocument(std::shared_ptr<Script>);
|
||||
void closeScriptDocument(std::shared_ptr<Script>);
|
||||
|
||||
void openFile(std::string path);
|
||||
|
@ -69,11 +69,11 @@ public:
|
|||
private:
|
||||
PlaceDocument* placeDocument;
|
||||
|
||||
void setUpCommandBar();
|
||||
void connectActionHandlers();
|
||||
void updateToolbars();
|
||||
void closeEvent(QCloseEvent* evt) override;
|
||||
ScriptDocument* findScriptWindow(std::shared_ptr<Script>);
|
||||
|
||||
void connectActionHandlers();
|
||||
|
||||
std::optional<std::string> openFileDialog(QString filter, QString defaultExtension, QFileDialog::AcceptMode acceptMode, QString title = "");
|
||||
};
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Openblocks Editor</string>
|
||||
<string>MainWindow</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
|
@ -172,7 +172,7 @@
|
|||
</widget>
|
||||
<widget class="QToolBar" name="editTools">
|
||||
<property name="windowTitle">
|
||||
<string>Edit Tools</string>
|
||||
<string>toolBar_3</string>
|
||||
</property>
|
||||
<attribute name="toolBarArea">
|
||||
<enum>TopToolBarArea</enum>
|
||||
|
@ -237,7 +237,7 @@
|
|||
</widget>
|
||||
<widget class="QToolBar" name="toolBar">
|
||||
<property name="windowTitle">
|
||||
<string>Sound Controls</string>
|
||||
<string>toolBar</string>
|
||||
</property>
|
||||
<attribute name="toolBarArea">
|
||||
<enum>TopToolBarArea</enum>
|
||||
|
@ -247,17 +247,6 @@
|
|||
</attribute>
|
||||
<addaction name="actionToggleEditSounds"/>
|
||||
</widget>
|
||||
<widget class="QToolBar" name="commandBar">
|
||||
<property name="windowTitle">
|
||||
<string>Command Bar</string>
|
||||
</property>
|
||||
<attribute name="toolBarArea">
|
||||
<enum>BottomToolBarArea</enum>
|
||||
</attribute>
|
||||
<attribute name="toolBarBreak">
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
</widget>
|
||||
<action name="actionAddPart">
|
||||
<property name="icon">
|
||||
<iconset>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "outputtextview.h"
|
||||
#include "logger.h"
|
||||
#include "mainwindow.h"
|
||||
#include "objects/script.h"
|
||||
#include "panes/outputtextview.h"
|
||||
#include <QEvent>
|
||||
#include <QTextEdit>
|
||||
|
@ -12,7 +12,8 @@
|
|||
#include <string>
|
||||
|
||||
OutputTextView::OutputTextView(QWidget* parent) : QTextEdit(parent) {
|
||||
Logger::addLogListener(std::bind(&OutputTextView::handleLog, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
||||
Logger::addLogListener(std::bind(&OutputTextView::handleLog, this, std::placeholders::_1, std::placeholders::_2));
|
||||
Logger::addLogListener(std::bind(&OutputTextView::handleLogTrace, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
|
||||
ensureCursorVisible();
|
||||
|
||||
QFont font("");
|
||||
|
@ -35,9 +36,13 @@ OutputTextView::OutputTextView(QWidget* parent) : QTextEdit(parent) {
|
|||
|
||||
OutputTextView::~OutputTextView() = default;
|
||||
|
||||
void OutputTextView::handleLog(Logger::LogLevel logLevel, std::string message, Logger::ScriptSource source) {
|
||||
void OutputTextView::handleLog(Logger::LogLevel logLevel, std::string message) {
|
||||
if (logLevel == Logger::LogLevel::DEBUG) return;
|
||||
|
||||
// Skip if trace, as that is handled by handleLogTrace
|
||||
if (logLevel == Logger::LogLevel::TRACE && !message.starts_with("Stack"))
|
||||
return;
|
||||
|
||||
// https://stackoverflow.com/a/61722734/16255372
|
||||
moveCursor(QTextCursor::MoveOperation::End);
|
||||
QTextCursor cursor = textCursor();
|
||||
|
@ -53,13 +58,24 @@ void OutputTextView::handleLog(Logger::LogLevel logLevel, std::string message, L
|
|||
format.setFontWeight(QFont::Bold);
|
||||
}
|
||||
|
||||
// Add anchor point if source is provided
|
||||
if (source.script != nullptr) {
|
||||
cursor.insertText(message.c_str(), format);
|
||||
cursor.insertText("\n", QTextCharFormat());
|
||||
}
|
||||
|
||||
void OutputTextView::handleLogTrace(std::string message, std::string source, int line, void* userData) {
|
||||
std::weak_ptr<Script>* script = (std::weak_ptr<Script>*)userData;
|
||||
|
||||
// https://stackoverflow.com/a/61722734/16255372
|
||||
QTextCursor cursor = textCursor();
|
||||
QTextCharFormat format = cursor.charFormat();
|
||||
format.setForeground(QColor(0, 127, 255));
|
||||
|
||||
if (userData != nullptr && !script->expired()) {
|
||||
int id = stackTraceScriptsLastId++;
|
||||
stackTraceScripts[id] = source.script;
|
||||
stackTraceScripts[id] = *script;
|
||||
|
||||
format.setAnchor(true);
|
||||
format.setAnchorHref(QString::number(id) + ":" + QString::number(source.line));
|
||||
format.setAnchorHref(QString::number(id));
|
||||
}
|
||||
|
||||
cursor.insertText(message.c_str(), format);
|
||||
|
@ -71,13 +87,11 @@ void OutputTextView::mousePressEvent(QMouseEvent *e) {
|
|||
QString anchor = anchorAt(e->pos());
|
||||
if (anchor == "" || e->modifiers() & Qt::AltModifier) return QTextEdit::mousePressEvent(e);
|
||||
|
||||
int idx = anchor.indexOf(":");
|
||||
int id = anchor.mid(0, idx).toInt(), line = anchor.mid(idx+1).toInt();
|
||||
auto script = stackTraceScripts[id];
|
||||
auto script = stackTraceScripts[anchor.toInt()];
|
||||
if (script.expired()) return QTextEdit::mousePressEvent(e);
|
||||
|
||||
MainWindow* mainWnd = dynamic_cast<MainWindow*>(window());
|
||||
mainWnd->openScriptDocument(script.lock(), line);
|
||||
mainWnd->openScriptDocument(script.lock());
|
||||
}
|
||||
|
||||
void OutputTextView::mouseReleaseEvent(QMouseEvent *e) {
|
||||
|
|
|
@ -14,7 +14,8 @@ private:
|
|||
void mouseReleaseEvent(QMouseEvent *e) override;
|
||||
void mouseMoveEvent(QMouseEvent *e) override;
|
||||
|
||||
void handleLog(Logger::LogLevel, std::string, Logger::ScriptSource source);
|
||||
void handleLog(Logger::LogLevel, std::string);
|
||||
void handleLogTrace(std::string, std::string, int, void*);
|
||||
|
||||
std::map<int, std::weak_ptr<Script>> stackTraceScripts;
|
||||
int stackTraceScriptsLastId = 0;
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include <QStyledItemDelegate>
|
||||
#include <QPainter>
|
||||
#include <QTime>
|
||||
#include <cfloat>
|
||||
#include <cmath>
|
||||
#include <functional>
|
||||
#include <qapplication.h>
|
||||
|
@ -23,14 +24,6 @@
|
|||
#include <qnamespace.h>
|
||||
#include <qtreewidget.h>
|
||||
|
||||
QDoubleSpinBox* makeDoubleSpinBox(QWidget* parent = nullptr) {
|
||||
QDoubleSpinBox* spinBox = new QDoubleSpinBox(parent);
|
||||
spinBox->setMaximum(INFINITY);
|
||||
spinBox->setMinimum(-INFINITY);
|
||||
spinBox->setDecimals(4);
|
||||
return spinBox;
|
||||
}
|
||||
|
||||
class PropertiesItemDelegate : public QStyledItemDelegate {
|
||||
PropertiesView* view;
|
||||
public:
|
||||
|
@ -71,7 +64,7 @@ public:
|
|||
Vector3 vector = currentValue.get<Vector3>();
|
||||
float value = componentName == "X" ? vector.X() : componentName == "Y" ? vector.Y() : componentName == "Z" ? vector.Z() : 0;
|
||||
|
||||
QDoubleSpinBox* spinBox = makeDoubleSpinBox(parent);
|
||||
QDoubleSpinBox* spinBox = new QDoubleSpinBox(parent);
|
||||
spinBox->setValue(value);
|
||||
|
||||
return spinBox;
|
||||
|
@ -82,9 +75,9 @@ public:
|
|||
|
||||
if (meta.type.descriptor == &FLOAT_TYPE) {
|
||||
QDoubleSpinBox* spinBox = new QDoubleSpinBox(parent);
|
||||
spinBox->setValue(currentValue.get<float>());
|
||||
spinBox->setMinimum(-INFINITY);
|
||||
spinBox->setMaximum(INFINITY);
|
||||
spinBox->setValue(currentValue.get<float>());
|
||||
|
||||
if (meta.flags & PROP_UNIT_FLOAT) {
|
||||
spinBox->setMinimum(0);
|
||||
|
@ -228,8 +221,7 @@ public:
|
|||
: componentName == "Z" ? Vector3(prev.X(), prev.Y(), value) : prev;
|
||||
|
||||
inst->SetProperty(propertyName, newVector).expect();
|
||||
// SetProperty above already causes the composite to be rebuilt. So we get rid of it here to prevent errors
|
||||
// view->rebuildCompositeProperty(view->itemFromIndex(index.parent()), &Vector3::TYPE, newVector);
|
||||
view->rebuildCompositeProperty(view->itemFromIndex(index.parent()), &Vector3::TYPE, newVector);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,115 +0,0 @@
|
|||
#include "commandedit.h"
|
||||
#include "common.h"
|
||||
#include "logger.h"
|
||||
#include "lua.h"
|
||||
#include "objects/service/script/scriptcontext.h"
|
||||
#include "luaapis.h" // IWYU pragma: keep
|
||||
#include <qevent.h>
|
||||
#include <qlineedit.h>
|
||||
#include <qnamespace.h>
|
||||
|
||||
int script_errhandler(lua_State*);
|
||||
|
||||
CommandEdit::CommandEdit(QWidget* parent) : QLineEdit(parent) {
|
||||
connect(this, &QLineEdit::returnPressed, this, &CommandEdit::executeCommand);
|
||||
}
|
||||
|
||||
CommandEdit::~CommandEdit() = default;
|
||||
|
||||
void CommandEdit::executeCommand() {
|
||||
std::string command = this->text().toStdString();
|
||||
|
||||
// Output
|
||||
Logger::infof("> %s", command.c_str());
|
||||
|
||||
// Select all so that the user can type over it
|
||||
this->selectAll();
|
||||
|
||||
// Execute via Lua
|
||||
auto context = gDataModel->GetService<ScriptContext>();
|
||||
lua_State* L = context->state;
|
||||
|
||||
int top = lua_gettop(L);
|
||||
lua_State* Lt = lua_newthread(L);
|
||||
|
||||
// Push wrapper as thread function
|
||||
lua_getfield(Lt, LUA_REGISTRYINDEX, "LuaPCallWrapper");
|
||||
|
||||
// Load source code and push onto thread as upvalue for wrapper
|
||||
int status = luaL_loadstring(Lt, command.c_str());
|
||||
if (status != LUA_OK) {
|
||||
// Failed to parse/load chunk
|
||||
Logger::error(lua_tostring(Lt, -1));
|
||||
|
||||
lua_settop(L, top);
|
||||
return;
|
||||
}
|
||||
|
||||
getOrCreateEnvironment(Lt);
|
||||
lua_setfenv(Lt, -2); // Set env of loaded function
|
||||
|
||||
// Push our error handler and then generate the wrapped function
|
||||
lua_pushcfunction(Lt, script_errhandler);
|
||||
lua_call(Lt, 2, 1);
|
||||
|
||||
|
||||
// Resume the thread
|
||||
lua_resume(Lt, 0);
|
||||
|
||||
lua_pop(L, 1); // Pop the thread
|
||||
lua_settop(L, top);
|
||||
|
||||
// Push to history
|
||||
if (commandHistory.size() == 0 || commandHistory.back() != command) {
|
||||
historyIndex = commandHistory.size();
|
||||
commandHistory.push_back(command);
|
||||
}
|
||||
};
|
||||
|
||||
// Gets the command bar environment from the registry, or creates a new one and registers it
|
||||
void CommandEdit::getOrCreateEnvironment(lua_State* L) {
|
||||
auto context = gDataModel->GetService<ScriptContext>();
|
||||
|
||||
// Try to find existing environment
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "commandBarEnv");
|
||||
if (!lua_isnil(L, -1))
|
||||
return; // Return the found environment
|
||||
lua_pop(L, 1); // Pop nil
|
||||
|
||||
// Initialize script globals
|
||||
context->NewEnvironment(L); // Pushes envtable, metatable
|
||||
|
||||
// Set source in metatable
|
||||
lua_pushstring(L, "commandbar");
|
||||
lua_setfield(L, -2, "source");
|
||||
|
||||
lua_pop(L, 1); // Pop metatable
|
||||
|
||||
// Register it
|
||||
lua_pushvalue(L, -1); // Copy
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, "commandBarEnv");
|
||||
|
||||
// Remainder on stack:
|
||||
// 1. Env table
|
||||
}
|
||||
|
||||
void CommandEdit::keyPressEvent(QKeyEvent* evt) {
|
||||
switch (evt->key()) {
|
||||
case Qt::Key_Up:
|
||||
if (historyIndex > 0)
|
||||
historyIndex--;
|
||||
|
||||
if (commandHistory.size() > 0)
|
||||
this->setText(QString::fromStdString(commandHistory[historyIndex]));
|
||||
return;
|
||||
case Qt::Key_Down:
|
||||
if (historyIndex+1 < (int)commandHistory.size())
|
||||
historyIndex++;
|
||||
|
||||
if (commandHistory.size() > 0)
|
||||
this->setText(QString::fromStdString(commandHistory[historyIndex]));
|
||||
return;
|
||||
}
|
||||
|
||||
return QLineEdit::keyPressEvent(evt);
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <qlineedit.h>
|
||||
#include <vector>
|
||||
|
||||
struct lua_State;
|
||||
|
||||
class CommandEdit : public QLineEdit {
|
||||
Q_OBJECT
|
||||
|
||||
std::vector<std::string> commandHistory;
|
||||
int historyIndex = 0;
|
||||
|
||||
void executeCommand();
|
||||
void getOrCreateEnvironment(lua_State* L);
|
||||
public:
|
||||
CommandEdit(QWidget* parent = nullptr);
|
||||
~CommandEdit();
|
||||
|
||||
void keyPressEvent(QKeyEvent *) override;
|
||||
};
|
|
@ -179,13 +179,6 @@ ScriptDocument::ScriptDocument(std::shared_ptr<Script> script, QWidget* parent):
|
|||
ScriptDocument::~ScriptDocument() {
|
||||
}
|
||||
|
||||
void ScriptDocument::moveCursor(int line) {
|
||||
if (line == -1) return;
|
||||
|
||||
int lineLength = scintilla->lineLength(line-1);
|
||||
scintilla->setCursorPosition(line-1, lineLength-1);
|
||||
}
|
||||
|
||||
QsciAPIs* makeApis(QsciLexer* lexer) {
|
||||
QsciAPIs* apis = new QsciAPIs(lexer);
|
||||
|
||||
|
|
|
@ -17,5 +17,4 @@ public:
|
|||
~ScriptDocument() override;
|
||||
|
||||
inline std::shared_ptr<Script> getScript() { return script; }
|
||||
void moveCursor(int line);
|
||||
};
|
Loading…
Add table
Reference in a new issue