From f2266b843959413277767a990bb9a9c5593f3489 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 13 Jun 2021 13:47:33 +0500 Subject: Implemented basic RmlUi integration --- src/Render.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src/Render.cpp') diff --git a/src/Render.cpp b/src/Render.cpp index d583205..7835b98 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "imgui_impl_sdl_gl3.h" #include "Shader.hpp" @@ -16,6 +17,7 @@ #include "Settings.hpp" #include "Framebuffer.hpp" #include "Plugin.hpp" +#include "Rml.hpp" Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) { @@ -30,6 +32,8 @@ Render::Render(unsigned int windowWidth, unsigned int windowHeight, AssetManager::InitAssetManager(); glCheckError(); PrepareToRendering(); + glCheckError(); + InitRml(); glCheckError(); //Read settings @@ -73,6 +77,10 @@ Render::~Render() { Settings::WriteDouble("resolutionScale", fieldResolutionScale); Settings::Save(); + Rml::RemoveContext("default"); + rmlRender.reset(); + rmlSystem.reset(); + PluginSystem::Init(); framebuffer.reset(); @@ -125,6 +133,7 @@ void Render::InitGlew() { glViewport(0, 0, width, height); glClearColor(0.8,0.8,0.8, 1.0f); glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); @@ -220,6 +229,8 @@ void Render::HandleEvents() { SDL_GL_GetDrawableSize(window, &width, &height); renderState.WindowWidth = width; renderState.WindowHeight = height; + rmlRender->Update(width, height); + rmlContext->SetDimensions(Rml::Vector2i(width, height)); framebuffer->Resize(width * fieldResolutionScale, height * fieldResolutionScale); Framebuffer::GetDefault().Resize(width, height); break; @@ -331,6 +342,7 @@ void Render::HandleEvents() { break; } } + rmlContext->Update(); } void Render::HandleMouseCapture() { @@ -366,6 +378,9 @@ void Render::Update() { void Render::RenderGui() { OPTICK_EVENT(); + + rmlContext->Render(); + ImGui_ImplSdlGL3_NewFrame(window); if (isMouseCaptured) { @@ -750,3 +765,28 @@ void Render::InitEvents() { } }); } + +void Render::InitRml() { + LOG(INFO) << "Initializing Rml"; + + rmlSystem = std::make_unique(); + Rml::SetSystemInterface(rmlSystem.get()); + + rmlRender = std::make_unique(renderState); + Rml::SetRenderInterface(rmlRender.get()); + rmlRender->Update(renderState.WindowWidth, renderState.WindowHeight); + + if (!Rml::Initialise()) + LOG(WARNING) << "Rml not initialized"; + + 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"; +} -- cgit v1.2.3 From e5da79b7bb1f0db50085df8eef905502d98617aa Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 13 Jun 2021 13:53:13 +0500 Subject: Removed ImGui --- src/Render.cpp | 342 --------------------------------------------------------- 1 file changed, 342 deletions(-) (limited to 'src/Render.cpp') diff --git a/src/Render.cpp b/src/Render.cpp index 7835b98..0f04992 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -1,11 +1,9 @@ #include "Render.hpp" -#include #include #include #include -#include "imgui_impl_sdl_gl3.h" #include "Shader.hpp" #include "AssetManager.hpp" #include "Event.hpp" @@ -84,7 +82,6 @@ Render::~Render() { PluginSystem::Init(); framebuffer.reset(); - ImGui_ImplSdlGL3_Shutdown(); SDL_GL_DeleteContext(glContext); SDL_DestroyWindow(window); SDL_Quit(); @@ -152,8 +149,6 @@ void Render::PrepareToRendering() { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D_ARRAY, AssetManager::GetTextureAtlasId()); - ImGui_ImplSdlGL3_Init(window); - int width, height; SDL_GL_GetDrawableSize(window, &width, &height); framebuffer = std::make_unique(width, height, true); @@ -162,9 +157,6 @@ void Render::PrepareToRendering() { } void Render::UpdateKeyboard() { - if (ImGui::GetIO().WantCaptureKeyboard) - return; - SDL_Scancode toUpdate[] = { SDL_SCANCODE_A,SDL_SCANCODE_W,SDL_SCANCODE_S,SDL_SCANCODE_D,SDL_SCANCODE_SPACE }; const Uint8 *kbState = SDL_GetKeyboardState(0); for (auto key : toUpdate) { @@ -213,8 +205,6 @@ void Render::HandleEvents() { SDL_PumpEvents(); SDL_Event event; while (SDL_PollEvent(&event)) { - ImGui_ImplSdlGL3_ProcessEvent(&event); - switch (event.type) { case SDL_QUIT: { LOG(INFO) << "Received close event by window closing"; @@ -286,15 +276,6 @@ void Render::HandleEvents() { case SDL_SCANCODE_SLASH: case SDL_SCANCODE_T: { - if (!ImGui::GetIO().WantCaptureKeyboard) { - auto state = GetState(); - if (state == State::Playing) { - SetState(State::Chat); - } else if (state == State::Chat) { - SetState(State::Playing); - } - } - break; } @@ -306,35 +287,14 @@ 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)); - } - break; } case SDL_MOUSEBUTTONDOWN: { - if (isMouseCaptured && !ImGui::GetIO().WantCaptureMouse) { - if (event.button.button == SDL_BUTTON_LEFT) - PUSH_EVENT("LmbPressed", 0); - else if (event.button.button == SDL_BUTTON_RIGHT) - PUSH_EVENT("RmbPressed", 0); - } - break; } case SDL_MOUSEBUTTONUP: { - if (isMouseCaptured && !ImGui::GetIO().WantCaptureMouse) { - if (event.button.button == SDL_BUTTON_LEFT) - PUSH_EVENT("LmbReleased", 0); - else if (event.button.button == SDL_BUTTON_RIGHT) - PUSH_EVENT("RmbReleased", 0); - } break; } @@ -380,308 +340,6 @@ void Render::RenderGui() { OPTICK_EVENT(); rmlContext->Render(); - - ImGui_ImplSdlGL3_NewFrame(window); - - if (isMouseCaptured) { - auto& io = ImGui::GetIO(); - io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); - } - - const ImGuiWindowFlags windowFlags = ImGuiWindowFlags_NoTitleBar | - ImGuiWindowFlags_NoResize | - ImGuiWindowFlags_NoMove | - ImGuiWindowFlags_AlwaysAutoResize| - ImGuiWindowFlags_NoSavedSettings; - - //ImGui::ShowTestWindow(); - - ImGui::SetNextWindowPos(ImVec2(10, 10)); - ImGui::Begin("DebugInfo", 0, ImVec2(0, 0), 0.4f, windowFlags); - ImGui::Text("Debug Info:"); - ImGui::Separator(); - ImGui::Text("State: %s", stateString.c_str()); - ImGui::Text("FPS: %.1f (%.3fms)", ImGui::GetIO().Framerate, 1000.0f / ImGui::GetIO().Framerate); - float gameTime = DebugInfo::gameThreadTime / 100.0f; - if (world) { - Entity *playerPtr = GetGameState()->GetPlayer(); - SelectionStatus selectionStatus = GetGameState()->GetSelectionStatus(); - const World *worldPtr = &GetGameState()->GetWorld(); - - ImGui::Text("TPS: %.1f (%.2fms)", 1000.0f / gameTime, gameTime); - ImGui::Text("Sections loaded: %d", (int) DebugInfo::totalSections); - ImGui::Text( - "SectionsRenderer: %d (%d)", - (int) DebugInfo::renderSections,(int) DebugInfo::readyRenderer); - - ImGui::Text( - "Culled sections: %d", - (int) DebugInfo::renderSections - world->culledSections); - - ImGui::Text( - "Rendered faces: %d", - (int)DebugInfo::renderFaces); - - ImGui::Text( - "Player pos: %.1f %.1f %.1f OnGround=%d", - playerPtr->pos.x, - playerPtr->pos.y, - playerPtr->pos.z, - playerPtr->onGround); - - ImGui::Text( - "Player block pos: %d %d %d in %d %d %d", - (int)(playerPtr->pos.x - std::floor(playerPtr->pos.x / 16.0) * 16), - (int)(playerPtr->pos.y - std::floor(playerPtr->pos.y / 16.0) * 16), - (int)(playerPtr->pos.z - std::floor(playerPtr->pos.z / 16.0) * 16), - - (int)std::floor(playerPtr->pos.x / 16.0), - (int)std::floor(playerPtr->pos.y / 16.0), - (int)std::floor(playerPtr->pos.z / 16.0)); - - ImGui::Text( - "Player vel: %.1f %.1f %.1f", - playerPtr->vel.x, - playerPtr->vel.y, - playerPtr->vel.z); - - ImGui::Text( - "Player health: %.1f/%.1f", - GetGameState()->GetPlayerStatus().health, 20.0f); - - ImGui::Text( - "Selected block: %d %d %d : %.1f", - selectionStatus.selectedBlock.x, - selectionStatus.selectedBlock.y, - selectionStatus.selectedBlock.z, - selectionStatus.distanceToSelectedBlock); - - ImGui::Text("Selected block light: %d (%d)", - worldPtr->GetBlockLight(selectionStatus.selectedBlock), - worldPtr->GetBlockSkyLight(selectionStatus.selectedBlock)); - - ImGui::Text("Selected block id: %d:%d (%s)", - worldPtr->GetBlockId(selectionStatus.selectedBlock).id, - worldPtr->GetBlockId(selectionStatus.selectedBlock).state, - AssetManager::GetAssetNameByBlockId(BlockId{ worldPtr->GetBlockId(selectionStatus.selectedBlock).id,0 }).c_str()); - - ImGui::Text("Selected block variant: %s:%s", - GetBlockInfo(worldPtr->GetBlockId(selectionStatus.selectedBlock)).blockstate.c_str(), - GetBlockInfo(worldPtr->GetBlockId(selectionStatus.selectedBlock)).variant.c_str()); - } - ImGui::End(); - - - switch (GetState()) { - case State::MainMenu: { - ImGui::SetNextWindowPosCenter(); - ImGui::Begin("Menu", 0, windowFlags); - if (ImGui::Button("Connect")) { - std::string addr(fieldServerAddr); - size_t index = addr.find_last_of(':'); - unsigned short port; - if (index == std::string::npos) - port = 25565; - else { - try { - port = std::stoi(addr.substr(index + 1)); - } catch (std::exception &e) { - port = 25565; - } - } - PUSH_EVENT("ConnectToServer", std::make_tuple(addr.substr(0, index), port, std::string(fieldUsername))); - } - ImGui::InputText("Username", fieldUsername, 512); - ImGui::InputText("Address", fieldServerAddr, 512); - ImGui::Separator(); - if (ImGui::Button("Exit")) - PUSH_EVENT("Exit",0); - ImGui::End(); - break; - } - - case State::Loading: - break; - - case State::Chat: { - ImGui::SetNextWindowPosCenter(); - ImGui::Begin("Chat", 0, windowFlags); - for (const auto& msg : chatMessages) { - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1,1,1,1)); - ImGui::TextWrapped("%s", msg.c_str()); - ImGui::PopStyleColor(); - } - static char buff[256]; - ImGui::InputText("", buff, 256); - ImGui::SameLine(); - if (ImGui::Button("Send")) { - PUSH_EVENT("SendChatMessage", std::string(buff)); - } - ImGui::End(); - break; - } - - case State::Inventory: { - auto renderSlot = [](const SlotDataType &slot, int i) -> bool { - return ImGui::Button(((slot.BlockId == -1 ? " ##" : - AssetManager::GetAssetNameByBlockId(BlockId{ (unsigned short)slot.BlockId,0 }) + " x" + std::to_string(slot.ItemCount) + "##") - + std::to_string(i)).c_str()); - }; - ImGui::SetNextWindowPosCenter(); - ImGui::Begin("Inventory", 0, windowFlags); - const Window& inventory = GetGameState()->GetInventory(); - //Hand and drop slots - if (renderSlot(inventory.handSlot, -1)) { - - } - ImGui::SameLine(); - if (ImGui::Button("Drop")) { - //inventory.MakeClick(-1, true, true); - } - ImGui::SameLine(); - ImGui::Text("Hand slot and drop mode"); - ImGui::Separator(); - //Crafting - if (renderSlot(inventory.slots[1], 1)) { - //inventory.MakeClick(1, true); - } - ImGui::SameLine(); - if (renderSlot(inventory.slots[2], 2)) { - //inventory.MakeClick(2, true); - } - //Crafting result - ImGui::SameLine(); - ImGui::Text("Result"); - ImGui::SameLine(); - if (renderSlot(inventory.slots[0], 0)) { - //inventory.MakeClick(0, true); - } - //Crafting second line - if (renderSlot(inventory.slots[3], 3)) { - //inventory.MakeClick(3, true); - } - ImGui::SameLine(); - if (renderSlot(inventory.slots[4], 4)) { - //inventory.MakeClick(4, true); - } - ImGui::Separator(); - //Armor and offhand - for (int i = 5; i < 8 + 1; i++) { - if (renderSlot(inventory.slots[i], i)) { - //inventory.MakeClick(i, true); - } - ImGui::SameLine(); - } - if (renderSlot(inventory.slots[45], 45)) { - //inventory.MakeClick(45, true); - } - ImGui::SameLine(); - ImGui::Text("Armor and offhand"); - ImGui::Separator(); - for (int i = 36; i < 44 + 1; i++) { - if (renderSlot(inventory.slots[i], i)) { - //inventory.MakeClick(i, true); - } - ImGui::SameLine(); - } - ImGui::Text("Hotbar"); - ImGui::Separator(); - ImGui::Text("Main inventory"); - for (int i = 9; i < 17 + 1; i++) { - if (renderSlot(inventory.slots[i], i)) { - //inventory.MakeClick(i, true); - } - ImGui::SameLine(); - } - ImGui::Text(""); - for (int i = 18; i < 26 + 1; i++) { - if (renderSlot(inventory.slots[i], i)) { - //inventory.MakeClick(i, true); - } - ImGui::SameLine(); - } - ImGui::Text(""); - for (int i = 27; i < 35 + 1; i++) { - if (renderSlot(inventory.slots[i], i)) { - //inventory.MakeClick(i, true); - } - ImGui::SameLine(); - } - ImGui::End(); - - break; - } - - case State::Paused: { - ImGui::SetNextWindowPosCenter(); - ImGui::Begin("Pause Menu", 0, windowFlags); - if (ImGui::Button("Continue")) { - SetState(State::Playing); - } - ImGui::Separator(); - - ImGui::SliderFloat("Render distance", &fieldDistance, 1.0f, 16.0f); - - ImGui::SliderFloat("Brightness", &fieldBrightness, 0.0f, 1.0f); - - ImGui::SliderFloat("Sensetivity", &fieldSensetivity, 0.01f, 1.0f); - - ImGui::SliderFloat("Target FPS", &fieldTargetFps, 1.0f, 300.0f); - - ImGui::SliderFloat("Resolution scale", &fieldResolutionScale, 0.1f, 2.0f); - - ImGui::Checkbox("Wireframe", &fieldWireframe); - - ImGui::Checkbox("VSync", &fieldVsync); - - ImGui::Checkbox("Creative flight", &fieldFlight); - - if (ImGui::Button("Apply settings")) { - if (fieldDistance != world->MaxRenderingDistance) { - world->MaxRenderingDistance = fieldDistance; - PUSH_EVENT("UpdateSectionsRender", 0); - } - - if (fieldSensetivity != sensetivity) - sensetivity = fieldSensetivity; - - GetGameState()->GetPlayer()->isFlying = fieldFlight; - - isWireframe = fieldWireframe; - GetTime()->SetDelayLength(std::chrono::duration(1.0 / fieldTargetFps * 1000.0)); - if (fieldVsync) { - GetTime()->SetDelayLength(std::chrono::milliseconds(0)); - SDL_GL_SetSwapInterval(1); - } else - SDL_GL_SetSwapInterval(0); - - PUSH_EVENT("SetMinLightLevel", fieldBrightness); - - int width, height; - SDL_GL_GetDrawableSize(window, &width, &height); - framebuffer->Resize(width * fieldResolutionScale, height * fieldResolutionScale); - } - ImGui::Separator(); - - if (ImGui::Button("Disconnect")) { - PUSH_EVENT("Disconnect", std::string("Disconnected by user")); - } - ImGui::End(); - break; - } - - case State::InitialLoading: - break; - - case State::Playing: { - ImGui::SetNextWindowPosCenter(); - ImGui::Begin("",0,windowFlags); - ImGui::End(); - break; - } - } - - ImGui::Render(); } void Render::InitEvents() { -- cgit v1.2.3 From cc937302964a04aa8e7cdc0e6796503559efaa69 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 13 Jun 2021 21:20:25 +0500 Subject: Improved RmlUi integration --- src/Render.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'src/Render.cpp') 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 #include #include +#include #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")) -- cgit v1.2.3 From 94b3b6b32bc8b996c9689fb89a381cf216353641 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Thu, 17 Jun 2021 01:56:25 +0500 Subject: Implemented main menu in Rml and improved RmlUi support --- src/Render.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 4 deletions(-) (limited to 'src/Render.cpp') diff --git a/src/Render.cpp b/src/Render.cpp index 48bbadd..39322f9 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -18,6 +18,36 @@ #include "Plugin.hpp" #include "Rml.hpp" +const std::map keyMapping = { + {SDLK_BACKSPACE, Rml::Input::KI_BACK}, + {SDLK_INSERT, Rml::Input::KI_INSERT}, + {SDLK_DELETE, Rml::Input::KI_DELETE}, + {SDLK_HOME, Rml::Input::KI_HOME}, + {SDLK_END, Rml::Input::KI_END}, + {SDLK_LEFT, Rml::Input::KI_LEFT}, + {SDLK_RIGHT, Rml::Input::KI_RIGHT}, + {SDLK_UP, Rml::Input::KI_UP}, + {SDLK_DOWN, Rml::Input::KI_DOWN}, + {SDLK_TAB, Rml::Input::KI_TAB} +}; + +inline int ConvertKeymodsSdlToRml(unsigned short keyMods) { + int ret = 0; + if (keyMods & KMOD_SHIFT) + ret |= Rml::Input::KM_SHIFT; + if (keyMods & KMOD_CTRL) + ret |= Rml::Input::KM_CTRL; + if (keyMods & KMOD_ALT) + ret |= Rml::Input::KM_ALT; + if (keyMods & KMOD_GUI) + ret |= Rml::Input::KM_META; + if (keyMods & KMOD_NUM) + ret |= Rml::Input::KM_NUMLOCK; + if (keyMods & KMOD_CAPS) + ret |= Rml::Input::KM_CAPSLOCK; + return ret; +} + Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) { InitEvents(); @@ -129,7 +159,7 @@ void Render::InitGlew() { int width, height; SDL_GL_GetDrawableSize(window, &width, &height); glViewport(0, 0, width, height); - glClearColor(0.8,0.8,0.8, 1.0f); + glClearColor(0.0f,0.0f,0.0f, 1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); @@ -203,6 +233,8 @@ void Render::RenderFrame() { } void Render::HandleEvents() { + int rmlKeymods = ConvertKeymodsSdlToRml(sdlKeyMods); + SDL_PumpEvents(); SDL_Event event; while (SDL_PollEvent(&event)) { @@ -247,6 +279,13 @@ void Render::HandleEvents() { } case SDL_KEYDOWN: { + sdlKeyMods = event.key.keysym.mod; + rmlKeymods = ConvertKeymodsSdlToRml(sdlKeyMods); + + auto it = keyMapping.find(event.key.keysym.sym); + Rml::Input::KeyIdentifier ki = it != keyMapping.end() ? it->second : Rml::Input::KeyIdentifier::KI_UNKNOWN; + rmlContext->ProcessKeyDown(ki, rmlKeymods); + switch (event.key.keysym.scancode) { case SDL_SCANCODE_ESCAPE: { auto state = GetState(); @@ -294,6 +333,16 @@ void Render::HandleEvents() { break; } + case SDL_KEYUP: { + sdlKeyMods = event.key.keysym.mod; + rmlKeymods = ConvertKeymodsSdlToRml(sdlKeyMods); + + auto it = keyMapping.find(event.key.keysym.sym); + Rml::Input::KeyIdentifier ki = it != keyMapping.end() ? it->second : Rml::Input::KeyIdentifier::KI_UNKNOWN; + rmlContext->ProcessKeyUp(ki, rmlKeymods); + break; + } + case SDL_MOUSEMOTION: { if (isMouseCaptured) { double deltaX = event.motion.xrel; @@ -304,7 +353,7 @@ void Render::HandleEvents() { } else { int mouseX, mouseY; SDL_GetMouseState(&mouseX, &mouseY); - rmlContext->ProcessMouseMove(mouseX, mouseY, 0); + rmlContext->ProcessMouseMove(mouseX, mouseY, rmlKeymods); } break; } @@ -320,7 +369,7 @@ void Render::HandleEvents() { 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); + rmlContext->ProcessMouseButtonDown(event.button.button - 1, rmlKeymods); } break; @@ -337,16 +386,34 @@ void Render::HandleEvents() { 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); + rmlContext->ProcessMouseButtonUp(event.button.button - 1, rmlKeymods); } break; } + case SDL_TEXTINPUT: { + rmlContext->ProcessTextInput(Rml::String(event.text.text)); + break; + } + default: break; } } + char* rawClipboard = SDL_GetClipboardText(); + std::string clipboard = rawClipboard; + SDL_free(rawClipboard); + + if (clipboard != rmlSystem->clipboard) { + rmlSystem->clipboard = clipboard; + } + rmlContext->Update(); + + if (clipboard != rmlSystem->clipboard) { + clipboard = rmlSystem->clipboard; + SDL_SetClipboardText(clipboard.c_str()); + } } void Render::HandleMouseCapture() { -- cgit v1.2.3 From 8c033fff3d82d5f0e4c5d2eb3c5d10efc60ee851 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Fri, 18 Jun 2021 17:14:19 +0500 Subject: Implemented RmlFileInterface --- src/Render.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'src/Render.cpp') 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"; } -- cgit v1.2.3 From 14939a87010ab17425d73b53829aa22c17c96d79 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Tue, 22 Jun 2021 03:38:08 +0500 Subject: Added Settings UI --- src/Render.cpp | 93 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 43 deletions(-) (limited to 'src/Render.cpp') diff --git a/src/Render.cpp b/src/Render.cpp index c0885e3..bee8ffb 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -67,47 +67,11 @@ Render::Render(unsigned int windowWidth, unsigned int windowHeight, AssetManager::InitPostRml(); glCheckError(); - //Read settings - strcpy(fieldUsername, Settings::Read("username", "HelloOne").c_str()); - strcpy(fieldServerAddr, Settings::Read("serverAddr", "127.0.0.1").c_str()); - fieldDistance = Settings::ReadDouble("renderDistance", 2.0); - fieldTargetFps = Settings::ReadDouble("targetFps", 60.0); - fieldSensetivity = Settings::ReadDouble("mouseSensetivity", 0.1); - fieldVsync = Settings::ReadBool("vsync", false); - fieldWireframe = Settings::ReadBool("wireframe", false); - fieldFlight = Settings::ReadBool("flight", false); - fieldBrightness = Settings::ReadDouble("brightness", 0.2f); - fieldResolutionScale = Settings::ReadDouble("resolutionScale", 1.0f); - - //Apply settings - if (fieldSensetivity != sensetivity) - sensetivity = fieldSensetivity; - isWireframe = fieldWireframe; - GetTime()->SetDelayLength(std::chrono::duration(1.0 / fieldTargetFps * 1000.0)); - if (fieldVsync) { - GetTime()->SetDelayLength(std::chrono::milliseconds(0)); - SDL_GL_SetSwapInterval(1); - } - else - SDL_GL_SetSwapInterval(0); - framebuffer->Resize(renderState.WindowWidth * fieldResolutionScale, renderState.WindowHeight * fieldResolutionScale); LOG(INFO) << "Supported threads: " << std::thread::hardware_concurrency(); } Render::~Render() { - Settings::Write("username", fieldUsername); - Settings::Write("serverAddr", fieldServerAddr); - Settings::WriteDouble("renderDistance", fieldDistance); - Settings::WriteDouble("targetFps", fieldTargetFps); - Settings::WriteDouble("mouseSensetivity", fieldSensetivity); - Settings::WriteBool("vsync", fieldVsync); - Settings::WriteBool("wireframe", fieldWireframe); - Settings::WriteBool("flight", fieldFlight); - Settings::WriteDouble("brightness", fieldBrightness); - Settings::WriteDouble("resolutionScale", fieldResolutionScale); - Settings::Save(); - Rml::RemoveContext("default"); rmlRender.reset(); rmlSystem.reset(); @@ -256,7 +220,8 @@ void Render::HandleEvents() { renderState.WindowHeight = height; rmlRender->Update(width, height); rmlContext->SetDimensions(Rml::Vector2i(width, height)); - framebuffer->Resize(width * fieldResolutionScale, height * fieldResolutionScale); + double resolutionScale = Settings::ReadDouble("resolutionScale", 1.0f); + framebuffer->Resize(width * resolutionScale, height * resolutionScale); Framebuffer::GetDefault().Resize(width, height); break; } @@ -463,7 +428,7 @@ void Render::InitEvents() { listener.RegisterHandler("PlayerConnected", [this](const Event&) { stateString = "Loading terrain..."; world = std::make_unique(); - world->MaxRenderingDistance = fieldDistance; + world->MaxRenderingDistance = Settings::ReadDouble("renderDistance", 2.0f); PUSH_EVENT("UpdateSectionsRender", 0); }); @@ -471,9 +436,9 @@ void Render::InitEvents() { stateString = "Playing"; renderWorld = true; SetState(State::Playing); - glClearColor(0, 0, 0, 1.0f); - GetGameState()->GetPlayer()->isFlying = this->fieldFlight; - PUSH_EVENT("SetMinLightLevel", fieldBrightness); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + GetGameState()->GetPlayer()->isFlying = Settings::ReadBool("flight", false); + PUSH_EVENT("SetMinLightLevel", (float)Settings::ReadDouble("brightness", 0.2f)); }); listener.RegisterHandler("ConnectionFailed", [this](const Event& eventData) { @@ -481,7 +446,7 @@ void Render::InitEvents() { renderWorld = false; world.reset(); SetState(State::MainMenu); - glClearColor(0.8, 0.8, 0.8, 1.0f); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); }); listener.RegisterHandler("Disconnected", [this](const Event& eventData) { @@ -489,7 +454,7 @@ void Render::InitEvents() { renderWorld = false; world.reset(); SetState(State::MainMenu); - glClearColor(0.8, 0.8, 0.8, 1.0f); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); }); listener.RegisterHandler("Connecting", [this](const Event&) { @@ -535,6 +500,48 @@ void Render::InitEvents() { break; } }); + + listener.RegisterHandler("SettingsUpdate", [this](const Event& eventData) { + if (world) { + float renderDistance = Settings::ReadDouble("renderDistance", 2.0f); + if (renderDistance != world->MaxRenderingDistance) { + world->MaxRenderingDistance = renderDistance; + PUSH_EVENT("UpdateSectionsRender", 0); + } + } + + + float mouseSensetivity = Settings::ReadDouble("mouseSensetivity", 0.1f); + if (mouseSensetivity != sensetivity) + sensetivity = mouseSensetivity; + + if (GetGameState()) { + bool flight = Settings::ReadBool("flight", false); + GetGameState()->GetPlayer()->isFlying = flight; + } + + bool wireframe = Settings::ReadBool("wireframe", false); + isWireframe = wireframe; + + float targetFps = Settings::ReadDouble("targetFps", 60.0f); + GetTime()->SetDelayLength(std::chrono::duration(1.0 / targetFps * 1000.0)); + + bool vsync = Settings::ReadBool("vsync", false); + if (vsync) { + GetTime()->SetDelayLength(std::chrono::milliseconds(0)); + SDL_GL_SetSwapInterval(1); + } + else + SDL_GL_SetSwapInterval(0); + + float brightness = Settings::ReadDouble("brightness", 0.2f); + PUSH_EVENT("SetMinLightLevel", brightness); + + float resolutionScale = Settings::ReadDouble("resolutionScale", 1.0f); + int width, height; + SDL_GL_GetDrawableSize(window, &width, &height); + framebuffer->Resize(width * resolutionScale, height * resolutionScale); + }); } void Render::InitRml() { -- cgit v1.2.3