//////////////////////////////////////////////////////////////////////////
// high level scripting interface
//////////////////////////////////////////////////////////////////////////
HRESULT CBKeyboardState::ScCallMethod(CScScript* Script, CScStack *Stack, CScStack *ThisStack, char *Name)
{
	//////////////////////////////////////////////////////////////////////////
	// IsKeyDown
	//////////////////////////////////////////////////////////////////////////
	if (strcmp(Name, "IsKeyDown")==0)
	{
		Stack->CorrectParams(1);
		CScValue* val = Stack->Pop();
		int vKey;

		if (val->m_Type==VAL_STRING && strlen(val->GetString())>0)
		{
			char* str = val->GetString();
			if(str[0]>='A' && str[0]<='Z') str[0] += ('a' - 'A');
			vKey = (int)str[0];
		}
		else vKey = val->GetInt();

    	const Uint8* state = SDL_GetKeyboardState(NULL);
		SDL_Scancode scanCode = SDL_GetScancodeFromKey(VKeyToKeyCode(vKey));
		bool isDown = state[scanCode] > 0;

		Stack->PushBool(isDown);
		return S_OK;
	}

	else return CBScriptable::ScCallMethod(Script, Stack, ThisStack, Name);
}
char* CSXArray::ScToString()
{
	static char Dummy[32768];
	strcpy(Dummy, "");
	char PropName[20];
	for(int i=0; i<m_Length; i++){
		sprintf(PropName, "%d", i);
		CScValue* val = m_Values->GetProp(PropName);
		if(val){
			if(strlen(Dummy) + strlen(val->GetString()) < 32768){
				strcat(Dummy, val->GetString());
			}
		}

		if(i<m_Length-1 && strlen(Dummy)+1 < 32768) strcat(Dummy, ",");
	}
	return Dummy;
}
Beispiel #3
0
CSXFile::CSXFile(CBGame *inGame, CScStack *Stack): CBScriptable(inGame) {
	Stack->CorrectParams(1);
	CScValue *Val = Stack->Pop();

	m_Filename = NULL;
	if (!Val->IsNULL()) CBUtils::SetString(&m_Filename, Val->GetString());

	m_ReadFile = NULL;
	m_WriteFile = NULL;

	m_Mode = 0;
	m_TextMode = false;
}
Beispiel #4
0
//////////////////////////////////////////////////////////////////////////
// high level scripting interface
//////////////////////////////////////////////////////////////////////////
HRESULT CUIButton::ScCallMethod(CScScript *Script, CScStack *Stack, CScStack *ThisStack, char *Name) {
	//////////////////////////////////////////////////////////////////////////
	// SetDisabledFont
	//////////////////////////////////////////////////////////////////////////
	if (strcmp(Name, "SetDisabledFont") == 0) {
		Stack->CorrectParams(1);
		CScValue *Val = Stack->Pop();

		if (m_FontDisable) Game->m_FontStorage->RemoveFont(m_FontDisable);
		if (Val->IsNULL()) {
			m_FontDisable = NULL;
			Stack->PushBool(true);
		} else {
			m_FontDisable = Game->m_FontStorage->AddFont(Val->GetString());
			Stack->PushBool(m_FontDisable != NULL);
		}
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetHoverFont
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "SetHoverFont") == 0) {
		Stack->CorrectParams(1);
		CScValue *Val = Stack->Pop();

		if (m_FontHover) Game->m_FontStorage->RemoveFont(m_FontHover);
		if (Val->IsNULL()) {
			m_FontHover = NULL;
			Stack->PushBool(true);
		} else {
			m_FontHover = Game->m_FontStorage->AddFont(Val->GetString());
			Stack->PushBool(m_FontHover != NULL);
		}
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetPressedFont
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "SetPressedFont") == 0) {
		Stack->CorrectParams(1);
		CScValue *Val = Stack->Pop();

		if (m_FontPress) Game->m_FontStorage->RemoveFont(m_FontPress);
		if (Val->IsNULL()) {
			m_FontPress = NULL;
			Stack->PushBool(true);
		} else {
			m_FontPress = Game->m_FontStorage->AddFont(Val->GetString());
			Stack->PushBool(m_FontPress != NULL);
		}
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetFocusedFont
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "SetFocusedFont") == 0) {
		Stack->CorrectParams(1);
		CScValue *Val = Stack->Pop();

		if (m_FontFocus) Game->m_FontStorage->RemoveFont(m_FontFocus);
		if (Val->IsNULL()) {
			m_FontFocus = NULL;
			Stack->PushBool(true);
		} else {
			m_FontFocus = Game->m_FontStorage->AddFont(Val->GetString());
			Stack->PushBool(m_FontFocus != NULL);
		}
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetDisabledImage
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "SetDisabledImage") == 0) {
		Stack->CorrectParams(1);

		delete m_ImageDisable;
		m_ImageDisable = new CBSprite(Game);
		char *Filename = Stack->Pop()->GetString();
		if (!m_ImageDisable || FAILED(m_ImageDisable->LoadFile(Filename))) {
			delete m_ImageDisable;
			m_ImageDisable = NULL;
			Stack->PushBool(false);
		} else Stack->PushBool(true);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetDisabledImage
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "GetDisabledImage") == 0) {
		Stack->CorrectParams(0);
		if (!m_ImageDisable || !m_ImageDisable->m_Filename) Stack->PushNULL();
		else Stack->PushString(m_ImageDisable->m_Filename);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetDisabledImageObject
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "GetDisabledImageObject") == 0) {
		Stack->CorrectParams(0);
		if (!m_ImageDisable) Stack->PushNULL();
		else Stack->PushNative(m_ImageDisable, true);

		return S_OK;
	}


	//////////////////////////////////////////////////////////////////////////
	// SetHoverImage
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "SetHoverImage") == 0) {
		Stack->CorrectParams(1);

		delete m_ImageHover;
		m_ImageHover = new CBSprite(Game);
		char *Filename = Stack->Pop()->GetString();
		if (!m_ImageHover || FAILED(m_ImageHover->LoadFile(Filename))) {
			delete m_ImageHover;
			m_ImageHover = NULL;
			Stack->PushBool(false);
		} else Stack->PushBool(true);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetHoverImage
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "GetHoverImage") == 0) {
		Stack->CorrectParams(0);
		if (!m_ImageHover || !m_ImageHover->m_Filename) Stack->PushNULL();
		else Stack->PushString(m_ImageHover->m_Filename);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetHoverImageObject
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "GetHoverImageObject") == 0) {
		Stack->CorrectParams(0);
		if (!m_ImageHover) Stack->PushNULL();
		else Stack->PushNative(m_ImageHover, true);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetPressedImage
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "SetPressedImage") == 0) {
		Stack->CorrectParams(1);

		delete m_ImagePress;
		m_ImagePress = new CBSprite(Game);
		char *Filename = Stack->Pop()->GetString();
		if (!m_ImagePress || FAILED(m_ImagePress->LoadFile(Filename))) {
			delete m_ImagePress;
			m_ImagePress = NULL;
			Stack->PushBool(false);
		} else Stack->PushBool(true);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetPressedImage
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "GetPressedImage") == 0) {
		Stack->CorrectParams(0);
		if (!m_ImagePress || !m_ImagePress->m_Filename) Stack->PushNULL();
		else Stack->PushString(m_ImagePress->m_Filename);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetPressedImageObject
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "GetPressedImageObject") == 0) {
		Stack->CorrectParams(0);
		if (!m_ImagePress) Stack->PushNULL();
		else Stack->PushNative(m_ImagePress, true);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetFocusedImage
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "SetFocusedImage") == 0) {
		Stack->CorrectParams(1);

		delete m_ImageFocus;
		m_ImageFocus = new CBSprite(Game);
		char *Filename = Stack->Pop()->GetString();
		if (!m_ImageFocus || FAILED(m_ImageFocus->LoadFile(Filename))) {
			delete m_ImageFocus;
			m_ImageFocus = NULL;
			Stack->PushBool(false);
		} else Stack->PushBool(true);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetFocusedImage
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "GetFocusedImage") == 0) {
		Stack->CorrectParams(0);
		if (!m_ImageFocus || !m_ImageFocus->m_Filename) Stack->PushNULL();
		else Stack->PushString(m_ImageFocus->m_Filename);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetFocusedImageObject
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "GetFocusedImageObject") == 0) {
		Stack->CorrectParams(0);
		if (!m_ImageFocus) Stack->PushNULL();
		else Stack->PushNative(m_ImageFocus, true);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// Press
	//////////////////////////////////////////////////////////////////////////
	else if (strcmp(Name, "Press") == 0) {
		Stack->CorrectParams(0);

		if (m_Visible && !m_Disable) {
			m_OneTimePress = true;
			m_OneTimePressTime = CBPlatform::GetTime();
		}
		Stack->PushNULL();

		return S_OK;
	}


	else return CUIObject::ScCallMethod(Script, Stack, ThisStack, Name);
}
//////////////////////////////////////////////////////////////////////////
// high level scripting interface
//////////////////////////////////////////////////////////////////////////
HRESULT CBScriptHolder::ScCallMethod(CScScript* Script, CScStack *Stack, CScStack *ThisStack, char *Name)
{
	//////////////////////////////////////////////////////////////////////////
	// DEBUG_CrashMe
	//////////////////////////////////////////////////////////////////////////
	if(strcmp(Name, "DEBUG_CrashMe")==0)
	{
		Stack->CorrectParams(0);
		BYTE* p = 0;
		*p = 10;
		Stack->PushNULL();

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// ApplyEvent
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "ApplyEvent")==0)
	{
		Stack->CorrectParams(1);
		CScValue* val = Stack->Pop();
		HRESULT ret;
		ret = ApplyEvent(val->GetString());

		if(SUCCEEDED(ret)) Stack->PushBool(true);
		else Stack->PushBool(false);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// CanHandleEvent
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "CanHandleEvent")==0)
	{
		Stack->CorrectParams(1);
		Stack->PushBool(CanHandleEvent(Stack->Pop()->GetString()));

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// CanHandleMethod
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "CanHandleMethod")==0)
	{
		Stack->CorrectParams(1);
		Stack->PushBool(CanHandleMethod(Stack->Pop()->GetString()));

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// AttachScript
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "AttachScript")==0)
	{
		Stack->CorrectParams(1);
		Stack->PushBool(SUCCEEDED(AddScript(Stack->Pop()->GetString())));
		
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// DetachScript
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "DetachScript")==0)
	{
		Stack->CorrectParams(2);
		char* Filename = Stack->Pop()->GetString();
		bool KillThreads = Stack->Pop()->GetBool(false);
		bool ret = false;
		for(int i=0; i<m_Scripts.GetSize(); i++)
		{
			if(CBPlatform::stricmp(m_Scripts[i]->m_Filename, Filename)==0)
			{
				m_Scripts[i]->Finish(KillThreads);
				ret = true;
				break;
			}
		}
		Stack->PushBool(ret);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// IsScriptRunning
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "IsScriptRunning")==0)
	{
		Stack->CorrectParams(1);
		char* Filename = Stack->Pop()->GetString();
		bool ret = false;
		for(int i=0; i<m_Scripts.GetSize(); i++)
		{
			if(CBPlatform::stricmp(m_Scripts[i]->m_Filename, Filename)==0 && m_Scripts[i]->m_State!=SCRIPT_FINISHED && m_Scripts[i]->m_State!=SCRIPT_ERROR)
			{
				ret = true;
				break;
			}
		}
		Stack->PushBool(ret);

		return S_OK;
	}
	else return CBScriptable::ScCallMethod(Script, Stack, ThisStack, Name);
}
//////////////////////////////////////////////////////////////////////////
// high level scripting interface
//////////////////////////////////////////////////////////////////////////
HRESULT CAdTalkHolder::ScCallMethod(CScScript* Script, CScStack *Stack, CScStack *ThisStack, char *Name)
{	
	//////////////////////////////////////////////////////////////////////////
	// SetSprite
	//////////////////////////////////////////////////////////////////////////
	if(strcmp(Name, "SetSprite")==0){
		Stack->CorrectParams(1);

		CScValue* Val = Stack->Pop();

		bool SetCurrent = false;
		if(m_CurrentSprite && m_CurrentSprite == m_Sprite) SetCurrent = true;

		SAFE_DELETE(m_Sprite);

		if(Val->IsNULL())
		{
			m_Sprite = NULL;
			if(SetCurrent) m_CurrentSprite = NULL;
			Stack->PushBool(true);
		}
		else
		{
			char* Filename = Val->GetString();		
			CBSprite* spr = new CBSprite(Game, this);
			if(!spr || FAILED(spr->LoadFile(Filename))){				
				Script->RuntimeError("SetSprite method failed for file '%s'", Filename);
				Stack->PushBool(false);
			}
			else {
				m_Sprite = spr;
				if(SetCurrent) m_CurrentSprite = m_Sprite;
				Stack->PushBool(true);
			}
		}
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetSprite
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "GetSprite")==0){
		Stack->CorrectParams(0);

		if(!m_Sprite || !m_Sprite->m_Filename) Stack->PushNULL();
		else Stack->PushString(m_Sprite->m_Filename);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetSpriteObject
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "GetSpriteObject")==0){
		Stack->CorrectParams(0);

		if(!m_Sprite) Stack->PushNULL();
		else Stack->PushNative(m_Sprite, true);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// AddTalkSprite
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "AddTalkSprite")==0){
		Stack->CorrectParams(2);

		char* Filename = Stack->Pop()->GetString();
		bool Ex = Stack->Pop()->GetBool();

		CBSprite* spr = new CBSprite(Game, this);
		if(!spr || FAILED(spr->LoadFile(Filename))){
			Stack->PushBool(false);
			Script->RuntimeError("AddTalkSprite method failed for file '%s'", Filename);
		}
		else {
			if(Ex) m_TalkSpritesEx.Add(spr);
			else m_TalkSprites.Add(spr);
			Stack->PushBool(true);			
		}
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// RemoveTalkSprite
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "RemoveTalkSprite")==0){
		Stack->CorrectParams(2);

		char* Filename = Stack->Pop()->GetString();
		bool Ex = Stack->Pop()->GetBool();
		int i;

		bool SetCurrent = false;
		bool SetTemp2 = false;

		if(Ex){
			for(i=0; i<m_TalkSpritesEx.GetSize(); i++){
				if(stricmp(m_TalkSpritesEx[i]->m_Filename, Filename)==0){
					if(m_CurrentSprite==m_TalkSpritesEx[i]) SetCurrent = true;
					if(m_TempSprite2==m_TalkSpritesEx[i]) SetTemp2 = true;
					delete m_TalkSpritesEx[i];
					m_TalkSpritesEx.RemoveAt(i);
					break;
				}
			}
		}
		else{
			for(i=0; i<m_TalkSprites.GetSize(); i++){
				if(stricmp(m_TalkSprites[i]->m_Filename, Filename)==0){
					if(m_CurrentSprite==m_TalkSprites[i]) SetCurrent = true;
					if(m_TempSprite2==m_TalkSprites[i]) SetTemp2 = true;
					delete m_TalkSprites[i];
					m_TalkSprites.RemoveAt(i);
					break;
				}
			}

		}

		Stack->PushBool(true);
		if(SetCurrent) m_CurrentSprite = m_Sprite;
		if(SetTemp2) m_TempSprite2 = m_Sprite;

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetTalkSprite
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "SetTalkSprite")==0){
		Stack->CorrectParams(2);

		char* Filename = Stack->Pop()->GetString();
		bool Ex = Stack->Pop()->GetBool();
		bool SetCurrent = false;
		bool SetTemp2 = false;

		CBSprite* spr = new CBSprite(Game, this);
		if(!spr || FAILED(spr->LoadFile(Filename))){
			Stack->PushBool(false);
			Script->RuntimeError("SetTalkSprite method failed for file '%s'", Filename);
		}
		else {

			// delete current
			int i;
			if(Ex){
				for(i=0; i<m_TalkSpritesEx.GetSize(); i++){
					if(m_TalkSpritesEx[i]==m_CurrentSprite) SetCurrent = true;
					if(m_TalkSpritesEx[i]==m_TempSprite2) SetTemp2 = true;
					delete m_TalkSpritesEx[i];
				}
				m_TalkSpritesEx.RemoveAll();
			}
			else{
				for(i=0; i<m_TalkSprites.GetSize(); i++){
					if(m_TalkSprites[i]==m_CurrentSprite) SetCurrent = true;
					if(m_TalkSprites[i]==m_TempSprite2) SetTemp2 = true;
					delete m_TalkSprites[i];
				}
				m_TalkSprites.RemoveAll();
			}

			// set new
			if(Ex) m_TalkSpritesEx.Add(spr);
			else m_TalkSprites.Add(spr);
			Stack->PushBool(true);

			if(SetCurrent) m_CurrentSprite = spr;
			if(SetTemp2) m_TempSprite2 = spr;
		}
		return S_OK;
	}

	else return CAdObject::ScCallMethod(Script, Stack, ThisStack, Name);
}
//////////////////////////////////////////////////////////////////////////
// high level scripting interface
//////////////////////////////////////////////////////////////////////////
HRESULT CBObject::ScCallMethod(CScScript* Script, CScStack *Stack, CScStack *ThisStack, char *Name)
{

	//////////////////////////////////////////////////////////////////////////
	// SkipTo
	//////////////////////////////////////////////////////////////////////////
	if(strcmp(Name, "SkipTo")==0){
		Stack->CorrectParams(2);
		m_PosX = Stack->Pop()->GetInt();
		m_PosY = Stack->Pop()->GetInt();
		AfterMove();
		Stack->PushNULL();

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// Caption
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "Caption")==0){
		Stack->CorrectParams(1);
		Stack->PushString(GetCaption(Stack->Pop()->GetInt()));

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetCursor
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "SetCursor")==0){
		Stack->CorrectParams(1);
		if(SUCCEEDED(SetCursor(Stack->Pop()->GetString()))) Stack->PushBool(true);
		else Stack->PushBool(false);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// RemoveCursor
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "RemoveCursor")==0){
		Stack->CorrectParams(0);
		if(!m_SharedCursors) SAFE_DELETE(m_Cursor);
		else m_Cursor = NULL;
		Stack->PushNULL();

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetCursor
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "GetCursor")==0){
		Stack->CorrectParams(0);
		if(!m_Cursor || !m_Cursor->m_Filename) Stack->PushNULL();
		else Stack->PushString(m_Cursor->m_Filename);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetCursorObject
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "GetCursorObject")==0){
		Stack->CorrectParams(0);
		if(!m_Cursor) Stack->PushNULL();
		else Stack->PushNative(m_Cursor, true);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// HasCursor
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "HasCursor")==0){
		Stack->CorrectParams(0);

		if(m_Cursor) Stack->PushBool(true);
		else Stack->PushBool(false);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetCaption
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "SetCaption")==0){
		Stack->CorrectParams(2);
		SetCaption(Stack->Pop()->GetString(), Stack->Pop()->GetInt());
		Stack->PushNULL();

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// LoadSound
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "LoadSound")==0){
		Stack->CorrectParams(1);
		char* Filename = Stack->Pop()->GetString();
		if(SUCCEEDED(PlaySFX(Filename, false, false)))
			Stack->PushBool(true);
		else
			Stack->PushBool(false);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// PlaySound
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "PlaySound")==0){
		Stack->CorrectParams(3);

		char* Filename;
		bool Looping;
		DWORD LoopStart;
		
		CScValue* val1 = Stack->Pop();
		CScValue* val2 = Stack->Pop();
		CScValue* val3 = Stack->Pop();

		if(val1->m_Type==VAL_BOOL){
			Filename=NULL;
			Looping = val1->GetBool();
			LoopStart = val2->GetInt();
		}
		else{
			if(val1->IsNULL()) Filename = NULL;
			else Filename = val1->GetString();
			Looping = val2->IsNULL()?false:val2->GetBool();
			LoopStart = val3->GetInt();
		}

		if(FAILED(PlaySFX(Filename, Looping, true, NULL, LoopStart))) Stack->PushBool(false);
		else Stack->PushBool(true);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// PlaySoundEvent
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "PlaySoundEvent")==0){
		Stack->CorrectParams(2);

		char* Filename;
		char* EventName;
		
		CScValue* val1 = Stack->Pop();
		CScValue* val2 = Stack->Pop();

		if(val2->IsNULL()){
			Filename=NULL;
			EventName = val1->GetString();
		}
		else{
			Filename = val1->GetString();
			EventName = val2->GetString();
		}

		if(FAILED(PlaySFX(Filename, false, true, EventName))) Stack->PushBool(false);
		else Stack->PushBool(true);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// StopSound
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "StopSound")==0){
		Stack->CorrectParams(0);
		
		if(FAILED(StopSFX())) Stack->PushBool(false);
		else Stack->PushBool(true);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// PauseSound
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "PauseSound")==0){
		Stack->CorrectParams(0);
		
		if(FAILED(PauseSFX())) Stack->PushBool(false);
		else Stack->PushBool(true);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// ResumeSound
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "ResumeSound")==0){
		Stack->CorrectParams(0);
		
		if(FAILED(ResumeSFX())) Stack->PushBool(false);
		else Stack->PushBool(true);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// IsSoundPlaying
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "IsSoundPlaying")==0){
		Stack->CorrectParams(0);
		
		if(m_SFX && m_SFX->IsPlaying()) Stack->PushBool(true);
		else Stack->PushBool(false);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetSoundPosition
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "SetSoundPosition")==0){
		Stack->CorrectParams(1);
		
		DWORD Time = Stack->Pop()->GetInt();
		if(FAILED(SetSFXTime(Time))) Stack->PushBool(false);
		else Stack->PushBool(true);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetSoundPosition
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "GetSoundPosition")==0){
		Stack->CorrectParams(0);

		if(!m_SFX) Stack->PushInt(0);
		else Stack->PushInt(m_SFX->GetPositionTime());
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetSoundVolume
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "SetSoundVolume")==0){
		Stack->CorrectParams(1);
		
		int Volume = Stack->Pop()->GetInt();
		if(FAILED(SetSFXVolume(Volume))) Stack->PushBool(false);
		else Stack->PushBool(true);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetSoundVolume
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "GetSoundVolume")==0){
		Stack->CorrectParams(0);

		if(!m_SFX) Stack->PushInt(m_SFXVolume);
		else Stack->PushInt(m_SFX->GetVolume());
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetShadowImage
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "SetShadowImage")==0){
		Stack->CorrectParams(1);
		CScValue* Val = Stack->Pop();
		if(m_ShadowImage) Game->m_SurfaceStorage->RemoveSurface(m_ShadowImage);
		m_ShadowImage = NULL;

		if(Val->IsString())
		{
			m_ShadowImage = Game->m_SurfaceStorage->AddSurface(Val->GetString());
			Stack->PushBool(m_ShadowImage!=NULL);
		}
		else Stack->PushBool(true);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetShadowImage
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "GetShadowImage")==0){
		Stack->CorrectParams(0);
		if(m_ShadowImage && m_ShadowImage->m_Filename) Stack->PushString(m_ShadowImage->m_Filename);
		else Stack->PushNULL();

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetLightPosition
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "SetLightPosition")==0){
		Stack->CorrectParams(3);
		
		double x = Stack->Pop()->GetFloat();
		double y = Stack->Pop()->GetFloat();
		double z = Stack->Pop()->GetFloat();
		m_ShadowLightPos = D3DXVECTOR3(x, y, z);
		Stack->PushNULL();

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SoundFXNone
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "SoundFXNone")==0)
	{
		Stack->CorrectParams(0);
		m_SFXType = SFX_NONE;
		m_SFXParam1 = 0;
		m_SFXParam2 = 0;
		m_SFXParam3 = 0;
		m_SFXParam4 = 0;
		Stack->PushNULL();

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SoundFXEcho
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "SoundFXEcho")==0)
	{
		Stack->CorrectParams(4);
		m_SFXType = SFX_ECHO;
		m_SFXParam1 = (float)Stack->Pop()->GetFloat(0); // Wet/Dry Mix [%] (0-100)
		m_SFXParam2 = (float)Stack->Pop()->GetFloat(0); // Feedback [%] (0-100)
		m_SFXParam3 = (float)Stack->Pop()->GetFloat(333.0f); // Left Delay [ms] (1-2000)
		m_SFXParam4 = (float)Stack->Pop()->GetFloat(333.0f); // Right Delay [ms] (1-2000)
		Stack->PushNULL();

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SoundFXReverb
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "SoundFXReverb")==0)
	{
		Stack->CorrectParams(4);
		m_SFXType = SFX_REVERB;
		m_SFXParam1 = (float)Stack->Pop()->GetFloat(0); // In Gain [dB] (-96 - 0)
		m_SFXParam2 = (float)Stack->Pop()->GetFloat(0); // Reverb Mix [dB] (-96 - 0)
		m_SFXParam3 = (float)Stack->Pop()->GetFloat(1000.0f); // Reverb Time [ms] (0.001 - 3000)
		m_SFXParam4 = (float)Stack->Pop()->GetFloat(0.001f); // HighFreq RT Ratio (0.001 - 0.999)
		Stack->PushNULL();

		return S_OK;
	}

	else return CBScriptHolder::ScCallMethod(Script, Stack, ThisStack, Name);
}
//////////////////////////////////////////////////////////////////////////
// high level scripting interface
//////////////////////////////////////////////////////////////////////////
HRESULT CBSprite::ScCallMethod(CScScript* Script, CScStack *Stack, CScStack *ThisStack, char *Name)
{
	//////////////////////////////////////////////////////////////////////////
	// GetFrame
	//////////////////////////////////////////////////////////////////////////
	if(strcmp(Name, "GetFrame")==0)
	{
		Stack->CorrectParams(1);
		int Index = Stack->Pop()->GetInt(-1);
		if(Index<0 || Index>=m_Frames.GetSize())
		{
			Script->RuntimeError("Sprite.GetFrame: Frame index %d is out of range.", Index);
			Stack->PushNULL();
		}
		else Stack->PushNative(m_Frames[Index], true);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// DeleteFrame
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "DeleteFrame")==0)
	{
		Stack->CorrectParams(1);
		CScValue* Val = Stack->Pop();
		if(Val->IsInt())
		{
			int Index = Val->GetInt(-1);
			if(Index<0 || Index>=m_Frames.GetSize())
			{
				Script->RuntimeError("Sprite.DeleteFrame: Frame index %d is out of range.", Index);
			}			
		}
		else
		{
			CBFrame* Frame = (CBFrame*)Val->GetNative();
			for(int i=0; i<m_Frames.GetSize(); i++)
			{
				if(m_Frames[i]==Frame)
				{
					if(i==m_CurrentFrame) m_LastFrameTime = 0;
					delete m_Frames[i];
					m_Frames.RemoveAt(i);
					break;
				}
			}
		}
		Stack->PushNULL();
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// Reset
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "Reset")==0)
	{
		Stack->CorrectParams(0);
		Reset();
		Stack->PushNULL();
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// AddFrame
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "AddFrame")==0)
	{
		Stack->CorrectParams(1);
		CScValue* Val = Stack->Pop();
		char* Filename = NULL;
		if(!Val->IsNULL()) Filename = Val->GetString();

		CBFrame* Frame = new CBFrame(Game);		
		if(Filename!=NULL)
		{
			CBSubFrame* Sub = new CBSubFrame(Game);			
			if(SUCCEEDED(Sub->SetSurface(Filename)))
			{
				Sub->SetDefaultRect();
				Frame->m_Subframes.Add(Sub);
			}
			else delete Sub;
		}
		m_Frames.Add(Frame);

		Stack->PushNative(Frame, true);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// InsertFrame
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "InsertFrame")==0)
	{
		Stack->CorrectParams(2);
		int Index = Stack->Pop()->GetInt();
		if(Index<0) Index = 0;

		CScValue* Val = Stack->Pop();
		char* Filename = NULL;
		if(!Val->IsNULL()) Filename = Val->GetString();

		CBFrame* Frame = new CBFrame(Game);		
		if(Filename!=NULL)
		{
			CBSubFrame* Sub = new CBSubFrame(Game);			
			if(SUCCEEDED(Sub->SetSurface(Filename))) Frame->m_Subframes.Add(Sub);
			else delete Sub;
		}

		if(Index>=m_Frames.GetSize()) m_Frames.Add(Frame);
		else m_Frames.InsertAt(Index, Frame);

		Stack->PushNative(Frame, true);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// Pause
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "Pause")==0)
	{
		Stack->CorrectParams(0);
		m_Paused = true;
		Stack->PushNULL();
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// Play
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "Play")==0)
	{
		Stack->CorrectParams(0);
		m_Paused = false;
		Stack->PushNULL();
		return S_OK;
	}

	else return CBScriptHolder::ScCallMethod(Script, Stack, ThisStack, Name);
}
//////////////////////////////////////////////////////////////////////////
// high level scripting interface
//////////////////////////////////////////////////////////////////////////
HRESULT CUIWindow::ScCallMethod(CScScript* Script, CScStack *Stack, CScStack *ThisStack, char *Name)
{
    //////////////////////////////////////////////////////////////////////////
    // GetWidget / GetControl
    //////////////////////////////////////////////////////////////////////////
    if(strcmp(Name, "GetWidget")==0 || strcmp(Name, "GetControl")==0)
    {
        Stack->CorrectParams(1);
        CScValue* val = Stack->Pop();
        if(val->GetType()==VAL_INT)
        {
            int widget= val->GetInt();
            if(widget<0 || widget>=m_Widgets.GetSize()) Stack->PushNULL();
            else Stack->PushNative(m_Widgets[widget], true);
        }
        else
        {
            for(int i=0; i<m_Widgets.GetSize(); i++)
            {
                if(CBPlatform::stricmp(m_Widgets[i]->m_Name, val->GetString())==0)
                {
                    Stack->PushNative(m_Widgets[i], true);
                    return S_OK;
                }
            }
            Stack->PushNULL();
        }

        return S_OK;
    }

    //////////////////////////////////////////////////////////////////////////
    // SetInactiveFont
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "SetInactiveFont")==0)
    {
        Stack->CorrectParams(1);

        if(m_FontInactive) Game->m_FontStorage->RemoveFont(m_FontInactive);
        m_FontInactive = Game->m_FontStorage->AddFont(Stack->Pop()->GetString());
        Stack->PushBool(m_FontInactive!=NULL);

        return S_OK;
    }

    //////////////////////////////////////////////////////////////////////////
    // SetInactiveImage
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "SetInactiveImage")==0)
    {
        Stack->CorrectParams(1);

        SAFE_DELETE(m_ImageInactive);
        m_ImageInactive = new CBSprite(Game);
        char* Filename = Stack->Pop()->GetString();
        if(!m_ImageInactive || FAILED(m_ImageInactive->LoadFile(Filename)))
        {
            SAFE_DELETE(m_ImageInactive);
            Stack->PushBool(false);
        }
        else Stack->PushBool(true);

        return S_OK;
    }

    //////////////////////////////////////////////////////////////////////////
    // GetInactiveImage
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "GetInactiveImage")==0)
    {
        Stack->CorrectParams(0);
        if(!m_ImageInactive || !m_ImageInactive->m_Filename) Stack->PushNULL();
        else Stack->PushString(m_ImageInactive->m_Filename);

        return S_OK;
    }

    //////////////////////////////////////////////////////////////////////////
    // GetInactiveImageObject
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "GetInactiveImageObject")==0)
    {
        Stack->CorrectParams(0);
        if(!m_ImageInactive) Stack->PushNULL();
        else Stack->PushNative(m_ImageInactive, true);

        return S_OK;
    }


    //////////////////////////////////////////////////////////////////////////
    // Close
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "Close")==0)
    {
        Stack->CorrectParams(0);
        Stack->PushBool(SUCCEEDED(Close()));
        return S_OK;
    }

    //////////////////////////////////////////////////////////////////////////
    // GoExclusive
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "GoExclusive")==0)
    {
        Stack->CorrectParams(0);
        GoExclusive();
        Script->WaitFor(this);
        Stack->PushNULL();
        return S_OK;
    }

    //////////////////////////////////////////////////////////////////////////
    // GoSystemExclusive
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "GoSystemExclusive")==0)
    {
        Stack->CorrectParams(1);
        char* Event = Stack->Pop()->GetString();
        GoSystemExclusive();

        if (Event != NULL)
        {
            ApplyEvent(Event);
        }
        Script->WaitFor(this);
        Stack->PushNULL();
        return S_OK;
    }

    //////////////////////////////////////////////////////////////////////////
    // Center
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "Center")==0)
    {
        Stack->CorrectParams(0);
        m_PosX = (Game->m_Renderer->m_Width - m_Width) / 2;
        m_PosY = (Game->m_Renderer->m_Height - m_Height) / 2;
        Stack->PushNULL();
        return S_OK;
    }

    //////////////////////////////////////////////////////////////////////////
    // LoadFromFile
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "LoadFromFile")==0)
    {
        Stack->CorrectParams(1);

        CScValue* Val = Stack->Pop();
        Cleanup();
        if(!Val->IsNULL())
        {
            Stack->PushBool(SUCCEEDED(LoadFile(Val->GetString())));
        }
        else Stack->PushBool(true);

        return S_OK;
    }

    //////////////////////////////////////////////////////////////////////////
    // CreateButton
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "CreateButton")==0)
    {
        Stack->CorrectParams(1);
        CScValue* Val = Stack->Pop();

        CUIButton* Btn = new CUIButton(Game);
        if(!Val->IsNULL()) Btn->SetName(Val->GetString());
        Stack->PushNative(Btn, true);

        Btn->m_Parent = this;
        m_Widgets.Add(Btn);

        return S_OK;
    }

    //////////////////////////////////////////////////////////////////////////
    // CreateStatic
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "CreateStatic")==0)
    {
        Stack->CorrectParams(1);
        CScValue* Val = Stack->Pop();

        CUIText* Sta = new CUIText(Game);
        if(!Val->IsNULL()) Sta->SetName(Val->GetString());
        Stack->PushNative(Sta, true);

        Sta->m_Parent = this;
        m_Widgets.Add(Sta);

        return S_OK;
    }

    //////////////////////////////////////////////////////////////////////////
    // CreateEditor
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "CreateEditor")==0)
    {
        Stack->CorrectParams(1);
        CScValue* Val = Stack->Pop();

        CUIEdit* Edi = new CUIEdit(Game);
        if(!Val->IsNULL()) Edi->SetName(Val->GetString());
        Stack->PushNative(Edi, true);

        Edi->m_Parent = this;
        m_Widgets.Add(Edi);

        return S_OK;
    }

    //////////////////////////////////////////////////////////////////////////
    // CreateWindow
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "CreateWindow")==0)
    {
        Stack->CorrectParams(1);
        CScValue* Val = Stack->Pop();

        CUIWindow* Win = new CUIWindow(Game);
        if(!Val->IsNULL()) Win->SetName(Val->GetString());
        Stack->PushNative(Win, true);

        Win->m_Parent = this;
        m_Widgets.Add(Win);

        return S_OK;
    }

    //////////////////////////////////////////////////////////////////////////
    // DeleteControl / DeleteButton / DeleteStatic / DeleteEditor / DeleteWindow
    //////////////////////////////////////////////////////////////////////////
    else if(strcmp(Name, "DeleteControl")==0 || strcmp(Name, "DeleteButton")==0 || strcmp(Name, "DeleteStatic")==0 || strcmp(Name, "DeleteEditor")==0 || strcmp(Name, "DeleteWindow")==0)
    {
        Stack->CorrectParams(1);
        CScValue* val = Stack->Pop();
        CUIObject* obj = (CUIObject*)val->GetNative();

        for(int i=0; i<m_Widgets.GetSize(); i++)
        {
            if(m_Widgets[i]==obj)
            {
                delete m_Widgets[i];
                m_Widgets.RemoveAt(i);
                if(val->GetType()==VAL_VARIABLE_REF) val->SetNULL();
            }
        }
        Stack->PushNULL();
        return S_OK;
    }
    else if SUCCEEDED(Game->WindowScriptMethodHook(this, Script, Stack, Name)) return S_OK;

    else return CUIObject::ScCallMethod(Script, Stack, ThisStack, Name);
//////////////////////////////////////////////////////////////////////////
// high level scripting interface
//////////////////////////////////////////////////////////////////////////
HRESULT CUIObject::ScCallMethod(CScScript* Script, CScStack *Stack, CScStack *ThisStack, char *Name)
{
	//////////////////////////////////////////////////////////////////////////
	// SetFont
	//////////////////////////////////////////////////////////////////////////
	if(strcmp(Name, "SetFont")==0){
		Stack->CorrectParams(1);
		CScValue* Val = Stack->Pop();

		if(m_Font) Game->m_FontStorage->RemoveFont(m_Font);
		if(Val->IsNULL())
		{
			m_Font = NULL;
			Stack->PushBool(true);
		}
		else
		{
			m_Font = Game->m_FontStorage->AddFont(Val->GetString());
			Stack->PushBool(m_Font!=NULL);
		}
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// SetImage
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "SetImage")==0){
		Stack->CorrectParams(1);
		CScValue* Val = Stack->Pop();

		SAFE_DELETE(m_Image);		
		if(Val->IsNULL()){
			Stack->PushBool(true);
			return S_OK;
		}
		
		m_Image = new CBSprite(Game);
		if(!m_Image || FAILED(m_Image->LoadFile(Val->GetString()))){
			SAFE_DELETE(m_Image);
			Stack->PushBool(false);			
		}
		else Stack->PushBool(true);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetImage
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "GetImage")==0){
		Stack->CorrectParams(0);
		if(!m_Image || !m_Image->m_Filename) Stack->PushNULL();
		else Stack->PushString(m_Image->m_Filename);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// GetImageObject
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "GetImageObject")==0){
		Stack->CorrectParams(0);
		if(!m_Image) Stack->PushNULL();
		else Stack->PushNative(m_Image, true);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// Focus
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "Focus")==0){
		Stack->CorrectParams(0);
		Focus();
		Stack->PushNULL();
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// MoveAfter / MoveBefore
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "MoveAfter")==0 || strcmp(Name, "MoveBefore")==0){
		Stack->CorrectParams(1);

		if(m_Parent && m_Parent->m_Type == UI_WINDOW){
			CUIWindow* win = (CUIWindow*)m_Parent;

			int i;
			bool found = false;			
			CScValue* val = Stack->Pop();
			// find directly
			if(val->IsNative()){
				CUIObject* widget = (CUIObject*)val->GetNative();
				for(i=0; i<win->m_Widgets.GetSize(); i++){
					if(win->m_Widgets[i]==widget){
						found = true;
						break;
					}
				}
			}
			// find by name
			else{
				char* name = val->GetString();
				for(i=0; i<win->m_Widgets.GetSize(); i++){
					if(CBPlatform::stricmp(win->m_Widgets[i]->m_Name, name)==0){
						found = true;
						break;
					}
				}
			}

			if(found){
				bool done=false;
				for(int j=0; j<win->m_Widgets.GetSize(); j++){
					if(win->m_Widgets[j]==this){
						if(strcmp(Name, "MoveAfter")==0) i++;
						if(j>=i) j++;

						win->m_Widgets.InsertAt(i, this);
						win->m_Widgets.RemoveAt(j);						

						done = true;
						Stack->PushBool(true);
						break;
					}
				}
				if(!done) Stack->PushBool(false);
			}
			else Stack->PushBool(false);

		}
		else Stack->PushBool(false);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// MoveToBottom
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "MoveToBottom")==0){
		Stack->CorrectParams(0);

		if(m_Parent && m_Parent->m_Type == UI_WINDOW){
			CUIWindow* win = (CUIWindow*)m_Parent;
			for(int i=0; i<win->m_Widgets.GetSize(); i++){
				if(win->m_Widgets[i]==this){
					win->m_Widgets.RemoveAt(i);
					win->m_Widgets.InsertAt(0, this);
					break;
				}
			}
			Stack->PushBool(true);
		}
		else Stack->PushBool(false);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// MoveToTop
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "MoveToTop")==0){
		Stack->CorrectParams(0);

		if(m_Parent && m_Parent->m_Type == UI_WINDOW){
			CUIWindow* win = (CUIWindow*)m_Parent;
			for(int i=0; i<win->m_Widgets.GetSize(); i++){
				if(win->m_Widgets[i]==this){
					win->m_Widgets.RemoveAt(i);
					win->m_Widgets.Add(this);
					break;
				}
			}
			Stack->PushBool(true);
		}
		else Stack->PushBool(false);

		return S_OK;
	}

	else return CBObject::ScCallMethod(Script, Stack, ThisStack, Name);
}
//////////////////////////////////////////////////////////////////////////
// high level scripting interface
//////////////////////////////////////////////////////////////////////////
HRESULT CAdLayer::ScCallMethod(CScScript* Script, CScStack *Stack, CScStack *ThisStack, char *Name)
{
	//////////////////////////////////////////////////////////////////////////
	// GetNode
	//////////////////////////////////////////////////////////////////////////
	if(strcmp(Name, "GetNode")==0)
	{
		Stack->CorrectParams(1);
		CScValue* val = Stack->Pop();
		int node = -1;
		
		if(val->m_Type==VAL_INT) node = val->GetInt();
		else{ // get by name
			for(int i=0; i<m_Nodes.GetSize(); i++){
				if( (m_Nodes[i]->m_Type==OBJECT_ENTITY && CBPlatform::stricmp(m_Nodes[i]->m_Entity->m_Name, val->GetString())==0) ||
					(m_Nodes[i]->m_Type==OBJECT_REGION && CBPlatform::stricmp(m_Nodes[i]->m_Region->m_Name, val->GetString())==0)){
					node = i;
					break;
				}					
			}
		}

		if(node<0 || node>= m_Nodes.GetSize()) Stack->PushNULL();
		else{
			switch(m_Nodes[node]->m_Type){
				case OBJECT_ENTITY:
					Stack->PushNative(m_Nodes[node]->m_Entity, true);
					break;
				case OBJECT_REGION:
					Stack->PushNative(m_Nodes[node]->m_Region, true);
					break;
				default:
					Stack->PushNULL();
			}
		}
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// AddRegion / AddEntity
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "AddRegion")==0 || strcmp(Name, "AddEntity")==0)
	{
		Stack->CorrectParams(1);
		CScValue* Val = Stack->Pop();

		CAdSceneNode* Node = new CAdSceneNode(Game);
		if(strcmp(Name, "AddRegion")==0)
		{
			CAdRegion* Region = new CAdRegion(Game);
			if(!Val->IsNULL()) Region->SetName(Val->GetString());
			Node->SetRegion(Region);
			Stack->PushNative(Region, true);
		}
		else
		{
			CAdEntity* Entity = new CAdEntity(Game);
			if(!Val->IsNULL()) Entity->SetName(Val->GetString());
			Node->SetEntity(Entity);
			Stack->PushNative(Entity, true);
		}
		m_Nodes.Add(Node);
		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// InsertRegion / InsertEntity
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "InsertRegion")==0 || strcmp(Name, "InsertEntity")==0)
	{
		Stack->CorrectParams(2);
		int Index = Stack->Pop()->GetInt();
		CScValue* Val = Stack->Pop();

		CAdSceneNode* Node = new CAdSceneNode(Game);
		if(strcmp(Name, "InsertRegion")==0)
		{
			CAdRegion* Region = new CAdRegion(Game);
			if(!Val->IsNULL()) Region->SetName(Val->GetString());
			Node->SetRegion(Region);
			Stack->PushNative(Region, true);
		}
		else
		{
			CAdEntity* Entity = new CAdEntity(Game);
			if(!Val->IsNULL()) Entity->SetName(Val->GetString());
			Node->SetEntity(Entity);
			Stack->PushNative(Entity, true);
		}
		if(Index < 0) Index = 0;
		if(Index <= m_Nodes.GetSize() - 1) m_Nodes.InsertAt(Index, Node);
		else m_Nodes.Add(Node);

		return S_OK;
	}

	//////////////////////////////////////////////////////////////////////////
	// DeleteNode
	//////////////////////////////////////////////////////////////////////////
	else if(strcmp(Name, "DeleteNode")==0)
	{
		Stack->CorrectParams(1);
		CScValue* Val = Stack->Pop();

		CAdSceneNode* ToDelete = NULL;
		if(Val->IsNative())
		{
			CBScriptable* Temp = Val->GetNative();
			for(int i=0; i<m_Nodes.GetSize(); i++)
			{
				if(m_Nodes[i]->m_Region==Temp || m_Nodes[i]->m_Entity==Temp)
				{
					ToDelete = m_Nodes[i];
					break;
				}
			}
		}
		else
		{
			int Index = Val->GetInt();
			if(Index >= 0 && Index < m_Nodes.GetSize())
			{
				ToDelete = m_Nodes[Index];
			}
		}
		if(ToDelete==NULL)
		{
			Stack->PushBool(false);
			return S_OK;
		}

		for(int i=0; i<m_Nodes.GetSize(); i++)
		{
			if(m_Nodes[i]==ToDelete)
			{				
				SAFE_DELETE(m_Nodes[i]);
				m_Nodes.RemoveAt(i);
				break;
			}
		}
		Stack->PushBool(true);
		return S_OK;
	}

	else return CBObject::ScCallMethod(Script, Stack, ThisStack, Name);
}