able to click with cursor, button detect clicking event
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include "Vector.h"
|
#include "Vector.h"
|
||||||
|
#include "Cursor.h"
|
||||||
|
|
||||||
Button::Button(const std::string & text, Vec2f position, float width, float height) : Text(text,position)
|
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;
|
alfa = 1.0f;
|
||||||
else
|
else
|
||||||
alfa = 0.5f;
|
alfa = 0.5f;
|
||||||
|
|
||||||
|
if (cursorOnButton && Cursor::getInstance()->clicked)
|
||||||
|
this->setColor(Vec3f(0, 255, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Button::setForeground(Vec3f color)
|
void Button::setForeground(Vec3f color)
|
||||||
|
|||||||
+2
-2
@@ -25,7 +25,7 @@ void CrystalPoint::init()
|
|||||||
menu = new Menu();
|
menu = new Menu();
|
||||||
menu->AddMenuElement(new Text("Hello", Vec2f(10, 10)));
|
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("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;
|
lastFrameTime = 0;
|
||||||
|
|
||||||
glClearColor(0.7f, 0.7f, 1.0f, 1.0f);
|
glClearColor(0.7f, 0.7f, 1.0f, 1.0f);
|
||||||
@@ -92,8 +92,8 @@ void CrystalPoint::update()
|
|||||||
|
|
||||||
//worldhandler->update(deltaTime);
|
//worldhandler->update(deltaTime);
|
||||||
|
|
||||||
cursor->update(cursor->mousePosition + mouseOffset);
|
|
||||||
menu->update();
|
menu->update();
|
||||||
|
cursor->update(cursor->mousePosition + mouseOffset);
|
||||||
|
|
||||||
mouseOffset = Vec2f(0, 0);
|
mouseOffset = Vec2f(0, 0);
|
||||||
prevKeyboardState = keyboardState;
|
prevKeyboardState = keyboardState;
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ Cursor::Cursor()
|
|||||||
{
|
{
|
||||||
enabled = false;
|
enabled = false;
|
||||||
mousePosition = Vec2f(CrystalPoint::width / 2, CrystalPoint::height / 2);
|
mousePosition = Vec2f(CrystalPoint::width / 2, CrystalPoint::height / 2);
|
||||||
|
clicked = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor::~Cursor()
|
Cursor::~Cursor()
|
||||||
@@ -68,4 +69,11 @@ void Cursor::update(Vec2f newPosition)
|
|||||||
newPosition.y = CrystalPoint::height;
|
newPosition.y = CrystalPoint::height;
|
||||||
|
|
||||||
mousePosition = newPosition;
|
mousePosition = newPosition;
|
||||||
|
|
||||||
|
if (clicked)
|
||||||
|
clicked = !clicked;
|
||||||
|
if (state != prev)
|
||||||
|
if(state == GLUT_UP)
|
||||||
|
clicked = true;
|
||||||
|
prev = state;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ private:
|
|||||||
|
|
||||||
static Cursor* instance;
|
static Cursor* instance;
|
||||||
bool enabled;
|
bool enabled;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Vec2f mousePosition;
|
Vec2f mousePosition;
|
||||||
~Cursor();
|
~Cursor();
|
||||||
@@ -17,6 +18,10 @@ public:
|
|||||||
void enable(bool enable);
|
void enable(bool enable);
|
||||||
bool isEnabled(void);
|
bool isEnabled(void);
|
||||||
|
|
||||||
|
bool clicked;
|
||||||
|
int state, prev;
|
||||||
|
|
||||||
|
|
||||||
void draw(void);
|
void draw(void);
|
||||||
void update(Vec2f newPosition);
|
void update(Vec2f newPosition);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#include "stb_image.h"
|
#include "stb_image.h"
|
||||||
|
|
||||||
|
#include "Cursor.h"
|
||||||
|
|
||||||
void configureOpenGL(void);
|
void configureOpenGL(void);
|
||||||
|
|
||||||
CrystalPoint* app;
|
CrystalPoint* app;
|
||||||
@@ -53,6 +55,17 @@ int main(int argc, char* argv[])
|
|||||||
glutPassiveMotionFunc(mousemotion);
|
glutPassiveMotionFunc(mousemotion);
|
||||||
glutMotionFunc(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::height = GLUT_WINDOW_HEIGHT;
|
||||||
CrystalPoint::width = GLUT_WINDOW_WIDTH;
|
CrystalPoint::width = GLUT_WINDOW_WIDTH;
|
||||||
|
|
||||||
@@ -67,6 +80,7 @@ void configureOpenGL()
|
|||||||
//Init window and glut display mode
|
//Init window and glut display mode
|
||||||
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
|
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
|
||||||
glutInitWindowSize(800, 600);
|
glutInitWindowSize(800, 600);
|
||||||
|
//glutInitWindowPosition(1000,800);
|
||||||
glutCreateWindow("Crystal Point");
|
glutCreateWindow("Crystal Point");
|
||||||
glutFullScreen();
|
glutFullScreen();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user