-
Notifications
You must be signed in to change notification settings - Fork 30
/
InternalDisplay.cs
119 lines (96 loc) · 3.66 KB
/
InternalDisplay.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace kOS
{
public class kOSInternalDisplay : InternalModule
{
public RenderTexture InternalDisplayTexture = new RenderTexture(512, 512, 32);
public GameObject screenGo;
public CPU Cpu;
private bool lockState = false;
private int unlockWait = 0;
public void Start()
{
var prop = internalProp;
Cpu = part.GetComponent<kOSProcessor>().cpu;
var size = 0.1f;
var m = new Mesh();
m.name = "Scripted_Plane_New_Mesh";
m.vertices = new Vector3[] { new Vector3(size, -size, 0.01f), new Vector3(size, size, 0.01f), new Vector3(-size, size, 0.01f), new Vector3(-size, -size, 0.01f) };
m.uv = new Vector2[] { new Vector2(0, 0.4f), new Vector2(0, 1), new Vector2(0.79f, 1), new Vector2(0.79f, 0.4f) };
m.triangles = new int[] { 0, 1, 2, 0, 2, 3 };
m.RecalculateNormals();
MeshFilter mf = prop.FindModelComponents<MeshFilter>()[0];
prop.FindModelComponents<MeshFilter>()[1].mesh = null;
mf.mesh = m;
screenGo = mf.gameObject;
BoxCollider b = screenGo.GetComponent<BoxCollider>() ?? screenGo.AddComponent<BoxCollider>();
ClickHandler handler = screenGo.GetComponent<ClickHandler>() ?? screenGo.AddComponent<ClickHandler>();
handler.OnClick = OnMouseDown;
foreach (MeshRenderer renderer in prop.FindModelComponents<MeshRenderer>())
{
Material mat = new Material(Shader.Find(" Diffuse"));
mat.SetTexture("_MainTex", InternalDisplayTexture);
mat.SetTextureScale("_MainTex", new Vector2(1, 1));
renderer.material = mat;
}
foreach (PartModule module in part.Modules)
{
if (module is kOSProcessor)
{
var k = (kOSProcessor)module;
k.InternalDisplayTexture = InternalDisplayTexture;
}
}
}
public void LockCamera()
{
InputManager.Lock(Cpu);
lockState = true;
unlockWait = 3;
}
public void UnlockCamera()
{
InputManager.Unlock();
lockState = false;
}
public void Update()
{
foreach (MeshRenderer renderer in internalProp.FindModelComponents<MeshRenderer>())
{
renderer.enabled = Cpu.InternalDisplayEnabled;
}
if (lockState == true)
{
if (unlockWait > 0) unlockWait --;
if (unlockWait == 0 && Event.current.type == EventType.MouseDown)
{
UnlockCamera();
}
else
{
InternalCamera.Instance.camera.transform.LookAt(screenGo.transform.position, screenGo.transform.up);
InternalCamera.Instance.camera.fieldOfView = 18;
InternalCamera.Instance.UnlockMouse();
FlightCamera.fetch.transform.localRotation = InternalCamera.Instance.camera.transform.localRotation;
InputManager.ProcessKeyStrokes();
}
}
}
public void OnMouseDown()
{
if (!lockState) LockCamera(); else UnlockCamera();
}
}
public class ClickHandler : MonoBehaviour
{
public Action OnClick;
public void OnMouseDown()
{
OnClick();
}
}
}