diff options
author | LaG1924 <lag1924@gmail.com> | 2021-06-13 18:20:25 +0200 |
---|---|---|
committer | LaG1924 <lag1924@gmail.com> | 2021-06-13 18:20:25 +0200 |
commit | cc937302964a04aa8e7cdc0e6796503559efaa69 (patch) | |
tree | 3a6b890d32ed735d42f042a08c1af9637cb62b70 | |
parent | Fixed Linux build of RmlUi (diff) | |
download | AltCraft-cc937302964a04aa8e7cdc0e6796503559efaa69.tar AltCraft-cc937302964a04aa8e7cdc0e6796503559efaa69.tar.gz AltCraft-cc937302964a04aa8e7cdc0e6796503559efaa69.tar.bz2 AltCraft-cc937302964a04aa8e7cdc0e6796503559efaa69.tar.lz AltCraft-cc937302964a04aa8e7cdc0e6796503559efaa69.tar.xz AltCraft-cc937302964a04aa8e7cdc0e6796503559efaa69.tar.zst AltCraft-cc937302964a04aa8e7cdc0e6796503559efaa69.zip |
-rw-r--r-- | cwd/assets/altcraft/shaders/rml.json | 1 | ||||
-rw-r--r-- | cwd/assets/altcraft/shaders/rmltex.json | 1 | ||||
-rw-r--r-- | cwd/assets/altcraft/shaders/vert/rml.vs | 7 | ||||
-rw-r--r-- | cwd/test.rcss | 24 | ||||
-rw-r--r-- | cwd/test.rml | 20 | ||||
-rw-r--r-- | src/Plugin.cpp | 4 | ||||
-rw-r--r-- | src/Plugin.hpp | 3 | ||||
-rw-r--r-- | src/Render.cpp | 46 | ||||
-rw-r--r-- | src/Rml.cpp | 6 |
9 files changed, 104 insertions, 8 deletions
diff --git a/cwd/assets/altcraft/shaders/rml.json b/cwd/assets/altcraft/shaders/rml.json index c62ff25..0cd85cc 100644 --- a/cwd/assets/altcraft/shaders/rml.json +++ b/cwd/assets/altcraft/shaders/rml.json @@ -2,6 +2,7 @@ "vert": "/altcraft/shaders/vert/rml", "frag": "/altcraft/shaders/frag/rml", "uniforms": [ + "translation", "viewportSize" ] }
\ No newline at end of file diff --git a/cwd/assets/altcraft/shaders/rmltex.json b/cwd/assets/altcraft/shaders/rmltex.json index 3fef969..a8a1323 100644 --- a/cwd/assets/altcraft/shaders/rmltex.json +++ b/cwd/assets/altcraft/shaders/rmltex.json @@ -2,6 +2,7 @@ "vert": "/altcraft/shaders/vert/rml", "frag": "/altcraft/shaders/frag/rmltex", "uniforms": [ + "translation", "viewportSize", "fontTexture" ] diff --git a/cwd/assets/altcraft/shaders/vert/rml.vs b/cwd/assets/altcraft/shaders/vert/rml.vs index bfc50fb..bdd3b71 100644 --- a/cwd/assets/altcraft/shaders/vert/rml.vs +++ b/cwd/assets/altcraft/shaders/vert/rml.vs @@ -1,6 +1,7 @@ #version 330 core uniform uvec2 viewportSize; +uniform vec2 translation; uniform mat4 rotationMat; layout (location = 0) in vec2 pos; @@ -13,9 +14,9 @@ out VS_OUT { } vs_out; void main() { - float x = (pos.x - viewportSize.x) / viewportSize.x; - float y = ((pos.y - viewportSize.y) / viewportSize.y) * -1; - gl_Position = vec4(x, y, -1.0f, 1.0f); + float x = ((pos.x + translation.x) / viewportSize.x) * 2.0f - 1.0f; + float y = ((pos.y + translation.y) / viewportSize.y) * 2.0f - 1.0f; + gl_Position = vec4(x, -y, -1.0f, 1.0f); vs_out.color = vec4(float(color.x) / 255.0f, float(color.y) / 255.0f, float(color.z) / 255.0f, float(color.w) / 255.0f); vs_out.tex_coord = tex_coord; } diff --git a/cwd/test.rcss b/cwd/test.rcss index 47441bb..f1e8cee 100644 --- a/cwd/test.rcss +++ b/cwd/test.rcss @@ -1,6 +1,28 @@ body { background-color: red; color: blue; - font-size: 100px; + font-size: 50px; font-family: "open sans"; + width: 100%; +} + +body:hover { + background-color: yellow; +} + +div { + background-color: green; +} + +div:hover { + background-color: white; +} + +button:active { + background-color: teal; +} + +.right-pos { + position: absolute; + right: 0px; } diff --git a/cwd/test.rml b/cwd/test.rml index ee72ece..4c7bfa6 100644 --- a/cwd/test.rml +++ b/cwd/test.rml @@ -1,10 +1,26 @@ <rml> <head> <link type="text/rcss" href="test.rcss" /> + <script> + redLvl = 0 + function Test(elem) + elem.style["background-color"] = "rgb(" .. redLvl .. ",255,255)" + print("New background-color: "..elem.style["background-color"]) + redLvl = redLvl + 50 + end + </script> </head> <body> - <div style="background-color:green;"> - Test RmlUi + Test RmlUi + <br /> + New line + <div> + Substring + </div> + <br /> + <button onclick="print('Button clicked!')" style="background-color:navy;">Clickable!</button> + <div class="right-pos" onclick="Test(element)"> + (Clickable) Right viewport border is here -> </div> </body> </rmL> diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 83e9bdd..d7a9820 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -214,6 +214,10 @@ void PluginSystem::Init() { apiTable["RegisterDimension"] = PluginApi::RegisterDimension; } +lua_State* PluginSystem::GetLuaState() { + return lua.lua_state(); +} + void PluginSystem::Execute(const std::string &luaCode, bool except) { OPTICK_EVENT(); try { diff --git a/src/Plugin.hpp b/src/Plugin.hpp index a849f5c..7af27a4 100644 --- a/src/Plugin.hpp +++ b/src/Plugin.hpp @@ -5,10 +5,13 @@ #include "Vector.hpp" class BlockInfo; +struct lua_State; namespace PluginSystem { void Init(); + lua_State* GetLuaState(); + void Execute(const std::string &luaCode, bool except = false); void CallOnChangeState(std::string newState); diff --git a/src/Render.cpp b/src/Render.cpp index 0f04992..48bbadd 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -3,6 +3,7 @@ #include <easylogging++.h> #include <optick.h> #include <RmlUi/Core.h> +#include <RmlUi/Lua.h> #include "Shader.hpp" #include "AssetManager.hpp" @@ -276,6 +277,13 @@ void Render::HandleEvents() { case SDL_SCANCODE_SLASH: case SDL_SCANCODE_T: { + auto state = GetState(); + if (state == State::Playing) { + SetState(State::Chat); + } + else if (state == State::Chat) { + SetState(State::Playing); + } break; } @@ -287,14 +295,50 @@ void Render::HandleEvents() { } case SDL_MOUSEMOTION: { + if (isMouseCaptured) { + double deltaX = event.motion.xrel; + double deltaY = event.motion.yrel; + deltaX *= sensetivity; + deltaY *= sensetivity * -1; + PUSH_EVENT("MouseMove", std::make_tuple(deltaX, deltaY)); + } else { + int mouseX, mouseY; + SDL_GetMouseState(&mouseX, &mouseY); + rmlContext->ProcessMouseMove(mouseX, mouseY, 0); + } break; } case SDL_MOUSEBUTTONDOWN: { + if (isMouseCaptured) { + if (event.button.button == SDL_BUTTON_LEFT) + PUSH_EVENT("LmbPressed", 0); + else if (event.button.button == SDL_BUTTON_RIGHT) + PUSH_EVENT("RmbPressed", 0); + } else { + if (event.button.button == SDL_BUTTON_MIDDLE) + event.button.button = SDL_BUTTON_RIGHT; + else if (event.button.button == SDL_BUTTON_RIGHT) + event.button.button = SDL_BUTTON_MIDDLE; + rmlContext->ProcessMouseButtonDown(event.button.button - 1, 0); + } + break; } case SDL_MOUSEBUTTONUP: { + if (isMouseCaptured) { + if (event.button.button == SDL_BUTTON_LEFT) + PUSH_EVENT("LmbReleased", 0); + else if (event.button.button == SDL_BUTTON_RIGHT) + PUSH_EVENT("RmbReleased", 0); + } else { + if (event.button.button == SDL_BUTTON_MIDDLE) + event.button.button = SDL_BUTTON_RIGHT; + else if (event.button.button == SDL_BUTTON_RIGHT) + event.button.button = SDL_BUTTON_MIDDLE; + rmlContext->ProcessMouseButtonUp(event.button.button - 1, 0); + } break; } @@ -437,6 +481,8 @@ void Render::InitRml() { 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")) diff --git a/src/Rml.cpp b/src/Rml.cpp index 7987121..bcdca0c 100644 --- a/src/Rml.cpp +++ b/src/Rml.cpp @@ -84,10 +84,12 @@ RmlRenderInterface::~RmlRenderInterface() { void RmlRenderInterface::RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture, const Rml::Vector2f& translation) { if (texture) { AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rmltex")->shader->Activate(); + AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rmltex")->shader->SetUniform("translation", glm::vec2(translation.x, translation.y)); glBindTexture(GL_TEXTURE_2D, texture); - } - else + } else { AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rml")->shader->Activate(); + AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rml")->shader->SetUniform("translation", glm::vec2(translation.x, translation.y)); + } glCheckError(); glBindVertexArray(Vao); |