public void TestMeasureOverride() { var rand = new Random(); var imageSize = new Vector3(100, 50, 0); var Sprite = new Sprite { Region = new Rectangle(0, 0, (int)imageSize.X, (int)imageSize.Y), Borders = new Vector4(1, 2, 3, 4) }; var image = new ImageElement { Source = Sprite }; // Fixed sized image.StretchType = StretchType.None; image.Measure(rand.NextVector3()); Assert.AreEqual(imageSize, image.DesiredSizeWithMargins); // Uniform sized image.StretchType = StretchType.Uniform; image.Measure(new Vector3(50)); Assert.AreEqual(new Vector3(50, 25, 0), image.DesiredSizeWithMargins); // Uniform to fill sized image.StretchType = StretchType.UniformToFill; image.Measure(new Vector3(50)); Assert.AreEqual(new Vector3(100, 50, 0), image.DesiredSizeWithMargins); // Fill on stretch image.StretchType = StretchType.FillOnStretch; image.Measure(new Vector3(50)); Assert.AreEqual(new Vector3(50, 25, 0), image.DesiredSizeWithMargins); // Fill image.StretchType = StretchType.Fill; image.Measure(new Vector3(50)); Assert.AreEqual(new Vector3(50, 50, 0), image.DesiredSizeWithMargins); // Test minimal size due to borders image.StretchType = StretchType.Fill; image.Measure(new Vector3()); Assert.AreEqual(new Vector3(4, 6, 0), image.DesiredSizeWithMargins); // Test with infinite value for (var type = 0; type < 5; ++type) TestMeasureOverrideInfiniteValues((StretchType)type); // Test stretch directions image.StretchType = StretchType.Fill; image.StretchDirection = StretchDirection.DownOnly; image.Measure(new Vector3(200, 300, 220)); Assert.AreEqual(new Vector3(100, 50, 0), image.DesiredSizeWithMargins); image.Measure(new Vector3(20, 15, 30)); Assert.AreEqual(new Vector3(20, 15, 0), image.DesiredSizeWithMargins); image.StretchDirection = StretchDirection.UpOnly; image.Measure(new Vector3(200, 300, 220)); Assert.AreEqual(new Vector3(200, 300, 0), image.DesiredSizeWithMargins); image.Measure(new Vector3(20, 30, 22)); Assert.AreEqual(new Vector3(100, 50, 0), image.DesiredSizeWithMargins); }
/// <summary> /// Load resource and construct ui components /// </summary> public override void Start() { // Load resources shared by different UI screens spriteFont = Asset.Load<SpriteFont>("Font"); uiImages = Asset.Load<SpriteSheet>("UIImages"); buttonImage = uiImages["button"]; // Load and create specific UI screens. CreateMainMenuUI(); CreateGameUI(); CreateGameOverUI(); }
private static void InvalidateTrackBackground(object propertyowner, PropertyKey<Sprite> propertykey, Sprite propertyoldvalue) { var slider = (Slider)propertyowner; slider.InvalidateMeasure(); if (propertyoldvalue != null) propertyoldvalue.SizeChanged -= slider.OnSizeChanged; if(slider.TrackBackgroundImage != null) slider.TrackBackgroundImage.SizeChanged += slider.OnSizeChanged; }
public BackgroundSection(Sprite backgroundSprite, Vector3 screenVirtualResolution, float scrollSpeed, float depth, Vector2 startPos = default(Vector2)) { screenResolution = new Int2((int)screenVirtualResolution.X, (int)screenVirtualResolution.Y); screenCenter = new Vector2(screenVirtualResolution.X / 2, screenVirtualResolution.Y /2); this.depth = depth; firstQuadPos = startPos; secondQuadPos = startPos; ScrollSpeed = scrollSpeed; ScrollPos = 0; CreateBackground(backgroundSprite.Texture, backgroundSprite.Region); }
protected override async Task LoadContent() { await base.LoadContent(); var sprite = new Sprite(Asset.Load<Texture>("BorderButton")) { Borders = new Vector4(64, 64, 64, 64) }; var bi1 = new ImageElement { Source = sprite, Height = 150 }; var bi2 = new ImageElement { Source = sprite, Height = 300 }; var bi3 = new ImageElement { Source = sprite, Height = 500 }; stackPanel = new StackPanel { Orientation = Orientation.Vertical }; stackPanel.Children.Add(bi1); stackPanel.Children.Add(bi2); stackPanel.Children.Add(bi3); UIComponent.RootElement = new ScrollViewer { Content = stackPanel, ScrollMode = ScrollingMode.HorizontalVertical }; }
public void Constructor2Tests() { RunDrawTest(() => { // texture var textureSize = new Vector2(50, 75); var texture = Texture.New2D(GraphicsDevice, (int)textureSize.X, (int)textureSize.Y, 1, PixelFormat.R8G8B8A8_UNorm); var sprite = new Sprite(texture); var sprite2 = new Sprite(texture); Assert.IsNotNull(sprite.Name); Assert.AreNotEqual(sprite.Name, sprite2.Name); Assert.AreEqual(ImageOrientation.AsIs, sprite.Orientation); Assert.AreEqual(textureSize/2, sprite.Center); Assert.AreEqual(Vector4.Zero, sprite.Borders); Assert.IsFalse(sprite.HasBorders); Assert.AreEqual(texture, sprite.Texture); Assert.IsTrue(sprite.IsTransparent); Assert.AreEqual(new Vector2(100), sprite.PixelsPerUnit); Assert.AreEqual(new RectangleF(0, 0, textureSize.X, textureSize.Y), sprite.Region); Assert.AreEqual(textureSize/100, sprite.Size); Assert.AreEqual(textureSize, sprite.SizeInPixels); }); RunDrawTest(() => { // texture + name var textureSize = new Vector2(50, 75); var texture = Texture.New2D(GraphicsDevice, (int)textureSize.X, (int)textureSize.Y, 1, PixelFormat.R8G8B8A8_UNorm); var sprite = new Sprite(DefaultName, texture); Assert.AreEqual(DefaultName, sprite.Name); Assert.AreEqual(ImageOrientation.AsIs, sprite.Orientation); Assert.AreEqual(textureSize / 2, sprite.Center); Assert.AreEqual(Vector4.Zero, sprite.Borders); Assert.IsFalse(sprite.HasBorders); Assert.AreEqual(texture, sprite.Texture); Assert.IsTrue(sprite.IsTransparent); Assert.AreEqual(new Vector2(100), sprite.PixelsPerUnit); Assert.AreEqual(new RectangleF(0, 0, textureSize.X, textureSize.Y), sprite.Region); Assert.AreEqual(textureSize / 100, sprite.Size); Assert.AreEqual(textureSize, sprite.SizeInPixels); }); }
public void TestBasicInvalidations() { var source = new Sprite(); // ReSharper disable ImplicitlyCapturedClosure // - test the properties that are supposed to invalidate the object measurement UIElementLayeringTests.TestMeasureInvalidation(this, () => StretchType = StretchType.None); UIElementLayeringTests.TestMeasureInvalidation(this, () => StretchDirection = StretchDirection.DownOnly); UIElementLayeringTests.TestMeasureInvalidation(this, () => Source = source); UIElementLayeringTests.TestMeasureInvalidation(this, () => source.Region = new Rectangle(1, 2, 3, 4)); UIElementLayeringTests.TestMeasureInvalidation(this, () => source.Orientation = ImageOrientation.Rotated90); UIElementLayeringTests.TestMeasureInvalidation(this, () => source.Borders = Vector4.One); // - test the properties that are not supposed to invalidate the object layout state UIElementLayeringTests.TestNoInvalidation(this, () => source.Region = new Rectangle(8, 9, 3, 4)); // if the size of the region does not change we avoid re-measuring UIElementLayeringTests.TestNoInvalidation(this, () => source.Orientation = ImageOrientation.Rotated90); // no changes UIElementLayeringTests.TestNoInvalidation(this, () => source.Borders = Vector4.One); // no changes // ReSharper restore ImplicitlyCapturedClosure }
public void Constructor1Tests() { { // empty var sprite = new Sprite(); var sprite2 = new Sprite(); Assert.IsNotNull(sprite.Name); Assert.AreNotEqual(sprite.Name, sprite2.Name); Assert.AreEqual(ImageOrientation.AsIs, sprite.Orientation); Assert.AreEqual(Vector2.Zero, sprite.Center); Assert.AreEqual(Vector4.Zero, sprite.Borders); Assert.IsFalse(sprite.HasBorders); Assert.IsNull(sprite.Texture); Assert.IsTrue(sprite.IsTransparent); Assert.AreEqual(new Vector2(100), sprite.PixelsPerUnit); Assert.AreEqual(new RectangleF(), sprite.Region); Assert.AreEqual(Vector2.Zero, sprite.Size); Assert.AreEqual(Vector2.Zero, sprite.SizeInPixels); } { // name var sprite = new Sprite(DefaultName); Assert.AreEqual(DefaultName, sprite.Name); Assert.AreEqual(ImageOrientation.AsIs, sprite.Orientation); Assert.AreEqual(Vector2.Zero, sprite.Center); Assert.AreEqual(Vector4.Zero, sprite.Borders); Assert.IsFalse(sprite.HasBorders); Assert.IsNull(sprite.Texture); Assert.IsTrue(sprite.IsTransparent); Assert.AreEqual(new Vector2(100), sprite.PixelsPerUnit); Assert.AreEqual(new RectangleF(), sprite.Region); Assert.AreEqual(Vector2.Zero, sprite.Size); Assert.AreEqual(Vector2.Zero, sprite.SizeInPixels); } }
public void TestArrangeOverrideInfiniteValues(StretchType stretch) { var imageSize = new Vector3(100, 50, 0); var Sprite = new Sprite { Region = new Rectangle(0, 0, (int)imageSize.X, (int)imageSize.Y), Borders = new Vector4(1, 2, 3, 4) }; var image = new ImageElement { Source = Sprite, StretchType = stretch }; image.Arrange(new Vector3(float.PositiveInfinity), false); Assert.AreEqual(imageSize, image.RenderSize); image.Arrange(new Vector3(150, float.PositiveInfinity, 10), false); Assert.AreEqual(stretch == StretchType.None ? imageSize : new Vector3(150, 75, 0), image.RenderSize); }
private UIElement CreateMainScreneTopBar() { // Create Life bar lifeBarGaugeImage = mainScreneImages["life_bar"]; var lifeBarCoverImage = mainScreneImages["character_frame"]; gaugeBarRegion = lifeBarGaugeImage.Region; var lifebarGauge = new ImageElement { Name = "LifeBarBackground", Source = lifeBarGaugeImage, StretchType = StretchType.Fill, }; lifebarGauge.SetGridColumn(1); lifeBarGrid = new Grid(); lifeBarGrid.Children.Add(lifebarGauge); lifeBarGrid.ColumnDefinitions.Add(new StripDefinition(StripType.Fixed, lifeBarCoverImage.Borders.X)); lifeBarGrid.ColumnDefinitions.Add(new StripDefinition(StripType.Star, 0)); lifeBarGrid.ColumnDefinitions.Add(new StripDefinition(StripType.Star, 100)); lifeBarGrid.ColumnDefinitions.Add(new StripDefinition(StripType.Fixed, lifeBarCoverImage.Borders.Y)); lifeBarGrid.RowDefinitions.Add(new StripDefinition()); lifeBarGrid.LayerDefinitions.Add(new StripDefinition()); lifeBarGrid.SetCanvasRelativePosition(new Vector3(0f, 0.185f, 0f)); lifeBarGrid.SetCanvasRelativeSize(new Vector3(1f, 0.25f, 1f)); lifeBarGrid.SetPanelZIndex(-1); var lifebarForeground = new ImageElement { Name = "LifeBarForeGround", Source = lifeBarCoverImage, StretchType = StretchType.Fill, }; lifebarForeground.SetGridColumnSpan(3); lifebarForeground.SetGridRowSpan(3); lifebarForeground.SetCanvasRelativeSize(new Vector3(1f, 1f, 1f)); // Life count lifeCounter = new TextBlock { Text = CreateLifeCountText(), TextColor = Color.Gold, Font = westernFont, TextSize = 32, HorizontalAlignment = HorizontalAlignment.Left }; lifeCounter.SetCanvasAbsolutePosition(new Vector3(lifeBarCoverImage.Borders.X, float.NaN, float.NaN)); lifeCounter.SetCanvasRelativePosition(new Vector3(float.NaN, 0.44f, 0f)); lifeCounter.SetPanelZIndex(1); LifeStatus = 3; // Bonus items var bonusIcon = new ImageElement { Source = mainScreneImages["gold_icon"], Name = "bonus Icon", VerticalAlignment = VerticalAlignment.Center }; bonusCounter = new TextBlock { Text = CreateBonusCountText(), TextColor = Color.White, TextSize = 27, Font = westernFont, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(10, 0, 0, 0) }; // Money var moneyIcon = new ImageElement { Source = mainScreneImages["money_icon"], Name = "money Icon", Margin = new Thickness(20, 0, 0, 0), VerticalAlignment = VerticalAlignment.Center }; moneyCounter = new TextBlock { Text = CreateMoneyCountText(), TextColor = Color.White, TextSize = 27, Font = westernFont, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(10, 0, 0, 0) }; // Stack panel containing the bonus and money counters var moneyBonusStackPanel = new StackPanel { Name = "MoneyBonusStackPanel", Orientation = Orientation.Horizontal, }; moneyBonusStackPanel.Children.Add(bonusIcon); moneyBonusStackPanel.Children.Add(bonusCounter); moneyBonusStackPanel.Children.Add(moneyIcon); moneyBonusStackPanel.Children.Add(moneyCounter); moneyBonusStackPanel.SetCanvasRelativePosition(new Vector3(0.93f, 0.44f, 0f)); moneyBonusStackPanel.SetCanvasRelativeSize(new Vector3(float.NaN, 0.4f, 1f)); moneyBonusStackPanel.SetCanvasPinOrigin(new Vector3(1f, 0f, 0f)); moneyBonusStackPanel.SetPanelZIndex(1); // the main grid of the top bar var mainLayer = new Canvas { VerticalAlignment = VerticalAlignment.Top, MaximumHeight = 150 }; mainLayer.Children.Add(lifeBarGrid); mainLayer.Children.Add(lifebarForeground); mainLayer.Children.Add(lifeCounter); mainLayer.Children.Add(moneyBonusStackPanel); return mainLayer; }
protected override void LoadScene() { mainScreneImages = LoadAsset<SpriteSheet>("MainSceneImages"); westernFont = LoadAsset<SpriteFont>("WesternFont"); popupWindowImage = mainScreneImages["popup_window"]; // Preload stars starSprites.Add(mainScreneImages["star0"]); starSprites.Add(mainScreneImages["star1"]); starSprites.Add(mainScreneImages["star2"]); starSprites.Add(mainScreneImages["star3"]); borderStarImages.Add(mainScreneImages["bstar0"]); borderStarImages.Add(mainScreneImages["bstar1"]); borderStarImages.Add(mainScreneImages["bstar2"]); borderStarImages.Add(mainScreneImages["bstar3"]); // Create space ships var random = new Random(); for (var i = 0; i < ShipNameList.Count; i++) { shipList.Add(new SpaceShip { Name = ShipNameList[i], Power = random.Next(4), Control = random.Next(4), Speed = random.Next(4), IsLocked = (i % 3) == 2, }); } var mainStackPanel = CreateMainScene(); CreateWelcomePopup(); CreateShipSelectionPopup(); // Create the background var background = new ImageElement { Source = mainScreneImages["background_uiimage"], StretchType = StretchType.Fill }; background.SetPanelZIndex(-1); // Overlay pop-ups and the main screen var overlay = new UniformGrid(); overlay.Children.Add(background); overlay.Children.Add(mainStackPanel); overlay.Children.Add(welcomePopup); overlay.Children.Add(shipSelectPopup); // Set the root element to the overall overlay var uiComponent = Entity.Get<UIComponent>(); uiComponent.RootElement = overlay; Script.AddTask(FillLifeBar); }
private static void OnAspectImageInvalidated(object propertyOwner, PropertyKey<Sprite> propertyKey, Sprite propertyOldValue) { var button = (Button)propertyOwner; button.OnAspectImageInvalidated(); }