#pragma progs_dat "../menu.dat"

//#pragma target fte
#define MENU					//select the module
#include "fteextensions.qc"		//also sets up system defs

#includelist
menusys/mitems.qc				//root item type
menusys/mitems_common.qc		//basic types
menusys/mitem_desktop.qc		//other sort of root item
menusys/mitem_exmenu.qc			//fullscreen/exclusive menus
menusys/mitem_edittext.qc		//simple text editor
menusys/mitem_tabs.qc			//tabs
menusys/mitem_colours.qc		//colour picker
menusys/mitem_checkbox.qc		//checkbox (boolean thingies)
menusys/mitem_slider.qc			//scrollbars
menusys/mitem_combo.qc			//multiple-choice thingies
menusys/mitem_bind.qc			//key binding thingie
menusys/mitem_spinnymodel.qc	//menu art
#endlist

//might as well put this here.
void(mitem_desktop desktop) M_Pop =
{
	mitem it = desktop.item_kactivechild;
	if (it)
		it.item_remove();
};

//define the commands.
//cmd argments are: Name, Function, Sourcefile(may be empty)
#define concommandslist									\
	cmd("m_main",		M_Main,			menu/main.qc)			\
	cmd("m_pop",		M_Pop,			)				\
	cmd("m_options",	M_Options,		menu/options.qc)		\
	cmd("m_keys",		M_Options_Keys,		menu/options_keys.qc)		\
	cmd("m_basicopts",	M_Options_Basic,	menu/options_basic.qc)		\
	cmd("m_video",		M_Options_Video,	menu/options_video.qc)		\
	cmd("m_effects",	M_Options_Effects,	menu/options_effects.qc)	\
	cmd("m_audio",		M_Options_Audio,	menu/options_audio.qc)		\
	cmd("m_particles",	M_Options_Particles,	menu/options_particles.qc)	\
	cmd("m_hud",		M_Options_Hud,		menu/options_hud.qc)		\
	cmd("m_load",		M_Load,			menu/loadsave.qc)		\
	cmd("m_save",		M_Save,			)				\
	cmd("m_quit",		M_Quit,			menu/quit.qc)			\
	cmd("m_mods",		M_Menu_Mods,		menu/mods.qc)			\
	cmd("m_updates",	M_Menu_Updates,		menu/updates.qc)		\
	cmd("m_cvars",		M_Menu_Cvars,		menu/cvars.qc)			\
	cmd("m_newgame",	M_NewGame,		menu/newgame.qc)		\
	cmd("m_servers",	M_Servers,		menu/servers.qc)		\
	cmd("m_configs",	M_Configs,		menu/options_configs.qc)	\
	cmd("m_reset",		M_Reset,		)				\
	cmd("m_dir",		M_Dir,		)				\
	cmd("m_preset",		M_Preset,		menu/presets.qc)
	
//make sure all the right files are included
#define cmd(n,fnc,inc) inc
#includelist
	concommandslist
#endlist
#undef cmd

mitem_desktop desktop;
void() m_shutdown = {};
void(vector screensize) m_draw =
{
	if (dp_workarounds)
		cltime = gettime(0);
	items_draw(desktop, screensize);
};
float(float evtype, float scanx, float chary, float devid) Menu_InputEvent = {return items_keypress(desktop, evtype, scanx, chary, devid);};
void(float scan, float chr) m_keydown = {ui.mousepos = getmousepos();queryscreensize();items_keypress(desktop, IE_KEYDOWN, scan, chr, 0);};	//for DP compat.
void(float scan, float chr) m_keyup = {ui.mousepos = getmousepos();queryscreensize();items_keypress(desktop, IE_KEYUP, scan, chr, 0);};		//for DP compat.
void(float mode) m_toggle
{	//mode is stupid. 1=enable,0=disable,-1=actually toggle.
	if (mode < 0)
		mode = !desktop.item_kactivechild;
	if (mode)
		M_Main(desktop);
	else while(desktop.item_kactivechild)
	{
		mitem it = desktop.item_kactivechild;
		if (it.item_flags & IF_NOKILL)
			break;
		it.item_remove();
	}

	items_updategrabs(TRUE);
};
float(string cstr) m_consolecommand =
{
	tokenize(cstr);
	string cmd = argv(0);

	switch(cmd)
	{
//switch on the known commands.
#define cmd(n,f) case n: f(desktop); break;
	concommandslist
#undef cmd
	default:
		return FALSE;
	}
	items_updategrabs(TRUE);
	return TRUE;
}

var float autocvar_dp_workarounds = FALSE;
void() m_init =
{
	FingerprintEngine();

	desktop = spawn(mitem_desktop);

	if (checkbuiltin(registercommand))
	{
#define cmd(n,f) registercommand(n);
		concommandslist
#undef cmd
	}
	else
	{
	//register the console commands via the alias command.
#define cmd(n,f) localcmd("alias " n " \"menu_cmd " n " $*\"\n");
		concommandslist
#undef cmd
	}

	//work around some dp differences/bugs.
	if (autocvar(dp_workarounds, FALSE))
		dp_workarounds = TRUE;
	
	if (dp_workarounds)
		print("^1WORKING AROUND DP BUGS\n");
	
	//for compat with DP, 'none' is the default cursor in menuqc.
	//naturally this is not ideal.
	if (checkextension("FTE_QC_HARDWARECURSORS"))
		setcursormode(TRUE, "");
	else
		print("No hardware cursors\n");

	if (clientstate() == 1)	//disconnected==1, supposedly
		m_toggle(1);
};
void(string cstr) GameCommand =
{
	tokenize(cstr);
	string cmd = argv(0);

	switch(cmd)
	{
//switch on the known commands.
#define cmd(n,f) case n: f(desktop); break;
	concommandslist
#undef cmd
	default:
		print("unknown command ", cmd, "\n");
		break;
	}
	items_updategrabs(TRUE);
};

