Skip to content

Commit

Permalink
Fix issue with the collapsible sections in control mapping collapsing…
Browse files Browse the repository at this point in the history
… on every change.
  • Loading branch information
hrydgard committed Jan 29, 2024
1 parent f02142c commit c29f81d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
5 changes: 4 additions & 1 deletion Common/UI/View.h
Expand Up @@ -899,14 +899,17 @@ class CheckBox : public ClickableItem {

class CollapsibleHeader : public CheckBox {
public:
CollapsibleHeader(bool *toggle, const std::string &text, LayoutParams *layoutParams = nullptr);
CollapsibleHeader(bool *open, const std::string &text, LayoutParams *layoutParams = nullptr);
void Draw(UIContext &dc) override;
void GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;

Point GetFocusPosition(FocusDirection dir) const override;

void SetHasSubitems(bool hasSubItems) { hasSubItems_ = hasSubItems; }
void SetOpenPtr(bool *open) {
toggle_ = open;
}
private:
bool hasSubItems_ = true;
};
Expand Down
11 changes: 6 additions & 5 deletions Common/UI/ViewGroup.cpp
Expand Up @@ -1175,11 +1175,12 @@ StickyChoice *ChoiceStrip::Choice(int index) {
}

CollapsibleSection::CollapsibleSection(const std::string &title, LayoutParams *layoutParams) : LinearLayout(ORIENT_VERTICAL, layoutParams) {
open_ = &localOpen_;
SetSpacing(0.0f);

heading_ = new CollapsibleHeader(&open_, title);
views_.push_back(heading_);
heading_->OnClick.Add([=](UI::EventParams &) {
header_ = new CollapsibleHeader(open_, title);
views_.push_back(header_);
header_->OnClick.Add([=](UI::EventParams &) {
// Change the visibility of all children except the first one.
// Later maybe try something more ambitious.
UpdateVisibility();
Expand All @@ -1189,12 +1190,12 @@ CollapsibleSection::CollapsibleSection(const std::string &title, LayoutParams *l

void CollapsibleSection::Update() {
ViewGroup::Update();
heading_->SetHasSubitems(views_.size() > 1);
header_->SetHasSubitems(views_.size() > 1);
}

void CollapsibleSection::UpdateVisibility() {
for (size_t i = 1; i < views_.size(); i++) {
views_[i]->SetVisibility(open_ ? V_VISIBLE : V_GONE);
views_[i]->SetVisibility(*open_ ? V_VISIBLE : V_GONE);
}
}

Expand Down
10 changes: 8 additions & 2 deletions Common/UI/ViewGroup.h
Expand Up @@ -332,14 +332,20 @@ class CollapsibleSection : public LinearLayout {
void Update() override;

void SetOpen(bool open) {
*open_ = open;
UpdateVisibility();
}
void SetOpenPtr(bool *open) {
header_->SetOpenPtr(open);
open_ = open;
UpdateVisibility();
}

private:
void UpdateVisibility();
bool open_ = true;
CollapsibleHeader *heading_;
bool localOpen_ = true;
bool *open_;
CollapsibleHeader *header_;
};

} // namespace UI
18 changes: 9 additions & 9 deletions UI/ControlMappingScreen.cpp
Expand Up @@ -280,22 +280,22 @@ void ControlMappingScreen::CreateViews() {
struct Cat {
const char *catName;
int firstKey;
bool openByDefault;
bool *open;
};
// Category name, first input from psp_button_names.
static const Cat cats[] = {
{"Standard PSP controls", CTRL_UP, true},
{"Control modifiers", VIRTKEY_ANALOG_ROTATE_CW, true},
{"Emulator controls", VIRTKEY_FASTFORWARD, true},
{"Extended PSP controls", VIRTKEY_AXIS_RIGHT_Y_MAX, false},
{"Standard PSP controls", CTRL_UP, &categoryToggles_[0]},
{"Control modifiers", VIRTKEY_ANALOG_ROTATE_CW, &categoryToggles_[1]},
{"Emulator controls", VIRTKEY_FASTFORWARD, &categoryToggles_[2]},
{"Extended PSP controls", VIRTKEY_AXIS_RIGHT_Y_MAX, &categoryToggles_[3]},
};

int curCat = -1;
CollapsibleSection *curSection = nullptr;
for (size_t i = 0; i < numMappableKeys; i++) {
if (curCat < (int)ARRAY_SIZE(cats) && mappableKeys[i].key == cats[curCat + 1].firstKey) {
if (curCat >= 0 && !cats[curCat].openByDefault) {
curSection->SetOpen(false);
if (curCat >= 0) {
curSection->SetOpenPtr(cats[curCat].open);
}
curCat++;
curSection = rightColumn->Add(new CollapsibleSection(km->T(cats[curCat].catName)));
Expand All @@ -307,8 +307,8 @@ void ControlMappingScreen::CreateViews() {
mapper->SetTag(StringFromFormat("KeyMap%s", mappableKeys[i].name));
mappers_.push_back(mapper);
}
if (curCat >= 0 && curSection && !cats[curCat].openByDefault) {
curSection->SetOpen(false);
if (curCat >= 0 && curSection) {
curSection->SetOpenPtr(cats[curCat].open);
}

keyMapGeneration_ = KeyMap::g_controllerMapGeneration;
Expand Down
2 changes: 2 additions & 0 deletions UI/ControlMappingScreen.h
Expand Up @@ -39,6 +39,8 @@ class ControlMappingScreen : public UIDialogScreenWithGameBackground {
explicit ControlMappingScreen(const Path &gamePath) : UIDialogScreenWithGameBackground(gamePath) {
categoryToggles_[0] = true;
categoryToggles_[1] = true;
categoryToggles_[2] = true;
categoryToggles_[3] = false;
}
const char *tag() const override { return "ControlMapping"; }

Expand Down

0 comments on commit c29f81d

Please sign in to comment.