From ca292cf791a1fcdcae9b1b3df5e81f55c787d33b Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sun, 27 Oct 2013 16:00:23 +0000
Subject: [PATCH] [Add] Use image array for shipyard and made toolkit_getList
 apply to image array.

---
 src/land.c    | 78 ++++++++++++++++++++++++++++++---------------------
 src/ship.c    | 12 ++++----
 src/ship.h    |  2 +-
 src/toolkit.c | 46 +++++++++++++-----------------
 src/toolkit.h |  1 -
 5 files changed, 73 insertions(+), 66 deletions(-)

diff --git a/src/land.c b/src/land.c
index b283fbe..704fdda 100644
--- a/src/land.c
+++ b/src/land.c
@@ -30,24 +30,24 @@
 #define COMMODITY_HEIGHT  400
 
 /* Outfits. */
-#define OUTFITS_WIDTH   800
-#define OUTFITS_HEIGHT  600
+#define OUTFITS_WIDTH     800
+#define OUTFITS_HEIGHT    600
 
 /* Shipyard. */
-#define SHIPYARD_WIDTH  700
-#define SHIPYARD_HEIGHT  600
+#define SHIPYARD_WIDTH    800
+#define SHIPYARD_HEIGHT   600
 
 /* News window. */
-#define NEWS_WIDTH    400
-#define NEWS_HEIGHT   500
+#define NEWS_WIDTH        400
+#define NEWS_HEIGHT       500
 
 /* Bar window. */
-#define BAR_WIDTH     460
-#define BAR_HEIGHT    300
+#define BAR_WIDTH         460
+#define BAR_HEIGHT        300
 
 /* Mission computer window. */
-#define MISSION_WIDTH   700
-#define MISSION_HEIGHT  600
+#define MISSION_WIDTH     700
+#define MISSION_HEIGHT    600
 
 /* We use visited flags to not duplicate missions generated. */
 #define VISITED_LAND      (1<<0)
@@ -342,8 +342,7 @@ static void outfits_update(char* str) {
   Outfit* outfit;
   char buf[128], buf2[16], buf3[16];
 
-  outfitname = toolkit_getImageArray(secondary_wid, "iarOutfits");
-
+  outfitname = toolkit_getList(secondary_wid, "iarOutfits");
   if(strcmp(outfitname, "None")==0) { /* No outfits. */
     window_modifyImage(secondary_wid, "imgOutfit", NULL);
     window_disableButton(secondary_wid, "btnBuyOutfit");
@@ -454,7 +453,7 @@ static void outfits_buy(char* str) {
   Outfit* outfit;
   int q;
 
-  outfitname = toolkit_getImageArray(secondary_wid, "iarOutfits");
+  outfitname = toolkit_getList(secondary_wid, "iarOutfits");
   outfit = outfit_get(outfitname);
 
   q = outfits_getMod();
@@ -495,7 +494,7 @@ static void outfits_sell(char* str) {
   Outfit* outfit;
   int q;
 
-  outfitname = toolkit_getImageArray(secondary_wid, "iarOutfits");
+  outfitname = toolkit_getList(secondary_wid, "iarOutfits");
   outfit = outfit_get(outfitname);
 
   q = outfits_getMod();
@@ -536,14 +535,18 @@ static void outfits_renderMod(double bx, double by, double w, double h) {
 }
 
 static void shipyard_open(void) {
-  char** ships;
+  int i;
+  Ship** ships;
+  char** sships;
+  glTexture** tships;
   int nships;
   char buf[128];
 
+  /* Window creation. */
   snprintf(buf, 128, "%s - Shipyard", land_planet->name);
   secondary_wid = window_create(buf,
                                 -1, -1, SHIPYARD_WIDTH, SHIPYARD_HEIGHT);
-
+  /* Buttons. */
   window_addButton(secondary_wid, -20, 20,
                    BUTTON_WIDTH, BUTTON_HEIGHT, "btnCloseShipyard",
                    "Close", shipyard_close);
@@ -560,13 +563,15 @@ static void shipyard_open(void) {
                    BUTTON_WIDTH, BUTTON_HEIGHT, "btnInfoShip",
                    "Info", shipyard_info);
 
+  /* Target gfx. */
   window_addRect(secondary_wid, -40, -50,
                  128, 96, "rctTarget", &cBlack, 0);
 
   window_addImage(secondary_wid, -40-128, -50-96,
                   "imgTarget", NULL, 1);
 
-  window_addText(secondary_wid, 40+200+40, -55,
+  /* Text. */
+  window_addText(secondary_wid, 40+300+40, -55,
                  80, 96, 0, "txtSDesc", &gl_smallFont, &cDConsole,
                  "Name:\n"
                  "Class:\n"
@@ -575,11 +580,11 @@ static void shipyard_open(void) {
                  "Price:\n"
                  "Money:\n");
 
-  window_addText(secondary_wid, 40+200+40+80, -55,
+  window_addText(secondary_wid, 40+300+40+80, -55,
                  130, 96, 0, "txtDDesc", &gl_smallFont, &cBlack, NULL);
 
 
-  window_addText(secondary_wid, 20+200+40, -160,
+  window_addText(secondary_wid, 20+300+40, -160,
                  SHIPYARD_WIDTH-300, 200, 0, "txtDescription",
                  &gl_smallFont, NULL, NULL);
 
@@ -587,14 +592,23 @@ static void shipyard_open(void) {
   ships = ship_getTech(&nships, land_planet->tech, PLANET_TECH_MAX);
 
   if(nships <= 0) {
-    ships = malloc(sizeof(char*));
-    ships[0] = strdup("None");
-    nships = 1;
+    sships    = malloc(sizeof(char*));
+    sships[0] = strdup("None");
+    tships    = malloc(sizeof(glTexture*));
+    tships[0] = NULL;
+    nships    = 1;
+  } else {
+    sships = malloc(sizeof(char*)*nships);
+    tships = malloc(sizeof(glTexture*)*nships);
+    for(i = 0; i < nships; i++) {
+        sships[i] = strdup(ships[i]->name);
+        tships[i] = ships[i]->gfx_target;
+    }
+    free(ships);
   }
-
-  window_addList(secondary_wid, 20, 40,
-                 200, SHIPYARD_HEIGHT-80, "lstShipyard",
-                 ships, nships, 0, shipyard_update);
+  window_addImageArray(secondary_wid, 20, 40,
+      310, SHIPYARD_HEIGHT-80, "iarShipyard", 64./96.*128., 64.,
+      tships, sships, nships, shipyard_update);
 
   /* Write the shipyard stuff. */
   shipyard_update(NULL);
@@ -617,7 +631,7 @@ static void shipyard_update(char* str) {
   Ship* ship;
   char buf[80], buf2[16], buf3[16];
 
-  shipname = toolkit_getList(secondary_wid, "lstShipyard");
+  shipname = toolkit_getList(secondary_wid, "iarShipyard");
 
   /* No ships. */
   if(strcmp(shipname, "None")==0) {
@@ -670,7 +684,7 @@ static void shipyard_info(char* str) {
   (void)str;
   char* shipname;
 
-  shipname = toolkit_getList(secondary_wid, "lstShipyard");
+  shipname = toolkit_getList(secondary_wid, "iarShipyard");
   ship_view(shipname);
 }
 
@@ -679,7 +693,7 @@ static void shipyard_buy(char* str) {
   char* shipname, buf[16];
   Ship* ship;
 
-  shipname = toolkit_getList(secondary_wid, "lstShipyard");
+  shipname = toolkit_getList(secondary_wid, "iarShipyard");
   ship = ship_get(shipname);
 
   if(pilot_cargoUsed(player) > ship->cap_cargo) {
@@ -737,7 +751,7 @@ static void shipyard_yours_open(char* str) {
                   "imgTarget", NULL, 1);
 
   /* Text. */
-  window_addText(terciary_wid, 40+200+40, -55,
+  window_addText(terciary_wid, 40+300+40, -55,
                  100, 96, 0, "txtSDesc", &gl_smallFont, &cDConsole,
                  "Name:\n"
                  "Ship:\n"
@@ -750,14 +764,14 @@ static void shipyard_yours_open(char* str) {
                  "Transportation\n"
                  "Sell price\n");
 
-  window_addText(terciary_wid, 40+200+40+100, -55,
+  window_addText(terciary_wid, 40+300+40+100, -55,
                  130, 96, 0, "txtDDesc", &gl_smallFont, &cBlack, NULL);
 
   window_addText(terciary_wid, 40+200+40, -215,
                  100, 20, 0, "txtSOutfits", &gl_smallFont, &cDConsole,
                  "Outfits:\n");
 
-  window_addText(terciary_wid, 40+200+40, -215-gl_smallFont.h-5,
+  window_addText(terciary_wid, 40+300+40, -215-gl_smallFont.h-5,
                  SHIPYARD_WIDTH-40-200-40-20, 200, 0, "txtDOutfits",
                  &gl_smallFont, &cBlack, NULL);
 
diff --git a/src/ship.c b/src/ship.c
index 486f17b..3902356 100644
--- a/src/ship.c
+++ b/src/ship.c
@@ -43,9 +43,9 @@ Ship* ship_get(const char* name) {
 }
 
 /* Return all the ships in text form. */
-char** ship_getTech(int* n, const int* tech, const int techmax) {
+Ship** ship_getTech(int* n, const int* tech, const int techmax) {
   int i, j, k, num, price;
-  char** shipnames;
+  Ship** result;
   Ship** ships;
   
   /* Get availabble ships for tech. */
@@ -67,7 +67,7 @@ char** ship_getTech(int* n, const int* tech, const int techmax) {
   /* Now sort by price. */
   *n = 0;
   price = -1;
-  shipnames = malloc(sizeof(char*) * num);
+  result = malloc(sizeof(Ship*) * num);
   /* Until we fill the new stack. */
   for(i = 0; i < num; i++) {
     /* Check for cheapest. */
@@ -76,7 +76,7 @@ char** ship_getTech(int* n, const int* tech, const int techmax) {
       if((price == -1) || (ships[price]->price > ships[j]->price)) {
         /* Check if already in stack. */
         for(k = 0; k < (*n); k++)
-          if(strcmp(shipnames[k], ships[j]->name)==0)
+          if(strcmp(result[k]->name, ships[j]->name)==0)
             break;
 
         /* Not in stack and therefore is cheapest. */
@@ -85,7 +85,7 @@ char** ship_getTech(int* n, const int* tech, const int techmax) {
       }
     }
     /* Add the current cheapest to stack. */
-    shipnames[i] = strdup(ships[price]->name);
+    result[i] = ships[price];
     (*n)++;
     price = -1;
   }
@@ -93,7 +93,7 @@ char** ship_getTech(int* n, const int* tech, const int techmax) {
   /* Cleanup */
   free(ships);
 
-  return shipnames;
+  return result;
 }
 
 /* Get the ship's classname. */
diff --git a/src/ship.h b/src/ship.h
index 83d8b53..3ef9399 100644
--- a/src/ship.h
+++ b/src/ship.h
@@ -78,7 +78,7 @@ typedef struct Ship_ {
 
 /* Get. */
 Ship* ship_get(const char* name);
-char** ship_getTech(int* n, const int* tech, const int techmax);
+Ship** ship_getTech(int* n, const int* tech, const int techmax);
 char* ship_class(Ship* p);
 int ship_basePrice(Ship* s);
 
diff --git a/src/toolkit.c b/src/toolkit.c
index 3603ce7..0485e22 100644
--- a/src/toolkit.c
+++ b/src/toolkit.c
@@ -1682,7 +1682,13 @@ static void toolkit_listScroll(Widget* wgt, int direction) {
   }
 }
 
-/* Get what is selected currently in a list. */
+/**
+ * @fn char* toolkit_getList(const unsigned int wid, char* name)
+ *
+ * @brief Get what is selected currently in a list.
+ *
+ * List included Image Array.
+ */
 char* toolkit_getList(const unsigned int wid, char* name) {
   Widget* wgt = window_getwgt(wid, name);
 
@@ -1691,10 +1697,20 @@ char* toolkit_getList(const unsigned int wid, char* name) {
     return NULL;
   }
 
-  if((wgt->type != WIDGET_LIST) || (wgt->dat.lst.selected == -1))
-    return NULL;
+  switch(wgt->type) {
+    case WIDGET_LIST:
+      if(wgt->dat.lst.selected == -1)
+        return NULL;
+      return wgt->dat.lst.options[wgt->dat.lst.selected];
 
-  return wgt->dat.lst.options[wgt->dat.lst.selected];
+    case WIDGET_IMAGEARRAY:
+      if(wgt->dat.iar.selected == -1)
+        return NULL;
+      return wgt->dat.iar.captions[wgt->dat.iar.selected];
+
+    default:
+      return NULL;
+  }
 }
 
 /* Get the position of current item in the list. */
@@ -1712,28 +1728,6 @@ int toolkit_getListPos(const unsigned int wid, char* name) {
   return wgt->dat.lst.selected;
 }
 
-/**
- * @fn char* toolkit_getImageArray(const unsigned int wid, char* name)
- *
- * @brief Get what is currently selected in an image array.
- *    @param wid Window to get array data from.
- *    @param name Name of the image array widget.
- *    @return Caption of the currently selected element.
- */
-char* toolkit_getImageArray(const unsigned int wid, char* name) {
-  Widget* wgt = window_getwgt(wid, name);
-
-  if(wgt == NULL) {
-    WARN("Widget '%s' not found", name);
-    return NULL;
-  }
-
-  if((wgt->type != WIDGET_IMAGEARRAY) || (wgt->dat.iar.selected == -1))
-    return NULL;
-
-  return wgt->dat.iar.captions[wgt->dat.iar.selected];
-}
-
 /* List mouse even focus. */
 static void toolkit_listFocus(Widget* lst, double bx, double by) {
   (void)bx;
diff --git a/src/toolkit.h b/src/toolkit.h
index 5150a7e..5f5c9a1 100644
--- a/src/toolkit.h
+++ b/src/toolkit.h
@@ -74,7 +74,6 @@ void window_moveWidget(const unsigned int wid,
 /* Specific. */
 char* toolkit_getList(const unsigned int wid, char* name);
 int toolkit_getListPos(const unsigned int wid, char* name);
-char* toolkit_getImageArray(const unsigned int wid, char* name);
 glTexture* window_getImage(const unsigned int wid, char* name);
 
 /* Destroy window. */