1
0
Fork 0

Codechange: Use EnumBitSet for picker window invalidation flags.

pull/13551/head
Peter Nelson 2025-02-13 18:48:25 +00:00 committed by Peter Nelson
parent 75387b9e2b
commit 71dafdb21e
6 changed files with 30 additions and 27 deletions

View File

@ -118,7 +118,6 @@ public:
{
ResetObjectToPlace();
this->ConstructWindow();
this->InvalidateData();
}
void SetStringParameters(WidgetID widget) const override
@ -288,7 +287,8 @@ public:
if (!gui_scope) return;
if ((data & PickerWindow::PFI_POSITION) != 0) {
PickerInvalidations pi(data);
if (pi.Test(PickerInvalidation::Position)) {
const auto objclass = ObjectClass::Get(_object_gui.sel_class);
const auto spec = objclass->GetSpec(_object_gui.sel_type);
_object_gui.sel_view = std::min<int>(_object_gui.sel_view, spec->views - 1);
@ -302,7 +302,7 @@ public:
case WID_BO_OBJECT_SPRITE:
if (_object_gui.sel_type != std::numeric_limits<uint16_t>::max()) {
_object_gui.sel_view = this->GetWidget<NWidgetBase>(widget)->GetParentWidget<NWidgetMatrix>()->GetCurrentElement();
this->InvalidateData(PickerWindow::PFI_POSITION);
this->InvalidateData(PickerInvalidation::Position);
if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
}
break;

View File

@ -234,7 +234,7 @@ void PickerWindow::ConstructWindow()
this->FinishInitNested(this->window_number);
this->InvalidateData(PFI_CLASS | PFI_TYPE | PFI_POSITION | PFI_VALIDATE);
this->InvalidateData(PICKER_INVALIDATION_ALL);
}
void PickerWindow::Close(int data)
@ -340,7 +340,7 @@ void PickerWindow::OnClick(Point pt, WidgetID widget, int)
if (this->callbacks.GetSelectedClass() != *it || HasBit(this->callbacks.mode, PFM_ALL)) {
ClrBit(this->callbacks.mode, PFM_ALL); // Disable showing all.
this->callbacks.SetSelectedClass(*it);
this->InvalidateData(PFI_TYPE | PFI_POSITION | PFI_VALIDATE);
this->InvalidateData({PickerInvalidation::Type, PickerInvalidation::Position, PickerInvalidation::Validate});
}
if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
CloseWindowById(WC_SELECT_STATION, 0);
@ -355,7 +355,7 @@ void PickerWindow::OnClick(Point pt, WidgetID widget, int)
/* Enabling used or saved filters automatically enables all. */
SetBit(this->callbacks.mode, PFM_ALL);
}
this->InvalidateData(PFI_CLASS | PFI_TYPE | PFI_POSITION);
this->InvalidateData({PickerInvalidation::Class, PickerInvalidation::Type, PickerInvalidation::Position});
break;
/* Type Picker */
@ -371,14 +371,14 @@ void PickerWindow::OnClick(Point pt, WidgetID widget, int)
} else {
this->callbacks.saved.erase(it);
}
this->InvalidateData(PFI_TYPE);
this->InvalidateData(PickerInvalidation::Type);
break;
}
if (this->callbacks.IsTypeAvailable(item.class_index, item.index)) {
this->callbacks.SetSelectedClass(item.class_index);
this->callbacks.SetSelectedType(item.index);
this->InvalidateData(PFI_POSITION);
this->InvalidateData(PickerInvalidation::Position);
}
if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
CloseWindowById(WC_SELECT_STATION, 0);
@ -391,16 +391,17 @@ void PickerWindow::OnInvalidateData(int data, bool gui_scope)
{
if (!gui_scope) return;
if ((data & PFI_CLASS) != 0) this->classes.ForceRebuild();
if ((data & PFI_TYPE) != 0) this->types.ForceRebuild();
PickerInvalidations pi(data);
if (pi.Test(PickerInvalidation::Class)) this->classes.ForceRebuild();
if (pi.Test(PickerInvalidation::Type)) this->types.ForceRebuild();
this->BuildPickerClassList();
if ((data & PFI_VALIDATE) != 0) this->EnsureSelectedClassIsValid();
if ((data & PFI_POSITION) != 0) this->EnsureSelectedClassIsVisible();
if (pi.Test(PickerInvalidation::Validate)) this->EnsureSelectedClassIsValid();
if (pi.Test(PickerInvalidation::Position)) this->EnsureSelectedClassIsVisible();
this->BuildPickerTypeList();
if ((data & PFI_VALIDATE) != 0) this->EnsureSelectedTypeIsValid();
if ((data & PFI_POSITION) != 0) this->EnsureSelectedTypeIsVisible();
if (pi.Test(PickerInvalidation::Validate)) this->EnsureSelectedTypeIsValid();
if (pi.Test(PickerInvalidation::Position)) this->EnsureSelectedTypeIsVisible();
if (this->has_type_picker) {
SetWidgetLoweredState(WID_PW_MODE_ALL, HasBit(this->callbacks.mode, PFM_ALL));
@ -433,13 +434,13 @@ void PickerWindow::OnEditboxChanged(WidgetID wid)
case WID_PW_CLASS_FILTER:
this->class_string_filter.SetFilterTerm(this->class_editbox.text.GetText());
this->classes.SetFilterState(!class_string_filter.IsEmpty());
this->InvalidateData(PFI_CLASS);
this->InvalidateData(PickerInvalidation::Class);
break;
case WID_PW_TYPE_FILTER:
this->type_string_filter.SetFilterTerm(this->type_editbox.text.GetText());
this->types.SetFilterState(!type_string_filter.IsEmpty());
this->InvalidateData(PFI_TYPE);
this->InvalidateData(PickerInvalidation::Type);
break;
default:
@ -513,7 +514,7 @@ void PickerWindow::RefreshUsedTypeList()
this->callbacks.used.clear();
this->callbacks.FillUsedItems(this->callbacks.used);
this->InvalidateData(PFI_TYPE);
this->InvalidateData(PickerInvalidation::Type);
}
/** Builds the filter list of types. */

View File

@ -153,12 +153,15 @@ public:
PFM_SAVED = 2, ///< Show saved types.
};
enum PickerFilterInvalidation : uint8_t {
PFI_CLASS = 1U << 0, ///< Refresh the class list.
PFI_TYPE = 1U << 1, ///< Refresh the type list.
PFI_POSITION = 1U << 2, ///< Update scroll positions.
PFI_VALIDATE = 1U << 3, ///< Validate selected item.
enum class PickerInvalidation : uint8_t {
Class, ///< Refresh the class list.
Type, ///< Refresh the type list.
Position, ///< Update scroll positions.
Validate, ///< Validate selected item.
};
using PickerInvalidations = EnumBitSet<PickerInvalidation, uint8_t>;
static constexpr PickerInvalidations PICKER_INVALIDATION_ALL{PickerInvalidation::Class, PickerInvalidation::Type, PickerInvalidation::Position, PickerInvalidation::Validate};
static const int PREVIEW_WIDTH = 64; ///< Width of each preview button.
static const int PREVIEW_HEIGHT = 48; ///< Height of each preview button.
@ -185,6 +188,8 @@ public:
PCWHK_FOCUS_FILTER_BOX, ///< Focus the edit box for editing the filter string
};
void InvalidateData(PickerInvalidations data) { this->Window::InvalidateData(data.base()); }
protected:
void ConstructWindow();

View File

@ -1081,7 +1081,6 @@ public:
{
this->coverage_height = 2 * GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal;
this->ConstructWindow();
this->InvalidateData();
}
void OnInit() override
@ -1854,7 +1853,6 @@ struct BuildRailWaypointWindow : public PickerWindow {
BuildRailWaypointWindow(WindowDesc &desc, Window *parent) : PickerWindow(desc, parent, TRANSPORT_RAIL, WaypointPickerCallbacks::instance)
{
this->ConstructWindow();
this->InvalidateData();
}
static inline HotkeyList hotkeys{"buildrailwaypoint", {

View File

@ -1677,7 +1677,6 @@ struct BuildRoadWaypointWindow : public PickerWindow {
BuildRoadWaypointWindow(WindowDesc &desc, Window *parent) : PickerWindow(desc, parent, TRANSPORT_ROAD, RoadWaypointPickerCallbacks::instance)
{
this->ConstructWindow();
this->InvalidateData();
}
static inline HotkeyList hotkeys{"buildroadwaypoint", {

View File

@ -1634,7 +1634,6 @@ struct BuildHouseWindow : public PickerWindow {
{
HousePickerCallbacks::instance.SetClimateMask();
this->ConstructWindow();
this->InvalidateData();
}
void UpdateSelectSize(const HouseSpec *spec)
@ -1759,7 +1758,8 @@ struct BuildHouseWindow : public PickerWindow {
const HouseSpec *spec = HouseSpec::Get(HousePickerCallbacks::sel_type);
if ((data & PickerWindow::PFI_POSITION) != 0) {
PickerInvalidations pi(data);
if (pi.Test(PickerInvalidation::Position)) {
UpdateSelectSize(spec);
this->house_info = GetHouseInformation(spec);
}