浏览代码

able to click with cursor, button detect clicking event

Remco 9 年之前
父节点
当前提交
2e50d11e07
共有 5 个文件被更改,包括 33 次插入2 次删除
  1. 4 0
      Button.cpp
  2. 2 2
      CrystalPoint.cpp
  3. 8 0
      Cursor.cpp
  4. 5 0
      Cursor.h
  5. 14 0
      Main.cpp

+ 4 - 0
Button.cpp

@@ -2,6 +2,7 @@
 #include <string>
 #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)

+ 2 - 2
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;

+ 8 - 0
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;
 }

+ 5 - 0
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);
 };

+ 14 - 0
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();