Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor in the UI control code #333

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/common/GlobalConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@
#define HALF_PI 1.5707963f
#define QUARTER_PI 0.7853982f

#define MENU_ITEM_NEIGHBOR_UP 0
#define MENU_ITEM_NEIGHBOR_DOWN 1
#define MENU_ITEM_NEIGHBOR_LEFT 2
#define MENU_ITEM_NEIGHBOR_RIGHT 3

#define NUM_POWERUPS 26
#define NUM_WORLD_POWERUPS 15
#define NUM_WORLD_SCORE_BONUSES 20
Expand Down
51 changes: 28 additions & 23 deletions src/common/math/Vec2.h
Original file line number Diff line number Diff line change
@@ -1,57 +1,62 @@
#pragma once


struct Vec2f {
float x = 0.f;
float y = 0.f;
template<typename T>
struct Vec2 {
T x = 0;
T y = 0;

constexpr explicit Vec2f() = default;
constexpr explicit Vec2f(float inX, float inY) : x(inX), y(inY) {}
constexpr explicit Vec2() = default;
constexpr Vec2(T inX, T inY) : x(inX), y(inY) {}

static constexpr Vec2f zero() {
return Vec2f();
static constexpr Vec2 zero() {
return Vec2();
}

constexpr Vec2f& operator+=(const Vec2f& rhs) {
constexpr Vec2& operator+=(const Vec2& rhs) {
x += rhs.x;
y += rhs.y;
return *this;
}
constexpr Vec2f& operator-=(const Vec2f& rhs) {
constexpr Vec2& operator-=(const Vec2& rhs) {
x -= rhs.x;
y -= rhs.y;
return *this;
}

constexpr Vec2f& operator*=(float val) {
template<typename Scalar> constexpr Vec2& operator*=(Scalar val) {
x *= val;
y *= val;
return *this;
}
constexpr Vec2f& operator/=(float val) {
template<typename Scalar> constexpr Vec2& operator/=(Scalar val) {
x /= val;
y /= val;
return *this;
}
};


constexpr Vec2f operator+(const Vec2f& lhs, const Vec2f& rhs) {
return Vec2f(lhs.x + rhs.x, lhs.y + rhs.y);
template<typename T> constexpr Vec2<T> operator+(const Vec2<T>& lhs, const Vec2<T>& rhs) {
return {lhs.x + rhs.x, lhs.y + rhs.y};
}
constexpr Vec2f operator-(const Vec2f& lhs, const Vec2f& rhs) {
return Vec2f(lhs.x - rhs.x, lhs.y - rhs.y);
template<typename T> constexpr Vec2<T> operator-(const Vec2<T>& lhs, const Vec2<T>& rhs) {
return {lhs.x - rhs.x, lhs.y - rhs.y};
}
constexpr Vec2f operator*(const Vec2f& lhs, const Vec2f& rhs) {
return Vec2f(lhs.x * rhs.x, lhs.y * rhs.y);
template<typename T> constexpr Vec2<T> operator*(const Vec2<T>& lhs, const Vec2<T>& rhs) {
return {lhs.x * rhs.x, lhs.y * rhs.y};
}
constexpr Vec2f operator/(const Vec2f& lhs, const Vec2f& rhs) {
return Vec2f(lhs.x / rhs.x, lhs.y / rhs.y);
template<typename T> constexpr Vec2<T> operator/(const Vec2<T>& lhs, const Vec2<T>& rhs) {
return {lhs.x / rhs.x, lhs.y / rhs.y};
}

constexpr Vec2f operator*(const Vec2f& lhs, float scalar) {
return Vec2f(lhs.x * scalar, lhs.y * scalar);
template<typename T, typename Scalar> constexpr Vec2<T> operator*(const Vec2<T>& lhs, Scalar scalar) {
return {lhs.x * scalar, lhs.y * scalar};
}
constexpr Vec2f operator/(const Vec2f& lhs, float scalar) {
return Vec2f(lhs.x / scalar, lhs.y / scalar);
template<typename T, typename Scalar> constexpr Vec2<T> operator/(const Vec2<T>& lhs, Scalar scalar) {
return {lhs.x / scalar, lhs.y / scalar};
}


using Vec2f = Vec2<float>;
using Vec2s = Vec2<short>;
20 changes: 10 additions & 10 deletions src/common/ui/MI_Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ MenuCodeEnum MI_Button::SendInput(CPlayerInput*)

void MI_Button::Draw()
{
if (!fShow)
if (!m_visible)
return;

spr->draw(ix, iy, 0, (fSelected ? 32 : 0) + iAdjustmentY, iHalfWidth, 32);
spr->draw(ix + iHalfWidth, iy, 512 - iWidth + iHalfWidth, (fSelected ? 32 : 0) + iAdjustmentY, iWidth - iHalfWidth, 32);
spr->draw(m_pos.x, m_pos.y, 0, (fSelected ? 32 : 0) + iAdjustmentY, iHalfWidth, 32);
spr->draw(m_pos.x + iHalfWidth, m_pos.y, 512 - iWidth + iHalfWidth, (fSelected ? 32 : 0) + iAdjustmentY, iWidth - iHalfWidth, 32);

switch (m_text_align) {
case TextAlign::LEFT:
rm->menu_font_large.drawChopRight(ix + 16 + (iImageW > 0 ? iImageW + 2 : 0), iy + 5, iWidth - 32, szName.c_str());
rm->menu_font_large.drawChopRight(m_pos.x + 16 + (iImageW > 0 ? iImageW + 2 : 0), m_pos.y + 5, iWidth - 32, szName.c_str());
if (sprImage)
sprImage->draw(ix + 16, iy + 16 - (iImageH >> 1), iImageSrcX, iImageSrcY, iImageW, iImageH);
sprImage->draw(m_pos.x + 16, m_pos.y + 16 - (iImageH >> 1), iImageSrcX, iImageSrcY, iImageW, iImageH);
break;
case TextAlign::CENTER:
rm->menu_font_large.drawCentered(ix + ((iWidth + (iImageW > 0 ? iImageW + 2 : 0)) >> 1), iy + 5, szName.c_str());
rm->menu_font_large.drawCentered(m_pos.x + ((iWidth + (iImageW > 0 ? iImageW + 2 : 0)) >> 1), m_pos.y + 5, szName.c_str());
if (sprImage)
sprImage->draw(ix + (iWidth >> 1) - ((iTextW + iImageW) >> 1) - 1, iy + 16 - (iImageH >> 1), iImageSrcX, iImageSrcY, iImageW, iImageH);
sprImage->draw(m_pos.x + (iWidth >> 1) - ((iTextW + iImageW) >> 1) - 1, m_pos.y + 16 - (iImageH >> 1), iImageSrcX, iImageSrcY, iImageW, iImageH);
break;
case TextAlign::RIGHT:
rm->menu_font_large.drawRightJustified(ix + iWidth - 16, iy + 5, szName.c_str());
rm->menu_font_large.drawRightJustified(m_pos.x + iWidth - 16, m_pos.y + 5, szName.c_str());
if (sprImage)
sprImage->draw(ix + iWidth - 18 - iTextW - iImageW, iy + 16 - (iImageH >> 1), iImageSrcX, iImageSrcY, iImageW, iImageH);
sprImage->draw(m_pos.x + iWidth - 18 - iTextW - iImageW, m_pos.y + 16 - (iImageH >> 1), iImageSrcX, iImageSrcY, iImageW, iImageH);
break;
}
}
Expand All @@ -81,7 +81,7 @@ MenuCodeEnum MI_Button::MouseClick(short iMouseX, short iMouseY)
if (fDisable)
return MENU_CODE_NONE;

if (iMouseX >= ix && iMouseX < ix + iWidth && iMouseY >= iy && iMouseY < iy + 32) {
if (iMouseX >= m_pos.x && iMouseX < m_pos.x + iWidth && iMouseY >= m_pos.y && iMouseY < m_pos.y + 32) {
return menuCode;
}

Expand Down
8 changes: 4 additions & 4 deletions src/common/ui/MI_Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ MI_Image::MI_Image(

void MI_Image::Update()
{
if (!fShow)
if (!m_visible)
return;

if (iSpeed > 0 && ++iTimer >= iSpeed) {
Expand Down Expand Up @@ -79,7 +79,7 @@ void MI_Image::Update()

void MI_Image::Draw()
{
if (!fShow || (fBlink && !fBlinkShow))
if (!m_visible || (fBlink && !fBlinkShow))
return;

short iXOffset = 0;
Expand All @@ -91,7 +91,7 @@ void MI_Image::Draw()
}

if (fPulse)
spr->drawStretch(ix - iPulseValue + iXOffset, iy - iPulseValue + iYOffset, iw + (iPulseValue << 1), ih + (iPulseValue << 1), iXFrame, iYFrame, iw, ih);
spr->drawStretch(m_pos.x - iPulseValue + iXOffset, m_pos.y - iPulseValue + iYOffset, iw + (iPulseValue << 1), ih + (iPulseValue << 1), iXFrame, iYFrame, iw, ih);
else
spr->draw(ix + iXOffset, iy + iYOffset, iXFrame, iYFrame, iw, ih);
spr->draw(m_pos.x + iXOffset, m_pos.y + iYOffset, iXFrame, iYFrame, iw, ih);
}
8 changes: 2 additions & 6 deletions src/common/ui/MI_Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class MI_Image : public UI_Control {
void Update() override;
void Draw() override;

void SetPosition(short x, short y) {
ix = x;
iy = y;
}
void SetAnimationSpeed(short speed) {
iSpeed = speed;
}
Expand Down Expand Up @@ -58,8 +54,8 @@ class MI_Image : public UI_Control {
}

void GetPositionAndSize(short& x, short& y, short& w, short& h) const {
x = ix;
y = iy;
x = m_pos.x;
y = m_pos.y;
w = iw;
h = ih;
}
Expand Down
14 changes: 7 additions & 7 deletions src/common/ui/MI_ImageSelectField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ MI_ImageSelectField::MI_ImageSelectField(

void MI_ImageSelectField::Draw()
{
if (!fShow)
if (!m_visible)
return;

m_spr->draw(ix, iy, 0, (fSelected ? 32 : 0), m_indent - 16, 32);
m_spr->draw(ix + m_indent - 16, iy, 0, (fSelected ? 96 : 64), 32, 32);
m_spr->draw(ix + m_indent + 16, iy, 528 - m_width + m_indent, (fSelected ? 32 : 0), m_width - m_indent - 16, 32);
m_spr->draw(m_pos.x, m_pos.y, 0, (fSelected ? 32 : 0), m_indent - 16, 32);
m_spr->draw(m_pos.x + m_indent - 16, m_pos.y, 0, (fSelected ? 96 : 64), 32, 32);
m_spr->draw(m_pos.x + m_indent + 16, m_pos.y, 528 - m_width + m_indent, (fSelected ? 32 : 0), m_width - m_indent - 16, 32);

rm->menu_font_large.drawChopRight(ix + 16, iy + 5, m_indent - 8, m_name.c_str());
rm->menu_font_large.drawChopRight(m_pos.x + 16, m_pos.y + 5, m_indent - 8, m_name.c_str());

if (!m_items.empty()) {
rm->menu_font_large.drawChopRight(ix + m_indent + iImageWidth + 10, iy + 5, m_width - m_indent - 24, currentItem().name.c_str());
rm->menu_font_large.drawChopRight(m_pos.x + m_indent + iImageWidth + 10, m_pos.y + 5, m_width - m_indent - 24, currentItem().name.c_str());
}

spr_image->draw(ix + m_indent + 8, iy + 16 - (iImageHeight >> 1), (currentItem().iconOverride >= 0 ? currentItem().iconOverride : currentItem().value) * iImageWidth, 0, iImageWidth, iImageHeight);
spr_image->draw(m_pos.x + m_indent + 8, m_pos.y + 16 - (iImageHeight >> 1), (currentItem().iconOverride >= 0 ? currentItem().iconOverride : currentItem().value) * iImageWidth, 0, iImageWidth, iImageHeight);

miModifyImageRight->Draw();
miModifyImageLeft->Draw();
Expand Down
34 changes: 17 additions & 17 deletions src/common/ui/MI_MapField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ MI_MapField::MI_MapField(gfxSprite* nspr, short x, short y, std::string name, sh
, iSearchStringTimer(0)
, fShowtags(showtags)
{
miModifyImageLeft = std::make_unique<MI_Image>(nspr, ix + indent - 26, iy + 4, 32, 64, 26, 24, 4, 1, 8);
miModifyImageLeft->Show(false);
miModifyImageLeft = std::make_unique<MI_Image>(nspr, m_pos.x + indent - 26, m_pos.y + 4, 32, 64, 26, 24, 4, 1, 8);
miModifyImageLeft->setVisible(false);

miModifyImageRight = std::make_unique<MI_Image>(nspr, ix + iWidth - 16, iy + 4, 32, 88, 26, 24, 4, 1, 8);
miModifyImageRight->Show(false);
miModifyImageRight = std::make_unique<MI_Image>(nspr, m_pos.x + iWidth - 16, m_pos.y + 4, 32, 88, 26, 24, 4, 1, 8);
miModifyImageRight->setVisible(false);

if (fShowtags) {
iSlideListOut = (iWidth - 352) >> 1;
Expand All @@ -44,8 +44,8 @@ MenuCodeEnum MI_MapField::Modify(bool modify)
if (fDisable)
return MENU_CODE_UNSELECT_ITEM;

miModifyImageLeft->Show(modify);
miModifyImageRight->Show(modify);
miModifyImageLeft->setVisible(modify);
miModifyImageRight->setVisible(modify);

fModifying = modify;
return MENU_CODE_MODIFY_ACCEPTED;
Expand Down Expand Up @@ -107,8 +107,8 @@ MenuCodeEnum MI_MapField::SendInput(CPlayerInput * playerInput)
}

if (playerInput->outputControls[iPlayer].menu_select.fPressed || playerInput->outputControls[iPlayer].menu_cancel.fPressed) {
miModifyImageLeft->Show(false);
miModifyImageRight->Show(false);
miModifyImageLeft->setVisible(false);
miModifyImageRight->setVisible(false);

fModifying = false;
return MENU_CODE_UNSELECT_ITEM;
Expand Down Expand Up @@ -188,16 +188,16 @@ void MI_MapField::Update()

void MI_MapField::Draw()
{
if (!fShow)
if (!m_visible)
return;

//Draw the select field background
spr->draw(ix, iy, 0, (fSelected ? 32 : 0), iIndent - 16, 32);
spr->draw(ix + iIndent - 16, iy, 0, (fSelected ? 96 : 64), 32, 32);
spr->draw(ix + iIndent + 16, iy, 528 - iWidth + iIndent, (fSelected ? 32 : 0), iWidth - iIndent - 16, 32);
spr->draw(m_pos.x, m_pos.y, 0, (fSelected ? 32 : 0), iIndent - 16, 32);
spr->draw(m_pos.x + iIndent - 16, m_pos.y, 0, (fSelected ? 96 : 64), 32, 32);
spr->draw(m_pos.x + iIndent + 16, m_pos.y, 528 - iWidth + iIndent, (fSelected ? 32 : 0), iWidth - iIndent - 16, 32);

rm->menu_font_large.drawChopRight(ix + 16, iy + 5, iIndent - 8, szName.c_str());
rm->menu_font_large.drawChopRight(ix + iIndent + 8, iy + 5, iWidth - iIndent - 24, szMapName);
rm->menu_font_large.drawChopRight(m_pos.x + 16, m_pos.y + 5, iIndent - 8, szName.c_str());
rm->menu_font_large.drawChopRight(m_pos.x + iIndent + 8, m_pos.y + 5, iWidth - iIndent - 24, szMapName);

MI_MapPreview::Draw();

Expand Down Expand Up @@ -233,7 +233,7 @@ MenuCodeEnum MI_MapField::MouseClick(short iMouseX, short iMouseY)
}

//Otherwise just check to see if we clicked on the whole control
if (iMouseX >= ix && iMouseX < ix + iWidth && iMouseY >= iy && iMouseY < iy + 32)
if (iMouseX >= m_pos.x && iMouseX < m_pos.x + iWidth && iMouseY >= m_pos.y && iMouseY < m_pos.y + 32)
return MENU_CODE_CLICKED;

//Otherwise this control wasn't clicked at all
Expand Down Expand Up @@ -277,8 +277,8 @@ void MI_MapField::SetDimensions(short width, short indent)
{
MI_MapPreview::SetDimensions(width, indent);

miModifyImageLeft->SetPosition(ix + indent - 26, iy + 4);
miModifyImageRight->SetPosition(ix + iWidth - 16, iy + 4);
miModifyImageLeft->SetPosition(m_pos.x + indent - 26, m_pos.y + 4);
miModifyImageRight->SetPosition(m_pos.x + iWidth - 16, m_pos.y + 4);

if (fShowtags) {
//iSlideListOut = (iWidth - 352) >> 1;
Expand Down
12 changes: 6 additions & 6 deletions src/common/ui/MI_MapPreview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ void MI_MapPreview::Update()

void MI_MapPreview::Draw()
{
if (!fShow)
if (!m_visible)
return;

short iMapBoxX = ix + (iWidth >> 1) - 176 - iSlideListOut;
short iMapBoxX = m_pos.x + (iWidth >> 1) - 176 - iSlideListOut;

//Draw the background for the map preview
rm->menu_dialog.draw(iMapBoxX, iy + 30, 0, 0, 336, 254);
rm->menu_dialog.draw(iMapBoxX + 336, iy + 30, 496, 0, 16, 254);
rm->menu_dialog.draw(iMapBoxX, iy + 284, 0, 464, 336, 16);
rm->menu_dialog.draw(iMapBoxX + 336, iy + 284, 496, 464, 16, 16);
rm->menu_dialog.draw(iMapBoxX, m_pos.y + 30, 0, 0, 336, 254);
rm->menu_dialog.draw(iMapBoxX + 336, m_pos.y + 30, 496, 0, 16, 254);
rm->menu_dialog.draw(iMapBoxX, m_pos.y + 284, 0, 464, 336, 16);
rm->menu_dialog.draw(iMapBoxX + 336, m_pos.y + 284, 496, 464, 16, 16);

rectDst.x = iMapBoxX + 16;

Expand Down
22 changes: 11 additions & 11 deletions src/common/ui/MI_ScoreText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ MI_ScoreText::MI_ScoreText(short x, short y)

void MI_ScoreText::Draw()
{
if (!fShow)
if (!m_visible)
return;

rm->spr_scoretext.draw(iDigitRightDstX, iy, iDigitRightSrcX, 0, 16, 16);
rm->spr_scoretext.draw(iDigitRightDstX, m_pos.y, iDigitRightSrcX, 0, 16, 16);

if (iDigitLeftSrcX > 0) {
rm->spr_scoretext.draw(iDigitMiddleDstX, iy, iDigitMiddleSrcX, 0, 16, 16);
rm->spr_scoretext.draw(iDigitLeftDstX, iy, iDigitLeftSrcX, 0, 16, 16);
rm->spr_scoretext.draw(iDigitMiddleDstX, m_pos.y, iDigitMiddleSrcX, 0, 16, 16);
rm->spr_scoretext.draw(iDigitLeftDstX, m_pos.y, iDigitLeftSrcX, 0, 16, 16);
}
else if (iDigitMiddleSrcX > 0) {
rm->spr_scoretext.draw(iDigitMiddleDstX, iy, iDigitMiddleSrcX, 0, 16, 16);
rm->spr_scoretext.draw(iDigitMiddleDstX, m_pos.y, iDigitMiddleSrcX, 0, 16, 16);
}
}

Expand All @@ -37,14 +37,14 @@ void MI_ScoreText::SetScore(short sScore)

if (iDigitLeftSrcX == 0) {
if (iDigitMiddleSrcX == 0) {
iDigitRightDstX = ix - 8;
iDigitRightDstX = m_pos.x - 8;
} else {
iDigitMiddleDstX = ix - 16;
iDigitRightDstX = ix;
iDigitMiddleDstX = m_pos.x - 16;
iDigitRightDstX = m_pos.x;
}
} else {
iDigitLeftDstX = ix - 24;
iDigitMiddleDstX = ix - 8;
iDigitRightDstX = ix + 8;
iDigitLeftDstX = m_pos.x - 24;
iDigitMiddleDstX = m_pos.x - 8;
iDigitRightDstX = m_pos.x + 8;
}
}
Loading