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 26, 2024
2 parents 555c466 + e336f63 commit 4d5b0db
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 37 deletions.
144 changes: 116 additions & 28 deletions gui/components/sortable_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "../../simworld.h"
#include "../../simcolor.h"
#include "../../display/simgraph.h"
#include "../../display/viewport.h"

#include "../../dataobj/translator.h"

Expand Down Expand Up @@ -61,11 +62,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 +78,74 @@ 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)

coord_cell_t::coord_cell_t(const char* alt_text, koord coord_, align_t align_)
{
coord = coord_;
min_size = scr_size(proportional_string_width(translator::translate(get_text())), LINESPACE);
align = align_;
if (alt_text != NULL) {
buf.printf("%s", alt_text);
}
else {
buf.append("-");
}
min_size = scr_size(proportional_string_width(buf), LINESPACE);
set_size(min_size);
}

coord_cell_t::coord_cell_t(koord coord_, align_t align_)
{
coord = coord_;
align = align_;
if (coord != koord::invalid) {
buf.printf("%s", coord.get_str());
show_posicon = true;
}
else {
buf.append("-");
}
min_size = scr_size(proportional_string_width(buf) + (D_POS_BUTTON_WIDTH + 2) * show_posicon, show_posicon ? max(LINESPACE, D_POS_BUTTON_HEIGHT) : 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)
void coord_cell_t::draw(scr_coord offset)
{
table_cell_item_t::draw(offset);

offset += pos;
PIXVAL text_col= SYSCOL_TEXT;
if (world()->get_viewport()->get_world_position() == coord) {
text_col=SYSCOL_TEXT_HIGHLIGHT;
if (show_posicon){
display_color_img(skinverwaltung_t::posbutton->get_image_id(SKIN_BUTTON_POS_PRESSED), offset.x + draw_offset.x, offset.y + (size.h-D_POS_BUTTON_HEIGHT)/2, 0, false, false);
}
}
display_proportional_clip_rgb(offset.x + draw_offset.x + (D_POS_BUTTON_WIDTH+2) * show_posicon, offset.y + draw_offset.y, buf, ALIGN_LEFT, text_col, false);
if (coord != koord::invalid) {
display_fillbox_wh_clip_rgb(offset.x + draw_offset.x + (D_POS_BUTTON_WIDTH + 2) * show_posicon, offset.y + draw_offset.y + LINESPACE - 1, min_size.w - (D_POS_BUTTON_WIDTH + 2) * show_posicon, 1, text_col, false);
}
}


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 +227,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 +242,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 +271,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 +392,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 +421,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
39 changes: 31 additions & 8 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 @@ -79,21 +81,29 @@ class text_cell_t : public table_cell_item_t


// Hold the coordinates and click a cell to jump to the coordinates.
class coord_cell_t : public text_cell_t
class coord_cell_t : public table_cell_item_t
{
koord coord;
cbuffer_t buf;
bool show_posicon=false;

public:
coord_cell_t(const char* text = NULL, koord coord=koord::invalid, PIXVAL color = SYSCOL_TEXT, align_t align = left);
coord_cell_t(const char* alt_text = NULL, koord coord=koord::invalid, align_t align = left);
coord_cell_t(koord coord = koord::invalid, align_t align = left);

koord get_coord() const { return coord; }

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

void draw(scr_coord offset) OVERRIDE;
};


// 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,27 +113,39 @@ 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.
virtual void set_value(sint64 value);

sint64 get_value() const { return value; }

void set_color(PIXVAL color_) { color = color_; }

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

void draw(scr_coord offset) OVERRIDE;
};

// 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 +160,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 4d5b0db

Please sign in to comment.