From b66247ebcad514de95c3873c8c67d74a1b92bc8f Mon Sep 17 00:00:00 2001 From: Lendemor Date: Thu, 19 Jan 2017 14:12:55 +0100 Subject: [PATCH] added button and menu --- CMakeLists.txt | 2 + src/defaultkeysmap.h | 9 +- src/engine.h | 1 + src/scene/graphicalnode.h | 3 - src/scene/gui/buttonnode.cpp | 50 +++++++++++ src/scene/gui/buttonnode.h | 27 ++++++ src/scene/gui/buttonshape.cpp | 13 +++ src/scene/gui/buttonshape.h | 24 ++++++ src/scene/gui/callback.h | 11 +++ src/sparrowshell/sparrowshell.cpp | 7 ++ src/test/main.cpp | 132 ++++++++++++++++++++++++++++-- 11 files changed, 265 insertions(+), 14 deletions(-) create mode 100644 src/scene/gui/buttonnode.cpp create mode 100644 src/scene/gui/buttonnode.h create mode 100644 src/scene/gui/buttonshape.cpp create mode 100644 src/scene/gui/buttonshape.h create mode 100644 src/scene/gui/callback.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f555a87..1b4c798 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,8 @@ file(GLOB LIB_SRC_LIST src/*.cpp src/tools/*.cpp src/scene/*.cpp src/scene/gui/* file(GLOB LIB_HEAD_LIST src/*.h src/tools/*.h src/scene/*.h src/scene/gui/*.h src/sparrowshell/*.h) file(GLOB EXEC_SRC_LIST src/test/*.cpp) +set(RESOURCES_FILES ${LIB_HEAD_LIST}) + #set compilation option set(IS_LIBRARY True) set(USE_RENDERER True) diff --git a/src/defaultkeysmap.h b/src/defaultkeysmap.h index 24f86b6..d212b51 100644 --- a/src/defaultkeysmap.h +++ b/src/defaultkeysmap.h @@ -8,7 +8,7 @@ class DefaultKeysMap : public IKeysMap public: enum{MAIN_ACTION, SECONDARY_ACTION, TERTIARY_ACTION, MOVE_FORWARD, MOVE_BACKWARD, STRAFE_LEFT, STRAFE_RIGHT, JUMP, TOGGLE_NOCLIP, TOGGLE_PHYSICS_DEBUG, TOGGLE_CONSOLE, - MOVE_CURSOR_LEFT, MOVE_CURSOR_RIGHT, PLOP_TEST, CLEAR_CONSOLE, + MOVE_CURSOR_LEFT, MOVE_CURSOR_RIGHT, PLOP_TEST, CLEAR_CONSOLE, LEFT_CLICK, EXIT_GAME,LAST_DEFAULT_ACTION}; DefaultKeysMap(){ @@ -19,7 +19,7 @@ public: keys.push_back( {MOVE_BACKWARD, sf::Keyboard::S, IKeysMap::HOLD} ); keys.push_back( {STRAFE_LEFT, sf::Keyboard::Q, IKeysMap::HOLD} ); keys.push_back( {STRAFE_RIGHT, sf::Keyboard::D, IKeysMap::HOLD} ); - keys.push_back( {JUMP, sf::Keyboard::Space, IKeysMap::PRESSED} ); + keys.push_back( {JUMP, sf::Keyboard::Space, IKeysMap::PRESSED} ); keys.push_back( {TOGGLE_NOCLIP, sf::Keyboard::G, IKeysMap::PRESSED} ); keys.push_back( {TOGGLE_PHYSICS_DEBUG, sf::Keyboard::P, IKeysMap::PRESSED} ); keys.push_back( {TOGGLE_CONSOLE, sf::Keyboard::F3, IKeysMap::PRESSED} ); @@ -28,6 +28,7 @@ public: keys.push_back( {PLOP_TEST, sf::Keyboard::F7, IKeysMap::PRESSED} ); keys.push_back( {CLEAR_CONSOLE, sf::Keyboard::F2, IKeysMap::PRESSED} ); keys.push_back( {EXIT_GAME, sf::Keyboard::Escape, IKeysMap::PRESSED} ); + keys.push_back( {LEFT_CLICK, sf::Keyboard::KeyCount + sf::Mouse::Left, IKeysMap::PRESSED} ); } static std::vector getDefaultContext() @@ -39,6 +40,10 @@ public: { return {TOGGLE_CONSOLE,MOVE_CURSOR_LEFT,MOVE_CURSOR_RIGHT,PLOP_TEST,CLEAR_CONSOLE}; } + + static std::vector getMenuContext(){ + return {LEFT_CLICK,EXIT_GAME,TOGGLE_CONSOLE}; + } }; #endif // DEFAULTKEYSMAP_H diff --git a/src/engine.h b/src/engine.h index a4ce378..0afaed2 100644 --- a/src/engine.h +++ b/src/engine.h @@ -48,6 +48,7 @@ public: SparrowRenderer* getRenderer() const {return m_renderer;} btDiscreteDynamicsWorld* getPhysics() const {return m_world;} SparrowShell* getShell() const {return m_sparrowshell;} + SceneTree* getScene() const {return m_scene;} void outputShell(std::string str) const; diff --git a/src/scene/graphicalnode.h b/src/scene/graphicalnode.h index c020353..260d891 100644 --- a/src/scene/graphicalnode.h +++ b/src/scene/graphicalnode.h @@ -43,9 +43,6 @@ public: GraphicalNode(bool visible = true) : m_parentVisible(true), m_visible(visible), m_transformChanged(true), m_motionState(this) {} virtual ~GraphicalNode() { setVisible(false); } -// bool isVisible(){return m_visible;} -// void toggleVisibility(); - virtual void setSceneTree(SceneTree* tree); // transform methods diff --git a/src/scene/gui/buttonnode.cpp b/src/scene/gui/buttonnode.cpp new file mode 100644 index 0000000..8715ec3 --- /dev/null +++ b/src/scene/gui/buttonnode.cpp @@ -0,0 +1,50 @@ +#include "buttonnode.h" +#include "buttonshape.h" +#include "backgroundnode.h" + +#include + +#include "scene/scenetree.h" +#include "input.h" +#include "sparrowshell/sparrowshell.h" +#include "mesh.h" +#include "phongmaterial.h" + +ButtonNode::ButtonNode(glm::vec2 position,ButtonShape* shape): + m_position(position),m_shape(shape) +{ +} + +void ButtonNode::setBackGround(BackGroundNode* background){ + m_background = background; + addChild(background); +} + +BackGroundNode* ButtonNode::getBackGround() +{ + return m_background; +} + +void ButtonNode::update() +{ + Input* input = getEngine().getInput(); + sf::Vector2i v = input->getPosition(); + glm::vec2 pos = glm::vec2(v.x,v.y); + if (m_shape->hover(pos)){ + PhongMaterial* mat = (PhongMaterial*) m_background->getGeometryNode()->mesh->getMaterial(); + mat->diffuse=glm::vec3(0.2,0.6,0.6); + }else{ + PhongMaterial* mat = (PhongMaterial*) m_background->getGeometryNode()->mesh->getMaterial(); + mat->diffuse=glm::vec3(0.6,0.2,0.6); + } + + for (auto action : input->getActions()) + { + if (action == m_action){ + if (m_shape->hover(pos)){ + m_callback->exec(); +// getEngine().getShell()->out("hover"); + } + } + } +} diff --git a/src/scene/gui/buttonnode.h b/src/scene/gui/buttonnode.h new file mode 100644 index 0000000..d36ae82 --- /dev/null +++ b/src/scene/gui/buttonnode.h @@ -0,0 +1,27 @@ +#ifndef BUTTONNODE_H +#define BUTTONNODE_H + +#include "scene/meshnode.h" +#include "scene/graphicalcontainernode.h" +#include "scene/gui/callback.h" + +class ButtonShape; +class BackGroundNode; + +class ButtonNode : public GraphicalContainerNode +{ + int m_action; + glm::vec2 m_position; + ButtonShape* m_shape; + CallBack* m_callback; + BackGroundNode* m_background; +public: + ButtonNode(glm::vec2 m_position, ButtonShape* shape); + void setBackGround(BackGroundNode*); + BackGroundNode* getBackGround(); + void setCallBack(CallBack* callback){m_callback=callback;} + void setAction(int action){m_action=action;} + void update(); +}; + +#endif // BUTTONNODE_H diff --git a/src/scene/gui/buttonshape.cpp b/src/scene/gui/buttonshape.cpp new file mode 100644 index 0000000..051e6f0 --- /dev/null +++ b/src/scene/gui/buttonshape.cpp @@ -0,0 +1,13 @@ +#include "buttonshape.h" +#include "glm/common.hpp" + +RectangleButtonShape::RectangleButtonShape(glm::vec2 position, glm::vec2 dimension):ButtonShape(position),m_dimension(dimension) +{ + +} + +bool RectangleButtonShape::hover(glm::vec2 mouse_position){ + glm::vec2 pos = getPosition(); + return (mouse_position.x >= pos.x && mouse_position.x < pos.x + m_dimension.x) + && (mouse_position.y >= pos.y && mouse_position.y < pos.y + m_dimension.y); +} diff --git a/src/scene/gui/buttonshape.h b/src/scene/gui/buttonshape.h new file mode 100644 index 0000000..535c78f --- /dev/null +++ b/src/scene/gui/buttonshape.h @@ -0,0 +1,24 @@ +#ifndef BUTTONSHAPE_H +#define BUTTONSHAPE_H + +#include "glm/vec2.hpp" + +class ButtonShape +{ + glm::vec2 m_position; +public: + ButtonShape(glm::vec2 position) : m_position(position){} + + virtual bool hover(glm::vec2 mouse_position) = 0; + glm::vec2 getPosition(){return m_position;} +}; + +class RectangleButtonShape:public ButtonShape +{ + glm::vec2 m_dimension; +public: + RectangleButtonShape(glm::vec2, glm::vec2); + bool hover(glm::vec2 mouse_position); +}; + +#endif // BUTTONSHAPE_H diff --git a/src/scene/gui/callback.h b/src/scene/gui/callback.h new file mode 100644 index 0000000..c02636f --- /dev/null +++ b/src/scene/gui/callback.h @@ -0,0 +1,11 @@ +#ifndef CALLBACK_H +#define CALLBACK_H + +class CallBack +{ +public: + CallBack(){} + virtual void exec()=0; +}; + +#endif // CALLBACK_H diff --git a/src/sparrowshell/sparrowshell.cpp b/src/sparrowshell/sparrowshell.cpp index b1096b6..09e8e10 100644 --- a/src/sparrowshell/sparrowshell.cpp +++ b/src/sparrowshell/sparrowshell.cpp @@ -29,6 +29,8 @@ SparrowShell::SparrowShell(sf::Window* window): m_input_cursor_pos(0), m_input_mesh(nullptr) { + // setVisible(false); + sf::Vector2u size = window->getSize(); m_dimension = glm::ivec2(size.x,DEFAULT_FONT_SIZE*(BUFFER_DISPLAYED_LINES+1)); m_buffer->setFontSize(DEFAULT_FONT_SIZE); @@ -40,8 +42,12 @@ SparrowShell::SparrowShell(sf::Window* window): //Create mesh for background m_background = new BackGroundNode(glm::vec2(0),m_dimension,glm::vec3(0.1,0.1,0.1),0.5,SHELL_DEPTH); +// m_background->setVisible(true); //Create mesh for scrollbar m_scrollbar = new ShellScrollBar(this); +// m_scrollbar->setVisible(true); + +// m_buffer->setVisible(true); this->addChild(m_background); this->addChild(m_buffer); @@ -92,6 +98,7 @@ void SparrowShell::toggleShell() getEngine().getWindow()->setKeyRepeatEnabled(true); } m_shellEnabled = !m_shellEnabled; + //toggleVisibility(); for(auto child : m_children) child->toggleVisibility(); m_buffer->toggleBuffer(); diff --git a/src/test/main.cpp b/src/test/main.cpp index 47fd4c8..53556a0 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -28,8 +28,15 @@ #include +#include "scene/gui/buttonnode.h" +#include "scene/gui/buttonshape.h" + +#include "scene/gui/backgroundnode.h" + #include "potator.h" +#include "scene/gui/callback.h" + class TestGen : public TerrainGenerator { float map[64*64]; @@ -127,7 +134,7 @@ void generateSponza(SceneTree *scene, btDiscreteDynamicsWorld *world) struct Config { std::string mode; // fullscreen / windowed / borderless - std::string scene; // terrain / sponza / none + std::string scene; // terrain / sponza / menu / none bool vsync; int width; int height; @@ -157,6 +164,99 @@ struct Config }; +class Demo { + SceneTree* m_demo_scene; + PlayerCharacterNode* m_player; +public: + Demo(Engine* engine,Config* config){ + m_demo_scene = engine->createScene(); + + //player + m_player = new PlayerCharacterNode(false); + m_player->setInputs(DefaultKeysMap::MOVE_FORWARD, DefaultKeysMap::MOVE_BACKWARD, DefaultKeysMap::STRAFE_LEFT, DefaultKeysMap::STRAFE_RIGHT, DefaultKeysMap::JUMP, DefaultKeysMap::TOGGLE_NOCLIP); + m_demo_scene->getRootObject()->addChild(m_player); + m_demo_scene->setMainCamera(m_player); + + //potator + Potator *potator = new Potator(m_player, DefaultKeysMap::MAIN_ACTION, DefaultKeysMap::SECONDARY_ACTION, DefaultKeysMap::TERTIARY_ACTION); + m_demo_scene->getRootObject()->addChild(potator); + + //lighting + LightNode *ambientLight = new LightNode(new AmbientLight(glm::vec3(0.05f))); + + DirectionnalLight* sun = new DirectionnalLight(glm::vec3(5, 8, -2), glm::vec3(0.9f)); + LightNode *sunLight = new LightNode(sun); + + m_demo_scene->getRootObject()->addChild(ambientLight); + m_demo_scene->getRootObject()->addChild(sunLight); + + if(config->scene == "sponza") + { + sun->initShadowMap(4096); + generateSponza(m_demo_scene, engine->getPhysics()); + m_demo_scene->getRootObject()->addChild(new LightNode(new PointLight(glm::vec3(-3.5, 2, 1.8), 15, glm::vec3(0.35f)))); + m_demo_scene->getRootObject()->addChild(new LightNode(new PointLight(glm::vec3(-5, 6, 2), 15, glm::vec3(0.35f)))); + m_player->setPosition(0.f, 2.f, 0.f); + sun->setShadowView(glm::vec3(30, 30, 50)); + } + else if(config->scene == "terrain") + { + sun->initShadowMap(4096); + generateTerrain(m_demo_scene, engine->getPhysics()); + m_player->setPosition(0.f, 15.f, 0.f); + sun->setShadowView(glm::vec3(130, 130, 70)); + } + } + + SceneTree* getScene(){return m_demo_scene;} + PlayerCharacterNode* getPlayer(){return m_player;} +}; + +class ButtonDemoCallBack : public CallBack +{ + Engine* m_engine; + Demo* m_demo; +public: + ButtonDemoCallBack(Engine* engine, Demo* demo):m_engine(engine),m_demo(demo){} + + void exec(){ + m_engine->setScene(m_demo->getScene()); + m_engine->getInput()->setCurrentContext("default"); + m_engine->getInput()->setMouseGrabbed(true); + m_engine->getPhysics()->addRigidBody(m_demo->getPlayer()->getRigidbody()); + m_engine->toggleMouseVisibility(); + } +}; + +class Menu { + int m_left_click_action; + ButtonNode* m_button_demo; + SceneTree* m_menu_scene; +public: + Menu(Engine* engine,Config* config){ + m_menu_scene = engine->createScene(); + m_button_demo = new ButtonNode(glm::vec2(100,100),new RectangleButtonShape(glm::vec2(100,100),glm::vec2(300,100))); + BackGroundNode* bg = new BackGroundNode(glm::vec2(100,100),glm::vec2(300,100),glm::vec3(1,0,0.5),1,11); + m_button_demo->setBackGround(bg); +// m_button_demo->setAction(m_left_click_action); + m_menu_scene->getRootObject()->addChild(m_button_demo); + m_menu_scene->getRootObject()->addChild(bg); + bg->setVisible(true); + m_button_demo->setVisible(true); + } + void setLeftClickAction(int action){ + m_left_click_action = action; + m_button_demo->setAction(m_left_click_action); + } + + void setButtonCallBack(ButtonDemoCallBack* button_callback){ + m_button_demo->setCallBack(button_callback); + } + + SceneTree* getScene(){return m_menu_scene;} +}; + + int main(){ Config config; config.loadFromMap(Loader::loadConfigFile("../config.ini")); @@ -172,12 +272,12 @@ int main(){ engine.createWindow("Sparrow Engine Demo", config.width, config.height, config.mode); engine.getWindow()->setVerticalSyncEnabled(config.vsync); - engine.toggleMouseVisibility(); +// engine.toggleMouseVisibility(); // setting up SparrowEngine engine.initPhysics(); - SceneTree *scene = engine.createScene(); - engine.setScene(scene); + // SceneTree *scene = engine.createScene(); + // engine.setScene(scene); // settin gup SparrowInput Input* input = engine.getInput(); @@ -198,13 +298,26 @@ int main(){ input->addContext(Context("shell",DefaultKeysMap::getShellContext())); input->updateKeyBindings(); + //setup menu + Menu* menu = new Menu(&engine,&config); + Demo* demo = new Demo(&engine,&config); + menu->setLeftClickAction(DefaultKeysMap::LEFT_CLICK); + input->addContext(Context("menu",DefaultKeysMap::getMenuContext())); + input->setCurrentContext("menu"); + input->updateKeyBindings(); + + menu->setButtonCallBack(new ButtonDemoCallBack(&engine,demo)); + + engine.setScene(menu->getScene()); + engine.getInput()->setMouseGrabbed(false); + /* // trackball camera TrackBallCameraNode *trackBallCam = new TrackBallCameraNode(engine.getInput()); trackBallCam->setInputs(myKeysMap::SECONDARY_HOLD, myKeysMap::MAIN_HOLD); scene.getRootObject()->addChild(trackBallCam); scene.setMainCamera(trackBallCam); -*/ + // first person player controller PlayerCharacterNode *player = new PlayerCharacterNode(false); player->setInputs(DefaultKeysMap::MOVE_FORWARD, DefaultKeysMap::MOVE_BACKWARD, DefaultKeysMap::STRAFE_LEFT, DefaultKeysMap::STRAFE_RIGHT, DefaultKeysMap::JUMP, DefaultKeysMap::TOGGLE_NOCLIP); @@ -224,9 +337,9 @@ int main(){ scene->getRootObject()->addChild(ambientLight); scene->getRootObject()->addChild(sunLight); - +*/ // scene - if(config.scene == "sponza") + /* if(config.scene == "sponza") { sun->initShadowMap(4096); generateSponza(scene, engine.getPhysics()); @@ -241,14 +354,15 @@ int main(){ generateTerrain(scene, engine.getPhysics()); player->setPosition(0.f, 15.f, 0.f); sun->setShadowView(glm::vec3(130, 130, 70)); - } + }*/ // shell output tests engine.outputShell("Hello World!"); engine.outputShell("Starting test :"); // preparing shaders and launching the engine - scene->updateShaders(); + engine.getScene()->updateShaders(); +// scene->updateShaders(); engine.start(); // pathfinding tests