implement multiple screen.

This commit is contained in:
hecomi
2016-10-28 17:28:22 +09:00
parent ba5ebe8094
commit d9f83cf89e
18 changed files with 409 additions and 196 deletions
Binary file not shown.
@@ -1,44 +0,0 @@
using UnityEngine;
namespace uDesktopDuplication
{
[RequireComponent(typeof(uDesktopDuplication))]
public class MouseDrawer : MonoBehaviour
{
[SerializeField]
Transform cursor;
[SerializeField]
Vector2 modelScale = Vector2.one;
[SerializeField]
Vector2 offset = new Vector2(0.5f, 0.5f);
uDesktopDuplication udd_;
void OnEnable()
{
udd_ = GetComponent<uDesktopDuplication>();
udd_.onMouseMove += OnMouseMove;
}
void OnDisable()
{
udd_.onMouseMove -= OnMouseMove;
}
void OnMouseMove(Vector2 pos)
{
var x = pos.x * modelScale.x * 0.5f;
var y = pos.y * modelScale.y * 0.5f;
var iy = udd_.invertY ? +1 : -1;
var localPos = transform.right * x + iy * transform.up * y;
var worldPos = transform.TransformPoint(localPos);
worldPos += cursor.right * offset.x * cursor.localScale.x;
worldPos += -cursor.up * offset.y * cursor.localScale.y;
cursor.position = worldPos;
}
}
}
@@ -0,0 +1,39 @@
using UnityEngine;
namespace uDesktopDuplication
{
[RequireComponent(typeof(uDD_Texture))]
public class uDD_Cursor : MonoBehaviour
{
[SerializeField]
Transform cursor;
[SerializeField]
Vector2 modelScale = Vector2.one;
[SerializeField]
Vector2 offset = new Vector2(0.5f, 0.5f);
private uDD_Texture texture_;
void OnEnable()
{
texture_ = GetComponent<uDD_Texture>();
}
void Update()
{
var monitor = texture_.monitor;
if (monitor.isPointerVisible) {
var x = (1f * monitor.pointerX / monitor.width - 0.5f) * modelScale.x;
var y = (0.5f - 1f * monitor.pointerY / monitor.height) * modelScale.y;
var iy = texture_.invertY ? +1 : -1;
var localPos = transform.right * x + iy * transform.up * y;
var worldPos = transform.TransformPoint(localPos);
worldPos += cursor.right * offset.x * cursor.localScale.x;
worldPos += -cursor.up * offset.y * cursor.localScale.y;
cursor.position = worldPos;
}
}
}
}
@@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: d2d9e4ca459ecb44da8815bafe9655c5
timeCreated: 1477559806
timeCreated: 1477635630
licenseType: Pro
MonoImporter:
serializedVersion: 2
@@ -0,0 +1,39 @@
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace uDesktopDuplication
{
public static class uDD_Lib
{
[DllImport("uDesktopDuplication")]
public static extern int GetMonitorCount();
[DllImport("uDesktopDuplication")]
public static extern IntPtr GetRenderEventFunc();
[DllImport("uDesktopDuplication")]
public static extern void GetName(int id, StringBuilder buf, int len);
[DllImport("uDesktopDuplication")]
public static extern int GetWidth(int id);
[DllImport("uDesktopDuplication")]
public static extern int GetHeight(int id);
[DllImport("uDesktopDuplication")]
public static extern bool IsPrimary(int id);
[DllImport("uDesktopDuplication")]
public static extern bool IsPointerVisible(int id);
[DllImport("uDesktopDuplication")]
public static extern int GetPointerX(int id);
[DllImport("uDesktopDuplication")]
public static extern int GetPointerY(int id);
[DllImport("uDesktopDuplication")]
public static extern int SetTexturePtr(int id, IntPtr ptr);
public static string GetName(int id)
{
var buf = new StringBuilder(32);
GetName(id, buf, buf.Capacity);
return buf.ToString();
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: cb02196c3e766c14ba398dc130e443e8
timeCreated: 1477643164
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 89616e59e1cccb346bd4e959257990ca, type: 3}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,74 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace uDesktopDuplication
{
public class uDD_Manager : MonoBehaviour
{
private static uDD_Manager instance_;
public static uDD_Manager instance
{
get
{
if (instance_) return instance_;
var go = new GameObject("uDesktopDuplicationManager");
return go.AddComponent<uDD_Manager>();
}
}
private List<uDD_Monitor> monitors_ = new List<uDD_Monitor>();
static public List<uDD_Monitor> monitors
{
get { return instance.monitors_; }
}
static public uDD_Monitor primary
{
get
{
return instance.monitors_.Find(monitor => monitor.isPrimary);
}
}
private Coroutine renderCoroutine_ = null;
void Awake()
{
if (instance_ != null) return;
instance_ = this;
for (int i = 0; i < uDD_Monitor.count; ++i) {
monitors.Add(new uDD_Monitor(i));
}
}
void OnEnable()
{
renderCoroutine_ = StartCoroutine(OnRender());
}
void OnDisable()
{
if (renderCoroutine_ != null) {
StopCoroutine(renderCoroutine_);
renderCoroutine_ = null;
}
}
IEnumerator OnRender()
{
for (;;) {
yield return new WaitForEndOfFrame();
foreach (var monitor in monitors) {
if (monitor.shouldBeUpdated) {
monitor.Render();
}
monitor.shouldBeUpdated = false;
}
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 4dccb434f55ebee4f91bfb9935092bbf
timeCreated: 1477643174
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 89616e59e1cccb346bd4e959257990ca, type: 3}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,84 @@
using UnityEngine;
namespace uDesktopDuplication
{
public class uDD_Monitor
{
public uDD_Monitor(int id)
{
this.id = id;
}
public static int count
{
get { return uDD_Lib.GetMonitorCount(); }
}
public int id
{
get;
private set;
}
public string name
{
get { return uDD_Lib.GetName(id); }
}
public bool isPrimary
{
get { return uDD_Lib.IsPrimary(id); }
}
public int width
{
get { return uDD_Lib.GetWidth(id); }
}
public int height
{
get { return uDD_Lib.GetHeight(id); }
}
public bool isPointerVisible
{
get { return uDD_Lib.IsPointerVisible(id); }
}
public int pointerX
{
get { return uDD_Lib.GetPointerX(id); }
}
public int pointerY
{
get { return uDD_Lib.GetPointerY(id); }
}
public bool shouldBeUpdated
{
get;
set;
}
private Texture2D texture_;
public Texture2D texture
{
get
{
if (texture_ == null) {
texture_ = new Texture2D(width, height, TextureFormat.BGRA32, false);
uDD_Lib.SetTexturePtr(id, texture_.GetNativeTexturePtr());
}
return texture_;
}
}
public void Render()
{
GL.IssuePluginEvent(uDD_Lib.GetRenderEventFunc(), id);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 310590b113d909b43b4ea19702904ac3
timeCreated: 1477643181
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 89616e59e1cccb346bd4e959257990ca, type: 3}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,46 @@
using UnityEngine;
namespace uDesktopDuplication
{
public class uDD_Texture : MonoBehaviour
{
public uDD_Monitor monitor { get; set; }
public bool invertX = false;
public bool invertY = false;
private Material material_;
void OnEnable()
{
if (monitor == null) {
monitor = uDD_Manager.primary;
}
material_ = GetComponent<Renderer>().material;
material_.mainTexture = monitor.texture;
}
void Update()
{
monitor.shouldBeUpdated = true;
UpdateMaterial();
}
void UpdateMaterial()
{
if (invertX) {
material_.EnableKeyword("INVERT_X");
} else {
material_.DisableKeyword("INVERT_X");
}
if (invertY) {
material_.EnableKeyword("INVERT_Y");
} else {
material_.DisableKeyword("INVERT_Y");
}
}
}
}
@@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: bb54a34570e4747429b1c5b66c69d356
timeCreated: 1477559813
timeCreated: 1477635636
licenseType: Pro
MonoImporter:
serializedVersion: 2
@@ -1,97 +0,0 @@
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
namespace uDesktopDuplication
{
public class uDesktopDuplication : MonoBehaviour
{
[DllImport("uDesktopDuplication")]
private static extern void Initialize();
[DllImport("uDesktopDuplication")]
private static extern int GetWidth();
[DllImport("uDesktopDuplication")]
private static extern int GetHeight();
[DllImport("uDesktopDuplication")]
private static extern bool IsPointerVisible();
[DllImport("uDesktopDuplication")]
private static extern int GetPointerX();
[DllImport("uDesktopDuplication")]
private static extern int GetPointerY();
[DllImport("uDesktopDuplication")]
private static extern int SetTexturePtr(IntPtr ptr);
[DllImport("uDesktopDuplication")]
private static extern IntPtr GetRenderEventFunc();
private Material material_;
public bool invertX = false;
public bool invertY = false;
public delegate void MouseMoveEventHandler(Vector2 pos);
public MouseMoveEventHandler onMouseMove { get; set; }
private Coroutine renderCoroutine_ = null;
void OnEnable()
{
var tex = new Texture2D(GetWidth(), GetHeight(), TextureFormat.BGRA32, false);
material_ = GetComponent<Renderer>().material;
material_.mainTexture = tex;
SetTexturePtr(tex.GetNativeTexturePtr());
renderCoroutine_ = StartCoroutine(OnRender());
}
void OnDisable()
{
if (renderCoroutine_ != null) {
StopCoroutine(renderCoroutine_);
renderCoroutine_ = null;
}
}
void Update()
{
UpdateMouseEvent();
UpdateMaterial();
}
void UpdateMouseEvent()
{
var isVisible = IsPointerVisible();
if (isVisible && onMouseMove != null) {
var x = GetPointerX();
var y = GetPointerY();
var w = GetWidth();
var h = GetHeight();
onMouseMove(new Vector2(2f * x / w - 1f, 1f - 2f * y / h));
}
}
void UpdateMaterial()
{
if (invertX) {
material_.EnableKeyword("INVERT_X");
} else {
material_.DisableKeyword("INVERT_X");
}
if (invertY) {
material_.EnableKeyword("INVERT_Y");
} else {
material_.DisableKeyword("INVERT_Y");
}
}
IEnumerator OnRender()
{
for (;;) {
yield return new WaitForEndOfFrame();
GL.IssuePluginEvent(GetRenderEventFunc(), 0);
}
}
}
}