This commit is contained in:
Anselme 2018-01-30 10:30:33 +01:00
commit 5a2bf29fba
6 changed files with 247 additions and 0 deletions

View File

@ -14,6 +14,7 @@
#include "imgui/imgui.h" #include "imgui/imgui.h"
#include "tools/loader.h" #include "tools/loader.h"
#include "editor.h" #include "editor.h"
#include "keymapper.h"
#include "tools/loadingthread.h" #include "tools/loadingthread.h"
Engine::Engine() : Engine::Engine() :
@ -67,6 +68,7 @@ void Engine::createWindow(std::string title,
m_renderer->initGL(w, h); m_renderer->initGL(w, h);
m_sparrowshell = new SparrowShell(m_window); m_sparrowshell = new SparrowShell(m_window);
m_editor = new Editor(); m_editor = new Editor();
m_keymapper = new KeyMapper();
m_loadingThread = LoadingThread::init(); m_loadingThread = LoadingThread::init();
} }
@ -180,12 +182,14 @@ void Engine::setScene(std::string scene)
previous_scene->getRootObject()->removeChild(m_sparrowshell); previous_scene->getRootObject()->removeChild(m_sparrowshell);
previous_scene->getRootObject()->removeChild(m_editor); previous_scene->getRootObject()->removeChild(m_editor);
previous_scene->getRootObject()->removeChild(m_keymapper);
m_renderer->setScene(new_scene); m_renderer->setScene(new_scene);
m_renderer->resizeGL(m_window->getSize().x, m_window->getSize().y); m_renderer->resizeGL(m_window->getSize().x, m_window->getSize().y);
new_scene->getRootObject()->addChild(m_sparrowshell); new_scene->getRootObject()->addChild(m_sparrowshell);
new_scene->getRootObject()->addChild(m_editor); new_scene->getRootObject()->addChild(m_editor);
new_scene->getRootObject()->addChild(m_keymapper);
} }
void Engine::enablePhysicsDebug() void Engine::enablePhysicsDebug()

View File

@ -9,6 +9,7 @@ class SceneTree;
class SparrowShell; class SparrowShell;
class PhysicsDebugNode; class PhysicsDebugNode;
class Editor; class Editor;
class KeyMapper;
class LoadingThread; class LoadingThread;
namespace sf namespace sf
@ -54,6 +55,7 @@ public:
SparrowShell* getShell() const {return m_sparrowshell;} SparrowShell* getShell() const {return m_sparrowshell;}
PhysicsDebugNode* getPhysicsDebug() const {return m_physicsDebugNode;} PhysicsDebugNode* getPhysicsDebug() const {return m_physicsDebugNode;}
Editor* getEditor() const {return m_editor;} Editor* getEditor() const {return m_editor;}
KeyMapper* getKeyMapper() const{return m_keymapper;}
LoadingThread* getLoadingThread() const {return m_loadingThread;} LoadingThread* getLoadingThread() const {return m_loadingThread;}
SceneTree* getScene() const; SceneTree* getScene() const;
@ -81,6 +83,7 @@ private:
PhysicsDebugNode *m_physicsDebugNode; PhysicsDebugNode *m_physicsDebugNode;
SparrowRenderer* m_renderer; SparrowRenderer* m_renderer;
Editor* m_editor; Editor* m_editor;
KeyMapper* m_keymapper;
LoadingThread* m_loadingThread; LoadingThread* m_loadingThread;
void update(); void update();

206
src/keymapper.cpp Normal file
View File

@ -0,0 +1,206 @@
#include "keymapper.h"
#include <imgui/imgui.h>
#include "engine.h"
#include "SparrowInput/input.h"
#include "defaultkeysmap.h"
#include <iostream>
KeyMapper::KeyMapper() :
m_enabled(false),
m_waiting_for_input(-1)
{
// mapping between action numerical value and text value
m_actions_list[DefaultKeysMap::MAIN_ACTION] = "Main Action";
m_actions_list[DefaultKeysMap::SECONDARY_ACTION] = "Secondary Action";
m_actions_list[DefaultKeysMap::TERTIARY_ACTION] = "Tertiary Action";
m_actions_list[DefaultKeysMap::MOVE_FORWARD] = "Move Forward";
m_actions_list[DefaultKeysMap::MOVE_BACKWARD] = "Move Backward";
m_actions_list[DefaultKeysMap::STRAFE_LEFT] = "Strafe Left";
m_actions_list[DefaultKeysMap::STRAFE_RIGHT] = "Strafe Right";
m_actions_list[DefaultKeysMap::RUN] = "Run";
m_actions_list[DefaultKeysMap::MAIN_ACTION_HOLD] = "Main Action Hold";
m_actions_list[DefaultKeysMap::SECONDARY_ACTION_HOLD] = "Secondary Action Hold";
m_actions_list[DefaultKeysMap::JUMP] = "Jump";
m_actions_list[DefaultKeysMap::TOGGLE_NOCLIP] = "Toggle NoClip";
m_actions_list[DefaultKeysMap::TOGGLE_PHYSICS_DEBUG] = "Toggle Physics Debug";
m_actions_list[DefaultKeysMap::TOGGLE_CONSOLE] = "Toggle Console";
m_actions_list[DefaultKeysMap::MOVE_CURSOR_LEFT] = "Move Cursor Left";
m_actions_list[DefaultKeysMap::MOVE_CURSOR_RIGHT] = "Move Cursor Right";
m_actions_list[DefaultKeysMap::HISTORY_UP] = "History Up";
m_actions_list[DefaultKeysMap::HISTORY_DOWN] = "History Down";
m_actions_list[DefaultKeysMap::TOGGLE_MOUSE_CURSOR] = "Toggle Mouse Cursor";
m_actions_list[DefaultKeysMap::EXIT_GAME] = "Exit Game";
m_actions_list[DefaultKeysMap::LEFT_CLICK] = "Left Click";
// mapping between mouse numerical value and text value
m_mouse_buttons[sf::Mouse::Left] = "Mouse Left";
m_mouse_buttons[sf::Mouse::Middle] = "Mouse Middle";
m_mouse_buttons[sf::Mouse::Right] = "Mouse Right";
m_mouse_buttons[sf::Mouse::XButton1] = "Mouse Extra 1";
m_mouse_buttons[sf::Mouse::XButton2] = "Mouse Extra 2";
// mapping between keyboard numerical value and text value
m_keyboard_keys[sf::Keyboard::A] = "A";
m_keyboard_keys[sf::Keyboard::Z] = "Z";
m_keyboard_keys[sf::Keyboard::E] = "E";
m_keyboard_keys[sf::Keyboard::R] = "R";
m_keyboard_keys[sf::Keyboard::T] = "T";
m_keyboard_keys[sf::Keyboard::Y] = "Y";
m_keyboard_keys[sf::Keyboard::U] = "U";
m_keyboard_keys[sf::Keyboard::I] = "I";
m_keyboard_keys[sf::Keyboard::O] = "O";
m_keyboard_keys[sf::Keyboard::P] = "P";
m_keyboard_keys[sf::Keyboard::Q] = "Q";
m_keyboard_keys[sf::Keyboard::S] = "S";
m_keyboard_keys[sf::Keyboard::D] = "D";
m_keyboard_keys[sf::Keyboard::F] = "F";
m_keyboard_keys[sf::Keyboard::G] = "G";
m_keyboard_keys[sf::Keyboard::H] = "H";
m_keyboard_keys[sf::Keyboard::J] = "J";
m_keyboard_keys[sf::Keyboard::K] = "K";
m_keyboard_keys[sf::Keyboard::L] = "L";
m_keyboard_keys[sf::Keyboard::M] = "M";
m_keyboard_keys[sf::Keyboard::W] = "W";
m_keyboard_keys[sf::Keyboard::X] = "X";
m_keyboard_keys[sf::Keyboard::C] = "C";
m_keyboard_keys[sf::Keyboard::V] = "V";
m_keyboard_keys[sf::Keyboard::B] = "B";
m_keyboard_keys[sf::Keyboard::N] = "N";
m_keyboard_keys[sf::Keyboard::F1] = "F1";
m_keyboard_keys[sf::Keyboard::F2] = "F2";
m_keyboard_keys[sf::Keyboard::F3] = "F3";
m_keyboard_keys[sf::Keyboard::F4] = "F4";
m_keyboard_keys[sf::Keyboard::F5] = "F5";
m_keyboard_keys[sf::Keyboard::F6] = "F6";
m_keyboard_keys[sf::Keyboard::F7] = "F7";
m_keyboard_keys[sf::Keyboard::F8] = "F8";
m_keyboard_keys[sf::Keyboard::F9] = "F9";
m_keyboard_keys[sf::Keyboard::F10] = "F10";
m_keyboard_keys[sf::Keyboard::F11] = "F11";
m_keyboard_keys[sf::Keyboard::F12] = "F12";
m_keyboard_keys[sf::Keyboard::Left] = "Arrow Left";
m_keyboard_keys[sf::Keyboard::Right] = "Arrow Right";
m_keyboard_keys[sf::Keyboard::Up] = "Arrow Up";
m_keyboard_keys[sf::Keyboard::Down] = "Arrow Down";
m_keyboard_keys[sf::Keyboard::Escape]= "Escape";
m_keyboard_keys[sf::Keyboard::Space] = "Space";
m_keyboard_keys[sf::Keyboard::LShift]= "Left Shift";
m_keyboard_keys[sf::Keyboard::LControl] = "Left Control";
}
void KeyMapper::update()
{
if(m_enabled)
gui();
}
void KeyMapper::gui()
{
std::vector<std::string> contexts = getEngine().getInput()->getContextsList();
std::vector<std::string> types_binding = {"pressed","released","hold"};
ImGui::Begin("KeyMapper");
std::vector<Binding>& keys = m_keysmap.data();
for(unsigned int i = 0;i<m_keysmap.data().size();i++)
{
Binding& b = keys[i];
std::string action_label;
if(m_actions_list.count(b.action.action))
action_label = m_actions_list[b.action.action];
else
action_label = "Unknown Action";
std::string key_label;
if (b.action.source == input::MOUSE && m_mouse_buttons.count(b.key))
key_label = m_mouse_buttons[b.key];
else if(b.action.source == input::KEYBOARD && m_keyboard_keys.count(b.key))
key_label = m_keyboard_keys[b.key];
else
key_label = "UNDEFINED";
ImGui::Text("%s",action_label.data());
ImGui::SameLine(200);
ImGui::PushID(action_label.data());
if(m_waiting_for_input == i)
ImGui::Button("...",ImVec2(100,30));
else
{
if(ImGui::Button(key_label.data(),ImVec2(100,30)))
m_waiting_for_input = i;
}
ImGui::SameLine(400);
ImGui::PushItemWidth(100);
if(ImGui::BeginCombo("Type :",types_binding[b.type].data()))
{
for(unsigned int j = 0;j < types_binding.size();j++)
{
bool is_selected = (b.type == j);
if(ImGui::Selectable(types_binding[j].data(),is_selected))
b.type = j;
if(is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
ImGui::PopID();
ImGui::PopItemWidth();
ImGui::Separator();
}
/*if(ImGui::Button("Test 1"))
std::cout << "Test 1" << std::endl;
if(ImGui::Button("Test 1"))
std::cout << "Test 2" << std::endl;*/
if(m_waiting_for_input != -1)
{
ImGuiIO io = ImGui::GetIO();
for(int i = sf::Keyboard::A ; i < sf::Keyboard::KeyCount;i++)
{
if(io.KeysDown[i])
{
keys[m_waiting_for_input].key = i;
keys[m_waiting_for_input].action.source = input::KEYBOARD;
m_waiting_for_input = -1;
}
}
for(int j = sf::Mouse::Left; j < sf::Mouse::ButtonCount;j++)
{
if(io.MouseDown[j])
{
keys[m_waiting_for_input].key = j;
keys[m_waiting_for_input].action.source = input::MOUSE;
m_waiting_for_input = -1;
}
}
}
//apply
if(ImGui::Button("Apply changes")){
getEngine().getInput()->setKeysMap(m_keysmap);
getEngine().getInput()->updateKeyBindings();
}
//save
//load
ImGui::End();
}
void KeyMapper::toggle()
{
m_enabled = !m_enabled;
if(m_enabled){
m_keysmap = getEngine().getInput()->getKeysMap();
}
}

27
src/keymapper.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef KEYMAPPER_H
#define KEYMAPPER_H
#include "scene/containernode.h"
#include "SparrowInput/keybindings.h"
#include <map>
#include <set>
class KeyMapper : public ContainerNode
{
bool m_enabled;
int m_selected_context;
IKeysMap m_keysmap;
std::map<int,std::string> m_mouse_buttons;
std::map<int,std::string> m_keyboard_keys;
std::map<int,std::string> m_actions_list;
int m_waiting_for_input;
void gui();
public:
KeyMapper();
void update();
void toggle();
};
#endif // KEYMAPPER_H

View File

@ -10,6 +10,7 @@
#include "SparrowSerializer/Version.h" #include "SparrowSerializer/Version.h"
#include "tools/utils.h" #include "tools/utils.h"
#include "editor.h" #include "editor.h"
#include "keymapper.h"
#define LUA_DEFINE(var) S[#var]=var #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); #define LUA_SET_FUN(var) m_script.set_function(#var,&ScriptNode::var,this);this->functions_name.push_back(#var);
@ -29,6 +30,7 @@ ScriptNode::ScriptNode()
LUA_SET_FUN(rendering); LUA_SET_FUN(rendering);
LUA_SET_FUN(resourcePackEditor); LUA_SET_FUN(resourcePackEditor);
LUA_SET_FUN(editor); LUA_SET_FUN(editor);
LUA_SET_FUN(keymapper);
m_script.new_usertype<Engine>("Engine", m_script.new_usertype<Engine>("Engine",
"time",&Engine::getTime "time",&Engine::getTime
@ -118,3 +120,7 @@ void ScriptNode::resourcePackEditor(){
void ScriptNode::editor(){ void ScriptNode::editor(){
this->getEngine().getEditor()->toggleEditor(); this->getEngine().getEditor()->toggleEditor();
} }
void ScriptNode::keymapper(){
this->getEngine().getKeyMapper()->toggle();
}

View File

@ -28,6 +28,7 @@ public:
void rendering(); void rendering();
void resourcePackEditor(); void resourcePackEditor();
void editor(); void editor();
void keymapper();
}; };
#endif // SCRIPTNODE_H #endif // SCRIPTNODE_H