Tanxl_InsertAction.H VERSION_0_3
#ifndef VECTOR
#define VECTOR
#include <vector>
#endif
#ifndef IOSTREAM
#define IOSTREAM
#include <iostream>
#endif
#ifndef GL_GLEW_H
#define GL_GLEW_H
#include <GL\glew.h>
#endif
#ifndef GLFW_GLFW3_H
#define GLFW_GLFW3_H
#include <GLFW\glfw3.h>
#endif
#ifndef STRING
#define STRING
#include <string>
#endif
struct Key_Unit
{
Key_Unit();
Key_Unit(int GLKEY, bool MOVX, bool MOVY, float MOVL);
int GLFW_KEY;
bool MoveToX;
bool MoveToY;
float MoveLen;
};
class InsertEventBase
{
public:
static InsertEventBase& GetInsertBase();
void RegistEvent(Key_Unit KU);
void GetInsert(GLFWwindow* window, float* MoveX, float* MoveY);
private:
std::vector<Key_Unit> KeyEventS;
InsertEventBase();
~InsertEventBase();
InsertEventBase(const InsertEventBase&);
InsertEventBase& operator=(const InsertEventBase&);
};
Tanxl_InsertAction.CPP VERSION_0_3
#include "Tanxl_InsertAction.h"
Key_Unit::Key_Unit() :GLFW_KEY(NULL), MoveToX(false), MoveToY(false), MoveLen(0.0f) {}
Key_Unit::Key_Unit(int GLKEY, bool MOVX, bool MOVY, float MOVL)
: GLFW_KEY(GLKEY), MoveToX(MOVX), MoveToY(MOVY), MoveLen(MOVL) {}
InsertEventBase& InsertEventBase::GetInsertBase()
{
InsertEventBase* IEB = new InsertEventBase;
return *IEB;
}
void InsertEventBase::RegistEvent(Key_Unit KU)
{
KeyEventS.push_back(KU);
}
void InsertEventBase::GetInsert(GLFWwindow* window, float* MoveX, float* MoveY)
{
for (int i = 0; i < KeyEventS.size(); i++)
{
if (glfwGetKey(window, KeyEventS.at(i).GLFW_KEY) == GLFW_PRESS)
{
if (KeyEventS.at(i).MoveToX)
*MoveX += KeyEventS.at(i).MoveLen;
if (KeyEventS.at(i).MoveToY)
*MoveY += KeyEventS.at(i).MoveLen;
std::cout << "BUTTON PUSHED x_" << *MoveX << "y_" << *MoveY << std::endl;
}
}
}
InsertEventBase::InsertEventBase() :KeyEventS(NULL) {}
InsertEventBase::~InsertEventBase()
{
for (int i = 0; i < KeyEventS.size(); i++)
delete &KeyEventS.at(i);
KeyEventS.clear();
}
InsertEventBase::InsertEventBase(const InsertEventBase&) {}
InsertEventBase& InsertEventBase::operator=(const InsertEventBase&)
{
return *this;
}
添加键盘操作示例
Key_Unit MOVE_UP;
MOVE_UP.GLFW_KEY = GLFW_KEY_UP;
MOVE_UP.MoveLen = 0.01f;
MOVE_UP.MoveToY = true;
Key_Unit MOVE_LEFT;
MOVE_LEFT.GLFW_KEY = GLFW_KEY_LEFT;
MOVE_LEFT.MoveLen = -0.01f;
MOVE_LEFT.MoveToX = true;
Key_Unit MOVE_RIGHT;
MOVE_RIGHT.GLFW_KEY = GLFW_KEY_RIGHT;
MOVE_RIGHT.MoveLen = 0.01f;
MOVE_RIGHT.MoveToX = true;
Key_Unit MOVE_DOWN;
MOVE_DOWN.GLFW_KEY = GLFW_KEY_DOWN;
MOVE_DOWN.MoveLen = -0.01f;
MOVE_DOWN.MoveToY = true;
IEB->RegistEvent(MOVE_UP);
IEB->RegistEvent(MOVE_LEFT);
IEB->RegistEvent(MOVE_RIGHT);
IEB->RegistEvent(MOVE_DOWN);
考虑篇幅限制,后期会把整个实现的项目打包上传…
|