git repos / blockattack-game

commit bd788178

sago007 · 2018-03-31 16:03
bd7881788ea448b04080638f8bef1e4af60d357d patch · browse files
parent e7e189c7ca7db0dadaf8885eef5458a5a6cda729

Changed the font for the dialog. Added a width method to SagoTextField

Changed files

M source/code/DialogBox.cpp before
M source/code/DialogBox.hpp before
M source/code/sago/SagoTextField.cpp before
M source/code/sago/SagoTextField.hpp before
diff --git a/source/code/DialogBox.cpp b/source/code/DialogBox.cpp index fac7312..94ed269 100644 --- a/source/code/DialogBox.cpp +++ b/source/code/DialogBox.cpp
@@ -35,10 +35,6 @@ static void setButtonFont(const sago::SagoDataHolder* holder, sago::SagoTextFiel
field.SetText(text);
}
-static void NFont_Write(SDL_Renderer* target, int x, int y, const std::string& text) {
- globalData.standard_blue_font.draw(target, x, y, text);
-}
-
static void DrawRect(SDL_Renderer* target, int topx, int topy, int height, int width, const std::string& name) {
const int size = 32;
SDL_Rect bounds_ns = {topx, topy+size, width, height-2*size}; //bounds for south
@@ -95,6 +91,9 @@ DialogBox::DialogBox(int x, int y, const std::string& name, const std::string& h
setButtonFont(&globalData.spriteHolder->GetDataHolder(), headerLabel, header.c_str());
setButtonFont(&globalData.spriteHolder->GetDataHolder(), enterLabel, _("Enter to accept"));
setButtonFont(&globalData.spriteHolder->GetDataHolder(), cancelLabel, _("Esc to cancel"));
+ sagoTextSetBlueFont(textField);
+ sagoTextSetBlueFont(cursorLabel);
+ cursorLabel.SetText("|");
}
@@ -115,12 +114,16 @@ void DialogBox::Draw(SDL_Renderer* target) {
enterLabel.Draw(target, x+150, y+140, sago::SagoTextField::Alignment::center);
cancelLabel.Draw(target, x+450, y+140, sago::SagoTextField::Alignment::center);
DrawRectWhite(target, x+26, y+64, 54, 600-2*26);
- NFont_Write(target, x+40, y+76,rk->GetString());
+ textField.SetText(rk->GetString().c_str());
+ textField.Draw(target, x+40, y+76);
std::string strHolder = rk->GetString();
strHolder.erase((int)rk->CharsBeforeCursor());
if (((SDL_GetTicks()/600)%2)==1) {
- NFont_Write(target, x+40+globalData.standard_blue_font.getWidth( strHolder),y+76,"|");
+ int width = 0;
+ textField.GetRenderedSize( strHolder.c_str(), &width);
+ width -= 2;
+ cursorLabel.Draw(target, x+40+width,y+76);
}
}
diff --git a/source/code/DialogBox.hpp b/source/code/DialogBox.hpp index 562f8ef..0cf99c4 100644 --- a/source/code/DialogBox.hpp +++ b/source/code/DialogBox.hpp
@@ -55,6 +55,8 @@ private:
sago::SagoTextField headerLabel;
sago::SagoTextField enterLabel;
sago::SagoTextField cancelLabel;
+ sago::SagoTextField textField;
+ sago::SagoTextField cursorLabel;
};
#endif /* DIALOGBOX_HPP */
diff --git a/source/code/sago/SagoTextField.cpp b/source/code/sago/SagoTextField.cpp index 52d019f..d38382b 100644 --- a/source/code/sago/SagoTextField.cpp +++ b/source/code/sago/SagoTextField.cpp
@@ -164,8 +164,6 @@ void SagoTextField::UpdateCache(SDL_Renderer* target) {
TTF_Font *font = data->tex->getFontPtr(data->fontName, data->fontSize);
data->textSurface = TTF_RenderUTF8_Blended (font, data->text.c_str(), data->color);
data->texture = SDL_CreateTextureFromSurface(target, data->textSurface);
- int textWidth = 0;
- SDL_QueryTexture(data->texture, NULL, NULL, &textWidth, NULL);
if (data->outline > 0) {
OutlineHandler oh(font, data->outline);
data->outlineTextSurface = TTF_RenderUTF8_Blended (font, data->text.c_str(), data->outlineColor);
@@ -176,6 +174,20 @@ void SagoTextField::UpdateCache(SDL_Renderer* target) {
data->renderedVersion = data->tex->getVersion();
}
+void SagoTextField::GetRenderedSize(const char* text, int* w, int* h) {
+ TTF_Font *font = data->tex->getFontPtr(data->fontName, data->fontSize);
+ int ret = TTF_SizeUTF8(font, text, w, h);
+ if (ret) {
+ if (w) {
+ w = 0;
+ }
+ if (h) {
+ h = 0;
+ }
+ std::cerr << "GetRenderedSize failed to find size of " << text << ". Error code: " << ret << "\n";
+ }
+}
+
void SagoTextField::Draw(SDL_Renderer* target, int x, int y, Alignment alignment, VerticalAlignment verticalAlignment) {
if (data->text.empty()) {
return;
diff --git a/source/code/sago/SagoTextField.hpp b/source/code/sago/SagoTextField.hpp index f10af0b..188cf2e 100644 --- a/source/code/sago/SagoTextField.hpp +++ b/source/code/sago/SagoTextField.hpp
@@ -73,6 +73,7 @@ public:
* @return The text
*/
const std::string& GetText() const;
+ void GetRenderedSize(const char* text, int* w = nullptr, int* h = nullptr);
enum class Alignment { left = 0, right=1, 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);