Added Wren scripting with module system 📚

Added Input pressed, released system
Added interface button support with wren onclick callback
This commit is contained in:
Antoine Pilote
2021-06-09 22:15:53 -04:00
parent aa2ebd04c8
commit 19db72aa22
1185 changed files with 320199 additions and 486 deletions

View File

@@ -3,39 +3,70 @@
#include <GLFW/glfw3.h>
Input* Input::s_Instance;
// TODO: Key press, and release system.
std::map<int, bool> Input::m_Keys = std::map<int, bool>();
bool Input::m_MouseButtons[5] = { false, false, false, false, false };
#pragma region Keys
// Only true if the key is currently being pressed
bool Input::IsKeyDown(int keycode)
{
auto window = Window::Get()->GetHandle();
int state = glfwGetKey(window, keycode);
bool result = state == GLFW_PRESS;
m_Keys[keycode] = state;
return result;
}
// Only true if the key is pressed for the first frame. no repeat.
bool Input::IsKeyPressed(int keycode)
{
auto window = Window::Get()->GetHandle();
int state = glfwGetKey(window, keycode);
bool result = state == GLFW_PRESS;
return state == GLFW_PRESS || state == GLFW_REPEAT;
// First time pressed?
if (m_Keys.find(keycode) == m_Keys.end() || m_Keys[keycode] == false)
{
if (result)
m_Keys[keycode] = true;
return result;
}
return false;
}
bool Input::IsKeyPress(int keycode)
{
auto window = Window::Get()->GetHandle();
int state = glfwGetKey(window, keycode);
return state == GLFW_PRESS;
}
bool Input::IsKeyReleased(int keycode)
{
auto window = Window::Get()->GetHandle();
int state = glfwGetKey(window, keycode);
return state == GLFW_RELEASE;
}
bool result = state == GLFW_RELEASE;
// First time pressed?
if (m_Keys.find(keycode) == m_Keys.end())
return result;
if (result && m_Keys[keycode] == true)
{
return true;
}
return false;
}
#pragma endregion
#pragma region Mouse
// Visibility
void Input::HideMouse() {
auto window = Window::Get()->GetHandle();
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}
// TODO: Rename
bool Input::IsMouseHidden() {
auto window = Window::Get()->GetHandle();
return glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED;
@@ -46,7 +77,9 @@ void Input::ShowMouse() {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
bool Input::IsMouseButtonPressed(int button)
// Action
bool Input::IsMouseButtonDown(int button)
{
auto window = Window::Get()->GetHandle();
auto state = glfwGetMouseButton(window, button);
@@ -54,6 +87,29 @@ bool Input::IsMouseButtonPressed(int button)
return state == GLFW_PRESS;
}
bool Input::IsMouseButtonPressed(int button)
{
auto window = Window::Get()->GetHandle();
auto state = glfwGetMouseButton(window, button);
if (m_MouseButtons[button] == false && state == GLFW_PRESS)
{
m_MouseButtons[button] = true;
return true;
}
return false;
}
bool Input::IsMouseButtonReleased(int button)
{
auto window = Window::Get()->GetHandle();
auto state = glfwGetMouseButton(window, button);
return state == GLFW_RELEASE && m_MouseButtons[button] == true;
}
// Position
float Input::GetMouseX()
{
auto window = Window::Get()->GetHandle();
@@ -74,15 +130,16 @@ float Input::GetMouseY()
return (float)ypos;
}
std::pair<float, float> Input::GetMousePosition()
Vector2 Input::GetMousePosition()
{
auto window = Window::Get()->GetHandle();
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
return { (float)xpos, (float)ypos };
return Vector2(xpos, ypos);
}
#pragma endregion
bool Input::Init()
{
@@ -91,4 +148,20 @@ bool Input::Init()
return false;
}
void Input::Update()
{
// Reset all input to false.
for (auto& k : m_Keys)
{
if(!IsKeyDown(k.first))
k.second = false;
}
for (int i = 0; i < 5; i++)
{
if(!IsMouseButtonDown(i))
m_MouseButtons[i] = false;
}
}