summaryrefslogtreecommitdiff
path: root/Pathfinding Visualizer/Assets/CodeMonkey/CMDebug.cs
blob: 76d60380a3cc39fcbe2cd0c920ecfb21e0bba43c (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
171
172
173
/* 
    ------------------- 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 CodeMonkey.Utils;

namespace CodeMonkey {

    /*
     * Debug Class with various helper functions to quickly create buttons, text, etc
     * */
    public static class CMDebug {

        // Creates a Button in the World
        public static World_Sprite Button(Transform parent, Vector3 localPosition, string text, System.Action ClickFunc, int fontSize = 30, float paddingX = 5, float paddingY = 5) {
            return World_Sprite.CreateDebugButton(parent, localPosition, text, ClickFunc, fontSize, paddingX, paddingY);
        }

        // Creates a Button in the UI
        public static UI_Sprite ButtonUI(Vector2 anchoredPosition, string text, Action ClickFunc) {
            return UI_Sprite.CreateDebugButton(anchoredPosition, text, ClickFunc);
        }

        public static UI_Sprite ButtonUI(Transform parent, Vector2 anchoredPosition, string text, Action ClickFunc) {
            return UI_Sprite.CreateDebugButton(parent, anchoredPosition, text, ClickFunc);
        }

        // Creates a World Text object at the world position
        public static void Text(string text, Vector3 localPosition = default(Vector3), Transform parent = null, int fontSize = 40, Color? color = null, TextAnchor textAnchor = TextAnchor.UpperLeft, TextAlignment textAlignment = TextAlignment.Left, int sortingOrder = UtilsClass.sortingOrderDefault) {
            UtilsClass.CreateWorldText(text, parent, localPosition, fontSize, color, textAnchor, textAlignment, sortingOrder);
        }
        
        // World text pop up at mouse position
        public static void TextPopupMouse(string text, Vector3? offset = null) {
            if (offset == null) {
                offset = Vector3.one;
            }
            UtilsClass.CreateWorldTextPopup(text, UtilsClass.GetMouseWorldPosition() + (Vector3)offset);
        }

        // World text pop up at mouse position
        public static void TextPopupMouse(object obj, Vector3? offset = null) {
            TextPopupMouse(obj.ToString(), offset);
        }

        // Creates a Text pop up at the world position
        public static void TextPopup(string text, Vector3 position, float popupTime = 1f) {
            UtilsClass.CreateWorldTextPopup(text, position, popupTime);
        }

        // Text Updater in World, (parent == null) = world position
        public static FunctionUpdater TextUpdater(Func<string> GetTextFunc, Vector3 localPosition, Transform parent = null, int fontSize = 40, Color? color = null, TextAnchor textAnchor = TextAnchor.UpperLeft, TextAlignment textAlignment = TextAlignment.Left, int sortingOrder = UtilsClass.sortingOrderDefault) {
            return UtilsClass.CreateWorldTextUpdater(GetTextFunc, localPosition, parent, fontSize, color, textAnchor, textAlignment, sortingOrder);
        }

        // Text Updater in UI
        public static FunctionUpdater TextUpdaterUI(Func<string> GetTextFunc, Vector2 anchoredPosition) {
            return UtilsClass.CreateUITextUpdater(GetTextFunc, anchoredPosition);
        }

        // Text Updater always following mouse
        public static void MouseTextUpdater(Func<string> GetTextFunc, Vector3 positionOffset = default(Vector3)) {
            GameObject gameObject = new GameObject();
            FunctionUpdater.Create(() => {
                gameObject.transform.position = UtilsClass.GetMouseWorldPosition() + positionOffset;
                return false;
            });
            TextUpdater(GetTextFunc, Vector3.zero, gameObject.transform);
        }

        // Trigger Action on Key
        public static FunctionUpdater KeyCodeAction(KeyCode keyCode, Action onKeyDown) {
            return UtilsClass.CreateKeyCodeAction(keyCode, onKeyDown);
        }
        


        // Debug DrawLine to draw a projectile, turn Gizmos On
        public static void DebugProjectile(Vector3 from, Vector3 to, float speed, float projectileSize) {
            Vector3 dir = (to - from).normalized;
            Vector3 pos = from;
            FunctionUpdater.Create(() => {
                Debug.DrawLine(pos, pos + dir * projectileSize);
                float distanceBefore = Vector3.Distance(pos, to);
                pos += dir * speed * Time.deltaTime;
                float distanceAfter = Vector3.Distance(pos, to);
                if (distanceBefore < distanceAfter) {
                    return true;
                }
                return false;
            });
        }



        public static void SpritePopupMouse(Sprite sprite, float scale = 1f) {
            SpritePopup(UtilsClass.GetMouseWorldPosition(), sprite, scale);
        }


        public static void SpritePopup(Vector3 position, Sprite sprite, float scale) {
            float popupTime = 1f;
            GameObject gameObject = DrawSpriteTimedAlpha(position, sprite, scale, popupTime);

            Vector3 finalPopupPosition = position + new Vector3(0, 1, 0) * 20f;
            Transform transform = gameObject.transform;
            Vector3 moveAmount = (finalPopupPosition - position) / popupTime;

            FunctionUpdater.Create(delegate () {
                if (gameObject == null) {
                    return true;
                }
                transform.position += moveAmount * Time.unscaledDeltaTime;
                return false;
            }, "SpritePopup");
        }

        public static GameObject DrawSpriteTimed(Sprite sprite, float scale, float timer) {
            return DrawSpriteTimed(UtilsClass.GetMouseWorldPosition(), sprite, scale, timer);
        }

        public static GameObject DrawSpriteTimed(Vector3 position, Sprite sprite, float scale, float timer) {
            GameObject gameObject = new GameObject("SpriteTimed", typeof(SpriteRenderer));
            gameObject.transform.position = position;
            gameObject.transform.localScale = Vector3.one * scale;
            gameObject.GetComponent<SpriteRenderer>().sprite = sprite;

            GameObject.Destroy(gameObject, timer);

            return gameObject;
        }

        public static GameObject DrawSpriteTimedAlpha(Sprite sprite, float scale, float timer, float startDecayTimeNormalized = .8f) {
            return DrawSpriteTimedAlpha(UtilsClass.GetMouseWorldPosition(), sprite, scale, timer, startDecayTimeNormalized);
        }

        public static GameObject DrawSpriteTimedAlpha(Vector3 position, Sprite sprite, float scale, float timer, float startDecayTimeNormalized = .8f) {
            GameObject gameObject = DrawSpriteTimed(position, sprite, scale, timer);

            SpriteRenderer spriteRenderer = gameObject.GetComponent<SpriteRenderer>();

            float startAlphaDecayTime = timer * startDecayTimeNormalized;
            float totalAlphaDecayTime = timer - startAlphaDecayTime;
            float currentTime = 0f;

            FunctionUpdater.Create(() => {
                if (gameObject == null) {
                    return true;
                }
                currentTime += Time.unscaledDeltaTime;
                if (currentTime >= startAlphaDecayTime) {
                    spriteRenderer.color = new Color(1, 1, 1, Mathf.Lerp(1f, 0f, 1 - ((timer - currentTime) / totalAlphaDecayTime)));
                }
                return false;
            });

            return gameObject;
        }


    }

}