Compare commits

...

5 commits

20 changed files with 181 additions and 54 deletions

BIN
assets/icons/message.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 413 B

View file

@ -3,10 +3,10 @@
// I/O
out vec4 fColor;
uniform vec3 aColor;
uniform vec4 aColor;
// Main
void main() {
fColor = vec4(aColor, 1.0);
fColor = aColor;
}

View file

@ -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) {

View file

@ -0,0 +1,4 @@
#include "hint.h"
Hint::Hint(): Message(&TYPE) {}
Hint::~Hint() = default;

18
core/src/objects/hint.h Normal file
View file

@ -0,0 +1,18 @@
#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>(); };
};

View file

@ -0,0 +1,5 @@
#include "message.h"
Message::Message(const InstanceType* type) : Instance(type) {}
Message::Message(): Instance(&TYPE) {}
Message::~Message() = default;

View file

@ -0,0 +1,21 @@
#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>(); };
};

View file

@ -1,9 +1,11 @@
#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"
@ -27,6 +29,8 @@ 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

View file

@ -1,5 +1,6 @@
#include "script.h"
#include "common.h"
#include "datatypes/variant.h"
#include "lauxlib.h"
#include "logger.h"
#include "objects/base/instance.h"
@ -31,23 +32,6 @@ void Script::Run() {
this->thread = lua_newthread(L);
lua_State* Lt = thread;
lua_pushthread(Lt); // Push thread for later*
// 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
InstanceRef(shared_from_this()).PushLuaValue(Lt);
lua_setfield(Lt, -2, "script");
lua_setfenv(Lt, -2); // *Set env of current thread
lua_pop(Lt, 1); // Pop thread
// Push wrapper as thread function
lua_getfield(Lt, LUA_REGISTRYINDEX, "LuaPCallWrapper");
@ -61,6 +45,21 @@ 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);
@ -76,6 +75,34 @@ void Script::Stop() {
// TODO:
}
static std::shared_ptr<Script> getfsource(lua_State* L, lua_Debug* dbg) {
int top = lua_gettop(L);
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;
}
// Get source from metatable
lua_getmetatable(L, -1);
lua_getfield(L, -1, "source");
auto result = InstanceRef::FromLuaValue(L, -1);
if (!result) {
lua_settop(L, top);
return nullptr;
}
lua_settop(L, top);
std::shared_ptr<Instance> ref = result.expect().get<InstanceRef>();
if (!ref->IsA<Script>()) return nullptr;
return ref->CastTo<Script>().expect();
}
int script_errhandler(lua_State* L) {
std::string errorMessage = lua_tostring(L, -1);
Logger::error(errorMessage);
@ -92,7 +119,10 @@ int script_errhandler(lua_State* L) {
if (strcmp(dbg.what, "C") == 0 || strcmp(dbg.source, "=PCALL_WRAPPER") == 0)
continue;
Logger::scriptLogf("'%s', Line %d", Logger::LogLevel::TRACE, {}, dbg.source, dbg.currentline);
// 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("Stack end");

View file

@ -11,6 +11,8 @@ 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);
@ -41,22 +43,6 @@ 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;

View file

@ -63,7 +63,8 @@ std::shared_ptr<Font> loadFont(std::string fontName) {
std::shared_ptr<Font> font = std::make_shared<Font>();
FT_Set_Pixel_Sizes(face, 0, 48);
FT_Set_Pixel_Sizes(face, 0, 16);
font->height = face->size->metrics.y_ppem;
// Load each glyph
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
@ -131,7 +132,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 = y - (ch.size.y - ch.bearing.y) * scale;
float ypos = viewportHeight - y - font->height - (ch.size.y - ch.bearing.y) * scale;
float w = ch.size.x * scale;
float h = ch.size.y * scale;
@ -159,4 +160,17 @@ 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;
}

View file

@ -14,10 +14,12 @@ 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));
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);

View file

@ -20,6 +20,8 @@
#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"
@ -62,7 +64,8 @@ bool wireframeRendering = false;
int viewportWidth, viewportHeight;
void renderDebugInfo();
void drawRect(int x, int y, int width, int height, glm::vec3 color);
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 renderInit(int width, int height) {
viewportWidth = width, viewportHeight = height;
@ -649,6 +652,33 @@ 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();
@ -668,13 +698,14 @@ 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::vec3 color) {
void drawRect(int x, int y, int width, int height, glm::vec4 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

View file

@ -610,19 +610,23 @@ ScriptDocument* MainWindow::findScriptWindow(std::shared_ptr<Script> script) {
return nullptr;
}
void MainWindow::openScriptDocument(std::shared_ptr<Script> script) {
void MainWindow::openScriptDocument(std::shared_ptr<Script> script, int line) {
// 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) {

View file

@ -58,7 +58,7 @@ public:
GridSnappingMode snappingMode;
bool editSoundEffects = true;
void openScriptDocument(std::shared_ptr<Script>);
void openScriptDocument(std::shared_ptr<Script>, int line = -1);
void closeScriptDocument(std::shared_ptr<Script>);
void openFile(std::string path);

View file

@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
<string>Openblocks Editor</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">

View file

@ -1,7 +1,6 @@
#include "outputtextview.h"
#include "logger.h"
#include "mainwindow.h"
#include "objects/script.h"
#include "panes/outputtextview.h"
#include <QEvent>
#include <QTextEdit>
@ -60,7 +59,7 @@ void OutputTextView::handleLog(Logger::LogLevel logLevel, std::string message, L
stackTraceScripts[id] = source.script;
format.setAnchor(true);
format.setAnchorHref(QString::number(id));
format.setAnchorHref(QString::number(id) + ":" + QString::number(source.line));
}
cursor.insertText(message.c_str(), format);
@ -72,11 +71,13 @@ void OutputTextView::mousePressEvent(QMouseEvent *e) {
QString anchor = anchorAt(e->pos());
if (anchor == "" || e->modifiers() & Qt::AltModifier) return QTextEdit::mousePressEvent(e);
auto script = stackTraceScripts[anchor.toInt()];
int idx = anchor.indexOf(":");
int id = anchor.mid(0, idx).toInt(), line = anchor.mid(idx+1).toInt();
auto script = stackTraceScripts[id];
if (script.expired()) return QTextEdit::mousePressEvent(e);
MainWindow* mainWnd = dynamic_cast<MainWindow*>(window());
mainWnd->openScriptDocument(script.lock());
mainWnd->openScriptDocument(script.lock(), line);
}
void OutputTextView::mouseReleaseEvent(QMouseEvent *e) {

View file

@ -32,11 +32,6 @@ void CommandEdit::executeCommand() {
int top = lua_gettop(L);
lua_State* Lt = lua_newthread(L);
lua_pushthread(Lt); // Push thread
getOrCreateEnvironment(Lt);
lua_setfenv(Lt, -2); // Set env of current thread
lua_pop(Lt, 1); // Pop thread
// Push wrapper as thread function
lua_getfield(Lt, LUA_REGISTRYINDEX, "LuaPCallWrapper");
@ -50,10 +45,14 @@ void CommandEdit::executeCommand() {
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);

View file

@ -179,6 +179,13 @@ 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);

View file

@ -17,4 +17,5 @@ public:
~ScriptDocument() override;
inline std::shared_ptr<Script> getScript() { return script; }
void moveCursor(int line);
};