Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/2407-S0-sortable-ta…
Browse files Browse the repository at this point in the history
…ble' into 2407-vehicle-trading-between-players
  • Loading branch information
Ranran-the-JuicyPork committed Jul 24, 2024
2 parents 555c466 + 03e6fbd commit 25b020a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 33 deletions.
99 changes: 73 additions & 26 deletions gui/components/sortable_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ void table_cell_item_t::draw(scr_coord offset)



text_cell_t::text_cell_t(const char* text_, PIXVAL col, align_t align_)
text_cell_t::text_cell_t(const char* text_, PIXVAL col, align_t align_, bool underlined_)
{
text= text_;
color= col;
align=align_;
underlined = underlined_;
min_size = scr_size(proportional_string_width(translator::translate(text)), LINESPACE);
set_size(min_size);
}
Expand All @@ -76,22 +77,34 @@ void text_cell_t::draw(scr_coord offset)

offset+=pos;
display_proportional_clip_rgb(offset.x+ draw_offset.x, offset.y+ draw_offset.y, translator::translate(text), ALIGN_LEFT, color, false);
if (underlined) {
display_fillbox_wh_clip_rgb(offset.x + draw_offset.x, offset.y + draw_offset.y + LINESPACE-1, min_size.w, 1, color, false);
}
}

coord_cell_t::coord_cell_t(const char* text, koord coord_, PIXVAL color, align_t align)
: text_cell_t((text==NULL && coord_!=koord::invalid) ? coord.get_fullstr() : text, color, align)
: text_cell_t((text==NULL && coord_!=koord::invalid) ? coord.get_fullstr() : text, color, align, coord_ == koord::invalid ? false : true)
{
coord = coord_;
min_size = scr_size(proportional_string_width(translator::translate(get_text())), LINESPACE);
set_size(min_size);
}


value_cell_t::value_cell_t(sint64 value_, gui_chart_t::chart_suffix_t suffix, align_t align_, PIXVAL col)
value_cell_t::value_cell_t(sint64 value_, gui_chart_t::chart_suffix_t suffix_, align_t align_, PIXVAL col)
{
color = col;
align = align_;
value=value_;
suffix= suffix_;

set_value(value_);
set_size(min_size);
}

void value_cell_t::set_value(sint64 value_)
{
value = value_;
buf.clear();

switch(suffix)
{
Expand Down Expand Up @@ -173,7 +186,10 @@ value_cell_t::value_cell_t(sint64 value_, gui_chart_t::chart_suffix_t suffix, al
break;
}
min_size = scr_size(proportional_string_width(buf), LINESPACE);
set_size(min_size);
if (min_size.w> (size.w - L_CELL_PADDING*2) ) {
dbg->warning("value_cell_t::set_value", "Cell's text width has been changed to %i, exceeding the column width of %i.", min_size.w, size.w);
}
set_width(size.w - L_CELL_PADDING*2); // recalc draw_offset.x
}

void value_cell_t::draw(scr_coord offset)
Expand All @@ -185,21 +201,27 @@ void value_cell_t::draw(scr_coord offset)
}


values_cell_t::values_cell_t(sint64 value_, sint64 sub_value_, PIXVAL col)
: value_cell_t(value_)
values_cell_t::values_cell_t(sint64 value_, sint64 sub_value_, PIXVAL col, bool single_line_)
{
buf.clear();
sub_buf.clear();

color = col;
min_size.h = LINESPACE;
align = centered;
single_line = single_line_;

set_values(value_, sub_value_);

set_size(min_size);
}

sub_value=sub_value_;
void values_cell_t::set_values(sint64 value_, sint64 sub_value_)
{
value = value_;
sub_value = sub_value_;

bool two_lines = false;
buf.clear();
sub_buf.clear();

if (value==0 && sub_value==0) {
if (value == 0 && sub_value == 0) {
buf.append("-");
color = SYSCOL_TEXT_WEAK;
}
Expand All @@ -208,31 +230,40 @@ values_cell_t::values_cell_t(sint64 value_, sint64 sub_value_, PIXVAL col)
}
if (sub_value != 0) {
if (value != 0) {
// display two lines
sub_buf.printf("(%i)", sub_value);
min_size.h += LINESPACE;
two_lines = true;
if (single_line) {
buf.printf(" (%i)", sub_value);
}
else {
// display two lines
sub_buf.printf("(%i)", sub_value);
min_size.h += LINESPACE;
sub_draw_offset_x = proportional_string_width(sub_buf) / 2;
min_size.w = max(proportional_string_width(buf), sub_draw_offset_x * 2 + 1);
}
}
else {
buf.printf("(%i)", sub_value);
}
}
if (two_lines) {
sub_draw_offset_x = proportional_string_width(sub_buf) / 2;
min_size.w = max(proportional_string_width(buf), sub_draw_offset_x * 2 + 1);
}
else {
if (single_line) {
min_size.w = proportional_string_width(buf);
}
set_size(min_size);
if (min_size.w > (size.w - L_CELL_PADDING * 2)) {
dbg->warning("values_cell_t::set_value", "Cell's text width has been changed to %i, exceeding the column width of %i.", min_size.w, size.w);
}
set_width(size.w - L_CELL_PADDING * 2); // recalc draw_offset.x
}

void values_cell_t::set_width(scr_coord_val new_width)
{
const scr_coord_val row1_width = proportional_string_width(buf);

size.w = new_width + L_CELL_PADDING * 2;
draw_offset.x = (size.w - row1_width) / 2;
if (single_line) {
draw_offset.x = (size.w - min_size.w) / 2;
}
else {
draw_offset.x = (size.w - proportional_string_width(buf)) / 2;
}

}

void values_cell_t::draw(scr_coord offset)
Expand Down Expand Up @@ -320,6 +351,19 @@ int gui_sort_table_row_t::compare_text(const table_cell_item_t* a, const table_c
return STRICMP(a_text, b_text);
}

int gui_sort_table_row_t::compare_coord(const table_cell_item_t* a, const table_cell_item_t* b)
{
int cmp = 0;
const coord_cell_t* cell_a = dynamic_cast<const coord_cell_t*>(a);
const coord_cell_t* cell_b = dynamic_cast<const coord_cell_t*>(b);
cmp = cell_a->get_coord().x - cell_b->get_coord().x;
if (cmp == 0) {
cmp = cell_a->get_coord().y - cell_b->get_coord().y;
}
return cmp;
}


int gui_sort_table_row_t::compare(const table_cell_item_t* a, const table_cell_item_t* b)
{
sint64 cmp = 0;
Expand All @@ -336,6 +380,9 @@ int gui_sort_table_row_t::compare(const table_cell_item_t* a, const table_cell_i
case table_cell_item_t::cell_text:
cmp = gui_sort_table_row_t::compare_text(a, b);
break;
case table_cell_item_t::cell_coord:
cmp = gui_sort_table_row_t::compare_coord(a, b);
break;
case table_cell_item_t::cell_no_sorting:
default:
// nothing to do
Expand Down
28 changes: 22 additions & 6 deletions gui/components/sortable_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class table_cell_item_t : virtual public gui_component_t
cell_no_sorting = 0,
cell_value = 1,
cell_values = 2,
cell_text = 3
cell_text = 3,
cell_coord = 4
};

enum align_t {
Expand Down Expand Up @@ -65,10 +66,11 @@ class text_cell_t : public table_cell_item_t
{
const char* text; // only for direct access of non-translatable things. Do not use!
PIXVAL color;
bool underlined;
// bool is_bold;

public:
text_cell_t(const char* text = NULL, PIXVAL color = SYSCOL_TEXT, align_t align = left);
text_cell_t(const char* text = NULL, PIXVAL color = SYSCOL_TEXT, align_t align = left, bool underlined = false);

const uint8 get_type() const OVERRIDE { return cell_text; }

Expand All @@ -87,13 +89,16 @@ class coord_cell_t : public text_cell_t
coord_cell_t(const char* text = NULL, koord coord=koord::invalid, PIXVAL color = SYSCOL_TEXT, align_t align = left);

koord get_coord() const { return coord; }

const uint8 get_type() const OVERRIDE { return cell_coord; }
};


// The cell holds a single value for sorting
// Can specify a suffix.
class value_cell_t : public table_cell_item_t
{
gui_chart_t::chart_suffix_t suffix;
protected:
cbuffer_t buf;
PIXVAL color;
Expand All @@ -103,6 +108,9 @@ class value_cell_t : public table_cell_item_t
public:
value_cell_t(sint64 value, gui_chart_t::chart_suffix_t suffix= gui_chart_t::STANDARD, align_t align = left, PIXVAL color = SYSCOL_TEXT);

// When resetting the value, care must be taken not to increase the column width.
void set_value(sint64 value);

sint64 get_value() const { return value; }

const uint8 get_type() const OVERRIDE { return cell_value; }
Expand All @@ -112,18 +120,25 @@ class value_cell_t : public table_cell_item_t

// The cell holds two values for sorting
// There is no suffix
class values_cell_t : public value_cell_t
class values_cell_t : public table_cell_item_t
{
protected:
cbuffer_t sub_buf;
sint64 sub_value;
cbuffer_t buf, sub_buf;
PIXVAL color;
sint64 value, sub_value;
scr_coord_val sub_draw_offset_x = 0;
bool single_line;

public:
values_cell_t(sint64 value, sint64 sub_value, PIXVAL color = SYSCOL_TEXT);
// single_line: Displaying two values ​​on the same line
values_cell_t(sint64 value, sint64 sub_value, PIXVAL color = SYSCOL_TEXT, bool single_line=false);

void set_width(scr_coord_val new_width) OVERRIDE;

// When resetting the value, care must be taken not to increase the column width.
void set_values(sint64 value, sint64 sub_value=0);

sint64 get_value() const { return value; }
sint64 get_sub_value() const { return sub_value; }

const uint8 get_type() const OVERRIDE { return cell_values; }
Expand All @@ -138,6 +153,7 @@ class gui_sort_table_row_t : public gui_container_t, public gui_scrolled_list_t:
static sint64 compare_value(const table_cell_item_t* a, const table_cell_item_t* b);
static sint64 compare_values(const table_cell_item_t* a, const table_cell_item_t* b);
static int compare_text(const table_cell_item_t* a, const table_cell_item_t* b);
static int compare_coord(const table_cell_item_t* a, const table_cell_item_t* b);


protected:
Expand Down
2 changes: 1 addition & 1 deletion gui/components/sortable_table_header.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ bool gui_sort_table_header_t::infowin_event(const event_t* ev)
{
bool swallowed = gui_container_t::infowin_event(ev);

if (IS_LEFTRELEASE(ev) && getroffen(ev->mouse_pos)) {
if (!swallowed && IS_LEFTRELEASE(ev) && getroffen(ev->mouse_pos)) {
uint idx=0;
for (auto& cell : owned_cells) {
if (cell->getroffen(ev->mouse_pos)) {
Expand Down

0 comments on commit 25b020a

Please sign in to comment.