refactor: add GUI instance reference to UI nodes
This commit is contained in:
@@ -38,7 +38,7 @@ void EngineController::deleteWorld(const std::string& name) {
|
||||
logger.info() << "deleting " << folder.string();
|
||||
io::remove_all(folder);
|
||||
if (!engine.isHeadless()) {
|
||||
engine.getGUI()->getMenu()->back();
|
||||
engine.getGUI().getMenu()->back();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -109,7 +109,7 @@ static void show_convert_request(
|
||||
text += util::str2wstr_utf8(line) + L"\n";
|
||||
}
|
||||
guiutil::confirm_with_memo(
|
||||
engine.getGUI()->getMenu(),
|
||||
engine,
|
||||
langs::get(message),
|
||||
text,
|
||||
on_confirm,
|
||||
|
||||
@@ -4,9 +4,10 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include "BlocksController.hpp"
|
||||
#include "content/Content.hpp"
|
||||
#include "core_defs.hpp"
|
||||
#include "settings.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "items/Inventory.hpp"
|
||||
#include "items/ItemDef.hpp"
|
||||
#include "items/ItemStack.hpp"
|
||||
@@ -16,17 +17,15 @@
|
||||
#include "objects/Players.hpp"
|
||||
#include "physics/Hitbox.hpp"
|
||||
#include "physics/PhysicsSolver.hpp"
|
||||
#include "scripting/scripting.hpp"
|
||||
#include "settings.hpp"
|
||||
#include "voxels/Block.hpp"
|
||||
#include "voxels/Chunks.hpp"
|
||||
#include "voxels/voxel.hpp"
|
||||
#include "window/Camera.hpp"
|
||||
#include "window/Events.hpp"
|
||||
#include "window/Window.hpp"
|
||||
#include "window/input.hpp"
|
||||
#include "world/Level.hpp"
|
||||
#include "BlocksController.hpp"
|
||||
#include "scripting/scripting.hpp"
|
||||
|
||||
const float INTERACTION_RELOAD = 0.160f;
|
||||
const float STEPS_SPEED = 2.2f;
|
||||
@@ -40,9 +39,7 @@ const float RUN_ZOOM = 1.1f;
|
||||
const float C_ZOOM = 0.1f;
|
||||
const float CROUCH_SHIFT_Y = -0.2f;
|
||||
|
||||
CameraControl::CameraControl(
|
||||
Player& player, const CameraSettings& settings
|
||||
)
|
||||
CameraControl::CameraControl(Player& player, const CameraSettings& settings)
|
||||
: player(player),
|
||||
camera(player.fpCamera),
|
||||
settings(settings),
|
||||
@@ -70,7 +67,9 @@ void CameraControl::updateMouse(PlayerInput& input) {
|
||||
(input.zoom ? settings.sensitivity.get() / 4.f
|
||||
: settings.sensitivity.get());
|
||||
|
||||
auto d = glm::degrees(Events::delta / (float)Window::height * sensitivity);
|
||||
auto d = glm::degrees(
|
||||
input.delta / static_cast<float>(Window::height) * sensitivity
|
||||
);
|
||||
rotation.x -= d.x;
|
||||
rotation.y -= d.y;
|
||||
|
||||
@@ -147,15 +146,16 @@ void CameraControl::updateFovEffects(
|
||||
// more extensible but uglier
|
||||
void CameraControl::switchCamera() {
|
||||
const std::vector<std::shared_ptr<Camera>> playerCameras {
|
||||
camera, player.tpCamera, player.spCamera
|
||||
};
|
||||
camera, player.tpCamera, player.spCamera};
|
||||
|
||||
auto index = std::distance(
|
||||
playerCameras.begin(),
|
||||
std::find_if(
|
||||
playerCameras.begin(),
|
||||
playerCameras.end(),
|
||||
[this](auto& ptr) { return ptr.get() == player.currentCamera.get(); }
|
||||
[this](auto& ptr) {
|
||||
return ptr.get() == player.currentCamera.get();
|
||||
}
|
||||
)
|
||||
);
|
||||
if (static_cast<size_t>(index) != playerCameras.size()) {
|
||||
@@ -205,8 +205,8 @@ void CameraControl::update(
|
||||
tpCamera->front = camera->front;
|
||||
tpCamera->right = camera->right;
|
||||
}
|
||||
if (player.currentCamera == spCamera ||
|
||||
player.currentCamera == tpCamera || player.currentCamera == camera) {
|
||||
if (player.currentCamera == spCamera || player.currentCamera == tpCamera ||
|
||||
player.currentCamera == camera) {
|
||||
player.currentCamera->setFov(glm::radians(settings.fov.get()));
|
||||
}
|
||||
}
|
||||
@@ -239,10 +239,7 @@ void PlayerController::onFootstep(const Hitbox& hitbox) {
|
||||
continue;
|
||||
}
|
||||
blocksController.onBlockInteraction(
|
||||
&player,
|
||||
glm::ivec3(x, y, z),
|
||||
def,
|
||||
BlockInteraction::step
|
||||
&player, glm::ivec3(x, y, z), def, BlockInteraction::step
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -265,9 +262,9 @@ void PlayerController::updateFootsteps(float delta) {
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerController::update(float delta, bool input) {
|
||||
if (input) {
|
||||
updateKeyboard();
|
||||
void PlayerController::update(float delta, const Input* inputEvents) {
|
||||
if (inputEvents) {
|
||||
updateKeyboard(*inputEvents);
|
||||
player.updateSelectedEntity();
|
||||
} else {
|
||||
resetKeyboard();
|
||||
@@ -275,7 +272,7 @@ void PlayerController::update(float delta, bool input) {
|
||||
updatePlayer(delta);
|
||||
}
|
||||
|
||||
void PlayerController::postUpdate(float delta, bool input, bool pause) {
|
||||
void PlayerController::postUpdate(float delta, const Input* input, bool pause) {
|
||||
if (!pause) {
|
||||
updateFootsteps(delta);
|
||||
}
|
||||
@@ -287,23 +284,25 @@ void PlayerController::postUpdate(float delta, bool input, bool pause) {
|
||||
player.postUpdate();
|
||||
camControl.update(this->input, pause ? 0.0f : delta, *player.chunks);
|
||||
if (input) {
|
||||
updateInteraction(delta);
|
||||
updateInteraction(*input, delta);
|
||||
} else {
|
||||
player.selection = {};
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerController::updateKeyboard() {
|
||||
input.moveForward = Events::active(BIND_MOVE_FORWARD);
|
||||
input.moveBack = Events::active(BIND_MOVE_BACK);
|
||||
input.moveLeft = Events::active(BIND_MOVE_LEFT);
|
||||
input.moveRight = Events::active(BIND_MOVE_RIGHT);
|
||||
input.sprint = Events::active(BIND_MOVE_SPRINT);
|
||||
input.shift = Events::active(BIND_MOVE_CROUCH);
|
||||
input.cheat = Events::active(BIND_MOVE_CHEAT);
|
||||
input.jump = Events::active(BIND_MOVE_JUMP);
|
||||
input.zoom = Events::active(BIND_CAM_ZOOM);
|
||||
input.cameraMode = Events::jactive(BIND_CAM_MODE);
|
||||
void PlayerController::updateKeyboard(const Input& inputEvents) {
|
||||
const auto& bindings = inputEvents.getBindings();
|
||||
input.moveForward = bindings.active(BIND_MOVE_FORWARD);
|
||||
input.moveBack = bindings.active(BIND_MOVE_BACK);
|
||||
input.moveLeft = bindings.active(BIND_MOVE_LEFT);
|
||||
input.moveRight = bindings.active(BIND_MOVE_RIGHT);
|
||||
input.sprint = bindings.active(BIND_MOVE_SPRINT);
|
||||
input.shift = bindings.active(BIND_MOVE_CROUCH);
|
||||
input.cheat = bindings.active(BIND_MOVE_CHEAT);
|
||||
input.jump = bindings.active(BIND_MOVE_JUMP);
|
||||
input.zoom = bindings.active(BIND_CAM_ZOOM);
|
||||
input.cameraMode = bindings.jactive(BIND_CAM_MODE);
|
||||
input.delta = inputEvents.getCursor().delta;
|
||||
}
|
||||
|
||||
void PlayerController::resetKeyboard() {
|
||||
@@ -316,6 +315,7 @@ void PlayerController::resetKeyboard() {
|
||||
input.shift = false;
|
||||
input.cheat = false;
|
||||
input.jump = false;
|
||||
input.delta = {};
|
||||
}
|
||||
|
||||
void PlayerController::updatePlayer(float delta) {
|
||||
@@ -328,18 +328,12 @@ static int determine_rotation(
|
||||
if (def && def->rotatable) {
|
||||
const std::string& name = def->rotations.name;
|
||||
if (name == "pipe") {
|
||||
if (norm.x < 0.0f)
|
||||
return BLOCK_DIR_WEST;
|
||||
if (norm.x > 0.0f)
|
||||
return BLOCK_DIR_EAST;
|
||||
if (norm.y > 0.0f)
|
||||
return BLOCK_DIR_UP;
|
||||
if (norm.y < 0.0f)
|
||||
return BLOCK_DIR_DOWN;
|
||||
if (norm.z > 0.0f)
|
||||
return BLOCK_DIR_NORTH;
|
||||
if (norm.z < 0.0f)
|
||||
return BLOCK_DIR_SOUTH;
|
||||
if (norm.x < 0.0f) return BLOCK_DIR_WEST;
|
||||
if (norm.x > 0.0f) return BLOCK_DIR_EAST;
|
||||
if (norm.y > 0.0f) return BLOCK_DIR_UP;
|
||||
if (norm.y < 0.0f) return BLOCK_DIR_DOWN;
|
||||
if (norm.z > 0.0f) return BLOCK_DIR_NORTH;
|
||||
if (norm.z < 0.0f) return BLOCK_DIR_SOUTH;
|
||||
} else if (name == "pane") {
|
||||
if (abs(camDir.x) > abs(camDir.z)) {
|
||||
if (camDir.x > 0.0f) return BLOCK_DIR_EAST;
|
||||
@@ -417,7 +411,9 @@ voxel* PlayerController::updateSelection(float maxDistance) {
|
||||
return vox;
|
||||
}
|
||||
|
||||
void PlayerController::processRightClick(const Block& def, const Block& target) {
|
||||
void PlayerController::processRightClick(
|
||||
const Block& def, const Block& target
|
||||
) {
|
||||
const auto& selection = player.selection;
|
||||
auto& chunks = *player.chunks;
|
||||
auto camera = player.fpCamera.get();
|
||||
@@ -427,8 +423,8 @@ void PlayerController::processRightClick(const Block& def, const Block& target)
|
||||
|
||||
if (!input.shift && target.rt.funcsset.oninteract) {
|
||||
if (scripting::on_block_interact(
|
||||
&player, target, selection.actualPosition
|
||||
)) {
|
||||
&player, target, selection.actualPosition
|
||||
)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -443,7 +439,8 @@ void PlayerController::processRightClick(const Block& def, const Block& target)
|
||||
if (def.obstacle) {
|
||||
const auto& hitboxes = def.rt.hitboxes[state.rotation];
|
||||
for (const AABB& blockAABB : hitboxes) {
|
||||
if (level.entities->hasBlockingInside(blockAABB.translated(coord))) {
|
||||
if (level.entities->hasBlockingInside(blockAABB.translated(coord)
|
||||
)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -466,7 +463,7 @@ void PlayerController::processRightClick(const Block& def, const Block& target)
|
||||
if (chosenBlock != vox->id && chosenBlock) {
|
||||
if (!player.isInfiniteItems()) {
|
||||
auto& slot = player.getInventory()->getSlot(player.getChosenSlot());
|
||||
slot.setCount(slot.getCount()-1);
|
||||
slot.setCount(slot.getCount() - 1);
|
||||
}
|
||||
blocksController.placeBlock(
|
||||
&player, def, state, coord.x, coord.y, coord.z
|
||||
@@ -490,21 +487,22 @@ void PlayerController::updateEntityInteraction(
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerController::updateInteraction(float delta) {
|
||||
void PlayerController::updateInteraction(const Input& inputEvents, float delta) {
|
||||
auto indices = level.content.getIndices();
|
||||
const auto& selection = player.selection;
|
||||
|
||||
|
||||
if (interactionTimer > 0.0f) {
|
||||
interactionTimer -= delta;
|
||||
}
|
||||
bool xkey = Events::active(BIND_PLAYER_FAST_INTERACTOIN);
|
||||
const auto& bindings = inputEvents.getBindings();
|
||||
bool xkey = bindings.active(BIND_PLAYER_FAST_INTERACTOIN);
|
||||
float maxDistance = xkey ? 200.0f : 10.0f;
|
||||
bool longInteraction = interactionTimer <= 0 || xkey;
|
||||
bool lclick = Events::jactive(BIND_PLAYER_DESTROY) ||
|
||||
(longInteraction && Events::active(BIND_PLAYER_DESTROY));
|
||||
bool lattack = Events::jactive(BIND_PLAYER_ATTACK);
|
||||
bool rclick = Events::jactive(BIND_PLAYER_BUILD) ||
|
||||
(longInteraction && Events::active(BIND_PLAYER_BUILD));
|
||||
bool lclick = bindings.jactive(BIND_PLAYER_DESTROY) ||
|
||||
(longInteraction && bindings.active(BIND_PLAYER_DESTROY));
|
||||
bool lattack = bindings.jactive(BIND_PLAYER_ATTACK);
|
||||
bool rclick = bindings.jactive(BIND_PLAYER_BUILD) ||
|
||||
(longInteraction && bindings.active(BIND_PLAYER_BUILD));
|
||||
if (lclick || rclick) {
|
||||
interactionTimer = INTERACTION_RELOAD;
|
||||
}
|
||||
@@ -527,8 +525,8 @@ void PlayerController::updateInteraction(float delta) {
|
||||
auto iend = selection.position;
|
||||
if (lclick && !input.shift && item.rt.funcsset.on_block_break_by) {
|
||||
if (scripting::on_item_break_block(
|
||||
&player, item, iend.x, iend.y, iend.z
|
||||
)) {
|
||||
&player, item, iend.x, iend.y, iend.z
|
||||
)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "objects/Player.hpp"
|
||||
#include "util/Clock.hpp"
|
||||
|
||||
class Input;
|
||||
class Engine;
|
||||
class Camera;
|
||||
class Level;
|
||||
@@ -54,11 +55,11 @@ class PlayerController {
|
||||
BlocksController& blocksController;
|
||||
float interactionTimer = 0.0f;
|
||||
|
||||
void updateKeyboard();
|
||||
void updateKeyboard(const Input& inputEvents);
|
||||
void resetKeyboard();
|
||||
void updatePlayer(float delta);
|
||||
void updateEntityInteraction(entityid_t eid, bool lclick, bool rclick);
|
||||
void updateInteraction(float delta);
|
||||
void updateInteraction(const Input& inputEvents, float delta);
|
||||
|
||||
float stepsTimer = 0.0f;
|
||||
void onFootstep(const Hitbox& hitbox);
|
||||
@@ -76,13 +77,13 @@ public:
|
||||
|
||||
/// @brief Called after blocks update if not paused
|
||||
/// @param delta delta time
|
||||
/// @param input process user input
|
||||
void update(float delta, bool input);
|
||||
/// @param inputEvents nullable window inputs
|
||||
void update(float delta, const Input* inputEvents);
|
||||
|
||||
/// @brief Called after whole level update
|
||||
/// @param delta delta time
|
||||
/// @param input process user input
|
||||
/// @param inputEvents nullable window inputs
|
||||
/// @param pause is game paused
|
||||
void postUpdate(float delta, bool input, bool pause);
|
||||
void postUpdate(float delta, const Input* inputEvents, bool pause);
|
||||
Player* getPlayer();
|
||||
};
|
||||
|
||||
@@ -14,7 +14,7 @@ static int l_add_command(lua::State* L) {
|
||||
lua::pushvalue(L, 3);
|
||||
auto func = lua::create_lambda(L);
|
||||
try {
|
||||
engine->getCommandsInterpreter()->getRepository()->add(
|
||||
engine->getCmd().getRepository()->add(
|
||||
scheme,
|
||||
description,
|
||||
[func](auto, auto args, auto kwargs) {
|
||||
@@ -32,7 +32,7 @@ static int l_add_command(lua::State* L) {
|
||||
static int l_execute(lua::State* L) {
|
||||
auto prompt = lua::require_string(L, 1);
|
||||
try {
|
||||
auto result = engine->getCommandsInterpreter()->execute(prompt);
|
||||
auto result = engine->getCmd().execute(prompt);
|
||||
lua::pushvalue(L, result);
|
||||
return 1;
|
||||
} catch (const parsing_error& err) {
|
||||
@@ -45,19 +45,18 @@ static int l_execute(lua::State* L) {
|
||||
|
||||
static int l_get(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
return lua::pushvalue(L, (*engine->getCommandsInterpreter())[name]);
|
||||
return lua::pushvalue(L, engine->getCmd()[name]);
|
||||
}
|
||||
|
||||
static int l_set(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
auto value = lua::tovalue(L, 2);
|
||||
(*engine->getCommandsInterpreter())[name] = value;
|
||||
engine->getCmd()[name] = value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_get_commands_list(lua::State* L) {
|
||||
auto interpreter = engine->getCommandsInterpreter();
|
||||
auto repo = interpreter->getRepository();
|
||||
auto repo = engine->getCmd().getRepository();
|
||||
const auto& commands = repo->getCommands();
|
||||
|
||||
lua::createtable(L, commands.size(), 0);
|
||||
@@ -71,8 +70,7 @@ static int l_get_commands_list(lua::State* L) {
|
||||
|
||||
static int l_get_command_info(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
auto interpreter = engine->getCommandsInterpreter();
|
||||
auto repo = interpreter->getRepository();
|
||||
auto repo = engine->getCmd().getRepository();
|
||||
auto command = repo->get(name);
|
||||
if (command == nullptr) {
|
||||
return 0;
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
#include "logic/LevelController.hpp"
|
||||
#include "util/listutil.hpp"
|
||||
#include "util/platform.hpp"
|
||||
#include "window/Events.hpp"
|
||||
#include "world/Level.hpp"
|
||||
#include "world/generator/WorldGenerator.hpp"
|
||||
|
||||
|
||||
@@ -80,8 +80,9 @@ static int l_container_add(lua::State* L) {
|
||||
}
|
||||
auto xmlsrc = lua::require_string(L, 2);
|
||||
try {
|
||||
auto subnode =
|
||||
guiutil::create(xmlsrc, docnode.document->getEnvironment());
|
||||
auto subnode = guiutil::create(
|
||||
engine->getGUI(), xmlsrc, docnode.document->getEnvironment()
|
||||
);
|
||||
node->add(subnode);
|
||||
UINode::getIndices(subnode, docnode.document->getMapWriteable());
|
||||
} catch (const std::exception& err) {
|
||||
@@ -93,7 +94,7 @@ static int l_container_add(lua::State* L) {
|
||||
static int l_node_destruct(lua::State* L) {
|
||||
auto docnode = get_document_node(L);
|
||||
auto node = docnode.node;
|
||||
engine->getGUI()->postRunnable([node]() {
|
||||
engine->getGUI().postRunnable([node]() {
|
||||
auto parent = node->getParent();
|
||||
if (auto container = dynamic_cast<Container*>(parent)) {
|
||||
container->remove(node.get());
|
||||
@@ -651,7 +652,7 @@ static void p_set_focused(
|
||||
const std::shared_ptr<UINode>& node, lua::State* L, int idx
|
||||
) {
|
||||
if (lua::toboolean(L, idx) && !node->isFocused()) {
|
||||
engine->getGUI()->setFocus(node);
|
||||
engine->getGUI().setFocus(node);
|
||||
} else if (node->isFocused()) {
|
||||
node->defocus();
|
||||
}
|
||||
@@ -782,7 +783,7 @@ static int l_gui_get_locales_info(lua::State* L) {
|
||||
}
|
||||
|
||||
static int l_gui_getviewport(lua::State* L) {
|
||||
return lua::pushvec2(L, engine->getGUI()->getContainer()->getSize());
|
||||
return lua::pushvec2(L, engine->getGUI().getContainer()->getSize());
|
||||
}
|
||||
|
||||
static int l_gui_clear_markup(lua::State* L) {
|
||||
@@ -845,6 +846,7 @@ static int l_gui_load_document(lua::State* L) {
|
||||
auto args = lua::tovalue(L, 3);
|
||||
|
||||
auto documentPtr = UiDocument::read(
|
||||
engine->getGUI(),
|
||||
scripting::get_root_environment(),
|
||||
alias,
|
||||
filename,
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
#include <filesystem>
|
||||
|
||||
#include "engine/Engine.hpp"
|
||||
#include "io/io.hpp"
|
||||
#include "frontend/hud.hpp"
|
||||
#include "frontend/screens/Screen.hpp"
|
||||
#include "graphics/ui/GUI.hpp"
|
||||
#include "graphics/ui/elements/Container.hpp"
|
||||
#include "io/io.hpp"
|
||||
#include "libgui.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
#include "window/Events.hpp"
|
||||
#include "window/input.hpp"
|
||||
#include "libgui.hpp"
|
||||
|
||||
namespace scripting {
|
||||
extern Hud* hud;
|
||||
@@ -37,26 +37,26 @@ static int l_add_callback(lua::State* L) {
|
||||
lua::pushvalue(L, 2);
|
||||
auto actual_callback = lua::create_simple_handler(L);
|
||||
observer_handler handler;
|
||||
|
||||
|
||||
auto& gui = engine->getGUI();
|
||||
auto& input = engine->getInput();
|
||||
|
||||
if (pos != std::string::npos) {
|
||||
std::string prefix = bindname.substr(0, pos);
|
||||
if (prefix == "key") {
|
||||
auto key = input_util::keycode_from(bindname.substr(pos + 1));
|
||||
handler = Events::addKeyCallback(key, actual_callback);
|
||||
handler = input.addKeyCallback(key, actual_callback);
|
||||
}
|
||||
}
|
||||
auto callback = [=]() -> bool {
|
||||
if (!scripting::engine->getGUI()->isFocusCaught()) {
|
||||
auto callback = [&gui, actual_callback]() -> bool {
|
||||
if (!gui.isFocusCaught()) {
|
||||
return actual_callback();
|
||||
}
|
||||
return false;
|
||||
};
|
||||
if (handler == nullptr) {
|
||||
const auto& bind = Events::bindings.find(bindname);
|
||||
if (bind == Events::bindings.end()) {
|
||||
throw std::runtime_error("unknown binding " + util::quote(bindname));
|
||||
}
|
||||
handler = bind->second.onactived.add(callback);
|
||||
auto& bind = input.requireBinding(bindname);
|
||||
handler = bind.onactived.add(callback);
|
||||
}
|
||||
|
||||
if (hud) {
|
||||
@@ -64,7 +64,8 @@ static int l_add_callback(lua::State* L) {
|
||||
return 0;
|
||||
} else if (lua::gettop(L) >= 3) {
|
||||
auto node = get_document_node(L, 3);
|
||||
if (auto container = std::dynamic_pointer_cast<gui::Container>(node.node)) {
|
||||
if (auto container =
|
||||
std::dynamic_pointer_cast<gui::Container>(node.node)) {
|
||||
container->keepAlive(handler);
|
||||
return 0;
|
||||
}
|
||||
@@ -74,11 +75,11 @@ static int l_add_callback(lua::State* L) {
|
||||
}
|
||||
|
||||
static int l_get_mouse_pos(lua::State* L) {
|
||||
return lua::pushvec2(L, Events::cursor);
|
||||
return lua::pushvec2(L, engine->getInput().getCursor().pos);
|
||||
}
|
||||
|
||||
static int l_get_bindings(lua::State* L) {
|
||||
auto& bindings = Events::bindings;
|
||||
const auto& bindings = engine->getInput().getBindings().getAll();
|
||||
lua::createtable(L, bindings.size(), 0);
|
||||
|
||||
int i = 0;
|
||||
@@ -92,25 +93,14 @@ static int l_get_bindings(lua::State* L) {
|
||||
|
||||
static int l_get_binding_text(lua::State* L) {
|
||||
auto bindname = lua::require_string(L, 1);
|
||||
auto index = Events::bindings.find(bindname);
|
||||
|
||||
if (index == Events::bindings.end()) {
|
||||
throw std::runtime_error("unknown binding " + util::quote(bindname));
|
||||
lua::pushstring(L, "");
|
||||
} else {
|
||||
lua::pushstring(L, index->second.text());
|
||||
}
|
||||
|
||||
return 1;
|
||||
const auto& bind = engine->getInput().requireBinding(bindname);
|
||||
return lua::pushstring(L, bind.text());
|
||||
}
|
||||
|
||||
static int l_is_active(lua::State* L) {
|
||||
auto bindname = lua::require_string(L, 1);
|
||||
const auto& bind = Events::bindings.find(bindname);
|
||||
if (bind == Events::bindings.end()) {
|
||||
throw std::runtime_error("unknown binding " + util::quote(bindname));
|
||||
}
|
||||
return lua::pushboolean(L, bind->second.active());
|
||||
auto& bind = engine->getInput().requireBinding(bindname);
|
||||
return lua::pushboolean(L, bind.active());
|
||||
}
|
||||
|
||||
static int l_is_pressed(lua::State* L) {
|
||||
@@ -123,12 +113,11 @@ static int l_is_pressed(lua::State* L) {
|
||||
auto name = code.substr(sep + 1);
|
||||
if (prefix == "key") {
|
||||
return lua::pushboolean(
|
||||
L, Events::pressed(static_cast<int>(input_util::keycode_from(name)))
|
||||
L, engine->getInput().pressed(input_util::keycode_from(name))
|
||||
);
|
||||
} else if (prefix == "mouse") {
|
||||
return lua::pushboolean(
|
||||
L,
|
||||
Events::clicked(static_cast<int>(input_util::mousecode_from(name)))
|
||||
L, engine->getInput().clicked(input_util::mousecode_from(name))
|
||||
);
|
||||
} else {
|
||||
throw std::runtime_error("unknown input type " + util::quote(code));
|
||||
@@ -140,9 +129,7 @@ static void reset_pack_bindings(const io::path& packFolder) {
|
||||
auto bindsFile = configFolder / "bindings.toml";
|
||||
if (io::is_regular_file(bindsFile)) {
|
||||
Events::loadBindings(
|
||||
bindsFile.string(),
|
||||
io::read_string(bindsFile),
|
||||
BindType::REBIND
|
||||
bindsFile.string(), io::read_string(bindsFile), BindType::REBIND
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -157,12 +144,8 @@ static int l_reset_bindings(lua::State*) {
|
||||
|
||||
static int l_set_enabled(lua::State* L) {
|
||||
std::string bindname = lua::require_string(L, 1);
|
||||
bool enable = lua::toboolean(L, 2);
|
||||
const auto& bind = Events::bindings.find(bindname);
|
||||
if (bind == Events::bindings.end()) {
|
||||
throw std::runtime_error("unknown binding " + util::quote(bindname));
|
||||
}
|
||||
Events::bindings[bindname].enable = enable;
|
||||
bool enabled = lua::toboolean(L, 2);
|
||||
engine->getInput().requireBinding(bindname).enabled = enabled;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
static int l_get(lua::State* L) {
|
||||
static int l_get(lua::State* L, network::Network& network) {
|
||||
std::string url(lua::require_lstring(L, 1));
|
||||
|
||||
lua::pushvalue(L, 2);
|
||||
auto onResponse = lua::create_lambda_nothrow(L);
|
||||
|
||||
engine->getNetwork().get(url, [onResponse](std::vector<char> bytes) {
|
||||
network.get(url, [onResponse](std::vector<char> bytes) {
|
||||
engine->postRunnable([=]() {
|
||||
onResponse({std::string(bytes.data(), bytes.size())});
|
||||
});
|
||||
@@ -20,13 +20,13 @@ static int l_get(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_get_binary(lua::State* L) {
|
||||
static int l_get_binary(lua::State* L, network::Network& network) {
|
||||
std::string url(lua::require_lstring(L, 1));
|
||||
|
||||
lua::pushvalue(L, 2);
|
||||
auto onResponse = lua::create_lambda_nothrow(L);
|
||||
|
||||
engine->getNetwork().get(url, [onResponse](std::vector<char> bytes) {
|
||||
network.get(url, [onResponse](std::vector<char> bytes) {
|
||||
auto buffer = std::make_shared<util::Buffer<ubyte>>(
|
||||
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
|
||||
);
|
||||
@@ -37,7 +37,7 @@ static int l_get_binary(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_post(lua::State* L) {
|
||||
static int l_post(lua::State* L, network::Network& network) {
|
||||
std::string url(lua::require_lstring(L, 1));
|
||||
auto data = lua::tovalue(L, 2);
|
||||
|
||||
@@ -61,12 +61,12 @@ static int l_post(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_connect(lua::State* L) {
|
||||
static int l_connect(lua::State* L, network::Network& network) {
|
||||
std::string address = lua::require_string(L, 1);
|
||||
int port = lua::tointeger(L, 2);
|
||||
lua::pushvalue(L, 3);
|
||||
auto callback = lua::create_lambda_nothrow(L);
|
||||
u64id_t id = engine->getNetwork().connect(address, port, [callback](u64id_t id) {
|
||||
u64id_t id = network.connect(address, port, [callback](u64id_t id) {
|
||||
engine->postRunnable([=]() {
|
||||
callback({id});
|
||||
});
|
||||
@@ -75,25 +75,25 @@ static int l_connect(lua::State* L) {
|
||||
}
|
||||
|
||||
|
||||
static int l_close(lua::State* L) {
|
||||
static int l_close(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto connection = engine->getNetwork().getConnection(id)) {
|
||||
if (auto connection = network.getConnection(id)) {
|
||||
connection->close(true);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_closeserver(lua::State* L) {
|
||||
static int l_closeserver(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto server = engine->getNetwork().getServer(id)) {
|
||||
if (auto server = network.getServer(id)) {
|
||||
server->close();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_send(lua::State* L) {
|
||||
static int l_send(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
auto connection = engine->getNetwork().getConnection(id);
|
||||
auto connection = network.getConnection(id);
|
||||
if (connection == nullptr ||
|
||||
connection->getState() == network::ConnectionState::CLOSED) {
|
||||
return 0;
|
||||
@@ -120,7 +120,7 @@ static int l_send(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_recv(lua::State* L) {
|
||||
static int l_recv(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
int length = lua::tointeger(L, 2);
|
||||
auto connection = engine->getNetwork().getConnection(id);
|
||||
@@ -149,19 +149,19 @@ static int l_recv(lua::State* L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_available(lua::State* L) {
|
||||
static int l_available(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto connection = engine->getNetwork().getConnection(id)) {
|
||||
if (auto connection = network.getConnection(id)) {
|
||||
return lua::pushinteger(L, connection->available());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_open(lua::State* L) {
|
||||
static int l_open(lua::State* L, network::Network& network) {
|
||||
int port = lua::tointeger(L, 1);
|
||||
lua::pushvalue(L, 2);
|
||||
auto callback = lua::create_lambda_nothrow(L);
|
||||
u64id_t id = engine->getNetwork().openServer(port, [callback](u64id_t id) {
|
||||
u64id_t id = network.openServer(port, [callback](u64id_t id) {
|
||||
engine->postRunnable([=]() {
|
||||
callback({id});
|
||||
});
|
||||
@@ -169,9 +169,9 @@ static int l_open(lua::State* L) {
|
||||
return lua::pushinteger(L, id);
|
||||
}
|
||||
|
||||
static int l_is_alive(lua::State* L) {
|
||||
static int l_is_alive(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto connection = engine->getNetwork().getConnection(id)) {
|
||||
if (auto connection = network.getConnection(id)) {
|
||||
return lua::pushboolean(
|
||||
L,
|
||||
connection->getState() != network::ConnectionState::CLOSED ||
|
||||
@@ -181,9 +181,9 @@ static int l_is_alive(lua::State* L) {
|
||||
return lua::pushboolean(L, false);
|
||||
}
|
||||
|
||||
static int l_is_connected(lua::State* L) {
|
||||
static int l_is_connected(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto connection = engine->getNetwork().getConnection(id)) {
|
||||
if (auto connection = network.getConnection(id)) {
|
||||
return lua::pushboolean(
|
||||
L, connection->getState() == network::ConnectionState::CONNECTED
|
||||
);
|
||||
@@ -191,9 +191,9 @@ static int l_is_connected(lua::State* L) {
|
||||
return lua::pushboolean(L, false);
|
||||
}
|
||||
|
||||
static int l_get_address(lua::State* L) {
|
||||
static int l_get_address(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto connection = engine->getNetwork().getConnection(id)) {
|
||||
if (auto connection = network.getConnection(id)) {
|
||||
lua::pushstring(L, connection->getAddress());
|
||||
lua::pushinteger(L, connection->getPort());
|
||||
return 2;
|
||||
@@ -201,47 +201,65 @@ static int l_get_address(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_is_serveropen(lua::State* L) {
|
||||
static int l_is_serveropen(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto server = engine->getNetwork().getServer(id)) {
|
||||
if (auto server = network.getServer(id)) {
|
||||
return lua::pushboolean(L, server->isOpen());
|
||||
}
|
||||
return lua::pushboolean(L, false);
|
||||
}
|
||||
|
||||
static int l_get_serverport(lua::State* L) {
|
||||
static int l_get_serverport(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto server = engine->getNetwork().getServer(id)) {
|
||||
if (auto server = network.getServer(id)) {
|
||||
return lua::pushinteger(L, server->getPort());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_get_total_upload(lua::State* L) {
|
||||
return lua::pushinteger(L, engine->getNetwork().getTotalUpload());
|
||||
static int l_get_total_upload(lua::State* L, network::Network& network) {
|
||||
return lua::pushinteger(L, network.getTotalUpload());
|
||||
}
|
||||
|
||||
static int l_get_total_download(lua::State* L) {
|
||||
return lua::pushinteger(L, engine->getNetwork().getTotalDownload());
|
||||
static int l_get_total_download(lua::State* L, network::Network& network) {
|
||||
return lua::pushinteger(L, network.getTotalDownload());
|
||||
}
|
||||
|
||||
template <int(*func)(lua::State*, network::Network&)>
|
||||
int wrap(lua_State* L) {
|
||||
int result = 0;
|
||||
try {
|
||||
result = func(L, engine->getNetwork());
|
||||
}
|
||||
// transform exception with description into lua_error
|
||||
catch (std::exception& e) {
|
||||
luaL_error(L, e.what());
|
||||
}
|
||||
// Rethrow any other exception (lua error for example)
|
||||
catch (...) {
|
||||
throw;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
const luaL_Reg networklib[] = {
|
||||
{"get", lua::wrap<l_get>},
|
||||
{"get_binary", lua::wrap<l_get_binary>},
|
||||
{"post", lua::wrap<l_post>},
|
||||
{"get_total_upload", lua::wrap<l_get_total_upload>},
|
||||
{"get_total_download", lua::wrap<l_get_total_download>},
|
||||
{"__open", lua::wrap<l_open>},
|
||||
{"__closeserver", lua::wrap<l_closeserver>},
|
||||
{"__connect", lua::wrap<l_connect>},
|
||||
{"__close", lua::wrap<l_close>},
|
||||
{"__send", lua::wrap<l_send>},
|
||||
{"__recv", lua::wrap<l_recv>},
|
||||
{"__available", lua::wrap<l_available>},
|
||||
{"__is_alive", lua::wrap<l_is_alive>},
|
||||
{"__is_connected", lua::wrap<l_is_connected>},
|
||||
{"__get_address", lua::wrap<l_get_address>},
|
||||
{"__is_serveropen", lua::wrap<l_is_serveropen>},
|
||||
{"__get_serverport", lua::wrap<l_get_serverport>},
|
||||
{"get", wrap<l_get>},
|
||||
{"get_binary", wrap<l_get_binary>},
|
||||
{"post", wrap<l_post>},
|
||||
{"get_total_upload", wrap<l_get_total_upload>},
|
||||
{"get_total_download", wrap<l_get_total_download>},
|
||||
{"__open", wrap<l_open>},
|
||||
{"__closeserver", wrap<l_closeserver>},
|
||||
{"__connect", wrap<l_connect>},
|
||||
{"__close", wrap<l_close>},
|
||||
{"__send", wrap<l_send>},
|
||||
{"__recv", wrap<l_recv>},
|
||||
{"__available", wrap<l_available>},
|
||||
{"__is_alive", wrap<l_is_alive>},
|
||||
{"__is_connected", wrap<l_is_connected>},
|
||||
{"__get_address", wrap<l_get_address>},
|
||||
{"__is_serveropen", wrap<l_is_serveropen>},
|
||||
{"__get_serverport", wrap<l_get_serverport>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@@ -259,7 +259,7 @@ static int l_pack_request_writeable(lua::State* L) {
|
||||
util::replaceAll(str, L"%{0}", util::str2wstr_utf8(packid));
|
||||
guiutil::confirm(*engine, str, [packid, handler]() {
|
||||
handler({engine->getPaths().createWriteablePackDevice(packid)});
|
||||
engine->getGUI()->getMenu()->reset();
|
||||
engine->getGUI().getMenu()->reset();
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -268,21 +268,21 @@ void scripting::on_world_load(LevelController* controller) {
|
||||
lua::call_nothrow(L, 0, 0);
|
||||
}
|
||||
|
||||
for (auto& pack : scripting::engine->getAllContentPacks()) {
|
||||
for (auto& pack : Engine::getInstance().getAllContentPacks()) {
|
||||
lua::emit_event(L, pack.id + ":.worldopen");
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_world_tick() {
|
||||
auto L = lua::get_main_state();
|
||||
for (auto& pack : scripting::engine->getAllContentPacks()) {
|
||||
for (auto& pack : Engine::getInstance().getAllContentPacks()) {
|
||||
lua::emit_event(L, pack.id + ":.worldtick");
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_world_save() {
|
||||
auto L = lua::get_main_state();
|
||||
for (auto& pack : scripting::engine->getAllContentPacks()) {
|
||||
for (auto& pack : Engine::getInstance().getAllContentPacks()) {
|
||||
lua::emit_event(L, pack.id + ":.worldsave");
|
||||
}
|
||||
if (lua::getglobal(L, "__vc_on_world_save")) {
|
||||
@@ -292,7 +292,7 @@ void scripting::on_world_save() {
|
||||
|
||||
void scripting::on_world_quit() {
|
||||
auto L = lua::get_main_state();
|
||||
for (auto& pack : scripting::engine->getAllContentPacks()) {
|
||||
for (auto& pack : Engine::getInstance().getAllContentPacks()) {
|
||||
lua::emit_event(L, pack.id + ":.worldquit");
|
||||
}
|
||||
if (lua::getglobal(L, "__vc_on_world_quit")) {
|
||||
@@ -308,7 +308,7 @@ void scripting::on_world_quit() {
|
||||
void scripting::cleanup() {
|
||||
auto L = lua::get_main_state();
|
||||
lua::requireglobal(L, "pack");
|
||||
for (auto& pack : scripting::engine->getAllContentPacks()) {
|
||||
for (auto& pack : Engine::getInstance().getAllContentPacks()) {
|
||||
lua::requirefield(L, "unload");
|
||||
lua::pushstring(L, pack.id);
|
||||
lua::call_nothrow(L, 1);
|
||||
|
||||
@@ -44,7 +44,7 @@ void scripting::on_frontend_init(Hud* hud, WorldRenderer* renderer) {
|
||||
lua::call_nothrow(L, 0, 0);
|
||||
}
|
||||
|
||||
for (auto& pack : engine->getAllContentPacks()) {
|
||||
for (auto& pack : Engine::getInstance().getAllContentPacks()) {
|
||||
lua::emit_event(
|
||||
lua::get_main_state(),
|
||||
pack.id + ":.hudopen",
|
||||
@@ -56,7 +56,7 @@ void scripting::on_frontend_init(Hud* hud, WorldRenderer* renderer) {
|
||||
}
|
||||
|
||||
void scripting::on_frontend_render() {
|
||||
for (auto& pack : engine->getAllContentPacks()) {
|
||||
for (auto& pack : Engine::getInstance().getAllContentPacks()) {
|
||||
lua::emit_event(
|
||||
lua::get_main_state(),
|
||||
pack.id + ":.hudrender",
|
||||
@@ -67,7 +67,7 @@ void scripting::on_frontend_render() {
|
||||
|
||||
void scripting::on_frontend_close() {
|
||||
auto L = lua::get_main_state();
|
||||
for (auto& pack : engine->getAllContentPacks()) {
|
||||
for (auto& pack : Engine::getInstance().getAllContentPacks()) {
|
||||
lua::emit_event(
|
||||
L,
|
||||
pack.id + ":.hudclose",
|
||||
@@ -109,7 +109,7 @@ gui::PageLoaderFunc scripting::create_page_loader() {
|
||||
auto func = lua::create_lambda(L);
|
||||
return [func](const std::string& name) -> std::shared_ptr<gui::UINode> {
|
||||
auto docname = func({name}).asString();
|
||||
return engine->getAssets()->require<UiDocument>(docname).getRoot();
|
||||
return Engine::getInstance().getAssets()->require<UiDocument>(docname).getRoot();
|
||||
};
|
||||
}
|
||||
return nullptr;
|
||||
|
||||
@@ -260,7 +260,7 @@ std::unique_ptr<GeneratorScript> scripting::load_generator(
|
||||
const io::path& file,
|
||||
const std::string& dirPath
|
||||
) {
|
||||
auto L = create_state(engine->getPaths(), StateType::GENERATOR);
|
||||
auto L = create_state(Engine::getInstance().getPaths(), StateType::GENERATOR);
|
||||
|
||||
return std::make_unique<LuaGeneratorScript>(L, def, file, dirPath);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user