using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace GamePhone {
[RequireComponent(typeof(BoxCollider2D))]
public class Division : MonoBehaviour {
public Bounds bounds;
public bool encapsulate;
private Division parent;
// Used to safely resize a division
public virtual void Resize (float width, float height) {
bounds.size = new Vector3(width, height, 0);
CheckEncapsulation (this);
}
// Use this for initialization
protected virtual void Start() {
GetComponent<BoxCollider2D>().size = bounds.size;
if (encapsulate) {
foreach (Division div in GetComponentsInChildren<Division> ()) {
CheckEncapsulation (div);
}
}
}
// Check if div is still encapsulated by parent
public void CheckEncapsulation (Division div) {
if (encapsulate) {
bounds.Encapsulate (div.bounds.min);
bounds.Encapsulate (div.bounds.max);
}
if (!parent) {
parent = transform.parent.GetComponentInParent<Division>();
}
if (parent) {
parent.CheckEncapsulation (div);
}
}
// Used to safely resize a division
public virtual void Resize (Bounds newBounds) {
bounds = newBounds;
CheckEncapsulation (this);
}
// Gets called when the user touches this division
public virtual void OnMouseSelect () {
//Debug.Log (name + " Select");
}
// Gets called when the user resleases this division
public virtual void OnMouseRelease () {
//Debug.Log (name + " Release");
}
// Gets called when the user clicks on this division
public virtual void OnMouseClick () {
//Debug.Log (name + " Click");
}
// Gets called every frame (after the treshold) that the user holds this division
public virtual void OnMouseHold () {
//Debug.Log (name + " Hold");
}
// Gets called every frame the division is dragged
public virtual void OnMouseDrag (Vector3 velocity) {
//Debug.Log (name + " Dragging with velocity " + velocity);
}
// Returns if this division is allowed to drag
public virtual bool CanDrag (Vector3 velocity) {
return false;
}
// Returns width of the parent division, or width of the screen if it does not have a parent division
protected Bounds parentBounds {
get {
if (transform.parent) {
if (!parent) {
parent = transform.parent.GetComponentInParent<Division>();
}
if (parent) {
return parent.bounds;
}
}
return new Bounds(Vector3.zero, UnityEngine.Screen.safeArea.size);
}
}
// Draw the outline of this division inside the editor
private void OnDrawGizmos() {
Gizmos.color = Color.white;
Gizmos.DrawLine(transform.TransformPoint(bounds.min), transform.TransformPoint(bounds.min + Vector3.up * bounds.size.y));
Gizmos.DrawLine(transform.TransformPoint(bounds.min), transform.TransformPoint(bounds.min + Vector3.right * bounds.size.x));
Gizmos.DrawLine(transform.TransformPoint(bounds.max), transform.TransformPoint(bounds.min + Vector3.up * bounds.size.y));
Gizmos.DrawLine(transform.TransformPoint(bounds.max), transform.TransformPoint(bounds.min + Vector3.right * bounds.size.x));
}
// Draw the outline of this division inside the editor when selected
private void OnDrawGizmosSelected() {
Gizmos.color = Color.yellow;
Gizmos.DrawLine(transform.TransformPoint(bounds.min), transform.TransformPoint(bounds.min + Vector3.up * bounds.size.y));
Gizmos.DrawLine(transform.TransformPoint(bounds.min), transform.TransformPoint(bounds.min + Vector3.right * bounds.size.x));
Gizmos.DrawLine(transform.TransformPoint(bounds.max), transform.TransformPoint(bounds.min + Vector3.up * bounds.size.y));
Gizmos.DrawLine(transform.TransformPoint(bounds.max), transform.TransformPoint(bounds.min + Vector3.right * bounds.size.x));
}
}
}