- Added drawtextat() function.

- Code cleanup - began to replace lengthy text creation blocks with calls
  to drawtextat().
- Bug fix: Creating a link between maps now updates the MODIFIED flag.
This commit is contained in:
Rob Pearce 2005-11-19 00:25:39 +00:00
parent dd835a7a8d
commit 9c949d0427
2 changed files with 89 additions and 106 deletions

193
netmapr.c
View File

@ -286,25 +286,10 @@ int main (int argc, char **argv) {
}
} else if (isonobox(event.button.x, event.button.y)) {
if (event.button.button == SDL_BUTTON_WHEELUP) {
if (obox.pos > 0) {
obox.pos--;
drawobox();
}
scrollobox(-1);
} else if (event.button.button == SDL_BUTTON_WHEELDOWN) {
int fitx,fity,fit;
/* figure out how many objects we can fit in the box */
fitx = (obox.width / obox.gridsize);
fity = ((obox.height+3) / obox.gridsize);
fit = fitx * fity;
/* check if incrementing position is okay or not */
if (((obox.pos+1)*3 + fit) <= (numobjtypes+2)) {
obox.pos++;
drawobox();
}
scrollobox(1);
}
} else if (isonobox(event.button.x, event.button.y)) {
}
break;
case SDL_MOUSEBUTTONUP:
@ -661,6 +646,7 @@ int main (int argc, char **argv) {
if (pos == -1) {
map[curmap].obj[map[curmap].selecteditem].child = pos;
sprintf(statustext, "Map link removed.");
modified = TRUE;
changestate(S_NONE);
drawmap();
} else if (pos <= (nummaps-1)) {
@ -668,6 +654,7 @@ int main (int argc, char **argv) {
if (pos != curmap) {
map[curmap].obj[map[curmap].selecteditem].child = pos;
sprintf(statustext, "Map link created.");
modified = TRUE;
changestate(S_NONE);
drawmap();
} else {
@ -1345,7 +1332,7 @@ int main (int argc, char **argv) {
drawstatusbar();
break;
case TB_SAVE:
sprintf(statustext, "LMB = Save the current map to disk (use a .BMP extension to export to bitmap format)");
sprintf(statustext, "LMB = Save the current map to disk (will automatically export based on extensions of BMP or SVG)");
drawstatusbar();
break;
default:
@ -1520,24 +1507,10 @@ int main (int argc, char **argv) {
drawmap();
}
if ((c == ',') || (c == 'q')) { /* scroll object box up */
if (obox.pos > 0) {
obox.pos--;
drawobox();
}
scrollobox(-1);
}
if ((c == '.') || (c == 'w')) { /* scroll object box down */
int fitx,fity,fit;
/* figure out how many objects we can fit in the box */
fitx = (obox.width / obox.gridsize);
fity = ((obox.height+3) / obox.gridsize);
fit = fitx * fity;
/* check if incrementing position is okay or not */
if (((obox.pos+1)*3 + fit) <= (numobjtypes+2)) {
obox.pos++;
drawobox();
}
scrollobox(1);
}
if (c == 'x') {
if (map[curmap].selecteditem != -1) {
@ -3426,35 +3399,24 @@ void drawstatusbar(void) {
if (modified) {
int tw,th;
temp.x = map[curmap].width - 30;
temp.y = map[curmap].height+2;
temp.h = DEFTEXTH-1;
/* calculate width */
TTF_SizeText(font[temp.h], "MOD", &tw,&th);
temp.w = tw;
temp.c = blue;
temp.anchor = -1;
strcpy(temp.text, "MOD");
drawbox(screen, temp.x-3,temp.y-1,temp.x+tw+1,temp.y+th,blue, NULL);
drawtext(screen, &temp);
TTF_SizeText(font[DEFTEXTH-1], "MOD", &tw,&th);
drawbox(screen, map[curmap].width-33,map[curmap].height+1,
map[curmap].width-30+tw+1,map[curmap].height+2+th,blue, NULL);
drawtextat(screen, map[curmap].width-30, map[curmap].height+2, "MOD", DEFTEXTH-1, blue);
}
/* show readonly status */
if (readonly) {
int tw,th;
/* same place as 'mod' box, as in readonly mode map can't be modified */
temp.x = map[curmap].width - 30;
temp.y = map[curmap].height+2;
temp.h = DEFTEXTH-1;
TTF_SizeText(font[DEFTEXTH-1], "RO", &tw,&th);
/* same place as 'mod' box, as in readonly mode map can't be modified */
drawbox(screen, map[curmap].width-33,map[curmap].height+1,
map[curmap].width-30+tw+1,map[curmap].height+2+th,blue, NULL);
drawtextat(screen, map[curmap].width-30, map[curmap].height+2, "RO", DEFTEXTH-1, blue);
TTF_SizeText(font[temp.h], "RO", &tw, &th);
temp.w = tw;
temp.c = blue;
temp.anchor = -1;
strcpy(temp.text, "RO");
drawbox(screen, temp.x-3,temp.y-1,temp.x+tw+1,temp.y+th,blue, NULL);
drawtext(screen, &temp);
}
SDL_UpdateRect(screen, area.x, area.y, area.w, area.h);
@ -3519,18 +3481,10 @@ void drawmapbox(void) {
drawbox(screen, mapbox.x, mapbox.y, mapbox.x + mapbox.width-2,mapbox.y + mapbox.height,white, NULL);
/* current map */
sprintf(temp.text, map[curmap].name);
temp.x = x;
temp.y = y;
temp.h = lh;
TTF_SizeText(font[lh], temp.text, &tw,&th);
temp.w = tw;
temp.anchor = -1;
temp.c = white;
drawtext(screen, &temp);
drawtextat(screen, x, y, map[curmap].name, lh, white);
TTF_SizeText(font[lh], map[curmap].name, &tw,&th);
//y += DEFTEXTH;
y += th;
/* seperator line */
@ -3605,8 +3559,6 @@ void drawmapbox(void) {
SDL_Rect ar;
/* off the bottom of the page - replace previous item with an arrow */
printf("showing arrow\n"); fflush(stdout);
y = y - th - 1; /* go back one line */
/* clear previous text */
ar.x = x;
@ -3656,41 +3608,19 @@ void drawmaplist(SDL_Surface *dest) {
x = 10;
y = 10;
strcpy(temp.text, "Select map to link to:");
temp.x = x;
temp.y = y;
temp.w = strlen(temp.text) * (DEFTEXTW*2);
temp.h = DEFTEXTH*2;
temp.c = black;
temp.anchor = -1;
drawtext(dest, &temp);
drawtextat(screen, x, y, "Select map to link to:", DEFTEXTH*2, black);
y = 50;
drawbox(dest, x, y, map[curmap].width-1,y+(DEFTEXTH*2),blue, NULL);
strcpy(temp.text, "(none)");
temp.x = x+1;
temp.y = y+1;
temp.w = strlen(temp.text) * (DEFTEXTW*2);
temp.h = (DEFTEXTH*2)-2;
temp.c = blue;
temp.anchor = -1;
drawtext(dest, &temp);
drawtextat(screen, x+1, y+1, "(none)", (DEFTEXTH*2)-2, blue);
y += (DEFTEXTH*2);
for (i = 0; i < nummaps; i++) {
drawbox(dest, x, y, map[curmap].width-1,y+(DEFTEXTH*2),blue, NULL);
strcpy(temp.text, map[i].name);
temp.x = x+1;
temp.y = y+1;
temp.w = strlen(temp.text) * (DEFTEXTW*2);
temp.h = (DEFTEXTH*2)-2;
temp.c = blue;
temp.anchor = -1;
drawtext(dest, &temp);
drawtextat(dest, x+1, y+1, map[i].name, (DEFTEXTH*2)-2, blue);
y += (DEFTEXTH*2);
@ -3862,6 +3792,33 @@ void drawtext(SDL_Surface *dest, text_t *t) {
}
/* x or y == -1 means to centre */
void drawtextat(SDL_Surface *dest, int x, int y, char *text, int size, SDL_Color c) {
int tw,th;
text_t temp;
strcpy(temp.text, text);
temp.h = size;
TTF_SizeText(font[size], temp.text, &tw,&th);
temp.w = tw;
if (x >= 0) {
temp.x = x;
} else {
temp.x = (map[curmap].width / 2) - (tw / 2);
}
if (y >= 0) {
temp.y = y;
} else {
temp.y = (map[curmap].height / 2) - (th / 2);
}
temp.c.r = c.r;
temp.c.g = c.g;
temp.c.b = c.b;
temp.anchor = -1;
drawtext(dest, &temp);
}
void drawtextSVG(text_t *t) {
int tw,th;
int txoff,tyoff;
@ -6759,6 +6716,30 @@ int savemap(void) {
return 0;
}
void scrollobox(int amt) {
int fitx,fity,fit;
if (amt < 0) {
if ((obox.pos + amt) >= 0 ) {
obox.pos += amt;
drawobox();
}
} else if (amt > 0) {
/* figure out how many objects we can fit in the box */
fitx = (obox.width / obox.gridsize);
fity = ((obox.height+3) / obox.gridsize);
fit = fitx * fity;
/* check if incrementing position is okay or not */
if (((obox.pos+amt)*3 + fit) <= (numobjtypes+2)) {
obox.pos+= amt;
drawobox();
}
}
}
int showfiledialog(void) {
Uint32 fillcol;
text_t temp;
@ -6766,6 +6747,8 @@ int showfiledialog(void) {
int y;
SDL_Color ccol;
int tw,th;
char t[BUFLEN];
/* clear map */
fillcol = SDL_MapRGB(buffer->format, map[curmap].bgcol.r,map[curmap].bgcol.g,map[curmap].bgcol.b);
@ -6774,27 +6757,25 @@ int showfiledialog(void) {
/* */
switch (state) {
case S_SAVING:
strcpy(temp.text,"Enter save filename:");
strcpy(t,"Enter save filename:");
break;
case S_LOADING:
strcpy(temp.text,"Enter load filename:");
strcpy(t,"Enter load filename:");
break;
case S_MAPNAMING:
strcpy(temp.text,"Enter new map name:");
strcpy(t,"Enter new map name:");
break;
default:
strcpy(temp.text,"Enter filename:");
strcpy(t,"Enter filename:");
break;
}
temp.h = DEFTEXTH*2;
TTF_SizeText(font[DEFTEXTH*2], temp.text, &tw,&th);
temp.w = tw;
temp.x = (map[curmap].width / 2) - (temp.w / 2);
temp.y = (map[curmap].height / 2) - (th*4);
temp.c = black;
temp.anchor = -1;
drawtext(buffer, &temp);
TTF_SizeText(font[DEFTEXTH*2], t, &tw,&th);
drawtextat(buffer, -1, (map[curmap].height / 2) - (th*4), t, DEFTEXTH*2, black);
if (state == S_SAVING) {
drawtextat(buffer, -1, (map[curmap].height / 2) - (th*3), "(use extension of .SVG or .BMP to export)", DEFTEXTH*2, black);
}
/* draw outlines */
drawbox(buffer,0,0,map[curmap].width-1,map[curmap].height-th-3,blue, NULL);

View File

@ -206,6 +206,7 @@ void drawobox(void);
void drawscreen(void);
void drawstatusbar(void);
void drawtext(SDL_Surface *dest, text_t *t);
void drawtextat(SDL_Surface *dest, int x, int y, char *text, int size, SDL_Color c);
void drawtextSVG(text_t *t);
void drawtoolbox(void);
void drawtoolboxselector(int buttonid, int altcolour);
@ -264,6 +265,7 @@ void initvars(void);
void raiseselected(void);
void setmod(int tf);
int savemap(void);
void scrollobox(int amt);
int showfiledialog(void);
void startedittext (int o);
int startlink (int x, int y);