commit d91e7363
Now draws the menu text as a SagoFontField
Changed files
| M | source/code/MenuSystem.cpp before |
| M | source/code/MenuSystem.h before |
| M | source/code/sago/SagoTextField.cpp before |
| M | source/code/sago/SagoTextField.hpp before |
diff --git a/source/code/MenuSystem.cpp b/source/code/MenuSystem.cpp
index dd4aa3c..dfbf279 100644
--- a/source/code/MenuSystem.cpp
+++ b/source/code/MenuSystem.cpp
@@ -49,6 +49,21 @@ void ButtonGfx::setSurfaces() {
}
}
+sago::SagoTextField* ButtonGfx::getLabel(const std::string& text) {
+ const auto& theLabel = labels.find(text);
+ if (theLabel != labels.end()) {
+ return labels[text];
+ }
+ sago::SagoTextField* newField = new sago::SagoTextField();
+ newField->SetHolder(&globalData.spriteHolder->GetDataHolder());
+ newField->SetFont("freeserif");
+ newField->SetFontSize(24);
+ newField->SetOutline(2, {0,0,0,255});
+ newField->SetText(text.c_str());
+ labels[text] = newField;
+ return labels[text];
+}
+
Button::Button() {
label = "";
marked = false;
@@ -98,7 +113,9 @@ static void drawToScreen(const Button& b) {
globalData.spriteHolder->GetSprite(menu_unmarked).Draw(globalData.screen, SDL_GetTicks(), b.x, b.y);
}
- standardButton.thefont->drawCenterAlsoHeight(globalData.screen, b.x+standardButton.xsize/2,b.y+standardButton.ysize/2, b.label);
+ standardButton.getLabel(b.label)->Draw(globalData.screen, b.x+standardButton.xsize/2,b.y+standardButton.ysize/2,
+ sago::SagoTextField::Alignment::center, sago::SagoTextField::VerticalAlignment::center);
+ //standardButton.thefont->drawCenterAlsoHeight(globalData.screen, b.x+standardButton.xsize/2,b.y+standardButton.ysize/2, b.label);
}
diff --git a/source/code/MenuSystem.h b/source/code/MenuSystem.h
index e95cd02..fa91362 100644
--- a/source/code/MenuSystem.h
+++ b/source/code/MenuSystem.h
@@ -27,12 +27,14 @@ http://blockattack.net
#include <string>
#include "SDL.h"
+#include <map>
#include <vector>
#include "Libs/NFont.h"
#include "sago/SagoSprite.hpp"
#include "sago/GameStateInterface.hpp"
#include <memory>
#include "FontWrapper.hpp"
+#include "sago/SagoTextField.hpp"
//The ButtonGfx object hold common media for all buttons, so we can reskin them by only changeing one pointer
struct ButtonGfx
@@ -42,7 +44,10 @@ struct ButtonGfx
int ysize = 0;
//A TTFont used for writing the label on the buttons
FontWrapper* thefont;
+ sago::SagoTextField* getLabel(const std::string& text);
void setSurfaces();
+private:
+ std::map<std::string, sago::SagoTextField*> labels;
};
extern ButtonGfx standardButton;
diff --git a/source/code/sago/SagoTextField.cpp b/source/code/sago/SagoTextField.cpp
index 831d3f1..a6c795b 100644
--- a/source/code/sago/SagoTextField.cpp
+++ b/source/code/sago/SagoTextField.cpp
@@ -83,6 +83,16 @@ SagoTextField::SagoTextField(SagoTextField&& o) noexcept {
o.data = nullptr;
}
+SagoTextField& SagoTextField::operator=(const SagoTextField& base) {
+ data = base.data;
+ //Copy all data but do not reuse the cache as it would result in a double free
+ data->outlineTextSurface = nullptr;
+ data->outlineTexture = nullptr;
+ data->textSurface = nullptr;
+ data->texture = nullptr;
+ return *this;
+}
+
SagoTextField::~SagoTextField() {
if(!data) {
return;
@@ -160,7 +170,7 @@ void SagoTextField::UpdateCache(SDL_Renderer* target) {
data->renderedVersion = data->tex->getVersion();
}
-void SagoTextField::Draw(SDL_Renderer* target, int x, int y) {
+void SagoTextField::Draw(SDL_Renderer* target, int x, int y, Alignment alignment, VerticalAlignment verticalAlignment) {
if (data->text.empty()) {
return;
}
@@ -173,6 +183,12 @@ void SagoTextField::Draw(SDL_Renderer* target, int x, int y) {
int texW = 0;
int texH = 0;
SDL_QueryTexture(data->texture, NULL, NULL, &texW, &texH);
+ if (alignment == Alignment::center) {
+ x -= texW/2;
+ }
+ if (verticalAlignment == VerticalAlignment::center) {
+ y -= texH/2;
+ }
SDL_Rect dstrect = { x, y, texW, texH };
if (data->outlineTexture) {
int outlineTexW = 0;
diff --git a/source/code/sago/SagoTextField.hpp b/source/code/sago/SagoTextField.hpp
index ab05c49..ad1a35c 100644
--- a/source/code/sago/SagoTextField.hpp
+++ b/source/code/sago/SagoTextField.hpp
@@ -43,6 +43,7 @@ public:
SagoTextField();
SagoTextField(SagoTextField&& o) noexcept;
SagoTextField& operator=(const SagoTextField&& base) = delete;
+ SagoTextField& operator=(const SagoTextField& base);
virtual ~SagoTextField();
/**
* Sets the data holder. This is MANDATORY
@@ -72,7 +73,9 @@ public:
* @return The text
*/
const std::string& GetText() const;
- void Draw(SDL_Renderer* target, int x, int y);
+ enum class Alignment { left = 0, center = 2 };
+ enum class VerticalAlignment { top = 0, center = 1};
+ void Draw(SDL_Renderer* target, int x, int y, Alignment alignment = Alignment::left, VerticalAlignment verticalAlignment = VerticalAlignment::top);
/**
* Updates the cache.
* You normally do not want to call this from the outside as it is done just in time.
@@ -88,7 +91,6 @@ public:
void ClearCache();
private:
SagoTextField(const SagoTextField& orig) = delete;
- SagoTextField& operator=(const SagoTextField& base) = delete;
struct SagoTextFieldData;
SagoTextFieldData *data;
};