Skip to content

Commit

Permalink
Fixes to scores menu
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhilkinSerg committed Dec 4, 2024
1 parent 04cb3d9 commit 2ba3b85
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 53 deletions.
14 changes: 14 additions & 0 deletions data/raw/keybindings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4861,6 +4861,20 @@
{ "input_method": "keyboard_code", "mod": [ "ctrl" ], "key": "y" }
]
},
{
"type": "keybinding",
"id": "TOGGLE_MONSTER_GROUP",
"category": "SCORES_UI",
"name": "Toggle monster group",
"bindings": [ { "input_method": "keyboard_any", "key": "m" } ]
},
{
"type": "keybinding",
"id": "TOGGLE_NPC_GROUP",
"category": "SCORES_UI",
"name": "Toggle NPC group",
"bindings": [ { "input_method": "keyboard_any", "key": "n" } ]
},
{
"type": "keybinding",
"id": "HELP_KEYBINDINGS",
Expand Down
22 changes: 22 additions & 0 deletions src/enum_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,26 @@ inline bool operator!( E e )
return !static_cast<I>( e );
}

template<typename E>
static E &operator++( E &e )
{
using I = std::underlying_type_t<E>;
e = static_cast<E>( static_cast<I>( e ) + 1 );
if( e == enum_traits<E>::last ) {
e = enum_traits<E>::first;
}
return e;
}

template<typename E>
static E &operator--( E &e )
{
using I = std::underlying_type_t<E>;
if( e == enum_traits<E>::first ) {
e = enum_traits<E>::last;
}
e = static_cast<E>( static_cast<I>( e ) - 1 );
return e;
}

#endif // CATA_SRC_ENUM_TRAITS_H
103 changes: 50 additions & 53 deletions src/scores_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,19 @@
#include "ui.h"
#include "ui_manager.h"

enum class scores_ui_tab_enum : int {
enum class scores_ui_tab : int {
achievements = 0,
conducts,
scores,
kills,
num_tabs
};

static scores_ui_tab_enum &operator++( scores_ui_tab_enum &c )
{
c = static_cast<scores_ui_tab_enum>( static_cast<int>( c ) + 1 );
if( c == scores_ui_tab_enum::num_tabs ) {
c = static_cast<scores_ui_tab_enum>( 0 );
}
return c;
}

static scores_ui_tab_enum &operator--( scores_ui_tab_enum &c )
{
if( c == static_cast<scores_ui_tab_enum>( 0 ) ) {
c = scores_ui_tab_enum::num_tabs;
}
c = static_cast<scores_ui_tab_enum>( static_cast<int>( c ) - 1 );
return c;
}
template<>
struct enum_traits<scores_ui_tab> {
static constexpr scores_ui_tab first = scores_ui_tab::achievements;
static constexpr scores_ui_tab last = scores_ui_tab::num_tabs;
};

class scores_ui;

Expand All @@ -69,27 +57,32 @@ class scores_ui_impl : public cataimgui::window
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav ) {}

private:
void draw_achievements_text( bool use_conducts = false );
void draw_scores_text();
void draw_kills_text();
void draw_achievements_text( bool use_conducts = false ) const;
void draw_scores_text() const;
void draw_kills_text() const;

scores_ui_tab selected_tab = enum_traits<scores_ui_tab>::first;
scores_ui_tab switch_tab = enum_traits<scores_ui_tab>::last;

scores_ui_tab_enum selected_tab = scores_ui_tab_enum::achievements;
scores_ui_tab_enum switch_tab = scores_ui_tab_enum::num_tabs;
size_t window_width = ImGui::GetMainViewport()->Size.x * 8 / 9;
size_t window_height = ImGui::GetMainViewport()->Size.y * 8 / 9;

size_t window_width = ImGui::GetMainViewport()->Size.x / 3.0 * 2.0;
size_t window_height = ImGui::GetMainViewport()->Size.y / 3.0 * 2.0;
bool monster_group_collapsed = false;
bool npc_group_collapsed = false;

protected:
void draw_controls() override;
};

void scores_ui::draw_scores_ui()
{
input_context ctxt;
input_context ctxt( "SCORES_UI" );
scores_ui_impl p_impl;

ctxt.register_navigate_ui_list();
ctxt.register_leftright();
ctxt.register_action( "TOGGLE_MONSTER_GROUP" );
ctxt.register_action( "TOGGLE_NPC_GROUP" );
ctxt.register_action( "NEXT_TAB" );
ctxt.register_action( "PREV_TAB" );
ctxt.register_action( "SELECT" );
Expand All @@ -110,7 +103,7 @@ void scores_ui::draw_scores_ui()
}
}

void scores_ui_impl::draw_achievements_text( bool use_conducts )
void scores_ui_impl::draw_achievements_text( bool use_conducts ) const
{
const achievements_tracker &achievements = g->achievements();
if( !achievements.is_enabled() ) {
Expand Down Expand Up @@ -162,7 +155,7 @@ void scores_ui_impl::draw_achievements_text( bool use_conducts )
);
}

void scores_ui_impl::draw_scores_text()
void scores_ui_impl::draw_scores_text() const
{
stats_tracker &stats = g->stats();
std::vector<const score *> valid_scores = stats.valid_scores();
Expand All @@ -179,7 +172,7 @@ void scores_ui_impl::draw_scores_text()
);
}

void scores_ui_impl::draw_kills_text()
void scores_ui_impl::draw_kills_text() const
{
const kill_tracker &kills_data = g->get_kill_tracker();

Expand All @@ -197,7 +190,7 @@ void scores_ui_impl::draw_kills_text()
}

if( ImGui::CollapsingHeader( string_format( _( "Monster kills (%d):" ), monster_kills ).c_str(),
ImGuiTreeNodeFlags_DefaultOpen ) ) {
monster_group_collapsed ? ImGuiTreeNodeFlags_None : ImGuiTreeNodeFlags_DefaultOpen ) ) {
if( monster_kills == 0 ) {
ImGui::TextWrapped( "%s", _( "You haven't killed any monsters yet!" ) );
} else {
Expand All @@ -219,7 +212,7 @@ void scores_ui_impl::draw_kills_text()
}
}
if( ImGui::CollapsingHeader( string_format( _( "NPC kills (%d):" ), npc_kills ).c_str(),
ImGuiTreeNodeFlags_DefaultOpen ) ) {
npc_group_collapsed ? ImGuiTreeNodeFlags_None : ImGuiTreeNodeFlags_DefaultOpen ) ) {
if( npc_kills == 0 ) {
ImGui::TextWrapped( "%s", _( "You haven't killed any NPCs yet!" ) );
} else {
Expand All @@ -242,76 +235,80 @@ void scores_ui_impl::draw_controls()

if( last_action == "QUIT" ) {
return;
} else if( last_action == "TOGGLE_MONSTER_GROUP" ) {
monster_group_collapsed = !monster_group_collapsed;
} else if( last_action == "TOGGLE_NPC_GROUP" ) {
npc_group_collapsed = !npc_group_collapsed;
} else if( last_action == "UP" ) {
ImGui::SetKeyboardFocusHere( -1 );
ImGui::SetScrollY( ImGui::GetScrollY() - ImGui::GetTextLineHeightWithSpacing() );
} else if( last_action == "DOWN" ) {
ImGui::SetKeyboardFocusHere( 1 );
ImGui::SetScrollY( ImGui::GetScrollY() + ImGui::GetTextLineHeightWithSpacing() );
} else if( last_action == "NEXT_TAB" || last_action == "RIGHT" ) {
ImGui::SetScrollY( 0 );
switch_tab = selected_tab;
++switch_tab;
} else if( last_action == "PREV_TAB" || last_action == "LEFT" ) {
ImGui::SetScrollY( 0 );
switch_tab = selected_tab;
--switch_tab;
} else if( last_action == "PAGE_UP" ) {
ImGui::SetWindowFocus(); // Dumb hack! Clear our focused item so listbox selection isn't nav highlighted.
ImGui::SetScrollY( ImGui::GetScrollY() - ( window_height / 5.0f ) );
ImGui::SetScrollY( ImGui::GetScrollY() - window_height );
} else if( last_action == "PAGE_DOWN" ) {
ImGui::SetWindowFocus(); // Dumb hack! Clear our focused item so listbox selection isn't nav highlighted.
ImGui::SetScrollY( ImGui::GetScrollY() + ( window_height / 5.0f ) );
ImGui::SetScrollY( ImGui::GetScrollY() + window_height );
}

ImGuiTabItemFlags_ flags = ImGuiTabItemFlags_None;

if( ImGui::BeginTabBar( "##TAB_BAR" ) ) {
flags = ImGuiTabItemFlags_None;
if( switch_tab == scores_ui_tab_enum::achievements ) {
if( switch_tab == scores_ui_tab::achievements ) {
flags = ImGuiTabItemFlags_SetSelected;
switch_tab = scores_ui_tab_enum::num_tabs;
switch_tab = scores_ui_tab::num_tabs;
}
if( ImGui::BeginTabItem( _( "ACHIEVEMENTS" ), nullptr, flags ) ) {
selected_tab = scores_ui_tab_enum::achievements;
selected_tab = scores_ui_tab::achievements;
ImGui::EndTabItem();
}
flags = ImGuiTabItemFlags_None;
if( switch_tab == scores_ui_tab_enum::conducts ) {
if( switch_tab == scores_ui_tab::conducts ) {
flags = ImGuiTabItemFlags_SetSelected;
switch_tab = scores_ui_tab_enum::num_tabs;
switch_tab = scores_ui_tab::num_tabs;
}
if( ImGui::BeginTabItem( _( "CONDUCTS" ), nullptr, flags ) ) {
selected_tab = scores_ui_tab_enum::conducts;
selected_tab = scores_ui_tab::conducts;
ImGui::EndTabItem();
}
flags = ImGuiTabItemFlags_None;
if( switch_tab == scores_ui_tab_enum::scores ) {
if( switch_tab == scores_ui_tab::scores ) {
flags = ImGuiTabItemFlags_SetSelected;
switch_tab = scores_ui_tab_enum::num_tabs;
switch_tab = scores_ui_tab::num_tabs;
}
if( ImGui::BeginTabItem( _( "SCORES" ), nullptr, flags ) ) {
selected_tab = scores_ui_tab_enum::scores;
selected_tab = scores_ui_tab::scores;
ImGui::EndTabItem();
}
flags = ImGuiTabItemFlags_None;
if( switch_tab == scores_ui_tab_enum::kills ) {
if( switch_tab == scores_ui_tab::kills ) {
flags = ImGuiTabItemFlags_SetSelected;
switch_tab = scores_ui_tab_enum::num_tabs;
switch_tab = scores_ui_tab::num_tabs;
}
if( ImGui::BeginTabItem( _( "KILLS" ), nullptr, flags ) ) {
selected_tab = scores_ui_tab_enum::kills;
selected_tab = scores_ui_tab::kills;
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}

if( selected_tab == scores_ui_tab_enum::achievements ) {
if( selected_tab == scores_ui_tab::achievements ) {
draw_achievements_text( false );
}
if( selected_tab == scores_ui_tab_enum::conducts ) {
if( selected_tab == scores_ui_tab::conducts ) {
draw_achievements_text( true );
}
if( selected_tab == scores_ui_tab_enum::scores ) {
if( selected_tab == scores_ui_tab::scores ) {
draw_scores_text();
}
if( selected_tab == scores_ui_tab_enum::kills ) {
if( selected_tab == scores_ui_tab::kills ) {
draw_kills_text();
}

Expand Down

0 comments on commit 2ba3b85

Please sign in to comment.