Skip to content

Commit

Permalink
Neighbor-related refactoring in the UI code
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed Jul 2, 2024
1 parent 67b9f0a commit 0d023fc
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 70 deletions.
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
7 changes: 7 additions & 0 deletions src/common/ui/MenuCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,10 @@ enum MenuCodeEnum {
MENU_CODE_TO_NET_ROOM_START_IN_PROGRESS,
MENU_CODE_NET_ROOM_GO,
};

enum class MenuNavDirection : unsigned char {
Up,
Down,
Left,
Right,
};
12 changes: 6 additions & 6 deletions src/common/uicontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
UI_Control::UI_Control(short x, short y)
: m_pos(x, y)
{
neighborControls.fill(nullptr);
m_neighbors.fill(nullptr);
}


Expand All @@ -21,8 +21,8 @@ UI_Control& UI_Control::operator= (const UI_Control& other)
fDisable = other.fDisable;
m_visible = other.m_visible;

uiMenu = nullptr;
neighborControls.fill(nullptr);
m_parentMenu = nullptr;
m_neighbors.fill(nullptr);
iControllingTeam = other.iControllingTeam;
}
return *this;
Expand All @@ -35,8 +35,8 @@ UI_Control::UI_Control(const UI_Control& other)
}


void UI_Control::SetNeighbor(unsigned short iNeighbor, UI_Control* uiControl)
void UI_Control::setNeighbor(MenuNavDirection iNeighbor, UI_Control* uiControl)
{
assert(iNeighbor < neighborControls.size());
neighborControls[iNeighbor] = uiControl;
const auto idx = static_cast<std::size_t>(iNeighbor);
m_neighbors[idx] = uiControl;
}
52 changes: 27 additions & 25 deletions src/common/uicontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ class UI_Control
return MENU_CODE_NONE;
}

/// Called when user selects this control to change it's value
virtual MenuCodeEnum Modify(bool modify) {
if (fDisable)
return MENU_CODE_UNSELECT_ITEM;

fModifying = modify;
return MENU_CODE_MODIFY_ACCEPTED;
}

virtual MenuCodeEnum MouseClick(short iMouseX, short iMouseY) {
return MENU_CODE_NONE;
}

virtual void Refresh() {}

virtual void Disable(bool disable) {
fDisable = disable;
}

bool Select(bool select) {
fSelected = select;

Expand All @@ -44,14 +63,6 @@ class UI_Control
return fModifying;
}

virtual MenuCodeEnum Modify(bool modify) {
if (fDisable)
return MENU_CODE_UNSELECT_ITEM;

fModifying = modify;
return MENU_CODE_MODIFY_ACCEPTED;
}

void SetAutoModify(bool autoModify) {
fAutoModify = autoModify;
}
Expand All @@ -63,10 +74,11 @@ class UI_Control
m_pos = {x, y};
}

void SetNeighbor(unsigned short iNeighbor, UI_Control* uiControl);
void setNeighbor(MenuNavDirection iNeighbor, UI_Control* uiControl);

UI_Control* GetNeighbor(short iNeighbor) const {
return neighborControls[iNeighbor];
UI_Control* neighbor(MenuNavDirection iNeighbor) const {
const auto idx = static_cast<std::size_t>(iNeighbor);
return m_neighbors[idx];
}

void setVisible(bool show) {
Expand All @@ -76,8 +88,8 @@ class UI_Control
return m_visible;
}

void SetMenuParent(UI_Menu* menu) {
uiMenu = menu;
void setParent(UI_Menu* menu) {
m_parentMenu = menu;
}

bool IsModifying() const {
Expand All @@ -88,16 +100,6 @@ class UI_Control
iControllingTeam = teamid;
}

virtual MenuCodeEnum MouseClick(short iMouseX, short iMouseY) {
return MENU_CODE_NONE;
}

virtual void Refresh() {}

virtual void Disable(bool disable) {
fDisable = disable;
}

protected:
Vec2s m_pos = Vec2s::zero();

Expand All @@ -107,8 +109,8 @@ class UI_Control
bool fDisable = false;
bool m_visible = true;

std::array<UI_Control*, 4> neighborControls;
std::array<UI_Control*, 4> m_neighbors;

UI_Menu* uiMenu = nullptr;
UI_Menu* m_parentMenu = nullptr;
short iControllingTeam = -1;
};
45 changes: 31 additions & 14 deletions src/common/uimenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,41 @@
#include "GameValues.h" // UI_Menu::SendInput
#include "uicontrol.h"

#include <cassert>

extern short LookupTeamID(short id);

extern CGameValues game_values;


namespace {
constexpr MenuCodeEnum navDir2menuCode(MenuNavDirection direction)
{
switch (direction) {
case MenuNavDirection::Up: return MenuCodeEnum::MENU_CODE_NEIGHBOR_UP;
case MenuNavDirection::Down: return MenuCodeEnum::MENU_CODE_NEIGHBOR_DOWN;
case MenuNavDirection::Left: return MenuCodeEnum::MENU_CODE_NEIGHBOR_LEFT;
case MenuNavDirection::Right: return MenuCodeEnum::MENU_CODE_NEIGHBOR_RIGHT;
}
assert(false);
return MenuCodeEnum::MENU_CODE_NONE;
}
} // namespace


void UI_Menu::AddControl(UI_Control* control, UI_Control* up, UI_Control* down, UI_Control* left, UI_Control* right)
{
control->SetMenuParent(this);
control->SetNeighbor(MENU_ITEM_NEIGHBOR_UP, up);
control->SetNeighbor(MENU_ITEM_NEIGHBOR_DOWN, down);
control->SetNeighbor(MENU_ITEM_NEIGHBOR_LEFT, left);
control->SetNeighbor(MENU_ITEM_NEIGHBOR_RIGHT, right);
control->setParent(this);
control->setNeighbor(MenuNavDirection::Up, up);
control->setNeighbor(MenuNavDirection::Down, down);
control->setNeighbor(MenuNavDirection::Left, left);
control->setNeighbor(MenuNavDirection::Right, right);
controls.emplace_back(control);
}

void UI_Menu::AddNonControl(UI_Control* control)
{
control->SetMenuParent(this);
control->setParent(this);
controls.emplace_back(control);
}

Expand Down Expand Up @@ -94,19 +111,19 @@ MenuCodeEnum UI_Menu::SendInput(CPlayerInput* playerInput)
}

if (playerInput->outputControls[iPlayer].menu_up.fPressed) {
return MoveNextControl(MENU_CODE_NEIGHBOR_UP);
return MoveNextControl(MenuNavDirection::Up);
}

if (playerInput->outputControls[iPlayer].menu_down.fPressed) {
return MoveNextControl(MENU_CODE_NEIGHBOR_DOWN);
return MoveNextControl(MenuNavDirection::Down);
}

if (playerInput->outputControls[iPlayer].menu_left.fPressed) {
return MoveNextControl(MENU_CODE_NEIGHBOR_LEFT);
return MoveNextControl(MenuNavDirection::Left);
}

if (playerInput->outputControls[iPlayer].menu_right.fPressed) {
return MoveNextControl(MENU_CODE_NEIGHBOR_RIGHT);
return MoveNextControl(MenuNavDirection::Right);
}

if (playerInput->outputControls[iPlayer].menu_select.fPressed) {
Expand Down Expand Up @@ -137,22 +154,22 @@ MenuCodeEnum UI_Menu::SendInput(CPlayerInput* playerInput)
return MENU_CODE_NONE;
}

MenuCodeEnum UI_Menu::MoveNextControl(MenuCodeEnum iDirection)
MenuCodeEnum UI_Menu::MoveNextControl(MenuNavDirection iDirection)
{
if (!m_currentFocus)
return MENU_CODE_NONE;

UI_Control* neighbor = m_currentFocus->GetNeighbor(iDirection);
UI_Control* neighbor = m_currentFocus->neighbor(iDirection);

while (neighbor && !neighbor->IsVisible()) {
neighbor = neighbor->GetNeighbor(iDirection);
neighbor = neighbor->neighbor(iDirection);
}

if (neighbor) {
m_currentFocus->Select(false);
m_currentFocus = neighbor;
fModifyingItem = m_currentFocus->Select(true);
return iDirection;
return navDir2menuCode(iDirection);
}

return MENU_CODE_NONE;
Expand Down
2 changes: 1 addition & 1 deletion src/common/uimenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class UI_Menu {
void Refresh();

protected:
MenuCodeEnum MoveNextControl(MenuCodeEnum iDirection);
MenuCodeEnum MoveNextControl(MenuNavDirection iDirection);

std::vector<std::unique_ptr<UI_Control>> controls;

Expand Down
4 changes: 2 additions & 2 deletions src/smw/menu/network/NetRoomMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ void UI_NetRoomMenu::Refresh()
{
miNetRoomHeaderText->SetText(netplay.currentRoom.name.c_str());

miNetRoomStartButton->SetNeighbor(MENU_ITEM_NEIGHBOR_UP, miSkinSelector[netplay.remotePlayerNumber]);
miNetRoomMessageField->SetNeighbor(MENU_ITEM_NEIGHBOR_DOWN, miSkinSelector[netplay.remotePlayerNumber]);
miNetRoomStartButton->setNeighbor(MenuNavDirection::Up, miSkinSelector[netplay.remotePlayerNumber]);
miNetRoomMessageField->setNeighbor(MenuNavDirection::Down, miSkinSelector[netplay.remotePlayerNumber]);

for (short p = 0; p < 4; p++) {
miNetRoomPlayerName[p]->SetText(netplay.currentRoom.playerNames[p].c_str());
Expand Down
2 changes: 1 addition & 1 deletion src/smw/ui/MI_BonusWheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void MI_BonusWheel::Update()
short iPoofX = m_pos.x + 152 + (short)(110.0f * cos(dSelectionSector[iDisplayPowerupIndex]));
short iPoofY = m_pos.y + 200 + (short)(110.0f * sin(dSelectionSector[iDisplayPowerupIndex]));

uiMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_poof, iPoofX, iPoofY, 4, 5));
m_parentMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_poof, iPoofX, iPoofY, 4, 5));

ifSoundOnPlay(rm->sfx_cannon);

Expand Down
2 changes: 1 addition & 1 deletion src/smw/ui/MI_PowerupSelection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void MI_PowerupSelection::EnablePowerupFields(bool fEnable)
miPowerupSlider[iPowerup]->Disable(!fEnable);
}

miPreset->SetNeighbor(1, fEnable ? miPowerupSlider[0] : NULL);
miPreset->setNeighbor(MenuNavDirection::Down, fEnable ? miPowerupSlider[0] : NULL);
}

MenuCodeEnum MI_PowerupSelection::Modify(bool modify)
Expand Down
4 changes: 2 additions & 2 deletions src/smw/ui/MI_StringScroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ MenuCodeEnum MI_StringScroll::SendInput(CPlayerInput * playerInput)
}

if (playerInput->outputControls[iPlayer].menu_left.fPressed) {
if (neighborControls[2])
if (neighbor(MenuNavDirection::Left))
return MENU_CODE_UNSELECT_ITEM;

return MENU_CODE_NONE;
}

if (playerInput->outputControls[iPlayer].menu_right.fPressed) {
if (neighborControls[3])
if (neighbor(MenuNavDirection::Right))
return MENU_CODE_UNSELECT_ITEM;

return MENU_CODE_NONE;
Expand Down
8 changes: 4 additions & 4 deletions src/smw/ui/MI_TournamentScoreboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void MI_TournamentScoreboard::Update()
}
}

if (uiMenu) {
if (m_parentMenu) {
if (iTournamentWinner != -1) { //Single tournament winning team
if (--iFireworksCounter < 0 && iTournamentWinner >= 0) {
iFireworksCounter = (short)(RANDOM_INT(30) + 10);
Expand All @@ -167,7 +167,7 @@ void MI_TournamentScoreboard::Update()
float dVelY = dVel * sin(dAngle);

short iRandomColor = (short)RANDOM_INT(iTeamCounts[iTournamentWinner]);
uiMenu->AddEyeCandy(new EC_FallingObject(&rm->spr_bonus, iRandX, iRandY, dVelX, dVelY, 4, 2, 0, game_values.colorids[iTeamIDs[iTournamentWinner][iRandomColor]] << 4, 16, 16));
m_parentMenu->AddEyeCandy(new EC_FallingObject(&rm->spr_bonus, iRandX, iRandY, dVelX, dVelY, 4, 2, 0, game_values.colorids[iTeamIDs[iTournamentWinner][iRandomColor]] << 4, 16, 16));
dAngle -= (float)PI / 14;
}
} else {
Expand All @@ -177,7 +177,7 @@ void MI_TournamentScoreboard::Update()
short iRandY = (short)(RANDOM_INT(416));
short iRandomColor = (short)RANDOM_INT(iTeamCounts[iTournamentWinner]);

uiMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_fireworks, iRandX, iRandY, 8, 4, 0, game_values.colorids[iTeamIDs[iTournamentWinner][iRandomColor]] << 6, 64, 64));
m_parentMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_fireworks, iRandX, iRandY, 8, 4, 0, game_values.colorids[iTeamIDs[iTournamentWinner][iRandomColor]] << 6, 64, 64));
}
}

Expand All @@ -196,7 +196,7 @@ void MI_TournamentScoreboard::Update()
short iRandX = (short)(RANDOM_INT(App::screenWidth - iStringWidth) + (iStringWidth >> 1));
short iRandY = (short)(RANDOM_INT(App::screenHeight - 100) + 100);

uiMenu->AddEyeCandy(new EC_GravText(&rm->menu_font_large, iRandX, iRandY, szWinnerText, -VELJUMP));
m_parentMenu->AddEyeCandy(new EC_GravText(&rm->menu_font_large, iRandX, iRandY, szWinnerText, -VELJUMP));
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/smw/ui/MI_World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void MI_World::DisplayTeamControlAnnouncement()
else
sprintf(szMessage, "Team %d Is In Control", iControllingTeam + 1);

uiMenu->AddEyeCandy(new EC_Announcement(&rm->menu_font_large, &rm->spr_announcementicons, szMessage, game_values.colorids[iControllingPlayerId], 120, 100));
m_parentMenu->AddEyeCandy(new EC_Announcement(&rm->menu_font_large, &rm->spr_announcementicons, szMessage, game_values.colorids[iControllingPlayerId], 120, 100));
}

void MI_World::SetCurrentStageToCompleted(short iWinningTeam)
Expand Down Expand Up @@ -923,7 +923,7 @@ bool MI_World::UsePowerup(short iPlayer, short iTeam, short iIndex, bool fPopupI
g_worldmap.UpdateTile(sMapSurface[0], iDestX - iMapDrawOffsetCol, iDestY - iMapDrawOffsetRow, iMapDrawOffsetCol, iMapDrawOffsetRow, iAnimationFrame);
g_worldmap.UpdateTile(sMapSurface[1], iDestX - iMapDrawOffsetCol, iDestY - iMapDrawOffsetRow, iMapDrawOffsetCol, iMapDrawOffsetRow, iAnimationFrame);

uiMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_poof, (iDestX << 5) + iMapOffsetX - 8, (iDestY << 5) + iMapOffsetY - 8, 4, 5));
m_parentMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_poof, (iDestX << 5) + iMapOffsetX - 8, (iDestY << 5) + iMapOffsetY - 8, 4, 5));

fUsedItem = true;
ifSoundOnPlay(rm->sfx_transform);
Expand All @@ -938,28 +938,28 @@ bool MI_World::UsePowerup(short iPlayer, short iTeam, short iIndex, bool fPopupI
short iPlayerY = (iPlayerCurrentTileY << 5) + iMapOffsetY;

if (iDoorsOpened & 0x1) {
uiMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_fireballexplosion, iPlayerX - TILESIZE, iPlayerY, 3, 8));
m_parentMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_fireballexplosion, iPlayerX - TILESIZE, iPlayerY, 3, 8));

g_worldmap.UpdateTile(sMapSurface[0], iPlayerCurrentTileX - iMapDrawOffsetCol - 1, iPlayerCurrentTileY - iMapDrawOffsetRow, iMapDrawOffsetCol, iMapDrawOffsetRow, iAnimationFrame);
g_worldmap.UpdateTile(sMapSurface[1], iPlayerCurrentTileX - iMapDrawOffsetCol - 1, iPlayerCurrentTileY - iMapDrawOffsetRow, iMapDrawOffsetCol, iMapDrawOffsetRow, iAnimationFrame);
}

if (iDoorsOpened & 0x2) {
uiMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_fireballexplosion, iPlayerX + TILESIZE, iPlayerY, 3, 8));
m_parentMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_fireballexplosion, iPlayerX + TILESIZE, iPlayerY, 3, 8));

g_worldmap.UpdateTile(sMapSurface[0], iPlayerCurrentTileX - iMapDrawOffsetCol + 1, iPlayerCurrentTileY - iMapDrawOffsetRow, iMapDrawOffsetCol, iMapDrawOffsetRow, iAnimationFrame);
g_worldmap.UpdateTile(sMapSurface[1], iPlayerCurrentTileX - iMapDrawOffsetCol + 1, iPlayerCurrentTileY - iMapDrawOffsetRow, iMapDrawOffsetCol, iMapDrawOffsetRow, iAnimationFrame);
}

if (iDoorsOpened & 0x4) {
uiMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_fireballexplosion, iPlayerX, iPlayerY - TILESIZE, 3, 8));
m_parentMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_fireballexplosion, iPlayerX, iPlayerY - TILESIZE, 3, 8));

g_worldmap.UpdateTile(sMapSurface[0], iPlayerCurrentTileX - iMapDrawOffsetCol, iPlayerCurrentTileY - iMapDrawOffsetRow - 1, iMapDrawOffsetCol, iMapDrawOffsetRow, iAnimationFrame);
g_worldmap.UpdateTile(sMapSurface[1], iPlayerCurrentTileX - iMapDrawOffsetCol, iPlayerCurrentTileY - iMapDrawOffsetRow - 1, iMapDrawOffsetCol, iMapDrawOffsetRow, iAnimationFrame);
}

if (iDoorsOpened & 0x8) {
uiMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_fireballexplosion, iPlayerX, iPlayerY + TILESIZE, 3, 8));
m_parentMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_fireballexplosion, iPlayerX, iPlayerY + TILESIZE, 3, 8));

g_worldmap.UpdateTile(sMapSurface[0], iPlayerCurrentTileX - iMapDrawOffsetCol, iPlayerCurrentTileY - iMapDrawOffsetRow + 1, iMapDrawOffsetCol, iMapDrawOffsetRow, iAnimationFrame);
g_worldmap.UpdateTile(sMapSurface[1], iPlayerCurrentTileX - iMapDrawOffsetCol, iPlayerCurrentTileY - iMapDrawOffsetRow + 1, iMapDrawOffsetCol, iMapDrawOffsetRow, iAnimationFrame);
Expand Down Expand Up @@ -1022,5 +1022,5 @@ void MI_World::UseCloud(bool fUseCloud)

short iPlayerX, iPlayerY;
g_worldmap.GetPlayerPosition(&iPlayerX, &iPlayerY);
uiMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_poof, iPlayerX + iMapOffsetX - 8, iPlayerY + iMapOffsetY - 8, 4, 5));
m_parentMenu->AddEyeCandy(new EC_SingleAnimation(&rm->spr_poof, iPlayerX + iMapOffsetX - 8, iPlayerY + iMapOffsetY - 8, 4, 5));
}
Loading

0 comments on commit 0d023fc

Please sign in to comment.