From 7cb857daf30952a9d83ec25b51ddd584a273e062 Mon Sep 17 00:00:00 2001 From: Razakhel Date: Sat, 1 Jun 2024 16:53:08 +0200 Subject: [PATCH] [Interface/Component/LuaScriptGroup] Added a Lua script group - A Lua script can be given to be applied to its entity - Bumped license year --- LICENSE | 2 +- assets/translations/english.ts | 74 +++++++++++++------ assets/translations/french.ts | 74 +++++++++++++------ .../Interface/Component/LuaScriptGroup.hpp | 15 ++++ interface/LuaScriptComp.ui | 42 +++++++++++ interface/MeshComp.ui | 3 - .../Interface/Component/AppComponents.cpp | 39 ++++++++++ .../Interface/Component/LuaScriptGroup.cpp | 44 +++++++++++ 8 files changed, 247 insertions(+), 46 deletions(-) create mode 100644 include/RaZor/Interface/Component/LuaScriptGroup.hpp create mode 100644 interface/LuaScriptComp.ui create mode 100644 src/RaZor/Interface/Component/LuaScriptGroup.cpp diff --git a/LICENSE b/LICENSE index 3662f52..b3addbc 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Romain Milbert +Copyright (c) 2024 Romain Milbert Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/assets/translations/english.ts b/assets/translations/english.ts index 67f55a7..f95160e 100644 --- a/assets/translations/english.ts +++ b/assets/translations/english.ts @@ -40,104 +40,109 @@ AppWindow - + Failed to find an entity named Failed to find an entity named - + component(s) not displayed. component(s) not displayed. - + Failed to import mesh Failed to import mesh - + + Lua script + Lua script + + + Line Line - + Plane Plane - + Sphere Sphere - + Triangle Triangle - + Quad Quad - + AABB AABB - + OBB OBB - - + + Add component Add component - + Transform Transform - + Mesh Mesh - + Mesh renderer Mesh renderer - + Light Light - + Point light Point light - + Directional light Directional light - + Rigid body Rigid body - + Collider Collider - + Sound Sound @@ -325,6 +330,33 @@ Up + + LuaScriptComp + + + + Lua script + Lua script + + + + Script file + Script file + + + + LuaScriptGroup + + + Could not open the file + Could not open the file + + + + Failed to load the script: + Failed to load the script: + + MainWindow diff --git a/assets/translations/french.ts b/assets/translations/french.ts index b2c50b9..f864c83 100644 --- a/assets/translations/french.ts +++ b/assets/translations/french.ts @@ -40,104 +40,109 @@ AppWindow - + Failed to find an entity named N'a pas pu trouver une entité appelée - + component(s) not displayed. composant(s) non affiché(s). - + Failed to import mesh Échec de l'import du maillage - + + Lua script + Script Lua + + + Line Ligne - + Plane Plan - + Sphere Sphère - + Triangle Triangle - + Quad Carré - + AABB AABB - + OBB OBB - - + + Add component Ajouter composant - + Transform Transformation - + Mesh Maillage - + Mesh renderer Rendu du maillage - + Light Lumière - + Point light Lumière positionnelle - + Directional light Lumière directionnelle - + Rigid body - + Collider - + Sound Son @@ -325,6 +330,33 @@ Haut + + LuaScriptComp + + + + Lua script + Script Lua + + + + Script file + Fichier de script + + + + LuaScriptGroup + + + Could not open the file + Échec de l'ouverture du fichier + + + + Failed to load the script: + Échec du chargement du script : + + MainWindow diff --git a/include/RaZor/Interface/Component/LuaScriptGroup.hpp b/include/RaZor/Interface/Component/LuaScriptGroup.hpp new file mode 100644 index 0000000..47ba81f --- /dev/null +++ b/include/RaZor/Interface/Component/LuaScriptGroup.hpp @@ -0,0 +1,15 @@ +#pragma once + +#ifndef RAZOR_LUASCRIPTGROUP_HPP +#define RAZOR_LUASCRIPTGROUP_HPP + +#include "RaZor/Interface/Component/ComponentGroup.hpp" + +class LuaScriptGroup final : public ComponentGroup { +public: + LuaScriptGroup(Raz::Entity& entity, AppWindow& appWindow); + + void removeComponent() override; +}; + +#endif // RAZOR_LUASCRIPTGROUP_HPP diff --git a/interface/LuaScriptComp.ui b/interface/LuaScriptComp.ui new file mode 100644 index 0000000..7aa9287 --- /dev/null +++ b/interface/LuaScriptComp.ui @@ -0,0 +1,42 @@ + + + LuaScriptComp + + + Lua script + + + Lua script + + + + + + Script file + + + + + + + true + + + + + + + + LuaScriptGroup + QGroupBox +
RaZor/Interface/Component/LuaScriptGroup.hpp
+
+ + FileWidget<FileType::LUA_SCRIPT> + QLineEdit +
RaZor/Interface/FileWidget.hpp
+
+
+ + +
diff --git a/interface/MeshComp.ui b/interface/MeshComp.ui index 4389ee3..fd8ffad 100644 --- a/interface/MeshComp.ui +++ b/interface/MeshComp.ui @@ -46,9 +46,6 @@ - - true - true diff --git a/src/RaZor/Interface/Component/AppComponents.cpp b/src/RaZor/Interface/Component/AppComponents.cpp index e52a7d1..894dda8 100644 --- a/src/RaZor/Interface/Component/AppComponents.cpp +++ b/src/RaZor/Interface/Component/AppComponents.cpp @@ -4,6 +4,7 @@ #include "RaZor/Interface/Component/ColliderGroup.hpp" #include "RaZor/Interface/Component/LightGroup.hpp" #include "RaZor/Interface/Component/ListenerGroup.hpp" +#include "RaZor/Interface/Component/LuaScriptGroup.hpp" #include "RaZor/Interface/Component/MeshGroup.hpp" #include "RaZor/Interface/Component/MeshRendererGroup.hpp" #include "RaZor/Interface/Component/RigidBodyGroup.hpp" @@ -21,8 +22,28 @@ #include #include #include +#include #include +namespace { + +constexpr std::string_view defaultLuaScript = R"( +-- Constants and/or global variables can be defined here + +function setup() + -- Code here will be executed once whenever the script is (re)loaded; this function is entirely optional + -- You can use the special 'this' variable, which represents the entity containing the script + -- For example: this:getTransform().position = Vec3f.new(0, 0, 0) +end + +function update(timeInfo) + -- Code here will be executed every frame; this function is required to exist + -- The 'timeInfo' parameter contains the same fields that are given to each system & world (deltaTime, globalTime, ...) + -- For example: this:getTransform():translate(0, math.sin(timeInfo.globalTime) * timeInfo.deltaTime, 0) +end)"; + +} // namespace + void AppWindow::loadComponents() { loadComponents(m_parentWindow->m_window.entitiesList->currentItem()->text()); } @@ -86,6 +107,11 @@ void AppWindow::loadComponents(const QString& entityName) { --remainingComponentCount; } + if (entity.hasComponent()) { + m_parentWindow->m_window.componentsLayout->addWidget(new LuaScriptGroup(entity, *this)); + --remainingComponentCount; + } + if (remainingComponentCount > 0) m_parentWindow->m_window.componentsLayout->addWidget(new QLabel(QString::number(remainingComponentCount) + tr(" component(s) not displayed."))); @@ -210,6 +236,19 @@ void AppWindow::showAddComponent(Raz::Entity& entity, const QString& entityName, }); } + // Lua script + + QAction* addLuaScript = contextMenu->addAction(tr("Lua script")); + + if (entity.hasComponent()) { + addLuaScript->setEnabled(false); + } else { + connect(addLuaScript, &QAction::triggered, [this, &entity, entityName] () { + entity.addComponent(std::string(defaultLuaScript)); + loadComponents(entityName); + }); + } + connect(addComponent, &QPushButton::clicked, contextMenu, [contextMenu] () { contextMenu->popup(QCursor::pos()); }); m_parentWindow->m_window.componentsLayout->addWidget(addComponent); diff --git a/src/RaZor/Interface/Component/LuaScriptGroup.cpp b/src/RaZor/Interface/Component/LuaScriptGroup.cpp new file mode 100644 index 0000000..8ae46e2 --- /dev/null +++ b/src/RaZor/Interface/Component/LuaScriptGroup.cpp @@ -0,0 +1,44 @@ +#include "RaZor/Interface/AppWindow.hpp" +#include "RaZor/Interface/Component/LuaScriptGroup.hpp" +#include "ui_LuaScriptComp.h" + +#include +#include +#include + +#include + +LuaScriptGroup::LuaScriptGroup(Raz::Entity& entity, AppWindow& appWindow) : ComponentGroup(entity, appWindow) { + Ui::LuaScriptComp luaScriptComp {}; + luaScriptComp.setupUi(this); + + auto& luaScript = entity.getComponent(); + + // Script file + + connect(luaScriptComp.luaScriptFile, &QLineEdit::textChanged, [&entity, &luaScript] (const QString& filePath) { + QFile file(filePath); + + if (!file.open(QFile::ReadOnly | QFile::Text)) { + Raz::Logger::error("[LuaScript] " + tr("Could not open the file").toStdString() + '\'' + filePath.toStdString() + '\''); + return; + } + + const std::string fileContent = QTextStream(&file).readAll().toStdString(); + + try { + // Running a dummy script, checking that it's syntactically correct + Raz::LuaScript testScript(fileContent); + testScript.registerEntity(entity, "this"); + testScript.update({}); + + luaScript.loadCode(fileContent); + } catch (const std::exception& ex) { + Raz::Logger::error("[LuaScript] " + tr("Failed to load the script:").toStdString() + '\n' + ex.what()); + } + }); +} + +void LuaScriptGroup::removeComponent() { + m_entity.removeComponent(); +}