////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2007-2020 , Inc. All Rights Reserved.
//
////////////////////////////////////////////////////////////////////////////////
using System;
using UnityEngine;
using GCSeries.Core.Interop;
namespace GCSeries.Core.Sdk
{
public class ZContext : ZNativeResource
{
///
/// The ZContext constructor.
///
///
///
/// Will throw an exception if the SDK failed to initialize.
///
public ZContext()
{
ZPlugin.ThrowOnError(ZPlugin.Initialize(out this._nativePtr));
this.DisplayManager = new ZDisplayManager(this);
this.TargetManager = new ZTargetManager(this);
this.MouseEmulator = new ZMouseEmulator(this);
this.MouseEmulator.Target = this.TargetManager.StylusTarget;
}
~ZContext()
{
this.Dispose(false);
}
////////////////////////////////////////////////////////////////////////
// Public Properties
////////////////////////////////////////////////////////////////////////
///
/// The manager responsible for managing display information
/// corresponding to all active displays.
///
public ZDisplayManager DisplayManager { get; } = null;
///
/// The manager responsible for managing tracking information
/// corresponding to all active, trackable targets.
///
///
///
/// Currently the glasses and stylus are the only supported
/// trackable targets.
///
public ZTargetManager TargetManager { get; } = null;
///
/// The mouse emulator which provides support for allowing any
/// 6-DOF trackable target to emulate the system-level mouse.
///
public ZMouseEmulator MouseEmulator { get; } = null;
///
/// The version of SDK runtime that is currently installed
/// on the user's machine.
///
public Version RuntimeVersion
{
get
{
int major = 0;
int minor = 0;
int patch = 0;
ZPlugin.LogOnError(ZPlugin.GetRuntimeVersion(
this._nativePtr, out major, out minor, out patch),
"GetRuntimeVersion");
return new Version(major, minor, patch);
}
}
///
/// Specifies whether tracking is enabled.
///
///
///
/// This property acts as a global flag to enable or disable
/// updates for all tracking related information.
///
public bool IsTrackingEnabled
{
get
{
bool isEnabled = false;
ZPlugin.LogOnError(
ZPlugin.IsTrackingEnabled(this._nativePtr, out isEnabled),
"IsTrackingEnabled");
return isEnabled;
}
set
{
ZPlugin.LogOnError(
ZPlugin.SetTrackingEnabled(this._nativePtr, value),
"SetTrackingEnabled");
}
}
////////////////////////////////////////////////////////////////////////
// Public Methods
////////////////////////////////////////////////////////////////////////
///
/// Updates the internal state of the context.
///
///
///
/// In general, this method should only be called once per frame.
///
/// The update is responsible for capturing the latest tracking
/// information, forwarding the latest head pose information to all
/// active frustums, etc.
///
public void Update()
{
ZPlugin.LogOnError(ZPlugin.Update(this._nativePtr), "Update");
}
///
/// Creates an instance of the ZViewport class at the specified
/// virtual desktop position.
///
///
///
/// The (x, y) virtual desktop position in pixels corresponding
/// to the viewport's top-left corner.
///
///
///
/// An instance of the ZViewport class.
///
public ZViewport CreateViewport(Vector2Int position)
{
// Create the viewport.
IntPtr viewportNativePtr;
ZPlugin.LogOnError(
ZPlugin.CreateViewport(this._nativePtr, out viewportNativePtr),
"CreateViewport");
ZViewport viewport = new ZViewport(viewportNativePtr);
viewport.Position = position;
// Update the context to ensure the appropriate display
// angle has been passed to the viewport's frustum.
this.Update();
// Initialize the frustum.
ZFrustum frustum = viewport.Frustum;
frustum.HeadPose = frustum.DefaultHeadPose;
return viewport;
}
////////////////////////////////////////////////////////////////////////
// Protected Methods
////////////////////////////////////////////////////////////////////////
protected override void Dispose(bool disposing)
{
if (this._isDisposed)
{
return;
}
this._isDisposed = true;
// Free managed objects.
if (disposing)
{
this.DisplayManager.ClearCache();
this.TargetManager.ClearCache();
}
// Free unmanaged objects.
ZPlugin.LogOnError(ZPlugin.ShutDown(this._nativePtr), "ShutDown");
// Call to base class implementation.
base.Dispose(disposing);
}
////////////////////////////////////////////////////////////////////////
// Private Members
////////////////////////////////////////////////////////////////////////
private bool _isDisposed = false;
}
}