//
// IShockOverlayHandler interface
//
// implemented by script module that wishes to have custom HUD elements, registered with ShockOverlay::SetHandler
//

DECLARE_INTERFACE(IShockOverlayHandler)
{
	// handler called by the engine so script implementation of IShockOverlayHandler can draw HUD elements
	STDMETHOD_(void,DrawHUD)(THIS) PURE;

	// handler called by the engine so script implementation of IShockOverlayHandler can update and draw transparent
	// overlay elements (transparent overlay elements are bitmaps drawn directly to the 3D buffer with alpha blending,
	// like the security icon)
	STDMETHOD_(void,DrawTOverlay)(THIS) PURE;

	// handler called by the engine when dark initializes UI components, this is also called when resuming game after
	// in-game menu and other UI screens and is a good place to update positioning of HUD elements in case display
	// resolution changed
	STDMETHOD_(void,OnUIEnterMode)(THIS) PURE;

	// return FALSE if script wants to prevent a native overlay item, that's about to be enabled, to be enabled(/displayed)
	STDMETHOD_(BOOL,CanEnableElement)(THIS_ int which) PURE;

	//
	// mouse event handlers
	//

	// return TRUE if mouse is over a HUD element that interacts with mouse
	STDMETHOD_(BOOL,IsMouseOver)(THIS_ int x, int y) PURE;

	// return TRUE if mouse click was processed/intercepted
	STDMETHOD_(BOOL,MouseClick)(THIS_ int x, int y) PURE;

	// return TRUE if mouse double-click was processed/intercepted
	STDMETHOD_(BOOL,MouseDblClick)(THIS_ int x, int y) PURE;

	// return TRUE if event was processed/intercepted (cursor_mode is the same as ShockOverlay::GetCursorMode)
	STDMETHOD_(BOOL,MouseDragDrop)(THIS_ int x, int y, BOOL start_drag, int cursor_mode) PURE;

	// return TRUE if key event was processed/intercepted (events are only received if keyboard input capture is enabled)
	STDMETHOD_(BOOL,HandleKey)(THIS_ int ch, int keycode) PURE;
};

//
// ShockOverlay script service
//

DECLARE_SCRIPT_SERVICE(ShockOverlay, 0x22a)
{
	// set the current overlay handler, there can only be one handler set at any given time, NULL clears handler
	STDMETHOD_(void,SetHandler)(IShockOverlayHandler *pHandler) PURE;

	// enable/disable keyboard input capture by overlay handler, use with care because it overrides key bindings
	// and grabs all keyboard input, keep following in mind:
	//     - only enable capture if no native HUD element that captures input is active (like keypad), or remove that first
	//     - when a native HUD element that captures input is about to be displayed the script has to either release
	//       capture or prevent the native element from activating (both done through CanEnableElement)
	//     - when OnUIEnterMode is called any previously enabled capture is lost and has to be re-captured if necessary
	//     - only enable capture for the duration that an overlay item needs keyboard input and is active
	STDMETHOD_(void,SetKeyboardInputCapture)(BOOL bCapture) PURE;

	// get/load a bitmap that can be used for HUD drawing (max 128 bitmaps can be loaded, cleared when db resets)
	// returns a handle that can be used in subsequent bitmap functions or -1 if failed to load
	STDMETHOD_(int,GetBitmap)(const char *name, const char *path /*= "iface\\"*/) PURE;

	// discard a no longer used bitmap handle, only needs to be called when using a lot of bitmaps to stay below 128
	STDMETHOD_(void,FlushBitmap)(int handle) PURE;

	// get size of a loaded bitmap
	STDMETHOD_(void,GetBitmapSize)(int handle, int ref width, int ref height) PURE;

	// set custom font ('index' is 0 to 3 for which custom font slot to use, if a slot already has a font that one
	// will be replaced with the new font)
	STDMETHOD_(BOOL,SetCustomFont)(int index, const char *name, const char *path /*= "fonts\\"*/) PURE;

	// get the rect for an overlay element
	STDMETHOD_(void,GetOverlayRect)(int which, int ref left, int ref top, int ref right, int ref bottom) PURE;

	// get cursor mode (cursor_mode is one of the SCM_* defines below)
	STDMETHOD_(int,GetCursorMode)() PURE;

	// reset cursor mode to normal (also resets any ongoing drag and drop)
	STDMETHOD_(void,ClearCursorMode)() PURE;

	// set cursor bitmap (resets to default if 'name' is NULL or bitmap load failed), returns TRUE if new bitmap was set
	STDMETHOD_(BOOL,SetCursorBitmap)(const char *name, const char *path /*= "iface\\"*/) PURE;

	// set an object the mouse is currently over (for example the mouse is over an inventory item in the inventory)
	STDMETHOD_(void,SetInterfaceMouseOverObject)(const object ref obj) PURE;

	// get the current interface focus object
	STDMETHOD_(void,GetInterfaceFocusObject)(object ref obj) PURE;

	// open "look" overlay (HUD element with info about an object)
	STDMETHOD_(void,OpenLookPopup)(const object ref obj) PURE;

	// toggle "look" cursor mode ("look" cursor mode is with the ? icon)
	STDMETHOD_(void,ToggleLookCursor)() PURE;

	// start drag and drop operation of 'obj', returns TRUE on success
	STDMETHOD_(BOOL,StartObjectDragDrop)(const object ref obj) PURE;

	// play a schema for UI sound
	STDMETHOD_(void,PlaySound)(const char *schema_name) PURE;

	//
	// coordinate mapping (may ONLY be called inside the ShockOverlayHandler::DrawHUD and DrawTOverlay handlers)
	//

	// map a 3D position in the world to a screen coordinate, returns FALSE if off-screen
	STDMETHOD_(BOOL,WorldToScreen)(const vector ref pos, int ref x, int ref y) PURE;

	// get the screen space bounding rect of an object, returns FALSE if entirely off-screen
	STDMETHOD_(BOOL,GetObjectScreenBounds)(const object ref obj, int ref x1, int ref y1, int ref x2, int ref y2) PURE;

	//
	// transparent overlay management
	//

	// create a transparent overlay item (like the security icon), alpha is 0 to 255
	// its contents have to be updated using Begin/EndTOverlayUpdate and draw calls, note that constant updating
	// isn't too optimal, so it should only be done when necessary and avoided on larger items
	// returns an overlay handle, or -1 if failed
	STDMETHOD_(int,CreateTOverlayItem)(int x, int y, int width, int height, int alpha, BOOL trans_bg) PURE;

	// create a transparent overlay item from a bitmap, does not require its contents to be updated
	// (max 64 overlays can be created, cleared when db resets)
	// returns an overlay handle, or -1 if failed (will fail if bm_handle isn't a valid bitmap)
	STDMETHOD_(int,CreateTOverlayItemFromBitmap)(int x, int y, int alpha, int bm_handle, BOOL trans_bg) PURE;

	// destroy a transparent overlay item, not necessary to call but frees resources and overlay slot before a db reset
	STDMETHOD_(void,DestroyTOverlayItem)(int handle) PURE;

	// change alpha of a transparent overlay
	STDMETHOD_(void,UpdateTOverlayAlpha)(int handle, int alpha) PURE;

	// change position of a transparent overlay
	STDMETHOD_(void,UpdateTOverlayPosition)(int handle, int x, int y) PURE;

	// change display size of a transparent overlay (for scaled overlay items)
	STDMETHOD_(void,UpdateTOverlaySize)(int handle, int width, int height) PURE;

	//
	// methods that may ONLY be called inside the ShockOverlayHandler::DrawHUD handler
	// or inside a ShockOverlayHandler::Begin/EndTOverlayUpdate pair
	//

	// draw a loaded bitmap at the given position, bitmap will be drawn unscaled
	STDMETHOD_(void,DrawBitmap)(int handle, int x, int y) PURE;

	// draw a sub-rect of a loaded bitmap at the given position
	// src_x/src_y are relative to the bitmap (0,0 would be the upper left of the bitmap)
	STDMETHOD_(void,DrawSubBitmap)(int handle, int x, int y, int src_x, int src_y, int src_width, int src_height) PURE;

	// draw an object's icon
	STDMETHOD_(void,DrawObjectIcon)(const object ref obj, int x, int y) PURE;

	// set current font, font_type is 0 - default, 1 - mono font, 2 - custom font 0, 3 - custom font 1 etc.
	STDMETHOD_(void,SetFont)(int font_type) PURE;

	// set current text color with explicit RGB values (if R is < 0 then the default text color is set)
	// not all font types support custom colors, FONTT_FLAT8 font type has color native in font and is unaffected
	STDMETHOD_(void,SetTextColor)(int r, int g, int b) PURE;

	// get string extents (with current font)
	STDMETHOD_(void,GetStringSize)(const char *text, int ref width, int ref height) PURE;

	// draw text string (with current font and text color)
	STDMETHOD_(void,DrawString)(const char *text, int x, int y) PURE;

	// draw line (with current text color)
	STDMETHOD_(void,DrawLine)(int x1, int y1, int x2, int y2) PURE;

	//
	// methods that may ONLY be called inside a ShockOverlayHandler::Begin/EndTOverlayUpdate pair
	//

	// fill contents of a transparent overlay item to palette index color (0 is black), 'alpha' sets alpha component of
	// the image data and should not be confused with the alpha specified in the CreateOverlayItem functions,
	// which is applied on top of the alpha in image data (normally it's set to 255)
	STDMETHOD_(void,FillTOverlay)(int color_idx /*= 0*/, int alpha /*= 255*/) PURE;

	//
	// methods that may ONLY be called inside the ShockOverlayHandler::DrawTOverlay handler
	//

	// must be called before updating a transparent overlay item contents with draw calls
	// make sure the return value is TRUE before drawing anything
	STDMETHOD_(BOOL,BeginTOverlayUpdate)(int handle) PURE;

	// end update of a transparent overlay item, may only be called after a successful BeginTOverlayUpdate
	STDMETHOD_(void,EndTOverlayUpdate)() PURE;

	// draw a transparent overlay item, handle is an overlay item handle returned by one of the CreateTOverlayItem functions
	STDMETHOD_(void,DrawTOverlayItem)(int handle) PURE;
};

// overlay elements
#define kOverlayInv        0
#define kOverlayFrame      1  
#define kOverlayText       2
#define kOverlayRep        3
#define kOverlayBook       4
#define kOverlayComm       5
#define kOverlayContainer  6
#define kOverlayHRM        7
#define kOverlayRadar      8
#define kOverlayLetterbox  9
#define kOverlayOverload   10
#define kOverlayPsi        11
#define kOverlayYorN       12
#define kOverlayKeypad     13
#define kOverlayLook       14
#define kOverlayAmmo       15
#define kOverlayMeters     16
#define kOverlayHUD        17
#define kOverlayStats      18
#define kOverlaySkills     19
#define kOverlayBuyTraits  20
#define kOverlaySetting    21    
#define kOverlayCrosshair  22    
#define kOverlayResearch   23
#define kOverlayPDA        24
#define kOverlayEmail      25
#define kOverlayMap        26
#define kOverlayAlarm      27
#define kOverlayPsiIcons   28
#define kOverlayHackIcon   29
#define kOverlayRadiation  30
#define kOverlayPoison     31
#define kOverlayMiniFrame  32
#define kOverlaySecurity   33
#define kOverlayTicker     34
#define kOverlayBuyStats   35
#define kOverlayBuyTech    36
#define kOverlayBuyWeapon  37
#define kOverlayBuyPsi     38
#define kOverlayTechSkill  39
#define kOverlayMFDGame    40
#define kOverlayTlucText   41
#define kOverlaySecurComp  42
#define kOverlayHackComp   43
#define kOverlayHRMPlug    44
#define kOverlayMiniMap    45
#define kOverlayElevator   46
#define kOverlayVersion    47
#define kOverlayTurret     48
#define kOverlayMouseMode  49

// cursor modes
#define SCM_NORMAL   0
#define SCM_DRAGOBJ  1
#define SCM_USEOBJ   2
#define SCM_LOOK     3
#define SCM_PSI      4
#define SCM_SPLIT    5

// keycode flags
#define KB_FLAG_DOWN    (1<<(0+8))
#define KB_FLAG_CTRL    (1<<(1+8))
#define KB_FLAG_ALT     (1<<(2+8))
#define KB_FLAG_SPECIAL (1<<(3+8))
#define KB_FLAG_SHIFT   (1<<(4+8))
#define KB_FLAG_2ND     (1<<(5+8))

// non-ascii and special keycodes
#define KEY_BS          0x08
#define KEY_TAB         0x09
#define KEY_ENTER       0x0D
#define KEY_ESC         0x1B
#define KEY_SPACE       0x20
#define KEY_F1          (0x3B | KB_FLAG_SPECIAL)
#define KEY_F2          (0x3C | KB_FLAG_SPECIAL)
#define KEY_F3          (0x3D | KB_FLAG_SPECIAL)
#define KEY_F4          (0x3E | KB_FLAG_SPECIAL)
#define KEY_F5          (0x3F | KB_FLAG_SPECIAL)
#define KEY_F6          (0x40 | KB_FLAG_SPECIAL)
#define KEY_F7          (0x41 | KB_FLAG_SPECIAL)
#define KEY_F8          (0x42 | KB_FLAG_SPECIAL)
#define KEY_F9          (0x43 | KB_FLAG_SPECIAL)
#define KEY_F10         (0x44 | KB_FLAG_SPECIAL)
#define KEY_F11         (0x57 | KB_FLAG_SPECIAL)
#define KEY_F12         (0x58 | KB_FLAG_SPECIAL)
#define KEY_INS         (0x52 | KB_FLAG_SPECIAL | KB_FLAG_2ND)
#define KEY_DEL         (0x53 | KB_FLAG_SPECIAL | KB_FLAG_2ND)
#define KEY_HOME        (0x47 | KB_FLAG_SPECIAL | KB_FLAG_2ND)
#define KEY_END         (0x4F | KB_FLAG_SPECIAL | KB_FLAG_2ND)
#define KEY_PGUP        (0x49 | KB_FLAG_SPECIAL | KB_FLAG_2ND)
#define KEY_PGDN        (0x51 | KB_FLAG_SPECIAL | KB_FLAG_2ND)
#define KEY_LEFT        (0x4B | KB_FLAG_SPECIAL | KB_FLAG_2ND)
#define KEY_RIGHT       (0x4D | KB_FLAG_SPECIAL | KB_FLAG_2ND)
#define KEY_UP          (0x48 | KB_FLAG_SPECIAL | KB_FLAG_2ND)
#define KEY_DOWN        (0x50 | KB_FLAG_SPECIAL | KB_FLAG_2ND)
#define KEY_GREY_SLASH  ('/' | KB_FLAG_2ND)
#define KEY_GREY_STAR   ('*' | KB_FLAG_2ND)
#define KEY_GREY_PLUS   ('+' | KB_FLAG_2ND)
#define KEY_GREY_MINUS  ('-' | KB_FLAG_2ND)
#define KEY_GREY_ENTER  (KEY_ENTER | KB_FLAG_2ND)
#define KEY_PAD_HOME    (0x47 | KB_FLAG_SPECIAL)
#define KEY_PAD_UP      (0x48 | KB_FLAG_SPECIAL)
#define KEY_PAD_PGUP    (0x49 | KB_FLAG_SPECIAL)
#define KEY_PAD_LEFT    (0x4B | KB_FLAG_SPECIAL)
#define KEY_PAD_CENTER  (0x4C | KB_FLAG_SPECIAL)
#define KEY_PAD_RIGHT   (0x4D | KB_FLAG_SPECIAL)
#define KEY_PAD_END     (0x4F | KB_FLAG_SPECIAL)
#define KEY_PAD_DOWN    (0x50 | KB_FLAG_SPECIAL)
#define KEY_PAD_PGDN    (0x51 | KB_FLAG_SPECIAL)
#define KEY_PAD_INS     (0x52 | KB_FLAG_SPECIAL)
#define KEY_PAD_DEL     (0x53 | KB_FLAG_SPECIAL)
