From 8c033fff3d82d5f0e4c5d2eb3c5d10efc60ee851 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Fri, 18 Jun 2021 17:14:19 +0500 Subject: Implemented RmlFileInterface --- cwd/assets/altcraft/scripts/init.lua | 7 +- cwd/assets/altcraft/ui/main-menu-styles.rcss | 105 +++++++++++++++++++++++++++ cwd/assets/altcraft/ui/main-menu.rml | 19 +++++ cwd/test.rcss | 105 --------------------------- cwd/test.rml | 19 ----- src/AssetManager.cpp | 3 + src/AssetManager.hpp | 2 + src/Plugin.cpp | 2 +- src/Render.cpp | 14 ++-- src/Render.hpp | 4 +- src/Rml.cpp | 46 ++++++++++++ src/Rml.hpp | 26 +++++++ 12 files changed, 215 insertions(+), 137 deletions(-) create mode 100644 cwd/assets/altcraft/ui/main-menu-styles.rcss create mode 100644 cwd/assets/altcraft/ui/main-menu.rml delete mode 100644 cwd/test.rcss delete mode 100644 cwd/test.rml diff --git a/cwd/assets/altcraft/scripts/init.lua b/cwd/assets/altcraft/scripts/init.lua index 96b5ec3..b9b34a7 100644 --- a/cwd/assets/altcraft/scripts/init.lua +++ b/cwd/assets/altcraft/scripts/init.lua @@ -9,11 +9,14 @@ local plugin = { } function plugin.onLoad () - print("Loaded AltCraft plugin!") + rmlui:LoadFontFace("altcraft/fonts/OpenSans-Regular") end function plugin.onChangeState (newState) - AC.LogWarning("New state: "..newState) + if newState == "MainMenu" then + local con = rmlui.contexts["default"] + con:LoadDocument("altcraft/ui/main-menu"):Show() + end end function plugin.onUnload () diff --git a/cwd/assets/altcraft/ui/main-menu-styles.rcss b/cwd/assets/altcraft/ui/main-menu-styles.rcss new file mode 100644 index 0000000..ee86894 --- /dev/null +++ b/cwd/assets/altcraft/ui/main-menu-styles.rcss @@ -0,0 +1,105 @@ +body { + background-color: #160f08; + color: white; + font-family: "open sans"; + width: 100%; + height: 100%; + text-align: center; +} + +.menu { + width: 50%; + margin: auto; + background-color: #a79f9c; +} + +.mc-title { + color: #8e8e8e; + display: block; +} + +.mc-p { + color: #d6d4d6; + display: block; + text-align: left; + font-size: 4vh; +} + +.mc-text { + border-width: 2dp; + border-color: #9f9793; + background-color: #010001; + color: #d6d4d6; + text-align: center; + vertical-align: middle; + font-size: 5vh; +} + +.mc-button { + border-width: 2dp; + border-color: #14110c; + background-color: #6e6f70; + color: #c5c6c7; + text-align: center; + vertical-align: middle; + font-size: 5vh; +} + +.mc-button:hover { + background-color: #7e86bc; +} + +#title { + margin: 0% auto auto; + font-size: 20vh; +} + +#disclaimer { + width: 70%; + margin: 0 auto; +} + +#hostname-text { + width: 70%; + margin: 5% auto; +} + +#hostname { + display: inline-block; + width: 45%; + height: 8%; + position: fixed; + margin: 0% auto auto; +} + +#username { + display: inline-block; + width: 45%; + height: 8%; + position: fixed; + margin: 10% auto auto; +} + +#connect { + display: inline-block; + width: 45%; + height: 8%; + position: fixed; + margin: 20% auto auto; +} + +#options { + display: inline-block; + width: 22%; + height: 8%; + position: fixed; + margin: 33% auto auto 27.5%; +} + +#exit { + display: inline-block; + width: 22%; + height: 8%; + position: fixed; + margin: 33% 27.5% auto auto; +} diff --git a/cwd/assets/altcraft/ui/main-menu.rml b/cwd/assets/altcraft/ui/main-menu.rml new file mode 100644 index 0000000..9fd2898 --- /dev/null +++ b/cwd/assets/altcraft/ui/main-menu.rml @@ -0,0 +1,19 @@ + + + + + + AltCraft +

AltCraft is currently not finished, but there is some buggy early testing going on.

+

Enter the hostname of a server and your username to connect to it:

+ + + + + + +
diff --git a/cwd/test.rcss b/cwd/test.rcss deleted file mode 100644 index ee86894..0000000 --- a/cwd/test.rcss +++ /dev/null @@ -1,105 +0,0 @@ -body { - background-color: #160f08; - color: white; - font-family: "open sans"; - width: 100%; - height: 100%; - text-align: center; -} - -.menu { - width: 50%; - margin: auto; - background-color: #a79f9c; -} - -.mc-title { - color: #8e8e8e; - display: block; -} - -.mc-p { - color: #d6d4d6; - display: block; - text-align: left; - font-size: 4vh; -} - -.mc-text { - border-width: 2dp; - border-color: #9f9793; - background-color: #010001; - color: #d6d4d6; - text-align: center; - vertical-align: middle; - font-size: 5vh; -} - -.mc-button { - border-width: 2dp; - border-color: #14110c; - background-color: #6e6f70; - color: #c5c6c7; - text-align: center; - vertical-align: middle; - font-size: 5vh; -} - -.mc-button:hover { - background-color: #7e86bc; -} - -#title { - margin: 0% auto auto; - font-size: 20vh; -} - -#disclaimer { - width: 70%; - margin: 0 auto; -} - -#hostname-text { - width: 70%; - margin: 5% auto; -} - -#hostname { - display: inline-block; - width: 45%; - height: 8%; - position: fixed; - margin: 0% auto auto; -} - -#username { - display: inline-block; - width: 45%; - height: 8%; - position: fixed; - margin: 10% auto auto; -} - -#connect { - display: inline-block; - width: 45%; - height: 8%; - position: fixed; - margin: 20% auto auto; -} - -#options { - display: inline-block; - width: 22%; - height: 8%; - position: fixed; - margin: 33% auto auto 27.5%; -} - -#exit { - display: inline-block; - width: 22%; - height: 8%; - position: fixed; - margin: 33% 27.5% auto auto; -} diff --git a/cwd/test.rml b/cwd/test.rml deleted file mode 100644 index de48053..0000000 --- a/cwd/test.rml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - AltCraft -

AltCraft is currently not finished, but there is some buggy early testing going on.

-

Enter the hostname of a server and your username to connect to it:

- - - - - - -
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 0db495a..8528a1b 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -64,6 +64,9 @@ void AssetManager::InitAssetManager() ParseBlockModels(); PluginSystem::Init(); +} + +void AssetManager::InitPostRml() { LoadScripts(); } diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index bf948b3..b67d920 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -174,6 +174,8 @@ struct AssetScript : Asset { namespace AssetManager { void InitAssetManager(); + void InitPostRml(); + BlockFaces &GetBlockModelByBlockId(BlockId block); std::string GetAssetNameByBlockId(BlockId block); diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 13045e1..776ba8d 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -42,8 +42,8 @@ namespace PluginApi { plugin["onRequestBlockInfo"].get_or(std::function()), }; plugins.push_back(nativePlugin); + LOG(INFO)<<"Loading plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name); nativePlugin.onLoad(); - LOG(INFO) << "Loaded plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name); } diff --git a/src/Render.cpp b/src/Render.cpp index 39322f9..c0885e3 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -63,6 +63,8 @@ Render::Render(unsigned int windowWidth, unsigned int windowHeight, PrepareToRendering(); glCheckError(); InitRml(); + glCheckError(); + AssetManager::InitPostRml(); glCheckError(); //Read settings @@ -545,19 +547,13 @@ void Render::InitRml() { Rml::SetRenderInterface(rmlRender.get()); rmlRender->Update(renderState.WindowWidth, renderState.WindowHeight); + rmlFile = std::make_unique(); + Rml::SetFileInterface(rmlFile.get()); + if (!Rml::Initialise()) LOG(WARNING) << "Rml not initialized"; Rml::Lua::Initialise(PluginSystem::GetLuaState()); rmlContext = Rml::CreateContext("default", Rml::Vector2i(renderState.WindowWidth, renderState.WindowHeight)); - - if (!Rml::LoadFontFace("OpenSans-Regular.ttf")) - LOG(WARNING) << "Rml font not loaded"; - - Rml::ElementDocument* document = rmlContext->LoadDocument("test.rml"); - if (document) - document->Show(); - else - LOG(WARNING) << "Rml document not loaded"; } diff --git a/src/Render.hpp b/src/Render.hpp index 7b2313c..a7b510a 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -15,6 +15,7 @@ class RendererWorld; class Framebuffer; class RmlRenderInterface; class RmlSystemInterface; +class RmlFileInterface; namespace Rml { class Context; @@ -51,8 +52,9 @@ class Render { float fieldResolutionScale; std::unique_ptr rmlRender; std::unique_ptr rmlSystem; + std::unique_ptr rmlFile; Rml::Context* rmlContext; - unsigned short sdlKeyMods; + unsigned short sdlKeyMods = 0; void SetMouseCapture(bool IsCaptured); diff --git a/src/Rml.cpp b/src/Rml.cpp index abec7db..cbc795f 100644 --- a/src/Rml.cpp +++ b/src/Rml.cpp @@ -162,3 +162,49 @@ void RmlRenderInterface::Update(unsigned int windowWidth, unsigned int windowHei AssetManager::GetAsset("/altcraft/shaders/rmltex")->shader->SetUniform("fontTexture", 0); glCheckError(); } + +Rml::FileHandle RmlFileInterface::Open(const Rml::String& path) { + Rml::FileHandle fileId = handles.rbegin() != handles.rend() ? handles.rbegin()->first + 1 : 1; + while (handles.find(fileId) != handles.end()) + fileId++; + + AssetHandle handle; + handle.fileName = path; + std::string assetName = path; + if (*assetName.begin() != '/') + assetName = "/" + assetName; + handle.assetPtr = AssetManager::GetAssetByAssetName(assetName); + handle.filePos = 0; + + if (handle.assetPtr != nullptr) + handles.insert(std::make_pair(fileId, handle)); + else + fileId = 0; + return fileId; +} + +void RmlFileInterface::Close(Rml::FileHandle file) { + handles.erase(file); +} + +size_t RmlFileInterface::Read(void* buffer, size_t size, Rml::FileHandle file) { + size_t readed = 0; + readed = _min(handles[file].assetPtr->data.size() - handles[file].filePos, size); + std::memcpy(buffer, handles[file].assetPtr->data.data() + handles[file].filePos, readed); + handles[file].filePos += readed; + return readed; +} + +bool RmlFileInterface::Seek(Rml::FileHandle file, long offset, int origin) { + unsigned long long base = 0; + if (origin == SEEK_CUR) + base = handles[file].filePos; + else if (origin == SEEK_END) + base = handles[file].assetPtr->data.size(); + handles[file].filePos = base + offset; + return true; +} + +size_t RmlFileInterface::Tell(Rml::FileHandle file) { + return handles[file].filePos; +} diff --git a/src/Rml.hpp b/src/Rml.hpp index 7b312c0..6e4d857 100644 --- a/src/Rml.hpp +++ b/src/Rml.hpp @@ -1,10 +1,15 @@ #pragma once +#include + #include #include +#include #include "Renderer.hpp" +class AssetTreeNode; + class RmlSystemInterface : public Rml::SystemInterface { double totalTime; public: @@ -58,3 +63,24 @@ public: void Update(unsigned int windowWidth, unsigned int windowHeight); }; + +class RmlFileInterface : public Rml::FileInterface { + struct AssetHandle { + std::string fileName; + unsigned long long filePos; + AssetTreeNode* assetPtr; + }; + std::map handles; +public: + + virtual Rml::FileHandle Open(const Rml::String& path) override; + + virtual void Close(Rml::FileHandle file) override; + + virtual size_t Read(void* buffer, size_t size, Rml::FileHandle file) override; + + virtual bool Seek(Rml::FileHandle file, long offset, int origin) override; + + virtual size_t Tell(Rml::FileHandle file) override; + +}; -- cgit v1.2.3