Esempio n. 1
0
void CUIButton::Press() {
	ApplyEvent("Press");
	if (m_ListenerObject) m_ListenerObject->Listen(m_ListenerParamObject, m_ListenerParamDWORD);
	if (m_ParentNotify && m_Parent) m_Parent->ApplyEvent(m_Name);

	m_OneTimePress = false;
}
HRESULT CBObject::UpdateSounds()
{
	if(m_SoundEvent){
		if(m_SFX && !m_SFX->IsPlaying()){
			ApplyEvent(m_SoundEvent);
			SetSoundEvent(NULL);
		}
	}

	if(m_SFX) UpdateOneSound(m_SFX);

	return S_OK;
}
HRESULT CBSprite::Display(int X, int Y, CBObject* Register, float ZoomX, float ZoomY, DWORD Alpha, float Rotate, TSpriteBlendMode BlendMode)
{
	if(m_CurrentFrame<0 || m_CurrentFrame>=m_Frames.GetSize()) return S_OK;

	// on change...
	if(m_Changed)
	{
		if(m_Frames[m_CurrentFrame]->m_KillSound)
		{
			KillAllSounds();
		}
		ApplyEvent("FrameChanged");
		m_Frames[m_CurrentFrame]->OneTimeDisplay(m_Owner, Game->m_EditorMode && m_EditorMuted);
	}
	
	// draw frame
	return m_Frames[m_CurrentFrame]->Draw(X - Game->m_OffsetX, Y - Game->m_OffsetY, Register, ZoomX, ZoomY, m_Precise, Alpha, m_EditorAllFrames, Rotate, BlendMode);
}
//////////////////////////////////////////////////////////////////////////
// IWmeObject
//////////////////////////////////////////////////////////////////////////
bool CBScriptHolder::SendEvent(const char* EventName)
{
	return SUCCEEDED(ApplyEvent((char*)EventName));
}
//////////////////////////////////////////////////////////////////////////
// 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 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);