소스 검색

able to give button a funtion pointer, who will be called when clicked on the button

Remco 9 년 전
부모
커밋
7758f2c294
3개의 변경된 파일23개의 추가작업 그리고 2개의 파일을 삭제
  1. 7 1
      Button.cpp
  2. 8 0
      Button.h
  3. 8 1
      CrystalPoint.cpp

+ 7 - 1
Button.cpp

@@ -54,7 +54,13 @@ void Button::update(int x, int y)
 		alfa = 0.5f;
 
 	if (cursorOnButton && Cursor::getInstance()->clicked)
-		this->setColor(Vec3f(0, 255, 0));
+		if(action != nullptr)
+			action(this);
+}
+
+void Button::addAction(action_function action)
+{
+	this->action = action;
 }
 
 void Button::setForeground(Vec3f color)

+ 8 - 0
Button.h

@@ -1,21 +1,29 @@
 #pragma once
 #include "Text.h"
 
+
+
 class Button : public Text
 {
 private:	
+	typedef void(*action_function)(Button* b);
 	float width, height;	
 	Vec3f background;
 	Vec2f planePosition;
 	bool cursorOnButton;
 	float alfa;
+	action_function action = nullptr;
 public:
 	Button(const std::string &text, Vec2f position, float width, float height);
 	~Button();
 
+
+
 	void draw();
 	void update(int x, int y);
 
+	void addAction(action_function action);
+
 	void setForeground(Vec3f color);
 	void setBackground(Vec3f color);
 };

+ 8 - 1
CrystalPoint.cpp

@@ -25,7 +25,14 @@ 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));
+	Button* b = new Button("Test", Vec2f(1920 / 2 - 50, 1080 / 2 - 100), 100, 50);
+	
+	b->addAction([](Button* b)
+	{
+		b->setColor(Vec3f(0, 255, 0));
+	});
+	menu->AddMenuElement(b);
+
 	lastFrameTime = 0;
 
 	glClearColor(0.7f, 0.7f, 1.0f, 1.0f);