From 2e50d11e07375c7cdcec7e1689192f877dfd5465 Mon Sep 17 00:00:00 2001 From: Remco Date: Mon, 20 Jun 2016 13:29:28 +0200 Subject: [PATCH] able to click with cursor, button detect clicking event --- Button.cpp | 4 ++++ CrystalPoint.cpp | 4 ++-- Cursor.cpp | 8 ++++++++ Cursor.h | 5 +++++ Main.cpp | 14 ++++++++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Button.cpp b/Button.cpp index 004820c..9b02b3c 100644 --- a/Button.cpp +++ b/Button.cpp @@ -2,6 +2,7 @@ #include #include "Util.h" #include "Vector.h" +#include "Cursor.h" Button::Button(const std::string & text, Vec2f position, float width, float height) : Text(text,position) { @@ -51,6 +52,9 @@ void Button::update(int x, int y) alfa = 1.0f; else alfa = 0.5f; + + if (cursorOnButton && Cursor::getInstance()->clicked) + this->setColor(Vec3f(0, 255, 0)); } void Button::setForeground(Vec3f color) diff --git a/CrystalPoint.cpp b/CrystalPoint.cpp index 7c115f8..9d835d2 100644 --- a/CrystalPoint.cpp +++ b/CrystalPoint.cpp @@ -25,7 +25,7 @@ void CrystalPoint::init() menu = new Menu(); menu->AddMenuElement(new Text("Hello", Vec2f(10, 10))); menu->AddMenuElement(new Button("Start", Vec2f(1920 / 2 - 50, 1080 / 2 - 25), 100, 50)); - + menu->AddMenuElement(new Button("Test", Vec2f(1920 / 2 - 50, 1080 / 2 - 100), 100, 50)); lastFrameTime = 0; glClearColor(0.7f, 0.7f, 1.0f, 1.0f); @@ -92,8 +92,8 @@ void CrystalPoint::update() //worldhandler->update(deltaTime); - cursor->update(cursor->mousePosition + mouseOffset); menu->update(); + cursor->update(cursor->mousePosition + mouseOffset); mouseOffset = Vec2f(0, 0); prevKeyboardState = keyboardState; diff --git a/Cursor.cpp b/Cursor.cpp index 39613af..25e39e6 100644 --- a/Cursor.cpp +++ b/Cursor.cpp @@ -9,6 +9,7 @@ Cursor::Cursor() { enabled = false; mousePosition = Vec2f(CrystalPoint::width / 2, CrystalPoint::height / 2); + clicked = false; } Cursor::~Cursor() @@ -68,4 +69,11 @@ void Cursor::update(Vec2f newPosition) newPosition.y = CrystalPoint::height; mousePosition = newPosition; + + if (clicked) + clicked = !clicked; + if (state != prev) + if(state == GLUT_UP) + clicked = true; + prev = state; } diff --git a/Cursor.h b/Cursor.h index 3bc3044..dd0835e 100644 --- a/Cursor.h +++ b/Cursor.h @@ -8,6 +8,7 @@ private: static Cursor* instance; bool enabled; + public: Vec2f mousePosition; ~Cursor(); @@ -17,6 +18,10 @@ public: void enable(bool enable); bool isEnabled(void); + bool clicked; + int state, prev; + + void draw(void); void update(Vec2f newPosition); }; diff --git a/Main.cpp b/Main.cpp index 740ea80..3c4649c 100644 --- a/Main.cpp +++ b/Main.cpp @@ -7,6 +7,8 @@ #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" +#include "Cursor.h" + void configureOpenGL(void); CrystalPoint* app; @@ -53,6 +55,17 @@ int main(int argc, char* argv[]) glutPassiveMotionFunc(mousemotion); glutMotionFunc(mousemotion); + auto mouseclick = [](int button, int state, + int x, int y) + { + if (button == GLUT_LEFT_BUTTON) + Cursor::getInstance()->state = state; + + //std::cout << "Left button is down" << std::endl; + }; + + glutMouseFunc(mouseclick); + CrystalPoint::height = GLUT_WINDOW_HEIGHT; CrystalPoint::width = GLUT_WINDOW_WIDTH; @@ -67,6 +80,7 @@ void configureOpenGL() //Init window and glut display mode glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(800, 600); + //glutInitWindowPosition(1000,800); glutCreateWindow("Crystal Point"); glutFullScreen();