Skip to content

Commit 54c81a3

Browse files
committed
[Lua API] Key definition: OK. Can't be remapped yet.
1 parent 03f1d05 commit 54c81a3

File tree

13 files changed

+183
-196
lines changed

13 files changed

+183
-196
lines changed

docs/lua-api-key.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,25 @@ mod:key {
88
name = "Inventory",
99
default_key = "E"
1010

11-
key_callback = function(client, screen_width, screen_height, gui_scale)
11+
callback = function(client, screen_width, screen_height, gui_scale)
1212
show_inventory(client, screen_width, screen_height, gui_scale)
1313
end
1414
}
1515
```
1616

1717
## Attributes
1818

19+
### `callback`
20+
21+
Function called when the key is pressed.
22+
23+
Example:
24+
```lua
25+
callback = function(client, screen_width, screen_height, gui_scale)
26+
show_inventory(client, screen_width, screen_height, gui_scale)
27+
end
28+
```
29+
1930
### `default_key`
2031

2132
Keyboard key mapped to this key by default.
@@ -25,6 +36,8 @@ Example:
2536
default_key = "E"
2637
```
2738

39+
Names are defined [here](https://github.com/Unarelith/GameKit/blob/master/source/core/input/KeyboardUtils.cpp).
40+
2841
### `name`
2942

3043
Name of the key. Optional field, uses `id` if not defined.

mods/creative_inventory/init.lua

+63-57
Original file line numberDiff line numberDiff line change
@@ -26,81 +26,87 @@
2626
--
2727
mod = openminer.mod_loader:register_mod("creative_inventory")
2828

29-
function show_creative_window(client, screen_width, screen_height, gui_scale)
30-
items = {}
31-
for k, v in pairs(openminer.registry:items()) do
32-
if k ~= 1 and not v:has_group("group:ci_ignore") then
33-
items[#items + 1] = {v:string_id()}
29+
mod:key {
30+
id = "creative_inventory",
31+
name = "Creative window",
32+
default_key = "H",
33+
34+
callback = function(client, screen_width, screen_height, gui_scale)
35+
items = {}
36+
for k, v in pairs(openminer.registry:items()) do
37+
if k ~= 1 and not v:has_group("group:ci_ignore") then
38+
items[#items + 1] = {v:string_id()}
39+
end
3440
end
35-
end
3641

37-
local gui = LuaGUI.new()
42+
local gui = LuaGUI.new()
3843

39-
gui:set_size(195, 136)
40-
gui:set_centered(true)
44+
gui:set_size(195, 136)
45+
gui:set_centered(true)
4146

42-
gui:image {
43-
name = "img_background",
44-
pos = {x = 0, y = 0},
47+
gui:image {
48+
name = "img_background",
49+
pos = {x = 0, y = 0},
4550

46-
texture = mod:path() .. "/textures/gui/creative_window.png",
47-
clip = {x = 0, y = 0, width = 195, height = 136},
48-
}
51+
texture = mod:path() .. "/textures/gui/creative_window.png",
52+
clip = {x = 0, y = 0, width = 195, height = 136},
53+
}
4954

50-
gui:inventory_data {
51-
name = "inv_data",
55+
gui:inventory_data {
56+
name = "inv_data",
5257

53-
width = 9,
54-
height = 7,
58+
width = 9,
59+
height = 7,
5560

56-
items = items,
61+
items = items,
5762

58-
is_unlimited = true,
59-
}
63+
is_unlimited = true,
64+
}
6065

61-
gui:inventory {
62-
name = "inv_creative_items",
63-
pos = {x = 8, y = 17},
66+
gui:inventory {
67+
name = "inv_creative_items",
68+
pos = {x = 8, y = 17},
6469

65-
inventory = {
66-
source = "temp",
67-
inventory_name = "inv_data",
68-
offset = 0,
69-
count = 9 * 5,
70-
},
70+
inventory = {
71+
source = "temp",
72+
inventory_name = "inv_data",
73+
offset = 0,
74+
count = 9 * 5,
75+
},
7176

72-
size = {x = 9, y = 7}
73-
}
77+
size = {x = 9, y = 7}
78+
}
7479

75-
gui:scroll_bar {
76-
name = "scroll_bar",
77-
pos = {x = 175, y = 18},
80+
gui:scroll_bar {
81+
name = "scroll_bar",
82+
pos = {x = 175, y = 18},
7883

79-
texture = mod:path() .. "/textures/gui/tabs.png",
80-
clip = {x = 232, y = 0, width = 12, height = 15},
81-
clip_selected = {x = 244, y = 0, width = 12, height = 15},
84+
texture = mod:path() .. "/textures/gui/tabs.png",
85+
clip = {x = 232, y = 0, width = 12, height = 15},
86+
clip_selected = {x = 244, y = 0, width = 12, height = 15},
8287

83-
widget = "inv_creative_items",
88+
widget = "inv_creative_items",
8489

85-
min_y = 0,
86-
max_y = 110 - 15,
87-
}
90+
min_y = 0,
91+
max_y = 110 - 15,
92+
}
8893

89-
gui:inventory {
90-
name = "inv_hotbar",
91-
pos = {x = 8, y = 111},
94+
gui:inventory {
95+
name = "inv_hotbar",
96+
pos = {x = 8, y = 111},
9297

93-
inventory = {
94-
source = "player",
95-
player = "player",
96-
inventory_name = "main",
97-
offset = 0,
98-
count = 9,
99-
},
98+
inventory = {
99+
source = "player",
100+
player = "player",
101+
inventory_name = "main",
102+
offset = 0,
103+
count = 9,
104+
},
100105

101-
size = {x = 9, y = 1},
102-
}
106+
size = {x = 9, y = 1},
107+
}
103108

104-
gui:show(client);
105-
end
109+
gui:show(client);
110+
end
111+
}
106112

mods/default/inventory.lua

+51-55
Original file line numberDiff line numberDiff line change
@@ -26,77 +26,73 @@
2626
--
2727
local modpath = mod:path()
2828

29-
function show_inventory(client, screen_width, screen_height, gui_scale)
30-
local gui = LuaGUI.new()
31-
32-
gui:set_size(176, 166)
33-
gui:set_centered(true)
29+
mod:key {
30+
id = "inventory",
31+
name = "Inventory",
32+
default_key = "E",
3433

35-
gui:image {
36-
name = "img_background",
37-
pos = {x = 0, y = 0},
34+
callback = function(client, screen_width, screen_height, gui_scale)
35+
local gui = LuaGUI.new()
3836

39-
texture = modpath .. "/textures/gui/inventory.png",
40-
clip = {x = 0, y = 0, width = 176, height = 166},
41-
}
37+
gui:set_size(176, 166)
38+
gui:set_centered(true)
4239

43-
gui:inventory {
44-
name = "inv_main",
45-
pos = {x = 7, y = 83},
40+
gui:image {
41+
name = "img_background",
42+
pos = {x = 0, y = 0},
4643

47-
inventory = {
48-
source = "player",
49-
player = "player",
50-
inventory_name = "main",
51-
offset = 9,
52-
count = 9 * 3,
53-
},
44+
texture = modpath .. "/textures/gui/inventory.png",
45+
clip = {x = 0, y = 0, width = 176, height = 166},
46+
}
5447

55-
size = {x = 9, y = 3},
48+
gui:inventory {
49+
name = "inv_main",
50+
pos = {x = 7, y = 83},
5651

57-
shift_destination = "inv_hotbar,inv_main",
58-
}
52+
inventory = {
53+
source = "player",
54+
player = "player",
55+
inventory_name = "main",
56+
offset = 9,
57+
count = 9 * 3,
58+
},
5959

60-
gui:inventory {
61-
name = "inv_hotbar",
62-
pos = {x = 7, y = 141},
60+
size = {x = 9, y = 3},
6361

64-
inventory = {
65-
source = "player",
66-
player = "player",
67-
inventory_name = "main",
68-
offset = 0,
69-
count = 9,
70-
},
62+
shift_destination = "inv_hotbar,inv_main",
63+
}
7164

72-
size = {x = 9, y = 1},
65+
gui:inventory {
66+
name = "inv_hotbar",
67+
pos = {x = 7, y = 141},
7368

74-
shift_destination = "inv_main,inv_hotbar",
75-
}
69+
inventory = {
70+
source = "player",
71+
player = "player",
72+
inventory_name = "main",
73+
offset = 0,
74+
count = 9,
75+
},
7676

77-
gui:crafting {
78-
name = "inv_crafting",
79-
pos = {x = 97, y = 17},
80-
result_pos = {x = 97 + 56, y = 17 + 10},
77+
size = {x = 9, y = 1},
8178

82-
inventory = {
83-
source = "temp",
84-
size = 2,
85-
},
79+
shift_destination = "inv_main,inv_hotbar",
80+
}
8681

87-
shift_destination = "inv_main,inv_hotbar",
88-
}
82+
gui:crafting {
83+
name = "inv_crafting",
84+
pos = {x = 97, y = 17},
85+
result_pos = {x = 97 + 56, y = 17 + 10},
8986

90-
gui:show(client)
91-
end
87+
inventory = {
88+
source = "temp",
89+
size = 2,
90+
},
9291

93-
mod:key {
94-
id = "inventory",
95-
name = "Inventory",
96-
default_key = "E",
92+
shift_destination = "inv_main,inv_hotbar",
93+
}
9794

98-
key_callback = function(client, screen_width, screen_height, gui_scale)
99-
show_inventory(client, screen_width, screen_height, gui_scale)
95+
gui:show(client)
10096
end
10197
}
10298

source/client/core/KeyboardHandler.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ KeyboardHandler::KeyboardHandler() {
4848
addKey(GameKey::Dig, "Dig", sf::Keyboard::L);
4949
addKey(GameKey::Use, "Use", sf::Keyboard::M);
5050

51-
addKey(GameKey::Inventory, "Inventory", sf::Keyboard::E);
52-
addKey(GameKey::CreativeWindow, "CreativeWindow", sf::Keyboard::H);
53-
5451
addKey(GameKey::Chat, "Chat", sf::Keyboard::T);
5552
addKey(GameKey::Command, "Command", sf::Keyboard::Divide);
5653

source/client/network/ClientCommandHandler.cpp

+7-14
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,6 @@ void ClientCommandHandler::sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block)
8989
m_client.send(packet);
9090
}
9191

92-
void ClientCommandHandler::sendPlayerInventoryRequest() {
93-
Network::Packet packet;
94-
packet << Network::Command::PlayerInventory
95-
<< u16(Config::screenWidth) << u16(Config::screenHeight) << u8(Config::guiScale);
96-
m_client.send(packet);
97-
}
98-
99-
void ClientCommandHandler::sendPlayerCreativeWindowRequest() {
100-
Network::Packet packet;
101-
packet << Network::Command::PlayerCreativeWindow
102-
<< u16(Config::screenWidth) << u16(Config::screenHeight) << u8(Config::guiScale);
103-
m_client.send(packet);
104-
}
105-
10692
void ClientCommandHandler::sendPlayerHeldItemChanged(u8 hotbarSlot, u16 itemID) {
10793
Network::Packet packet;
10894
packet << Network::Command::PlayerHeldItemChanged
@@ -144,6 +130,13 @@ void ClientCommandHandler::sendChatMessage(const std::string &message) {
144130
m_client.send(packet);
145131
}
146132

133+
void ClientCommandHandler::sendKeyPressed(u16 keyID) {
134+
Network::Packet packet;
135+
packet << Network::Command::KeyPressed << keyID
136+
<< Config::screenWidth << Config::screenHeight << Config::guiScale;
137+
m_client.send(packet);
138+
}
139+
147140
template<typename ComponentType>
148141
static void addComponentCommandCallback(Network::Command command, Client &client, ClientCommandHandler::EntityMap &entityMap, ClientWorld &world) {
149142
client.setCommandCallback(command, [&](Network::Packet &packet) {

source/client/network/ClientCommandHandler.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ class ClientCommandHandler {
5151
void sendPlayerRotUpdate();
5252
void sendPlayerDigBlock(const glm::ivec4 &selectedBlock);
5353
void sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block);
54-
void sendPlayerInventoryRequest();
55-
void sendPlayerCreativeWindowRequest();
5654
void sendPlayerHeldItemChanged(u8 hotbarSlot, u16 itemID);
5755
void sendBlockActivated(const glm::ivec4 &selectedBlock);
5856
void sendBlockInvUpdate(Inventory &inventory);
5957
void sendChunkRequest(s32 chunkX, s32 chunkY, s32 chunkZ);
6058
void sendChatMessage(const std::string &message);
59+
void sendKeyPressed(u16 keyID);
6160

6261
void setupCallbacks();
6362

source/client/states/GameState.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ void GameState::onEvent(const sf::Event &event) {
118118
gk::Mouse::setCursorGrabbed(true);
119119
gk::Mouse::setCursorVisible(false);
120120
}
121+
else if (event.type == sf::Event::KeyPressed) {
122+
for (auto &key : m_registry.keys()) {
123+
if (event.key.code == key.defaultKeyCode()) {
124+
m_clientCommandHandler.sendKeyPressed(key.id());
125+
}
126+
}
127+
}
121128

122129
m_hud.onEvent(event);
123130
}
@@ -140,13 +147,6 @@ void GameState::update() {
140147

141148
if (!m_stateStack->empty() && &m_stateStack->top() == this) {
142149
m_player.processInputs();
143-
144-
if (gk::GamePad::isKeyPressedOnce(GameKey::Inventory)) {
145-
m_clientCommandHandler.sendPlayerInventoryRequest();
146-
}
147-
else if (gk::GamePad::isKeyPressedOnce(GameKey::CreativeWindow)) {
148-
m_clientCommandHandler.sendPlayerCreativeWindowRequest();
149-
}
150150
}
151151

152152
m_player.updatePosition(m_world);

0 commit comments

Comments
 (0)