Script editor asset and script component created

Script component can track a script editor asset by it's UUID
Double clicking a script asset opens the script in vs code
master
Joey Pollack 3 years ago
parent fa6bf27183
commit de8bc580df

@ -10,6 +10,7 @@ Core:
☐ Create log rotation system so that log files do not grow to GB in size... @critical
- If file is X MBs, check for file with .old ext and erase if exists, .old to current file and create new log file
☐ Figure out how to represent Unique Entities and entity instances - and how this will work with UUIDs @critical
☐ Design Entity Template system
✔ Implement generic serialization system @done(22-06-29 18:44)
✔ JSON serializable base class @done(22-06-29 17:41)
✔ JSON implementions should be stubbed out in non-editor builds @done(22-06-29 17:41)
@ -85,8 +86,10 @@ GUI:
✔ Selected files should show up in the text box @done(22-04-18 13:33)
Scripting:
☐ Switch to Wren instead of LUA (https://github.com/wren-lang/wren) @high
☐ Remove SOL
✔ Switch to Wren instead of LUA (https://github.com/wren-lang/wren) @high @done(22-11-02 18:56)
✔ Remove SOL @done(22-11-02 18:56)
✔ Script Asset @done(22-11-14 18:19)
Script Managment class:
✘ Manage LUA states @cancelled(22-05-13 17:31)
@ -119,7 +122,7 @@ ECS:
✔ BlockOut @done(22-09-08 15:41)
☐ SpriteRenderer
☐ Animation Controller
☐ Script
✔ Script @done(22-11-14 18:19)
☐ Audio Listener
Physics:
☐ Rigid Body (Box2D)
@ -127,9 +130,9 @@ ECS:
World (Lunariums version of a "Scene"):
☐ Add Render layer property to all renderable components
Implement memento pattern to save the initial state of the world
Implement running the world and resetting to initial state
☐ Implement the world without Regions first
Implement memento pattern to save the initial state of the world @done(22-11-14 18:19)
Implement running the world and resetting to initial state @done(22-11-14 18:19)
✔ Implement the world without Regions first @done(22-11-14 18:20)
✔ Serialize world @done(22-07-06 18:33)
✔ JSON @done(22-07-06 18:33)
☐ Binary

@ -1,6 +1,8 @@
Editor:
☐ Editor Assets need to switch to using UUIDs @critical
☐ Add pure virtual ShowProperties method to EditorAsset
✔ Script Editor Asset @done(22-11-14 18:19)
✔ Editor Assets need to switch to using UUIDs @critical @done(22-11-02 18:51)
✔ Remove Entity @done(22-10-14 18:28)
✔ Remove Component @done(22-10-14 18:28)
✔ Add Entity children @done(22-09-14 15:07)
@ -43,7 +45,7 @@ Editor:
✔ Open existing project @done (2/8/2022, 4:05:42 PM)
Content Manager:
☐ Switch to using LUUIDs for asset ids @critical
✔ Switch to using LUUIDs for asset ids @critical @done(22-11-02 18:52)
✔ Design interface @done (2/24/2022, 3:15:39 PM)
✔ Generate new content file @done (2/24/2022, 3:16:00 PM)
✔ Load existing contents @done (3/3/2022, 3:16:21 PM)

@ -8,11 +8,16 @@
#include "component_guis.h"
#include <editor/editor.h>
#include <editor/contents/content_manager.h>
#include <utils/logger.h>
#include <gui/imgui_ext.h>
#include <dearimgui/imgui.h>
#include <dearimgui/imgui_internal.h>
#include <editor/contents/script.h>
#include <vector>
namespace lunarium { namespace editor
{
/////////////////////////////////////////////////////////////////////
@ -132,6 +137,44 @@ namespace lunarium { namespace editor
ImGui::InputInt("##Render Layer", &comp.RenderLayer, 1, 10);
return remove;
}
bool CompGui::RenderScriptComp(ScriptComponent& comp)
{
bool remove = false;
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(ImGui::GetStyle().ItemSpacing.x, ImGui::GetStyle().ItemSpacing.y + 10.0f));
remove = DrawTitle("Script Component");
ImGui::PopStyleVar();
ImGui::TextUnformatted("Script: ");
ImGui::SameLine();
// TODO: Finish Script GUI
// Drag and Drop script asset
// Click to Open Script (editor event?)
// ComboBox to set script
lunarium::editor::Script* pScript = (lunarium::editor::Script*)ContentManager::GetInstance().GetAsset(comp.ScriptID);
std::string preview = (pScript ? pScript->GetScriptFile().filename().string().c_str() : "NOT SET");
if (ImGui::BeginCombo("Script", preview.c_str()))
{
std::vector<EditorAsset*> assets;
ContentManager::GetInstance().GetAllAssetsByType(assets, AssetType::EATYPE_SCRIPT);
for (auto iter = assets.begin(); iter != assets.end(); iter++)
{
editor::Script* pScript = (editor::Script*)(*iter);
bool selected = false;
if (ImGui::Selectable(pScript->GetScriptFile().string().c_str(), &selected))
{
comp.ScriptID = pScript->GetID();
}
}
ImGui::EndCombo();
}
return remove;
}
}}

@ -21,6 +21,7 @@ namespace lunarium { namespace editor
static bool RenderVelocityComp(VelocityComponent& comp);
static bool RenderCameraComp(CameraComponent& comp);
static bool RenderBlockOutComp(BlockOutComponent& comp);
static bool RenderScriptComp(ScriptComponent& comp);
};
}}

@ -21,6 +21,7 @@
// Asset types
#include "tile_set.h"
#include "world.h"
#include "script.h"
namespace lunarium { namespace editor
{
@ -222,7 +223,7 @@ namespace lunarium { namespace editor
//mNextID = 0;
}
void ContentManager::GetAllAssetIDs(std::vector<uint64_t>& container) const
void ContentManager::GetAllAssetIDs(std::vector<LUUID>& container) const
{
container.clear();
for (auto iter = mAssets.begin(); iter != mAssets.end(); iter++)
@ -256,7 +257,7 @@ namespace lunarium { namespace editor
}
}
EditorAsset* ContentManager::GetAsset(uint64_t id)
EditorAsset* ContentManager::GetAsset(LUUID id)
{
auto iter = mAssets.find(id);
if (iter == mAssets.end())
@ -268,7 +269,7 @@ namespace lunarium { namespace editor
}
OpRes ContentManager::AddGeneratedAsset(EditorAsset* asset, uint64_t& id)
OpRes ContentManager::AddGeneratedAsset(EditorAsset* asset, LUUID& id)
{
// if (mAssets.find(mNextID) != mAssets.end())
// {
@ -287,7 +288,7 @@ namespace lunarium { namespace editor
return OpRes::OK();
}
OpRes ContentManager::ImportFile(std::filesystem::path file, std::filesystem::path to_location, AssetType type, uint64_t& id)
OpRes ContentManager::ImportFile(std::filesystem::path file, std::filesystem::path to_location, AssetType type, LUUID& id)
{
// if (mAssets.find(mNextID) != mAssets.end())
// {
@ -326,7 +327,7 @@ namespace lunarium { namespace editor
return OpRes::OK();
}
void ContentManager::RemoveAsset(uint64_t asset_id)
void ContentManager::RemoveAsset(LUUID asset_id)
{
auto iter = mAssets.find(asset_id);
if (iter == mAssets.end())
@ -377,6 +378,7 @@ namespace lunarium { namespace editor
{
case AssetType::EATYPE_TILE_SET: return new TileSet();
case AssetType::EATYPE_WORLD: return new editor::World();
case AssetType::EATYPE_SCRIPT: return new editor::Script("", ""); // Dummy info will be overwritten
default: return nullptr;
}

@ -11,6 +11,7 @@
#ifndef CONTENT_MANAGER_H_
#define CONTENT_MANAGER_H_
#include <core/common_defs.h>
#include "definitions.h"
#include <filesystem>
#include <string>
@ -39,20 +40,20 @@ namespace lunarium { namespace editor
[[nodiscard]] OpRes Save();
void Unload();
void GetAllAssetIDs(std::vector<uint64_t>& container) const;
void GetAllAssetIDs(std::vector<LUUID>& container) const;
void GetAllAssetsByType(std::vector<EditorAsset*>& container, AssetType type) const;
void GetAllAssetsInDirectory(std::vector<EditorAsset*>& container, std::filesystem::path dir); // dir should be relative to the asset dir
EditorAsset* GetAsset(uint64_t id);
EditorAsset* GetAsset(LUUID id);
/// Add an asset that was generated by the editor (like tile maps or scripts)
[[nodiscard]] OpRes AddGeneratedAsset(EditorAsset* asset, uint64_t& id);
[[nodiscard]] OpRes AddGeneratedAsset(EditorAsset* asset, LUUID& id);
/// Import a raw asset file from outside of the project (like image or sound files)
/// file: the full path to the raw file
/// to_location: the location inside the project's asset directory to import to (this MUST be a relative path starting in the Asset dir)
[[nodiscard]] OpRes ImportFile(std::filesystem::path file, std::filesystem::path to_location, AssetType type, uint64_t& id);
void RemoveAsset(uint64_t asset_id);
[[nodiscard]] OpRes ImportFile(std::filesystem::path file, std::filesystem::path to_location, AssetType type, LUUID& id);
void RemoveAsset(LUUID asset_id);
void MoveAsset(EditorAsset* asset, std::filesystem::path to);

@ -32,6 +32,7 @@ namespace lunarium { namespace editor
EATYPE_TILE_SET,
EATYPE_TILE_MAP,
EATYPE_WORLD,
EATYPE_SCRIPT,
};
}}

@ -8,7 +8,66 @@
#include "script.h"
#include <editor/editor.h>
#include <editor/editor_helpers.h>
#include <utils/logger.h>
#include <fstream>
namespace lunarium { namespace editor
{
Script::Script(std::filesystem::path file_location, std::filesystem::path asset_dir)
: EditorAsset(AssetType::EATYPE_SCRIPT)
{
std::ofstream ofs(asset_dir / file_location);
if (!ofs.is_open())
{
Logger::Warn(Editor::LogCat, "Could not create file %s", (asset_dir / file_location).string().c_str());
}
ofs.close();
ofs.clear();
mLocation = file_location;
}
std::filesystem::path Script::GetScriptFile()
{
return GetFileLocation();
}
std::string Script::GetScript()
{
return FileLoaders::LoadTextFile(GetFileLocation());
}
OpRes Script::LoadRawFile()
{
return OpRes::OK();
}
OpRes Script::Serialize(nlohmann::ordered_json& node)
{
return OpRes::OK();
}
OpRes Script::Deserialize(nlohmann::ordered_json& node)
{
return OpRes::OK();
}
bool Script::IsValidNode(nlohmann::ordered_json& node)
{
return true;
}
nlohmann::ordered_json Script::AsJSON()
{
return nlohmann::ordered_json();
}
bool Script::DrawProperties()
{
return false;
}
}}

@ -10,11 +10,25 @@
#include "editor_asset.h"
#include <filesystem>
namespace lunarium { namespace editor
{
class Script : public EditorAsset
{
public:
Script(std::filesystem::path file_location, std::filesystem::path asset_dir);
OpRes LoadRawFile();
OpRes Serialize(nlohmann::ordered_json& node);
OpRes Deserialize(nlohmann::ordered_json& node);
bool IsValidNode(nlohmann::ordered_json& node);
nlohmann::ordered_json AsJSON();
std::filesystem::path GetScriptFile();
std::string GetScript();
bool DrawProperties();
};
}}

@ -7,7 +7,6 @@
******************************************************************************/
#include "editor.h"
#include "editor/contents/editor_asset.h"
#include <utils/helpers.h>
#include <gui/panel_manager.h>
@ -26,6 +25,12 @@
#include "panels/asset_browser.h"
#include "panels/editor_console.h"
// Assets
#include "editor/contents/editor_asset.h"
#include "contents/definitions.h"
#include "contents/script.h"
#include "contents/world.h"
// Tools
#include "tools/map_editor/map_editor.h"
@ -467,12 +472,41 @@ namespace editor
((PropertiesView*)mPanelManager.GetPanel(mPanels.PropertiesView))->SetSelection(pEnt);
}
void Editor::ChangeWorld(lunarium::World* pWorld)
// void Editor::ChangeWorld(lunarium::World* pWorld)
// {
// // TODO: Unload current world, Load new world
// ((WorldTree*)mPanelManager.GetPanel(mPanels.WorldTree))->SetWorld(pWorld);
// ((WorldView*)mPanelManager.GetPanel(mPanels.WorldView))->SetWorld(pWorld);
// }
void Editor::OnAssetOpen(EditorAsset* pAsset)
{
switch (pAsset->GetType())
{
case AssetType::EATYPE_WORLD:
{
// TODO: Unload current world, Load new world
lunarium::World* pWorld = ((editor::World*)pAsset)->GetWorld();
((WorldTree*)mPanelManager.GetPanel(mPanels.WorldTree))->SetWorld(pWorld);
((WorldView*)mPanelManager.GetPanel(mPanels.WorldView))->SetWorld(pWorld);
}
break;
case AssetType::EATYPE_SCRIPT:
{
// TODO: Hardcoding to open vs code - this should become an editor setting
std::filesystem::path location = mProject.GetAssetDirectory() / pAsset->GetFileLocation();
// the open -n can be used to force code to open the file in a new window
std::string command = "code \"";
command.append(location.string());
command.append("\"");
Logger::Debug(LogCat, "Opening code with command: %s", command.c_str());
std::system(command.c_str());
}
}
}
void Editor::OnAssetSelected(EditorAsset* pAsset)
{

@ -59,7 +59,8 @@ namespace lunarium { namespace editor
// Panel events
void OnEntitySelect(lunarium::Entity* pEnt);
void ChangeWorld(lunarium::World* pWorld);
//void ChangeWorld(lunarium::World* pWorld);
void OnAssetOpen(EditorAsset* pAsset);
void OnAssetSelected(EditorAsset* pAsset);
void OnNewAsset(EditorAsset* pAsset);
void OnAssetUpdate(EditorAsset* pAsset);

@ -9,8 +9,10 @@
#include "editor_helpers.h"
#include <core/core.h>
#include <utils/logger.h>
#include <renderer/texture.h>
#include <utils/stb/stb_image.h>
#include <fstream>
namespace lunarium { namespace editor
{
@ -36,4 +38,29 @@ namespace lunarium { namespace editor
return t;
}
std::string FileLoaders::LoadTextFile(std::filesystem::path file)
{
std::string contents = "";
std::ifstream ifs(file.string().c_str());
if (!ifs.is_open())
{
Logger::Warn(LogCategory::UTILITIES, "Failed to open file: %s", file.string().c_str());
return "";
}
while (true)
{
std::getline(ifs, contents);
if (ifs.eof()) break;
contents += "\n";
}
ifs.close();
ifs.clear();
return contents;
}
}}

@ -22,6 +22,7 @@ namespace lunarium { namespace editor
{
public:
static lunarium::Texture* LoadImage(std::filesystem::path file);
static std::string LoadTextFile(std::filesystem::path file);
};
}}

@ -17,6 +17,7 @@
#include <editor/contents/content_manager.h>
#include <editor/contents/editor_asset.h>
#include <editor/contents/world.h>
#include <editor/contents/script.h>
#include <renderer/texture.h>
namespace lunarium
@ -222,7 +223,9 @@ namespace editor
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
{
// Logger::Info(Editor::LogCat, "Asset double clicked on. Relevant editor should open!");
HandleAssetDoubleClick((*iter));
//HandleAssetDoubleClick((*iter));
mpEditor->OnAssetOpen((*iter));
}
if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Right))
@ -258,6 +261,11 @@ namespace editor
{
OpenPopup(PopUp::NEW_WORLD).LogIfFailed(Editor::LogCat);
}
if (ImGui::MenuItem("New Script"))
{
OpenPopup(PopUp::NEW_SCRIPT).LogIfFailed(Editor::LogCat);
}
ImGui::EndMenu();
}
@ -323,7 +331,7 @@ namespace editor
{
auto assets_dir = ab->mpEditor->GetProject()->MakeRelativeToAssets(ab->mSelectedDir);
EditorAsset* pAsset = new editor::World(assets_dir / name_buf);
u64 id;
LUUID id;
ContentManager::GetInstance().AddGeneratedAsset(pAsset, id).LogIfFailed(Editor::LogCat);
stay_open = false;
@ -337,18 +345,48 @@ namespace editor
}
return stay_open;
}).LogIfFailed(Editor::LogCat);
}
// New script asset
AddPopup(PopUp::NEW_SCRIPT, "New Script Name", [](Panel *p)
{
bool stay_open = true;
AssetBrowser* ab = (AssetBrowser*)p;
char name_buf[64] = "New Script";
ImGui::TextUnformatted("Script Name: ");
ImGui::SameLine();
if (ImGui::InputText("##Script Name", name_buf, 64, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue))
{
auto assets_dir = ab->mpEditor->GetProject()->MakeRelativeToAssets(ab->mSelectedDir);
std::string sname_buf = name_buf;
sname_buf.append(".wren");
std::filesystem::path location = assets_dir / sname_buf;
EditorAsset* pAsset = new editor::Script(location, ab->mpEditor->GetProject()->GetAssetDirectory());
LUUID id;
ContentManager::GetInstance().AddGeneratedAsset(pAsset, id).LogIfFailed(Editor::LogCat);
stay_open = false;
ImGui::CloseCurrentPopup();
}
void AssetBrowser::HandleAssetDoubleClick(EditorAsset* pAsset)
{
if (pAsset->GetType() == AssetType::EATYPE_WORLD)
if (Core::Input().IsKeyDown(KeyCode::ESCAPE, true))
{
World* pAssetWorld = (World*)pAsset;
mpEditor->ChangeWorld(pAssetWorld->GetWorld());
stay_open = false;
ImGui::CloseCurrentPopup();
}
return stay_open;
}).LogIfFailed(Editor::LogCat);
}
// void AssetBrowser::HandleAssetDoubleClick(EditorAsset* pAsset)
// {
// if (pAsset->GetType() == AssetType::EATYPE_WORLD)
// {
// World* pAssetWorld = (World*)pAsset;
// mpEditor->ChangeWorld(pAssetWorld->GetWorld());
// }
// }
}
}

@ -49,7 +49,7 @@ namespace editor
// General Helpers
void HandleAssetDrop(std::filesystem::path dir);
void HandleAssetDoubleClick(EditorAsset* pAsset);
//void HandleAssetDoubleClick(EditorAsset* pAsset);
private: // POPUP ENUMS
@ -57,6 +57,7 @@ namespace editor
{
NEW_FOLDER,
NEW_WORLD,
NEW_SCRIPT,
};
};

@ -68,6 +68,7 @@ namespace lunarium { namespace editor
PRESENT_COMP_CHOICE("Velocity Component", VelocityComponent, pv)
PRESENT_COMP_CHOICE("Camera Component", CameraComponent, pv)
PRESENT_COMP_CHOICE("Block Out Component", BlockOutComponent, pv)
PRESENT_COMP_CHOICE("Script Component", ScriptComponent, pv)
if ((ImGui::IsMouseClicked(ImGuiMouseButton_Left) && is_hover < 1))
@ -187,6 +188,7 @@ namespace lunarium { namespace editor
DRAW_COMP_GUI(VelocityComponent, RenderVelocityComp)
DRAW_COMP_GUI(CameraComponent, RenderCameraComp)
DRAW_COMP_GUI(BlockOutComponent, RenderBlockOutComp)
DRAW_COMP_GUI(ScriptComponent, RenderScriptComp)
// After all components rendered
if (ImGuiExt::ButtonCentered("Add Component"))

@ -84,6 +84,14 @@ namespace lunarium
BlockOutComponent(const BlockOutComponent&) = default;
};
struct ScriptComponent
{
LUUID ScriptID;
ScriptComponent() = default;
ScriptComponent(const ScriptComponent&) = default;
};
/////////////////////////////////////////////////////////////////////
// UTILITY COMPONENTS
/////////////////////////////////////////////////////////////////////

@ -268,6 +268,17 @@ namespace lunarium
components.emplace_back(blockout);
}
if (HasComponent<ScriptComponent>())
{
nlohmann::ordered_json script;
ScriptComponent& comp = GetComponent<ScriptComponent>();
script["type_name"] = "ScriptComponent";
script["script_id"] = comp.ScriptID;
components.emplace_back(script);
}
if (HasComponent<ParentEntityComponent>())
{
nlohmann::ordered_json parent;
@ -411,6 +422,13 @@ namespace lunarium
AddComponent<BlockOutComponent>(Color, Size, layer);
}
if ("ScriptComponent" == comp_type_name)
{
LUUID id = comp["script_id"];
AddComponent<ScriptComponent>(id);
}
if ("ParentEntityComponent" == comp_type_name)
{
LUUID parent = comp["UUID"].get<u64>();

Loading…
Cancel
Save