fangflecked-old

old project. ive since repurposed the name
git clone git://moonbender.net/fangflecked-old
Log | Files | Refs | README

menu.qc (5623B)


      1 //
      2 //	Menu stuff
      3 //
      4 
      5 
      6 void() menu_single =
      7 {
      8 	in_menu = MENU_SINGLE;
      9 	time_in_menu = 0;
     10 };
     11 
     12 void() menu_multi =
     13 {
     14 	in_menu = MENU_MULTI;
     15 	time_in_menu = 0;
     16 };
     17 
     18 void() menu_settings =
     19 {
     20 	in_menu = MENU_SETTINGS;
     21 	time_in_menu = 0;
     22 };
     23 
     24 void() menu_about =
     25 {
     26 	in_menu = MENU_ABOUT;
     27 	time_in_menu = 0;
     28 };
     29 
     30 void() menu_quit =
     31 {
     32 	localcmd("quit\n");
     33 };
     34 
     35 void() menu_back =
     36 {
     37 	in_menu = MENU_MAIN;
     38 	time_in_menu = 0;
     39 };
     40 
     41 void() menu_loadtest =
     42 {
     43 	print("loading test\n");
     44 	localcmd("map test_lms\n");
     45 };
     46 
     47 void() menu_join =
     48 {
     49 	setcursormode(TRUE,"menu/cursor");
     50 	in_menu = MENU_JOIN;
     51 };
     52 
     53 void() game_join =
     54 {
     55 	localcmd("cmd joingame\n");
     56 	in_menu = MENU_NONE;
     57 	setcursormode(FALSE);
     58 };
     59 
     60 void() game_spec =
     61 {
     62 	localcmd("cmd specgame\n");
     63 	in_menu = MENU_NONE;
     64 	setcursormode(FALSE);
     65 };
     66 
     67 //struct for all buttons, note pos and scale are going to be multiplied with width/height in draw step, so keep them between 0 and 1
     68 var struct
     69 {
     70 	vector pos;
     71 	vector scale;
     72 	string text;
     73 	float active; //used for mouseover hilight (positive values) and alpha (negative values)
     74 	void() action; //function that is called when the button is pressed
     75 	float group; //a bit field, button will be usable/visible when these bits are active
     76 } buttons[] =
     77 {
     78 	{[0.025, 0.500, 0], [0.25, 0.05, 0], "Singleplayer", -1, menu_single, MENU_MAIN},
     79 	{[0.025, 0.575, 0], [0.25, 0.05, 0], "Multiplayer", -1, menu_multi, MENU_MAIN},
     80 	{[0.025, 0.650, 0], [0.25, 0.05, 0], "Settings", -1, menu_settings, MENU_MAIN},
     81 	{[0.025, 0.725, 0], [0.25, 0.05, 0], "About", -1, menu_about, MENU_MAIN},
     82 	{[0.025, 0.925, 0], [0.25, 0.05, 0], "Quit", -1, menu_quit, MENU_MAIN},
     83 	{[0.025, 0.925, 0], [0.25, 0.05, 0], "Back", -1, menu_back, MENU_SINGLE + MENU_MULTI + MENU_SETTINGS + MENU_ABOUT},
     84 	{[0.025, 0.500, 0], [0.25, 0.05, 0], "Load test", -1, menu_loadtest, MENU_SINGLE},
     85 	{[0.475, 0.500, 0], [0.25, 0.05, 0], "Join", -1, game_join, MENU_JOIN},
     86 	{[0.475, 0.575, 0], [0.25, 0.05, 0], "Spectate", -1, game_spec, MENU_JOIN}
     87 };
     88 
     89 //REMEMBER TO UPDATE THIS CONST IF YOU ADD BUTTONS
     90 const float BUTTONS_COUNT = 9;
     91 
     92 
     93 //this function handles drawing the buttons and checking if they should be active/hilighted
     94 void(float index) Update_Button = 
     95 {
     96 	if(in_menu != in_menu & buttons[index].group)
     97 		buttons[index].active = -1;
     98 		
     99 	if(buttons[index].active == -1)
    100 	{
    101 		if(in_menu == in_menu & buttons[index].group)
    102 			buttons[index].active = 1;
    103 		else
    104 			return;
    105 	}
    106 	
    107 	local vector pos = buttons[index].pos;
    108 	pos_x *= g_width;
    109 	pos_y *= g_height;
    110 	local vector scale = buttons[index].scale;
    111 	scale_x *= g_width;
    112 	scale_y *= g_height;
    113 	
    114 	local float alphafactor;
    115 	
    116 	if(buttons[index].active > 0)
    117 		alphafactor = 1;
    118 	else
    119 		alphafactor = buttons[index].active + 1; //-1 invis, 0 normal
    120 	
    121 	if(cursor_pos_x > pos_x && cursor_pos_x < pos_x + scale_x && cursor_pos_y > pos_y && cursor_pos_y < pos_y + scale_y )
    122 		buttons[index].active = 1;
    123 	
    124 	drawfill(pos, scale, [0.2,0.2,0.2] + [0.15,0.15,0.15]*buttons[index].active, 0.3 * alphafactor);
    125 	drawline(1, pos, [pos_x + scale_x, pos_y, 0], [0.6, 0.6, 0.6], 0.5, 1); //top outline
    126 	drawline(1, pos, [pos_x, pos_y + scale_y, 0], [0.6, 0.6, 0.6], 0.5, 1); //left outline
    127 	drawline(1, [pos_x, pos_y + scale_y, 0], [pos_x + scale_x, pos_y + scale_y, 0], [0.6, 0.6, 0.6], 0.5, 1); //bottom outline
    128 	drawline(1, [pos_x + scale_x, pos_y, 0], [pos_x + scale_x, pos_y + scale_y, 0], [0.6, 0.6, 0.6], 0.5, 1); //right outline
    129 	drawstring(pos + [scale_x*0.05, scale_y*0.25, 0], buttons[index].text, [scale_y*0.5, scale_y*0.5, 1], [0.8,0.8,0.8], 1 * alphafactor, 1);
    130 	
    131 	if(buttons[index].active > 0)
    132 	{
    133 		buttons[index].active -= frametime * 5;
    134 		if(buttons[index].active < 0)
    135 			buttons[index].active = 0;
    136 	}
    137 };
    138 
    139 
    140 void(float index) Button_Click = 
    141 {
    142 	//don't click if not active
    143 	if(buttons[index].active == -1)
    144 		return;
    145 		
    146 	local vector pos = buttons[index].pos;
    147 	pos_x *= g_width;
    148 	pos_y *= g_height;
    149 	local vector scale = buttons[index].scale;
    150 	scale_x *= g_width;
    151 	scale_y *= g_height;
    152 	
    153 	if(cursor_pos_x > pos_x && cursor_pos_x < pos_x + scale_x && cursor_pos_y > pos_y && cursor_pos_y < pos_y + scale_y )
    154 	{
    155 		buttons[index].action(); //do whatever this button is supposed to do
    156 	}
    157 };
    158 
    159 
    160 void() Menu_Click =
    161 {
    162 	float i;
    163 	for(i = 0; i < BUTTONS_COUNT; i++)
    164 	{
    165 		Button_Click(i);
    166 	}
    167 };
    168 
    169 
    170 
    171 void() Draw_Menu =
    172 {
    173 
    174 	//Background picture with a nice scrolling effect, scrolling happens in the shader in scripts/menu.shader
    175 	if(serverkey("constate") == "disconnected")
    176 	{
    177 		if(g_width <= g_height * 1.77778)
    178 			drawpic([0,0,0], "menu/bg", [g_height * 1.77778, g_height, 1], [1, 1, 1], 1);
    179 		else
    180 			drawpic([0,0,0], "menu/bg", [g_width, g_height, 1], [1, 1, 1], 1);
    181 	}
    182 
    183 		
    184 	//Gradient on the background
    185 	drawpic([0,0,0], "menu/gradient_l2r", [0.75*g_width, g_height, 1], [1, 1, 1], 1);
    186 	
    187 
    188 	//Title with a simple but nice animation
    189 	local string title = "";
    190 	switch(in_menu)
    191 	{
    192 		case MENU_MAIN:
    193 			title = "Game: Video";
    194 			break;
    195 		case MENU_SINGLE:
    196 			title = "Singleplayer";
    197 			break;
    198 		case MENU_MULTI:
    199 			title = "Multiplayer";
    200 			break;
    201 		case MENU_SETTINGS:
    202 			title = "Settings";
    203 			break;
    204 		case MENU_ABOUT:
    205 			title = "About";
    206 			break;
    207 		default:
    208 			title = "FTEQW Example";
    209 	}
    210 
    211 	local float chars_to_keep;
    212 	time_in_menu += frametime;
    213 	
    214 	if(time_in_menu < 0.5)
    215 	{
    216 		chars_to_keep = time_in_menu * 2;
    217 		chars_to_keep = rint(chars_to_keep * strlen(title));
    218 	}
    219 	else
    220 		chars_to_keep = strlen(title);
    221 	
    222 	drawstring([0.025*g_width, 0.05*g_height, 0], substring(title, 0, chars_to_keep), [g_height * 0.05, g_height * 0.05, 1], [0.8, 0.8, 0.8], 1, 1);
    223 	
    224 	
    225 	//Update buttons
    226 	local float i;
    227 
    228 	for(i = 0; i < BUTTONS_COUNT; i++)
    229 	{
    230 		Update_Button(i);
    231 	}
    232 };