void AUISurfaceActor::HandleBackEvent()
{
	Coherent::UI::View* UIView = CoherentUIComponent->GetView();
	if (UIView) {
		UIView->ExecuteScript("history.back();");
	}
}
void AUISurfaceActor::HandleMouseoverEventPixelCoordinates(FVector PixelCoordinates)
{
	Coherent::UI::View* UIView = CoherentUIComponent->GetView();
	if (UIView) {
		// Generate view mouse events from user input
		Coherent::UI::MouseEventData* UIMouseEvent = new Coherent::UI::MouseEventData();
		UIMouseEvent->X = PixelCoordinates.X;
		UIMouseEvent->Y = PixelCoordinates.Y;
		UIMouseEvent->Type = Coherent::UI::MouseEventData::EventType::MouseMove;
		UIView->MouseEvent(*UIMouseEvent);
	}
}
    virtual void ProcessEvent( EFlowEvent evt, SActivationInfo* pActInfo )
    {
        switch ( evt )
        {
        case eFE_Suspend:
            pActInfo->pGraph->SetRegularlyUpdated( pActInfo->myID, false );
            break;

        case eFE_Resume:
            pActInfo->pGraph->SetRegularlyUpdated( pActInfo->myID, true );
            break;

        case eFE_Initialize:
            break;

        case eFE_Activate:
        {
            if ( IsPortActive( pActInfo, EIP_ACTIVATE ) ) {
                m_iViewId = GetPortInt( pActInfo, EIP_VIEWID );
                m_bHandlerRegistered = false;
                pActInfo->pGraph->SetRegularlyUpdated( pActInfo->myID, true );
            }
        }
        break;

        case eFE_Update:
            if ( !m_bHandlerRegistered )
            {
                CCoherentViewListener* pViewListener = gCoherentUISystem->GetViewListener( m_iViewId );
                if ( pViewListener && pViewListener->IsReadyForBindings() )
                {
                    Coherent::UI::View* pView = pViewListener->GetView();
                    if ( pView )
                    {
                        std::string sEvent = GetPortString( pActInfo, EIP_EVENT );
                        pView->RegisterForEvent( sEvent.c_str(), Coherent::UI::MakeHandler(this, &CFlowCUIHandleEvent::HandleEvent));
                        m_bHandlerRegistered = true;
                    }
                }
            }
            if ( m_bReceivedEvent )
            {
                m_bReceivedEvent = false;
                string strArg = m_event.strArg.c_str();
                ActivateOutput( pActInfo, EIP_ARG1, strArg );
                ActivateOutput( pActInfo, EIP_ARG2, m_event.boolArg );
            }
            break;
        }
    }
    CCoherentViewListener* CCoherentUISystem::GetViewListener( int id )
    {
        if ( m_HudViewListener )
        {
            Coherent::UI::View* pHudView = m_HudViewListener->GetView();
            if ( pHudView != nullptr && pHudView->GetId() == id )
            {
                return m_HudViewListener.get();
            }
        }

        for ( View::const_iterator iter = m_Views.begin(); iter != m_Views.end(); ++iter )
        {
            Coherent::UI::View* pView = iter->first->GetView();
            if ( pView->GetId() == id )
            {
                return iter->first;
            }
        }
        return NULL;
    }
    void CCoherentUISystem::UpdateHUD()
    {
        static Vec3 lastPosition = Vec3Constants<Vec3::value_type>::fVec3_Zero;
        static float lastRotation = 0;

        CCoherentViewListener* pHUDListener = NULL;
        pHUDListener = ( m_HudViewListener ? m_HudViewListener.get() : NULL );

        if ( pHUDListener )
        {
            CCamera& camera = gEnv->pSystem->GetViewCamera();
            Vec3 viewDir = camera.GetViewdir();
            float rotation = cry_atan2f( viewDir.y, viewDir.x ) * 180.0f / 3.14159f;
            // Adjust rotation so it is the same as in the game
            rotation = -rotation - 135.0f;
            Coherent::UI::View* pView = pHUDListener->GetView();

            if ( pView && pHUDListener->IsReadyForBindings() )
            {
                if ( rotation != lastRotation )
                {
                    pView->TriggerEvent( "SetAbsoluteCompassRotation", rotation );
                    // Adjust the rotation for the map, too...
                    pView->TriggerEvent( "SetPlayerRotationOnMap", rotation - 45.0f );

                    lastRotation = rotation;
                }

                Vec3 cameraPosition = camera.GetPosition();

                if ( ( cameraPosition - lastPosition ).GetLengthSquared() > VEC_EPSILON )
                {
                    pView->TriggerEvent( "SetPlayerPositionOnMap", cameraPosition.x, cameraPosition.y );

                    lastPosition = cameraPosition;
                }
            }
        }
    }
            virtual void ProcessEvent( EFlowEvent evt, SActivationInfo* pActInfo )
            {
                switch ( evt )
                {
                    case eFE_Suspend:
                        break;

                    case eFE_Resume:
                        break;

                    case eFE_Initialize:
                        INITIALIZE_OUTPUTS( pActInfo );
                        break;

                    case eFE_SetEntityId:
                        m_pEntity = pActInfo->pEntity;
                        break;

                    case eFE_Activate:
                        if ( IsPortActive( pActInfo, EIP_ACTIVATE ) && m_pEntity )
                        {
                            // get the view definition
                            std::string sUrl = GetPortString( pActInfo, EIP_URL );
                            std::wstring sUrlW( sUrl.length(), L' ' );
                            sUrlW.assign( sUrl.begin(), sUrl.end() );

                            Coherent::UI::ViewInfo info;
                            info.Width = GetPortInt( pActInfo, EIP_WIDTH );
                            info.Height = GetPortInt( pActInfo, EIP_HEIGHT );
                            info.IsTransparent = GetPortBool( pActInfo, EIP_TRANSPARENT );
                            info.UsesSharedMemory = GetPortBool( pActInfo, EIP_SHARED_MEMORY );
                            info.SupportClickThrough = GetPortBool( pActInfo, EIP_CLICKABLE );

                            m_pViewConfig->ViewInfo = info;
                            m_pViewConfig->Url = sUrlW;
                            m_pViewConfig->Entity = m_pEntity;
                            m_pViewConfig->CollisionMesh = GetPortString( pActInfo, EIP_MESH );

                            // indicate that we have to create/update the view later
                            m_bViewNeedsUpdate = true;
                            pActInfo->pGraph->SetRegularlyUpdated( pActInfo->myID, true );
                        }

                        break;

                    case eFE_Update:
                        // make sure the view is created/updated, after the system is ready
                        if ( m_bViewNeedsUpdate &&  gCoherentUISystem->IsReady() )
                        {
                            if ( m_pViewListener )
                            {
                                gCoherentUISystem->DeleteView( m_pViewListener );
                            }
                            m_pViewListener = gCoherentUISystem->CreateView( m_pViewConfig );
                            m_bViewNeedsUpdate = false;
                        }

                        // set the view id output after the view is available
                        if ( m_pViewListener )
                        {
                            Coherent::UI::View* view = m_pViewListener->GetView();
                            if ( view )
                            {
                                ActivateOutput<int>( pActInfo, EOP_VIEWID, view->GetId() );
                                // updates are not necessary until next initialization
                                pActInfo->pGraph->SetRegularlyUpdated( pActInfo->myID, false );
                            }
                        }
                        break;
                }
            }
            virtual void ProcessEvent( EFlowEvent evt, SActivationInfo* pActInfo )
            {
                switch ( evt )
                {
                    case eFE_Suspend:
                        break;

                    case eFE_Resume:
                        break;

                    case eFE_Initialize:
                        INITIALIZE_OUTPUTS( pActInfo );
                        break;

                    case eFE_Activate:
                        {
                            if ( IsPortActive( pActInfo, EIP_ACTIVATE ) )
                            {
                                // get the view definition
                                std::string sPath = GetPortString( pActInfo, EIP_PATH );
                                m_sPathW.assign( sPath.begin(), sPath.end() );

                                // indicate that we have to create/update the view later
                                m_bViewNeedsUpdate = true;
                                pActInfo->pGraph->SetRegularlyUpdated( pActInfo->myID, true );
                            }
                        }
                        break;

                    case eFE_Update:
                        // make sure the view is created, after the system is ready
                        if ( m_bViewNeedsUpdate &&  gCoherentUISystem->IsReady() )
                        {
                            if ( m_pViewListener )
                            {
                                // update only
                                Coherent::UI::View* view = m_pViewListener->GetView();
                                view->Load( m_sPathW.c_str() );
                                view->Reload( true );
                            }
                            else
                            {
                                // create
                                m_pViewListener = gCoherentUISystem->CreateHUDView( m_sPathW );
                            }
                            m_bViewNeedsUpdate = false;
                        }

                        // set the view id output after the view is available
                        if ( m_pViewListener )
                        {
                            Coherent::UI::View* view = m_pViewListener->GetView();
                            if ( view )
                            {
                                ActivateOutput<int>( pActInfo, EOP_VIEWID, view->GetId() );
                                // updates are not necessary until next initialization
                                pActInfo->pGraph->SetRegularlyUpdated( pActInfo->myID, false );
                            }
                        }
                        break;
                }
            }