Example #1
0
        /// <summary>
        /// Create a new text reference that will remember the reference to a value in
        /// the program. Refer to the object, not to the ToString of the object. The
        /// TextRef object will make sure numbers are properly formatted.
        /// </summary>
        /// <param name="reference"></param>
        /// <returns></returns>
        public static TextRef Create(Func <object> reference)
        {
            TextRef tr = new TextRef()
            {
                script  = reference,
                refType = RefType.reference
            };

            return(tr);
        }
Example #2
0
        private void CreateTitle()
        {
            TextBox       titleTxt = new TextBox(go.transform, TextRef.Create(title), (int)(fontSize * 1.2f), TextAnchor.MiddleCenter);
            LayoutElement LayEl    = titleTxt.gameObject.AddComponent <LayoutElement>();

            LayEl.minHeight       = fontSize * 1.2f;
            LayEl.preferredHeight = fontSize * 2 * 1.2f;
            LayEl.flexibleWidth   = 1;
            LayEl.flexibleHeight  = 0;
        }
Example #3
0
        InfoTable(Transform parent, int width = 200, int fontSize = 12, TextRef title = null)
        {
            this.title    = title;
            this.fontSize = fontSize;
            go            = new GameObject("Info Table", typeof(RectTransform));
            go.transform.SetParent(parent, false);
            ((RectTransform)go.transform).sizeDelta = new Vector2(width, 100);
            VerticalLayoutGroup VLayGr = go.AddComponent <VerticalLayoutGroup>();

            VLayGr.childForceExpandHeight = false;
            VLayGr.childForceExpandWidth  = false;
        }
Example #4
0
 protected void BaseRedraw(List <List <TextRef> > info, int numberOfCol)
 {
     for (int i = go.transform.childCount - 1; i >= 0; i--)      // Kill all previous lines
     {
         UnityEngine.Object.Destroy(go.transform.GetChild(i).gameObject);
         go.transform.GetChild(i).SetParent(null);
     }
     if (title != null)
     {
         CreateTitle();
     }
     if (info.Count == 0)        // no data - place a dummy line
     {
         GameObject line = CreateLine();
         for (int i = 0; i < numberOfCol; i++)
         {
             GameObject dataCont = new GameObject("Data Container", typeof(RectTransform));
             dataCont.transform.SetParent(line.transform);
             TextBox data = new TextBox(dataCont.transform, TextRef.Create("#####", false), fontSize, i == 0 ? TextAnchor.MiddleLeft : TextAnchor.MiddleRight);
             if (i == 0)
             {
                 dataCont.AddComponent <LayoutElement>().flexibleWidth = 1;
             }
         }
     }
     else
     {
         for (int i = 0; i < info.Count; i++)
         {
             GameObject line = CreateLine();
             for (int j = 0; j < numberOfCol; j++)
             {
                 GameObject dataCont = new GameObject("Data Container", typeof(RectTransform));
                 dataCont.transform.SetParent(line.transform);
                 dataCont.AddComponent <LayoutElement>();
                 if (widths == null)
                 {
                     ((RectTransform)dataCont.transform).sizeDelta = new Vector2(((RectTransform)line.transform.parent).rect.width / numberOfCol, 50);
                 }
                 else
                 {
                     ((RectTransform)dataCont.transform).sizeDelta          = new Vector2(widths[j], fontSize + 4);
                     dataCont.GetComponent <LayoutElement>().preferredWidth = widths[j];
                 }
                 TextBox data = new TextBox(dataCont.transform, info[i][j], fontSize, j == 0 ? TextAnchor.MiddleLeft : TextAnchor.MiddleRight);
                 if (j == 0)
                 {
                     dataCont.GetComponent <LayoutElement>().flexibleWidth = 1;
                 }
             }
         }
     }
 }
Example #5
0
        /// <summary>
        /// Create a new text reference that will remember the reference to a value in
        /// the program. Refer to the object, not to the ToString of the object. The
        /// TextRef object will make sure numbers are properly formatted. This version
        /// of Create can also link an alternative text.
        /// </summary>
        /// <param name="reference"></param>
        /// <param name="altText"></param>
        /// <returns></returns>
        public static TextRef Create(Func <object> reference, string altText)
        {
            TextRef tr = new TextRef()
            {
                script     = reference,
                text2nd    = altText,
                refType    = RefType.reference,
                refType2nd = RefType.localised
            };

            return(tr);
        }
Example #6
0
        /// <summary>
        /// Create a new text reference that can store a string which can be read from the localisation files.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="localised">Whether or not this value is a key in the localisation files.</param>
        /// <returns></returns>
        public static TextRef Create(string text, bool localised = true)
        {
            TextRef tr = new TextRef();

            tr.text = text;
            if (localised)
            {
                tr.refType = RefType.localised;
            }
            else
            {
                tr.refType = RefType.direct;
            }
            return(tr);
        }
Example #7
0
 private void AddHeaders()
 {
     if (headers != null && headers.Count == colms)
     {
         List <TextRef> blank = new List <TextRef>()
         {
             TextRef.Create("")
         };
         info.Insert(0, blank.Concat(headers).ToList());
     }
     else if (headers != null && headers.Count == colms + 1)
     {
         info.Insert(0, headers);
     }
     else if (headers != null)
     {
         throw new ArgumentException("The headers of this infotable do not have a valid count. Use 'number of colums' or 'number of colums + 1'");
     }
 }
Example #8
0
        /// <summary>
        /// Create a new text reference that can store a string and an alternative string which can be read
        /// from the localisation files.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="altRef">This is mainly used for mouseover text.</param>
        /// <param name="localised">Whether or not this value is a key in the localisation files.</param>
        /// <returns></returns>
        public static TextRef Create(string text, Func <object> altRef, bool localised = true)
        {
            TextRef tr = new TextRef()
            {
                text      = text,
                script2nd = altRef
            };

            if (localised)
            {
                tr.refType = RefType.localised;
            }
            else
            {
                tr.refType = RefType.direct;
            }
            tr.refType2nd = RefType.reference;
            return(tr);
        }
Example #9
0
        /// <summary>
        /// Create a new text reference that can store a string and an alternative string which can be read
        /// from the localisation files.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="altText">This is mainly used for mouseover text.</param>
        /// <param name="localised">Whether or not this value is a key in the localisation files.</param>
        /// <returns></returns>
        public static TextRef Create(string text, string altText, bool localised = true)
        {
            TextRef tr = new TextRef()
            {
                text    = text,
                text2nd = altText
            };

            if (localised)
            {
                tr.refType = RefType.localised;
            }
            else
            {
                tr.refType = RefType.direct;
            }
            tr.refType2nd = RefType.localised;
            return(tr);
        }
Example #10
0
 /// <summary>
 ///
 /// Use this constructor if you want to make a multi column table where the number of elements is variable
 /// and where the data in the table is remebered, for interactable tables
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="parent"></param>
 /// <param name="listScript">This script creates the data in the table</param>
 /// <param name="lineScript">This function transforms the data entries into a line of TextRefs</param>
 /// <param name="width"></param>
 /// <param name="headers"></param>
 /// <param name="fontSize"></param>
 /// <param name="title"></param>
 /// <returns></returns>
 static public InfoTable Create <T>(Transform parent, Func <List <T> > listScript, Func <T, List <TextRef> > lineScript, int width = 200, List <TextRef> headers = null, int fontSize = 12, TextRef title = null)
 {
     return(new MultiColumnActiveMemory <T>(parent, listScript, lineScript, width, fontSize, title, headers));
 }
Example #11
0
            /// <summary>
            /// Use this constructor if the number of elements in the table is variable.
            /// BEWARE: this is badly optimised at the moment.
            /// </summary>
            /// <param name="parent"></param>
            /// <param name="script">This function must return the list with entries. The tuples in this list are the entries. The second item is the ToString() of whatever object is returned by the second function.</param>
            /// <param name="width"></param>
            /// <param name="fontSize"></param>
            /// <param name="title"></param>
            public MultiColumnActiveMemory(Transform parent, Func <List <T> > listScript, Func <T, List <TextRef> > lineScript, int width, int fontSize, TextRef title, List <TextRef> headers) :
                base(parent, width, fontSize, title)
            {
                info = null;
                ActiveInfoTable ait = go.AddComponent <ActiveInfoTable>();

                ait.parent      = this;
                this.listScript = listScript;
                this.lineScript = lineScript;
                this.headers    = headers;
                Redraw();
            }
Example #12
0
 /// <summary>
 /// Use this constructor if you want to make a multi column table where the number of elements is variable
 /// BEWARE: this is badly optimised at the moment.
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="script">This function must return the list with entries. The tuples in this list are the entries. The entries of the second List should return an object on which ToString() will be called.</param>
 /// <param name="width"></param>
 /// <param name="fontSize"></param>
 /// <param name="title"></param>
 static public InfoTable Create(Transform parent, Func <List <List <TextRef> > > script, int width = 200, List <TextRef> headers = null, int fontSize = 12, TextRef title = null)
 {
     return(new MultiColumnActive(parent, script, width, fontSize, title, headers));
 }
Example #13
0
 /// <summary>
 /// Use this constructor if you want to make a multi column table where the number of elements is fixed
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="info">The tuples in this list are the entries. The second item is the ToString() of whatever object is returned by the second function.</param>
 /// <param name="width"></param>
 /// <param name="fontSize"></param>
 /// <param name="title"></param>
 public MultiColumnPassiveMemory(Transform parent, List <T> dataList, Func <T, List <TextRef> > lineScript, int width, int fontSize, TextRef title, List <TextRef> headers) :
     base(parent, width, fontSize, title)
 {
     if (dataList == null || dataList.Count == 0)
     {
         throw new ArgumentException("The info of this infotable is empty. Please don't do this to me.");
     }
     colms         = info[0].Count;
     this.dataList = dataList;
     info          = dataList.ConvertAll(line => lineScript(line));
     this.headers  = headers;
     AddHeaders();
     Redraw();
 }
Example #14
0
 /// <summary>
 /// Use this constructor if you want to make a 2 column table where the number of elements is fixed
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="info">The tuples in this list are the entries. The second item is the ToString() of whatever object is returned by the second function.</param>
 /// <param name="width"></param>
 /// <param name="fontSize"></param>
 /// <param name="title"></param>
 public MultiColumnPassive(Transform parent, List <List <TextRef> > info, int width, int fontSize, TextRef title, List <TextRef> headers) :
     base(parent, width, fontSize, title)
 {
     if (info == null || info.Count == 0)
     {
         throw new ArgumentException("The info of this infotable is empty. Please don't do this to me.");
     }
     colms = info[0].Count;
     for (int i = 1; i < info.Count; i++)
     {
         if (info[i].Count != colms)
         {
             throw new ArgumentException("The info of this infotable has an inconsistant number of colums.");
         }
     }
     this.info    = info;
     this.headers = headers;
     AddHeaders();
     Redraw();
 }
Example #15
0
        public TextBox(Transform parent, TextRef Text, int size = 12, TextAnchor allignment = TextAnchor.MiddleLeft, Color?color = null)
        {
            this.Text = Text;
            go        = new GameObject(Text, typeof(RectTransform));
            go.transform.SetParent(parent, false);
            RectTransform tr = (RectTransform)go.transform;
            float         anchX = 0; float anchY = 0;

            switch (allignment)
            {
            case TextAnchor.LowerLeft: anchX = 0; anchY = 0; break;

            case TextAnchor.MiddleLeft: anchX = 0; anchY = 0.5f; break;

            case TextAnchor.UpperLeft: anchX = 0; anchY = 1; break;

            case TextAnchor.LowerCenter: anchX = 0.5f; anchY = 0; break;

            case TextAnchor.MiddleCenter: anchX = 0.5f; anchY = 0.5f; break;

            case TextAnchor.UpperCenter: anchX = 0.5f; anchY = 1; break;

            case TextAnchor.LowerRight: anchX = 1; anchY = 0; break;

            case TextAnchor.MiddleRight: anchX = 1; anchY = 0.5f; break;

            case TextAnchor.UpperRight: anchX = 1; anchY = 1; break;
            }
            tr.anchorMin        = new Vector2(anchX, anchY);
            tr.anchorMax        = new Vector2(anchX, anchY);
            tr.pivot            = new Vector2(anchX, anchY);
            tr.anchoredPosition = new Vector2(0, 0);

            if (Text.link == null)      // Just text
            {
                text                    = go.AddComponent <Text>();
                text.font               = Graphics.GetStandardFont();
                text.fontSize           = size * SCALING_FACTOR;
                text.alignment          = allignment;
                text.horizontalOverflow = HorizontalWrapMode.Overflow;
                text.verticalOverflow   = VerticalWrapMode.Overflow;
                TextBoxScript tbs = go.AddComponent <TextBoxScript>();
                tbs.parent       = this;
                tbs.hasMouseover = Text.AltText != null;
                text.text        = Text;
                text.color       = color ?? Graphics.Color_.text;

                tr.localScale = new Vector3(1f / SCALING_FACTOR, 1f / SCALING_FACTOR, 1);
                tr.sizeDelta  = new Vector2(text.preferredWidth / SCALING_FACTOR + size, (size + 2)) * SCALING_FACTOR;
            }
            else                        // Make a button
            {
                Image img = go.AddComponent <Image>();
                img.sprite = Graphics.GetSprite("tb_button_bg");
                img.type   = Image.Type.Sliced;
                Button but = go.AddComponent <Button>();
                but.onClick.AddListener(() => Text.link());

                GameObject sGo = new GameObject(Text, typeof(RectTransform));
                sGo.transform.SetParent(tr, false);
                RectTransform sTr = (RectTransform)sGo.transform;

                sTr.anchorMin = new Vector2(anchX, anchY);
                sTr.anchorMax = new Vector2(anchX, anchY);
                sTr.pivot     = new Vector2(anchX, anchY);
                switch (allignment)
                {
                case TextAnchor.LowerLeft:
                case TextAnchor.MiddleLeft:
                case TextAnchor.UpperLeft: sTr.anchoredPosition = new Vector2(4, 0); break;

                case TextAnchor.LowerCenter:
                case TextAnchor.MiddleCenter:
                case TextAnchor.UpperCenter: sTr.anchoredPosition = new Vector2(0, 0); break;

                case TextAnchor.LowerRight:
                case TextAnchor.MiddleRight:
                case TextAnchor.UpperRight: sTr.anchoredPosition = new Vector2(-4, 0); break;
                }
                text                    = sGo.AddComponent <Text>();
                text.font               = Graphics.GetStandardFont();
                text.fontSize           = size * SCALING_FACTOR;
                text.alignment          = allignment;
                text.horizontalOverflow = HorizontalWrapMode.Overflow;
                text.verticalOverflow   = VerticalWrapMode.Overflow;
                TextBoxScript tbs = go.AddComponent <TextBoxScript>();
                tbs.parent       = this;
                tbs.hasMouseover = Text.AltText != null;
                text.text        = Text;
                text.color       = color ?? Graphics.Color_.text;

                tr.sizeDelta   = new Vector2(text.preferredWidth / SCALING_FACTOR + size, (size + 2));
                sTr.localScale = new Vector3(1f / SCALING_FACTOR, 1f / SCALING_FACTOR, 1);
                sTr.sizeDelta  = new Vector2(text.preferredWidth / SCALING_FACTOR + size, (size + 2));
            }
        }
Example #16
0
        public TabbedWindow(Transform parent, Vector2 size, List <Tuple <TextRef, GameObject> > tabs, int tabFontSize = 12, bool canBeMinimised = true)
        {
            go = new GameObject("TabWindow", typeof(RectTransform));
            go.transform.SetParent(parent, false);
            this.size           = size;
            this.canBeMinimised = canBeMinimised;
            this.tabFontSize    = tabFontSize;
            ((RectTransform)go.transform).sizeDelta = size;
            var VLayGr = go.AddComponent <VerticalLayoutGroup>();

            VLayGr.childForceExpandHeight = false;
            VLayGr.childForceExpandWidth  = false;
            GameObject buttonLine = new GameObject("Tab Line", typeof(RectTransform));

            buttonLine.transform.SetParent(go.transform, false);
            var LayEl = buttonLine.AddComponent <LayoutElement>();

            LayEl.minHeight      = tabFontSize * 3 / 2 + 5;
            LayEl.flexibleHeight = 0;
            LayEl.flexibleWidth  = 1;
            var HLayGr = buttonLine.AddComponent <HorizontalLayoutGroup>();

            HLayGr.childForceExpandHeight = false;
            HLayGr.childForceExpandWidth  = false;
            HLayGr.padding = new RectOffset(5, 0, 3, 2);
            GameObject mainWindow = new GameObject("Main Window", typeof(RectTransform));

            mainWindow.transform.SetParent(go.transform, false);
            mainWindow.AddComponent <LayoutElement>().flexibleHeight = 1;
            mainWindow.AddComponent <HorizontalLayoutGroup>();   // used to strech the underlying windows

            windows = new List <Tuple <GameObject, GameObject> >();
            for (int i = 0; i < tabs.Count; i++)
            {
                GameObject tab = new GameObject("Tab", typeof(RectTransform));
                tab.transform.SetParent(buttonLine.transform);
                Image img = tab.AddComponent <Image>();
                img.sprite        = Graphics.GetSprite("tab_image_low");
                img.raycastTarget = true;
                img.type          = Image.Type.Sliced;
                img.fillCenter    = true;

                TextBox text = new TextBox(tab.transform, tabs[i].Item1, tabFontSize);
                text.transform.anchoredPosition += new Vector2(5, 0);

                tab.AddComponent <LayoutElement>().flexibleHeight = 1;
                tab.GetComponent <LayoutElement>().preferredWidth = text.Width + tabFontSize;

                int j = i;
                if (canBeMinimised)
                {
                    tab.AddComponent <Button>().onClick.AddListener(() => { MaximiseWindow(); SetTab(j); });
                }
                else
                {
                    tab.AddComponent <Button>().onClick.AddListener(() => SetTab(j));
                }

                GameObject window = tabs[i].Item2;
                window.transform.SetParent(mainWindow.transform);
                window.SetActive(false);
                windows.Add(new Tuple <GameObject, GameObject>(tab, window));
            }
            if (canBeMinimised)
            {
                GameObject tab = new GameObject("Tab", typeof(RectTransform));
                tab.transform.SetParent(buttonLine.transform);
                Image img = tab.AddComponent <Image>();
                img.sprite        = Graphics.GetSprite("tab_image_low");
                img.raycastTarget = true;
                img.type          = Image.Type.Sliced;
                img.fillCenter    = true;

                TextBox text = new TextBox(tab.transform, TextRef.Create("X", false), tabFontSize);
                ((RectTransform)text.gameObject.transform).anchoredPosition += new Vector2(5, 0);

                tab.AddComponent <LayoutElement>().flexibleHeight = 1;
                tab.GetComponent <LayoutElement>().preferredWidth = text.gameObject.GetComponent <Text>().preferredWidth + tabFontSize;
                int a = windows.Count;
                tab.AddComponent <Button>().onClick.AddListener(() => { SetTab(a); MinimiseWindow(); });

                GameObject window = new GameObject("Null Window", typeof(RectTransform));
                window.transform.SetParent(mainWindow.transform);
                window.SetActive(false);
                windows.Add(new Tuple <GameObject, GameObject>(tab, window));
            }
            if (canBeMinimised)
            {
                MinimiseWindow();
            }
            else
            {
                SetTab(0);
            }
        }