Last active
March 27, 2025 00:36
-
-
Save w0wca7a/005cc2afd5d40bb169b9508c59b8ce96 to your computer and use it in GitHub Desktop.
Сomponent for Stride Game Engine shows Popup text above entity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using Stride.Core; | |
using Stride.Core.Annotations; | |
using Stride.Engine; | |
using Stride.Rendering; | |
using Stride.Core.Mathematics; | |
using Stride.UI; | |
using Stride.UI.Panels; | |
using Stride.UI.Controls; | |
using Stride.Graphics; | |
namespace MyGame3 | |
{ | |
[DataContract] | |
public class TestPopup : SyncScript | |
{ | |
[DataMember] | |
public Entity ApproximateCamera { get; set; } | |
[DataMember] | |
public SpriteFont Font { get; set; } | |
[DataMember] | |
public string PopupText { get; set; } = "Enter popup text"; | |
[DataMember] | |
public RenderGroup RenderGroup { get; set; } = RenderGroup.Group0; | |
[DataMember] | |
[DataMemberRange(0.0d, 1)] | |
public float Distance { get; set; } = 1; | |
private UIComponent uIComponent; | |
private Entity uIEntity; | |
public override void Start() | |
{ | |
uIComponent = GetUiComponent(); | |
if (Entity.Get<UIComponent>() is null & !Entity.GetChildren().Any()) | |
{ | |
uIEntity = []; | |
uIEntity.Name = "UI Subentity"; | |
uIEntity.Transform.Position = Entity.Transform.WorldMatrix.TranslationVector + new Vector3(0, 1.5f, 0); | |
uIEntity.Components.Add(uIComponent); | |
Entity.AddChild(uIEntity); | |
} | |
} | |
public override void Update() | |
{ | |
if (uIComponent is null & ApproximateCamera is null) return; | |
//uIEntity.Transform.Position = Entity.Transform.WorldMatrix.TranslationVector + new Vector3(0, 1.5f, 0); | |
DebugText.Print("Camera pos " + ApproximateCamera.Transform.WorldMatrix.TranslationVector.ToString(), new Int2(40, 800)); | |
var distance = Vector3.Distance(uIComponent.Entity.Transform.WorldMatrix.TranslationVector, ApproximateCamera.Transform.WorldMatrix.TranslationVector); | |
DebugText.Print("Distance " + distance, new Int2(40, 830)); | |
// Включаем/выключаем UIComponent в зависимости от расстояния | |
uIComponent.Enabled = distance < Distance; | |
} | |
private UIComponent GetUiComponent() | |
{ | |
Grid grid = new() | |
{ | |
Children = | |
{ | |
new TextBlock() | |
{ | |
Text = PopupText, | |
Margin = new Thickness() { Top = 230f, Bottom = 250f }, | |
HorizontalAlignment = HorizontalAlignment.Stretch, | |
VerticalAlignment = VerticalAlignment.Stretch, | |
Visibility = Visibility.Visible, | |
IsEnabled = true, | |
TextAlignment = TextAlignment.Center, | |
TextSize = 200, | |
BackgroundColor = new Color(0), | |
Opacity = 1f, | |
TextColor = new Color(new Vector3(255f,255f,255f),0f), | |
OutlineColor = new Color(new Vector3(0,0,0),1f), | |
OutlineThickness = 5f, | |
DrawLayerNumber = 1, | |
Font = Font, | |
} | |
}, | |
Opacity = 1f, | |
BackgroundColor = new Color(0), | |
IsEnabled = true, | |
Visibility = Visibility.Visible, | |
HorizontalAlignment = HorizontalAlignment.Stretch, | |
VerticalAlignment = VerticalAlignment.Stretch, | |
}; | |
UIPage _page = new() | |
{ | |
Name = "Popup page", | |
RootElement = grid, | |
}; | |
UIComponent _uiComponent = new() | |
{ | |
Enabled = false, | |
IsBillboard = true, | |
IsFullScreen = false, | |
Size = new Vector3(1.28f, 0.72f, 1f), | |
RenderGroup = this.RenderGroup, | |
Page = _page, | |
}; | |
return _uiComponent; | |
} | |
} | |
} |
Author
w0wca7a
commented
Mar 26, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment