[Lua API] Key definition: OK. Can't be remapped yet.

This commit is contained in:
Quentin Bazin 2020-06-19 04:16:38 +02:00
parent ea38e6d397
commit fc3fafc1a8
13 changed files with 212 additions and 225 deletions

View File

@ -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.

View File

@ -26,7 +26,12 @@
--
mod = openminer.mod_loader:register_mod("creative_inventory")
function show_creative_window(client, screen_width, screen_height, gui_scale)
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
@ -102,5 +107,6 @@ function show_creative_window(client, screen_width, screen_height, gui_scale)
}
gui:show(client);
end
end
}

View File

@ -26,7 +26,12 @@
--
local modpath = mod:path()
function show_inventory(client, screen_width, screen_height, gui_scale)
mod:key {
id = "inventory",
name = "Inventory",
default_key = "E",
callback = function(client, screen_width, screen_height, gui_scale)
local gui = LuaGUI.new()
gui:set_size(176, 166)
@ -88,15 +93,6 @@ function show_inventory(client, screen_width, screen_height, gui_scale)
}
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)
end
}

View File

@ -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);

View File

@ -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<typename ComponentType>
static void addComponentCommandCallback(Network::Command command, Client &client, ClientCommandHandler::EntityMap &entityMap, ClientWorld &world) {
client.setCommandCallback(command, [&](Network::Packet &packet) {

View File

@ -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();

View File

@ -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);

View File

@ -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"},
};

View File

@ -44,9 +44,6 @@ namespace GameKey {
Dig,
Use,
Inventory,
CreativeWindow,
Chat,
Command,

View File

@ -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];

View File

@ -53,31 +53,32 @@ namespace Network {
PlayerPosUpdate = 0x0a, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z][bool isTeleportation] (both) // FIXME
PlayerRotUpdate = 0x0b, // <TCP> [NetworkCommand][u16 client id][float yaw][float pitch] (from Client only)
PlayerSpawn = 0x0c, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (from Server only)
PlayerInventory = 0x0d, // <TCP> [NetworkCommand][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
PlayerCreativeWindow = 0x0e, // <TCP> [NetworkCommand][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
PlayerChangeDimension = 0x0f, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z][u16 dimension] (from Server only)
PlayerHeldItemChanged = 0x10, // <TCP> [NetworkCommand][u8 hotbar slot][u16 item id (to check match with server)] (from Client only)
PlayerChangeDimension = 0x0d, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z][u16 dimension] (from Server only)
PlayerHeldItemChanged = 0x0e, // <TCP> [NetworkCommand][u8 hotbar slot][u16 item id (to check match with server)] (from Client only)
// Block commands
BlockUpdate = 0x11, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Server only)
BlockActivated = 0x12, // <TCP> [NetworkCommand][s32 x, y, z][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
BlockGUIData = 0x13, // <TCP> [NetworkCommand][LuaGUIData data] (from Server only)
BlockInvUpdate = 0x14, // <TCP> [NetworkCommand][s32 x, y, z][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
BlockDataUpdate = 0x15, // <TCP> [NetworkCommand][s32 x, y, z][u64 data] (both) [FIXME]
BlockUpdate = 0x0f, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Server only)
BlockActivated = 0x10, // <TCP> [NetworkCommand][s32 x, y, z][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
BlockGUIData = 0x11, // <TCP> [NetworkCommand][LuaGUIData data] (from Server only)
BlockInvUpdate = 0x12, // <TCP> [NetworkCommand][s32 x, y, z][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
BlockDataUpdate = 0x13, // <TCP> [NetworkCommand][s32 x, y, z][u64 data] (both) [FIXME]
// Registry commands
RegistryData = 0x16, // <TCP> [NetworkCommand][Block block] (from Server only)
RegistryData = 0x14, // <TCP> [NetworkCommand][Block block] (from Server only)
// Chat commands
ChatMessage = 0x17, // <TCP> [NetworkCommand][u16 client id][std::string message] (both)
ChatMessage = 0x15, // <TCP> [NetworkCommand][u16 client id][std::string message] (both)
// Entity commands
EntitySpawn = 0x18, // <TCP> [NetworkCommand][u32 entity id] (from Server only)
EntityDespawn = 0x19, // <TCP> [NetworkCommand][u32 entity id] (from Server only)
EntityPosition = 0x1a, // <TCP> [NetworkCommand][u32 entity id][double x, double y, double z] (from Server only)
EntityRotation = 0x1b, // <TCP> [NetworkCommand][u32 entity id][float w, float x, float y, float z] (from Server only)
EntityAnimation = 0x1c, // <TCP> [NetworkCommand][u32 entity id][AnimationComponent anim] (from Server only)
EntityDrawableDef = 0x1d, // <TCP> [NetworkCommand][u32 entity id][DrawableDef def] (from Server only)
EntitySpawn = 0x16, // <TCP> [NetworkCommand][u32 entity id] (from Server only)
EntityDespawn = 0x17, // <TCP> [NetworkCommand][u32 entity id] (from Server only)
EntityPosition = 0x18, // <TCP> [NetworkCommand][u32 entity id][double x, double y, double z] (from Server only)
EntityRotation = 0x19, // <TCP> [NetworkCommand][u32 entity id][float w, float x, float y, float z] (from Server only)
EntityAnimation = 0x1a, // <TCP> [NetworkCommand][u32 entity id][AnimationComponent anim] (from Server only)
EntityDrawableDef = 0x1b, // <TCP> [NetworkCommand][u32 entity id][DrawableDef def] (from Server only)
// Key commands
KeyPressed = 0x1c, // <TCP> [NetworkCommand][u16 key id][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
};
std::string commandToString(Command command);

View File

@ -30,6 +30,7 @@
#include <string>
#include <gk/core/IntTypes.hpp>
#include <gk/core/input/KeyboardUtils.hpp>
#include <sol/sol.hpp>
@ -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;
};

View File

@ -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) {