added button and menu

This commit is contained in:
Lendemor 2017-01-19 14:12:55 +01:00
parent ca797bf3f2
commit b66247ebca
11 changed files with 265 additions and 14 deletions

View File

@ -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 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) file(GLOB EXEC_SRC_LIST src/test/*.cpp)
set(RESOURCES_FILES ${LIB_HEAD_LIST})
#set compilation option #set compilation option
set(IS_LIBRARY True) set(IS_LIBRARY True)
set(USE_RENDERER True) set(USE_RENDERER True)

View File

@ -8,7 +8,7 @@ class DefaultKeysMap : public IKeysMap
public: public:
enum{MAIN_ACTION, SECONDARY_ACTION, TERTIARY_ACTION, MOVE_FORWARD, MOVE_BACKWARD, STRAFE_LEFT, STRAFE_RIGHT, JUMP, enum{MAIN_ACTION, SECONDARY_ACTION, TERTIARY_ACTION, MOVE_FORWARD, MOVE_BACKWARD, STRAFE_LEFT, STRAFE_RIGHT, JUMP,
TOGGLE_NOCLIP, TOGGLE_PHYSICS_DEBUG, TOGGLE_CONSOLE, 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}; EXIT_GAME,LAST_DEFAULT_ACTION};
DefaultKeysMap(){ DefaultKeysMap(){
@ -28,6 +28,7 @@ public:
keys.push_back( {PLOP_TEST, sf::Keyboard::F7, IKeysMap::PRESSED} ); keys.push_back( {PLOP_TEST, sf::Keyboard::F7, IKeysMap::PRESSED} );
keys.push_back( {CLEAR_CONSOLE, sf::Keyboard::F2, 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( {EXIT_GAME, sf::Keyboard::Escape, IKeysMap::PRESSED} );
keys.push_back( {LEFT_CLICK, sf::Keyboard::KeyCount + sf::Mouse::Left, IKeysMap::PRESSED} );
} }
static std::vector<int> getDefaultContext() static std::vector<int> getDefaultContext()
@ -39,6 +40,10 @@ public:
{ {
return {TOGGLE_CONSOLE,MOVE_CURSOR_LEFT,MOVE_CURSOR_RIGHT,PLOP_TEST,CLEAR_CONSOLE}; return {TOGGLE_CONSOLE,MOVE_CURSOR_LEFT,MOVE_CURSOR_RIGHT,PLOP_TEST,CLEAR_CONSOLE};
} }
static std::vector<int> getMenuContext(){
return {LEFT_CLICK,EXIT_GAME,TOGGLE_CONSOLE};
}
}; };
#endif // DEFAULTKEYSMAP_H #endif // DEFAULTKEYSMAP_H

View File

@ -48,6 +48,7 @@ public:
SparrowRenderer* getRenderer() const {return m_renderer;} SparrowRenderer* getRenderer() const {return m_renderer;}
btDiscreteDynamicsWorld* getPhysics() const {return m_world;} btDiscreteDynamicsWorld* getPhysics() const {return m_world;}
SparrowShell* getShell() const {return m_sparrowshell;} SparrowShell* getShell() const {return m_sparrowshell;}
SceneTree* getScene() const {return m_scene;}
void outputShell(std::string str) const; void outputShell(std::string str) const;

View File

@ -43,9 +43,6 @@ public:
GraphicalNode(bool visible = true) : m_parentVisible(true), m_visible(visible), m_transformChanged(true), m_motionState(this) {} GraphicalNode(bool visible = true) : m_parentVisible(true), m_visible(visible), m_transformChanged(true), m_motionState(this) {}
virtual ~GraphicalNode() { setVisible(false); } virtual ~GraphicalNode() { setVisible(false); }
// bool isVisible(){return m_visible;}
// void toggleVisibility();
virtual void setSceneTree(SceneTree* tree); virtual void setSceneTree(SceneTree* tree);
// transform methods // transform methods

View File

@ -0,0 +1,50 @@
#include "buttonnode.h"
#include "buttonshape.h"
#include "backgroundnode.h"
#include <iostream>
#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");
}
}
}
}

View File

@ -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

View File

@ -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);
}

View File

@ -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

11
src/scene/gui/callback.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef CALLBACK_H
#define CALLBACK_H
class CallBack
{
public:
CallBack(){}
virtual void exec()=0;
};
#endif // CALLBACK_H

View File

@ -29,6 +29,8 @@ SparrowShell::SparrowShell(sf::Window* window):
m_input_cursor_pos(0), m_input_cursor_pos(0),
m_input_mesh(nullptr) m_input_mesh(nullptr)
{ {
// setVisible(false);
sf::Vector2u size = window->getSize(); sf::Vector2u size = window->getSize();
m_dimension = glm::ivec2(size.x,DEFAULT_FONT_SIZE*(BUFFER_DISPLAYED_LINES+1)); m_dimension = glm::ivec2(size.x,DEFAULT_FONT_SIZE*(BUFFER_DISPLAYED_LINES+1));
m_buffer->setFontSize(DEFAULT_FONT_SIZE); m_buffer->setFontSize(DEFAULT_FONT_SIZE);
@ -40,8 +42,12 @@ SparrowShell::SparrowShell(sf::Window* window):
//Create mesh for background //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 = 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 //Create mesh for scrollbar
m_scrollbar = new ShellScrollBar(this); m_scrollbar = new ShellScrollBar(this);
// m_scrollbar->setVisible(true);
// m_buffer->setVisible(true);
this->addChild(m_background); this->addChild(m_background);
this->addChild(m_buffer); this->addChild(m_buffer);
@ -92,6 +98,7 @@ void SparrowShell::toggleShell()
getEngine().getWindow()->setKeyRepeatEnabled(true); getEngine().getWindow()->setKeyRepeatEnabled(true);
} }
m_shellEnabled = !m_shellEnabled; m_shellEnabled = !m_shellEnabled;
//toggleVisibility();
for(auto child : m_children) for(auto child : m_children)
child->toggleVisibility(); child->toggleVisibility();
m_buffer->toggleBuffer(); m_buffer->toggleBuffer();

View File

@ -28,8 +28,15 @@
#include <glm/ext.hpp> #include <glm/ext.hpp>
#include "scene/gui/buttonnode.h"
#include "scene/gui/buttonshape.h"
#include "scene/gui/backgroundnode.h"
#include "potator.h" #include "potator.h"
#include "scene/gui/callback.h"
class TestGen : public TerrainGenerator class TestGen : public TerrainGenerator
{ {
float map[64*64]; float map[64*64];
@ -127,7 +134,7 @@ void generateSponza(SceneTree *scene, btDiscreteDynamicsWorld *world)
struct Config struct Config
{ {
std::string mode; // fullscreen / windowed / borderless std::string mode; // fullscreen / windowed / borderless
std::string scene; // terrain / sponza / none std::string scene; // terrain / sponza / menu / none
bool vsync; bool vsync;
int width; int width;
int height; 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(){ int main(){
Config config; Config config;
config.loadFromMap(Loader::loadConfigFile("../config.ini")); config.loadFromMap(Loader::loadConfigFile("../config.ini"));
@ -172,12 +272,12 @@ int main(){
engine.createWindow("Sparrow Engine Demo", config.width, config.height, config.mode); engine.createWindow("Sparrow Engine Demo", config.width, config.height, config.mode);
engine.getWindow()->setVerticalSyncEnabled(config.vsync); engine.getWindow()->setVerticalSyncEnabled(config.vsync);
engine.toggleMouseVisibility(); // engine.toggleMouseVisibility();
// setting up SparrowEngine // setting up SparrowEngine
engine.initPhysics(); engine.initPhysics();
SceneTree *scene = engine.createScene(); // SceneTree *scene = engine.createScene();
engine.setScene(scene); // engine.setScene(scene);
// settin gup SparrowInput // settin gup SparrowInput
Input* input = engine.getInput(); Input* input = engine.getInput();
@ -198,13 +298,26 @@ int main(){
input->addContext(Context("shell",DefaultKeysMap::getShellContext())); input->addContext(Context("shell",DefaultKeysMap::getShellContext()));
input->updateKeyBindings(); 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 // trackball camera
TrackBallCameraNode *trackBallCam = new TrackBallCameraNode(engine.getInput()); TrackBallCameraNode *trackBallCam = new TrackBallCameraNode(engine.getInput());
trackBallCam->setInputs(myKeysMap::SECONDARY_HOLD, myKeysMap::MAIN_HOLD); trackBallCam->setInputs(myKeysMap::SECONDARY_HOLD, myKeysMap::MAIN_HOLD);
scene.getRootObject()->addChild(trackBallCam); scene.getRootObject()->addChild(trackBallCam);
scene.setMainCamera(trackBallCam); scene.setMainCamera(trackBallCam);
*/
// first person player controller // first person player controller
PlayerCharacterNode *player = new PlayerCharacterNode(false); PlayerCharacterNode *player = new PlayerCharacterNode(false);
player->setInputs(DefaultKeysMap::MOVE_FORWARD, DefaultKeysMap::MOVE_BACKWARD, DefaultKeysMap::STRAFE_LEFT, DefaultKeysMap::STRAFE_RIGHT, DefaultKeysMap::JUMP, DefaultKeysMap::TOGGLE_NOCLIP); 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(ambientLight);
scene->getRootObject()->addChild(sunLight); scene->getRootObject()->addChild(sunLight);
*/
// scene // scene
if(config.scene == "sponza") /* if(config.scene == "sponza")
{ {
sun->initShadowMap(4096); sun->initShadowMap(4096);
generateSponza(scene, engine.getPhysics()); generateSponza(scene, engine.getPhysics());
@ -241,14 +354,15 @@ int main(){
generateTerrain(scene, engine.getPhysics()); generateTerrain(scene, engine.getPhysics());
player->setPosition(0.f, 15.f, 0.f); player->setPosition(0.f, 15.f, 0.f);
sun->setShadowView(glm::vec3(130, 130, 70)); sun->setShadowView(glm::vec3(130, 130, 70));
} }*/
// shell output tests // shell output tests
engine.outputShell("Hello World!"); engine.outputShell("Hello World!");
engine.outputShell("Starting test :"); engine.outputShell("Starting test :");
// preparing shaders and launching the engine // preparing shaders and launching the engine
scene->updateShaders(); engine.getScene()->updateShaders();
// scene->updateShaders();
engine.start(); engine.start();
// pathfinding tests // pathfinding tests