// ManiaDrive Track Editor - CQFD Corp
// http://maniadrive.raydium.org/
char *version="0.41";
char *title="CQFD Corp. Mania2";

// since we play with our own camera and not Raydium's one:
#define RENDER_DEBUG_NO_CLIP
#define NO_ODE_SUPPORT
#include "raydium/index.c"
#include "mania.h"

#define POP_MODE_ELEM   1
#define POP_MODE_BOX    2

#ifdef WIN32
#define MANIA_BINARY "mania_drive.exe"
#else
#define MANIA_BINARY "./mania_drive.static"
#endif


int grid_load(char *filename);
void grid_save(char *filename);
void build_gui_access(raydium_gui_Object *w);
void build_gui_menu(raydium_gui_Object *w);
void grid_generate_obj(void);
void grid_init_all(void);
void box_init_all(void);
void grid_generate_obj(void);
void data_init(void);

GLfloat modl_zoom=10;
GLint px=0,py=0;
GLfloat pz=0;
GLint curobj=0;
GLint curbox=0;
GLint curangle=0;
int pop_mode=POP_MODE_ELEM;
signed char view_glue=0;


int n_boxpresets=0;
Box boxpreset[MAX_ELEMS];

char current_track[RAYDIUM_MAX_NAME_LEN];

//////////////////////////////////////////////////////////// gui
void btnExitOk(raydium_gui_Object *w)
{
raydium_parser_db_set("Mania2-CurrentTrack",current_track);
exit(0);
}

void btnExitCancel(raydium_gui_Object *w)
{
raydium_gui_window_delete_name("exit");
}

void btnClearOk(raydium_gui_Object *w)
{
grid_init_all();
box_init_all();
data_init();
grid_generate_obj();
current_track[0]=0;
px=0;
py=0;
pz=0;
raydium_gui_window_delete_name("clear");
}

void btnClearCancel(raydium_gui_Object *w)
{
raydium_gui_window_delete_name("clear");
}


void gui_exit(void)
{
int handle;

if(raydium_gui_window_find("exit")>=0)
    {
    btnExitCancel(NULL);
    return;
    }

handle=raydium_gui_window_create("exit",25,45,60,15);
raydium_gui_widget_sizes(0,0,18);
raydium_gui_label_create("lblExit",handle,50,80,"Do you really want to exit ?",0,0,0);
raydium_gui_widget_sizes(15,5,18);
raydium_gui_button_create("btnExitOk",handle,20,15,"OK",btnExitOk);
raydium_gui_button_create("btnExitCancel",handle,50,15,"Cancel",btnExitCancel);

raydium_gui_widget_focus_name("btnExitOk","exit");
}

void btnLoad(raydium_gui_Object *w)
{
char str[RAYDIUM_MAX_NAME_LEN];
raydium_gui_read_name("menu","edtFilename",str);
if(grid_load(str))
    build_gui_access(NULL);
}

void btnSave(raydium_gui_Object *w)
{
char str[RAYDIUM_MAX_NAME_LEN];
raydium_gui_read_name("menu","edtFilename",str);
grid_save(str);
build_gui_access(NULL);
}

void btnExit(raydium_gui_Object *w)
{
gui_exit();
}

void btnMetaOk(raydium_gui_Object *w)
{
char d[6][512];

raydium_gui_read_name("menu","edtName",d[0]);
raydium_gui_read_name("menu","edtAuthor",d[1]);
raydium_gui_read_name("menu","edtGold",d[2]);
raydium_gui_read_name("menu","edtAuthorTime",d[3]);
raydium_gui_read_name("menu","edtMsg",d[4]);
raydium_gui_read_name("menu","edtEnt",d[5]);

sprintf(tdata,"%s;%s;%s;%s;%s;%s",d[0],d[1],d[2],d[3],d[4],d[5]);
build_gui_menu(NULL);
}


void btnTest(raydium_gui_Object *w)
{
char str[RAYDIUM_MAX_NAME_LEN*2];
char mni[RAYDIUM_MAX_NAME_LEN];
char *mni_simple="dyn_track.mni";
int ret;

// must protect mni from shell
raydium_file_home_path_cpy(mni_simple,mni);

strcpy(str,current_track);
grid_save(mni);
strcpy(current_track,str);

sprintf(str,"%s --mni \"%s\"",MANIA_BINARY,mni);
ret=system(str);
if(ret!=0)
    {
    raydium_log("CANNOT TEST MNI TRACK FROM '%s'",mni);
    }
build_gui_access(NULL);
}

void build_gui_clear(raydium_gui_Object *w)
{
int handle;

handle=raydium_gui_window_create("clear",25,45,60,15);
raydium_gui_widget_sizes(0,0,18);
raydium_gui_label_create("lblClear",handle,50,80,"Do you really want to clear track ?",0,0,0);
raydium_gui_widget_sizes(15,5,18);
raydium_gui_button_create("btnClearOk",handle,20,15,"OK",btnClearOk);
raydium_gui_button_create("btnClearCancel",handle,50,15,"Cancel",btnClearCancel);

raydium_gui_widget_focus_name("btnClearOk","clear");
}


#undef YDEC
#define YDEC 5.f
void build_gui_help(raydium_gui_Object *w)
{
int handle;
float y;

raydium_gui_window_delete_name("menu");
handle=raydium_gui_window_create("menu",10,25,80,50);

raydium_gui_widget_sizes(0,0,18);
raydium_gui_label_create("lblTit(see above).
NID sending frequency is now limited, since a lot of overhead was generated
when new clients were joining a "big" network.
**/

__rayapi void raydium_ode_network_apply (raydium_ode_network_Event * ev);
/**
Internal. This callback is fired when new data is received. A lot of things
are done here (timeouts, dead reckoning, ...)
**/

__rayapi void raydium_ode_network_read (void);
/**
Internal. Reads new packets, if any.
**/

__rayapi void raydium_ode_network_element_new (int e);
/**
Internal. Send a new element to network.
**/

__rayapi void raydium_ode_network_element_delete (int e);
/**
Internal. Send "delete event" to network, since we're deleting one of "our" elements.
**/

__rayapi void raydium_ode_network_explosion_send (raydium_ode_network_Explosion * exp);
/**
Internal. Send a new explosion event.
**/

__rayapi signed char raydium_ode_network_element_isdistant (int elem);
/**
Will return true (1) if element ##elem## is "distant", or false (0) if it's
one of "our" elements.
**/

__rayapi signed char raydium_ode_network_element_isdistant_name (char *elem);
/**
Same as above, but using element's name.
**/

__rayapi signed char raydium_ode_network_element_distantowner(int elem);
/**
Returns UID (peer "user" ID) for the distant element owner. See ##network.c##
documentation for more informations about UID.
**/

__rayapi signed char raydium_ode_network_element_distantowner_name(char *elem);
/**
Same as above, but using element's name.
**/

__rayapi void raydium_ode_network_element_trajectory_correct (int elem);
/**
Internal. Applies dead reckoning values to element.
**/

__rayapi void raydium_ode_network_elment_next_local(void);
/**
Call this function when you don't want that the next created element is sent
to network ("local only" element).
**/

#endif