summaryrefslogtreecommitdiff
path: root/Pathfinding Visualizer/Assets/CodeMonkey/Utils/UI_Bar.cs
blob: 3ae73a132aa89fccb0e9f4cf99ea12fbcf5c44c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* 
    ------------------- Code Monkey -------------------

    Thank you for downloading the Code Monkey Utilities
    I hope you find them useful in your projects
    If you have any questions use the contact form
    Cheers!

               unitycodemonkey.com
    --------------------------------------------------
 */

using System;
using UnityEngine;
using UnityEngine.UI;

namespace CodeMonkey.Utils {

    /*
     * Bar in the UI with scaleable Bar and Background
     * */
    public class UI_Bar {
        
        public GameObject gameObject;
        private RectTransform rectTransform;
        private RectTransform background;
        private RectTransform bar;
        private Vector2 size;
        
        /* 
         * Outline into for Bar
         * */
        public class Outline {
            public float size = 1f;
            public Color color = Color.black;
            public Outline(float size, Color color) {
                this.size = size;
                this.color = color;
            }
        }

        public UI_Bar(GameObject gameObject, RectTransform background, RectTransform bar) {
            this.gameObject = gameObject;
            rectTransform = gameObject.GetComponent<RectTransform>();
            this.background = background;
            this.bar = bar;
            size = background.GetComponent<RectTransform>().sizeDelta;
        }

        public UI_Bar(GameObject gameObject, Transform background, Transform bar) {
            this.gameObject = gameObject;
            rectTransform = gameObject.GetComponent<RectTransform>();
            this.background = background.GetComponent<RectTransform>();
            this.bar = bar.GetComponent<RectTransform>();
            size = background.GetComponent<RectTransform>().sizeDelta;
        }

        public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color barColor, float sizeRatio) {
            SetupParent(parent, anchoredPosition, size);
            SetupBar(barColor);
            SetSize(sizeRatio);
        }

        public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color barColor, float sizeRatio, Outline outline) {
            SetupParent(parent, anchoredPosition, size);
            if (outline != null) SetupOutline(outline, size);
            SetupBar(barColor);
            SetSize(sizeRatio);
        }

        public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color backgroundColor, Color barColor, float sizeRatio) {
            SetupParent(parent, anchoredPosition, size);
            SetupBackground(backgroundColor);
            SetupBar(barColor);
            SetSize(sizeRatio);
        }

        public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color backgroundColor, Color barColor, float sizeRatio, Outline outline) {
            SetupParent(parent, anchoredPosition, size);
            if (outline != null) SetupOutline(outline, size);
            SetupBackground(backgroundColor);
            SetupBar(barColor);
            SetSize(sizeRatio);
        }

        private void SetupParent(Transform parent, Vector2 anchoredPosition, Vector2 size) {
            this.size = size;
            gameObject = new GameObject("UI_Bar", typeof(RectTransform));
            rectTransform = gameObject.GetComponent<RectTransform>();
            rectTransform.SetParent(parent, false);
            rectTransform.sizeDelta = size;
            rectTransform.anchorMin = new Vector2(0, .5f);
            rectTransform.anchorMax = new Vector2(0, .5f);
            rectTransform.pivot = new Vector2(0, .5f);
            rectTransform.anchoredPosition = anchoredPosition;
        }

        private RectTransform SetupOutline(Outline outline, Vector2 size) {
            return UtilsClass.DrawSprite(outline.color, gameObject.transform, Vector2.zero, size + new Vector2(outline.size, outline.size), "Outline");
        }

        private void SetupBackground(Color backgroundColor) {
            background = UtilsClass.DrawSprite(backgroundColor, gameObject.transform, Vector2.zero, Vector2.zero, "Background");
            background.anchorMin = new Vector2(0,0);
            background.anchorMax = new Vector2(1,1);
        }

        private void SetupBar(Color barColor) {
            bar = UtilsClass.DrawSprite(barColor, gameObject.transform, Vector2.zero, Vector2.zero, "Bar");
            bar.anchorMin = new Vector2(0,0);
            bar.anchorMax = new Vector2(0,1f);
            bar.pivot = new Vector2(0,.5f);
        }

        public void SetSize(float sizeRatio) {
            bar.sizeDelta = new Vector2(sizeRatio * size.x, 0f);
        }

        public void SetColor(Color color) {
            bar.GetComponent<Image>().color = color;
        }

        public void SetActive(bool active) {
            gameObject.SetActive(active);
        }

        public Vector2 GetAnchoredPosition() {
            return rectTransform.anchoredPosition;
        }

        public Vector2 GetSize() {
            return rectTransform.sizeDelta;
        }

        public void AddOutline(Outline outline) {
            RectTransform outlineRectTransform = SetupOutline(outline, size);
            outlineRectTransform.transform.SetAsFirstSibling();
        }

        public void SetRaycastTarget(bool set) {
            foreach (Transform trans in gameObject.transform) {
                if (trans.GetComponent<Image>() != null) {
                    trans.GetComponent<Image>().raycastTarget = set;
                }
            }
        }

        public void DestroySelf() {
            UnityEngine.Object.Destroy(gameObject);
        }

        public Button_UI AddButton() {
            return AddButton(null, null, null);
        }

        public Button_UI AddButton(Action ClickFunc, Action MouseOverOnceFunc, Action MouseOutOnceFunc) {
            Button_UI buttonUI = gameObject.AddComponent<Button_UI>();
            if (ClickFunc != null)
                buttonUI.ClickFunc = ClickFunc;
            if (MouseOverOnceFunc != null)
                buttonUI.MouseOverOnceFunc = MouseOverOnceFunc;
            if (MouseOutOnceFunc != null)
                buttonUI.MouseOutOnceFunc = MouseOutOnceFunc;
            return buttonUI;
        }

    }


}