// Copyright (C) 1999-2000 Id Software, Inc.
//
/*
=======================================================================
 
SETUP MENU
 
=======================================================================
*/


/*
The work contained within this file is software written by various
copyright holders.  The initial contributor, Id Software holds all
copyright over their software.  However, software used and written by
and for UI Enhanced has copyrights held by the initial author of the
software.
 
The changes written by and for UI Enhanced are contained alongside the
original work from Id Software for convenience and ease of interoperability.
 
For the code contained herein that was written by Id Software, see
the license agreement on their original archive for restrictions and
limitations.
 
The UI Enhanced copyright owner permit free reuse of his code contained
herein, as long as the following terms are met:
 
----------------------------------------------------------------------
1) Credit is given in a place where users of the mod may read
   it. (Title screen, credit screen or README will do).  The
   recommended format is: "First, Last, alias, email"
 
2) There are no attempts to misrepresent the public as to who made the
   alterations.  The UI Enhanced copyright owner does not give permission
   for others to release software under the UI Enhanced name.
----------------------------------------------------------------------
 
Ian Jefferies   - HypoThermia ( uie@planetquake.com )
http://www.planetquake.com/uie
*/


#include "../q3_ui/q3ui_local.h"


#define SETUP_MENU_VERTICAL_SPACING		34

#define ART_BACK0		"menu/art/back_0"
#define ART_BACK1		"menu/art/back_1"
#define ART_FRAMEL		"menu/art/frame2_l"
#define ART_FRAMER		"menu/art/frame1_r"

#define ID_CUSTOMIZEPLAYER		10
#define ID_CUSTOMIZECONTROLS	11
#define ID_SYSTEMCONFIG			12
#define ID_GAME					13
#define ID_CDKEY				14
#define ID_LOAD					15
#define ID_SAVE					16
#define ID_DEFAULTS				17
#define ID_BACK					18
#define ID_CUSTOMIZEMODEL		19


typedef struct {
    menuframework_s	menu;

    menutext_s		banner;
    menubitmap_s	framel;
    menubitmap_s	framer;
    menutext_s		setupplayer;
    menutext_s		setupmodel;
    menutext_s		setupcontrols;
    menutext_s		setupsystem;
    menutext_s		game;
    menutext_s		cdkey;
    menutext_s		load;
    menutext_s		save;
    menutext_s		defaults;
    menubitmap_s	back;
}
setupMenuInfo_t;

static setupMenuInfo_t	setupMenuInfo;


/*
=================
Setup_ResetDefaults_Action
=================
*/
static void Setup_ResetDefaults_Action( qboolean result ) {
    if( !result ) {
        return;
    }
    trap_Cmd_ExecuteText( EXEC_APPEND, "exec default.cfg\n");
    trap_Cmd_ExecuteText( EXEC_APPEND, "cvar_restart\n");
    trap_Cmd_ExecuteText( EXEC_APPEND, "vid_restart\n" );
}


/*
=================
Setup_ResetDefaults_Draw
=================
*/
static void Setup_ResetDefaults_Draw( void ) {
    UI_DrawProportionalString( SCREEN_WIDTH/2, 356 + PROP_HEIGHT * 0, "WARNING: This will reset *ALL*", UI_CENTER|UI_SMALLFONT, color_yellow );
    UI_DrawProportionalString( SCREEN_WIDTH/2, 356 + PROP_HEIGHT * 1, "options to their default values.", UI_CENTER|UI_SMALLFONT, color_yellow );
}


/*
===============
UI_SetupMenu_Event
===============
*/
static void UI_SetupMenu_Event( void *ptr, int event ) {
    if( event != QM_ACTIVATED ) {
        return;
    }

    switch( ((menucommon_s*)ptr)->id ) {
    case ID_CUSTOMIZEPLAYER:
        UI_PlayerSettingsMenu();
        break;

    case ID_CUSTOMIZEMODEL:
        UI_PlayerModelMenu();
        break;

    case ID_CUSTOMIZECONTROLS:
        UI_ControlsMenu();
        break;

    case ID_SYSTEMCONFIG:
        UI_GraphicsOptionsMenu();
        break;

    case ID_GAME:
        UI_PreferencesMenu();
        break;

    case ID_CDKEY:
        UI_CDKeyMenu();
        break;

    case ID_LOAD:
        UI_LoadConfigMenu();
        break;

    case ID_SAVE:
        UI_SaveConfigMenu();
        break;

    case ID_DEFAULTS:
        UI_ConfirmMenu( "SET TO DEFAULTS?", Setup_ResetDefaults_Draw, Setup_ResetDefaults_Action );
        break;

    case ID_BACK:
        UI_PopMenu();
        break;
    }
}


/*
===============
UI_SetupMenu_Init
===============
*/
static void UI_SetupMenu_Init( void ) {
    int				y;
    int 			style;

    UI_SetupMenu_Cache();

    memset( &setupMenuInfo, 0, sizeof(setupMenuInfo) );
    setupMenuInfo.menu.wrapAround = qtrue;
    setupMenuInfo.menu.fullscreen = qtrue;

    setupMenuInfo.banner.generic.type				= MTYPE_BTEXT;
    setupMenuInfo.banner.generic.x					= 320;
    setupMenuInfo.banner.generic.y					= 16;
    setupMenuInfo.banner.string						= "SETUP";
    setupMenuInfo.banner.color						= color_white;
    setupMenuInfo.banner.style						= UI_CENTER;

    setupMenuInfo.framel.generic.type				= MTYPE_BITMAP;
    setupMenuInfo.framel.generic.name				= ART_FRAMEL;
    setupMenuInfo.framel.generic.flags				= QMF_INACTIVE;
    setupMenuInfo.framel.generic.x					= 0;//INCIN full background half logo
    setupMenuInfo.framel.generic.y					= 0;
    setupMenuInfo.framel.width  					= 640/2;
    setupMenuInfo.framel.height  					= 480;//incin full background half logo

    setupMenuInfo.framer.generic.type				= MTYPE_BITMAP;
    setupMenuInfo.framer.generic.name				= ART_FRAMER;
    setupMenuInfo.framer.generic.flags				= QMF_INACTIVE;
    setupMenuInfo.framer.generic.x					= 640/2;//incin full background half logo
    setupMenuInfo.framer.generic.y					= 0;
    setupMenuInfo.framer.width  					= 640/2;
    setupMenuInfo.framer.height  					= 480;//incin full background half logo

    y = 128 - SETUP_MENU_VERTICAL_SPACING;
    setupMenuInfo.setupplayer.generic.type			= MTYPE_PTEXT;
    setupMenuInfo.setupplayer.generic.flags			= QMF_CENTER_JUSTIFY|QMF_PULSEIFFOCUS;
    setupMenuInfo.setupplayer.generic.x				= 320;
    setupMenuInfo.setupplayer.generic.y				= y;
    setupMenuInfo.setupplayer.generic.id			= ID_CUSTOMIZEPLAYER;
    setupMenuInfo.setupplayer.generic.callback		= UI_SetupMenu_Event;
    setupMenuInfo.setupplayer.string				= "PLAYER";
    setupMenuInfo.setupplayer.color					= color_green;
    setupMenuInfo.setupplayer.style					= UI_CENTER;

    y += SETUP_MENU_VERTICAL_SPACING;
    setupMenuInfo.setupmodel.generic.type		= MTYPE_PTEXT;
    setupMenuInfo.setupmodel.generic.flags		= QMF_CENTER_JUSTIFY|QMF_PULSEIFFOCUS;
    setupMenuInfo.setupmodel.generic.x			= 320;
    setupMenuInfo.setupmodel.generic.y			= y;
    setupMenuInfo.setupmodel.generic.id			= ID_CUSTOMIZEMODEL;
    setupMenuInfo.setupmodel.generic.callback	= UI_SetupMenu_Event;
    setupMenuInfo.setupmodel.string				= "MODEL";
    setupMenuInfo.setupmodel.color				= color_green;
    setupMenuInfo.setupmodel.style				= UI_CENTER;

    y += SETUP_MENU_VERTICAL_SPACING;
    setupMenuInfo.setupcontrols.generic.type		= MTYPE_PTEXT;
    setupMenuInfo.setupcontrols.generic.flags		= QMF_CENTER_JUSTIFY|QMF_PULSEIFFOCUS;
    setupMenuInfo.setupcontrols.generic.x			= 320;
    setupMenuInfo.setupcontrols.generic.y			= y;
    setupMenuInfo.setupcontrols.generic.id			= ID_CUSTOMIZECONTROLS;
    setupMenuInfo.setupcontrols.generic.callback	= UI_SetupMenu_Event;
    setupMenuInfo.setupcontrols.string				= "CONTROLS";
    setupMenuInfo.setupcontrols.color				= color_green;
    setupMenuInfo.setupcontrols.style				= UI_CENTER;

    y += SETUP_MENU_VERTICAL_SPACING;
    setupMenuInfo.setupsystem.generic.type			= MTYPE_PTEXT;
    setupMenuInfo.setupsystem.generic.flags			= QMF_CENTER_JUSTIFY|QMF_PULSEIFFOCUS;
    setupMenuInfo.setupsystem.generic.x				= 320;
    setupMenuInfo.setupsystem.generic.y				= y;
    setupMenuInfo.setupsystem.generic.id			= ID_SYSTEMCONFIG;
    setupMenuInfo.setupsystem.generic.callback		= UI_SetupMenu_Event;
    setupMenuInfo.setupsystem.string				= "SYSTEM";
    setupMenuInfo.setupsystem.color					= color_green;
    setupMenuInfo.setupsystem.style					= UI_CENTER;

    y += SETUP_MENU_VERTICAL_SPACING;
    setupMenuInfo.game.generic.type					= MTYPE_PTEXT;
    setupMenuInfo.game.generic.flags				= QMF_CENTER_JUSTIFY|QMF_PULSEIFFOCUS;
    setupMenuInfo.game.generic.x					= 320;
    setupMenuInfo.game.generic.y					= y;
    setupMenuInfo.game.generic.id					= ID_GAME;
    setupMenuInfo.game.generic.callback				= UI_SetupMenu_Event;
    setupMenuInfo.game.string						= "GAME OPTIONS";
    setupMenuInfo.game.color						= color_green;
    setupMenuInfo.game.style						= UI_CENTER;

    y += SETUP_MENU_VERTICAL_SPACING;
    setupMenuInfo.cdkey.generic.type				= MTYPE_PTEXT;
    setupMenuInfo.cdkey.generic.flags				= QMF_CENTER_JUSTIFY|QMF_PULSEIFFOCUS;
    setupMenuInfo.cdkey.generic.x					= 320;
    setupMenuInfo.cdkey.generic.y					= y;
    setupMenuInfo.cdkey.generic.id					= ID_CDKEY;
    setupMenuInfo.cdkey.generic.callback			= UI_SetupMenu_Event;
    setupMenuInfo.cdkey.string						= "CD Key";
    setupMenuInfo.cdkey.color						= color_green;
    setupMenuInfo.cdkey.style						= UI_CENTER;

    style = QMF_CENTER_JUSTIFY|QMF_PULSEIFFOCUS;
    if( trap_Cvar_VariableValue( "cl_paused" ) ) {
        style |= QMF_GRAYED;
    }

    y += SETUP_MENU_VERTICAL_SPACING;
    setupMenuInfo.load.generic.type					= MTYPE_PTEXT;
    setupMenuInfo.load.generic.flags				= style;
    setupMenuInfo.load.generic.x					= 320;
    setupMenuInfo.load.generic.y					= y;
    setupMenuInfo.load.generic.id					= ID_LOAD;
    setupMenuInfo.load.generic.callback				= UI_SetupMenu_Event;
    setupMenuInfo.load.string						= "LOAD/EXEC CONFIG";
    setupMenuInfo.load.color						= color_green;
    setupMenuInfo.load.style						= UI_CENTER;

    y += SETUP_MENU_VERTICAL_SPACING;
    setupMenuInfo.save.generic.type					= MTYPE_PTEXT;
    setupMenuInfo.save.generic.flags				= style;
    setupMenuInfo.save.generic.x					= 320;
    setupMenuInfo.save.generic.y					= y;
    setupMenuInfo.save.generic.id					= ID_SAVE;
    setupMenuInfo.save.generic.callback				= UI_SetupMenu_Event;
    setupMenuInfo.save.string						= "SAVE CONFIG";
    setupMenuInfo.save.color						= color_green;
    setupMenuInfo.save.style						= UI_CENTER;

    y += SETUP_MENU_VERTICAL_SPACING;
    setupMenuInfo.defaults.generic.type				= MTYPE_PTEXT;
    setupMenuInfo.defaults.generic.flags			= style;
    setupMenuInfo.defaults.generic.x				= 320;
    setupMenuInfo.defaults.generic.y				= y;
    setupMenuInfo.defaults.generic.id				= ID_DEFAULTS;
    setupMenuInfo.defaults.generic.callback			= UI_SetupMenu_Event;
    setupMenuInfo.defaults.string					= "DEFAULTS";
    setupMenuInfo.defaults.color					= color_green;
    setupMenuInfo.defaults.style					= UI_CENTER;

    setupMenuInfo.back.generic.type					= MTYPE_BITMAP;
    setupMenuInfo.back.generic.name					= ART_BACK0;
    setupMenuInfo.back.generic.flags				= QMF_LEFT_JUSTIFY|QMF_PULSEIFFOCUS;
    setupMenuInfo.back.generic.id					= ID_BACK;
    setupMenuInfo.back.generic.callback				= UI_SetupMenu_Event;
    setupMenuInfo.back.generic.x					= 0;
    setupMenuInfo.back.generic.y					= 480-64;
    setupMenuInfo.back.width						= 128;
    setupMenuInfo.back.height						= 64;
    setupMenuInfo.back.focuspic						= ART_BACK1;

    Menu_AddItem( &setupMenuInfo.menu, &setupMenuInfo.banner );
    Menu_AddItem( &setupMenuInfo.menu, &setupMenuInfo.framel );
    Menu_AddItem( &setupMenuInfo.menu, &setupMenuInfo.framer );
    Menu_AddItem( &setupMenuInfo.menu, &setupMenuInfo.setupplayer );
    Menu_AddItem( &setupMenuInfo.menu, &setupMenuInfo.setupmodel );
    Menu_AddItem( &setupMenuInfo.menu, &setupMenuInfo.setupcontrols );
    Menu_AddItem( &setupMenuInfo.menu, &setupMenuInfo.setupsystem );
    Menu_AddItem( &setupMenuInfo.menu, &setupMenuInfo.game );
    Menu_AddItem( &setupMenuInfo.menu, &setupMenuInfo.cdkey );
    Menu_AddItem( &setupMenuInfo.menu, &setupMenuInfo.defaults );
    Menu_AddItem( &setupMenuInfo.menu, &setupMenuInfo.load );
    Menu_AddItem( &setupMenuInfo.menu, &setupMenuInfo.save );
    Menu_AddItem( &setupMenuInfo.menu, &setupMenuInfo.back );
}


/*
=================
UI_SetupMenu_Cache
=================
*/
void UI_SetupMenu_Cache( void ) {
    trap_R_RegisterShaderNoMip( ART_BACK0 );
    trap_R_RegisterShaderNoMip( ART_BACK1 );
    trap_R_RegisterShaderNoMip( ART_FRAMEL );
    trap_R_RegisterShaderNoMip( ART_FRAMER );
}


/*
===============
UI_SetupMenu
===============
*/
void UI_SetupMenu( void ) {
    UI_SetupMenu_Init();
    UI_PushMenu( &setupMenuInfo.menu );
}
