Skip to content

Commit

Permalink
Moved the UI text to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed Dec 2, 2023
1 parent c0ae9d4 commit 7fa9ba9
Show file tree
Hide file tree
Showing 39 changed files with 184 additions and 148 deletions.
2 changes: 2 additions & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ add_library(CommonFiles STATIC
ui/MI_TextField.cpp
ui/MI_Button.h
ui/MI_Button.cpp
ui/MI_Text.h
ui/MI_Text.cpp
)

if (USE_PNG_SAVE AND NOT USE_SDL2_LIBS)
Expand Down
7 changes: 0 additions & 7 deletions src/common/ui/MI_Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
#include <string>


enum class TextAlignment: unsigned char {
LEFT,
CENTER,
RIGHT,
};


class MI_Button : public UI_Control {
public:
MI_Button(gfxSprite* nspr, short x, short y, std::string name, short width, TextAlignment align = TextAlignment::LEFT);
Expand Down
34 changes: 34 additions & 0 deletions src/common/ui/MI_Text.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "MI_Text.h"

#include "ResourceManager.h"

extern CResourceManager* rm;


MI_Text::MI_Text(std::string text, short x, short y, short w, bool use_large_font, TextAlignment align)
: UI_Control(x, y)
, szText(std::move(text))
, iw(w)
, m_align(align)
, font(use_large_font ? &rm->menu_font_large : &rm->menu_font_small)
{}

void MI_Text::SetText(std::string text)
{
szText = std::move(text);
}

void MI_Text::Draw()
{
if (!fShow)
return;

if (m_align == TextAlignment::LEFT && iw == 0)
font->draw(ix, iy, szText.c_str());
else if (m_align == TextAlignment::LEFT)
font->drawChopRight(ix, iy, iw, szText.c_str());
else if (m_align == TextAlignment::CENTER)
font->drawCentered(ix, iy, szText.c_str());
else if (m_align == TextAlignment::RIGHT)
font->drawRightJustified(ix, iy, szText.c_str());
}
26 changes: 26 additions & 0 deletions src/common/ui/MI_Text.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include "uicontrol.h"


class MI_Text : public UI_Control {
public:
MI_Text(std::string text, short x, short y, short w, bool use_large_font, TextAlignment align = TextAlignment::LEFT);

void SetText(std::string text);
void Draw() override;

private:
std::string szText;
short iw = 0;
TextAlignment m_align = TextAlignment::LEFT;
gfxFont* font = nullptr;
};


class MI_HeaderText: public MI_Text {
public:
MI_HeaderText(std::string text, short x, short y)
: MI_Text(std::move(text), x, y, 0, true, TextAlignment::CENTER)
{}
};
47 changes: 0 additions & 47 deletions src/common/uicontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,53 +152,6 @@ void MI_Image::Draw()
spr->draw(ix + iXOffset, iy + iYOffset, iXFrame, iYFrame, iw, ih);
}


/**************************************
* MI_Text Class
**************************************/

MI_Text::MI_Text(const char * text, short x, short y, short w, short size, short justified) :
UI_Control(x, y)
{
szText = new char[strlen(text) + 1];
strcpy(szText, text);

iw = w;
iJustified = justified;

if (size == 0)
font = &rm->menu_font_small;
else
font = &rm->menu_font_large;
}

MI_Text::~MI_Text()
{
delete [] szText;
}

void MI_Text::SetText(const char * text)
{
delete [] szText;
szText = new char[strlen(text) + 1];
strcpy(szText, text);
}

void MI_Text::Draw()
{
if (!fShow)
return;

if (iJustified == 0 && iw == 0)
font->draw(ix, iy, szText);
else if (iJustified == 0)
font->drawChopRight(ix, iy, iw, szText);
else if (iJustified == 1)
font->drawCentered(ix, iy, szText);
else if (iJustified == 2)
font->drawRightJustified(ix, iy, szText);
}

/**************************************
* MI_ScoreText Class
**************************************/
Expand Down
24 changes: 8 additions & 16 deletions src/common/uicontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

class UI_Menu;


enum class TextAlignment: unsigned char {
LEFT,
CENTER,
RIGHT,
};


class UI_Control
{
public:
Expand Down Expand Up @@ -193,22 +201,6 @@ class MI_Image : public UI_Control
bool fBlinkShow;
};

class MI_Text : public UI_Control
{
public:
MI_Text(const char * text, short x, short y, short w, short size, short justified);
virtual ~MI_Text();

void SetText(const char * text);
void Draw();

private:
char * szText;
short iw;
short iJustified;
gfxFont * font;
};

class MI_ScoreText : public UI_Control
{
public:
Expand Down
9 changes: 5 additions & 4 deletions src/smw/menu/GameSettingsMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "path.h"
#include "ResourceManager.h"
#include "ui/MI_Button.h"
#include "ui/MI_Text.h"

extern CGameMode * gamemodes[GAMEMODE_LAST];
extern short currentgamemode;
Expand Down Expand Up @@ -77,13 +78,13 @@ UI_GameSettingsMenu::UI_GameSettingsMenu() : UI_Menu()

miGameSettingsLeftHeaderBar = new MI_Image(&rm->menu_plain_field, 0, 0, 0, 0, 320, 32, 1, 1, 0);
miGameSettingsMenuRightHeaderBar = new MI_Image(&rm->menu_plain_field, 320, 0, 192, 0, 320, 32, 1, 1, 0);
miGameSettingsMenuHeaderText = new MI_Text("Single Game Menu", 320, 5, 0, 2, 1);
miGameSettingsMenuHeaderText = new MI_HeaderText("Single Game Menu", 320, 5);


//Exit tournament dialog box
miGameSettingsExitDialogImage = new MI_Image(&rm->spr_dialog, 224, 176, 0, 0, 192, 128, 1, 1, 0);
miGameSettingsExitDialogExitText = new MI_Text("Exit", 320, 195, 0, 2, 1);
miGameSettingsExitDialogTournamentText = new MI_Text("Tournament", 320, 220, 0, 2, 1);
miGameSettingsExitDialogExitText = new MI_HeaderText("Exit", 320, 195);
miGameSettingsExitDialogTournamentText = new MI_HeaderText("Tournament", 320, 220);
miGameSettingsExitDialogYesButton = new MI_Button(&rm->spr_selectfield, 235, 250, "Yes", 80, TextAlignment::CENTER);
miGameSettingsExitDialogNoButton = new MI_Button(&rm->spr_selectfield, 325, 250, "No", 80, TextAlignment::CENTER);

Expand Down Expand Up @@ -129,7 +130,7 @@ UI_GameSettingsMenu::UI_GameSettingsMenu() : UI_Menu()
SetHeadControl(miSettingsStartButton);

SetCancelCode(MENU_CODE_BACK_TEAM_SELECT_MENU);
};
}

UI_GameSettingsMenu::~UI_GameSettingsMenu() {
}
Expand Down
3 changes: 2 additions & 1 deletion src/smw/menu/MatchSelectionMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "GameValues.h"
#include "path.h"
#include "ResourceManager.h"
#include "ui/MI_Text.h"

extern CGameValues game_values;
extern CResourceManager* rm;
Expand Down Expand Up @@ -72,7 +73,7 @@ UI_MatchSelectionMenu::UI_MatchSelectionMenu() : UI_Menu()

miMatchSelectionMenuLeftHeaderBar = new MI_Image(&rm->menu_plain_field, 0, 0, 0, 0, 320, 32, 1, 1, 0);
miMatchSelectionMenuRightHeaderBar = new MI_Image(&rm->menu_plain_field, 320, 0, 192, 0, 320, 32, 1, 1, 0);
miMatchSelectionMenuHeaderText = new MI_Text("Match Type Menu", 320, 5, 0, 2, 1);
miMatchSelectionMenuHeaderText = new MI_HeaderText("Match Type Menu", 320, 5);

miMatchSelectionDisplayImage = new MI_Image(&rm->menu_match_select, 160, 80, 0, 0, 320, 240, 1, 1, 0);
miWorldPreviewDisplay = new MI_WorldPreviewDisplay(160, 80, 20, 15);
Expand Down
Loading

0 comments on commit 7fa9ba9

Please sign in to comment.