/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/include/sgx/gui/guimanager.hpp

*/

#ifndef SGX_CORE_GUI_GUIMANAGER_HPP
#define SGX_CORE_GUI_GUIMANAGER_HPP	1

class CSGXGUI : public CSGXEngine {
public:

	static CSGXGUI *ms_pSingleton;
	static CSGXGUI *get();

	virtual ~CSGXGUI();
	virtual void unregisterObjects();

	virtual CSGXGUISkin * getCurrentSkin();
	virtual const CSGXGUISkin * const getCurrentSkin() const;
	virtual CSGXGUISkin *	setCurrentSkin(CSGXGUISkin *pSkin);
	virtual CSGXDrawSurface *setCurrentSurface(CSGXDrawSurface *pSkin);
	virtual const CSGXFont * setDefaultFont(const CSGXFont *pFont);

	virtual void			applyPointerPosition(const sgxPoint2i &pos);
	virtual	void			applyPointerUp();
	virtual void			applyPointerDown();
	virtual void			applyActiveCycleNext();
	virtual void			applyActiveCyclePrevious();
	virtual void			applyActiveCycleSelect();
	virtual	void			applyKeyboardUp(const tUINT32 key);
	virtual void			applyKeyboardDown(const tUINT32 key);
	virtual void			applyKeyboardState(const tUINT32 key, const tBOOL state);
	virtual tUINT32			applyKeyboardState(const tBYTE *bitStates);	// 8 bytes (256 bits) of 'down' messages

	virtual sgxWidgetGroup *createGroup(const tUINT32 userData, const tBOOL registerWithGUI=TRUE);
	virtual sgxWidget *		createFrame(const tINT32 x=0, const tINT32 y=0, const sgxGUIFrameParams &params=sgxGUIFrameParams(), const tBOOL registerWithGUI=TRUE);
	virtual sgxWidget *		createButton(const tINT32 x=0, const tINT32 y=0, const sgxGUIButtonParams &params=sgxGUIButtonParams(), CSGXCallback *pCallback=NULL, const tUINT32 userdata=0, const tBOOL registerWithGUI=TRUE);
	virtual sgxWidget *		createImageButton(const tINT32 x=0, const tINT32 y=0, const sgxGUIImageButtonParams &params=sgxGUIImageButtonParams(), CSGXCallback *pCallback=NULL, const tUINT32 userdata=0, const tBOOL registerWithGUI=TRUE);
	virtual sgxWidget *		createCheckBox(const tINT32 x=0, const tINT32 y=0, const sgxGUICheckBoxParams &params=sgxGUICheckBoxParams(), CSGXCallback *pCallback=NULL, const tUINT32 userdata=0, const tBOOL registerWithGUI=TRUE);
	virtual sgxWidget *		createRadioButton(const tINT32 x=0, const tINT32 y=0, const sgxGUIRadioButtonParams &params=sgxGUIRadioButtonParams(), CSGXCallback *pCallback=NULL, const tUINT32 userdata=0, const tBOOL registerWithGUI=TRUE);
	virtual sgxWidget *		createHotSpot(const tINT32 x=0, const tINT32 y=0, const sgxGUIHotSpotParams &params=sgxGUIHotSpotParams(), CSGXCallback *pCallback=NULL, const tUINT32 userdata=0, const tBOOL registerWithGUI=TRUE);
	virtual sgxWidget *		createStaticText(const tINT32 x=0, const tINT32 y=0, const CGUIDesign::tJustify jx=CGUIDesign::fmtLeftJustify, const CGUIDesign::tJustify jy=CGUIDesign::fmtTopAlignment, const sgxGUIStaticTextParams &params=sgxGUIStaticTextParams(), const tBOOL registerWithGUI=TRUE);
	virtual sgxWidget *		createStaticImage(const tINT32 x=0, const tINT32 y=0, const sgxGUIStaticImageParams &params=sgxGUIStaticImageParams(), const tBOOL registerWithGUI=TRUE);
	virtual sgxWidget *		createDynamicImage(const tINT32 x=0, const tINT32 y=0, const sgxGUIDynamicImageParams &params=sgxGUIDynamicImageParams(NULL, NULL, 0), const tBOOL registerWithGUI=TRUE);
	virtual sgxWidget *		createGUIButton(const tINT32 x=0, const tINT32 y=0, const sgxGUIButtonParams &params=sgxGUIButtonParams(), const tBOOL registerWithGUI=TRUE);
	virtual sgxWidget *		createGUIImageButton(const tINT32 x=0, const tINT32 y=0, const sgxGUIImageButtonParams &params=sgxGUIImageButtonParams(), const tBOOL registerWithGUI=TRUE);

	virtual tBOOL 			registerWidget(sgxWidget *pWidget);

	virtual sgxWidget *		getWidgetAt(const sgxWidget::tSelectionSet fromSet = sgxWidget::eAll);
	virtual sgxWidget *		getWidgetAt(const sgxPoint2i &position, const sgxWidget::tSelectionSet fromSet);
	virtual sgxWidget *		getWidgetOfUserData(const sgxString &str);
	virtual sgxWidget *		getWidgetOfUserData(const tCHAR c, const tUINT32 idx);
	virtual sgxWidget *		getWidgetOfUserData(const tUINT32 userdata);
	virtual sgxWidget *		getWidgetWithKeyShortcut(const tUINT32 key);
		
	virtual sgxWidget *		getFocusWidget() const;
	virtual sgxWidget *		getActiveWidget() const;

	virtual void 			setPointerImage();	// use default according to skin
	virtual void			setPointerImage(CSGXTexture *pTexture, tUINT32 regionIn=0, tUINT32 regionOut=0, tUINT32 size=32);
	virtual void 			setPointerHotspot(const sgxPoint2i &vHotspot);
	virtual void			showPointer(tBOOL visible=TRUE);
	virtual void			hidePointer();

	virtual sgxWidget *		setRootWidget(sgxWidget *pWidget);
	virtual void			clearFocus();
	virtual void			clearActive();

	virtual void			update(const tREAL32 telaps);
	virtual void			update(sgxWidget *pRoot, const tREAL32 telaps);
	virtual void			draw() const;
	virtual void			draw(const sgxWidget *pFrame) const;
	virtual void			drawScreens() const;
	virtual void			drawScreens(const sgxWidget *pFrame) const;
	virtual void			drawCursor(CSGXDrawSurface *pSurface=NULL) const;

protected:
	virtual void			setFocus(sgxWidget *pWidget);
	virtual void			setActive(sgxWidget *pWidget);


private:
	CSGXGUI();

	CSGXDrawSurface *	m_pCurrentSurface;

	CSGXGUISkin *		m_pDefaultSkin;
	CSGXGUISkin *		m_pCurrentSkin;
	const CSGXFont *	m_pDefaultFont;
	sgxPoint2i			m_vPosition;
	tBOOL				m_PointerIsDown;
	sgxPoint2i			m_vLastPosition;
	tBOOL				m_WasPointerDown;

	tUINT32				m_wasKeyDown[8];	// 256 x bit-wise flags
	tUINT32				m_isKeyDown[8];
		
	tUINT32				m_iPointerImageSize;
	tBOOL				m_bPointerVisible;
	CSGXTexture *		m_pPointerTexture;
	tUINT32				m_iPointerRegionIn;
	tUINT32				m_iPointerRegionOut;
	sgxPoint2i			m_vPointerHotspot;

	sgxWidget *			m_pRootWidget;
	
	sgxWidget *			m_pActive;
	sgxWidget *			m_pFocus;
};


#endif /* SGX_CORE_GUI_GUIMANAGER_HPP */
