diff options
-rw-r--r-- | cwd/assets/altcraft/scripts/init.lua | 34 | ||||
-rw-r--r-- | cwd/assets/altcraft/scripts/ui.lua | 17 | ||||
-rw-r--r-- | cwd/assets/altcraft/ui/chat-styles.rcss | 62 | ||||
-rw-r--r-- | cwd/assets/altcraft/ui/chat.rml | 16 | ||||
-rw-r--r-- | src/GameState.cpp | 3 | ||||
-rw-r--r-- | src/Plugin.cpp | 25 | ||||
-rw-r--r-- | src/Plugin.hpp | 3 | ||||
-rw-r--r-- | src/Render.cpp | 12 | ||||
-rw-r--r-- | src/Render.hpp | 1 |
9 files changed, 160 insertions, 13 deletions
diff --git a/cwd/assets/altcraft/scripts/init.lua b/cwd/assets/altcraft/scripts/init.lua index e13fc81..b60a988 100644 --- a/cwd/assets/altcraft/scripts/init.lua +++ b/cwd/assets/altcraft/scripts/init.lua @@ -6,6 +6,7 @@ local plugin = { onChangeState = nil, onTick = nil, onRequestBlockInfo = nil, + onChatMessage = nil, } function plugin.onLoad () @@ -17,6 +18,7 @@ function plugin.onLoad () con:LoadDocument("altcraft/ui/options") con:LoadDocument("altcraft/ui/loading") con:LoadDocument("altcraft/ui/respawn") + con:LoadDocument("altcraft/ui/chat") uiMainMenu:Show() AC.Settings.Load() @@ -72,9 +74,39 @@ function plugin.onRequestBlockInfo(blockPos) return blocks.GetBlockInfo(blockPos) end +function plugin.onChatMessage(chat, pos) + local chatDoc = {} + for i,d in ipairs(rmlui.contexts["default"].documents) do + if d.title == "Chat" then + chatDoc = d + end + end + + local msg = chat:ToPlainText() + msg = string.gsub(msg,'&','&') + msg = string.gsub(msg,'<','<') + msg = string.gsub(msg,'>','>') + msg = string.gsub(msg,'""','"') + msg = string.gsub(msg,"''",''') + + local color = "" + if pos == 0 then + color = "" + elseif pos == 1 then + color = 'style="color: #BBBBBB"' + elseif pos == 2 then + color = 'style="color: maroon"' + else + color = 'style="color: navy"' + end + + chatDoc:GetElementById('chat').inner_rml = chatDoc:GetElementById('chat').inner_rml .. string.format('<p class="chat-msg" %s>%s</p>', color, msg) + MoveChatToBottom = true +end + AC.RegisterDimension(0, Dimension.new("overworld", true)) AC.RegisterDimension(-1, Dimension.new("the_nether", false)) AC.RegisterDimension(1, Dimension.new("the_end", false)) AC.RegisterPlugin(plugin) -plugin = nil
\ No newline at end of file +plugin = nil diff --git a/cwd/assets/altcraft/scripts/ui.lua b/cwd/assets/altcraft/scripts/ui.lua index 98f0fae..b938737 100644 --- a/cwd/assets/altcraft/scripts/ui.lua +++ b/cwd/assets/altcraft/scripts/ui.lua @@ -53,6 +53,16 @@ function ConnectToServer(doc) doc:GetElementById('username'):GetAttribute('value')) end +function SendChatMessage(doc) + local msg = doc:GetElementById("chat-input"):GetAttribute("value") + if msg == nil then + return + end + doc:GetElementById("chat-input"):SetAttribute("value", "") + + AC.SendChatMessage(msg) +end + function OptionsDefaultHandler(event) local input = event.current_element.previous_sibling local id = input:GetAttribute("id") @@ -85,14 +95,21 @@ end function UpdateUi() local doc = {} local uiDoc = {} + local chatDoc = {} for i,d in ipairs(rmlui.contexts["default"].documents) do if d.title == "Playing" then doc = d elseif d.title == "Options" then uiDoc = d + elseif d.title == "Chat" then + chatDoc = d end end + if MoveChatToBottom ~= nil and MoveChatToBottom == true then + chatDoc:GetElementById('chat').scroll_top = chatDoc:GetElementById('chat').scroll_height + end + if AC.GetGameState() and AC.GetGameState():GetPlayer() and AC.GetGameState():GetTimeStatus().worldAge > 0 then local time = AC.GetTime() local rawFps = 1.0 / time:GetRealDeltaS() diff --git a/cwd/assets/altcraft/ui/chat-styles.rcss b/cwd/assets/altcraft/ui/chat-styles.rcss new file mode 100644 index 0000000..91f5b81 --- /dev/null +++ b/cwd/assets/altcraft/ui/chat-styles.rcss @@ -0,0 +1,62 @@ +#body-chat { + +} + +p { + display: block; +} + +scrollbarvertical { + background-color: #2c2c2c55; + width: 3vh; + left: 0; +} + +scrollbarvertical sliderbar { + height: 10vh; + background-color: #9c9c9c55; +} + +scrollbarvertical sliderbar:active { + background-color: #cfd69d; +} + +scrollbarvertical sliderarrowdec { + display: none; +} + +scrollbarvertical sliderarrowinc { + display: none; +} + +#chat { + background-color: #00000055; + width: 80%; + height: 70%; + position: fixed; + bottom: 10%; + left: 0; + text-align: left; + overflow-y: auto; +} + +.chat-msg { + font-size: 5vh; + bottom: 0%; +} + +#chat-input { + width: 90%; +} + +#chat-send { + margin: 0.25%; +} + +#chat-footer { + position: fixed; + display: block; + bottom: 0; + width: 100%; + left: 0; +} diff --git a/cwd/assets/altcraft/ui/chat.rml b/cwd/assets/altcraft/ui/chat.rml new file mode 100644 index 0000000..8634450 --- /dev/null +++ b/cwd/assets/altcraft/ui/chat.rml @@ -0,0 +1,16 @@ +<rml> + <head> + <link type="text/rcss" href="mc-styles" /> + <link type="text/rcss" href="chat-styles" /> + <script src="/altcraft/scripts/ui"></script> + <title>Chat</title> + </head> + <body class="body-chat"> + <div id="chat"> + </div> + <div id="chat-footer"> + <input type="text" id="chat-input" class="mc-text" onkeydown="if event.parameters.key_identifier == 72 then SendChatMessage(document) end" /> + <button id="chat-send" class="mc-button" onclick="SendChatMessage(document)">Send</button> + </div> + </body> +</rml> diff --git a/src/GameState.cpp b/src/GameState.cpp index 50a9004..89743e4 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -7,6 +7,7 @@ #include "Event.hpp" #include "Packet.hpp" #include "Game.hpp" +#include "Plugin.hpp" void GameState::Update(double deltaTime) { OPTICK_EVENT(); @@ -168,7 +169,7 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr) { case ChatMessageCB: { auto packet = std::static_pointer_cast<PacketChatMessageCB>(ptr); LOG(INFO) << "Message (" << int(packet->Position) << "): " << packet->JsonData.ToPlainText(); - PUSH_EVENT("ChatMessageReceived", std::make_tuple(packet->JsonData, packet->Position)); + PluginSystem::CallOnChatMessage(packet->JsonData, packet->Position); break; } diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 64fc00b..98df8c3 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -12,6 +12,7 @@ #include "AssetManager.hpp" #include "Settings.hpp" #include "DebugInfo.hpp" +#include "Chat.hpp" struct Plugin { @@ -23,6 +24,7 @@ struct Plugin { const std::function<void(std::string)> onChangeState; const std::function<void(double)> onTick; const std::function<BlockInfo(Vector)> onRequestBlockInfo; + const std::function<void(Chat, int)> onChatMessage; }; @@ -42,6 +44,7 @@ namespace PluginApi { plugin["onChangeState"].get_or(std::function<void(std::string)>()), plugin["onTick"].get_or(std::function<void(double)>()), plugin["onRequestBlockInfo"].get_or(std::function<BlockInfo(Vector)>()), + plugin["onChatMessage"].get_or(std::function<void(Chat, int)>()), }; plugins.push_back(nativePlugin); LOG(INFO)<<"Loading plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name); @@ -128,6 +131,10 @@ namespace PluginApi { return 0; } } + + void SendChatMessage(const std::string& msg) { + PUSH_EVENT("SendChatMessage", msg); + } } int LoadFileRequire(lua_State* L) { @@ -265,6 +272,9 @@ void PluginSystem::Init() { "GetDeltaS", &LoopExecutionTimeController::GetDeltaS, "GetRealDeltaS", &LoopExecutionTimeController::GetRealDeltaS); + lua.new_usertype<Chat>("Chat", + "ToPlainText", &Chat::ToPlainText); + sol::table apiTable = lua["AC"].get_or_create<sol::table>(); sol::table apiSettings = lua["AC"]["Settings"].get_or_create<sol::table>(); @@ -293,6 +303,7 @@ void PluginSystem::Init() { apiTable["GetTime"] = GetTime; apiTable["GetBlockInfo"] = GetBlockInfo; apiTable["GetDebugValue"] = PluginApi::GetDebugValue; + apiTable["SendChatMessage"] = PluginApi::SendChatMessage; } lua_State* PluginSystem::GetLuaState() { @@ -356,3 +367,17 @@ BlockInfo PluginSystem::RequestBlockInfo(Vector blockPos) { } return ret; } + +void PluginSystem::CallOnChatMessage(const Chat& chat, int position) { + OPTICK_EVENT(); + for (Plugin& plugin : plugins) { + if (plugin.onRequestBlockInfo && plugin.errors < 10) + try { + plugin.onChatMessage(chat, position); + } + catch (sol::error& e) { + LOG(ERROR) << e.what(); + plugin.errors++; + } + } +} diff --git a/src/Plugin.hpp b/src/Plugin.hpp index 7af27a4..13b126e 100644 --- a/src/Plugin.hpp +++ b/src/Plugin.hpp @@ -6,6 +6,7 @@ class BlockInfo; struct lua_State; +class Chat; namespace PluginSystem { void Init(); @@ -19,4 +20,6 @@ namespace PluginSystem { void CallOnTick(double deltaTime); BlockInfo RequestBlockInfo(Vector blockPos); + + void CallOnChatMessage(const Chat& chat, int position); }
\ No newline at end of file diff --git a/src/Render.cpp b/src/Render.cpp index a76bba7..3bf1e6b 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -29,7 +29,8 @@ const std::map<SDL_Keycode, Rml::Input::KeyIdentifier> keyMapping = { {SDLK_RIGHT, Rml::Input::KI_RIGHT}, {SDLK_UP, Rml::Input::KI_UP}, {SDLK_DOWN, Rml::Input::KI_DOWN}, - {SDLK_TAB, Rml::Input::KI_TAB} + {SDLK_TAB, Rml::Input::KI_TAB}, + {SDLK_RETURN, Rml::Input::KI_RETURN} }; inline int ConvertKeymodsSdlToRml(unsigned short keyMods) { @@ -288,9 +289,6 @@ void Render::HandleEvents() { if (state == State::Playing) { SetState(State::Chat); } - else if (state == State::Chat) { - SetState(State::Playing); - } break; } @@ -477,12 +475,6 @@ void Render::InitEvents() { SetState(State::Loading); }); - listener.RegisterHandler("ChatMessageReceived", [this](const Event& eventData) { - auto data = eventData.get<std::tuple<Chat, unsigned char>>(); - std::string msg = "(" + std::to_string((int)std::get<1>(data)) + ") " + (std::get<0>(data).ToPlainText()); - chatMessages.push_back(msg); - }); - listener.RegisterHandler("StateUpdated", [this](const Event& eventData) { switch (GetState()) { case State::Playing: diff --git a/src/Render.hpp b/src/Render.hpp index 4f993c3..b8963c7 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -37,7 +37,6 @@ class Render { float sensetivity = 0.1f; bool isWireframe = false; std::unique_ptr<Framebuffer> framebuffer; - std::vector<std::string> chatMessages; EventListener listener; std::string stateString; std::unique_ptr<RmlRenderInterface> rmlRender; |