using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// Размещение маяков
/// Используется в редакторе и в плеере
///
public class BeaconController : MonoBehaviour
{
public float Height = 0.5f;
public int Rounding = 1;
//public bool move = false;
public bool info = false; // вывод таблицы с информацией
public enum Mode { Player, Editor};
public Mode mode = Mode.Player;
public string text_info;
public string id;
Text text;
GameObject canvas;
private Vector3 screenPoint;
private Vector3 offset;
private Vector3 curScreenPoint;
private Vector3 curPosition;
// Start is called before the first frame update
void Start()
{
text = transform.GetChild(0).GetChild(0).GetChild(0).GetComponent();
text.text = text_info;
canvas = transform.GetChild(0).gameObject;
canvas.SetActive(info);
}
// Update is called once per frame
void Update()
{
if (info != transform.GetChild(0).gameObject.activeSelf)
{
canvas.SetActive(info);
}
}
void OnMouseDown()
{
if(mode == Mode.Player) info = !info;
//Debug.Log(StagesEditorController.indexCursor);
if (mode == Mode.Editor)
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
info = true;
text.gameObject.GetComponent().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 100);
text.gameObject.GetComponent().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 15);
transform.GetComponent().enabled = false;
}
GetComponent().enabled = info;
}
// Перемещение
void OnMouseDrag()
{
if (mode == Mode.Editor)
{
curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
var x =(float) Math.Round(curPosition.x, Rounding);
var z = (float)Math.Round(curPosition.z, Rounding);
transform.position = new Vector3(x, Height, z);
text.text = $"id={id} x={x} y={z}";
canvas.transform.GetChild(0).transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y+20, screenPoint.z);
}
}
private void OnMouseUp()
{
if (mode == Mode.Editor)
{
info = false;
text.text = text_info;
text.gameObject.GetComponent().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 120);
text.gameObject.GetComponent().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 75);
transform.GetComponent().enabled = true;
}
}
///
/// Информационная панель над маяком
///
///
public void InformationPanel(Beacon b)
{
text_info = $"{b.id}\n{b.vagrant_label}\nuuid={b.uuid}\nmajor={b.major}\nminor={b.minor}\nmac={b.mac}";
}
public void BeaconEnabled(Beacon b, bool enabled)
{
b.enabled = enabled;
if (b.enabled == false) GetComponent().material.color = Color.grey;
else GetComponent().material.color = Color.red;
}
}