[Change] More toolkit changes. This time in map.

This commit is contained in:
Allanis 2014-01-18 01:25:00 +00:00
parent c132caba67
commit 8ec77ccd2a

148
src/map.c
View File

@ -10,6 +10,7 @@
#include "mission.h" #include "mission.h"
#include "map.h" #include "map.h"
#define MAP_WDWNAME "Star Map"
#define WINDOW_WIDTH 650 #define WINDOW_WIDTH 650
#define WINDOW_HEIGHT 540 #define WINDOW_HEIGHT 540
@ -17,9 +18,8 @@
#define MAP_HEIGHT (WINDOW_HEIGHT-100) #define MAP_HEIGHT (WINDOW_HEIGHT-100)
#define BUTTON_WIDTH 60 #define BUTTON_WIDTH 60
#define BUTTON_HEIGHT 40 #define BUTTON_HEIGHT 30
static int map_wid = 0;
static double map_zoom = 1.; /* Zoom of the map. */ static double map_zoom = 1.; /* Zoom of the map. */
static double map_xpos = 0.; /* Map position. */ static double map_xpos = 0.; /* Map position. */
static double map_ypos = 0.; static double map_ypos = 0.;
@ -37,17 +37,20 @@ extern int systems_nstack;
extern int planet_target; extern int planet_target;
extern int hyperspace_target; extern int hyperspace_target;
static void map_update(void); static void map_update(unsigned int wid);
static int map_inPath(StarSystem* sys); static int map_inPath(StarSystem* sys);
static void map_render(double bx, double by, double w, double h); static void map_render(double bx, double by, double w, double h);
static void map_mouse(SDL_Event* event, double mx, double my); static void map_mouse(unsigned int wid, SDL_Event* event, double mx, double my);
static void map_buttonZoom(char* str); static void map_buttonZoom(unsigned int wid, char* str);
static void map_selectCur(void); static void map_selectCur(void);
/* Open the map window. */ /* Open the map window. */
void map_open(void) { void map_open(void) {
if(map_wid) { unsigned int wid;
map_close();
wid = window_get(MAP_WDWNAME);
if(wid > 0) {
window_destroy(wid);
return; return;
} }
@ -62,7 +65,7 @@ void map_open(void) {
if(map_selected == -1) if(map_selected == -1)
map_selectCur(); map_selectCur();
map_wid = window_create("Star Map", -1, -1, WINDOW_WIDTH, WINDOW_HEIGHT); wid = window_create("Star Map", -1, -1, WINDOW_WIDTH, WINDOW_HEIGHT);
/* /*
* SIDE TEXT * SIDE TEXT
@ -87,43 +90,43 @@ void map_open(void) {
*/ */
/* System name. */ /* System name. */
window_addText(map_wid, -20, -20, 100, 20, 1, "txtSysname", window_addText(wid, -20, -20, 100, 20, 1, "txtSysname",
&gl_defFont, &cDConsole, systems_stack[map_selected].name); &gl_defFont, &cDConsole, systems_stack[map_selected].name);
/* Faction. */ /* Faction. */
window_addText(map_wid, -20, -60, 90, 20, 0, "txtSFaction", window_addText(wid, -20, -60, 90, 20, 0, "txtSFaction",
&gl_smallFont, &cDConsole, "Faction:"); &gl_smallFont, &cDConsole, "Faction:");
window_addText(map_wid, -20, -60-gl_smallFont.h-5, 80, 100, 0, "txtFaction", window_addText(wid, -20, -60-gl_smallFont.h-5, 80, 100, 0, "txtFaction",
&gl_smallFont, &cBlack, NULL); &gl_smallFont, &cBlack, NULL);
/* Standing. */ /* Standing. */
window_addText(map_wid, -20, -100, 90, 20, 0, "txtSStanding", window_addText(wid, -20, -100, 90, 20, 0, "txtSStanding",
&gl_smallFont, &cDConsole, "Standing:"); &gl_smallFont, &cDConsole, "Standing:");
window_addText(map_wid, -20, -100-gl_smallFont.h-5, 80, 100, 0, "txtStanding", window_addText(wid, -20, -100-gl_smallFont.h-5, 80, 100, 0, "txtStanding",
&gl_smallFont, &cBlack, NULL); &gl_smallFont, &cBlack, NULL);
/* Planets. */ /* Planets. */
window_addText(map_wid, -20, -140, 90, 20, 0, "txtSPlanets", window_addText(wid, -20, -140, 90, 20, 0, "txtSPlanets",
&gl_smallFont, &cDConsole, "Planets:"); &gl_smallFont, &cDConsole, "Planets:");
window_addText(map_wid, -20, -140-gl_smallFont.h-5, 80, 100, 0, "txtPlanets", window_addText(wid, -20, -140-gl_smallFont.h-5, 80, 100, 0, "txtPlanets",
&gl_smallFont, &cBlack, NULL); &gl_smallFont, &cBlack, NULL);
/* Services. */ /* Services. */
window_addText(map_wid, -20, -180, 90, 20, 0, "txtSServices", window_addText(wid, -20, -180, 90, 20, 0, "txtSServices",
&gl_smallFont, &cDConsole, "Services:"); &gl_smallFont, &cDConsole, "Services:");
window_addText(map_wid, -20, -180-gl_smallFont.h-5, 80, 100, 0, "txtServices", window_addText(wid, -20, -180-gl_smallFont.h-5, 80, 100, 0, "txtServices",
&gl_smallFont, &cBlack, NULL); &gl_smallFont, &cBlack, NULL);
/* Close button. */ /* Close button. */
window_addButton(map_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, window_addButton(wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
"btnClose", "Close", (void(*)(char*))map_close); "btnClose", "Close", (void(*)(char*))map_close);
/* The map itself. */ /* The map itself. */
window_addCust(map_wid, 20, -40, MAP_WIDTH, MAP_HEIGHT, window_addCust(wid, 20, -40, MAP_WIDTH, MAP_HEIGHT,
"cstMap", 1, map_render, map_mouse); "cstMap", 1, map_render, map_mouse);
/* /*
@ -132,23 +135,16 @@ void map_open(void) {
* [+] [-] Nebulae, Asteroids, Interference. * [+] [-] Nebulae, Asteroids, Interference.
*/ */
/* Zoom buttons. */ /* Zoom buttons. */
window_addButton(map_wid, 40, 20, 30, 30, "btnZoomIn", "+", map_buttonZoom); window_addButton(wid, 40, 20, 30, 30, "btnZoomIn", "+", map_buttonZoom);
window_addButton(map_wid, 80, 20, 30, 30, "btnZoomOut", "-", map_buttonZoom); window_addButton(wid, 80, 20, 30, 30, "btnZoomOut", "-", map_buttonZoom);
/* Situation text. */ /* Situation text. */
window_addText(map_wid, 140, 10, WINDOW_WIDTH-80-30-30, 30, 0, window_addText(wid, 140, 10, WINDOW_WIDTH-80-30-30, 30, 0,
"txtSystemStatus", &gl_smallFont, &cBlack, NULL); "txtSystemStatus", &gl_smallFont, &cBlack, NULL);
map_update(); map_update(wid);
} }
void map_close(void) { static void map_update(unsigned int wid) {
if(map_wid) {
window_destroy(map_wid);
map_wid = 0;
}
}
static void map_update(void) {
int i; int i;
StarSystem* sys; StarSystem* sys;
int f, y, h, standing, nstanding; int f, y, h, standing, nstanding;
@ -156,7 +152,7 @@ static void map_update(void) {
char buf[128]; char buf[128];
/* Needs map to update. */ /* Needs map to update. */
if(map_wid <= 0) if(!map_isOpen())
return; return;
sys = &systems_stack[map_selected]; sys = &systems_stack[map_selected];
@ -164,29 +160,29 @@ static void map_update(void) {
/* Right text. */ /* Right text. */
if(!sys_isKnown(sys)) { /* System isn't known. Erase all. */ if(!sys_isKnown(sys)) { /* System isn't known. Erase all. */
/* Right text. */ /* Right text. */
window_modifyText(map_wid, "txtSysname", "Unknown"); window_modifyText(wid, "txtSysname", "Unknown");
window_modifyText(map_wid, "txtFaction", "Unknown"); window_modifyText(wid, "txtFaction", "Unknown");
/* Standing. */ /* Standing. */
window_moveWidget(map_wid, "txtSStanding", -20, -100); window_moveWidget(wid, "txtSStanding", -20, -100);
window_moveWidget(map_wid, "txtStanding", -20, -100-gl_smallFont.h-5); window_moveWidget(wid, "txtStanding", -20, -100-gl_smallFont.h-5);
window_modifyText(map_wid, "txtStanding", "Unknown"); window_modifyText(wid, "txtStanding", "Unknown");
/* Planets. */ /* Planets. */
window_moveWidget(map_wid, "txtSPlanets", -20, -140); window_moveWidget(wid, "txtSPlanets", -20, -140);
window_moveWidget(map_wid, "txtPlanets", -20, -140-gl_smallFont.h-5); window_moveWidget(wid, "txtPlanets", -20, -140-gl_smallFont.h-5);
window_modifyText(map_wid, "txtPlanets", "Unknown"); window_modifyText(wid, "txtPlanets", "Unknown");
/* Services. */ /* Services. */
window_moveWidget(map_wid, "txtSServices", -20, -180); window_moveWidget(wid, "txtSServices", -20, -180);
window_moveWidget(map_wid, "txtServices", -20, -180-gl_smallFont.h-5); window_moveWidget(wid, "txtServices", -20, -180-gl_smallFont.h-5);
window_modifyText(map_wid, "txtServices", "Unknown"); window_modifyText(wid, "txtServices", "Unknown");
/* Bottom text. */ /* Bottom text. */
window_modifyText(map_wid, "txtSystemStatus", NULL); window_modifyText(wid, "txtSystemStatus", NULL);
return; return;
} }
/* System is known. */ /* System is known. */
window_modifyText(map_wid, "txtSysname", sys->name); window_modifyText(wid, "txtSysname", sys->name);
standing = 0; standing = 0;
nstanding = 0; nstanding = 0;
@ -204,10 +200,10 @@ static void map_update(void) {
} }
} }
if(f == -1) { if(f == -1) {
window_modifyText(map_wid, "txtFaction", "NA"); window_modifyText(wid, "txtFaction", "NA");
window_moveWidget(map_wid, "txtSStanding", -20, -100); window_moveWidget(wid, "txtSStanding", -20, -100);
window_moveWidget(map_wid, "txtStanding", -20, -100-gl_smallFont.h-5); window_moveWidget(wid, "txtStanding", -20, -100-gl_smallFont.h-5);
window_modifyText(map_wid, "txtStanding", "NA"); window_modifyText(wid, "txtStanding", "NA");
y = -100; y = -100;
} else { } else {
if(i == sys->nplanets) if(i == sys->nplanets)
@ -215,20 +211,20 @@ static void map_update(void) {
snprintf(buf, 100, "%s", faction_longname(f)); snprintf(buf, 100, "%s", faction_longname(f));
/* Modify the text. */ /* Modify the text. */
window_modifyText(map_wid, "txtFaction", buf); window_modifyText(wid, "txtFaction", buf);
window_modifyText(map_wid, "txtStanding", window_modifyText(wid, "txtStanding",
faction_getStanding(standing / nstanding)); faction_getStanding(standing / nstanding));
/* Lower text if needed. */ /* Lower text if needed. */
h = gl_printHeight(&gl_smallFont, 80, buf); h = gl_printHeight(&gl_smallFont, 80, buf);
y = -100 - (h - gl_smallFont.h); y = -100 - (h - gl_smallFont.h);
window_moveWidget(map_wid, "txtSStanding", -20, y); window_moveWidget(wid, "txtSStanding", -20, y);
window_moveWidget(map_wid, "txtStanding", -20, y-gl_smallFont.h-5); window_moveWidget(wid, "txtStanding", -20, y-gl_smallFont.h-5);
} }
/* Get planets. */ /* Get planets. */
if(sys->nplanets == 0) if(sys->nplanets == 0)
window_modifyText(map_wid, "txtPlanets", "None"); window_modifyText(wid, "txtPlanets", "None");
else { else {
buf[0] = '\0'; buf[0] = '\0';
if(sys->nplanets > 0) if(sys->nplanets > 0)
@ -237,17 +233,17 @@ static void map_update(void) {
strcat(buf, ",\n"); strcat(buf, ",\n");
strcat(buf, sys->planets[i]->name); strcat(buf, sys->planets[i]->name);
} }
window_modifyText(map_wid, "txtPlanets", buf); window_modifyText(wid, "txtPlanets", buf);
} }
y -= 40; y -= 40;
window_moveWidget(map_wid, "txtSPlanets", -20, y); window_moveWidget(wid, "txtSPlanets", -20, y);
window_moveWidget(map_wid, "txtPlanets", -20, y-gl_smallFont.h-5); window_moveWidget(wid, "txtPlanets", -20, y-gl_smallFont.h-5);
/* Get the services. */ /* Get the services. */
h = gl_printHeight(&gl_smallFont, 80, buf); h = gl_printHeight(&gl_smallFont, 80, buf);
y -= 40 +(h - gl_smallFont.h); y -= 40 +(h - gl_smallFont.h);
window_moveWidget(map_wid, "txtSServices", -20, y); window_moveWidget(wid, "txtSServices", -20, y);
window_moveWidget(map_wid, "txtServices", -20, y-gl_smallFont.h-5); window_moveWidget(wid, "txtServices", -20, y-gl_smallFont.h-5);
services = 0; services = 0;
for(i = 0; i < sys->nplanets; i++) for(i = 0; i < sys->nplanets; i++)
services |= sys->planets[i]->services; services |= sys->planets[i]->services;
@ -260,7 +256,7 @@ static void map_update(void) {
strcat(buf, "Shipyard\n"); strcat(buf, "Shipyard\n");
if(buf[0] == '\0') if(buf[0] == '\0')
strcat(buf, "None"); strcat(buf, "None");
window_modifyText(map_wid, "txtServices", buf); window_modifyText(wid, "txtServices", buf);
/* System status. */ /* System status. */
buf[0] = '\0'; buf[0] = '\0';
@ -279,7 +275,7 @@ static void map_update(void) {
strcat(buf, " Light"); strcat(buf, " Light");
strcat(buf, " Nebulae"); strcat(buf, " Nebulae");
} }
window_modifyText(map_wid, "txtSystemStatus", buf); window_modifyText(wid, "txtSystemStatus", buf);
} }
/** /**
@ -289,7 +285,7 @@ static void map_update(void) {
* @return 0 if map is closed, non-zero if it's open. * @return 0 if map is closed, non-zero if it's open.
*/ */
int map_isOpen(void) { int map_isOpen(void) {
return (map_wid > 0); return window_exists(MAP_WDWNAME);
} }
/* Return 1 if sys is part of the map_path. */ /* Return 1 if sys is part of the map_path. */
@ -411,7 +407,7 @@ static void map_render(double bx, double by, double w, double h) {
} }
/* Map event handling. */ /* Map event handling. */
static void map_mouse(SDL_Event* event, double mx, double my) { static void map_mouse(unsigned int wid, SDL_Event* event, double mx, double my) {
int i, j; int i, j;
double x, y, t; double x, y, t;
StarSystem* sys; StarSystem* sys;
@ -424,9 +420,9 @@ static void map_mouse(SDL_Event* event, double mx, double my) {
switch(event->type) { switch(event->type) {
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
if(event->button.button == SDL_BUTTON_WHEELUP) if(event->button.button == SDL_BUTTON_WHEELUP)
map_buttonZoom("btnZoomOut"); map_buttonZoom(0, "btnZoomOut");
else if(event->button.button == SDL_BUTTON_WHEELDOWN) else if(event->button.button == SDL_BUTTON_WHEELDOWN)
map_buttonZoom("btnZoomIn"); map_buttonZoom(0, "btnZoomIn");
/* Selecting star system. */ /* Selecting star system. */
else { else {
@ -460,7 +456,7 @@ static void map_mouse(SDL_Event* event, double mx, double my) {
break; break;
} }
} }
map_update(); map_update(wid);
break; break;
} }
} }
@ -482,7 +478,9 @@ static void map_mouse(SDL_Event* event, double mx, double my) {
} }
} }
static void map_buttonZoom(char* str) { static void map_buttonZoom(unsigned int wid, char* str) {
(void)wid;
if(strcmp(str, "btnZoomIn")==0) { if(strcmp(str, "btnZoomIn")==0) {
map_zoom += (map_zoom >= 1.) ? 0.5 : 0.25; map_zoom += (map_zoom >= 1.) ? 0.5 : 0.25;
map_zoom = MIN(2.5, map_zoom); map_zoom = MIN(2.5, map_zoom);
@ -494,6 +492,17 @@ static void map_buttonZoom(char* str) {
} }
} }
/**
* @brief Closes the map.
*/
void map_close(void) {
unsigned int wid;
wid = window_get(MAP_WDWNAME);
if(wid > 0)
window_destroy(wid);
}
/* Set the map to sane defaults. */ /* Set the map to sane defaults. */
void map_clear(void) { void map_clear(void) {
map_zoom = 1.; map_zoom = 1.;
@ -568,11 +577,14 @@ void map_jump(void) {
* @param sys System to select. * @param sys System to select.
*/ */
void map_select(StarSystem* sys) { void map_select(StarSystem* sys) {
unsigned int wid;
wid = window_get(MAP_WDWNAME);
if(sys == NULL) if(sys == NULL)
map_selectCur(); map_selectCur();
else else
map_selected = sys - systems_stack; map_selected = sys - systems_stack;
map_update(); map_update(wid);
} }
/* A* Algorithm fo shortest path finding. */ /* A* Algorithm fo shortest path finding. */