Пример #1
0
        /**
         * The initializationMonitoringFunction is run once per frame by the Cycle method of the POST page.
         * When it returns TRUE, the application switches to the first page that comes after the POST page.
         * If There is no other page than the POST page, then nothing will happen.
         */
        public MyOnScreenApplication WithDefaultPostPage(Func <MyOnScreenApplication, bool> initializationMonitoringFunction)
        {
            // Make sure the POST page is the first one added
            if (Pages.Count() > 0)
            {
                throw new InvalidOperationException("The POST page must be the first page ever added to the application");
            }

            // Enforce the order of builder operations to avoid null pointer exceptions
            if (Canvas == null)
            {
                throw new InvalidOperationException("Please call WithCanvas() before calling WithDefaultPostPage()");
            }

            // Make sure the initialization function is set
            if (initializationMonitoringFunction == null)
            {
                throw new ArgumentException("The initialization monitoring function must be a lambda taking in a MyOnScreenObject and returning a bool");
            }

            // Create the POST page and give it the functionality of switching to the next page once
            // the initialization monitoring function returns true
            MyPage POSTPage = (MyPage) new MyPage()
                              .WithInvertedColors()
                              .WithClientPreDrawMethod((MyCanvas TargetCanvas, int iterationIndex) => {
                TargetCanvas.Clear();
            })
                              .WithClientCycleMethod((MyOnScreenObject obj, int iterationIndex) => {
                if (initializationMonitoringFunction(this))
                {
                    SwitchToPage(1);     // will do nothing if the page does not exist
                }
            });

            // Add the POST page to the application
            this.AddPage(POSTPage);

            // Add another filled panel to the POST page, to serve as background for the INITIALIZING text
            MyPanel TextBackgroundPanel = new MyPanel(0, 0, 2, 2).WithOptionalParameters(true, true, false);

            POSTPage.AddChild(TextBackgroundPanel);

            // Add the INIIALIZING text label to the POST page
            MyTextLabel TextLabel = new MyTextLabel("INITIALIZING", 1, 1).WithOptionalParameters(true, true, true);

            POSTPage.AddChild(TextLabel);

            // Compute the location and dimensions of the text and that of its back panel based on
            // the resolution of the Canvas
            int textLabelWidth  = TextLabel.GetWidth();
            int textLabelHeight = TextLabel.GetHeight();

            // Update the label coordinates
            TextLabel.x = (Canvas.GetResX() - textLabelWidth) / 2;
            TextLabel.y = (Canvas.GetResY() - textLabelHeight) / 2 - 3;

            // Update the panel coordinates (the drawing framework handles overflows)
            TextBackgroundPanel.x = TextLabel.x - 3;
            TextBackgroundPanel.y = TextLabel.y - 2;
            TextBackgroundPanel.SetWidth(textLabelWidth + 6);
            TextBackgroundPanel.SetHeight(textLabelHeight + 3);

            // Add the moving square (a panel with some simple animation logic)
            POSTPage.AddChild(
                new MyPanel(TextBackgroundPanel.x, TextBackgroundPanel.y + TextBackgroundPanel.GetHeight() + 2, 7, 4)
                .WithOptionalParameters(true, true, false)
                .WithClientCycleMethod((MyOnScreenObject obj, int iterationIndex) => {
                obj.x++;
                if (obj.x > TextBackgroundPanel.x + TextBackgroundPanel.GetWidth() - 7)
                {
                    obj.x = TextBackgroundPanel.x;
                }
            })
                );

            return(this);
        }
Пример #2
0
        private void UpdateItemPositions()
        {
            // If the list is empty, don't do anything
            if (Items.Count() == 0)
            {
                return;
            }

            // If there's just one item per page, then the selected item has to be
            // placed in the middle of the page, while all the other items are to
            // be hidden
            if (oneItemPerPage)
            {
                foreach (MyOnScreenObject Item in Items)
                {
                    if (Item == SelectedItem)
                    {
                        Item.isVisible = true;
                        Item.x         = ComputeItemHorizontalPosition(Item);
                        Item.y         = (Panel.GetHeight() - Item.GetHeight()) / 2;
                    }
                    else
                    {
                        Item.isVisible = false;
                    }

                    Item.invertColors = false;
                }

                SelectionBackground.isVisible = false;
            }
            // If there are many items per page
            else
            {
                // Compute the max list height, as it will be needed later
                int listMaxHeight = GetHeight() - (padding * 2);

                // Update the startPosY:
                //    - if the selected item is above the top margin of the list box,
                //      then make it so the selected item is at the top of the list box
                //    - if the selected item is below the bottom margin of the list box,
                //      then make it so the selected item is at the bottom of the list box

                // Get the current y position of the selected item based on the
                // previously set startPosY
                int selItemY = startPosY;
                for (int idx = 0; idx < selectedItemIndex; idx++)
                {
                    selItemY += Items[idx].GetHeight();
                }

                // Update the startPosY, if required
                if (selItemY < padding)
                {
                    startPosY += padding - selItemY;
                }
                else if (selItemY + SelectedItem.GetHeight() > listMaxHeight)
                {
                    startPosY -= selItemY + SelectedItem.GetHeight() - listMaxHeight;
                }

                // Once the startPosY has been updated, the items may be layed out vertically

                // Update item vertical positions
                int currPosY = startPosY;
                foreach (MyOnScreenObject Item in Items)
                {
                    Item.y            = currPosY;
                    Item.x            = ComputeItemHorizontalPosition(Item);
                    currPosY         += Item.GetHeight();
                    Item.isVisible    = Item.y >= padding && Item.y + Item.GetHeight() <= listMaxHeight;
                    Item.invertColors = Item == SelectedItem;
                }

                // Update the vertical position of the selection background
                SelectionBackground.x = padding;
                SelectionBackground.y = SelectedItem.y;
                SelectionBackground.SetWidth(GetWidth() - Scrollbar.GetWidth() - (padding * 2));
                SelectionBackground.SetHeight(SelectedItem.GetHeight());
                SelectionBackground.isVisible = true;
            }
        }