summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaG1924 <lag1924@gmail.com>2021-06-16 22:56:25 +0200
committerLaG1924 <lag1924@gmail.com>2021-06-16 22:56:25 +0200
commit94b3b6b32bc8b996c9689fb89a381cf216353641 (patch)
tree460eaf04e64e0f31bfb8cea2d6b71737ac4a1861
parentImproved RmlUi integration (diff)
downloadAltCraft-94b3b6b32bc8b996c9689fb89a381cf216353641.tar
AltCraft-94b3b6b32bc8b996c9689fb89a381cf216353641.tar.gz
AltCraft-94b3b6b32bc8b996c9689fb89a381cf216353641.tar.bz2
AltCraft-94b3b6b32bc8b996c9689fb89a381cf216353641.tar.lz
AltCraft-94b3b6b32bc8b996c9689fb89a381cf216353641.tar.xz
AltCraft-94b3b6b32bc8b996c9689fb89a381cf216353641.tar.zst
AltCraft-94b3b6b32bc8b996c9689fb89a381cf216353641.zip
-rw-r--r--CMakeLists.txt3
-rw-r--r--cwd/test.rcss105
-rw-r--r--cwd/test.rml31
-rw-r--r--src/Plugin.cpp23
-rw-r--r--src/Render.cpp75
-rw-r--r--src/Render.hpp1
-rw-r--r--src/Rml.cpp8
-rw-r--r--src/Rml.hpp6
8 files changed, 214 insertions, 38 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ac3c254..e7d166a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -206,6 +206,9 @@ target_include_directories(AltCraft PRIVATE ${RmlUi_SOURCE_DIR}/Include)
set_property(DIRECTORY ${CMAKE_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT AltCraft)
+if (MSVC)
+ target_compile_options(AltCraft PRIVATE /bigobj)
+endif()
if(MSVC AND CMAKE_BUILD_TYPE MATCHES Release)
set_target_properties(AltCraft PROPERTIES WIN32_EXECUTABLE ON)
endif()
diff --git a/cwd/test.rcss b/cwd/test.rcss
index f1e8cee..ee86894 100644
--- a/cwd/test.rcss
+++ b/cwd/test.rcss
@@ -1,28 +1,105 @@
body {
- background-color: red;
- color: blue;
- font-size: 50px;
+ background-color: #160f08;
+ color: white;
font-family: "open sans";
width: 100%;
+ height: 100%;
+ text-align: center;
}
-body:hover {
- background-color: yellow;
+.menu {
+ width: 50%;
+ margin: auto;
+ background-color: #a79f9c;
}
-div {
- background-color: green;
+.mc-title {
+ color: #8e8e8e;
+ display: block;
}
-div:hover {
- background-color: white;
+.mc-p {
+ color: #d6d4d6;
+ display: block;
+ text-align: left;
+ font-size: 4vh;
}
-button:active {
- background-color: teal;
+.mc-text {
+ border-width: 2dp;
+ border-color: #9f9793;
+ background-color: #010001;
+ color: #d6d4d6;
+ text-align: center;
+ vertical-align: middle;
+ font-size: 5vh;
}
-.right-pos {
- position: absolute;
- right: 0px;
+.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
index 4c7bfa6..de48053 100644
--- a/cwd/test.rml
+++ b/cwd/test.rml
@@ -1,26 +1,19 @@
<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>
- 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>
+ <strong class="mc-title" id="title">AltCraft</strong>
+ <p class="mc-p" id="disclaimer">AltCraft is currently not finished, but there is some buggy early testing going on.</p>
+ <p class="mc-p" id="hostname-text">Enter the hostname of a server and your username to connect to it:</p>
+ <input class="mc-text" id="hostname" value="127.0.0.1:25565"/>
+ <input class="mc-text" id="username" value="HelloOne"/>
+ <button class="mc-button" id="connect" onclick="
+ AC.ConnectToServer(
+ document: GetElementById('hostname'): GetAttribute('value'),
+ document: GetElementById('username'): GetAttribute('value'));
+ document:Close()">Connect</button>
+ <button class="mc-button" id="options">Options...</button>
+ <button class="mc-button" id="exit" onclick="AC.Exit()">Quit game</button>
</body>
</rmL>
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 {