Remove dead code from GUI1's menu class

The GUI1 menu code is only kept to support one UI feature, the help browser.
More specifically, the left-hand pane that looks like a treeview.

Remove the code for sorting the list in the UI. The help articles are ordered
by category, and there isn't a UI control for the user to sort them into a
different order.

Remove erase_item(), which wasn't called from anywhere.

Remove clear_item(), recalculate_pos() and assert_pos(). These three were
only called by the sorting code and erase_item().
This commit is contained in:
Steve Cotton 2024-01-09 21:06:19 +01:00 committed by Steve Cotton
parent d9aa71ac86
commit 22c8a1581d
3 changed files with 4 additions and 327 deletions

View file

@ -29,7 +29,7 @@
namespace help {
help_menu::help_menu(const section& toplevel, int max_height) :
gui::menu(empty_string_vector, true, max_height, -1, nullptr, &gui::menu::bluebg_style),
gui::menu(empty_string_vector, true, max_height, -1, &gui::menu::bluebg_style),
visible_items_(),
toplevel_(toplevel),
expanded_(),

View file

@ -35,121 +35,9 @@
namespace gui {
menu::basic_sorter::basic_sorter()
: alpha_sort_()
, numeric_sort_()
, id_sort_()
, redirect_sort_()
, pos_sort_()
{
set_id_sort(-1);
}
menu::basic_sorter& menu::basic_sorter::set_alpha_sort(int column)
{
alpha_sort_.insert(column);
return *this;
}
menu::basic_sorter& menu::basic_sorter::set_numeric_sort(int column)
{
numeric_sort_.insert(column);
return *this;
}
menu::basic_sorter& menu::basic_sorter::set_id_sort(int column)
{
id_sort_.insert(column);
return *this;
}
menu::basic_sorter& menu::basic_sorter::set_redirect_sort(int column, int to)
{
if(column != to) {
redirect_sort_.emplace(column, to);
}
return *this;
}
menu::basic_sorter& menu::basic_sorter::set_position_sort(int column, const std::vector<int>& pos)
{
pos_sort_[column] = pos;
return *this;
}
bool menu::basic_sorter::column_sortable(int column) const
{
const std::map<int,int>::const_iterator redirect = redirect_sort_.find(column);
if(redirect != redirect_sort_.end()) {
return column_sortable(redirect->second);
}
return alpha_sort_.count(column) == 1 || numeric_sort_.count(column) == 1 ||
pos_sort_.count(column) == 1 || id_sort_.count(column) == 1;
}
bool menu::basic_sorter::less(int column, const item& row1, const item& row2) const
{
const std::map<int,int>::const_iterator redirect = redirect_sort_.find(column);
if(redirect != redirect_sort_.end()) {
return less(redirect->second,row1,row2);
}
if(id_sort_.count(column) == 1) {
return row1.id < row2.id;
}
if(column < 0 || column >= int(row2.fields.size())) {
return false;
}
if(column >= int(row1.fields.size())) {
return true;
}
const std::string& item1 = row1.fields[column];
const std::string& item2 = row2.fields[column];
if(alpha_sort_.count(column) == 1) {
std::string::const_iterator begin1 = item1.begin(), end1 = item1.end(),
begin2 = item2.begin(), end2 = item2.end();
while(begin1 != end1 && is_wml_separator(*begin1)) {
++begin1;
}
while(begin2 != end2 && is_wml_separator(*begin2)) {
++begin2;
}
return std::lexicographical_compare(begin1,end1,begin2,end2,utils::chars_less_insensitive);
} else if(numeric_sort_.count(column) == 1) {
int val_1 = lexical_cast_default<int>(item1, 0);
int val_2 = lexical_cast_default<int>(item2, 0);
return val_1 > val_2;
}
const std::map<int,std::vector<int>>::const_iterator itor = pos_sort_.find(column);
if(itor != pos_sort_.end()) {
const std::vector<int>& pos = itor->second;
if(row1.id >= pos.size()) {
return false;
}
if(row2.id >= pos.size()) {
return true;
}
return pos[row1.id] < pos[row2.id];
}
return false;
}
menu::menu(const std::vector<std::string>& items,
bool click_selects, int max_height, int max_width,
const sorter* sorter_obj, style *menu_style, const bool auto_join)
style *menu_style, const bool auto_join)
: scrollarea(auto_join), silent_(false),
max_height_(max_height), max_width_(max_width),
max_items_(-1), item_height_(-1),
@ -160,7 +48,7 @@ menu::menu(const std::vector<std::string>& items,
num_selects_(true),
ignore_next_doubleclick_(false),
last_was_doubleclick_(false), use_ellipsis_(false),
sorter_(sorter_obj), sortby_(-1), sortreversed_(false), highlight_heading_(-1)
highlight_heading_(-1)
{
style_ = (menu_style) ? menu_style : &default_style;
style_->init();
@ -200,71 +88,9 @@ void menu::fill_items(const std::vector<std::string>& items, bool strip_spaces)
}
}
if(sortby_ >= 0) {
do_sort();
}
update_size();
}
namespace {
class sort_func
{
public:
sort_func(const menu::sorter& pred, int column) : pred_(&pred), column_(column)
{}
bool operator()(const menu::item& a, const menu::item& b) const
{
return pred_->less(column_,a,b);
}
private:
const menu::sorter* pred_;
int column_;
};
}
void menu::do_sort()
{
if(sorter_ == nullptr || sorter_->column_sortable(sortby_) == false) {
return;
}
const int selectid = selection();
std::stable_sort(items_.begin(), items_.end(), sort_func(*sorter_, sortby_));
if (sortreversed_)
std::reverse(items_.begin(), items_.end());
recalculate_pos();
if(selectid >= 0 && selectid < int(item_pos_.size())) {
move_selection_to(selectid, true, NO_MOVE_VIEWPORT);
}
queue_redraw();
}
void menu::recalculate_pos()
{
std::size_t sz = items_.size();
item_pos_.resize(sz);
for(std::size_t i = 0; i != sz; ++i)
item_pos_[items_[i].id] = i;
assert_pos();
}
void menu::assert_pos()
{
std::size_t sz = items_.size();
assert(item_pos_.size() == sz);
for(std::size_t n = 0; n != sz; ++n) {
assert(item_pos_[n] < sz && n == items_[item_pos_[n]].id);
}
}
void menu::update_scrollbar_grip_height()
{
set_full_size(items_.size());
@ -313,46 +139,6 @@ void menu::set_inner_location(const SDL_Rect& /*rect*/)
update_scrollbar_grip_height();
}
void menu::change_item(int pos1, int pos2,const std::string& str)
{
if(pos1 < 0 || pos1 >= int(item_pos_.size()) ||
pos2 < 0 || pos2 >= int(items_[item_pos_[pos1]].fields.size())) {
return;
}
items_[item_pos_[pos1]].fields[pos2] = str;
queue_redraw();
}
void menu::erase_item(std::size_t index)
{
std::size_t nb_items = items_.size();
if (index >= nb_items)
return;
--nb_items;
clear_item(nb_items);
// fix ordered positions of items
std::size_t pos = item_pos_[index];
item_pos_.erase(item_pos_.begin() + index);
items_.erase(items_.begin() + pos);
for(std::size_t i = 0; i != nb_items; ++i) {
std::size_t &n1 = item_pos_[i], &n2 = items_[i].id;
if (n1 > pos) --n1;
if (n2 > index) --n2;
}
assert_pos();
if (selected_ >= nb_items)
selected_ = nb_items - 1;
update_scrollbar_grip_height();
adjust_viewport_to_selection();
itemRects_.clear();
queue_redraw();
}
void menu::set_heading(const std::vector<std::string>& heading)
{
itemRects_.clear();
@ -632,13 +418,6 @@ void menu::handle_event(const SDL_Event& event)
}
}
if(sorter_ != nullptr) {
const int heading = hit_heading(x,y);
if(heading >= 0 && sorter_->column_sortable(heading)) {
sort_by(heading);
}
}
} else if(!mouse_locked() && event.type == SDL_MOUSEMOTION) {
if(click_selects_) {
const int item = hit(event.motion.x,event.motion.y);
@ -693,38 +472,6 @@ void menu::scroll(unsigned int)
queue_redraw();
}
void menu::set_sorter(sorter *s)
{
if(sortby_ >= 0) {
//clear an existing sort
sort_by(-1);
}
sorter_ = s;
sortreversed_ = false;
sortby_ = -1;
}
void menu::sort_by(int column)
{
const bool already_sorted = (column == sortby_);
if(already_sorted) {
if(sortreversed_ == false) {
sortreversed_ = true;
} else {
sortreversed_ = false;
sortby_ = -1;
}
} else {
sortby_ = column;
sortreversed_ = false;
}
do_sort();
itemRects_.clear();
queue_redraw();
}
SDL_Rect menu::style::item_size(const std::string& item) const {
SDL_Rect res {0,0,0,0};
std::vector<std::string> img_text_items = utils::split(item, IMG_TEXT_SEPARATOR, utils::REMOVE_EMPTY);
@ -837,14 +584,6 @@ const std::vector<int>& menu::column_widths() const
return column_widths_;
}
void menu::clear_item(int item)
{
SDL_Rect rect = get_item_rect(item);
if (rect.w == 0)
return;
queue_redraw(rect);
}
void menu::draw_row(const std::size_t row_index, const SDL_Rect& loc, ROW_TYPE type)
{
//called from style, draws one row's contents in a generic and adaptable way
@ -870,8 +609,6 @@ void menu::draw_row(const std::size_t row_index, const SDL_Rect& loc, ROW_TYPE t
if(highlight_heading_ == int(i)) {
draw::fill(draw_rect, {255,255,255,77});
} else if(sortby_ == int(i)) {
draw::fill(draw_rect, {255,255,255,26});
}
}
@ -912,18 +649,6 @@ void menu::draw_row(const std::size_t row_index, const SDL_Rect& loc, ROW_TYPE t
font::pango_draw_text(true, text_loc, style_->get_font_size(), font::NORMAL_COLOR, str,
(type == HEADING_ROW ? xpos+padding : xpos), y);
if(type == HEADING_ROW && sortby_ == int(i)) {
const texture sort_tex(image::get_texture(
image::locator{sortreversed_ ? "buttons/sliders/slider_arrow_blue.png"
: "buttons/sliders/slider_arrow_blue.png~ROTATE(180)"}));
if(sort_tex && sort_tex.w() <= widths[i] && sort_tex.h() <= loc.h) {
const int sort_x = xpos + widths[i] - sort_tex.w() - padding;
const int sort_y = loc.y + loc.h/2 - sort_tex.h()/2;
SDL_Rect dest = {sort_x, sort_y, sort_tex.w(), sort_tex.h()};
draw::blit(sort_tex, dest);
}
}
xpos += dir * (text_size.first + 5);
}
}

View file

@ -108,38 +108,9 @@ public:
std::size_t id;
};
class sorter
{
public:
virtual ~sorter() {}
virtual bool column_sortable(int column) const = 0;
virtual bool less(int column, const item& row1, const item& row2) const = 0;
};
class basic_sorter : public sorter
{
public:
basic_sorter();
virtual ~basic_sorter() {}
basic_sorter& set_alpha_sort(int column);
basic_sorter& set_numeric_sort(int column);
basic_sorter& set_id_sort(int column);
basic_sorter& set_redirect_sort(int column, int to);
basic_sorter& set_position_sort(int column, const std::vector<int>& pos);
protected:
virtual bool column_sortable(int column) const;
virtual bool less(int column, const item& row1, const item& row2) const;
private:
std::set<int> alpha_sort_, numeric_sort_, id_sort_;
std::map<int,int> redirect_sort_;
std::map<int,std::vector<int>> pos_sort_;
};
menu(const std::vector<std::string>& items,
bool click_selects=false, int max_height=-1, int max_width=-1,
const sorter* sorter_obj=nullptr, style *menu_style=nullptr, const bool auto_join=true);
style *menu_style=nullptr, const bool auto_join=true);
/** Default implementation, but defined out-of-line for efficiency reasons. */
~menu();
@ -150,11 +121,6 @@ public:
void move_selection_keeping_viewport(std::size_t id);
void reset_selection();
// allows user to change_item while running (dangerous)
void change_item(int pos1,int pos2,const std::string& str);
virtual void erase_item(std::size_t index);
void set_heading(const std::vector<std::string>& heading);
/**
@ -189,13 +155,6 @@ public:
// scrollarea override
void scroll(unsigned int pos) override;
//currently, menus do not manage the memory of their sorter
//this should be changed to a more object-oriented approach
void set_sorter(sorter *s);
void sort_by(int column);
int get_sort_by() const {return sortby_;}
bool get_sort_reversed() const {return sortreversed_;}
protected:
bool item_ends_with_image(const std::string& item) const;
virtual void handle_event(const SDL_Event& event) override;
@ -279,9 +238,6 @@ private:
//ellipsis calculation is slightly off, so default to false
bool use_ellipsis_;
const sorter* sorter_;
int sortby_;
bool sortreversed_;
int highlight_heading_;
/**
@ -290,10 +246,6 @@ private:
*/
void fill_items(const std::vector<std::string>& items, bool strip_spaces);
void do_sort();
void recalculate_pos();
void assert_pos();
void update_size();
enum SELECTION_MOVE_VIEWPORT { MOVE_VIEWPORT, NO_MOVE_VIEWPORT };
void set_selection_pos(std::size_t pos, bool silent=false, SELECTION_MOVE_VIEWPORT move_viewport=MOVE_VIEWPORT);