/// <summary>
        /// Checks <paramref name="alwaysVisible"/> and if it is anywhere outside the screen, brings
        /// it back into the screen, the <paramref name="window"/> is moved by that delta instead.
        /// This is to be called after the resize, and also after the move.
        /// </summary>
        /// <param name="window">Parent to be moved.</param>
        /// <param name="alwaysVisible">Object to be clamped to screen.</param>
        /// <returns>True if the position changed.</returns>
        public static bool ClampToScreen(UIComponent window, [NotNull] UIComponent alwaysVisible)
        {
            Rect origRect = new Rect(
                position: alwaysVisible.absolutePosition,
                size: alwaysVisible.size);

            Rect    clampedRect = new Rect(origRect);
            Vector2 resolution  = UIScaler.BaseResolution;

            VectorUtil.ClampRectToScreen(
                rect: ref clampedRect,
                resolution: resolution);

            float xMotion = clampedRect.x - origRect.x;
            float yMotion = clampedRect.y - origRect.y;

            // Nothing to do, return here
            if (Mathf.Approximately(xMotion, 0f) && Mathf.Approximately(yMotion, 0f))
            {
                return(false);
            }

            // Move the parent window by the difference
            Vector3 pos = window.absolutePosition;

            pos += new Vector3(xMotion, yMotion, 0f);
            window.absolutePosition = pos;

            // Log._Debug(
            //     $"Clamping origRect={origRect} to new={clampedRect} "
            //     + $"moving by {xMotion};{yMotion} newpos={pos}");
            return(true);
        }
        public void UpdatePosition(Vector2 pos)
        {
            Rect    rect       = new Rect(pos.x, pos.y, activeProfile.MENU_WIDTH, activeProfile.MENU_HEIGHT);
            Vector2 resolution = UIView.GetAView().GetScreenResolution();

            VectorUtil.ClampRectToScreen(ref rect, resolution);
            Log.Info($"Setting main menu position to [{pos.x},{pos.y}]");
            absolutePosition = rect.position;
            Invalidate();
        }
コード例 #3
0
ファイル: MainMenuButton.cs プロジェクト: sirusmonkey/TMPE
        public void UpdatePosition(Vector2 pos)
        {
            float   size       = GetButtonDimensions();
            Rect    rect       = new Rect(pos.x, pos.y, size, size);
            Vector2 resolution = UIView.GetAView().GetScreenResolution();

            VectorUtil.ClampRectToScreen(ref rect, resolution);
            Log.Info($"Setting main menu button position to [{pos.x},{pos.y}]");
            absolutePosition = rect.position;

            Invalidate();
        }
コード例 #4
0
        //private UILabel optionsLabel;

        public override void Start()
        {
            isVisible = false;

            backgroundSprite = "GenericPanel";
            color            = new Color32(64, 64, 64, 240);
            width            = MENU_WIDTH;
            height           = MENU_HEIGHT;

            VersionLabel = AddUIComponent <VersionLabel>();
            StatsLabel   = AddUIComponent <StatsLabel>();

            Buttons = new MenuButton[MENU_BUTTON_TYPES.Length];

            int i = 0;
            int y = TOP_BORDER;

            for (int row = 0; row < NUM_ROWS; ++row)
            {
                int x = HSPACING;
                for (int col = 0; col < NUM_BUTTONS_PER_ROW; ++col)
                {
                    if (i >= Buttons.Length)
                    {
                        break;
                    }

                    MenuButton button = AddUIComponent(MENU_BUTTON_TYPES[i]) as MenuButton;
                    button.relativePosition = new Vector3(x, y);
                    Buttons[i++]            = button;
                    x += BUTTON_SIZE + HSPACING;
                }
                y += BUTTON_SIZE + VSPACING;
            }

            GlobalConfig config     = GlobalConfig.Instance;
            Rect         rect       = new Rect(config.Main.MainMenuX, config.Main.MainMenuY, MENU_WIDTH, MENU_HEIGHT);
            Vector2      resolution = UIView.GetAView().GetScreenResolution();

            VectorUtil.ClampRectToScreen(ref rect, resolution);
            absolutePosition = rect.position;

            var dragHandler = new GameObject("TMPE_Menu_DragHandler");

            dragHandler.transform.parent        = transform;
            dragHandler.transform.localPosition = Vector3.zero;
            Drag = dragHandler.AddComponent <UIDragHandle>();

            Drag.width   = width;
            Drag.height  = TOP_BORDER;
            Drag.enabled = !GlobalConfig.Instance.Main.MainMenuPosLocked;
        }
コード例 #5
0
        public void UpdatePosition(Vector2 pos)
        {
            Rect rect = new Rect(
                pos.x,
                pos.y,
                ModUI.Instance.MainMenu.width,
                ScaledSize.GetHeight());
            Vector2 resolution = UIView.GetAView().GetScreenResolution();

            VectorUtil.ClampRectToScreen(ref rect, resolution);
            Log.Info($"Setting main menu position to [{pos.x},{pos.y}]");
            absolutePosition = rect.position;
            Invalidate();
        }
        public override void Start()
        {
            // Place the button.
            GlobalConfig config     = GlobalConfig.Instance;
            Rect         rect       = new Rect(config.Main.MainMenuButtonX, config.Main.MainMenuButtonY, BUTTON_WIDTH, BUTTON_HEIGHT);
            Vector2      resolution = UIView.GetAView().GetScreenResolution();

            VectorUtil.ClampRectToScreen(ref rect, resolution);
            absolutePosition = rect.position;

            // Set the atlas and background/foreground
            atlas = TextureUtil.GenerateLinearAtlas("TMPE_MainMenuButtonAtlas", TextureResources.MainMenuButtonTexture2D, 6, new string[] {
                MAIN_MENU_BUTTON_BG_BASE,
                MAIN_MENU_BUTTON_BG_HOVERED,
                MAIN_MENU_BUTTON_BG_ACTIVE,
                MAIN_MENU_BUTTON_FG_BASE,
                MAIN_MENU_BUTTON_FG_HOVERED,
                MAIN_MENU_BUTTON_FG_ACTIVE
            });

            UpdateSprites();

            // Set the button dimensions.
            width  = BUTTON_WIDTH;
            height = BUTTON_HEIGHT;

            // Enable button sounds.
            playAudioEvents = true;

            var dragHandler = new GameObject("TMPE_MainButton_DragHandler");

            dragHandler.transform.parent        = transform;
            dragHandler.transform.localPosition = Vector3.zero;
            Drag = dragHandler.AddComponent <UIDragHandle>();

            Drag.width   = width;
            Drag.height  = height;
            Drag.enabled = !GlobalConfig.Instance.Main.MainMenuButtonPosLocked;
        }