From 5ed2f7a3cbe85eb07f40f7b4ebb1adb2b58285d5 Mon Sep 17 00:00:00 2001 From: Martin Gerhardy Date: Mon, 17 Feb 2020 09:18:10 +0100 Subject: [PATCH] FRONTEND: added PlayerAction class to trigger the tool --- data/mapview/mapview-keybindings.cfg | 1 + src/modules/frontend/CMakeLists.txt | 1 + src/modules/frontend/PlayerAction.cpp | 35 +++++++++++++++++++++++++++ src/modules/frontend/PlayerAction.h | 34 ++++++++++++++++++++++++++ src/tools/mapview/MapView.cpp | 8 ++++++ src/tools/mapview/MapView.h | 2 ++ 6 files changed, 81 insertions(+) create mode 100644 src/modules/frontend/PlayerAction.cpp create mode 100644 src/modules/frontend/PlayerAction.h diff --git a/data/mapview/mapview-keybindings.cfg b/data/mapview/mapview-keybindings.cfg index 69ca03fd5..bf1cfc92a 100644 --- a/data/mapview/mapview-keybindings.cfg +++ b/data/mapview/mapview-keybindings.cfg @@ -6,6 +6,7 @@ a +move_left s +move_backward d +move_right space +jump +x +triggeraction ctrl+q quit wheelup "+zoom_in" wheeldown "+zoom_out" diff --git a/src/modules/frontend/CMakeLists.txt b/src/modules/frontend/CMakeLists.txt index b3addf7cb..2490ed259 100644 --- a/src/modules/frontend/CMakeLists.txt +++ b/src/modules/frontend/CMakeLists.txt @@ -1,6 +1,7 @@ set(SRCS ClientEntity.h ClientEntity.cpp ClientEntityId.h + PlayerAction.h PlayerAction.cpp PlayerMovement.h PlayerMovement.cpp ) diff --git a/src/modules/frontend/PlayerAction.cpp b/src/modules/frontend/PlayerAction.cpp new file mode 100644 index 000000000..6d2aa876f --- /dev/null +++ b/src/modules/frontend/PlayerAction.cpp @@ -0,0 +1,35 @@ +/** + * @file + */ + +#include "PlayerAction.h" +#include "core/command/Command.h" +#include "frontend/ClientEntity.h" + +namespace frontend { + +network::Animation PlayerAction::animation() const { + if (_triggerAction.pressed()) { + return network::Animation::TOOL; + } + return network::Animation::IDLE; +} + +bool PlayerAction::init() { + return true; +} + +void PlayerAction::update(const ClientEntityPtr& entity) { + entity->setAnimation(animation()); +} + +void PlayerAction::construct() { + core::Command::registerActionButton("triggeraction", _triggerAction); +} + +void PlayerAction::shutdown() { + core::Command::unregisterActionButton("triggeraction"); + _triggerAction.handleUp(core::ACTION_BUTTON_ALL_KEYS, 0ul); +} + +} diff --git a/src/modules/frontend/PlayerAction.h b/src/modules/frontend/PlayerAction.h new file mode 100644 index 000000000..e6099f115 --- /dev/null +++ b/src/modules/frontend/PlayerAction.h @@ -0,0 +1,34 @@ +/** + * @file + */ + +#pragma once + +#include "core/command/ActionButton.h" +#include "core/IComponent.h" +#include "Shared_generated.h" + +namespace frontend { + +class ClientEntity; +typedef std::shared_ptr ClientEntityPtr; + +/** + * @brief Trigger action component that does the input listening + * + * @see core::ActionButton + */ +class PlayerAction : public core::IComponent { +private: + core::ActionButton _triggerAction; + + network::Animation animation() const; + +public: + bool init() override; + void update(const ClientEntityPtr& entity); + void construct() override; + void shutdown() override; +}; + +} diff --git a/src/tools/mapview/MapView.cpp b/src/tools/mapview/MapView.cpp index d567e924e..cacce933c 100644 --- a/src/tools/mapview/MapView.cpp +++ b/src/tools/mapview/MapView.cpp @@ -47,6 +47,7 @@ core::AppState MapView::onConstruct() { _rotationSpeed = core::Var::getSafe(cfg::ClientMouseRotationSpeed); _movement.construct(); + _action.construct(); _camera.construct(); core::Command::registerCommand("bird", [&] (const core::CmdArgs& args) { @@ -93,6 +94,11 @@ core::AppState MapView::onInit() { return core::AppState::InitFailure; } + if (!_action.init()) { + Log::error("Failed to init action component"); + return core::AppState::InitFailure; + } + if (!_stockDataProvider->init(filesystem()->load("stock.lua"))) { Log::error("Failed to init stock data provider: %s", _stockDataProvider->error().c_str()); return core::AppState::InitFailure; @@ -184,6 +190,7 @@ void MapView::beforeUI() { _movement.update(_deltaFrameSeconds, camera.horizontalYaw(), _entity, [&] (const glm::vec3& pos, float maxWalkHeight) { return _worldMgr->findWalkableFloor(pos, maxWalkHeight); }); + _action.update(_entity); _camera.update(_entity->position(), _deltaFrameMillis, _now); if (_updateWorld) { @@ -315,6 +322,7 @@ core::AppState MapView::onCleanup() { _worldRenderer.shutdown(); _axis.shutdown(); _movement.shutdown(); + _action.shutdown(); _camera.shutdown(); _entity = frontend::ClientEntityPtr(); const core::AppState state = Super::onCleanup(); diff --git a/src/tools/mapview/MapView.h b/src/tools/mapview/MapView.h index ea820641c..df9e39647 100644 --- a/src/tools/mapview/MapView.h +++ b/src/tools/mapview/MapView.h @@ -8,6 +8,7 @@ #include "RenderShaders.h" #include "voxelrender/WorldRenderer.h" #include "frontend/ClientEntity.h" +#include "frontend/PlayerAction.h" #include "render/Axis.h" #include "frontend/PlayerMovement.h" #include "voxelrender/PlayerCamera.h" @@ -35,6 +36,7 @@ protected: core::VarPtr _rotationSpeed; frontend::ClientEntityPtr _entity; frontend::PlayerMovement _movement; + frontend::PlayerAction _action; stock::StockDataProviderPtr _stockDataProvider; voxelformat::VolumeCachePtr _volumeCache; voxelrender::PlayerCamera _camera;