From 2f29a5627a6f791774ccb18c6255a97dd3a60910 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Wed, 23 Aug 2017 15:23:51 +0200 Subject: [PATCH] added picker() command in shell --- src/engine.cpp | 8 ++++- src/engine.h | 6 ++++ src/guitools.cpp | 52 +++++++++++++++++++++++++++++++ src/guitools.h | 16 ++++++++++ src/scene/playercharacternode.cpp | 13 +++++--- src/scene/playercharacternode.h | 2 ++ src/sparrowshell/scriptnode.cpp | 20 ++++++++++++ src/sparrowshell/scriptnode.h | 2 ++ src/test/main.cpp | 1 - 9 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 src/guitools.cpp create mode 100644 src/guitools.h diff --git a/src/engine.cpp b/src/engine.cpp index ecf45d8..0abb007 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -13,6 +13,7 @@ #include "scene/physicsdebugnode.h" #include "imgui/imgui.h" #include "tools/loader.h" +#include "guitools.h" Engine::Engine() : m_window(nullptr), @@ -23,11 +24,13 @@ Engine::Engine() : m_toggleShellAction(NO_ACTION), m_exitGameAction(NO_ACTION), m_showMouseAction(NO_ACTION), - m_mouseVisible(true) + m_mouseVisible(true), + m_pickerEnabled(false) { m_clock = new sf::Clock(); m_clock->restart(); m_renderer = new SparrowRenderer(); + m_guiTools = new GuiTools(this); } Engine::~Engine() @@ -115,6 +118,8 @@ void Engine::update() ImGui::End(); } } + + m_guiTools->update(); // update Physics if(m_world != nullptr) @@ -205,6 +210,7 @@ void Engine::disablePhysicsDebug() } } + void Engine::toggleMouseVisibility() { m_mouseVisible = !m_mouseVisible; diff --git a/src/engine.h b/src/engine.h index 6ca9c1b..3915ffb 100644 --- a/src/engine.h +++ b/src/engine.h @@ -8,6 +8,7 @@ class SparrowRenderer; class SceneTree; class SparrowShell; class PhysicsDebugNode; +class GuiTools; namespace sf { @@ -33,6 +34,7 @@ public: void enablePhysicsDebug(); void disablePhysicsDebug(); + void toggleMouseVisibility(); // special inputs @@ -49,11 +51,13 @@ public: SparrowRenderer* getRenderer() const {return m_renderer;} btDiscreteDynamicsWorld* getPhysics() const {return m_world;} SparrowShell* getShell() const {return m_sparrowshell;} + GuiTools* getGuiTools() const {return m_guiTools;} SceneTree* getScene() const; unsigned int getTime() const; unsigned int getDeltaTime() const; + // SceneTree* createScene(); void createScene(std::string scene_name); //void setCurrentScene(std::string scene_name){m_current_scene = scene_name;} @@ -73,6 +77,7 @@ private: btDiscreteDynamicsWorld* m_world; PhysicsDebugNode *m_physicsDebugNode; SparrowRenderer* m_renderer; + GuiTools* m_guiTools; void update(); @@ -81,6 +86,7 @@ private: int m_exitGameAction; int m_showMouseAction; bool m_mouseVisible; + bool m_pickerEnabled; void checkSpecialInputs(); }; diff --git a/src/guitools.cpp b/src/guitools.cpp new file mode 100644 index 0000000..883b3ba --- /dev/null +++ b/src/guitools.cpp @@ -0,0 +1,52 @@ +#include "guitools.h" +#include "imgui/imgui.h" + +#include "engine.h" +#include "scene/scenetree.h" +#include "SparrowRenderer/deferredpipeline.h" +#include "scene/playercharacternode.h" + +#include +#include +#include + +GuiTools::GuiTools(Engine* engine) : + m_engine(engine), + m_pickerEnabled(false) +{ + +} + +void GuiTools::update() +{ + if(m_pickerEnabled) + { + DeferredPipeline* pip = (DeferredPipeline*) m_engine->getScene()->getPipeline(); + FirstPersonCamera* cam = (FirstPersonCamera*) pip->getCamera(); + glm::vec3 dir_cam = cam->getDirection()*100; + btVector3 start(cam->getEyePosition().x,cam->getEyePosition().y,cam->getEyePosition().z); + btVector3 end(start.x() + dir_cam.x,start.y() + dir_cam.y,start.z() + dir_cam.z); + btCollisionWorld::ClosestRayResultCallback RayCallback(start, end); + m_engine->getPhysics()->rayTest(start,end,RayCallback); + + ImGui::Begin("Picker", &m_pickerEnabled); + if(RayCallback.hasHit()) + { + btVector3 target = RayCallback.m_hitPointWorld; + ImGui::Text("Target coordinates : (%.3f,%.3f,%.3f)",target.x(),target.y(),target.z()); + + } + else + { + ImGui::Text("Nothing"); + } + // if(ImGui::Button("Teleport")) + //move player to target coordinate + ImGui::End(); + } +} + +void GuiTools::togglePicker() +{ + m_pickerEnabled = !m_pickerEnabled; +} diff --git a/src/guitools.h b/src/guitools.h new file mode 100644 index 0000000..6629278 --- /dev/null +++ b/src/guitools.h @@ -0,0 +1,16 @@ +#ifndef GUITOOLS_H +#define GUITOOLS_H + +class Engine; + +class GuiTools +{ + Engine* m_engine; + bool m_pickerEnabled; +public: + GuiTools(Engine*); + void update(); + void togglePicker(); +}; + +#endif // GUITOOLS_H diff --git a/src/scene/playercharacternode.cpp b/src/scene/playercharacternode.cpp index c7f5ad2..b868a3e 100644 --- a/src/scene/playercharacternode.cpp +++ b/src/scene/playercharacternode.cpp @@ -189,7 +189,7 @@ void PlayerCharacterNode::update() btCollisionWorld::ClosestRayResultCallback RayCallback(start, end); getEngine().getPhysics()->rayTest(start, end, RayCallback); float controlRatio = 0.f; // 1 = total control, 0 = no control, can be seen as a slipperiness factor - if(RayCallback.hasHit()) // if ground is nearby + if(RayCallback.hasHit() ) // if ground is nearby { onGround = true; btVector3 normal = RayCallback.m_hitNormalWorld; @@ -199,10 +199,13 @@ void PlayerCharacterNode::update() if(onGround) { float displacement = RayCallback.m_hitPointWorld.y() - (end.y() + start.y())/2.f; - if(abs(displacement) > 0.1f) - pos.setY(pos.y() + deltaTime*0.01f*(displacement > 0 ? 1 : -1)); - else - pos.setY(pos.y() + displacement/4); + //if (m_last_displacement > displacement){ + if(abs(displacement) > 0.1f) + pos.setY(pos.y() + deltaTime*0.01f*(displacement > 0 ? 1 : -1)); + else + pos.setY(pos.y() + displacement/4); + //} + m_last_displacement = displacement; btTransform transform = btTransform::getIdentity(); transform.setOrigin(pos); m_rigidBody->setWorldTransform(transform); diff --git a/src/scene/playercharacternode.h b/src/scene/playercharacternode.h index d426ca8..8a9bca9 100644 --- a/src/scene/playercharacternode.h +++ b/src/scene/playercharacternode.h @@ -47,6 +47,8 @@ class PlayerCharacterNode : public CameraNode std::vector m_inputActions; + float m_last_displacement; + enum PlayerAction {FORWARD, BACKWARD, STRAFE_LEFT, STRAFE_RIGHT, JUMP, RUN, TOGGLE_NOCLIP}; public: diff --git a/src/sparrowshell/scriptnode.cpp b/src/sparrowshell/scriptnode.cpp index e575ff1..2ab213c 100644 --- a/src/sparrowshell/scriptnode.cpp +++ b/src/sparrowshell/scriptnode.cpp @@ -3,10 +3,12 @@ #include "sparrowshell/sparrowshell.h" #include #include +#include #include "Version.h" #include "SparrowInput/Version.h" #include "SparrowRenderer/Version.h" #include "SparrowSerializer/Version.h" +#include "guitools.h" #define LUA_DEFINE(var) S[#var]=var #define LUA_SET_FUN(var) m_script.set_function(#var,&ScriptNode::var,this);this->functions_name.push_back(#var); @@ -17,6 +19,7 @@ ScriptNode::ScriptNode() LUA_SET_FUN(print); LUA_SET_FUN(version); LUA_SET_FUN(clear); + LUA_SET_FUN(picker); } void ScriptNode::update(){ @@ -67,3 +70,20 @@ void ScriptNode::clear(){ float ScriptNode::getDeltaTime(){ return this->getEngine().getDeltaTime()/1000.; } + +void ScriptNode::testfunc(int i, float x=0.f,float y=0.f, float z=0.f){ + std::string s; + s= "Pop item "; + s+= std::to_string(i)+" at position ("; + s+=std::to_string(x); + s+=","; + s+=std::to_string(y); + s+=","; + s+=std::to_string(z); + s+=")"; + this->getEngine().getShell()->out(s.append("\n")); +} + +void ScriptNode::picker(){ + this->getEngine().getGuiTools()->togglePicker(); +} diff --git a/src/sparrowshell/scriptnode.h b/src/sparrowshell/scriptnode.h index b8723a5..3b937af 100644 --- a/src/sparrowshell/scriptnode.h +++ b/src/sparrowshell/scriptnode.h @@ -21,6 +21,8 @@ public: void version(); void clear(); float getDeltaTime(); + void testfunc(int, float,float,float); + void picker(); }; diff --git a/src/test/main.cpp b/src/test/main.cpp index 1dfef1f..4020ad5 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -355,7 +355,6 @@ int main(){ SparrowShell* shell = engine.getShell(); shell->setMoveCursorLeftAction(DefaultKeysMap::MOVE_CURSOR_LEFT); shell->setMoveCursorRightAction(DefaultKeysMap::MOVE_CURSOR_RIGHT); - //shell->setPlopTest(DefaultKeysMap::PLOP_TEST); input->addContext(Context("shell",DefaultKeysMap::getShellContext())); input->updateKeyBindings();