1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
|
/*
* IceBreaker
* Copyright (c) 2000-2020 Matthew Miller <mattdm@mattdm.org> and
* Enrico Tassi <gareuselesinge@infinito.it>
*
* <http://www.mattdm.org/icebreaker/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <SDL.h>
#include <stdio.h>
#include <string.h>
#include "icebreaker.h"
#include "globals.h"
#include "text.h"
#include "laundry.h"
#include "dialog.h"
#include "menu.h"
#include "sound.h"
#include "hiscore.h"
#include "options.h"
#include "fullscreen.h"
#include "themes.h"
#include "grid.h"
#include "status.h"
#include "level.h"
#include "event.h"
// fix -- this file is too big; we should split the menu stuff out
// and put it in menu.c
static PopupReturnType menuhandler(SDL_Rect menurect, int menulength, char ** menuitems, PopupReturnType (**menufunctions)(char *, int), int menuvaluetextwidth );
static void drawmenu(SDL_Rect menurect, int menulength, char ** menuitems, PopupReturnType (**menufunctions)(char *, int), int menuvaluetextwidth, int highlighted);
static PopupReturnType menuitem_newgame(char * notused, int alsonotused);
static PopupReturnType menuitem_options(char * notused, int alsonotused);
static PopupReturnType menuitem_highscores(char * notused, int alsonotused);
static PopupReturnType menuitem_help(char * notused, int alsonotused);
static PopupReturnType menuitem_quit(char * notused, int alsonotused);
static PopupReturnType menuitem_sound(char * val, int mbutton);
static PopupReturnType menuitem_autopause(char * val, int mbutton);
static PopupReturnType menuitem_fullscreen(char * val, int mbutton);
static PopupReturnType menuitem_difficulty(char * val, int mbutton);
static PopupReturnType menuitem_theme(char * val, int mbutton);
// puting this here is ugly, but it's less ugly than many of the
// alternatives -- it's very hard to switch themes on the fly
static SDL_Surface * optionsmenusave;
#define MAXMENUITEMS 10
#define MAXMENUVALUELENGTH 10
#define MAINMENULENGTH 5
PopupReturnType popuplevelmenu()
{
SDL_Rect menurect, menubuttonrect;
PopupReturnType rc;
SDL_Surface * levelmenusave;
char * mainmenu[MAINMENULENGTH] = { "NEW GAME", "OPTIONS", "HIGH SCORES", "HELP","QUIT" };
PopupReturnType (*mainmenufunctions[MAINMENULENGTH])(char *,int) = { &menuitem_newgame, &menuitem_options, &menuitem_highscores, &menuitem_help, &menuitem_quit };
menurect.w=128;
menurect.h=7+(MAINMENULENGTH*(CHARHEIGHT*2+3));
menurect.x=BORDERRIGHT-menurect.w+5;
menurect.y=BORDERBOTTOM-menurect.h+9;
menubuttonrect.x=WIDTH-(CHARWIDTH*2*4)-MARGINRIGHT-4;
menubuttonrect.y=BOTTOMSTATUSY;
menubuttonrect.w=CHARWIDTH*2*4+3;
menubuttonrect.h=CHARHEIGHT*2+3;
levelmenusave = SDL_CreateRGBSurface(SDL_SWSURFACE,menurect.w,menurect.h,screen->format->BitsPerPixel,0,0,0,0);
SDL_BlitSurface(screen, &menurect, levelmenusave, NULL);
rc=menuhandler(menurect, MAINMENULENGTH, mainmenu, mainmenufunctions, 0);
if (rc != POPUPQUITGAME)
{
if (gameflags.themechanged)
{
if (!strcmp(options.theme,"random") && rc != POPUPNEWGAME)
settheme("random");
redrawwholelevel();
drawmenubutton(&menubuttonrect,true);
gameflags.themechanged=false;
updateall();
}
else
{
SDL_BlitSurface(levelmenusave, NULL, screen, &menurect);
soil(menurect);
}
}
SDL_FreeSurface(levelmenusave);
return rc;
}
PopupReturnType menuitem_newgame(char * notused, int alsonotused)
{
return POPUPNEWGAME;
}
PopupReturnType menuitem_options(char * notused, int alsonotused)
{
SDL_Event event;
int x, y;
PopupReturnType rc=popupoptionsmenu();
if (rc!=POPUPEXITMENU)
{
return rc;
}
else
{
// add a fake event so "Options" gets un-highlighted if need be.
SDL_GetMouseState(&x,&y);
event.motion.x = x;
event.motion.y = y;
event.type = SDL_MOUSEMOTION;
SDL_PushEvent(&event);
return POPUPDONOTHING;
}
}
PopupReturnType menuitem_highscores(char * notused, int alsonotused)
{
PopupReturnType rc;
SDL_Event event;
rc=popuphighscores();
int x, y;
// add a fake event so menuitem gets un-highlighted if need be.
SDL_GetMouseState(&x,&y);
event.motion.x = x;
event.motion.y = y;
event.type = SDL_MOUSEMOTION;
SDL_PushEvent(&event);
return rc;
}
PopupReturnType menuitem_help(char * notused, int alsonotused)
{
PopupReturnType rc;
SDL_Event event;
rc=popuphelp();
int x, y;
// add a fake event so menuitem gets un-highlighted if need be.
SDL_GetMouseState(&x,&y);
event.motion.x = x;
event.motion.y = y;
event.type = SDL_MOUSEMOTION;
SDL_PushEvent(&event);
return rc;
}
PopupReturnType menuitem_quit(char * notused, int alsonotused)
{
return POPUPQUITGAME;
}
#define OPTIONSMENULENGTH 5
PopupReturnType popupoptionsmenu()
{
SDL_Rect menurect;
GameDifficultyType originaldifficulty=options.difficulty;
char originaltheme[MAXTHEMENAMELENGTH+11];
PopupReturnType rc;
int originalthemecl=false;
int recallme=false;
char * optionsmenu[OPTIONSMENULENGTH] = { "SOUND", "AUTO PAUSE", "FULL SCREEN", "DIFFICULTY", "THEME" };
PopupReturnType (*optionsmenufunctions[OPTIONSMENULENGTH])(char *,int) = { &menuitem_sound, &menuitem_autopause, &menuitem_fullscreen, &menuitem_difficulty, &menuitem_theme };
// truncation is on purpose
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-truncation"
if (strlen(commandline.theme)>0)
{
snprintf(originaltheme,MAXTHEMENAMELENGTH+1,"%s",commandline.theme);
originalthemecl=true;
}
else
{
snprintf(originaltheme,MAXTHEMENAMELENGTH+1,"%s",options.theme);
originalthemecl=false;
}
#pragma GCC diagnostic pop
menurect.w=229;
menurect.h=7+(OPTIONSMENULENGTH*(CHARHEIGHT*2+3));
menurect.x=BORDERRIGHT-menurect.w+10;
menurect.y=BORDERBOTTOM-menurect.h+9-((CHARHEIGHT*2+4)*4)-2;
optionsmenusave = SDL_CreateRGBSurface(SDL_SWSURFACE,menurect.w,menurect.h,screen->format->BitsPerPixel,0,0,0,0);
SDL_BlitSurface(screen, &menurect, optionsmenusave, NULL);
rc=menuhandler(menurect, OPTIONSMENULENGTH, optionsmenu, optionsmenufunctions, 84);
if (rc==POPUPEXITMENU)
{
if ((originalthemecl && strcmp(commandline.theme, originaltheme)) ||
(!originalthemecl && strcmp(options.theme, originaltheme)) )
{
gameflags.themechanged=true;
}
}
if (rc==POPUPEXITMENU && options.difficulty != originaldifficulty)
{
if (checkiflevelatstart())
{
rc=POPUPNEWGAME;
}
else
{
switch (yesnodialog("Changing difficulty","requires restart.","End this game?"))
{
case POPUPQUITGAME:
rc=POPUPQUITGAME;
break;
case POPUPYES:
rc=POPUPNEWGAME;
break;
case POPUPNO:
options.difficulty=originaldifficulty; // put things back. :)
recallme=true;
break;
default:
fprintf(stderr,"Internal weirdness: yes/no dialog box returned something other than yes or no.\n"
"That shouldn't ever happen.\n");
break;
}
}
}
//writeoptions(); // probably no need to do this until exit
// and restore the background
if (rc != POPUPQUITGAME)
{
SDL_BlitSurface(optionsmenusave, NULL, screen, &menurect);
soil(menurect);
}
SDL_FreeSurface(optionsmenusave);
if (recallme) rc=popupoptionsmenu(); // this is a bit sick.
return rc;
}
PopupReturnType menuitem_sound(char * val, int mbutton)
{
if (strlen(val)==0)
{
if (gameflags.soundsystemworks)
{
if (options.sound==SOUNDON)
strncpy(val,"on",MAXMENUVALUELENGTH);
else
strncpy(val,"off",MAXMENUVALUELENGTH);
}
else
{
strncpy(val,"n/a",MAXMENUVALUELENGTH);
}
return POPUPDONOTHING;
}
else
{
if (!gameflags.soundsystemworks) return POPUPDONOTHING;
if (options.sound==SOUNDON)
{
options.sound=SOUNDOFF;
strncpy(val,"off",MAXMENUVALUELENGTH);
}
else
{
options.sound=SOUNDON;
strncpy(val,"on",MAXMENUVALUELENGTH);
}
return POPUPREDRAWME;
}
}
PopupReturnType menuitem_autopause(char * val, int mbutton)
{
if (strlen(val)==0)
{
if (options.autopause==AUTOPAUSEON)
strncpy(val,"on",MAXMENUVALUELENGTH);
else
strncpy(val,"off",MAXMENUVALUELENGTH);
return POPUPDONOTHING;
}
else
{
if (options.autopause==AUTOPAUSEON)
{
options.autopause=AUTOPAUSEOFF;
strncpy(val,"off",MAXMENUVALUELENGTH);
}
else
{
options.autopause=AUTOPAUSEON;
strncpy(val,"on",MAXMENUVALUELENGTH);
}
return POPUPREDRAWME;
}
}
PopupReturnType menuitem_fullscreen(char * val, int mbutton)
{
// fix -- cope with failure to switch
if (strlen(val)==0)
{
switch (options.fullscreen)
{
case FULLSCREENOFF:
strncpy(val,"off",MAXMENUVALUELENGTH);
break;
case FULLSCREENON:
strncpy(val,"on",MAXMENUVALUELENGTH);
break;
case FULLSCREENALWAYS:
strncpy(val,"always",MAXMENUVALUELENGTH);
break;
case FULLSCREENUNKNOWN: // this should never happen!
strncpy(val,"???",MAXMENUVALUELENGTH);
break;
}
return POPUPDONOTHING;
}
else
{
if (mbutton==1 || mbutton==4) // left or scroll forwards
{
switch (options.fullscreen)
{
case FULLSCREENOFF:
options.fullscreen=FULLSCREENON;
strncpy(val,"on",MAXMENUVALUELENGTH);
makefullscreen();
break;
case FULLSCREENON:
options.fullscreen=FULLSCREENALWAYS;
strncpy(val,"always",MAXMENUVALUELENGTH);
break;
case FULLSCREENALWAYS:
options.fullscreen=FULLSCREENOFF;
strncpy(val,"off",MAXMENUVALUELENGTH);
makewindowed();
break;
case FULLSCREENUNKNOWN: // this should never happen!
options.fullscreen=FULLSCREENOFF;
strncpy(val,"off",MAXMENUVALUELENGTH);
makewindowed();
break;
}
}
else // right or middle or scroll back
{
switch (options.fullscreen)
{
case FULLSCREENOFF:
options.fullscreen=FULLSCREENALWAYS;
strncpy(val,"always",MAXMENUVALUELENGTH);
makefullscreen();
break;
case FULLSCREENON:
options.fullscreen=FULLSCREENOFF;
strncpy(val,"off",MAXMENUVALUELENGTH);
makewindowed();
break;
case FULLSCREENALWAYS:
options.fullscreen=FULLSCREENON;
strncpy(val,"on",MAXMENUVALUELENGTH);
break;
case FULLSCREENUNKNOWN: // this should never happen!
options.fullscreen=FULLSCREENOFF;
strncpy(val,"off",MAXMENUVALUELENGTH);
makewindowed();
break;
}
}
return POPUPREDRAWME;
}
}
PopupReturnType menuitem_difficulty(char * val, int mbutton)
{
if (strlen(val)==0)
{
switch (options.difficulty)
{
case NORMAL:
strncpy(val,"normal",MAXMENUVALUELENGTH);
break;
case EASY:
strncpy(val,"easy",MAXMENUVALUELENGTH);
break;
case HARD:
strncpy(val,"hard",MAXMENUVALUELENGTH);
break;
}
return POPUPDONOTHING;
}
else
{
if (mbutton==1 || mbutton==4) // left or scroll forwards
{
switch (options.difficulty)
{
case NORMAL:
options.difficulty=EASY;
strncpy(val,"easy",MAXMENUVALUELENGTH);
break;
case EASY:
options.difficulty=HARD;
strncpy(val,"hard",MAXMENUVALUELENGTH);
break;
case HARD:
options.difficulty=NORMAL;
strncpy(val,"normal",MAXMENUVALUELENGTH);
break;
}
}
else // right or middle or scroll back
{
switch (options.difficulty)
{
case NORMAL:
options.difficulty=HARD;
strncpy(val,"hard",MAXMENUVALUELENGTH);
break;
case EASY:
options.difficulty=NORMAL;
strncpy(val,"normal",MAXMENUVALUELENGTH);
break;
case HARD:
options.difficulty=EASY;
strncpy(val,"easy",MAXMENUVALUELENGTH);
break;
}
}
return POPUPREDRAWME;
}
}
PopupReturnType menuitem_theme(char * val, int mbutton)
{
char** themelist;
int t;
int themecount;
SDL_Rect tmprect;
// FIX -- this is kind of kludgy -- why define this twice?
char * mainmenu[MAINMENULENGTH] = { "NEW GAME", "OPTIONS", "HIGH SCORES", "HELP","QUIT" };
// fix -- all of these are also stupidly duplicated
SDL_Rect mainmenurect;
SDL_Rect optionsmenurect;
SDL_Rect menubuttonrect;
mainmenurect.w=128;
mainmenurect.h=7+(MAINMENULENGTH*(CHARHEIGHT*2+3));
mainmenurect.x=BORDERRIGHT-mainmenurect.w+5;
mainmenurect.y=BORDERBOTTOM-mainmenurect.h+9;
optionsmenurect.w=229;
optionsmenurect.h=7+(OPTIONSMENULENGTH*(CHARHEIGHT*2+3));
optionsmenurect.x=BORDERRIGHT-optionsmenurect.w+10;
optionsmenurect.y=BORDERBOTTOM-optionsmenurect.h+9-((CHARHEIGHT*2+4)*4)-2;
menubuttonrect.x=WIDTH-(CHARWIDTH*2*4)-MARGINRIGHT-4;
menubuttonrect.y=BOTTOMSTATUSY;
menubuttonrect.w=CHARWIDTH*2*4+3;
menubuttonrect.h=CHARHEIGHT*2+3;
if (strlen(val)==0)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-truncation"
if (strlen(commandline.theme)>0)
strncpy(val,commandline.theme,MAXMENUVALUELENGTH);
else
strncpy(val,options.theme,MAXMENUVALUELENGTH);
return POPUPDONOTHING;
#pragma GCC diagnostic pop
}
else
{
themecount=getthemenames(&themelist);
if (strlen(commandline.theme)>0)
{
t=getthemenumber(themelist,themecount,commandline.theme);
// once we've changed the theme, there's no going back
*(commandline.theme)='\0'; // makes this ""
}
else
{
t=getthemenumber(themelist,themecount,options.theme);
}
if (((mbutton==1 || mbutton==4) && t==themecount-1) || ((mbutton!=1 && mbutton!=4) && t==0))
{ // "random", at the end/beginning of the list
strncpy(val,"random",MAXMENUVALUELENGTH);
snprintf(options.theme,MAXMENUVALUELENGTH,"random");
settheme("linux"); // just for pretty
redrawwholelevel();
drawmenu(mainmenurect, MAINMENULENGTH, mainmenu, NULL, 0, 1);
drawmenubutton(&menubuttonrect,true);
puttext((WIDTH-gettextwidth(10,"?"))/2,(HEIGHT-CHARHEIGHT*10)/2,10,color.gameovertext,"?");
gameflags.themechanged=true;
}
else
{
if (mbutton==1 || mbutton==4) // left click or scroll forwards
{
strncpy(val,themelist[(t+1)%themecount],MAXMENUVALUELENGTH);
snprintf(options.theme,MAXMENUVALUELENGTH,"%s",themelist[(t+1)%themecount]);
}
else // right or middle or scroll back
{
if (t<0) t=themecount;
strncpy(val,themelist[(t-1)%themecount],MAXMENUVALUELENGTH);
snprintf(options.theme,MAXMENUVALUELENGTH,"%s",themelist[(t-1)%themecount]);
}
settheme(options.theme);
redrawwholelevel();
drawmenu(mainmenurect, MAINMENULENGTH, mainmenu, NULL, 0, 1);
drawmenubutton(&menubuttonrect,true);
}
SDL_BlitSurface(screen, &optionsmenurect, optionsmenusave, NULL);
freethemenames(&themelist,themecount);
tmprect.w=WIDTH; tmprect.h=HEIGHT;
tmprect.x=0;tmprect.y=0;
soil(tmprect);
return POPUPDONOTHING;
}
}
/* displays a menu; takes a rect for position and size, the length of the
* menu, an array of menu items, an array of pointers to functions for
* each menu item (functions should return type MenuReturnType
* indicating what to do when the item is clicked) and a flag telling whether
* menu items are option/value pairs.
*/
PopupReturnType menuhandler(SDL_Rect menurect, int menulength, char ** menuitems, PopupReturnType (**menufunctions)(char *, int), int menuvaluetextwidth)
{
SDL_Rect insiderect;
SDL_Rect menuitemrect[MAXMENUITEMS];
char menuvalues[MAXMENUITEMS][MAXMENUVALUELENGTH];
int menuitemglow=-1;
int menuitempressed=false;
SDL_Event event;
PopupReturnType rc=POPUPDONOTHING;
int redrawmenuflag=false;
int domenuitemflag=false;
Uint8 domenuitembutton=0;
int i;
int tmpx, tmpy;
SDL_EnableKeyRepeat(500,90);
insiderect.w=menurect.w-2;
insiderect.h=menurect.h-2;
insiderect.x=menurect.x+1;
insiderect.y=menurect.y+1;
// fix -- use drawmenu() here?
SDL_FillRect(screen,&menurect,color.gridline);
SDL_FillRect(screen,&insiderect,color.background);
// find current option values
for (i=0;i<menulength;i++)
{
menuitemrect[i].x=insiderect.x;
menuitemrect[i].y=insiderect.y+2+(i*(CHARHEIGHT*2+3));
menuitemrect[i].w=insiderect.w;
menuitemrect[i].h=(CHARHEIGHT*2)+3;
puttext(menuitemrect[i].x+5,menuitemrect[i].y+3,2,color.normaltext,menuitems[i]);
strncpy(menuvalues[i],"",MAXMENUVALUELENGTH);
if (menuvaluetextwidth)
{
if (menufunctions!=NULL && menufunctions[i]!=NULL)
(*menufunctions[i])(menuvalues[i],0);
puttext(menuitemrect[i].x+menuitemrect[i].w-menuvaluetextwidth,menuitemrect[i].y+3,2,color.normaltext,menuvalues[i]);
}
}
soil(menurect);
clean(); // fix -- any point in not doing this first thing in the
// loop and removing the clean at the bottom of the loop?
do
{
SDL_WaitEvent(NULL); // no new CPU cooler needed. :)
while (pollevent(&event))
{
if (event.type == SDL_QUIT)
{
rc=POPUPQUITGAME;
}
else if (event.type == SDL_MOUSEBUTTONDOWN)
{
if (event.button.x>menurect.x &&
event.button.y>menurect.y &&
event.button.x<menurect.x + menurect.w &&
event.button.y<menurect.y + menurect.h)
{
if (event.button.button == 1 || menuvaluetextwidth) // we only care about right clicks on items with options
{
if (event.button.y-menurect.y>=3)
if (menuitemglow!=(event.button.y-(menurect.y+3))/(CHARHEIGHT*2+3))
{
menuitemglow=(event.button.y-(menurect.y+3))/(CHARHEIGHT*2+3);
redrawmenuflag=true;
}
if (menuitemglow>=menulength)
menuitemglow=-1;
else
menuitempressed=true;
}
}
else
{
rc=POPUPEXITMENU;
}
}
else if (event.type == SDL_MOUSEBUTTONUP)
{
if (menuitempressed && (event.button.button == 1 || menuvaluetextwidth)) // again, we only care about right clicks on items with options
{
// in area, button was down
if (event.motion.x>menurect.x &&
event.motion.y>(menurect.y + 2) &&
event.motion.x<menurect.x + menurect.w &&
event.motion.y<menurect.y + menurect.h - 2)
{
if (menufunctions!=NULL && menufunctions[menuitemglow]!=NULL)
{
domenuitemflag=true;
domenuitembutton=event.button.button;
}
}
menuitempressed=false;
}
}
else if (event.type == SDL_MOUSEMOTION)
{
// are we in the menu area?
if (event.motion.x>menurect.x &&
event.motion.y>(menurect.y + 2) &&
event.motion.x<menurect.x + menurect.w &&
event.motion.y<menurect.y + menurect.h - 2)
{
menuitemglow=(event.motion.y-(menurect.y+3))/(CHARHEIGHT*2+3);
if (menuitemglow>=menulength) menuitemglow=-1;
redrawmenuflag=true;
}
else
{
if (menuitemglow != -1 && !menuitempressed)
{
menuitemglow=-1;
redrawmenuflag=true;
}
}
}
else if (event.type==SDL_KEYDOWN)
{
switch(translatekeyevent(&event))
{
case KEYMENU: // falls through
case KEYCANCEL:
rc=POPUPEXITMENU;
case KEYMOVEUP:
if (!menuitempressed)
{
menuitemglow--;
if (menuitemglow<0) menuitemglow=menulength-1;
redrawmenuflag=true;
}
break;
case KEYMOVEDOWN:
if (!menuitempressed)
{
menuitemglow++;
if (menuitemglow>=menulength) menuitemglow=0;
redrawmenuflag=true;
}
break;
case KEYSTARTLINE:
if (menuitemglow!=-1 && menufunctions!=NULL && menufunctions[menuitemglow]!=NULL)
{
domenuitemflag=true;
domenuitembutton=1;
}
break;
case KEYSWITCHLINE:
if (menuvaluetextwidth && menuitemglow!=-1 && menufunctions!=NULL && menufunctions[menuitemglow]!=NULL)
{
domenuitemflag=true;
domenuitembutton=2;
}
break;
case KEYMOVERIGHT:
if (menuvaluetextwidth && menuitemglow!=-1 && menufunctions!=NULL && menufunctions[menuitemglow]!=NULL)
{
domenuitemflag=true;
domenuitembutton=1;
}
break;
case KEYMOVELEFT:
if (menuvaluetextwidth && menuitemglow!=-1 && menufunctions!=NULL && menufunctions[menuitemglow]!=NULL)
{
domenuitemflag=true;
domenuitembutton=2;
}
break;
default:
break;
}
}
}
if (domenuitemflag)
{
// this calls the menu functions, in case that's not obvious
rc=(*menufunctions[menuitemglow])(menuvalues[menuitemglow],domenuitembutton);
domenuitemflag=false;
redrawmenuflag=true;
}
if (rc==POPUPREDRAWME)
{
rc=POPUPDONOTHING;
redrawmenuflag=true;
}
if (redrawmenuflag)
{
SDL_FillRect(screen,&menurect,color.gridline);
SDL_FillRect(screen,&insiderect,color.background);
for (i=0;i<menulength;i++)
{
if (i == menuitemglow)
{
SDL_FillRect(screen,&menuitemrect[i],color.menuhighlight);
puttext(menuitemrect[i].x+5,menuitemrect[i].y+3,2,color.background,menuitems[i]);
if (menuvaluetextwidth) puttext(menuitemrect[i].x+menuitemrect[i].w-menuvaluetextwidth,menuitemrect[i].y+3,2,color.background,menuvalues[i]);
}
else
{
puttext(menuitemrect[i].x+5,menuitemrect[i].y+3,2,color.normaltext,menuitems[i]);
if (menuvaluetextwidth) puttext(menuitemrect[i].x+menuitemrect[i].w-menuvaluetextwidth,menuitemrect[i].y+3,2,color.normaltext,menuvalues[i]);
}
}
soil(menurect);
redrawmenuflag=false;
}
clean();
} while (rc==POPUPDONOTHING);
// a fake event to update things that depend on mouse position
SDL_GetMouseState(&tmpx,&tmpy);
event.motion.x = tmpx;
event.motion.y = tmpy;
event.type = SDL_MOUSEMOTION;
SDL_PushEvent(&event);
SDL_EnableKeyRepeat(0,0);
return(rc);
}
void drawmenu(SDL_Rect menurect, int menulength, char ** menuitems, PopupReturnType (**menufunctions)(char *, int), int menuvaluetextwidth, int highlighted)
{
SDL_Rect insiderect;
SDL_Rect menuitemrect[MAXMENUITEMS];
char menuvalues[MAXMENUITEMS][MAXMENUVALUELENGTH];
int i;
insiderect.w=menurect.w-2;
insiderect.h=menurect.h-2;
insiderect.x=menurect.x+1;
insiderect.y=menurect.y+1;
SDL_FillRect(screen,&menurect,color.gridline);
SDL_FillRect(screen,&insiderect,color.background);
for (i=0;i<menulength;i++)
{
menuitemrect[i].x=insiderect.x;
menuitemrect[i].y=insiderect.y+2+(i*(CHARHEIGHT*2+3));
menuitemrect[i].w=insiderect.w;
menuitemrect[i].h=(CHARHEIGHT*2)+3;
strncpy(menuvalues[i],"",MAXMENUVALUELENGTH);
if (i == highlighted)
{
SDL_FillRect(screen,&menuitemrect[i],color.menuhighlight);
puttext(menuitemrect[i].x+5,menuitemrect[i].y+3,2,color.background,menuitems[i]);
if (menuvaluetextwidth) // get option value
{
if (menufunctions!=NULL && menufunctions[i]!=NULL)
(*menufunctions[i])(menuvalues[i],0);
puttext(menuitemrect[i].x+menuitemrect[i].w-menuvaluetextwidth,menuitemrect[i].y+3,2,color.background,menuvalues[i]);
}
}
else
{
puttext(menuitemrect[i].x+5,menuitemrect[i].y+3,2,color.normaltext,menuitems[i]);
if (menuvaluetextwidth)
{
if (menufunctions!=NULL && menufunctions[i]!=NULL)
(*menufunctions[i])(menuvalues[i],0);
puttext(menuitemrect[i].x+menuitemrect[i].w-menuvaluetextwidth,menuitemrect[i].y+3,2,color.normaltext,menuvalues[i]);
}
}
}
soil(menurect);
clean();
}
void drawmenubutton(SDL_Rect* mbrect,int highlighted)
{
if (highlighted)
{
SDL_FillRect(screen,mbrect,color.menuhighlight);
puttext(mbrect->x+3,mbrect->y+3,2,color.background,"MENU");
}
else
{
SDL_FillRect(screen,mbrect,color.background);
puttext(mbrect->x+3,mbrect->y+3,2,color.normaltext,"MENU");
}
soil(*mbrect);
}
|