// ----------------------------------------------------------------------------
// Blitz-Irrlicht 1.8.3 Wrapper by Ed Musolino 2015
// Based off of Frank Dodd's Irrlicht 1.7 Freebasic Wrapper (2006-2010)
// ----------------------------------------------------------------------------

//  This software is provided 'as-is', without any express Or implied
//  warranty.  In no event will the authors be held liable For any damages
//  arising from the use of this software.

//  Permission is granted To anyone To use this software For any purpose,
//  including commercial applications, And To alter it And redistribute it
//  freely, subject To the following restrictions:

//  1. The Origin of this software must Not be misrepresented; you must not
//     claim that you wrote the original software. If you use this software
//     in a product, an acknowledgement in the product documentation would be
//     appreciated but is Not required.
//  2. Altered source versions must be clearly marked as such, And must Not be
//     misrepresented as being the original software.
//  3. This notice may Not be removed Or altered from any source distribution.
//
// /////////////////////////////////////////////////////////////////////////////

/* ////////////////////////////////////////////////////////////////////////////
includes
*/
#include "bIrrlicht.h"


/* ////////////////////////////////////////////////////////////////////////////
global variables
*/

/* ////////////////////////////////////////////////////////////////////////////
external variables
*/
extern IrrlichtDevice *device;
extern IVideoDriver* driver;
extern ISceneManager* smgr;
extern IGUIEnvironment* guienv;



/* ////////////////////////////////////////////////////////////////////////////
Global Function Declarations

all of the below functions are declared as C functions and are exposed without
any mangled names so that they can be easily imported into imperative
languages like FreeBasic
*/
extern "C"
{

/* ////////////////////////////////////////////////////////////////////////////
GUI FUNCTIONS
*/

/* ----------------------------------------------------------------------------
remove all GUI objects from the display
*/
DLL_EXPORT void BBCALL iGUIClear( void )
{
	guienv->clear();
}

/* ----------------------------------------------------------------------------
remove a GUI object from the display
*/
DLL_EXPORT void BBCALL iGUIRemove( IGUIElement *element )
{
	if ( element )
	{
		IGUIElement * parent = element->getParent();
		if ( parent ) parent->removeChild( element );
	}
}

/* ----------------------------------------------------------------------------
gets gui object text
*/
DLL_EXPORT const char* BBCALL iGUIGetText( IGUIElement *element )  //NEEDS WORK CONVERT TO COMPAT STRING FOR RETURN
{
	if ( element )
		return (const char*)element->getText();
	else
		return "";
}

/* ----------------------------------------------------------------------------
sets gui object text
*/
DLL_EXPORT void BBCALL iGUISetText( IGUIElement *element, const char * wcptrText )
{
	if ( element )
		element->setText(ConvertCharToWChar(wcptrText));
}


/* ----------------------------------------------------------------------------
add a window frame object to the GUI
*/
DLL_EXPORT void * BBCALL iCreateWindow(
                                const char * wcptrTitle,
                                int iTopX, int iTopY, int iBotX, int iBotY,
                                bool modal, IGUIElement *parent )
{
	return guienv->addWindow(
			rect<s32>(iTopX, iTopY, iBotX, iBotY),
			modal, ConvertCharToWChar(wcptrTitle), parent);
}

/* ----------------------------------------------------------------------------
add a static text object to the GUI
*/
DLL_EXPORT void * BBCALL iStaticText(
                                const char * wcptrText,
                                int iTopX, int iTopY, int iBotX, int iBotY,
                                bool boBorder, bool boWordWrap, IGUIElement *parent )
{
	return guienv->addStaticText(
		ConvertCharToWChar(wcptrText),
            rect<int>(iTopX, iTopY, iBotX, iBotY),
            boBorder, boWordWrap, parent );
}

/* ----------------------------------------------------------------------------
adds a clickable button object
*/
DLL_EXPORT void * BBCALL iCreateButton( s32 x, s32 y, s32 w, s32 h, s32 id,
							  const char * wcptrLabel,
							  const char * wcptrTip,
							  IGUIElement *parent )
{
	return guienv->addButton(
			rect<s32>(x,y,w,h), parent, id,
			ConvertCharToWChar(wcptrLabel), ConvertCharToWChar(wcptrTip));
}

/* ----------------------------------------------------------------------------
adds a scroll bar object
*/
DLL_EXPORT void * BBCALL iCreateScrollBar( bool Horizontal,
								   s32 xt, s32 yt, s32 xb, s32 yb,
								   s32 id,
								   s32 pos,
								   s32 max,
								   IGUIElement *parent )
{
	IGUIScrollBar* scrollbar = guienv->addScrollBar(
			Horizontal,
			rect<s32>(xt, yt, xb, yb), parent, id);
	scrollbar->setMax(max);
	scrollbar->setPos(pos);

	return scrollbar;
}


/* ----------------------------------------------------------------------------
adds a list box object
*/
DLL_EXPORT void * BBCALL iCreateListBox( s32 xt, s32 yt, s32 xb, s32 yb,
								 s32 id,
								 bool background,
								 IGUIElement *parent )
{
	return guienv->addListBox(rect<s32>(xt, yt, xb, yb), parent, id, background);
}

/* ----------------------------------------------------------------------------
adds a list box object
*/
DLL_EXPORT void BBCALL iAddGadgetItem( IGUIElement *element, const char * wcptrText )
{
	if ( element )
		((IGUIListBox*)element)->addItem(ConvertCharToWChar(wcptrText));
}

/* ----------------------------------------------------------------------------
adds a list box object
*/
DLL_EXPORT void BBCALL iInsertGadgetItem( IGUIElement *element, const char * wcptrText, u32 index )
{
	if ( element )
		((IGUIListBox*)element)->insertItem(index, ConvertCharToWChar(wcptrText), 0);
}

/* ----------------------------------------------------------------------------
adds a list box object
*/
DLL_EXPORT void BBCALL iRemoveGadgetItem( IGUIElement *element, u32 index )
{
	if ( element )
		((IGUIListBox* )element)->removeItem( index );
}

/* ----------------------------------------------------------------------------
adds a list box object
*/
DLL_EXPORT void BBCALL iSelectGadgetItem( IGUIElement *element, u32 index )
{
	if ( element )
		((IGUIListBox* )element)->setSelected( index );
}

/* ----------------------------------------------------------------------------
adds an edit box object
*/
DLL_EXPORT void * BBCALL iCreateEditBox( const char * wcptrText,
								   s32 xt, s32 yt, s32 xb, s32 yb,
								   s32 id,
								   bool border,
								   bool password,
								   IGUIElement *parent )
{
	IGUIEditBox * editbox= guienv->addEditBox(
		ConvertCharToWChar(wcptrText), rect<s32>(xt, yt, xb, yb), border, parent, id);
	if ( password )
		editbox->setPasswordBox( true );
	return editbox;
}

/* ----------------------------------------------------------------------------
adds an image object
*/
DLL_EXPORT void * BBCALL iStaticImage(  ITexture * texture,
								s32 x, s32 y,
								bool useAlpha,
								s32 id,
								IGUIElement *parent )
{
	// add the engine logo
	return guienv->addImage( texture, position2d<int>(x,y), useAlpha, parent, id );
}

/* ----------------------------------------------------------------------------
adds a check box object
*/
DLL_EXPORT void * BBCALL iCreateCheckBox( const char * wcptrText,
								  s32 xt, s32 yt, s32 xb, s32 yb,
								  s32 id,
								   bool checked,
								   IGUIElement *parent )
{
	return guienv->addCheckBox(checked, rect<s32>(xt, yt, xb, yb), parent, id, ConvertCharToWChar(wcptrText));
}

/* ----------------------------------------------------------------------------
check the check box object
*/
DLL_EXPORT void BBCALL iCheckCheckBox( IGUIElement *element, bool checked )
{
	if ( element )
		((IGUICheckBox *)element)->setChecked( checked );
}


/* ----------------------------------------------------------------------------
adds a scroll bar object
*/
DLL_EXPORT void * BBCALL iAddFileOpen( const char * wcptrLabel, s32 id, bool modal, IGUIElement *parent )
{
	return guienv->addFileOpenDialog(ConvertCharToWChar(wcptrLabel), modal, parent, id);
}

/* ----------------------------------------------------------------------------
set the font used by the GUI
*/
DLL_EXPORT void BBCALL iGUISetFont( IGUIFont* font )
{
	IGUISkin* skin = guienv->getSkin();
	if (font)
		skin->setFont(font);
}

/* ----------------------------------------------------------------------------
set the font used by the GUI
*/
DLL_EXPORT void BBCALL iGUISetColor( EGUI_DEFAULT_COLOR element, s32 red, s32 green, s32 blue, s32 alpha )
{
	IGUISkin* skin = guienv->getSkin();
	skin->setColor(element, video::SColor(alpha, red, green, blue ));
}


/* ////////////////////////////////////////////////////////////////////////////
all of the above functions are declared as C functions and are exposed without
any mangled names
*/
}
