CoherentUIOpenGLUI::CoherentUIOpenGLUI(Utils::ILoggingManager &logManager, Input::Input &input) :
		m_Input(input),

		m_Renderer(),

		m_ViewListener(m_Renderer),
		m_SystemListener(m_ViewListener),

		m_Bindings(m_ViewListener),
		m_ElementFactory(m_ViewListener),

		m_Settings(),
		m_System(nullptr),

		m_KeyIDs(input.GetSpecialKeyIds())
	{
		// Attach View Event
		m_ViewListener.OnViewReady.AttachMember(this, &CoherentUIOpenGLUI::OnViewReady, m_EventScope);

		// Initialize System
		m_Settings.HostDirectory = L"Host";
#ifdef _DEBUG
		m_Settings.DebuggerPort = 1234;
#endif
		m_System = InitializeUISystem(COHERENT_UI_SDK_VER, COHERENT_KEY, m_Settings, &m_SystemListener, Coherent::Logging::Info, new LogHandler(logManager));
		if (!m_System)
			throw std::exception("Coherent UI failed to initialize!");

		// This is completely safe, because no callback will be called outside Update()
		m_SystemListener.SetSystem(m_System);

		// Create and compile our GLSL program from the shaders
		m_ProgramID = m_Renderer.CreateShaderProgram(
			"Content/Shaders/CoherentUI.vert",
			"Content/Shaders/CoherentUI.frag"
		);

		// Load the initial texture. The texture ID returned is saved internally by the renderer
		// and can be obtained by the Renderer::GetTextureID method. This texture ID is later
		// used by the ViewEventListener to change the image data of the texture with the
		// bytes received from Coherent UI.
		m_TextureID = m_Renderer.CreateTexture(1024, 768);

		// Get a handle for the texture sampler uniform
		m_TextureSamplerID = glGetUniformLocation(m_ProgramID, "textureSampler");

		float vertexData [] =
		{
			// -- position --     -- uv --
			-1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
			3.0f, -1.0f, 0.0f, 2.0f, 1.0f,
			-1.0f, 3.0f, 0.0f, 0.0f, -1.0f
		};

		glGenBuffers(1, &m_VertexBuffer);
		glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
		glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
	}
    bool CCoherentUISystem::InitializeCoherentUI()
    {
        // register listeners
        m_SystemEventsListener.reset( new CCoherentSystemEventListener( this ) );
        m_InputEventsListener.reset( new CCoherentInputEventListener() );

        gEnv->pGameFramework->RegisterListener( this, "CCoherentUISystem", eFLPriority_HUD );
        gEnv->pInput->AddEventListener( m_InputEventsListener.get() );

        std::string sPath( gPluginManager->GetPluginDirectory( PLUGIN_NAME ) );
        sPath += "\\host";
        std::wstring sPathW;
        sPathW.assign( sPath.begin(), sPath.end() );

        Coherent::UI::SystemSettings settings( sPathW.c_str(), false, true, L"coui://cookies.dat", L"cui_cache", L"cui_app_cache", true, false, 9999 );
        m_pUISystem = InitializeUISystem( COHERENT_KEY, settings, m_SystemEventsListener.get(), Coherent::Logging::Debug, nullptr, &m_PakFileHandler );

        return m_pUISystem != NULL;
    }