public RenderUIElement(UIComponent uiComponent, TransformComponent transformComponent) { UIComponent = uiComponent; TransformComponent = transformComponent; }
private void BuildUI() { var width = 400; var bufferRatio = GraphicsDevice.Presenter.BackBuffer.Width / (float)GraphicsDevice.Presenter.BackBuffer.Height; var ui = new UIComponent { Resolution = new Vector3(width, width / bufferRatio, 500) }; SceneSystem.SceneInstance.Scene.Entities.Add(new Entity { ui }); currentText = new TextBlock { Font = font, TextColor = Color.White, VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Center }; var buttonBack = new Button { Content = new TextBlock { Font = font, Text = "Previous" }, VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Left }; var buttonNext = new Button { Content = new TextBlock { Font = font, Text = "Next" }, VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Right }; currentText.SetGridColumn(1); buttonNext.SetGridColumn(2); buttonBack.Click += (o, _) => ChangeScene(-1); buttonNext.Click += (o, _) => ChangeScene(+1); ui.RootElement = new UniformGrid { Columns = 3, Children = { buttonBack, currentText, buttonNext } }; }
/// <summary> /// Returns if a screen position is within the borders of a tested ui component /// </summary> /// <param name="uiComponent">The <see cref="UIComponent"/> to be tested</param> /// <param name="viewport">The <see cref="Viewport"/> in which the component is being rendered</param> /// <param name="worldViewProj"></param> /// <param name="screenPosition">The position of the lick on the screen in normalized (0..1, 0..1) range</param> /// <param name="uiRay"><see cref="Ray"/> from the click in object space of the ui component in (-Resolution.X/2 .. Resolution.X/2, -Resolution.Y/2 .. Resolution.Y/2) range</param> /// <returns></returns> private bool GetTouchPosition(UIComponent uiComponent, ref Viewport viewport, ref Matrix worldViewProj, Vector2 screenPosition, out Ray uiRay) { uiRay = new Ray(new Vector3(float.NegativeInfinity), new Vector3(0, 1, 0)); // TODO XK-3367 This only works for a single view // Get a touch ray in object (UI component) space var touchRay = GetWorldRay(ref viewport, screenPosition, ref worldViewProj); // If the click point is outside the canvas ignore any further testing var dist = -touchRay.Position.Z / touchRay.Direction.Z; if (Math.Abs(touchRay.Position.X + touchRay.Direction.X * dist) > uiComponent.Resolution.X * 0.5f || Math.Abs(touchRay.Position.Y + touchRay.Direction.Y * dist) > uiComponent.Resolution.Y * 0.5f) return false; uiRay = touchRay; return true; }
protected Entity GetUIEntity(SpriteFont font, bool fixedSize, Vector3 position) { // Create and initialize "Touch Screen to Start" var touchStartLabel = new ContentDecorator { Content = new TextBlock { Font = font, TextSize = 32, Text = (fixedSize) ? "Fixed Size UI" : "Regular UI", TextColor = Color.White }, Padding = new Thickness(30, 20, 30, 25), HorizontalAlignment = HorizontalAlignment.Center }; //touchStartLabel.SetPanelZIndex(1); var grid = new Grid { BackgroundColor = (fixedSize) ? new Color(255, 0, 255) : new Color(255, 255, 0), MaximumWidth = 100, MaximumHeight = 100, MinimumWidth = 100, MinimumHeight = 100, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, }; grid.RowDefinitions.Add(new StripDefinition(StripType.Auto)); grid.ColumnDefinitions.Add(new StripDefinition()); grid.LayerDefinitions.Add(new StripDefinition()); grid.Children.Add(touchStartLabel); // Add the background var background = new ImageElement { StretchType = StretchType.Fill }; background.SetPanelZIndex(-1); var uiEntity = new Entity(); // Create a procedural model with a diffuse material var uiComponent = new UIComponent(); uiComponent.RootElement = new UniformGrid { Children = { background, grid } }; uiComponent.Resolution = new Vector3(100, 100, 100); // Same size as the inner grid uiComponent.IsFullScreen = false; // uiComponent.IsBillboard = true; uiComponent.IsFixedSize = fixedSize; uiComponent.Size = new Vector3(0.1f); // 10% of the vertical resolution uiEntity.Add(uiComponent); uiEntity.Transform.Position = position; return uiEntity; }