mirror of
				https://github.com/jhbruhn/eurorack.git
				synced 2025-10-29 02:16:02 +00:00 
			
		
		
		
	Implement MidiNote MenuItem
This commit is contained in:
		
							parent
							
								
									2e43ebf110
								
							
						
					
					
						commit
						9383e3c4cf
					
				
					 4 changed files with 64 additions and 24 deletions
				
			
		|  | @ -2,37 +2,37 @@ | |||
| #include <algorithm> | ||||
| #include <u8g2.h> | ||||
| 
 | ||||
| #define MENU_ITEM_HEIGHT 12 | ||||
| const int kMenuItemHeight = 12; | ||||
| 
 | ||||
| void Menu::render(u8g2_t* u8g2_, uint8_t xStart, uint8_t yStart, uint8_t width, uint8_t height) | ||||
| { | ||||
|   this->width = width; | ||||
|   this->height = height; | ||||
| 
 | ||||
|   uint8_t maxVisibleItems = height / MENU_ITEM_HEIGHT; | ||||
|   uint8_t maxVisibleItems = height / kMenuItemHeight; | ||||
| 
 | ||||
|   uint8_t itemsToRender = std::min(maxVisibleItems, uint8_t(this->itemCount - currentScrollStart)); | ||||
|   u8g2_SetFont(u8g2_, u8g2_font_6x12_tf); | ||||
|   for (uint8_t i = 0; i < itemsToRender; i++) { | ||||
|     bool selected = this->selectedItem == (i + this->currentScrollStart); | ||||
|     bool editing = this->currentEditingItem == (i + this->currentScrollStart); | ||||
|     uint8_t yPosition = yStart + i * MENU_ITEM_HEIGHT; | ||||
|     uint8_t yPosition = yStart + i * kMenuItemHeight; | ||||
| 
 | ||||
|     AbstractMenuItem* item = this->items[i + this->currentScrollStart]; | ||||
| 
 | ||||
|     u8g2_SetDrawColor(u8g2_, selected ? 1 : 0); | ||||
| 
 | ||||
|     if (editing) { | ||||
|       u8g2_DrawFrame(u8g2_, xStart, yPosition, width, MENU_ITEM_HEIGHT); | ||||
|       u8g2_DrawFrame(u8g2_, xStart, yPosition, width, kMenuItemHeight); | ||||
|     } else if (selected) { | ||||
|       u8g2_DrawBox(u8g2_, xStart, yPosition, width, MENU_ITEM_HEIGHT); | ||||
|       u8g2_DrawBox(u8g2_, xStart, yPosition, width, kMenuItemHeight); | ||||
|     } | ||||
| 
 | ||||
|     u8g2_SetDrawColor(u8g2_, editing || !selected ? 1 : 0); | ||||
|     u8g2_DrawStr(u8g2_, xStart + 2, yPosition + MENU_ITEM_HEIGHT - 3, item->get_label()); | ||||
|     u8g2_DrawStr(u8g2_, xStart + 2, yPosition + kMenuItemHeight - 3, item->get_label()); | ||||
| 
 | ||||
|     uint8_t valueStringWidth = u8g2_GetStrWidth(u8g2_, item->get_string_representation()); | ||||
|     u8g2_DrawStr(u8g2_, xStart + width - valueStringWidth - 2, yPosition + MENU_ITEM_HEIGHT - 3, item->get_string_representation()); | ||||
|     u8g2_DrawStr(u8g2_, xStart + width - valueStringWidth - 2, yPosition + kMenuItemHeight - 3, item->get_string_representation()); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
|  | @ -58,7 +58,7 @@ void Menu::down() | |||
|   if (this->currentEditingItem >= 0) { | ||||
|     this->items[this->selectedItem]->increase(); | ||||
|   } else { | ||||
|     uint8_t maxVisibleItems = height / MENU_ITEM_HEIGHT; | ||||
|     uint8_t maxVisibleItems = height / kMenuItemHeight; | ||||
|     if (this->selectedItem < this->itemCount - 1) { | ||||
|       if (this->selectedItem - this->currentScrollStart == maxVisibleItems - 2 && this->itemCount - this->currentScrollStart > maxVisibleItems) { | ||||
|         this->currentScrollStart++; | ||||
|  | @ -87,4 +87,3 @@ bool Menu::back() | |||
|   } | ||||
|   return true; | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -102,6 +102,44 @@ class UIntMenuItem : public NumberMenuItem<uint32_t> { | |||
|       : NumberMenuItem(_label, _initialValue, _minimumValue, _maximumValue, _step) {}; | ||||
| }; | ||||
| 
 | ||||
| class MidiNoteMenuItem : public NumberMenuItem<uint8_t> { | ||||
|   private: | ||||
|   char string_buffer[4]; | ||||
|   const char* note_strings[12]; | ||||
| 
 | ||||
|   protected: | ||||
|   const char* get_format_string() { return ""; } | ||||
| 
 | ||||
|   public: | ||||
|   char* get_string_representation() | ||||
|   { | ||||
|     uint8_t currentNote = this->get_value(); | ||||
|     int note = currentNote % 12; | ||||
|     int octave = (currentNote / 12) - 1; | ||||
| 
 | ||||
|     sprintf(this->string_buffer, "%s%d", note_strings[note], octave); | ||||
| 
 | ||||
|     return this->string_buffer; | ||||
|   } | ||||
| 
 | ||||
|   MidiNoteMenuItem(const char* _label, uint8_t _initialValue) | ||||
|       : NumberMenuItem(_label, _initialValue, 0, 127, 1) | ||||
|   { | ||||
|     note_strings[0] = "C"; | ||||
|     note_strings[1] = "C#"; | ||||
|     note_strings[2] = "D"; | ||||
|     note_strings[3] = "D#"; | ||||
|     note_strings[4] = "E"; | ||||
|     note_strings[5] = "F"; | ||||
|     note_strings[6] = "F#"; | ||||
|     note_strings[7] = "G"; | ||||
|     note_strings[8] = "G#"; | ||||
|     note_strings[9] = "A"; | ||||
|     note_strings[10] = "A#"; | ||||
|     note_strings[11] = "B"; | ||||
|   }; | ||||
| }; | ||||
| 
 | ||||
| class IntMenuItem : public NumberMenuItem<int32_t> { | ||||
|   private: | ||||
|   protected: | ||||
|  |  | |||
|  | @ -5,17 +5,19 @@ | |||
| 
 | ||||
| PartMenu::PartMenu() | ||||
|     : menu(128, 64) | ||||
|     , item1("wolle", 0, 0, 100, 1) | ||||
|     , item2("petry", 0, 0, 100, 1) | ||||
|     , item3("schale", 0, 0, 100, 1) | ||||
|     , item4("eine", 0, 0, 100, 1) | ||||
|     , item5("schale?", 0, 0, 100, 1) | ||||
|     , item_voice_count("Voice Count", 1, 1, 4, 1) | ||||
|     , item_voice_detail("Voice Detail", 1, 1, 4, 1) | ||||
|     , item_midi_filter_enabled("MIDI Filter Enabled", 0, 0, 1, 1) | ||||
|     , item_midi_channel("MIDI Channel", 0, 0, 100, 1) | ||||
|     , item_midi_lowest_note("MIDI Lowest Note", 0) | ||||
|     , item_midi_highest_note("MIDI Highest Note", 0) | ||||
| { | ||||
|   this->menu.add_item(&this->item1); | ||||
|   this->menu.add_item(&this->item2); | ||||
|   this->menu.add_item(&this->item3); | ||||
|   this->menu.add_item(&this->item4); | ||||
|   this->menu.add_item(&this->item5); | ||||
|   this->menu.add_item(&this->item_voice_count); | ||||
|   this->menu.add_item(&this->item_voice_detail); | ||||
|   this->menu.add_item(&this->item_midi_filter_enabled); | ||||
|   this->menu.add_item(&this->item_midi_channel); | ||||
|   this->menu.add_item(&this->item_midi_lowest_note); | ||||
|   this->menu.add_item(&this->item_midi_highest_note); | ||||
| } | ||||
| 
 | ||||
| void PartMenu::up() | ||||
|  |  | |||
|  | @ -17,9 +17,10 @@ class PartMenu { | |||
| 
 | ||||
|   private: | ||||
|   Menu menu; | ||||
|   UIntMenuItem item1; | ||||
|   UIntMenuItem item2; | ||||
|   UIntMenuItem item3; | ||||
|   UIntMenuItem item4; | ||||
|   UIntMenuItem item5; | ||||
|   UIntMenuItem item_voice_count; | ||||
|   UIntMenuItem item_voice_detail; | ||||
|   UIntMenuItem item_midi_filter_enabled; | ||||
|   UIntMenuItem item_midi_channel; | ||||
|   MidiNoteMenuItem item_midi_lowest_note; | ||||
|   MidiNoteMenuItem item_midi_highest_note; | ||||
| }; | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue