chrisgregan
11 years ago
6 changed files with 162 additions and 0 deletions
@ -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<View>(); |
||||||
|
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; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,8 @@ |
|||||||
|
fileFormatVersion: 2 |
||||||
|
guid: dce33924cf6804b2c94d17784a6037d1 |
||||||
|
MonoImporter: |
||||||
|
serializedVersion: 2 |
||||||
|
defaultReferences: [] |
||||||
|
executionOrder: 0 |
||||||
|
icon: {instanceID: 0} |
||||||
|
userData: |
Binary file not shown.
@ -0,0 +1,4 @@ |
|||||||
|
fileFormatVersion: 2 |
||||||
|
guid: fb9bec1d986874625a0d5844ea7dca74 |
||||||
|
NativeFormatImporter: |
||||||
|
userData: |
@ -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; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue