Tanoda
GuiRectUtil.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10#if UNITY_EDITOR
11using UnityEditor;
12#endif
13
14namespace Leap.Unity {
15
16 public static class GuiRectUtil {
17
18 public static Vector3 Corner00(this Rect rect) {
19 return new Vector3(rect.x, rect.y);
20 }
21
22 public static Vector3 Corner10(this Rect rect) {
23 return new Vector3(rect.x + rect.width, rect.y);
24 }
25
26 public static Vector3 Corner01(this Rect rect) {
27 return new Vector3(rect.x, rect.y + rect.height);
28 }
29
30 public static Vector3 Corner11(this Rect rect) {
31 return new Vector3(rect.x + rect.width, rect.y + rect.height);
32 }
33
34 public static Rect Encapsulate(this Rect rect, Vector2 point) {
35 if (point.x < rect.x) {
36 rect.width += rect.x - point.x;
37 rect.x = point.x;
38 } else if (point.x > rect.x + rect.width) {
39 rect.width = point.x - rect.x;
40 }
41
42 if (point.y < rect.y) {
43 rect.height += rect.y - point.y;
44 rect.y = point.y;
45 } else if (point.y > rect.y + rect.height) {
46 rect.height = point.y - rect.y;
47 }
48
49 return rect;
50 }
51
52 public static void SplitHorizontally(this Rect rect, out Rect left, out Rect right) {
53 left = rect;
54 left.width /= 2;
55 right = left;
56 right.x += right.width;
57 }
58
59 public static void SplitHorizontallyWithRight(this Rect rect, out Rect left, out Rect right, float rightWidth) {
60 left = rect;
61 left.width -= rightWidth;
62 right = left;
63 right.x += right.width;
64 right.width = rightWidth;
65 }
66
67 public static Rect NextLine(this Rect rect) {
68 rect.y += rect.height;
69 return rect;
70 }
71
72 public static Rect FromRight(this Rect rect, float width) {
73 rect.x = rect.width - width;
74 rect.width = width;
75 return rect;
76 }
77
78#if UNITY_EDITOR
79 public static Rect SingleLine(this Rect rect) {
80 rect.height = EditorGUIUtility.singleLineHeight;
81 return rect;
82 }
83
84 public static Rect Indent(this Rect rect) {
85 rect.x += EditorGUIUtility.singleLineHeight;
86 rect.width -= EditorGUIUtility.singleLineHeight;
87 return rect;
88 }
89#endif
90 }
91}