123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- namespace COM3D2.MeidoPhotoStudio.Plugin
- {
- internal class LightManager : IManager, ISerializable
- {
- public const string header = "LIGHT";
- private static bool cubeActive = true;
- public static bool CubeActive
- {
- get => cubeActive;
- set
- {
- if (value != cubeActive)
- {
- cubeActive = value;
- CubeActiveChange?.Invoke(null, EventArgs.Empty);
- }
- }
- }
- private static event EventHandler CubeActiveChange;
- private List<DragPointLight> lightList = new List<DragPointLight>();
- private int selectedLightIndex = 0;
- public int SelectedLightIndex
- {
- get => selectedLightIndex;
- set
- {
- selectedLightIndex = Mathf.Clamp(value, 0, lightList.Count - 1);
- lightList[SelectedLightIndex].IsActiveLight = true;
- }
- }
- public string[] LightNameList => lightList.Select(light => LightName(light.Name)).ToArray();
- public string ActiveLightName => LightName(lightList[SelectedLightIndex].Name);
- public DragPointLight CurrentLight
- {
- get
- {
- return lightList[SelectedLightIndex];
- }
- }
- public event EventHandler Rotate;
- public event EventHandler Scale;
- public event EventHandler ListModified;
- public event EventHandler Select;
- public void Serialize(System.IO.BinaryWriter binaryWriter)
- {
- binaryWriter.Write(header);
- binaryWriter.Write(lightList.Count);
- foreach (DragPointLight light in lightList)
- {
- light.Serialize(binaryWriter);
- }
- }
- public void Deserialize(System.IO.BinaryReader binaryReader)
- {
- ClearLights();
- int numberOfLights = binaryReader.ReadInt32();
- lightList[0].Deserialize(binaryReader);
- for (int i = 1; i < numberOfLights; i++)
- {
- AddLight();
- lightList[i].Deserialize(binaryReader);
- }
- }
- public void Activate()
- {
- GameMain.Instance.MainCamera.GetComponent<Camera>().backgroundColor = Color.black;
- AddLight(GameMain.Instance.MainLight.gameObject, true);
- CubeActiveChange += OnCubeActive;
- }
- public void Deactivate()
- {
- for (int i = 0; i < lightList.Count; i++)
- {
- DestroyLight(lightList[i]);
- }
- selectedLightIndex = 0;
- lightList.Clear();
- GameMain.Instance.MainLight.Reset();
- Light mainLight = GameMain.Instance.MainLight.GetComponent<Light>();
- mainLight.type = LightType.Directional;
- DragPointLight.SetLightProperties(mainLight, new LightProperty());
- CubeActiveChange -= OnCubeActive;
- }
- public void Update() { }
- public void AddLight(GameObject lightGo = null, bool isMain = false)
- {
- GameObject go = lightGo ?? new GameObject("MPS Light");
- DragPointLight light = DragPoint.Make<DragPointLight>(PrimitiveType.Cube, Vector3.one * 0.12f);
- light.Initialize(() => go.transform.position, () => go.transform.eulerAngles);
- light.Set(go.transform);
- light.IsMain = isMain;
- light.Rotate += OnRotate;
- light.Scale += OnScale;
- light.Delete += OnDelete;
- light.Select += OnSelect;
- lightList.Add(light);
- CurrentLight.IsActiveLight = false;
- SelectedLightIndex = lightList.Count;
- OnListModified();
- }
- public void DeleteActiveLight()
- {
- if (selectedLightIndex == 0) return;
- DeleteLight(SelectedLightIndex);
- }
- public void DeleteLight(int lightIndex, bool noUpdate = false)
- {
- if (lightIndex == 0) return;
- DestroyLight(lightList[lightIndex]);
- lightList.RemoveAt(lightIndex);
- if (lightIndex <= SelectedLightIndex) SelectedLightIndex -= 1;
- if (noUpdate) return;
- OnListModified();
- }
- public void SetColourModeActive(bool isColourMode)
- {
- lightList[0].IsColourMode = isColourMode;
- }
- public void ClearLights()
- {
- for (int i = lightList.Count - 1; i > 0; i--)
- {
- DeleteLight(i);
- }
- selectedLightIndex = 0;
- }
- private void DestroyLight(DragPointLight light)
- {
- if (light == null) return;
- light.Rotate -= OnRotate;
- light.Scale -= OnScale;
- light.Delete -= OnDelete;
- light.Select -= OnSelect;
- GameObject.Destroy(light.gameObject);
- }
- private string LightName(string name)
- {
- return Translation.Get("lightType", name);
- }
- private void OnDelete(object sender, EventArgs args)
- {
- DragPointLight theLight = (DragPointLight)sender;
- for (int i = 1; i < lightList.Count; i++)
- {
- DragPointLight light = lightList[i];
- if (light == theLight)
- {
- DeleteLight(i);
- return;
- }
- }
- }
- private void OnRotate(object sender, EventArgs args)
- {
- OnTransformEvent((DragPointLight)sender, Rotate);
- }
- private void OnScale(object sender, EventArgs args)
- {
- OnTransformEvent((DragPointLight)sender, Scale);
- }
- private void OnTransformEvent(DragPointLight light, EventHandler handler)
- {
- if (light.IsActiveLight)
- {
- handler?.Invoke(this, EventArgs.Empty);
- }
- }
- private void OnSelect(object sender, EventArgs args)
- {
- DragPointLight theLight = (DragPointLight)sender;
- int select = lightList.FindIndex(light => light == theLight);
- if (select >= 0)
- {
- this.SelectedLightIndex = select;
- this.Select?.Invoke(this, EventArgs.Empty);
- }
- }
- private void OnListModified()
- {
- ListModified?.Invoke(this, EventArgs.Empty);
- }
- private void OnCubeActive(object sender, EventArgs args)
- {
- foreach (DragPointLight dragPoint in lightList)
- {
- dragPoint.gameObject.SetActive(CubeActive);
- }
- }
- }
- }
|