Files
MI-38/Heli_with_panels/Assets/Scripts/ScEditor/ScrollViewAdapterPanel1.cs
2022-07-04 13:15:35 +03:00

435 lines
22 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class ScrollViewAdapterPanel1 : MonoBehaviour
{
public RectTransform prefab;
public RectTransform content;
//public int curObjAction;
ScEditor sce;
Manager man;
List<ScAction> actList;
List<GameObject> scrollGO = new List<GameObject>();
GameObject panel1;
int contentH;
private void Update()
{
//content.sizeDelta = new Vector2(content.sizeDelta.x, contentH);
}
public void Init()
{
sce = GameObject.Find("ScEditor").GetComponent<ScEditor>();
man = GameObject.Find("Manager").GetComponent<Manager>();
panel1 = GameObject.Find("Panel1");
if (sce.SC == null) Debug.Log(" Panel1 Start: sc = null");
//Debug.Log(" Start: sc.curModeSc.acts.Count = " + sc.curModeSc.acts.Count);
//Debug.Log(GameObject.Find("PodsvetButton").GetComponent<Button>().interactable);
}
public void UpdateItems()
{
sce = GameObject.Find("ScEditor").GetComponent<ScEditor>();
actList = sce.MakeCurObjectActionsList();
scrollGO.Clear();
foreach (Transform child in content)
Destroy(child.gameObject);
int newActY = -5;
for(int i=0;i<actList.Count;i++)
{
//Debug.Log("Left panel: Add action " + actList[i].type);
GameObject _blockSetState = prefab.Find("ScrollElements/SetState").gameObject;
GameObject _blockSetRangeState = prefab.Find("ScrollElements/SetRangeState").gameObject;
GameObject _blockFreeTextPanel = prefab.Find("ScrollElements/FreeTextPanel").gameObject;
GameObject _blockSetTextField = prefab.Find("ScrollElements/SetTextField").gameObject;
GameObject _blockSetIntField = prefab.Find("ScrollElements/SetIntField").gameObject;
GameObject block = null;
if (actList[i].type == "setState")
{
block = GameObject.Instantiate(_blockSetState);
block.transform.SetPositionAndRotation(new Vector3(-12, newActY, 0), new Quaternion()); newActY -= 25;
block.transform.SetParent(content, false);
block.transform.Find("StateText").GetComponent<Text>().text = actList[i].setState.name;
int actNum = i;
block.transform.Find("BtnSetState").GetComponent<Button>().onClick.AddListener(() => { InsertCurObjAction(actNum); });
}
if (actList[i].type == "setStateInRange")
{
block = GameObject.Instantiate(_blockSetRangeState);
block.transform.SetPositionAndRotation(new Vector3(-12, newActY, 0), new Quaternion()); newActY -= 37;
block.transform.SetParent(content, false);
block.transform.Find("ChangeValText").GetComponent<Text>().text = "Изменить значение состояния " + actList[i].setStateInRange.rangeStr;
block.transform.Find("IFRangeStateVal").GetComponent<TMP_InputField>().text = "";
block.transform.Find("IFRangeTime").GetComponent<TMP_InputField>().text = "0";
int actNum = i;
block.transform.Find("BtnSetRangeState").GetComponent<Button>().onClick.AddListener(() => { InsertCurObjAction(actNum); });
}
if (actList[i].type == "openPanel")
{
block = GameObject.Instantiate(_blockFreeTextPanel);
block.transform.SetPositionAndRotation(new Vector3(-12, newActY, 0), new Quaternion()); newActY -= 18;
block.transform.SetParent(content, false);
block.transform.Find("FreeText").GetComponent<Text>().text = "Открыть 2D-панель";
int actNum = i;
block.transform.Find("BtnFreeTextPanel").GetComponent<Button>().onClick.AddListener(() => { InsertCurObjAction(actNum); });
}
if (actList[i].type == "switchTo3D")
{
block = GameObject.Instantiate(_blockFreeTextPanel);
block.transform.SetPositionAndRotation(new Vector3(-12, newActY, 0), new Quaternion()); newActY -= 18;
block.transform.SetParent(content, false);
block.transform.Find("FreeText").GetComponent<Text>().text = "Закрыть 2D-панель";
int actNum = i;
block.transform.Find("BtnFreeTextPanel").GetComponent<Button>().onClick.AddListener(() => { InsertCurObjAction(actNum); });
}
if (actList[i].type == "setTextField")
{
block = GameObject.Instantiate(_blockSetTextField);
block.transform.SetPositionAndRotation(new Vector3(-12, newActY, 0), new Quaternion()); newActY -= 45;
block.transform.SetParent(content, false);
block.transform.Find("IFStateVal").GetComponent<TMP_InputField>().text = "";
int actNum = i;
block.transform.Find("BtnSetTextField").GetComponent<Button>().onClick.AddListener(() => { InsertCurObjAction(actNum); });
}
if (actList[i].type == "setIntField")
{
block = GameObject.Instantiate(_blockSetIntField);
block.transform.SetPositionAndRotation(new Vector3(-12, newActY, 0), new Quaternion()); newActY -= 48;
block.transform.SetParent(content, false);
block.transform.Find("ChangeIntText").GetComponent<Text>().text = "Изменить целочисленное значение:";
block.transform.Find("IFIntFrom").GetComponent<TMP_InputField>().text = "";
block.transform.Find("IFIntTo").GetComponent<TMP_InputField>().text = "";
block.transform.Find("IFIntTime").GetComponent<TMP_InputField>().text = "0";
int actNum = i;
block.transform.Find("BtnSetIntField").GetComponent<Button>().onClick.AddListener(() => { InsertCurObjAction(actNum); });
}
if (actList[i].type == "setFloatField")
{
block = GameObject.Instantiate(_blockSetIntField);
block.transform.SetPositionAndRotation(new Vector3(-12, newActY, 0), new Quaternion()); newActY -= 48;
block.transform.SetParent(content, false);
block.transform.Find("ChangeIntText").GetComponent<Text>().text = "Изменить дробное значение:";
block.transform.Find("IFIntFrom").GetComponent<TMP_InputField>().text = "0.0";
block.transform.Find("IFIntTo").GetComponent<TMP_InputField>().text = "0.0";
block.transform.Find("IFIntTime").GetComponent<TMP_InputField>().text = "0";
int actNum = i;
block.transform.Find("BtnSetIntField").GetComponent<Button>().onClick.AddListener(() => { InsertCurObjAction(actNum); });
}
scrollGO.Add(block);
if (block != null)
block.name = "curObjAction_" + i;
else
Debug.Log("Unknown action type: " + actList[i].type);
}
contentH = -newActY -195;
content.sizeDelta = new Vector2(content.sizeDelta.x, contentH);
}
public void InsertCurObjAction(int ind)
{
if (scrollGO[ind] == null) return;
GameObject block = scrollGO[ind];
if (actList[ind].type == "setState")
{
actList[ind].title = "[" + sce.curObject.name + "] " + "Установить состояние \"" + actList[ind].setState.name + "\"";
}
if (actList[ind].type == "setStateInRange")
{
int.TryParse(block.transform.Find("IFRangeStateVal").GetComponent<TMP_InputField>().text, out actList[ind].setStateInRange.val);
int.TryParse(block.transform.Find("IFRangeTime").GetComponent<TMP_InputField>().text, out actList[ind].setStateInRange.sec);
string timeStr = "";
if (actList[ind].setStateInRange.sec != 0)
timeStr = " за время " + actList[ind].setStateInRange.sec + " сек";
actList[ind].title = "[" + sce.curObject.name + "] " + "Установить значение состояния \"" + actList[ind].setStateInRange.val + "\"" + timeStr;
}
//if (actList[ind].type == "openPanel") { }
//if (actList[ind].type == "switchTo3D") { }
if (actList[ind].type == "setTextField")
{
actList[ind].setTextField.text = block.transform.Find("IFStateVal").GetComponent<TMP_InputField>().text;
string rusColor = "зелёный"; actList[ind].setTextField.color = "green";
if (block.transform.Find("TextColorGroup/ToggleRed").GetComponent<Toggle>().isOn)
{ actList[ind].setTextField.color = "red"; rusColor = "красный"; }
if (block.transform.Find("TextColorGroup/ToggleYellow").GetComponent<Toggle>().isOn)
{ actList[ind].setTextField.color = "yellow"; rusColor = "жёлтый"; }
if (block.transform.Find("TextColorGroup/ToggleWhite").GetComponent<Toggle>().isOn)
{ actList[ind].setTextField.color = "white"; rusColor = "белый"; }
actList[ind].title = "[" + sce.curObject.name + "] " + "Установить текстовое поле в \""+ actList[ind].setTextField.text + "\"" + " ("+ rusColor + ")";
}
if (actList[ind].type == "setIntField")
{
int.TryParse(block.transform.Find("IFIntFrom").GetComponent<TMP_InputField>().text, out actList[ind].setIntField.valFrom);
int.TryParse(block.transform.Find("IFIntTo").GetComponent<TMP_InputField>().text, out actList[ind].setIntField.valTo);
int.TryParse(block.transform.Find("IFIntTime").GetComponent<TMP_InputField>().text, out actList[ind].setIntField.sec);
string timeStr = "";
if (actList[ind].setIntField.sec != 0)
timeStr = " за время " + actList[ind].setIntField.sec + " сек";
string rusColor = "зелёный"; actList[ind].setIntField.color = "green";
if (block.transform.Find("TextColorGroup/ToggleRed").GetComponent<Toggle>().isOn)
{ actList[ind].setIntField.color = "red"; rusColor = "красный"; }
if (block.transform.Find("TextColorGroup/ToggleYellow").GetComponent<Toggle>().isOn)
{ actList[ind].setIntField.color = "yellow"; rusColor = "жёлтый"; }
if (block.transform.Find("TextColorGroup/ToggleWhite").GetComponent<Toggle>().isOn)
{ actList[ind].setTextField.color = "white"; rusColor = "белый"; }
actList[ind].title = "[" + sce.curObject.name + "] " + "Изменить числовое текстовое поле c \""+ actList[ind].setIntField.valFrom + "\" до \"" + actList[ind].setIntField.valTo + "\"" +timeStr +" (" + rusColor + ")";
}
if (actList[ind].type == "setFloatField")
{
float f;
f = float.Parse(block.transform.Find("IFIntFrom").GetComponent<TMP_InputField>().text, System.Globalization.CultureInfo.InvariantCulture);
//float.TryParse(block.transform.Find("IFIntFrom").GetComponent<TMP_InputField>().text, out f);
actList[ind].setIntField.valFrom = Mathf.FloorToInt(f * 10f);
//float.TryParse(block.transform.Find("IFIntTo").GetComponent<TMP_InputField>().text, out f);
f = float.Parse(block.transform.Find("IFIntTo").GetComponent<TMP_InputField>().text, System.Globalization.CultureInfo.InvariantCulture);
actList[ind].setIntField.valTo = Mathf.FloorToInt(f * 10f);
int.TryParse(block.transform.Find("IFIntTime").GetComponent<TMP_InputField>().text, out actList[ind].setIntField.sec);
string timeStr = "";
if (actList[ind].setIntField.sec != 0)
timeStr = " за время " + actList[ind].setIntField.sec + " сек";
string rusColor = "зелёный"; actList[ind].setIntField.color = "green";
if (block.transform.Find("TextColorGroup/ToggleRed").GetComponent<Toggle>().isOn)
{ actList[ind].setIntField.color = "red"; rusColor = "красный"; }
if (block.transform.Find("TextColorGroup/ToggleYellow").GetComponent<Toggle>().isOn)
{ actList[ind].setIntField.color = "yellow"; rusColor = "жёлтый"; }
if (block.transform.Find("TextColorGroup/ToggleWhite").GetComponent<Toggle>().isOn)
{ actList[ind].setTextField.color = "white"; rusColor = "белый"; }
actList[ind].title = "[" + sce.curObject.name + "] " +
"Изменить дробное числовое текстовое поле c \"" +
String.Format("{0:F1}", actList[ind].setIntField.valFrom/10f) +
"\" до \"" +
String.Format("{0:F1}", actList[ind].setIntField.valTo / 10f) +
"\"" + timeStr + " (" + rusColor + ")";
}
sce.InsertAction(actList[ind]);
}
// setState, setStateInRange, openPanel, setTextField, setIntField, switchTo3D, highlightOn, highlightOff,
// highlightTime, waitForClick, waitForTime, setCurCameraPos, showText, hideText, execScenario, playAudio
public void InsActHighlightOn()
{
if (sce.curObject == null) return;
ScAction newAct;
newAct = new ScAction(); newAct.type = "highlightOn";
newAct.objName = sce.curObject.name;
newAct.title = "[" + sce.curObject.name + "] " + "Включить подсветку объекта";
sce.InsertAction(newAct);
}
public void InsActHighlightOff()
{
if (sce.curObject == null) return;
ScAction newAct;
newAct = new ScAction(); newAct.type = "highlightOff";
newAct.objName = sce.curObject.name;
newAct.title = "[" + sce.curObject.name + "] " + "Выключить подсветку";
sce.InsertAction(newAct);
}
public void InsActHighlightTime()
{
if (sce.curObject == null) return;
ScAction newAct;
newAct = new ScAction(); newAct.type = "highlightTime";
newAct.objName = sce.curObject.name;
newAct.highlightTime = new actHighlightTime();
int.TryParse(GameObject.Find("IFFlashObjTime").GetComponent<TMP_InputField>().text, out newAct.highlightTime.sec);
newAct.title = "[" + sce.curObject.name + "] " + "Подсветить на время " + newAct.highlightTime.sec + " сек";
sce.InsertAction(newAct);
}
public void InsActWaitForClick()
{
if (sce.curObject == null) return;
ScAction newAct;
newAct = new ScAction(); newAct.type = "waitForClick";
newAct.objName = sce.curObject.name;
newAct.title = "[" + sce.curObject.name + "] " + "Ожидать нажатия на объект";
sce.InsertAction(newAct);
}
public void InsActWaitForTime()
{
ScAction newAct;
newAct = new ScAction(); newAct.type = "waitForTime";
newAct.objName = "";
newAct.waitForTime = new actWaitForTime();
int.TryParse(GameObject.Find("IFWaitForTime").GetComponent<TMP_InputField>().text, out newAct.waitForTime.sec);
newAct.title = "Пауза " + newAct.waitForTime.sec + " сек";
sce.InsertAction(newAct);
}
public void InsActShowText()
{
ScAction newAct;
newAct = new ScAction(); newAct.type = "showText";
newAct.objName = "";
newAct.showText = new actShowText();
newAct.showText.text = GameObject.Find("IFShowText").GetComponent<TMP_InputField>().text;
newAct.title = "Вывести текст \"" + newAct.showText.text;
//if(newAct.showText.text.Length > 15) newAct.title += "..";
newAct.title += "\"";
sce.InsertAction(newAct);
}
public void InsActHideText()
{
ScAction newAct;
newAct = new ScAction(); newAct.type = "hideText";
newAct.objName = "";
newAct.title = "Скрыть текст";
sce.InsertAction(newAct);
}
public void InsActSetCurCameraPos()
{
ScAction newAct;
newAct = new ScAction(); newAct.type = "setCurCameraPos";
newAct.objName = "";
newAct.setCurCameraPos = new actSetCurCameraPos();
GameObject viewCamera = man.viewCamera;
GameObject cabinCamera = man.cabinCamera;
GameObject curCamera;
if (cabinCamera.activeInHierarchy)
curCamera = cabinCamera;
else
curCamera = viewCamera;
newAct.setCurCameraPos.fov = (int)curCamera.GetComponent<Camera>().fieldOfView;
newAct.setCurCameraPos.viewMode = man.viewmode;
newAct.setCurCameraPos.position = new uVector3();
newAct.setCurCameraPos.position.x = curCamera.transform.position.x;
newAct.setCurCameraPos.position.y = curCamera.transform.position.y;
newAct.setCurCameraPos.position.z = curCamera.transform.position.z;
newAct.setCurCameraPos.rotation = new uVector3();
newAct.setCurCameraPos.rotation.x = curCamera.transform.rotation.eulerAngles.x;
newAct.setCurCameraPos.rotation.y = curCamera.transform.rotation.eulerAngles.y;
newAct.setCurCameraPos.rotation.z = curCamera.transform.rotation.eulerAngles.z;
newAct.setCurCameraPos.scale = new uVector3();
newAct.setCurCameraPos.scale.x = curCamera.transform.localScale.x;
newAct.setCurCameraPos.scale.y = curCamera.transform.localScale.y;
newAct.setCurCameraPos.scale.z = curCamera.transform.localScale.z;
if (cabinCamera.activeInHierarchy) // внутри
newAct.setCurCameraPos.camera = "cabin";
else // обзорка
{
newAct.setCurCameraPos.camera = "view";
newAct.setCurCameraPos.scriptOffset = new uVector3();
newAct.setCurCameraPos.scriptOffset.x = viewCamera.GetComponent<CameraFree>().offset.x;
newAct.setCurCameraPos.scriptOffset.y = viewCamera.GetComponent<CameraFree>().offset.y;
newAct.setCurCameraPos.scriptOffset.z = viewCamera.GetComponent<CameraFree>().offset.z;
}
newAct.title = "Установить положение камеры";
sce.InsertAction(newAct);
}
public void InsActExecScenario()
{
Dropdown scExecDropdown = GameObject.Find("DropdownCallScenario").GetComponent<Dropdown>();
if (scExecDropdown.options.Count == 0 || scExecDropdown.value == -1)
return;
ScAction newAct;
newAct = new ScAction(); newAct.type = "execScenario";
newAct.objName = "";
newAct.execScenario = new actExecScenario();
newAct.execScenario.fastFlag = GameObject.Find("fastCheckbox").GetComponent<Toggle>().isOn;
newAct.execScenario.name = scExecDropdown.options[scExecDropdown.value].text;
string fastStr = "";
if (newAct.execScenario.fastFlag)
fastStr = "(момент.)";
newAct.title = "Выполнить сценарий"+ fastStr + " \"" + newAct.execScenario.name;
//if (newAct.execScenario.name.Length > 15) newAct.title += "..";
newAct.title += "\"";
sce.InsertAction(newAct);
}
public void InsActPlayAudio()
{
Dropdown dd = GameObject.Find("DropdownPlayAudio").GetComponent<Dropdown>();
if (dd.options[dd.value].text == "") return;
StartCoroutine(InsActPlayAudioCoroutine(dd.options[dd.value].text));
}
public void InsActSetSkybox()
{
ScAction newAct;
newAct = new ScAction(); newAct.type = "setSkybox";
newAct.objName = "";
newAct.setSkybox = new actSetSkybox(); newAct.setSkybox.name = "";
string ruName = "";
if (GameObject.Find("groundCheckbox").GetComponent<Toggle>().isOn)
{ newAct.setSkybox.name = "ground"; ruName = "земля"; }
if (GameObject.Find("skyCheckbox").GetComponent<Toggle>().isOn)
{ newAct.setSkybox.name = "sky"; ruName = "небо"; }
newAct.title = "Установить окружение: " + ruName;
sce.InsertAction(newAct);
}
public void InsActShowRect(int x1, int y1, int x2, int y2)
{
ScAction newAct;
newAct = new ScAction(); newAct.type = "showRect";
newAct.objName = "";
newAct.showRect = new actShowRect();
newAct.showRect.x1 = x1; newAct.showRect.y1 = y1;// newAct.showRect.z1 = z1;
newAct.showRect.x2 = x2; newAct.showRect.y2 = y2;// newAct.showRect.z2 = z2;
newAct.title = "Выделение области";
sce.InsertAction(newAct);
}
public void InsActHideRects()
{
ScAction newAct;
newAct = new ScAction(); newAct.type = "hideRects";
newAct.objName = "";
newAct.title = "Скрыть все выделения областей";
sce.InsertAction(newAct);
}
IEnumerator InsActPlayAudioCoroutine(string name)
{
AudioClip clip;
string path = Path.Combine(Application.streamingAssetsPath, "Scenario", sce.SC.name, name);
UnityWebRequest www;
if (name.EndsWith(".wav"))
www = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.WAV);
else
www = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.MPEG);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogError("error loading audio - " + www.error);
clip = null;
yield break;
}
else
clip = DownloadHandlerAudioClip.GetContent(www);
ScAction newAct;
newAct = new ScAction(); newAct.type = "playAudio";
newAct.objName = "";
newAct.playAudio = new actPlayAudio();
newAct.playAudio.filename = name;
newAct.title = "Воспроизвести аудио ("+Math.Round(clip.length, 1)+"с) \"" + newAct.playAudio.filename + "\"";
sce.InsertAction(newAct);
}
//нет необходимости в доп. данных:
// public class actOpenPanel { }
// public class actSwitchTo3D { }
// public class actHighlightOn { }
// public class actHighlightOff { }
// public class actWaitForClick { }
// public class actHideText { }
}