From fc3fafc1a8237486016b50f0cf049c83c75160e8 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Fri, 19 Jun 2020 04:16:38 +0200 Subject: [PATCH] [Lua API] Key definition: OK. Can't be remapped yet. --- docs/lua-api-key.md | 15 +- mods/creative_inventory/init.lua | 156 +++++++++--------- mods/default/inventory.lua | 128 +++++++------- source/client/core/KeyboardHandler.cpp | 3 - .../client/network/ClientCommandHandler.cpp | 21 +-- .../client/network/ClientCommandHandler.hpp | 3 +- source/client/states/GameState.cpp | 14 +- source/client/states/SettingsMenuState.cpp | 2 - source/common/core/input/GameKey.hpp | 3 - source/common/network/Network.cpp | 4 +- source/common/network/Network.hpp | 35 ++-- source/common/world/Key.hpp | 15 +- .../server/network/ServerCommandHandler.cpp | 38 +---- 13 files changed, 212 insertions(+), 225 deletions(-) diff --git a/docs/lua-api-key.md b/docs/lua-api-key.md index 6c56fe70..8cbd03c1 100644 --- a/docs/lua-api-key.md +++ b/docs/lua-api-key.md @@ -8,7 +8,7 @@ mod:key { name = "Inventory", default_key = "E" - key_callback = function(client, screen_width, screen_height, gui_scale) + callback = function(client, screen_width, screen_height, gui_scale) show_inventory(client, screen_width, screen_height, gui_scale) end } @@ -16,6 +16,17 @@ mod:key { ## Attributes +### `callback` + +Function called when the key is pressed. + +Example: +```lua +callback = function(client, screen_width, screen_height, gui_scale) + show_inventory(client, screen_width, screen_height, gui_scale) +end +``` + ### `default_key` Keyboard key mapped to this key by default. @@ -25,6 +36,8 @@ Example: default_key = "E" ``` +Names are defined [here](https://github.com/Unarelith/GameKit/blob/master/source/core/input/KeyboardUtils.cpp). + ### `name` Name of the key. Optional field, uses `id` if not defined. diff --git a/mods/creative_inventory/init.lua b/mods/creative_inventory/init.lua index 99122356..106903fd 100644 --- a/mods/creative_inventory/init.lua +++ b/mods/creative_inventory/init.lua @@ -26,81 +26,87 @@ -- mod = openminer.mod_loader:register_mod("creative_inventory") -function show_creative_window(client, screen_width, screen_height, gui_scale) - items = {} - for k, v in pairs(openminer.registry:items()) do - if k ~= 1 and not v:has_group("group:ci_ignore") then - items[#items + 1] = {v:string_id()} +mod:key { + id = "creative_inventory", + name = "Creative window", + default_key = "H", + + callback = function(client, screen_width, screen_height, gui_scale) + items = {} + for k, v in pairs(openminer.registry:items()) do + if k ~= 1 and not v:has_group("group:ci_ignore") then + items[#items + 1] = {v:string_id()} + end end + + local gui = LuaGUI.new() + + gui:set_size(195, 136) + gui:set_centered(true) + + gui:image { + name = "img_background", + pos = {x = 0, y = 0}, + + texture = mod:path() .. "/textures/gui/creative_window.png", + clip = {x = 0, y = 0, width = 195, height = 136}, + } + + gui:inventory_data { + name = "inv_data", + + width = 9, + height = 7, + + items = items, + + is_unlimited = true, + } + + gui:inventory { + name = "inv_creative_items", + pos = {x = 8, y = 17}, + + inventory = { + source = "temp", + inventory_name = "inv_data", + offset = 0, + count = 9 * 5, + }, + + size = {x = 9, y = 7} + } + + gui:scroll_bar { + name = "scroll_bar", + pos = {x = 175, y = 18}, + + texture = mod:path() .. "/textures/gui/tabs.png", + clip = {x = 232, y = 0, width = 12, height = 15}, + clip_selected = {x = 244, y = 0, width = 12, height = 15}, + + widget = "inv_creative_items", + + min_y = 0, + max_y = 110 - 15, + } + + gui:inventory { + name = "inv_hotbar", + pos = {x = 8, y = 111}, + + inventory = { + source = "player", + player = "player", + inventory_name = "main", + offset = 0, + count = 9, + }, + + size = {x = 9, y = 1}, + } + + gui:show(client); end - - local gui = LuaGUI.new() - - gui:set_size(195, 136) - gui:set_centered(true) - - gui:image { - name = "img_background", - pos = {x = 0, y = 0}, - - texture = mod:path() .. "/textures/gui/creative_window.png", - clip = {x = 0, y = 0, width = 195, height = 136}, - } - - gui:inventory_data { - name = "inv_data", - - width = 9, - height = 7, - - items = items, - - is_unlimited = true, - } - - gui:inventory { - name = "inv_creative_items", - pos = {x = 8, y = 17}, - - inventory = { - source = "temp", - inventory_name = "inv_data", - offset = 0, - count = 9 * 5, - }, - - size = {x = 9, y = 7} - } - - gui:scroll_bar { - name = "scroll_bar", - pos = {x = 175, y = 18}, - - texture = mod:path() .. "/textures/gui/tabs.png", - clip = {x = 232, y = 0, width = 12, height = 15}, - clip_selected = {x = 244, y = 0, width = 12, height = 15}, - - widget = "inv_creative_items", - - min_y = 0, - max_y = 110 - 15, - } - - gui:inventory { - name = "inv_hotbar", - pos = {x = 8, y = 111}, - - inventory = { - source = "player", - player = "player", - inventory_name = "main", - offset = 0, - count = 9, - }, - - size = {x = 9, y = 1}, - } - - gui:show(client); -end +} diff --git a/mods/default/inventory.lua b/mods/default/inventory.lua index 9c18414b..5c54af43 100644 --- a/mods/default/inventory.lua +++ b/mods/default/inventory.lua @@ -26,77 +26,73 @@ -- local modpath = mod:path() -function show_inventory(client, screen_width, screen_height, gui_scale) - local gui = LuaGUI.new() - - gui:set_size(176, 166) - gui:set_centered(true) - - gui:image { - name = "img_background", - pos = {x = 0, y = 0}, - - texture = modpath .. "/textures/gui/inventory.png", - clip = {x = 0, y = 0, width = 176, height = 166}, - } - - gui:inventory { - name = "inv_main", - pos = {x = 7, y = 83}, - - inventory = { - source = "player", - player = "player", - inventory_name = "main", - offset = 9, - count = 9 * 3, - }, - - size = {x = 9, y = 3}, - - shift_destination = "inv_hotbar,inv_main", - } - - gui:inventory { - name = "inv_hotbar", - pos = {x = 7, y = 141}, - - inventory = { - source = "player", - player = "player", - inventory_name = "main", - offset = 0, - count = 9, - }, - - size = {x = 9, y = 1}, - - shift_destination = "inv_main,inv_hotbar", - } - - gui:crafting { - name = "inv_crafting", - pos = {x = 97, y = 17}, - result_pos = {x = 97 + 56, y = 17 + 10}, - - inventory = { - source = "temp", - size = 2, - }, - - shift_destination = "inv_main,inv_hotbar", - } - - gui:show(client) -end - mod:key { id = "inventory", name = "Inventory", default_key = "E", - key_callback = function(client, screen_width, screen_height, gui_scale) - show_inventory(client, screen_width, screen_height, gui_scale) + callback = function(client, screen_width, screen_height, gui_scale) + local gui = LuaGUI.new() + + gui:set_size(176, 166) + gui:set_centered(true) + + gui:image { + name = "img_background", + pos = {x = 0, y = 0}, + + texture = modpath .. "/textures/gui/inventory.png", + clip = {x = 0, y = 0, width = 176, height = 166}, + } + + gui:inventory { + name = "inv_main", + pos = {x = 7, y = 83}, + + inventory = { + source = "player", + player = "player", + inventory_name = "main", + offset = 9, + count = 9 * 3, + }, + + size = {x = 9, y = 3}, + + shift_destination = "inv_hotbar,inv_main", + } + + gui:inventory { + name = "inv_hotbar", + pos = {x = 7, y = 141}, + + inventory = { + source = "player", + player = "player", + inventory_name = "main", + offset = 0, + count = 9, + }, + + size = {x = 9, y = 1}, + + shift_destination = "inv_main,inv_hotbar", + } + + gui:crafting { + name = "inv_crafting", + pos = {x = 97, y = 17}, + result_pos = {x = 97 + 56, y = 17 + 10}, + + inventory = { + source = "temp", + size = 2, + }, + + shift_destination = "inv_main,inv_hotbar", + } + + gui:show(client) end } diff --git a/source/client/core/KeyboardHandler.cpp b/source/client/core/KeyboardHandler.cpp index e445c7fa..50315c3e 100644 --- a/source/client/core/KeyboardHandler.cpp +++ b/source/client/core/KeyboardHandler.cpp @@ -48,9 +48,6 @@ KeyboardHandler::KeyboardHandler() { addKey(GameKey::Dig, "Dig", sf::Keyboard::L); addKey(GameKey::Use, "Use", sf::Keyboard::M); - addKey(GameKey::Inventory, "Inventory", sf::Keyboard::E); - addKey(GameKey::CreativeWindow, "CreativeWindow", sf::Keyboard::H); - addKey(GameKey::Chat, "Chat", sf::Keyboard::T); addKey(GameKey::Command, "Command", sf::Keyboard::Divide); diff --git a/source/client/network/ClientCommandHandler.cpp b/source/client/network/ClientCommandHandler.cpp index bd8d2247..54a7c8b4 100644 --- a/source/client/network/ClientCommandHandler.cpp +++ b/source/client/network/ClientCommandHandler.cpp @@ -89,20 +89,6 @@ void ClientCommandHandler::sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block) m_client.send(packet); } -void ClientCommandHandler::sendPlayerInventoryRequest() { - Network::Packet packet; - packet << Network::Command::PlayerInventory - << u16(Config::screenWidth) << u16(Config::screenHeight) << u8(Config::guiScale); - m_client.send(packet); -} - -void ClientCommandHandler::sendPlayerCreativeWindowRequest() { - Network::Packet packet; - packet << Network::Command::PlayerCreativeWindow - << u16(Config::screenWidth) << u16(Config::screenHeight) << u8(Config::guiScale); - m_client.send(packet); -} - void ClientCommandHandler::sendPlayerHeldItemChanged(u8 hotbarSlot, u16 itemID) { Network::Packet packet; packet << Network::Command::PlayerHeldItemChanged @@ -144,6 +130,13 @@ void ClientCommandHandler::sendChatMessage(const std::string &message) { m_client.send(packet); } +void ClientCommandHandler::sendKeyPressed(u16 keyID) { + Network::Packet packet; + packet << Network::Command::KeyPressed << keyID + << Config::screenWidth << Config::screenHeight << Config::guiScale; + m_client.send(packet); +} + template static void addComponentCommandCallback(Network::Command command, Client &client, ClientCommandHandler::EntityMap &entityMap, ClientWorld &world) { client.setCommandCallback(command, [&](Network::Packet &packet) { diff --git a/source/client/network/ClientCommandHandler.hpp b/source/client/network/ClientCommandHandler.hpp index bf1f61e2..1b1a9729 100644 --- a/source/client/network/ClientCommandHandler.hpp +++ b/source/client/network/ClientCommandHandler.hpp @@ -51,13 +51,12 @@ class ClientCommandHandler { void sendPlayerRotUpdate(); void sendPlayerDigBlock(const glm::ivec4 &selectedBlock); void sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block); - void sendPlayerInventoryRequest(); - void sendPlayerCreativeWindowRequest(); void sendPlayerHeldItemChanged(u8 hotbarSlot, u16 itemID); void sendBlockActivated(const glm::ivec4 &selectedBlock); void sendBlockInvUpdate(Inventory &inventory); void sendChunkRequest(s32 chunkX, s32 chunkY, s32 chunkZ); void sendChatMessage(const std::string &message); + void sendKeyPressed(u16 keyID); void setupCallbacks(); diff --git a/source/client/states/GameState.cpp b/source/client/states/GameState.cpp index ee1f43bb..e3948e38 100644 --- a/source/client/states/GameState.cpp +++ b/source/client/states/GameState.cpp @@ -118,6 +118,13 @@ void GameState::onEvent(const sf::Event &event) { gk::Mouse::setCursorGrabbed(true); gk::Mouse::setCursorVisible(false); } + else if (event.type == sf::Event::KeyPressed) { + for (auto &key : m_registry.keys()) { + if (event.key.code == key.defaultKeyCode()) { + m_clientCommandHandler.sendKeyPressed(key.id()); + } + } + } m_hud.onEvent(event); } @@ -140,13 +147,6 @@ void GameState::update() { if (!m_stateStack->empty() && &m_stateStack->top() == this) { m_player.processInputs(); - - if (gk::GamePad::isKeyPressedOnce(GameKey::Inventory)) { - m_clientCommandHandler.sendPlayerInventoryRequest(); - } - else if (gk::GamePad::isKeyPressedOnce(GameKey::CreativeWindow)) { - m_clientCommandHandler.sendPlayerCreativeWindowRequest(); - } } m_player.updatePosition(m_world); diff --git a/source/client/states/SettingsMenuState.cpp b/source/client/states/SettingsMenuState.cpp index 97bfd9bb..2d8abe7d 100644 --- a/source/client/states/SettingsMenuState.cpp +++ b/source/client/states/SettingsMenuState.cpp @@ -221,8 +221,6 @@ void SettingsMenuState::addInputButtons() { {GameKey::Sprint, "Sprint"}, // {GameKey::Dig, "Dig"}, // {GameKey::Use, "Use"}, - {GameKey::Inventory, "Inventory"}, - {GameKey::CreativeWindow, "Creative window"}, {GameKey::Chat, "Chat"}, {GameKey::Command, "Command"}, }; diff --git a/source/common/core/input/GameKey.hpp b/source/common/core/input/GameKey.hpp index 0e64d605..72d79ac3 100644 --- a/source/common/core/input/GameKey.hpp +++ b/source/common/core/input/GameKey.hpp @@ -44,9 +44,6 @@ namespace GameKey { Dig, Use, - Inventory, - CreativeWindow, - Chat, Command, diff --git a/source/common/network/Network.cpp b/source/common/network/Network.cpp index 502a1952..b9b37e9d 100644 --- a/source/common/network/Network.cpp +++ b/source/common/network/Network.cpp @@ -48,8 +48,6 @@ std::string Network::commandToString(Network::Command command) { {Network::Command::PlayerPosUpdate, "PlayerPosUpdate"}, {Network::Command::PlayerRotUpdate, "PlayerRotUpdate"}, {Network::Command::PlayerSpawn, "PlayerSpawn"}, - {Network::Command::PlayerInventory, "PlayerInventory"}, - {Network::Command::PlayerCreativeWindow, "PlayerCreativeWindow"}, {Network::Command::PlayerChangeDimension, "PlayerChangeDimension"}, {Network::Command::PlayerHeldItemChanged, "PlayerHeldItemChanged"}, @@ -69,6 +67,8 @@ std::string Network::commandToString(Network::Command command) { {Network::Command::EntityRotation, "EntityRotation"}, {Network::Command::EntityAnimation, "EntityRotation"}, {Network::Command::EntityDrawableDef, "EntityDrawableDef"}, + + {Network::Command::KeyPressed, "KeyPressed"}, }; return commandNames[command]; diff --git a/source/common/network/Network.hpp b/source/common/network/Network.hpp index fcf82905..6fba6516 100644 --- a/source/common/network/Network.hpp +++ b/source/common/network/Network.hpp @@ -53,31 +53,32 @@ namespace Network { PlayerPosUpdate = 0x0a, // [NetworkCommand][u16 client id][s32 x, y, z][bool isTeleportation] (both) // FIXME PlayerRotUpdate = 0x0b, // [NetworkCommand][u16 client id][float yaw][float pitch] (from Client only) PlayerSpawn = 0x0c, // [NetworkCommand][u16 client id][s32 x, y, z] (from Server only) - PlayerInventory = 0x0d, // [NetworkCommand][u16 screenWidth, screenHeight][u8 guiScale] (from Client only) - PlayerCreativeWindow = 0x0e, // [NetworkCommand][u16 screenWidth, screenHeight][u8 guiScale] (from Client only) - PlayerChangeDimension = 0x0f, // [NetworkCommand][u16 client id][s32 x, y, z][u16 dimension] (from Server only) - PlayerHeldItemChanged = 0x10, // [NetworkCommand][u8 hotbar slot][u16 item id (to check match with server)] (from Client only) + PlayerChangeDimension = 0x0d, // [NetworkCommand][u16 client id][s32 x, y, z][u16 dimension] (from Server only) + PlayerHeldItemChanged = 0x0e, // [NetworkCommand][u8 hotbar slot][u16 item id (to check match with server)] (from Client only) // Block commands - BlockUpdate = 0x11, // [NetworkCommand][s32 x, y, z][u32 block] (from Server only) - BlockActivated = 0x12, // [NetworkCommand][s32 x, y, z][u16 screenWidth, screenHeight][u8 guiScale] (from Client only) - BlockGUIData = 0x13, // [NetworkCommand][LuaGUIData data] (from Server only) - BlockInvUpdate = 0x14, // [NetworkCommand][s32 x, y, z][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME] - BlockDataUpdate = 0x15, // [NetworkCommand][s32 x, y, z][u64 data] (both) [FIXME] + BlockUpdate = 0x0f, // [NetworkCommand][s32 x, y, z][u32 block] (from Server only) + BlockActivated = 0x10, // [NetworkCommand][s32 x, y, z][u16 screenWidth, screenHeight][u8 guiScale] (from Client only) + BlockGUIData = 0x11, // [NetworkCommand][LuaGUIData data] (from Server only) + BlockInvUpdate = 0x12, // [NetworkCommand][s32 x, y, z][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME] + BlockDataUpdate = 0x13, // [NetworkCommand][s32 x, y, z][u64 data] (both) [FIXME] // Registry commands - RegistryData = 0x16, // [NetworkCommand][Block block] (from Server only) + RegistryData = 0x14, // [NetworkCommand][Block block] (from Server only) // Chat commands - ChatMessage = 0x17, // [NetworkCommand][u16 client id][std::string message] (both) + ChatMessage = 0x15, // [NetworkCommand][u16 client id][std::string message] (both) // Entity commands - EntitySpawn = 0x18, // [NetworkCommand][u32 entity id] (from Server only) - EntityDespawn = 0x19, // [NetworkCommand][u32 entity id] (from Server only) - EntityPosition = 0x1a, // [NetworkCommand][u32 entity id][double x, double y, double z] (from Server only) - EntityRotation = 0x1b, // [NetworkCommand][u32 entity id][float w, float x, float y, float z] (from Server only) - EntityAnimation = 0x1c, // [NetworkCommand][u32 entity id][AnimationComponent anim] (from Server only) - EntityDrawableDef = 0x1d, // [NetworkCommand][u32 entity id][DrawableDef def] (from Server only) + EntitySpawn = 0x16, // [NetworkCommand][u32 entity id] (from Server only) + EntityDespawn = 0x17, // [NetworkCommand][u32 entity id] (from Server only) + EntityPosition = 0x18, // [NetworkCommand][u32 entity id][double x, double y, double z] (from Server only) + EntityRotation = 0x19, // [NetworkCommand][u32 entity id][float w, float x, float y, float z] (from Server only) + EntityAnimation = 0x1a, // [NetworkCommand][u32 entity id][AnimationComponent anim] (from Server only) + EntityDrawableDef = 0x1b, // [NetworkCommand][u32 entity id][DrawableDef def] (from Server only) + + // Key commands + KeyPressed = 0x1c, // [NetworkCommand][u16 key id][u16 screenWidth, screenHeight][u8 guiScale] (from Client only) }; std::string commandToString(Command command); diff --git a/source/common/world/Key.hpp b/source/common/world/Key.hpp index 88f33201..f24bb9d5 100644 --- a/source/common/world/Key.hpp +++ b/source/common/world/Key.hpp @@ -30,6 +30,7 @@ #include #include +#include #include @@ -42,17 +43,24 @@ class Key : public ISerializable { Key(u16 id, const std::string &stringID, const std::string &name) : m_id(id), m_stringID(stringID), m_name(name) {} - void serialize(sf::Packet &packet) const override { packet << m_id << m_stringID << m_name << m_defaultKey; } - void deserialize(sf::Packet &packet) override { packet >> m_id >> m_stringID >> m_name >> m_defaultKey; } + void serialize(sf::Packet &packet) const override { + packet << m_id << m_stringID << m_name << m_defaultKey; + } + + void deserialize(sf::Packet &packet) override { + packet >> m_id >> m_stringID >> m_name >> m_defaultKey; + m_defaultKeyCode = gk::KeyboardUtils::getKeyFromName(m_defaultKey); + } u16 id() const { return m_id; } const std::string &stringID() const { return m_stringID; } const std::string &name() const { return m_name; } - const std::string &defaultKey() const { return m_defaultKey; } + sf::Keyboard::Key defaultKeyCode() const { return m_defaultKeyCode; } void setDefaultKey(const std::string &defaultKey) { m_defaultKey = defaultKey; } + const sol::unsafe_function &callback() const { return m_callback; } void setCallback(const sol::unsafe_function &callback) { m_callback = callback; } private: @@ -62,6 +70,7 @@ class Key : public ISerializable { std::string m_name; std::string m_defaultKey; + sf::Keyboard::Key m_defaultKeyCode = sf::Keyboard::Unknown; sol::unsafe_function m_callback; }; diff --git a/source/server/network/ServerCommandHandler.cpp b/source/server/network/ServerCommandHandler.cpp index 6f31eb23..7c9a3c9f 100644 --- a/source/server/network/ServerCommandHandler.cpp +++ b/source/server/network/ServerCommandHandler.cpp @@ -296,36 +296,6 @@ void ServerCommandHandler::setupCallbacks() { gkError() << ("Failed to dig block using player " + std::to_string(client.id) + ": Player not found").c_str(); }); - m_server.setCommandCallback(Network::Command::PlayerInventory, [this](ClientInfo &client, Network::Packet &packet) { - u16 screenWidth, screenHeight; - u8 guiScale; - packet >> screenWidth >> screenHeight >> guiScale; - - sol::unsafe_function func = m_scriptEngine.lua()["show_inventory"]; - - try { - func(client, screenWidth, screenHeight, guiScale); - } - catch (const sol::error &error) { - gkError() << "Failed to send inventory GUI: " << error.what(); - } - }); - - m_server.setCommandCallback(Network::Command::PlayerCreativeWindow, [this](ClientInfo &client, Network::Packet &packet) { - u16 screenWidth, screenHeight; - u8 guiScale; - packet >> screenWidth >> screenHeight >> guiScale; - - sol::unsafe_function func = m_scriptEngine.lua()["show_creative_window"]; - - try { - func(client, screenWidth, screenHeight, guiScale); - } - catch (const sol::error &error) { - gkError() << "Failed to send creative window GUI: " << error.what(); - } - }); - m_server.setCommandCallback(Network::Command::PlayerHeldItemChanged, [this](ClientInfo &client, Network::Packet &packet) { ServerPlayer *player = m_players.getPlayer(client.id); if (player) { @@ -398,6 +368,14 @@ void ServerCommandHandler::setupCallbacks() { m_chatCommandHandler.parseCommand(message.substr(1), client); } }); + + m_server.setCommandCallback(Network::Command::KeyPressed, [this](ClientInfo &client, Network::Packet &packet) { + u16 keyID, screenWidth, screenHeight; + u8 guiScale; + packet >> keyID >> screenWidth >> screenHeight >> guiScale; + + m_registry.getKey(keyID).callback()(client, screenWidth, screenHeight, guiScale); + }); } void ServerCommandHandler::setPlayerPosition(u16 clientID, s32 x, s32 y, s32 z) {