diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Plugin.cpp | 23 | ||||
-rw-r--r-- | src/Render.cpp | 75 | ||||
-rw-r--r-- | src/Render.hpp | 1 | ||||
-rw-r--r-- | src/Rml.cpp | 8 | ||||
-rw-r--r-- | src/Rml.hpp | 6 |
5 files changed, 108 insertions, 5 deletions
diff --git a/src/Plugin.cpp b/src/Plugin.cpp index d7a9820..13045e1 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -74,6 +74,27 @@ namespace PluginApi { void RegisterDimension(int dimId, Dimension dim) { RegisterNewDimension(dimId, dim); } + + void ConnectToServer(std::string host, std::string username) { + size_t index = host.find_last_of(':'); + unsigned short port; + if (index == std::string::npos) + port = 25565; + else { + try { + port = std::stoi(host.substr(index + 1)); + } + catch (std::exception& e) { + port = 25565; + LOG(WARNING) << "Incorrect host format: " << host; + } + } + PUSH_EVENT("ConnectToServer", std::make_tuple(host.substr(0, index), port, username)); + } + + void Exit() { + PUSH_EVENT("Exit", 0); + } } int LoadFileRequire(lua_State* L) { @@ -212,6 +233,8 @@ void PluginSystem::Init() { apiTable["GetGameState"] = PluginApi::GetGameState; apiTable["RegisterBlock"] = PluginApi::RegisterBlock; apiTable["RegisterDimension"] = PluginApi::RegisterDimension; + apiTable["ConnectToServer"] = PluginApi::ConnectToServer; + apiTable["Exit"] = PluginApi::Exit; } lua_State* PluginSystem::GetLuaState() { 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<SDL_Keycode, Rml::Input::KeyIdentifier> 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() { diff --git a/src/Render.hpp b/src/Render.hpp index a8c437c..7b2313c 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -52,6 +52,7 @@ class Render { std::unique_ptr<RmlRenderInterface> rmlRender; std::unique_ptr<RmlSystemInterface> rmlSystem; Rml::Context* rmlContext; + unsigned short sdlKeyMods; void SetMouseCapture(bool IsCaptured); diff --git a/src/Rml.cpp b/src/Rml.cpp index bcdca0c..abec7db 100644 --- a/src/Rml.cpp +++ b/src/Rml.cpp @@ -37,6 +37,14 @@ bool RmlSystemInterface::LogMessage(Rml::Log::Type type, const Rml::String& mess return true; } +void RmlSystemInterface::SetClipboardText(const Rml::String& text) { + clipboard = text; +} + +void RmlSystemInterface::GetClipboardText(Rml::String& text) { + text = clipboard; +} + RmlRenderInterface::RmlRenderInterface(RenderState& renderState) : State(&renderState) { glGenVertexArrays(1, &Vao); glBindVertexArray(Vao); diff --git a/src/Rml.hpp b/src/Rml.hpp index 5815c3e..7b312c0 100644 --- a/src/Rml.hpp +++ b/src/Rml.hpp @@ -7,17 +7,21 @@ class RmlSystemInterface : public Rml::SystemInterface { double totalTime; - public: virtual double GetElapsedTime() override; virtual bool LogMessage(Rml::Log::Type type, const Rml::String& message) override; + virtual void SetClipboardText(const Rml::String& text) override; + + virtual void GetClipboardText(Rml::String& text) override; + inline void Update(double timeToUpdate) { totalTime += timeToUpdate; } + std::string clipboard; }; class RmlRenderInterface : public Rml::RenderInterface { |