New menu system

This commit is contained in:
Christian Colglazier 2021-10-30 18:38:53 -04:00
parent 247b89824b
commit e62bc0c51f
5 changed files with 110 additions and 61 deletions

View File

@ -5,8 +5,9 @@ using namespace daisy;
int main(void) { int main(void) {
patch.Init(); patch.Init();
sample_rate = patch.AudioSampleRate(); sample_rate = patch.AudioSampleRate();
droplet_left = GetDroplet(DropletState::kFull);
selected_menu = left_menu; selected_menu = left_menu;
droplet_left = GetDroplet(DropletState::kFull);
patch.StartAdc(); patch.StartAdc();
patch.StartAudio(AudioThrough); patch.StartAudio(AudioThrough);

View File

@ -6,6 +6,14 @@ Menu::Menu(DaisyPatch* m_patch,
patch = m_patch; patch = m_patch;
state = m_state; state = m_state;
manager = m_manager; manager = m_manager;
head = new MenuItem(MenuState::kSplit, "Split");
head->AddItemEnd(new MenuItem(MenuState::kChange, "Right"));
head->AddItemEnd(new MenuItem(MenuState::kVCO, "VCO"));
head->AddItemEnd(new MenuItem(MenuState::kNoise, "Noise"));
selected = head;
buffer = selected;
highlighted = selected;
} }
const std::string MENU_ITEMS[] = {"Split", const std::string MENU_ITEMS[] = {"Split",
"Change", "Change",
@ -15,7 +23,6 @@ const int MENU_SIZE = sizeof(MENU_ITEMS)/sizeof(*MENU_ITEMS);
const int MAX_CHAR_LENGTH = 15; const int MAX_CHAR_LENGTH = 15;
const int MENU_X[] = {0, 5, 10, 5, 0}; const int MENU_X[] = {0, 5, 10, 5, 0};
const int MENU_Y[] = {0, 11, 22, 41, 52}; const int MENU_Y[] = {0, 11, 22, 41, 52};
int selectedMenuItem = 0;
bool Menu::InMenu() { bool Menu::InMenu() {
return this->inMenu; return this->inMenu;
@ -25,32 +32,6 @@ void Menu::SetInMenu(bool menuState) {
inMenu = menuState; inMenu = menuState;
} }
void Menu::FilterMenuSelection() {
if (selectedMenuItem >= MENU_SIZE) {
selectedMenuItem = MENU_SIZE - 1;
} else if (selectedMenuItem < 0) {
selectedMenuItem = 0;
}
}
std::string Menu::FilterMenuText(int position) {
//return std::to_string(position);
if (position >= MENU_SIZE || position < 0) {
return "";
} else {
if (ConvertState(position) == MenuState::kSplit) {
if (manager->GetSplitMode()) {
return "Merge";
} else {
return "Split";
}
} else if (ConvertState(position) == MenuState::kChange) {
return manager->OtherStateName(state);
}
return MENU_ITEMS[position];
}
}
void Menu::CreateMenuItem(std::string text, void Menu::CreateMenuItem(std::string text,
int position, int position,
bool highlighted) { bool highlighted) {
@ -67,26 +48,60 @@ void Menu::CreateMenuItem(std::string text,
} }
void Menu::ProcessMenuOled() { void Menu::ProcessMenuOled() {
CreateMenuItem(FilterMenuText(selectedMenuItem-2), 1, false); MenuItem* ptr;
CreateMenuItem(FilterMenuText(selectedMenuItem-1), 2, false); // Item 3 Highlighted
CreateMenuItem(FilterMenuText(selectedMenuItem), 3, true); CreateMenuItem(highlighted->GetTitle(), 3, true);
CreateMenuItem(FilterMenuText(selectedMenuItem+1), 4, false);
CreateMenuItem(FilterMenuText(selectedMenuItem+2), 5, false); // Item 2
ptr = highlighted->GetPrevious();
if (ptr == NULL) {
CreateMenuItem("", 2, false);
} else {
CreateMenuItem(ptr->GetTitle(), 2, false);
ptr = ptr->GetPrevious();
}
// Item 1
if (ptr == NULL) {
CreateMenuItem("", 1, false);
} else {
CreateMenuItem(ptr->GetTitle(), 1, false);
}
// Item 4
ptr = highlighted->GetNext();
if (ptr == NULL) {
CreateMenuItem("", 4, false);
} else {
CreateMenuItem(ptr->GetTitle(), 4, false);
ptr = ptr->GetNext();
}
// Item 5
if (ptr == NULL) {
CreateMenuItem("", 5, false);
} else {
CreateMenuItem(ptr->GetTitle(), 5, false);
}
} }
void Menu::UpdateMenuPosition() { void Menu::UpdateMenuPosition() {
selectedMenuItem -= patch->encoder.Increment(); int move = patch->encoder.Increment();
FilterMenuSelection(); if (move < 0) {
if (highlighted->GetNextVisible() != NULL) {
highlighted = highlighted->GetNextVisible();
}
} else if (move > 0) {
if (highlighted->GetPreviousVisible() != NULL) {
highlighted = highlighted->GetPreviousVisible();
}
}
} }
std::string Menu::SelectedName() { std::string Menu::SelectedName() {
return MENU_ITEMS[selectedMenuItem]; return highlighted->GetTitle();
} }
MenuState Menu::GetState() { MenuState Menu::GetState() {
return ConvertState(selectedMenuItem); return highlighted->GetState();
} }
MenuState Menu::ConvertState(int menu_state) {
return static_cast<MenuState>(menu_state);
}

View File

@ -17,6 +17,10 @@ class Menu {
DaisyPatch* patch; DaisyPatch* patch;
DropletManager* manager; DropletManager* manager;
DropletState state; DropletState state;
MenuItem* selected;
MenuItem* buffer;
MenuItem* highlighted;
MenuItem* head;
bool inMenu = false; bool inMenu = false;
/* /*
@ -50,20 +54,6 @@ class Menu {
*/ */
void SetInMenu(bool); void SetInMenu(bool);
/*
* Keeps menu selection within the bounds of the menu's size.
*/
void FilterMenuSelection();
/*
* Returns item name based on given position and if out of the menu
* returns a blank string.
*
* @param position place in the menu
* @return menu item name
*/
std::string FilterMenuText(int position);
/* /*
* Draws a menu item on screen. * Draws a menu item on screen.
* *

View File

@ -1,6 +1,6 @@
#include "menu_item.h" #include "menu_item.h"
MenuItem::MenuItem(MenuState m_state, char* m_title) { MenuItem::MenuItem(MenuState m_state, std::string m_title) {
state = m_state; state = m_state;
this->SetTitle(m_title); this->SetTitle(m_title);
visible = true; visible = true;
@ -8,11 +8,11 @@ MenuItem::MenuItem(MenuState m_state, char* m_title) {
next = NULL; next = NULL;
} }
char* MenuItem::GetTitle() { std::string MenuItem::GetTitle() {
return title; return title;
} }
void MenuItem::SetTitle(char* m_title) { void MenuItem::SetTitle(std::string m_title) {
title = m_title; title = m_title;
} }
@ -61,3 +61,37 @@ void MenuItem::SetVisibility(bool m_visible) {
void MenuItem::ToggleVisibility() { void MenuItem::ToggleVisibility() {
visible = !visible; visible = !visible;
} }
MenuState MenuItem::GetState() {
return state;
}
void MenuItem::AddItemBefore(MenuItem* item) {
item->SetNext(this);
item->SetPrevious(this->GetPrevious());
this->SetPrevious(item);
}
void MenuItem::AddItemAfter(MenuItem* item) {
item->SetPrevious(this);
item->SetNext(this->GetNext());
this->SetNext(item);
}
void MenuItem::AddItemStart(MenuItem* item) {
if (this->GetPrevious() == NULL) {
item->SetNext(this);
this->SetPrevious(item);
} else {
this->GetPrevious()->AddItemStart(item);
}
}
void MenuItem::AddItemEnd(MenuItem* item) {
if (this->GetNext() == NULL) {
item->SetPrevious(this);
this->SetNext(item);
} else {
this->GetNext()->AddItemEnd(item);
}
}

View File

@ -3,31 +3,33 @@
#ifndef CASCADE_MENU_ITEM_H_ #ifndef CASCADE_MENU_ITEM_H_
#define CASCADE_MENU_ITEM_H_ #define CASCADE_MENU_ITEM_H_
#include <string>
enum class MenuState {kSplit, kChange, kVCO, kNoise}; enum class MenuState {kSplit, kChange, kVCO, kNoise};
class MenuItem { class MenuItem {
private: private:
MenuState state; MenuState state;
char* title; std::string title;
bool visible; bool visible;
MenuItem* previous; MenuItem* previous;
MenuItem* next; MenuItem* next;
public: public:
MenuItem(MenuState m_state, char* m_title); MenuItem(MenuState m_state, std::string m_title);
/* /*
* Returns the title of the menu item. * Returns the title of the menu item.
* *
* @return menu item title * @return menu item title
*/ */
char* GetTitle(); std::string GetTitle();
/* /*
* Sets the title for a munu item. * Sets the title for a munu item.
* *
* @param menu title * @param menu title
*/ */
void SetTitle(char* m_title); void SetTitle(std::string m_title);
/* /*
* Returns the previous menu item. * Returns the previous menu item.
@ -88,6 +90,13 @@ class MenuItem {
* Toggles visibility of menu item. * Toggles visibility of menu item.
*/ */
void ToggleVisibility(); void ToggleVisibility();
MenuState GetState();
void AddItemBefore(MenuItem* item);
void AddItemAfter(MenuItem* item);
void AddItemStart(MenuItem* item);
void AddItemEnd(MenuItem* item);
}; };
#endif // CASCADE_MENU_ITEM_H_ #endif // CASCADE_MENU_ITEM_H_