From 4dbac33400029fbf6aaa3905a19af4cf14087bbf Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Thu, 17 Apr 2014 08:57:48 +0100 Subject: [PATCH] Added PageBounds script & prefab Changing Pages to not manage Bounds directly. --- Assets/Fungus/Editor/PageBoundsEditor.cs | 98 ++++++++++++++++++ Assets/Fungus/Editor/PageBoundsEditor.cs.meta | 8 ++ Assets/Fungus/Prefabs/PageBounds.prefab | Bin 0 -> 5984 bytes Assets/Fungus/Prefabs/PageBounds.prefab.meta | 4 + Assets/Fungus/Scripts/PageBounds.cs | 44 ++++++++ Assets/Fungus/Scripts/PageBounds.cs.meta | 8 ++ 6 files changed, 162 insertions(+) create mode 100644 Assets/Fungus/Editor/PageBoundsEditor.cs create mode 100644 Assets/Fungus/Editor/PageBoundsEditor.cs.meta create mode 100644 Assets/Fungus/Prefabs/PageBounds.prefab create mode 100644 Assets/Fungus/Prefabs/PageBounds.prefab.meta create mode 100644 Assets/Fungus/Scripts/PageBounds.cs create mode 100644 Assets/Fungus/Scripts/PageBounds.cs.meta 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 0000000000000000000000000000000000000000..47336e2f9d652b89021747af240c74aff19c2912 GIT binary pat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p%z*Et&pk|=gbOAa;8ZiNsARQ%$9 zVM9czJo+T!ghS9v4HsFMm9h?SqyyF<85cBp2+DlWx&zLLCMHhjQl*VUSn3o_iQ+7U zrLm>lmwT$?fl1jmGEs!QQ}4yBjATlu&`*9OH9Cn5(oB!0_ETH(<1#G&QEKQpCE`D&c1_<9gLMsiN3tarQ0pB@ znQoO8??~QsLV4uwnsGEUy*#hC(JKiQLvWRyBncG#4q)yiNuW-zw`~3|qRgIhDlZZx zd%Ec=YnANjgjlEe_l*C$%Q&q{pv6Kw?LL9ha;a3bH~DoDyJ)iZ;@cW2_ihxW#T)ka z`O-*dOb^5BB_T8`7~ZH6%JX5n>Wm*@pEp-YZyioXQ7`+y?JX&}nKkGEhPm^NwY8=? zYPc3>PO2e2J9I$?*XBPzt6#HsV5^wwcLsM)tkHmXsE)%17sji%@6NwX{^i_3UtuR?0oe6A1AKj*&`gZ$bCHF0rqV_duZ1kMHt{R^5% B$ejQH literal 0 HcmV?d00001 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: