[Add] Use image array for shipyard and made toolkit_getList apply to image array.

This commit is contained in:
Allanis 2013-10-27 16:00:23 +00:00
parent ea6df25b0c
commit ca292cf791
5 changed files with 73 additions and 66 deletions

View File

@ -34,7 +34,7 @@
#define OUTFITS_HEIGHT 600 #define OUTFITS_HEIGHT 600
/* Shipyard. */ /* Shipyard. */
#define SHIPYARD_WIDTH 700 #define SHIPYARD_WIDTH 800
#define SHIPYARD_HEIGHT 600 #define SHIPYARD_HEIGHT 600
/* News window. */ /* News window. */
@ -342,8 +342,7 @@ static void outfits_update(char* str) {
Outfit* outfit; Outfit* outfit;
char buf[128], buf2[16], buf3[16]; 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. */ if(strcmp(outfitname, "None")==0) { /* No outfits. */
window_modifyImage(secondary_wid, "imgOutfit", NULL); window_modifyImage(secondary_wid, "imgOutfit", NULL);
window_disableButton(secondary_wid, "btnBuyOutfit"); window_disableButton(secondary_wid, "btnBuyOutfit");
@ -454,7 +453,7 @@ static void outfits_buy(char* str) {
Outfit* outfit; Outfit* outfit;
int q; int q;
outfitname = toolkit_getImageArray(secondary_wid, "iarOutfits"); outfitname = toolkit_getList(secondary_wid, "iarOutfits");
outfit = outfit_get(outfitname); outfit = outfit_get(outfitname);
q = outfits_getMod(); q = outfits_getMod();
@ -495,7 +494,7 @@ static void outfits_sell(char* str) {
Outfit* outfit; Outfit* outfit;
int q; int q;
outfitname = toolkit_getImageArray(secondary_wid, "iarOutfits"); outfitname = toolkit_getList(secondary_wid, "iarOutfits");
outfit = outfit_get(outfitname); outfit = outfit_get(outfitname);
q = outfits_getMod(); q = outfits_getMod();
@ -536,14 +535,18 @@ static void outfits_renderMod(double bx, double by, double w, double h) {
} }
static void shipyard_open(void) { static void shipyard_open(void) {
char** ships; int i;
Ship** ships;
char** sships;
glTexture** tships;
int nships; int nships;
char buf[128]; char buf[128];
/* Window creation. */
snprintf(buf, 128, "%s - Shipyard", land_planet->name); snprintf(buf, 128, "%s - Shipyard", land_planet->name);
secondary_wid = window_create(buf, secondary_wid = window_create(buf,
-1, -1, SHIPYARD_WIDTH, SHIPYARD_HEIGHT); -1, -1, SHIPYARD_WIDTH, SHIPYARD_HEIGHT);
/* Buttons. */
window_addButton(secondary_wid, -20, 20, window_addButton(secondary_wid, -20, 20,
BUTTON_WIDTH, BUTTON_HEIGHT, "btnCloseShipyard", BUTTON_WIDTH, BUTTON_HEIGHT, "btnCloseShipyard",
"Close", shipyard_close); "Close", shipyard_close);
@ -560,13 +563,15 @@ static void shipyard_open(void) {
BUTTON_WIDTH, BUTTON_HEIGHT, "btnInfoShip", BUTTON_WIDTH, BUTTON_HEIGHT, "btnInfoShip",
"Info", shipyard_info); "Info", shipyard_info);
/* Target gfx. */
window_addRect(secondary_wid, -40, -50, window_addRect(secondary_wid, -40, -50,
128, 96, "rctTarget", &cBlack, 0); 128, 96, "rctTarget", &cBlack, 0);
window_addImage(secondary_wid, -40-128, -50-96, window_addImage(secondary_wid, -40-128, -50-96,
"imgTarget", NULL, 1); "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, 80, 96, 0, "txtSDesc", &gl_smallFont, &cDConsole,
"Name:\n" "Name:\n"
"Class:\n" "Class:\n"
@ -575,11 +580,11 @@ static void shipyard_open(void) {
"Price:\n" "Price:\n"
"Money:\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); 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", SHIPYARD_WIDTH-300, 200, 0, "txtDescription",
&gl_smallFont, NULL, NULL); &gl_smallFont, NULL, NULL);
@ -587,14 +592,23 @@ static void shipyard_open(void) {
ships = ship_getTech(&nships, land_planet->tech, PLANET_TECH_MAX); ships = ship_getTech(&nships, land_planet->tech, PLANET_TECH_MAX);
if(nships <= 0) { if(nships <= 0) {
ships = malloc(sizeof(char*)); sships = malloc(sizeof(char*));
ships[0] = strdup("None"); sships[0] = strdup("None");
tships = malloc(sizeof(glTexture*));
tships[0] = NULL;
nships = 1; 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", window_addImageArray(secondary_wid, 20, 40,
ships, nships, 0, shipyard_update); 310, SHIPYARD_HEIGHT-80, "iarShipyard", 64./96.*128., 64.,
tships, sships, nships, shipyard_update);
/* Write the shipyard stuff. */ /* Write the shipyard stuff. */
shipyard_update(NULL); shipyard_update(NULL);
@ -617,7 +631,7 @@ static void shipyard_update(char* str) {
Ship* ship; Ship* ship;
char buf[80], buf2[16], buf3[16]; char buf[80], buf2[16], buf3[16];
shipname = toolkit_getList(secondary_wid, "lstShipyard"); shipname = toolkit_getList(secondary_wid, "iarShipyard");
/* No ships. */ /* No ships. */
if(strcmp(shipname, "None")==0) { if(strcmp(shipname, "None")==0) {
@ -670,7 +684,7 @@ static void shipyard_info(char* str) {
(void)str; (void)str;
char* shipname; char* shipname;
shipname = toolkit_getList(secondary_wid, "lstShipyard"); shipname = toolkit_getList(secondary_wid, "iarShipyard");
ship_view(shipname); ship_view(shipname);
} }
@ -679,7 +693,7 @@ static void shipyard_buy(char* str) {
char* shipname, buf[16]; char* shipname, buf[16];
Ship* ship; Ship* ship;
shipname = toolkit_getList(secondary_wid, "lstShipyard"); shipname = toolkit_getList(secondary_wid, "iarShipyard");
ship = ship_get(shipname); ship = ship_get(shipname);
if(pilot_cargoUsed(player) > ship->cap_cargo) { if(pilot_cargoUsed(player) > ship->cap_cargo) {
@ -737,7 +751,7 @@ static void shipyard_yours_open(char* str) {
"imgTarget", NULL, 1); "imgTarget", NULL, 1);
/* Text. */ /* Text. */
window_addText(terciary_wid, 40+200+40, -55, window_addText(terciary_wid, 40+300+40, -55,
100, 96, 0, "txtSDesc", &gl_smallFont, &cDConsole, 100, 96, 0, "txtSDesc", &gl_smallFont, &cDConsole,
"Name:\n" "Name:\n"
"Ship:\n" "Ship:\n"
@ -750,14 +764,14 @@ static void shipyard_yours_open(char* str) {
"Transportation\n" "Transportation\n"
"Sell price\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); 130, 96, 0, "txtDDesc", &gl_smallFont, &cBlack, NULL);
window_addText(terciary_wid, 40+200+40, -215, window_addText(terciary_wid, 40+200+40, -215,
100, 20, 0, "txtSOutfits", &gl_smallFont, &cDConsole, 100, 20, 0, "txtSOutfits", &gl_smallFont, &cDConsole,
"Outfits:\n"); "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", SHIPYARD_WIDTH-40-200-40-20, 200, 0, "txtDOutfits",
&gl_smallFont, &cBlack, NULL); &gl_smallFont, &cBlack, NULL);

View File

@ -43,9 +43,9 @@ Ship* ship_get(const char* name) {
} }
/* Return all the ships in text form. */ /* 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; int i, j, k, num, price;
char** shipnames; Ship** result;
Ship** ships; Ship** ships;
/* Get availabble ships for tech. */ /* Get availabble ships for tech. */
@ -67,7 +67,7 @@ char** ship_getTech(int* n, const int* tech, const int techmax) {
/* Now sort by price. */ /* Now sort by price. */
*n = 0; *n = 0;
price = -1; price = -1;
shipnames = malloc(sizeof(char*) * num); result = malloc(sizeof(Ship*) * num);
/* Until we fill the new stack. */ /* Until we fill the new stack. */
for(i = 0; i < num; i++) { for(i = 0; i < num; i++) {
/* Check for cheapest. */ /* 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)) { if((price == -1) || (ships[price]->price > ships[j]->price)) {
/* Check if already in stack. */ /* Check if already in stack. */
for(k = 0; k < (*n); k++) for(k = 0; k < (*n); k++)
if(strcmp(shipnames[k], ships[j]->name)==0) if(strcmp(result[k]->name, ships[j]->name)==0)
break; break;
/* Not in stack and therefore is cheapest. */ /* 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. */ /* Add the current cheapest to stack. */
shipnames[i] = strdup(ships[price]->name); result[i] = ships[price];
(*n)++; (*n)++;
price = -1; price = -1;
} }
@ -93,7 +93,7 @@ char** ship_getTech(int* n, const int* tech, const int techmax) {
/* Cleanup */ /* Cleanup */
free(ships); free(ships);
return shipnames; return result;
} }
/* Get the ship's classname. */ /* Get the ship's classname. */

View File

@ -78,7 +78,7 @@ typedef struct Ship_ {
/* Get. */ /* Get. */
Ship* ship_get(const char* name); 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); char* ship_class(Ship* p);
int ship_basePrice(Ship* s); int ship_basePrice(Ship* s);

View File

@ -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) { char* toolkit_getList(const unsigned int wid, char* name) {
Widget* wgt = window_getwgt(wid, name); Widget* wgt = window_getwgt(wid, name);
@ -1691,10 +1697,20 @@ char* toolkit_getList(const unsigned int wid, char* name) {
return NULL; return NULL;
} }
if((wgt->type != WIDGET_LIST) || (wgt->dat.lst.selected == -1)) switch(wgt->type) {
case WIDGET_LIST:
if(wgt->dat.lst.selected == -1)
return NULL; 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. */ /* 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; 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. */ /* List mouse even focus. */
static void toolkit_listFocus(Widget* lst, double bx, double by) { static void toolkit_listFocus(Widget* lst, double bx, double by) {
(void)bx; (void)bx;

View File

@ -74,7 +74,6 @@ void window_moveWidget(const unsigned int wid,
/* Specific. */ /* Specific. */
char* toolkit_getList(const unsigned int wid, char* name); char* toolkit_getList(const unsigned int wid, char* name);
int toolkit_getListPos(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); glTexture* window_getImage(const unsigned int wid, char* name);
/* Destroy window. */ /* Destroy window. */