Menü: Scrollen, wenn Knopf gedrückt gehalten.
authorWintermute <wintermute@hannover.ccc.de>
Tue, 22 Oct 2013 20:43:28 +0000 (22:43 +0200)
committerWintermute <wintermute@hannover.ccc.de>
Tue, 22 Oct 2013 20:43:28 +0000 (22:43 +0200)
badge/ui/menu.c

index b64e6d7..110b5ba 100644 (file)
@@ -69,35 +69,60 @@ size_t badge_menu(char const *const *menu,
                   size_t *first_visible,
                   size_t  selected)
 {
+  unsigned scroll_ticks = 0;
+  int scroll_direction = 0;
+
   for(;;) {
-    if(n <= MENU_ENTRIES_VISIBLE) {
-      *first_visible = 0;
-    } else if(selected + 1 == n) {
-      *first_visible = n - MENU_ENTRIES_VISIBLE;
-    } else if(selected <= *first_visible) {
-      *first_visible = selected == 0 ? 0 : selected - 1;
-    } else if(selected - *first_visible + 2 > MENU_ENTRIES_VISIBLE) {
-      *first_visible = selected - MENU_ENTRIES_VISIBLE + 2;
+    if(scroll_ticks == 0) {
+      if       (scroll_direction ==  1 && selected + 1 <  n) {
+        ++selected;
+      } else if(scroll_direction == -1 && selected     != 0) {
+        --selected;
+      }
+
+      if(n <= MENU_ENTRIES_VISIBLE) {
+        *first_visible = 0;
+      } else if(selected + 1 == n) {
+        *first_visible = n - MENU_ENTRIES_VISIBLE;
+      } else if(selected <= *first_visible) {
+        *first_visible = selected == 0 ? 0 : selected - 1;
+      } else if(selected - *first_visible + 2 > MENU_ENTRIES_VISIBLE) {
+        *first_visible = selected - MENU_ENTRIES_VISIBLE + 2;
+      }
+
+      badge_menu_show(menu, n, *first_visible, selected);
+
+      scroll_ticks = 25;
     }
 
-    badge_menu_show(menu, n, *first_visible, selected);
-
     badge_event_t ev;
 
-    do {
-      ev = badge_event_wait();
-    } while(badge_event_type(ev) != BADGE_EVENT_USER_INPUT);
-
-    uint8_t old_state = badge_event_old_input_state(ev);
-    uint8_t new_state = badge_event_new_input_state(ev);
-    uint8_t new_buttons = new_state & (old_state ^ new_state);
-
-    if(new_buttons & (BADGE_EVENT_KEY_BTN_A | BADGE_EVENT_KEY_BTN_B)) {
-      return selected;
-    } else if((new_buttons & BADGE_EVENT_KEY_UP  ) && selected != 0) {
-      --selected;
-    } else if(new_buttons & BADGE_EVENT_KEY_DOWN && selected + 1 < n) {
-      ++selected;
+    ev = badge_event_wait();
+    switch(badge_event_type(ev)) {
+    case BADGE_EVENT_USER_INPUT:
+    {
+      uint8_t old_state = badge_event_old_input_state(ev);
+      uint8_t new_state = badge_event_new_input_state(ev);
+      uint8_t new_buttons = new_state & (old_state ^ new_state);
+
+      if(new_buttons & (BADGE_EVENT_KEY_BTN_A | BADGE_EVENT_KEY_BTN_B)) {
+        return selected;
+      } else if((new_buttons & BADGE_EVENT_KEY_UP  )) {
+        scroll_direction = -1;
+      } else if((new_buttons & BADGE_EVENT_KEY_DOWN)) {
+        scroll_direction =  1;
+      } else {
+        scroll_direction =  0;
+      }
+
+      scroll_ticks = 0;
+      break;
+    }
+    case BADGE_EVENT_GAME_TICK:
+    {
+      --scroll_ticks;
+      break;
+    }
     }
   }
 }
This page took 0.028879 seconds and 4 git commands to generate.