diff --git a/Button.cpp b/Button.cpp index 65e1506..7154605 100644 --- a/Button.cpp +++ b/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) diff --git a/Button.h b/Button.h index fe7e0f7..8da9023 100644 --- a/Button.h +++ b/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); diff --git a/CrystalPoint.cpp b/CrystalPoint.cpp index 02617f4..bac936d 100644 --- a/CrystalPoint.cpp +++ b/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; diff --git a/Text.cpp b/Text.cpp index 9981492..67afd42 100644 --- a/Text.cpp +++ b/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) diff --git a/Text.h b/Text.h index 98a962c..b24bb25 100644 --- a/Text.h +++ b/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); }; diff --git a/Util.cpp b/Util.cpp index e5ebd9c..5f052de 100644 --- a/Util.cpp +++ b/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); +//} + diff --git a/Util.h b/Util.h index 63bd840..54089ee 100644 --- a/Util.h +++ b/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(); };