diff --git a/Assets/Fungus/Editor/PageBoundsEditor.cs b/Assets/Fungus/Editor/PageBoundsEditor.cs new file mode 100644 index 00000000..e4f45024 --- /dev/null +++ b/Assets/Fungus/Editor/PageBoundsEditor.cs @@ -0,0 +1,98 @@ +using UnityEditor; +using UnityEngine; +using System.Collections; +using System.Collections.Generic; + +namespace Fungus +{ + [CanEditMultipleObjects] + [CustomEditor (typeof(PageBounds))] + public class PageBoundsEditor : Editor + { + void OnSceneGUI () + { + PageBounds t = target as PageBounds; + + // Render the parent view to help user position the page + Transform parent = t.transform.parent; + if (parent != null) + { + View view = parent.gameObject.GetComponent(); + if (view != null) + { + ViewEditor.DrawView(view); + } + } + + if (t.enabled) + { + EditPageBounds(); + } + + if (GUI.changed) + { + EditorUtility.SetDirty(target); + } + } + + void EditPageBounds() + { + PageBounds t = target as PageBounds; + Vector3 pos = t.transform.position; + + Vector3[] verts = new Vector3[4]; + verts[0] = new Vector3(pos.x + t.bounds.min.x, pos.y + t.bounds.min.y, 0); + verts[1] = new Vector3(pos.x + t.bounds.min.x, pos.y + t.bounds.max.y, 0); + verts[2] = new Vector3(pos.x + t.bounds.max.x, pos.y + t.bounds.max.y, 0); + verts[3] = new Vector3(pos.x + t.bounds.max.x, pos.y + t.bounds.min.y, 0); + + Handles.DrawSolidRectangleWithOutline(verts, new Color(1,1,1,0.2f), new Color(0,0,0,1)); + + for(int i = 0; i < 4; ++i) + { + Vector3 vert = verts[i]; + Vector3 newPos = Handles.FreeMoveHandle(vert, + Quaternion.identity, + HandleUtility.GetHandleSize(pos) * 0.1f, + Vector3.zero, + Handles.CubeCap); + newPos.z = 0; + verts[i] = newPos; + + if (vert != newPos) + { + switch(i) + { + case 0: + verts[1].x = newPos.x; + verts[3].y = newPos.y; + break; + case 1: + verts[0].x = newPos.x; + verts[2].y = newPos.y; + break; + case 2: + verts[3].x = newPos.x; + verts[1].y = newPos.y; + break; + case 3: + verts[2].x = newPos.x; + verts[0].y = newPos.y; + break; + } + break; + } + } + + Bounds newBounds = new Bounds(verts[0], Vector3.zero); + newBounds.Encapsulate(verts[1]); + newBounds.Encapsulate(verts[2]); + newBounds.Encapsulate(verts[3]); + + t.transform.position = newBounds.center; + newBounds.center = Vector3.zero; + + t.bounds = newBounds; + } + } +} diff --git a/Assets/Fungus/Editor/PageBoundsEditor.cs.meta b/Assets/Fungus/Editor/PageBoundsEditor.cs.meta new file mode 100644 index 00000000..49d26ab2 --- /dev/null +++ b/Assets/Fungus/Editor/PageBoundsEditor.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: dce33924cf6804b2c94d17784a6037d1 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Fungus/Prefabs/PageBounds.prefab b/Assets/Fungus/Prefabs/PageBounds.prefab new file mode 100644 index 00000000..47336e2f Binary files /dev/null and b/Assets/Fungus/Prefabs/PageBounds.prefab differ diff --git a/Assets/Fungus/Prefabs/PageBounds.prefab.meta b/Assets/Fungus/Prefabs/PageBounds.prefab.meta new file mode 100644 index 00000000..1b238304 --- /dev/null +++ b/Assets/Fungus/Prefabs/PageBounds.prefab.meta @@ -0,0 +1,4 @@ +fileFormatVersion: 2 +guid: fb9bec1d986874625a0d5844ea7dca74 +NativeFormatImporter: + userData: diff --git a/Assets/Fungus/Scripts/PageBounds.cs b/Assets/Fungus/Scripts/PageBounds.cs new file mode 100644 index 00000000..2c651fa5 --- /dev/null +++ b/Assets/Fungus/Scripts/PageBounds.cs @@ -0,0 +1,44 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus +{ + /** + * Defines a screen aligned rectangular area for setting Page bounds + */ + public class PageBounds : MonoBehaviour + { + /// Rectangular bounds used to display page text. + public Bounds bounds = new Bounds(Vector3.zero, new Vector3(0.25f, 0.25f, 0f)); + + /// Layout style to use for Page + public Page.Layout layout = Page.Layout.FullSize; + + /** + * Modifies the active Page to use a rect defined by the bounds and the current camera transform + */ + public void UpdatePageRect() + { + // Y increases down the screen in GUI space, so top left is rect origin + Vector3 topLeft = transform.position + bounds.center; + topLeft.x -= bounds.extents.x; + topLeft.y += bounds.extents.y; + + Vector3 bottomRight = transform.position + bounds.center; + bottomRight.x += bounds.extents.x; + bottomRight.y -= bounds.extents.y; + + Vector2 tl = Camera.main.WorldToScreenPoint(topLeft); + Vector2 br = Camera.main.WorldToScreenPoint(bottomRight); + + float x1 = (tl.x / Screen.width); + float y1 = 1f - (tl.y / Screen.height); + float x2 = (br.x / Screen.width); + float y2 = 1f - (br.y / Screen.height); + + Page page = Game.GetInstance().activePage; + page.SetPageRect(x1, y1, x2, y2); + page.layout = layout; + } + } +} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/PageBounds.cs.meta b/Assets/Fungus/Scripts/PageBounds.cs.meta new file mode 100644 index 00000000..6df14949 --- /dev/null +++ b/Assets/Fungus/Scripts/PageBounds.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 627f97c837bdf479aa14a90d8ea3a736 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: