1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2025-12-10 06:52:05 +01:00

Codechange: Precalculate group column rects.

Simplifies drawing group list which no longer needs to calculate horizontal positions for each row.
This commit is contained in:
Peter Nelson
2025-11-27 15:35:32 +00:00
committed by Peter Nelson
parent e15539c430
commit 91a12a3928

View File

@@ -193,20 +193,21 @@ void BuildGuiGroupList(GUIGroupList &list, bool fold, Owner owner, VehicleType v
} }
} }
/* Columns in the group list */
enum VehicleGroupColumns : uint8_t {
VGC_FOLD, ///< Fold / Unfold button.
VGC_NAME, ///< Group name.
VGC_PROTECT, ///< Autoreplace protect icon.
VGC_AUTOREPLACE, ///< Autoreplace active icon.
VGC_PROFIT, ///< Profit icon.
VGC_NUMBER, ///< Number of vehicles in the group.
VGC_END
};
DECLARE_INCREMENT_DECREMENT_OPERATORS(VehicleGroupColumns);
class VehicleGroupWindow : public BaseVehicleListWindow { class VehicleGroupWindow : public BaseVehicleListWindow {
private: private:
/* Columns in the group list */
enum ListColumns : uint8_t {
VGC_FOLD, ///< Fold / Unfold button.
VGC_NAME, ///< Group name.
VGC_PROTECT, ///< Autoreplace protect icon.
VGC_AUTOREPLACE, ///< Autoreplace active icon.
VGC_PROFIT, ///< Profit icon.
VGC_NUMBER, ///< Number of vehicles in the group.
VGC_END
};
GroupID group_sel = GroupID::Invalid(); ///< Selected group (for drag/drop) GroupID group_sel = GroupID::Invalid(); ///< Selected group (for drag/drop)
GroupID group_rename = GroupID::Invalid(); ///< Group being renamed, GroupID::Invalid() if none GroupID group_rename = GroupID::Invalid(); ///< Group being renamed, GroupID::Invalid() if none
GroupID group_over = GroupID::Invalid(); ///< Group over which a vehicle is dragged, GroupID::Invalid() if none GroupID group_over = GroupID::Invalid(); ///< Group over which a vehicle is dragged, GroupID::Invalid() if none
@@ -216,6 +217,7 @@ private:
Scrollbar *group_sb = nullptr; Scrollbar *group_sb = nullptr;
std::array<Dimension, VGC_END> column_size{}; ///< Size of the columns in the group list. std::array<Dimension, VGC_END> column_size{}; ///< Size of the columns in the group list.
std::array<Rect, VGC_END> column_rects{};
bool last_overlay_state = false; bool last_overlay_state = false;
/** /**
@@ -279,6 +281,28 @@ private:
WidgetDimensions::scaled.framerect.right; WidgetDimensions::scaled.framerect.right;
} }
/**
* Update precalculated column rects for drawing group information.
*/
void UpdateColumnRects()
{
bool rtl = _current_text_dir == TD_RTL;
Rect r = this->GetWidget<NWidgetCore>(WID_GL_LIST_GROUP)->GetCurrentRect().Shrink(WidgetDimensions::scaled.framerect);
/* Place columns before VGC_NAME from start to end. */
for (VehicleGroupColumns lc{}; lc != VGC_NAME; ++lc) {
this->column_rects[lc] = r.WithWidth(this->column_size[lc].width, rtl);
r = r.Indent(this->column_size[lc].width + WidgetDimensions::scaled.hsep_normal, rtl);
}
/* Place columns after VGC_NAME from end to start. */
for (VehicleGroupColumns lc{VGC_END - 1}; lc != VGC_NAME; --lc) {
this->column_rects[lc] = r.WithWidth(this->column_size[lc].width, !rtl);
r = r.Indent(this->column_size[lc].width + WidgetDimensions::scaled.hsep_normal, !rtl);
}
/* Name column fills remaining space. */
this->column_rects[VGC_NAME] = r;
}
/** /**
* Get the string to draw for the given group. * Get the string to draw for the given group.
* @param g_id Group to get string for. * @param g_id Group to get string for.
@@ -348,42 +372,33 @@ private:
} }
/* draw fold / unfold button */ /* draw fold / unfold button */
r = r.Indent(indent * WidgetDimensions::scaled.hsep_indent, rtl);
if (has_children) { if (has_children) {
DrawSpriteIgnorePadding(Group::Get(g_id)->folded ? SPR_CIRCLE_FOLDED : SPR_CIRCLE_UNFOLDED, PAL_NONE, r.WithWidth(this->column_size[VGC_FOLD].width, rtl), SA_CENTER); DrawSpriteIgnorePadding(Group::Get(g_id)->folded ? SPR_CIRCLE_FOLDED : SPR_CIRCLE_UNFOLDED, PAL_NONE, r.WithX(this->column_rects[VGC_FOLD]).Translate(indent * level_width, 0), SA_CENTER);
} }
/* Group name text column shrinks to fit available space. */
int text_width = this->column_size[VGC_NAME].width - indent * WidgetDimensions::scaled.hsep_indent;
/* draw group name */ /* draw group name */
r = r.Indent(this->column_size[VGC_FOLD].width + WidgetDimensions::scaled.hsep_normal, rtl); DrawString(r.WithX(this->column_rects[VGC_NAME]).Indent(indent * WidgetDimensions::scaled.hsep_indent, rtl).CentreToHeight(this->column_size[VGC_NAME].height), this->GetGroupNameString(g_id), colour);
DrawString(r.WithWidth(text_width, rtl).CentreToHeight(this->column_size[VGC_NAME].height), this->GetGroupNameString(g_id), colour);
/* draw autoreplace protection */ /* draw autoreplace protection */
r = r.Indent(text_width + WidgetDimensions::scaled.hsep_wide, rtl);
if (protection) { if (protection) {
DrawSpriteIgnorePadding(SPR_GROUP_REPLACE_PROTECT, PAL_NONE, r.WithWidth(this->column_size[VGC_PROTECT].width, rtl), SA_CENTER); DrawSpriteIgnorePadding(SPR_GROUP_REPLACE_PROTECT, PAL_NONE, r.WithX(this->column_rects[VGC_PROTECT]), SA_CENTER);
} }
/* draw autoreplace status */ /* draw autoreplace status */
r = r.Indent(this->column_size[VGC_PROTECT].width + WidgetDimensions::scaled.hsep_normal, rtl);
if (stats.autoreplace_defined) { if (stats.autoreplace_defined) {
DrawSpriteIgnorePadding(SPR_GROUP_REPLACE_ACTIVE, stats.autoreplace_finished ? PALETTE_CRASH : PAL_NONE, r.WithWidth(this->column_size[VGC_AUTOREPLACE].width, rtl), SA_CENTER); DrawSpriteIgnorePadding(SPR_GROUP_REPLACE_ACTIVE, stats.autoreplace_finished ? PALETTE_CRASH : PAL_NONE, r.WithX(this->column_rects[VGC_AUTOREPLACE]), SA_CENTER);
} }
/* draw the profit icon */ /* draw the profit icon */
r = r.Indent(this->column_size[VGC_AUTOREPLACE].width + WidgetDimensions::scaled.hsep_normal, rtl); DrawSpriteIgnorePadding(this->GetGroupProfitSpriteID(g_id), PAL_NONE, r.WithX(this->column_rects[VGC_PROFIT]), SA_CENTER);
DrawSpriteIgnorePadding(this->GetGroupProfitSpriteID(g_id), PAL_NONE, r.WithWidth(this->column_size[VGC_PROFIT].width, rtl), SA_CENTER);
/* draw the number of vehicles of the group */ /* draw the number of vehicles of the group */
r = r.Indent(this->column_size[VGC_PROFIT].width + WidgetDimensions::scaled.hsep_normal, rtl);
int num_vehicle_with_subgroups = GetGroupNumVehicle(this->vli.company, g_id, this->vli.vtype); int num_vehicle_with_subgroups = GetGroupNumVehicle(this->vli.company, g_id, this->vli.vtype);
int num_vehicle = GroupStatistics::Get(this->vli.company, g_id, this->vli.vtype).num_vehicle; int num_vehicle = GroupStatistics::Get(this->vli.company, g_id, this->vli.vtype).num_vehicle;
if (IsAllGroupID(g_id) || IsDefaultGroupID(g_id) || num_vehicle_with_subgroups == num_vehicle) { if (IsAllGroupID(g_id) || IsDefaultGroupID(g_id) || num_vehicle_with_subgroups == num_vehicle) {
DrawString(r.CentreToHeight(this->column_size[VGC_NUMBER].height), GetString(STR_JUST_COMMA, num_vehicle), colour, SA_RIGHT | SA_FORCE, false, FS_SMALL); DrawString(r.WithX(this->column_rects[VGC_NUMBER]).CentreToHeight(this->column_size[VGC_NUMBER].height), GetString(STR_JUST_COMMA, num_vehicle), colour, SA_RIGHT | SA_FORCE, false, FS_SMALL);
} else { } else {
DrawString(r.CentreToHeight(this->column_size[VGC_NUMBER].height), GetString(STR_GROUP_COUNT_WITH_SUBGROUP, num_vehicle, num_vehicle_with_subgroups - num_vehicle), colour, SA_RIGHT | SA_FORCE); DrawString(r.WithX(this->column_rects[VGC_NUMBER]).CentreToHeight(this->column_size[VGC_NUMBER].height), GetString(STR_GROUP_COUNT_WITH_SUBGROUP, num_vehicle, num_vehicle_with_subgroups - num_vehicle), colour, SA_RIGHT | SA_FORCE);
} }
} }
@@ -997,6 +1012,7 @@ public:
{ {
this->group_sb->SetCapacityFromWidget(this, WID_GL_LIST_GROUP); this->group_sb->SetCapacityFromWidget(this, WID_GL_LIST_GROUP);
this->vscroll->SetCapacityFromWidget(this, WID_GL_LIST_VEHICLE); this->vscroll->SetCapacityFromWidget(this, WID_GL_LIST_VEHICLE);
this->UpdateColumnRects();
} }
void OnDropdownSelect(WidgetID widget, int index, int) override void OnDropdownSelect(WidgetID widget, int index, int) override