DEADSOFTWARE

menu: draw menus without direct access to sutructures
[flatwaifu.git] / src / menu.c
index 629f0c8d02f2b5129cbba7deff70efcb0d9c15ab..40384c85fc97ec654b0ca94d40383ab413cc4965 100644 (file)
@@ -94,18 +94,23 @@ static void GM_say (const char nm[8]) {
   }
 }
 
-static int GM_init_int (new_msg_t *msg, int i, int a, int b, int s) {
+int GM_init_int0 (new_msg_t *msg, int i, int a, int b, int s) {
   assert(msg != NULL);
-  assert(a <= b);
-  assert(s >= 0);
-  msg->integer.i = min(max(i, a), b);
+  msg->integer.i = i;
   msg->integer.a = a;
   msg->integer.b = b;
   msg->integer.s = s;
   return 1;
 }
 
-static int GM_init_str (new_msg_t *msg, char *str, int maxlen) {
+int GM_init_int (new_msg_t *msg, int i, int a, int b, int s) {
+  assert(msg != NULL);
+  assert(a <= b);
+  assert(s >= 0);
+  return GM_init_int0(msg, min(max(i, a), b), a, b, s);
+}
+
+int GM_init_str (new_msg_t *msg, char *str, int maxlen) {
   assert(msg != NULL);
   assert(str != NULL);
   assert(maxlen >= 0);
@@ -221,6 +226,13 @@ static int GM_save_handler (new_msg_t *msg, const new_menu_t *m, void *data) {
   return 0;
 }
 
+static int GM_options_handler (new_msg_t *msg, const new_menu_t *m, void *data) {
+  switch (msg->type) {
+    case GM_SELECT: GM_push(R_menu()); return 1;
+  }
+  return 0;
+}
+
 static int GM_exit_handler (new_msg_t *msg, const new_menu_t *m, void *data) {
   switch (msg->type) {
     case GM_ENTER:
@@ -289,7 +301,7 @@ static const new_menu_t newgame_menu = {
 }, options_menu = {
   GM_BIG, "Options", NULL, NULL,
   {
-    //{ GM_BUTTON, "Video", NULL, NULL, NULL },
+    { GM_BUTTON, "Video", NULL, &GM_options_handler, NULL },
     { GM_BUTTON, "Sound", NULL, NULL, &sound_menu },
     { GM_BUTTON, "Music", NULL, NULL, &music_menu },
     { 0, NULL, NULL, NULL, NULL } // end
@@ -369,12 +381,30 @@ static void GM_normalize_message (new_msg_t *msg) {
   }
 }
 
+static int count_menu_entries (const new_menu_t *m) {
+  assert(m != NULL);
+  int i = 0;
+  while (m->entries[i].type != 0) {
+    i += 1;
+  }
+  return i;
+}
+
 int GM_send_this (const new_menu_t *m, new_msg_t *msg) {
   assert(m != NULL);
   assert(msg != NULL);
-  if (m->handler != NULL) {
-    GM_normalize_message(msg);
-    return m->handler(msg, m, m->data);
+  int n;
+  switch (msg->type) {
+    case GM_QUERY:
+      n = count_menu_entries(m);
+      return GM_init_int0(msg, GM_geti(), n, n, m->type);
+    case GM_GETTITLE:
+      return GM_init_str(msg, m->title, strlen(m->title));
+    default:
+      if (m->handler != NULL) {
+        GM_normalize_message(msg);
+        return m->handler(msg, m, m->data);
+      }
   }
   return 0;
 }
@@ -384,9 +414,14 @@ int GM_send (const new_menu_t *m, int i, new_msg_t *msg) {
   assert(i >= 0);
   assert(msg != NULL);
   const new_var_t *v = &m->entries[i];
-  if (v->handler != NULL) {
-    GM_normalize_message(msg);
-    return v->handler(msg, m, v->data);
+  switch (msg->type) {
+    case GM_GETENTRY: return GM_init_int0(msg, v->type, 0, 0, 0);
+    case GM_GETCAPTION: return GM_init_str(msg, v->caption, strlen(v->caption));
+    default:
+      if (v->handler != NULL) {
+        GM_normalize_message(msg);
+        return v->handler(msg, m, v->data);
+      }
   }
   return 0;
 }
@@ -433,15 +468,6 @@ void G_code (void) {
   Z_sound(s,128);
 }
 
-static int count_menu_entries (const new_menu_t *m) {
-  assert(m != NULL);
-  int i = 0;
-  while (m->entries[i].type != 0) {
-    i += 1;
-  }
-  return i;
-}
-
 static int strnlen (const char *s, int len) {
   int i = 0;
   while (i < len && s[i] != 0) {
@@ -567,10 +593,16 @@ void GM_key (int key, int down) {
   if (down) {
     lastkey = key;
     if (!_2pl || cheat) {
-      for (i = 0; i < 31; ++i) {
+      for (i = 0; i < 31; i++) {
         cbuf[i] = cbuf[i + 1];
       }
-      //cbuf[31] = get_keychar(key);
+      if (key >= KEY_0 && key <= KEY_9) {
+        cbuf[31] = key - KEY_0 + '0';
+      } else if (key >= KEY_A && key <= KEY_Z) {
+        cbuf[31] = key - KEY_A + 'A';
+      } else {
+        cbuf[31] = 0;
+      }
     }
   }
 }