Skip to content

Commit

Permalink
Reworked PDA filter selection mechanism
Browse files Browse the repository at this point in the history
More robust, fixed few known and possible edge cases bugs
  • Loading branch information
Xottab-DUTY committed Aug 11, 2023
1 parent ca6a831 commit b91375b
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 156 deletions.
2 changes: 2 additions & 0 deletions src/xrGame/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,8 @@ set(SRC_FILES
"ui/UIMap.cpp"
"ui/UIMapDesc.cpp"
"ui/UIMapDesc.h"
"ui/UIMapFilters.cpp"
"ui/UIMapFilters.h"
"ui/UIMap.h"
"ui/UIMapInfo.cpp"
"ui/UIMapInfo.h"
Expand Down
161 changes: 161 additions & 0 deletions src/xrGame/ui/UIMapFilters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#include "StdAfx.h"

#include "UIMapFilters.h"
#include "UIHelper.h"
#include "xrUICore/Buttons/UICheckButton.h"

CUIMapFilters::CUIMapFilters() : CUIWindow("Map locations filters") {}

bool CUIMapFilters::Init(CUIXml& xml)
{
if (!CUIXmlInit::InitWindow(xml, "filters_wnd", 0, this, false))
{
if (const auto parent = GetParent())
{
SetWndPos(parent->GetWndPos());
SetWndSize(parent->GetWndSize());
}
}
constexpr std::tuple<eSpotsFilter, pcstr> filters[] =
{
{ Treasures, "filter_treasures" },
{ QuestNpcs, "filter_primary_objects" },
{ SecondaryTasks, "filter_secondary_tasks" },
{ PrimaryObjects, "filter_quest_npcs" },
};

for (const auto& [filter_id, filter_section] : filters)
{
auto& filter = m_filters[filter_id];
filter = UIHelper::CreateCheck(xml, filter_section, this, false);
if (filter)
{
filter->SetMessageTarget(this);
filter->SetWindowName(filter_section);
filter->SetCheck(true);
}
m_filters_state[filter_id] = true;
}

return true;
}

void CUIMapFilters::Reset()
{
inherited::Reset();
SelectFilter(false);
}

bool CUIMapFilters::OnKeyboardAction(int dik, EUIMessages keyboard_action)
{
if (const auto filter = GetSelectedFilter())
{
if (keyboard_action != WINDOW_KEY_PRESSED)
return true; // intercept all

switch (GetBindedAction(dik, EKeyContext::UI))
{
case kUI_BACK:
SelectFilter(false);
return true;

case kUI_ACCEPT:
filter->OnMouseDown(MOUSE_1);
return true;

case kUI_MOVE_LEFT:
case kUI_MOVE_DOWN:
SelectFilter(true, false);
return true;

case kUI_MOVE_RIGHT:
case kUI_MOVE_UP:
SelectFilter(true, true);
return true;
}
}
return inherited::OnKeyboardAction(dik, keyboard_action);
}

bool CUIMapFilters::OnControllerAction(int axis, float x, float y, EUIMessages controller_action)
{
switch (GetBindedAction(axis, EKeyContext::UI))
{
default:
return OnKeyboardAction(axis, controller_action);
case kUI_MOVE:
if (GetSelectedFilter())
return true; // just screw it for now
}
return inherited::OnControllerAction(axis, x, y, controller_action);
}

void CUIMapFilters::SendMessage(CUIWindow* pWnd, s16 msg, void* pData)
{
// This cycle could be implemented through CUIWndCallback,
// but we don't need it too much here
for (u32 i = 0; i < Filter_Count; ++i)
{
if (m_filters[i] == pWnd && msg == BUTTON_CLICKED)
{
m_filters_state[i] = m_filters[i]->GetCheck();
GetMessageTarget()->SendMessage(this, PDA_TASK_RELOAD_FILTERS, nullptr);
return;
}
}
if (msg == PDA_TASK_SELECT_FILTERS)
{
SelectFilter(!GetSelectedFilter());
return;
}
if (msg == WINDOW_KEYBOARD_CAPTURE_LOST || msg == WINDOW_FOCUS_LOST)
{
SelectFilter(false);
return;
}
inherited::SendMessage(pWnd, msg, pData);
}

CUICheckButton* CUIMapFilters::GetSelectedFilter() const
{
if (m_selected_filter == -1)
return nullptr;
return m_filters[m_selected_filter];
}

void CUIMapFilters::SelectFilter(bool select, bool next /*= true*/)
{
auto& cursor = GetUICursor();

if (!select)
{
m_selected_filter = -1;
cursor.WarpToWindow(nullptr, pInput->IsCurrentInputTypeController());
}
else
{
if (next)
{
if (m_selected_filter < int(m_filters.size() - 1))
m_selected_filter++;
else
m_selected_filter = 0;
}
else // prev
{
if (m_selected_filter > 0)
m_selected_filter--;
else
m_selected_filter = int(m_filters.size() - 1);
}
cursor.WarpToWindow(m_filters[m_selected_filter]);
cursor.PauseAutohiding(true);
}
}

void CUIMapFilters::SetFilterEnabled(eSpotsFilter filter, bool enable)
{
m_filters_state[filter] = enable;
if (m_filters[filter])
m_filters[filter]->SetCheck(enable);
}
53 changes: 53 additions & 0 deletions src/xrGame/ui/UIMapFilters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include "xrUICore/Windows/UIWindow.h"

class CUICheckButton;

class CUIMapFilters final : public CUIWindow
{
typedef CUIWindow inherited;

public:
enum eSpotsFilter
{
Treasures,
QuestNpcs,
SecondaryTasks,
PrimaryObjects,

Filter_Count
};

private:
std::array<CUICheckButton*, Filter_Count> m_filters{};
std::array<bool, Filter_Count> m_filters_state{};
int m_selected_filter{ -1 };

public:
CUIMapFilters();

bool Init(CUIXml& xml);

pcstr GetDebugType() override { return "CUIMapFilters"; }

void Reset() override;

bool OnKeyboardAction(int dik, EUIMessages keyboard_action) override;
bool OnControllerAction(int axis, float x, float y, EUIMessages controller_action) override;

void SendMessage(CUIWindow* pWnd, s16 msg, void* pData) override;

[[nodiscard]]
bool IsFilterEnabled(eSpotsFilter filter) const
{
return m_filters_state[filter];
}

void SetFilterEnabled(eSpotsFilter filter, bool enable);

private:
CUICheckButton* GetSelectedFilter() const;
void SelectFilter(bool select, bool next = true);
};

5 changes: 4 additions & 1 deletion src/xrGame/ui/UIPdaWnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ void RearrangeTabButtons(CUITabControl* pTab)

bool CUIPdaWnd::OnKeyboardAction(int dik, EUIMessages keyboard_action)
{
if (inherited::OnKeyboardAction(dik, keyboard_action))
return true;

switch (GetBindedAction(dik, EKeyContext::UI))
{
case kUI_TAB_PREV:
Expand All @@ -424,5 +427,5 @@ bool CUIPdaWnd::OnKeyboardAction(int dik, EUIMessages keyboard_action)
if (IsBinded(kACTIVE_JOBS, dik))
goto hide_pda;

return inherited::OnKeyboardAction(dik, keyboard_action);
return false;
}
Loading

0 comments on commit b91375b

Please sign in to comment.