/// <summary>
        ///     Used in UI window OnClose method to restore default window settings
        /// </summary>
        public static void RestoreUIScale()
        {
            try
            {
                GenericMachinePanelScript panel = GenericMachinePanelScript.instance;

                panel.gameObject.transform.localPosition = UIUtil.StartPos;
                // panel.Background_Panel.transform.localPosition = UIUtil.BackgroundStartPos; // Not Used
                //SetPosition(panel.gameObject, UIUtil.StartPos);
                //SetPosition(panel.Background_Panel, UIUtil.BackgroundStartPos);

                panel.gameObject.transform.localScale       = new Vector3(1.0f, 1.0f, 1.0f);
                panel.Background_Panel.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);

                panel.Label_Holder.transform.localScale        = new Vector3(1.0f, 1.0f, 1.0f);
                panel.Icon_Holder.transform.localScale         = new Vector3(1.0f, 1.0f, 1.0f);
                panel.Content_Holder.transform.localScale      = new Vector3(1.0f, 1.0f, 1.0f);
                panel.Content_Icon_Holder.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
                panel.Scroll_Bar.transform.localScale          = new Vector3(1.0f, 1.0f, 1.0f);
                panel.Source_Holder.transform.localScale       = new Vector3(1.0f, 1.0f, 1.0f);

                //panel.Generic_Machine_Title_Label.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); // Leave Off - Fixes Title Sizing

                firstopen = false;
            }
            catch (Exception ex)
            {
                Debug.LogError("RestoreUIScale: " + ex.Message + " : " + ex.StackTrace);
            }
        }
        /// <summary>
        ///     Closes out and removes references to .Mod UI entiries
        /// </summary>
        /// <param name="name"></param>
        /// <param name="resetWindow"></param>
        public static void CleanupUI(string name, bool resetWindow)
        {
            try
            {
                GenericMachinePanelScript panel = GenericMachinePanelScript.instance;

                UIManager.RemoveUIRules("Machine");

                if (panel == null)
                {
                    Debug.LogError("CleanupUI: panel=null");
                    return;
                }
                else
                {
                    UIUtil.TargetMachine = null;

                    if (resetWindow)
                    {
                        UIUtil.TargetWindow = null;
                    }

                    panel.gameObject.SetActive(false);
                    panel.Background_Panel.SetActive(false);

                    panel.currentWindow = null;
                    panel.targetEntity  = null;

                    DragAndDropManager.instance.CancelDrag();
                    DragAndDropManager.instance.DisableDragBackground();

                    UIUtil.UILock = false;

                    GenericMachineManager manager = typeof(GenericMachinePanelScript).GetField("manager",
                                                                                               BindingFlags.NonPublic | BindingFlags.Instance).GetValue(panel) as GenericMachineManager;

                    manager.windows.Remove(eSegmentEntity.Mod);

                    if (manager.windows.ContainsKey(eSegmentEntity.Mod))
                    {
                        Debug.LogWarning(name + ": was not able to remove the window entry!");
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.LogError("CleanupUI: " + ex.Message + " : " + ex.StackTrace);
            }
        }
        ///// <summary>
        /////     Call this in GetPopupText to handle your UI Window
        ///// </summary>
        ///// <param name="theMachine">Pass the current machine</param>
        ///// <param name="theWindow">The mod window inherited from BaseMachineWindow</param>
        ///// <returns></returns>
        //public static bool HandleThisMachineWindow(SegmentEntity theMachine, BaseMachineWindow theWindow)
        //{
        //    try
        //    {
        //        //GenericMachineManager manager = GenericMachinePanelScript.instance.manager; // not yet
        //        GenericMachineManager manager = typeof(GenericMachinePanelScript).GetField("manager", BindingFlags.NonPublic |
        //            BindingFlags.Instance).GetValue(GenericMachinePanelScript.instance) as GenericMachineManager;

        //        // this will replace with the current machine's window, which is OK as long as each Mod uses this technique
        //        manager.windows[eSegmentEntity.Mod] = theWindow;
        //        UIUtil.TargetMachine = theMachine;
        //        theWindow.manager = manager;
        //        UIUtil.UIdelay = 0;
        //        UIUtil.UILock = true;
        //    }
        //    catch (Exception ex)
        //    {
        //        //this.error = "Window Registration failed : " + ex.Message;
        //        UnityEngine.Debug.LogError("Window Registration failed : " + ex.Message + " : " + ex.StackTrace);
        //    }
        //    GenericMachinePanelScript panel = GenericMachinePanelScript.instance;

        //    try
        //    {
        //        // player looking at this machine
        //        if (WorldScript.instance.localPlayerInstance.mPlayerBlockPicker.selectedEntity == theMachine)
        //        {
        //            if (panel.Background_Panel.activeSelf == true) // window is open
        //            {
        //                panel.gameObject.SetActive(true); // undoes the default handler hiding the window
        //            }

        //            // player interacts with machine
        //            if (Input.GetButtonDown("Interact")) // "E" by default
        //            {
        //                // UIManager.UpdateGenericMachineWindow() -> GenericMachinePanelScript.TryShow()
        //                // default handler will try and fail as intended because our window is not in its dictionary, this is fine
        //                // similarly, the Hide() should not occur because the selectedEntity is this machine (panel.targetEntity)
        //                //Debug.Log("Interacted");
        //                if (panel.Background_Panel.activeSelf == true) // window is not already opened
        //                {
        //                    // Do nothing
        //                }
        //                else // window IS already opened, we pressed to interact again (meaning to close it)
        //                {
        //                    Hide(panel); // hide window, since we are not focused on this machine anymore
        //                    DragAndDropManager.instance.CancelDrag();
        //                    DragAndDropManager.instance.DisableDragBackground();
        //                }
        //            }
        //            else if (Input.GetKeyDown(KeyCode.Escape)) // escape should close window also
        //            {
        //                if (panel.isActiveAndEnabled)
        //                    UIManager.instance.UnpauseGame();
        //                Hide(panel); // hide window
        //                DragAndDropManager.instance.CancelDrag();
        //                DragAndDropManager.instance.DisableDragBackground();
        //            }
        //        }
        //        else // we are not the selected machine, or no machine is selected; but not due to user input (probably)
        //        {
        //            if (panel.targetEntity == theMachine) // this machine WAS focused with window open a moment ago, so it should handle closing its own window
        //            {
        //                Hide(panel); // hide window, since we are not focused on this machine anymore
        //                DragAndDropManager.instance.CancelDrag();
        //                DragAndDropManager.instance.DisableDragBackground();
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        return false;
        //    }

        //    return (panel.targetEntity == theMachine); // true if machine window is currently open, false otherwise (such as if Hide() called)
        //}

        ///// <summary>
        /////     Internal sub function for hiding the panel
        ///// </summary>
        ///// <param name="panel">The working panel</param>
        //public static void Hide(GenericMachinePanelScript panel)
        //{
        //    UIManager.RemoveUIRules("Machine");
        //    panel.currentWindow.OnClose(panel.targetEntity);
        //    panel.Scroll_Bar.GetComponent<UIScrollBar>().scrollValue = 0f;
        //    panel.targetEntity = null;
        //    panel.currentWindow = null;

        //    GenericMachineManager manager = typeof(GenericMachinePanelScript).GetField("manager", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(panel) as GenericMachineManager;
        //    manager.ClearWindow();

        //    panel.gameObject.SetActive(false);
        //    panel.Background_Panel.SetActive(false);
        //}

        /// <summary>
        ///     Internal sub function for hiding the panel
        /// </summary>
        /// <param name="panel">The working panel</param>
        public static void Hide(GenericMachinePanelScript panel)
        {
            try
            {
                UIManager.RemoveUIRules("Machine");

                if (panel == null)
                {
                    //Debug.LogError("Hide: panel=null");
                    return;
                }
                else
                {
                    if (panel.targetEntity == null)
                    {
                        //Debug.LogError("Hide: panel.targetEntity=null");
                    }
                    else
                    {
                        panel.currentWindow.OnClose(panel.targetEntity);
                    }

                    panel.Scroll_Bar.GetComponent <UIScrollBar>().scrollValue = 0f;

                    panel.targetEntity  = null;
                    panel.currentWindow = null;

                    panel.gameObject.SetActive(false);
                    panel.Background_Panel.SetActive(false);

                    GenericMachineManager manager = typeof(GenericMachinePanelScript).GetField("manager",
                                                                                               BindingFlags.NonPublic | BindingFlags.Instance).GetValue(panel) as GenericMachineManager;
                    manager.ClearWindow();
                }

                DragAndDropManager.instance.CancelDrag();
                DragAndDropManager.instance.DisableDragBackground();
            }
            catch (Exception ex)
            {
                Debug.LogError("Hide: " + ex.Message + " : " + ex.StackTrace);
            }
        }
        ///// <summary>
        /////     An emergency escape function when looking at the wrong machine (no longer necessary!)
        ///// </summary>
        //public static void EscapeUI()
        //{
        //    GenericMachinePanelScript panel = GenericMachinePanelScript.instance;
        //    panel.gameObject.SetActive(false);
        //    panel.Background_Panel.SetActive(false);
        //    panel.currentWindow = null;
        //    panel.targetEntity = null;
        //    GenericMachineManager manager2 = typeof(GenericMachinePanelScript).GetField("manager", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(GenericMachinePanelScript.instance) as GenericMachineManager;
        //    manager2.windows.Remove(eSegmentEntity.Mod);
        //    UIManager.RemoveUIRules("Machine");
        //    DragAndDropManager.instance.CancelDrag();
        //    DragAndDropManager.instance.DisableDragBackground();
        //}


        ///// <summary>
        /////     Helper class for scaling UI window elements
        ///// </summary>
        ///// <param name="scalingfactorx">Multiplying factor for the window width</param>
        ///// <param name="xoffsetoverride">Allows manual overriding of the window content offset if required</param>
        //public static void ScaleUIWindow(float scalingfactorx, float xoffsetoverride = 0)
        //{
        //    if (!firstopen)
        //    {
        //        float xoffset = 0;
        //        if (scalingfactorx > 0)
        //            xoffset = -140 * scalingfactorx;
        //        if (xoffsetoverride != 0)
        //            xoffset = xoffsetoverride;
        //        StartPos = GenericMachinePanelScript.instance.gameObject.transform.localPosition;
        //        GenericMachinePanelScript.instance.gameObject.transform.localScale = new Vector3(scalingfactorx, 1.0f, 1.0f);
        //        GenericMachinePanelScript.instance.gameObject.transform.localPosition = StartPos + new Vector3(xoffset, 0f, 0f);
        //        GenericMachinePanelScript.instance.Background_Panel.transform.localScale = new Vector3(scalingfactorx, 1.0f, 1.0f);
        //        GenericMachinePanelScript.instance.Label_Holder.transform.localScale = new Vector3(1f / scalingfactorx, 1.0f, 1.0f);
        //        GenericMachinePanelScript.instance.Icon_Holder.transform.localScale = new Vector3(1f / scalingfactorx, 1.0f, 1.0f);
        //        GenericMachinePanelScript.instance.Content_Holder.transform.localScale = new Vector3(1f / scalingfactorx, 1.0f, 1.0f);
        //        GenericMachinePanelScript.instance.Content_Icon_Holder.transform.localScale = new Vector3(1f / scalingfactorx, 1.0f, 1.0f);
        //        GenericMachinePanelScript.instance.Scroll_Bar.transform.localScale = new Vector3(1f / scalingfactorx, 1.0f, 1.0f);
        //        GenericMachinePanelScript.instance.Source_Holder.transform.localScale = new Vector3(1f / scalingfactorx, 1.0f, 1.0f);
        //        GenericMachinePanelScript.instance.Generic_Machine_Title_Label.transform.localScale = new Vector3(1f / scalingfactorx, 1.0f, 1.0f);

        //        firstopen = true;
        //    }
        //}

        ///// <summary>
        /////     Used in UI window OnClose method to restore default window settings
        ///// </summary>
        //public static void RestoreUIScale()
        //{
        //    GenericMachinePanelScript.instance.gameObject.transform.localPosition = StartPos;
        //    GenericMachinePanelScript.instance.gameObject.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
        //    GenericMachinePanelScript.instance.Background_Panel.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
        //    GenericMachinePanelScript.instance.Label_Holder.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
        //    GenericMachinePanelScript.instance.Icon_Holder.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
        //    GenericMachinePanelScript.instance.Content_Holder.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
        //    GenericMachinePanelScript.instance.Content_Icon_Holder.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
        //    GenericMachinePanelScript.instance.Scroll_Bar.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
        //    GenericMachinePanelScript.instance.Source_Holder.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
        //    GenericMachinePanelScript.instance.Generic_Machine_Title_Label.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
        //    firstopen = false;
        //}

        /// <summary>
        ///     Helper class for scaling UI window elements
        /// </summary>
        /// <param name="scalingfactorx">Multiplying factor for the window width</param>
        /// <param name="xoffsetoverride">Allows manual overriding of the window content offset if required</param>
        public static void ScaleUIWindow(float scalingfactorx, float xoffsetoverride = 0)
        {
            try
            {
                GenericMachinePanelScript panel = GenericMachinePanelScript.instance;
                if (!firstopen)
                {
                    firstopen = true;

                    if (scalingfactorx < 0.5f)
                    {
                        scalingfactorx = 0.5f;
                    }
                    else if (scalingfactorx > 5.0f)
                    {
                        scalingfactorx = 5.0f;
                    }

                    float xoffset = 0f;

                    if (Math.Abs(scalingfactorx - 1.0f) > Single.Epsilon) // 1f
                    {
                        xoffset = (-140f * scalingfactorx);
                    }

                    if (Math.Abs(xoffsetoverride) > Single.Epsilon) // 0f
                    {
                        xoffset = xoffsetoverride;
                    }

                    UIUtil.StartPos           = panel.gameObject.transform.localPosition;
                    UIUtil.BackgroundStartPos = panel.Background_Panel.transform.localPosition; // Not Used

                    panel.gameObject.transform.localScale       = new Vector3(scalingfactorx, 1.0f, 1.0f);
                    panel.Background_Panel.transform.localScale = new Vector3(scalingfactorx, 1.0f, 1.0f);

                    panel.gameObject.transform.localPosition = UIUtil.StartPos + new Vector3(xoffset, 0f, 0f);
                    // my testing only: adding this causes the window frame to be ofset up/left of background
                    //panel.Background_Panel.transform.localPosition = UIUtil.BackgroundStartPos + new Vector3(xoffset, 0f, 0f); //added
                    //SetPosition(panel.gameObject, UIUtil.StartPos + new Vector3(xoffset, 0f, 0f));
                    //SetPosition(panel.Background_Panel, UIUtil.BackgroundStartPos + new Vector3(xoffset, 0f, 0f));

                    if (Math.Abs(scalingfactorx - 1f) < Single.Epsilon) // 1f
                    {
                        return;
                    }

                    float adjFactor = (1f / scalingfactorx); // added: lets divide 1 time

                    panel.Label_Holder.transform.localScale        = new Vector3(adjFactor, 1.0f, 1.0f);
                    panel.Icon_Holder.transform.localScale         = new Vector3(adjFactor, 1.0f, 1.0f);
                    panel.Content_Holder.transform.localScale      = new Vector3(adjFactor, 1.0f, 1.0f);
                    panel.Content_Icon_Holder.transform.localScale = new Vector3(adjFactor, 1.0f, 1.0f);
                    panel.Scroll_Bar.transform.localScale          = new Vector3(adjFactor, 1.0f, 1.0f);
                    panel.Source_Holder.transform.localScale       = new Vector3(adjFactor, 1.0f, 1.0f);

                    //panel.Generic_Machine_Title_Label.transform.localScale = new Vector3(adjFactor, 1.0f, 1.0f); // Leave Off - Fixes Title Sizing
                }
            }
            catch (Exception ex)
            {
                Debug.LogError("ScaleUIWindow: " + ex.Message + " : " + ex.StackTrace);
            }
        }
        /// <summary>
        ///     Call this in HandleThisMachineWindow to handle your UI Window Viewing
        /// </summary>
        /// <param name="theMachine">Pass the current machine</param>
        /// <returns></returns>
        public static bool HandleWindowView(SegmentEntity theMachine)
        {
            bool flag = false;

            try
            {
                GenericMachinePanelScript panel = GenericMachinePanelScript.instance;

                // player looking at this machine
                if (WorldScript.instance.localPlayerInstance.mPlayerBlockPicker.selectedEntity == theMachine)
                {
                    //ModLogging.LogPlain(debugLocal, getPrefix(), "HandleWindowView: We are theMachine");

                    if (panel.Background_Panel.activeSelf) // window is open
                    {
                        //ModLogging.LogPlain(debugLocal, getPrefix(), "HandleWindowView: Panel SetActive");
                        panel.gameObject.SetActive(true); // undoes the default handler hiding the window
                    }

                    //ModLogging.LogPlain(debugLocal, getPrefix(), "HandleWindowView: TestButton");

                    // player interacts with machine
                    if (Input.GetButtonDown("Interact")) // "E" by default
                    {
                        //ModLogging.LogPlain(debugLocal, getPrefix(), "HandleWindowView: Interact Button Pressed");

                        // UIManager.UpdateGenericMachineWindow() -> GenericMachinePanelScript.TryShow()
                        // default handler will try and fail as intended because our window is not in its dictionary, this is fine
                        // similarly, the Hide() should not occur because the selectedEntity is this machine (panel.targetEntity)
                        //Debug.Log("Interacted");

                        if (panel.Background_Panel.activeSelf) // window is not already opened
                        {
                            //ModLogging.LogPlain(debugLocal, getPrefix(), "HandleWindowView: Panel Active - Do Nothing");
                            // Do nothing - GenericMachinePanelScript will handle
                        }
                        else // window IS already opened, we pressed to interact again (meaning to close it)
                        {
                            //ModLogging.LogPlain(debugLocal, getPrefix(), "HandleWindowView: Hide Panel");
                            Hide(panel); // hide window, since we are not focused on this machine anymore
                        }
                    }
                    else if (Input.GetKeyDown(KeyCode.Escape)) // escape should close window also
                    {
                        //ModLogging.LogPlain(debugLocal, getPrefix(), "HandleWindowView:Escape Button Pressed");

                        if (panel.isActiveAndEnabled)
                        {
                            UIManager.instance.UnpauseGame();
                        }
                        Hide(panel); // hide window
                    }
                }
                else // we are not the selected machine, or no machine is selected; but not due to user input (probably)
                {
                    if (panel.targetEntity == theMachine) // this machine WAS focused with window open a moment ago, so it should handle closing its own window
                    {
                        Hide(panel); // hide window, since we are not focused on this machine anymore
                    }
                }
                flag = (panel.targetEntity == theMachine); // true if machine window is currently open, false otherwise (such as if Hide() called)
            }
            catch (Exception ex)
            {
                Debug.LogError("HandleWindowView: failed : " + ex.Message + " : " + ex.StackTrace);
                flag = false;
            }
            return(flag);
        }