using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
///
/// Редактор
///
public class EditorController : MonoBehaviour
{
public static EditorController instance;
public InputField Name;
public InputField Image;
public InputField SizeX;
public InputField SizeY;
public GameObject BeaconsContent;
public GameObject PanelBeaconEdit;
public GameObject Player;
public GameObject ButtonMode;
public GameObject EditorTools;
public Toggle ToggleScalePanel;
public Dropdown DropdownLocation;
public Transform Locations;
public Button ButtonAddBeacon;
public GameObject Dialog;
public GameObject NewLocation;
public GameObject plane;
public Location newLocation;
public Dictionary> Beacons = new Dictionary>(); // маяки локации
CompanyController company;
ModeController mode;
ToolsController tools;
public Location EditLocation;
private void Awake()
{
instance = this;
}
// Start is called before the first frame update
void Start()
{
company = CompanyController.instance;
mode = ButtonMode.GetComponent();
tools = EditorTools.GetComponent();
ButtonAddBeacon.onClick.AddListener(() => {
AddBeacon(company);
});
var random = new System.Random();
int number = random.Next(2000000000, int.MaxValue);
uint uintNumber = (uint)(number + (uint)int.MaxValue);
if (NewLocation == null)
{
NewLocation = new GameObject();
NewLocation.transform.SetParent(Locations);
NewLocation.name = "NewLocation";
NewLocation.transform.position = Vector3.zero;
NewLocation.SetActive(false);
plane = Instantiate(Resources.Load("GameObjects/Plane", typeof(GameObject))) as GameObject;
plane.transform.SetParent(NewLocation.transform);
plane.transform.position = new Vector3(plane.transform.localScale.x * 5, 0, plane.transform.localScale.z * 5);
foreach (var g in Location.contents)
{
var go = new GameObject();
go.name = g;
go.transform.SetParent(NewLocation.transform);
}
newLocation = new Location
{
id = uintNumber,
company_id=(uint)company.active_company,
location = NewLocation,
walls = new List(),
zones = new List(),
beacons = new Dictionary(),
plane = plane,
texture_url = ""
};
company.locations[newLocation.id] = newLocation;
company.locations_index.Add(newLocation.id);
DropdownLocation.options.Add(new Dropdown.OptionData("Новая локация"));
DropdownLocation.RefreshShownValue();
}
}
// Update is called once per frame
void Update()
{
//if (company.active_location >= 0 && company.locations != null)
//{
// var l = company.locations[company.locations_index[company.active_location]];
// l.plane.GetComponent().Resize = ToggleScalePanel.isOn;
//}
}
///
/// Изменение размеров панели
/// Загрузка текстуры
///
public void UpdatePanel()
{
var p = company.locations[company.locations_index[company.active_location]].plane;
//if (!Image.text.Equals(texture_url))
//{
StartCoroutine(SendingFormController.LoadImage(Image.text, p, Vector3.zero, Vector3.zero, LoadTexture));
//texture_url = Image.text;
//}
}
public void UpdatePanelSize()
{
var p = company.locations[company.locations_index[company.active_location]].plane;
var x = 1f;
if (!SizeX.text.Equals(""))
x = float.Parse(SizeX.text, CultureInfo.InvariantCulture);
var y = 1f;
if (!SizeY.text.Equals(""))
y = float.Parse(SizeY.text, CultureInfo.InvariantCulture);
p.transform.localScale = new Vector3(x, 1, y);
var scale = p.transform.localScale;
p.transform.position = new Vector3(scale.x * 5, 0, scale.z * 5);
}
//public void LoadTexture(Texture2D texture)
//{
// var p = company.locations[company.locations_index[company.active_location]].plane;
// Renderer renderer = p.GetComponent();
// renderer.material.mainTexture = texture;
//}
public void LoadTexture(float x, float y)
{
SizeX.text = x.ToString();
SizeY.text = y.ToString();
}
public void Save()
{
//SceneManager.LoadScene("Location");
tools.CursorOff();
if (!Name.text.Equals(""))
{
//var location = new Location { id = 0, name = Name.text, beacons = Beacons, zones = WallCreate.Zones, walls = WallCreate.Walls, texture_url = Image.text, plane = plane, location = NewLocation};
Location location;
if (EditLocation.id != 0) location = EditLocation;
else location = new Location { id = Convert.ToUInt32(4000000000), plane = plane, location = NewLocation, company_id = (uint)company.active_company };
location.name = Name.text;
location.texture_url = Image.text;
if (WallCreate.Walls.ContainsKey(EditLocation.id)) location.walls.AddRange(WallCreate.Walls[EditLocation.id]);
//if (WallCreate.Zones.ContainsKey(EditLocation.id)) location.zones.AddRange(WallCreate.Zones[EditLocation.id]);
if (WallCreate.Zones.ContainsKey(EditLocation.id)) location.zones = WallCreate.Zones[EditLocation.id];
if (Beacons.ContainsKey(EditLocation.id))
{
foreach(var b in Beacons[EditLocation.id].Values)
{
var go = b.beacon;
b.x = go.transform.position.x;
b.y = go.transform.position.z;
b.z = go.transform.position.y;
if (location.beacons.ContainsKey(b.id))
location.beacons[b.id] = b;
}
//location.beacons.Add(Beacons[EditLocation.id]);
}
company.DropdownLocation.options.Last().text = location.name;
location.location.name = location.name;
location.Save();
location.SaveContents();
mode.Switch();
}
else
{
Dialog.SetActive(true);
Dialog.transform.GetChild(0).GetComponent().text = "Введите название локации";
Dialog.transform.GetChild(2).GetComponent