From 602faa5c8f7c8ea993b5d12d19369c10501fec32 Mon Sep 17 00:00:00 2001 From: Jan-Henrik Bruhn Date: Wed, 26 Feb 2020 19:29:42 +0100 Subject: [PATCH] Fix scrolling for invisible items --- midi2cv/menu/menu.cc | 51 +++++++++++++++++++++++++++++++++++------ midi2cv/ui/part_menu.cc | 12 +++++++--- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/midi2cv/menu/menu.cc b/midi2cv/menu/menu.cc index f3b23d6..bf646b0 100644 --- a/midi2cv/menu/menu.cc +++ b/midi2cv/menu/menu.cc @@ -59,13 +59,29 @@ void Menu::update_visible_items() void Menu::up() { - this->update_visible_items(); if (this->currentEditingVisibleItem >= 0) { - // we store the amount of visible items before the counter before - // changing and then compare afterwards to move the cursor back to - // its intended position if necessary - this->visibleItems[this->selectedVisibleItem]->decrease(); + this->update_visible_items(); + AbstractMenuItem* item = this->visibleItems[this->selectedVisibleItem]; + + item->decrease(); + + this->update_visible_items(); + + if (this->visibleItems[this->selectedVisibleItem] != item) { + int8_t delta = 0; + // the index of our visible item has changed. + // go through the list of visible items and find it! + for (size_t i = 0; i < this->visibleItemCount; i++) { + if (this->visibleItems[i] == item) { + delta = this->selectedVisibleItem - i; + this->selectedVisibleItem = this->currentEditingVisibleItem = i; + break; + } + } + this->currentVisibleScrollStart -= delta; + CONSTRAIN(this->currentEditingVisibleItem, 0, this->visibleItemCount); + } } else if (this->selectedVisibleItem > 0) { uint8_t newItem = this->selectedVisibleItem - 1; @@ -79,14 +95,35 @@ void Menu::up() this->currentVisibleScrollStart = 0; } } - this->update_visible_items(); } void Menu::down() { this->update_visible_items(); if (this->currentEditingVisibleItem >= 0) { - this->visibleItems[this->selectedVisibleItem]->increase(); + this->update_visible_items(); + + AbstractMenuItem* item = this->visibleItems[this->selectedVisibleItem]; + + item->increase(); + + this->update_visible_items(); + + if (this->visibleItems[this->selectedVisibleItem] != item) { + int8_t delta = 0; + // the index of our visible item has changed. + // go through the list of visible items and find it! + for (size_t i = 0; i < this->visibleItemCount; i++) { + if (this->visibleItems[i] == item) { + delta = this->selectedVisibleItem - i; + this->selectedVisibleItem = this->currentEditingVisibleItem = i; + break; + } + } + this->currentVisibleScrollStart -= delta; + CONSTRAIN(this->currentEditingVisibleItem, 0, this->visibleItemCount); + } + } else { uint8_t maxVisibleItems = height / kMenuItemHeight; if (this->selectedVisibleItem < this->visibleItemCount - 1) { diff --git a/midi2cv/ui/part_menu.cc b/midi2cv/ui/part_menu.cc index dbef5f0..378b70d 100644 --- a/midi2cv/ui/part_menu.cc +++ b/midi2cv/ui/part_menu.cc @@ -18,9 +18,15 @@ PartMenu::PartMenu(Part* _part) , item_midi_channel("MIDI channel", (uint8_t*)&_part->data.midi_filter_channel, kMidiChannelStrings, 17, [_part] { return _part->data.midi_filter_channel_enabled; }) - , item_midi_input("MIDI input", (uint8_t*)&_part->data.midi_filter_input, kMidiInputStrings, 3) - , item_midi_lowest_note("MIDI lowest", (uint8_t*)&_part->data.midi_filter_lowest_note) - , item_midi_highest_note("MIDI highest", (uint8_t*)&_part->data.midi_filter_highest_note) + , item_midi_input("MIDI input", (uint8_t*)&_part->data.midi_filter_input, kMidiInputStrings, 3, [_part] { + return _part->data.midi_filter_channel_enabled; + }) + , item_midi_lowest_note("MIDI lowest", (uint8_t*)&_part->data.midi_filter_lowest_note, [_part] { + return _part->data.midi_filter_channel_enabled; + }) + , item_midi_highest_note("MIDI highest", (uint8_t*)&_part->data.midi_filter_highest_note, [_part] { + return _part->data.midi_filter_channel_enabled; + }) , item_midi_thru_mode("MIDI thru", (uint8_t*)&_part->data.midi_thru_mode, kMidiThruStrings, 3) { this->menu.add_item(&this->item_voice_count);