// Here is where the canvas and the font are available, // so this is where the location of both the text label // and the animated sprite can be properly positioned protected override void Init() { // Get sprite dimensions int spriteWidth = AnimatedSprite.GetWidth(); int spriteHeight = AnimatedSprite.GetHeight(); // Get text dimensions int textWidth = TextLabel.GetWidth(); int textHeight = TextLabel.GetHeight(); // Compute the dimensions of the MyIconLabel if (floatingIconPosition == Constants.FLOATING_POSITION_LEFT || floatingIconPosition == Constants.FLOATING_POSITION_RIGHT) { width = spriteWidth + spacing + textWidth; height = spriteHeight > textHeight ? spriteHeight : textHeight; } else { width = spriteWidth > textWidth ? spriteWidth : textWidth; height = spriteHeight + spacing + textHeight; } // Compute the horizontal positions of the icon and label if (floatingIconPosition == Constants.FLOATING_POSITION_LEFT) { AnimatedSprite.x = 0; TextLabel.x = spriteWidth + spacing; } else if (floatingIconPosition == Constants.FLOATING_POSITION_RIGHT) { AnimatedSprite.x = width - spriteWidth; TextLabel.x = AnimatedSprite.x - spacing - textWidth; } else if (floatingIconPosition == Constants.FLOATING_POSITION_TOP || floatingIconPosition == Constants.FLOATING_POSITION_BOTTOM) { AnimatedSprite.x = (width - spriteWidth) / 2; TextLabel.x = (width - textWidth) / 2; } // Compute the vertical positions of the icon and label if (floatingIconPosition == Constants.FLOATING_POSITION_LEFT || floatingIconPosition == Constants.FLOATING_POSITION_RIGHT) { TextLabel.y = (height - textHeight) / 2; AnimatedSprite.y = (height - spriteHeight) / 2; } else if (floatingIconPosition == Constants.FLOATING_POSITION_TOP) { AnimatedSprite.y = 0; TextLabel.y = spriteHeight + spacing; } else if (floatingIconPosition == Constants.FLOATING_POSITION_BOTTOM) { TextLabel.y = 0; AnimatedSprite.y = textHeight + spacing; } }
/** * 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); }