Explorar o código

button, text is placed in middle of button, plane is placed behind text

Remco %!s(int64=9) %!d(string=hai) anos
pai
achega
8dcee70324
Modificáronse 7 ficheiros con 53 adicións e 26 borrados
  1. 15 11
      Button.cpp
  2. 6 7
      Button.h
  3. 2 2
      CrystalPoint.cpp
  4. 4 2
      Text.cpp
  5. 6 3
      Text.h
  6. 17 0
      Util.cpp
  7. 3 1
      Util.h

+ 15 - 11
Button.cpp

@@ -3,14 +3,18 @@
 #include "Util.h"
 #include "Vector.h"
 
-Button::Button(const std::string & text, Vec2f position, float width, float height) : MenuElement(position)
+Button::Button(const std::string & text, Vec2f position, float width, float height) : Text(text,position)
 {
 	this->width = width;
 	this->height = height;
-	this->text = text;
+	this->planePosition = position;
 
 	background = Vec3f(10, 10, 10);
-	foreground = Vec3f(255, 255, 255);
+	//foreground = Vec3f(255, 255, 255);
+	this->setColor(Vec3f(255, 255, 255));
+
+	this->position.x += width / 2 - textWidth / 2;
+	this->position.y += height / 2 - textHeight / 2;
 }
 
 Button::~Button()
@@ -23,15 +27,15 @@ void Button::draw(void)
 	glColor4f(background.x/255.0f, background.y / 255.0f, background.z / 255.0f, 1.0f);
 
 	glBegin(GL_QUADS);
-	glVertex2f(position.x, position.y);
-	glVertex2f(position.x, position.y + height);
-	glVertex2f(position.x + width, position.y + height);
-	glVertex2f(position.x + width, position.y);
+	glVertex2f(planePosition.x, planePosition.y);
+	glVertex2f(planePosition.x, planePosition.y + height);
+	glVertex2f(planePosition.x + width, planePosition.y + height);
+	glVertex2f(planePosition.x + width, planePosition.y);
 	glEnd();	
 
-	glColor4f(foreground.x / 255.0f, foreground.y / 255.0f, foreground.z / 255.0f, 1.0f);
-	Util::glutBitmapString(text, position.x, position.y+height/2+18/2);
-
+	/*glColor4f(foreground.x / 255.0f, foreground.y / 255.0f, foreground.z / 255.0f, 1.0f);
+	Util::glutBitmapString(text, position.x, position.y+height/2+14/2);*/
+	Text::draw();
 }
 
 void Button::update(int x, int y)
@@ -41,7 +45,7 @@ void Button::update(int x, int y)
 
 void Button::setForeground(Vec3f color)
 {
-	foreground = color;
+	this->setColor(color);
 }
 
 void Button::setBackground(Vec3f color)

+ 6 - 7
Button.h

@@ -1,18 +1,17 @@
 #pragma once
-#include "MenuElement.h"
+#include "Text.h"
 
-class Button : public MenuElement
+class Button : public Text
 {
-private:
-	std::string text;
-	float width, height;
-	Vec3f foreground;
+private:	
+	float width, height;	
 	Vec3f background;
+	Vec2f planePosition;
 public:
 	Button(const std::string &text, Vec2f position, float width, float height);
 	~Button();
 
-	void draw(void);
+	void draw();
 	void update(int x, int y);
 
 	void setForeground(Vec3f color);

+ 2 - 2
CrystalPoint.cpp

@@ -23,8 +23,8 @@ void CrystalPoint::init()
 	cursor = Cursor::getInstance();
 
 	menu = new Menu();
-	menu->AddMenuElement(new Text("Hello", Vec2f(10, 18)));
-	menu->AddMenuElement(new Button("Start", Vec2f(1920 / 2, 1080 / 2), 100, 50));
+	menu->AddMenuElement(new Text("Hello", Vec2f(10, 10)));
+	menu->AddMenuElement(new Button("Start", Vec2f(1920 / 2 - 50, 1080 / 2 - 25), 100, 50));
 
 	lastFrameTime = 0;
 

+ 4 - 2
Text.cpp

@@ -5,6 +5,8 @@ Text::Text(const std::string &text, Vec2f position) : MenuElement(position)
 {
 	this->text = text;
 	color = Vec3f(50, 150, 150);
+	textHeight = 14;
+	textWidth = Util::glutTextWidth(text);
 }
 
 
@@ -14,8 +16,8 @@ Text::~Text()
 
 void Text::draw()
 {
-	glColor4f(color.x, color.y, color.z, 1.0f);
-	Util::glutBitmapString(text, position.x, position.y);
+	glColor4f(color.x/255.0f, color.y/255.0f, color.z/255.0f, 1.0f);
+	Util::glutBitmapString(text, position.x-1, position.y+textHeight);
 }
 
 void Text::update(int x, int y)

+ 6 - 3
Text.h

@@ -7,13 +7,16 @@ class Text : public MenuElement
 {
 private:
 	std::string text;
-	Vec3f color;
+	Vec3f color;	
+protected:
+	int textWidth;
+	int textHeight;
 public:
 	Text(const std::string &text, Vec2f position);
 	~Text();
 
-	void draw();
-	void update(int x, int y);
+	virtual void draw();
+	virtual void update(int x, int y);
 
 	void setColor(Vec3f color);
 };

+ 17 - 0
Util.cpp

@@ -3,6 +3,7 @@
 
 Util::Util()
 {
+	
 }
 
 
@@ -46,3 +47,19 @@ void Util::glutBitmapString(std::string str, int x, int y)
 		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, str[i]);
 	}
 }
+
+int Util::glutTextWidth(const std::string str)
+{
+	int total = 0;
+	for (int i = 0; i < str.size(); i++)
+	{
+		total += glutBitmapWidth(GLUT_BITMAP_HELVETICA_18, str[i]);
+	}
+	return total;
+}
+
+//int Util::glutTextHeight()
+//{
+//	return glutBitmapHeight(GLUT_BITMAP_HELVETICA_18);
+//}
+

+ 3 - 1
Util.h

@@ -6,9 +6,11 @@ class Util
 {
 private:
 	Util();
-	~Util();
+	~Util();	
 public:
 	static GLuint loadTexture(const std::string &filename);
 	static void glutBitmapString(std::string str, int x, int y);
+	static int glutTextWidth(const std::string str);
+	//static int glutTextHeight();
 };