forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scores_ui.cpp
167 lines (153 loc) · 5.58 KB
/
scores_ui.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "scores_ui.h"
#include <algorithm>
#include <cassert>
#include <iterator>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "achievement.h"
#include "color.h"
#include "cursesdef.h"
#include "event_statistics.h"
#include "input.h"
#include "kill_tracker.h"
#include "output.h"
#include "point.h"
#include "stats_tracker.h"
#include "translations.h"
#include "ui.h"
#include "ui_manager.h"
static std::string get_achievements_text( const achievements_tracker &achievements )
{
std::string os;
std::vector<const achievement *> valid_achievements = achievements.valid_achievements();
valid_achievements.erase(
std::remove_if( valid_achievements.begin(), valid_achievements.end(),
[&]( const achievement * a ) {
return achievements.is_hidden( a );
} ), valid_achievements.end() );
using sortable_achievement =
std::tuple<achievement_completion, std::string, const achievement *>;
std::vector<sortable_achievement> sortable_achievements;
std::transform( valid_achievements.begin(), valid_achievements.end(),
std::back_inserter( sortable_achievements ),
[&]( const achievement * ach ) {
achievement_completion comp = achievements.is_completed( ach->id );
return std::make_tuple( comp, ach->name().translated(), ach );
} );
std::sort( sortable_achievements.begin(), sortable_achievements.end(), localized_compare );
for( const sortable_achievement &ach : sortable_achievements ) {
os += achievements.ui_text_for( std::get<const achievement *>( ach ) ) + "\n";
}
if( valid_achievements.empty() ) {
os += _( "This game has no valid achievements.\n" );
}
os += _( "Note that only achievements that existed when you started this game and still "
"exist now will appear here." );
return os;
}
static std::string get_scores_text( stats_tracker &stats )
{
std::string os;
std::vector<const score *> valid_scores = stats.valid_scores();
for( const score *scr : valid_scores ) {
os += scr->description( stats ) + "\n";
}
if( valid_scores.empty() ) {
os += _( "This game has no valid scores.\n" );
}
os += _( "\nNote that only scores that existed when you started this game and still exist now "
"will appear here." );
return os;
}
void show_scores_ui( const achievements_tracker &achievements, stats_tracker &stats,
const kill_tracker &kills )
{
catacurses::window w;
enum class tab_mode {
achievements,
scores,
kills,
num_tabs,
first_tab = achievements,
};
tab_mode tab = static_cast<tab_mode>( 0 );
input_context ctxt( "SCORES" );
ctxt.register_cardinal();
ctxt.register_action( "PAGE_UP" );
ctxt.register_action( "PAGE_DOWN" );
ctxt.register_action( "QUIT" );
ctxt.register_action( "PREV_TAB" );
ctxt.register_action( "NEXT_TAB" );
ctxt.register_action( "HELP_KEYBINDINGS" );
catacurses::window w_view;
scrolling_text_view view( w_view );
bool new_tab = true;
ui_adaptor ui;
const auto &init_windows = [&]( ui_adaptor & ui ) {
w = new_centered_win( TERMY - 2, FULL_SCREEN_WIDTH );
w_view = catacurses::newwin( getmaxy( w ) - 4, getmaxx( w ) - 1,
point( getbegx( w ), getbegy( w ) + 3 ) );
ui.position_from_window( w );
};
ui.on_screen_resize( init_windows );
// initialize explicitly here since w_view is used before first redraw
init_windows( ui );
const std::vector<std::pair<tab_mode, std::string>> tabs = {
{ tab_mode::achievements, _( "ACHIEVEMENTS" ) },
{ tab_mode::scores, _( "SCORES" ) },
{ tab_mode::kills, _( "KILLS" ) },
};
ui.on_redraw( [&]( const ui_adaptor & ) {
werase( w );
draw_tabs( w, tabs, tab );
draw_border_below_tabs( w );
wnoutrefresh( w );
view.draw( c_white );
} );
while( true ) {
if( new_tab ) {
switch( tab ) {
case tab_mode::achievements:
view.set_text( get_achievements_text( achievements ) );
break;
case tab_mode::scores:
view.set_text( get_scores_text( stats ) );
break;
case tab_mode::kills:
view.set_text( kills.get_kills_text() );
break;
case tab_mode::num_tabs:
assert( false );
break;
}
}
ui_manager::redraw();
const std::string action = ctxt.handle_input();
new_tab = false;
if( action == "RIGHT" || action == "NEXT_TAB" ) {
tab = static_cast<tab_mode>( static_cast<int>( tab ) + 1 );
if( tab >= tab_mode::num_tabs ) {
tab = tab_mode::first_tab;
}
new_tab = true;
} else if( action == "LEFT" || action == "PREV_TAB" ) {
tab = static_cast<tab_mode>( static_cast<int>( tab ) - 1 );
if( tab < tab_mode::first_tab ) {
tab = static_cast<tab_mode>( static_cast<int>( tab_mode::num_tabs ) - 1 );
}
new_tab = true;
} else if( action == "DOWN" ) {
view.scroll_down();
} else if( action == "UP" ) {
view.scroll_up();
} else if( action == "PAGE_DOWN" ) {
view.page_down();
} else if( action == "PAGE_UP" ) {
view.page_up();
} else if( action == "CONFIRM" || action == "QUIT" ) {
break;
}
}
}