[ADD] menu now works

menu switches to next item when hand is closed and calls OnClick() when hand is closed
This commit is contained in:
Jasper
2021-06-04 16:32:26 +02:00
parent 7cc918fdf8
commit b3412e414e
8 changed files with 171 additions and 72 deletions

View File

@@ -60,8 +60,8 @@ namespace computervision
imshow("camera", cameraFrame); imshow("camera", cameraFrame);
imshow("output", frameOut); imshow("output", frameOut);
imshow("foreground", foreground); //imshow("foreground", foreground);
imshow("handMask", handMask); //imshow("handMask", handMask);
imshow("handDetection", fingerCountDebug); imshow("handDetection", fingerCountDebug);
int key = waitKey(1); int key = waitKey(1);

View File

@@ -17,7 +17,6 @@ namespace gui
* position = The center position of the gui * position = The center position of the gui
* scale = The size (scale) of the gui * scale = The size (scale) of the gui
*/ */
struct GuiTexture struct GuiTexture
{ {
int texture; int texture;
@@ -27,10 +26,9 @@ namespace gui
virtual GuiType GetType() { virtual GuiType GetType() {
return GuiType::LABEL; return GuiType::LABEL;
} }
GuiTexture(int texture, glm::vec2 position, glm::vec2 scale): texture(texture), position(position), scale(scale) GuiTexture(int texture, glm::vec2 position, glm::vec2 scale): texture(texture), position(position), scale(scale)
{ {
scale.x /= (WINDOW_WIDTH / WINDOW_HEIGT); scale.x /= (WINDOW_WIDTH / WINDOW_HEIGHT);
} }
}; };
} }

View File

@@ -2,6 +2,8 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "gui_interactable.h" #include "gui_interactable.h"
#include <iostream>
namespace gui namespace gui
{ {
InteractableGui::InteractableGui(int default_texture, glm::vec2 position, glm::vec2 scale) InteractableGui::InteractableGui(int default_texture, glm::vec2 position, glm::vec2 scale)
@@ -41,6 +43,35 @@ namespace gui
} }
} }
void InteractableGui::ForceClick( int mouseButton)
{
if(mouseButton == GLFW_MOUSE_BUTTON_LEFT)
{
if (clicked_texture != 0)
{
texture = clicked_texture;
}
else
{
texture = default_texture;
}
if (!is_clicking)
{
OnClick();
is_clicking = true;
}
}
else
{
if (is_clicking)
{
is_clicking = false;
}
}
}
bool InteractableGui::IsHoveringAbove(GLFWwindow* window) bool InteractableGui::IsHoveringAbove(GLFWwindow* window)
{ {
double x_pos, y_pos; double x_pos, y_pos;

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <iostream>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include "../toolbox/toolbox.h" #include "../toolbox/toolbox.h"
#include "gui_element.h" #include "gui_element.h"
@@ -32,6 +33,14 @@ namespace gui
*/ */
void Update(GLFWwindow* window); void Update(GLFWwindow* window);
/*
* @brief: Call this function when you want to perform a mouseclick
*
* @param mousebutton: mouseButton you want to perform the click on
*/
void ForceClick(int mouseButton);
/* /*
* @brief: This function gets called when the InteractabeGui is clicked * @brief: This function gets called when the InteractabeGui is clicked
*/ */
@@ -50,7 +59,10 @@ namespace gui
/* /*
* @brief: This function sets the texture of the InteractableGUI for when the InteractableGUI is clicked * @brief: This function sets the texture of the InteractableGUI for when the InteractableGUI is clicked
*/ */
void SetClickedTexture(int texture) { clicked_texture = texture; } void SetClickedTexture(int texture)
{
clicked_texture = texture;
}
/* /*
* @brief: This function sets the texture of the InteractableGUI for when the mouse is hovering above the InteractableGUI * @brief: This function sets the texture of the InteractableGUI for when the mouse is hovering above the InteractableGUI

View File

@@ -25,9 +25,6 @@
#include "scenes/in_Game_Scene.h" #include "scenes/in_Game_Scene.h"
#include "scenes/startup_Scene.h" #include "scenes/startup_Scene.h"
#include "computervision/MenuTest.h"
#include "computervision/ObjectDetection.h"
#pragma comment(lib, "glfw3.lib") #pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "glew32s.lib") #pragma comment(lib, "glew32s.lib")
#pragma comment(lib, "opengl32.lib") #pragma comment(lib, "opengl32.lib")
@@ -35,7 +32,7 @@
static double UpdateDelta(); static double UpdateDelta();
static GLFWwindow* window; static GLFWwindow* window;
int chosen_item = 0;
scene::Scene* current_scene; scene::Scene* current_scene;
int main(void) int main(void)
@@ -43,7 +40,7 @@ int main(void)
#pragma region OPENGL_SETTINGS #pragma region OPENGL_SETTINGS
if (!glfwInit()) if (!glfwInit())
throw "Could not inditialize glwf"; throw "Could not inditialize glwf";
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGT, "SDBA", NULL, NULL); window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "SDBA", NULL, NULL);
if (!window) if (!window)
{ {
glfwTerminate(); glfwTerminate();
@@ -69,8 +66,7 @@ int main(void)
bool window_open = true; bool window_open = true;
computervision::ObjectDetection objDetect;
cv::Mat cameraFrame;
// Main game loop // Main game loop
while (!glfwWindowShouldClose(window) && window_open) while (!glfwWindowShouldClose(window) && window_open)
@@ -100,56 +96,6 @@ int main(void)
break; break;
} }
////////////////////////// KIMS SHIT ////////////////////////////////////
cameraFrame = objDetect.readCamera();
computervision::MenuTest menu_test;
//Get hand state from camera
bool hand_detection = objDetect.detectHand(cameraFrame);
if (hand_detection)
{
std::cout << "hand is opened" << std::endl;
//Loop through menu items
chosen_item = menu_test.GetMenuItem(true);
//For debug only, to see if chosen item is selected properly when hand is opened
std::cout << "chosen item: " << chosen_item << std::endl;
}
else if (!hand_detection)
{
//for debug only, to see if the chosen item is selected properly when hand is closed
std::cout << "hand is closed" << std::endl;
//std::cout << "item to start: " << chosen_item << std::endl;
//TODO link chosen item to the correct game states
switch (chosen_item)
{
case 1:
//Game state 0
std::cout << "in case: " << chosen_item << std::endl;
break;
case 2:
//Game state 1
std::cout << "in case: " << chosen_item << std::endl;
break;
case 3:
//Game state 2
std::cout << "in case: " << chosen_item << std::endl;
break;
case 4:
//Game state 3
std::cout << "in case: " << chosen_item << std::endl;
default:
break;
}
}
///////////////////////// END OF KIMS SHIT ///////////////////////////////
// Finish up // Finish up
//shader.Stop(); //shader.Stop();
glfwSwapBuffers(window); glfwSwapBuffers(window);

View File

@@ -26,7 +26,7 @@ namespace render_engine
glCullFace(GL_BACK); glCullFace(GL_BACK);
const glm::mat4 projectionMatrix = const glm::mat4 projectionMatrix =
glm::perspective(glm::radians(FOV), (WINDOW_WIDTH / WINDOW_HEIGT), NEAR_PLANE, FAR_PLANE); glm::perspective(glm::radians(FOV), (WINDOW_WIDTH / WINDOW_HEIGHT), NEAR_PLANE, FAR_PLANE);
// Load the projectionmatrix into the shader // Load the projectionmatrix into the shader
shader.Start(); shader.Start();

View File

@@ -3,6 +3,8 @@
#include <map> #include <map>
#include "startup_Scene.h" #include "startup_Scene.h"
#include <iostream> #include <iostream>
#include <opencv2/core/mat.hpp>
#include "../models/model.h" #include "../models/model.h"
#include "../renderEngine/loader.h" #include "../renderEngine/loader.h"
#include "../renderEngine/obj_loader.h" #include "../renderEngine/obj_loader.h"
@@ -10,6 +12,9 @@
#include "../shaders/entity_shader.h" #include "../shaders/entity_shader.h"
#include "../gui/gui_interactable.h" #include "../gui/gui_interactable.h"
#include "../toolbox/toolbox.h" #include "../toolbox/toolbox.h"
#include "../computervision/MenuTest.h"
#include "../computervision/ObjectDetection.h"
@@ -18,6 +23,9 @@ namespace scene
shaders::GuiShader* gui_shader1; shaders::GuiShader* gui_shader1;
std::vector<gui::GuiTexture*> guis1; std::vector<gui::GuiTexture*> guis1;
float item_number = 0;
Startup_Scene::Startup_Scene() { Startup_Scene::Startup_Scene() {
shaders::EntityShader shader; shaders::EntityShader shader;
shader.Init(); shader.Init();
@@ -29,12 +37,42 @@ namespace scene
} }
gui::Button* ConvertGuiTextureToButton(gui::GuiTexture* texture) { gui::Button* ConvertGuiTextureToButton(gui::GuiTexture* texture) {
if (texture->GetType() == gui::GuiType::BUTTON) { if (texture != NULL)
return (gui::Button*)texture; if (texture->GetType() == gui::GuiType::BUTTON) {
}
else { gui::Button* button = (gui::Button*)texture;
return NULL; std::cout << button->clicked_texture << std::endl;
return button;
}
else {
return NULL;
}
}
/*gui::InteractableGui* ConvertGuiTextureToInteractableGui(gui::GuiTexture* texture) {
if (texture != NULL)
if (texture->GetType() == gui::GuiType::BUTTON) {
return (gui::InteractableGui*)texture;
}
else {
return NULL;
}
}*/
gui::GuiTexture* GetMenuItem(bool hand_state) {
if(hand_state)
item_number += 0.20f;
int temp_item_number = item_number;
//If temp_item_number is equal to the size of the array, set item_number bac to zero to loop through the array again
if (temp_item_number == guis1.size()) {
item_number = 0;
temp_item_number = 0;
} }
std::cout << guis1[temp_item_number]->texture << std::endl;
return guis1[temp_item_number];
} }
scene::Scenes scene::Startup_Scene::start(GLFWwindow *window) scene::Scenes scene::Startup_Scene::start(GLFWwindow *window)
@@ -46,6 +84,7 @@ namespace scene
button_start.SetOnClickAction([]() button_start.SetOnClickAction([]()
{ {
std::cout << "Clicked on button: Start!" << std::endl; std::cout << "Clicked on button: Start!" << std::endl;
}); });
guis1.push_back(&button_start); guis1.push_back(&button_start);
@@ -55,6 +94,7 @@ namespace scene
button_calibrate.SetOnClickAction([]() button_calibrate.SetOnClickAction([]()
{ {
std::cout << "Clicked on button: Calibrate!" << std::endl; std::cout << "Clicked on button: Calibrate!" << std::endl;
}); });
guis1.push_back(&button_calibrate); guis1.push_back(&button_calibrate);
@@ -67,6 +107,10 @@ namespace scene
}); });
guis1.push_back(&button_quit); guis1.push_back(&button_quit);
computervision::ObjectDetection objDetect;
cv::Mat cameraFrame;
gui::GuiTexture* chosen_item = NULL;
while (return_value == scene::Scenes::STARTUP) while (return_value == scene::Scenes::STARTUP)
{ {
render(); render();
@@ -77,6 +121,74 @@ namespace scene
new_button->Update(window); new_button->Update(window);
} }
////////////////////////// KIMS SHIT ////////////////////////////////////
cameraFrame = objDetect.readCamera();
//computervision::MenuTest menu_test;
//Get hand state from camera
bool hand_detection = objDetect.detectHand(cameraFrame);
if (hand_detection)
{
std::cout << "hand is opened" << std::endl;
//Loop through menu items
chosen_item = GetMenuItem(true);
//For debug only, to see if chosen item is selected properly when hand is opened
std::cout << "chosen item: " << chosen_item << std::endl;
gui::Button* new_button = ConvertGuiTextureToButton(chosen_item);
if (new_button != NULL) {
//Set hover texture of button selected
const float x_pos = (chosen_item->position.x + 1.0)*WINDOW_WIDTH/2;
const float y_pos = (1.0 - chosen_item->position.y)*WINDOW_HEIGHT/2;
glfwSetCursorPos(window, x_pos, y_pos);
std::cout << "Cursor pos: " << x_pos << "::" << y_pos << std::endl;
}
}
else if (!hand_detection)
{
//for debug only, to see if the chosen item is selected properly when hand is closed
std::cout << "hand is closed" << std::endl;
//std::cout << "item to start: " << chosen_item << std::endl;
/*
//TODO link chosen item to the correct game states
switch (chosen_item)
{
case 1:
//Game state 0
std::cout << "in case: " << chosen_item << std::endl;
break;
case 2:
//Game state 1
std::cout << "in case: " << chosen_item << std::endl;
break;
case 3:
//Game state 2
std::cout << "in case: " << chosen_item << std::endl;
break;
case 4:
//Game state 3
std::cout << "in case: " << chosen_item << std::endl;
default:
break;
}
*/
//Click on button selected in guis1
chosen_item = GetMenuItem(false);
std::cout << chosen_item->texture << std::endl;
gui::Button* new_button = ConvertGuiTextureToButton(chosen_item);
if (new_button != NULL) {
//Run function click
new_button->ForceClick(GLFW_MOUSE_BUTTON_LEFT);
}
}
///////////////////////// END OF KIMS SHIT ///////////////////////////////
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();

View File

@@ -11,10 +11,10 @@ namespace toolbox
// Change these macros to change the window size // Change these macros to change the window size
#define WINDOW_WIDTH 1400.0f #define WINDOW_WIDTH 1400.0f
#define WINDOW_HEIGT 800.0f #define WINDOW_HEIGHT 800.0f
#define SCALED_WIDTH (WINDOW_WIDTH/DEFAULT_WIDTH) #define SCALED_WIDTH (WINDOW_WIDTH/DEFAULT_WIDTH)
#define SCALED_HEIGHT (WINDOW_HEIGT/DEFAULT_HEIGHT) #define SCALED_HEIGHT (WINDOW_HEIGHT/DEFAULT_HEIGHT)
// //
/* /*