Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Commit 61060b1

Browse files
author
Alexis Huvier
committed
feat(widget): Add GetDisplayedRect and GetTotalDisplayedRect
1 parent 2ba3fb3 commit 61060b1

File tree

10 files changed

+77
-0
lines changed

10 files changed

+77
-0
lines changed

SharpEngine/Widget/Button.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public Button(Vec2 position, string text = "", string font = "", Vec2? size = nu
7878
_state = ButtonState.Idle;
7979
}
8080

81+
/// <inheritdoc />
82+
public override Rect GetDisplayedRect() => new (RealPosition - Size / 2, Size);
83+
8184
/// <inheritdoc />
8285
public override void Update(float delta)
8386
{

SharpEngine/Widget/Checkbox.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public Checkbox(Vec2 position, Vec2? size = null, bool isChecked = false) : base
3939
Size = size ?? new Vec2(20);
4040
IsChecked = isChecked;
4141
}
42+
43+
/// <inheritdoc />
44+
public override Rect GetDisplayedRect() => new (RealPosition - Size / 2, Size);
4245

4346
/// <inheritdoc />
4447
public override void Update(float delta)

SharpEngine/Widget/ColorRect.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public ColorRect(Vec2 position, Vec2? size = null, Color? color = null, int rota
3737
Size = size ?? Vec2.One;
3838
Rotation = rotation;
3939
}
40+
41+
/// <inheritdoc />
42+
public override Rect GetDisplayedRect() => new (RealPosition - Size / 2, Size);
4043

4144
/// <inheritdoc />
4245
public override void Draw()

SharpEngine/Widget/Frame.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public Frame(Vec2 position, Vec2 size, int borderSize = 3, Color? borderColor =
4545
BorderSize = borderSize;
4646
BackgroundColor = backgroundColor;
4747
}
48+
49+
/// <inheritdoc />
50+
public override Rect GetDisplayedRect() => new (RealPosition - Size / 2, Size);
4851

4952
/// <inheritdoc />
5053
public override void Draw()

SharpEngine/Widget/Image.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ public Image(Vec2 position, string texture = "", Vec2? scale = null, int rotatio
3838
Rotation = rotation;
3939
}
4040

41+
/// <inheritdoc />
42+
public override Rect GetDisplayedRect()
43+
{
44+
var window = Scene?.Window;
45+
46+
if(!Displayed || Texture.Length <= 0 || window == null) return new Rect();
47+
48+
var texture = window.TextureManager.GetTexture(Texture);
49+
var size = new Vec2(texture.width * Scale.X, texture.height * Scale.Y);
50+
return new Rect(RealPosition - size / 2, size);
51+
}
52+
4153
/// <inheritdoc />
4254
public override void Draw()
4355
{

SharpEngine/Widget/Label.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ public Label(Vec2 position, string text = "", string font = "", Color? color = n
7070
Style = style;
7171
}
7272

73+
/// <inheritdoc />
74+
public override Rect GetDisplayedRect()
75+
{
76+
77+
var font = Scene?.Window?.FontManager.GetFont(Font);
78+
79+
if(!Displayed || Scene == null || Text.Length <= 0 || Font.Length <= 0 || font == null) return new Rect();
80+
81+
var fontSize = FontSize ?? font.Value.baseSize;
82+
83+
var textSize = Raylib.MeasureTextEx(font.Value, Text, fontSize, 2);
84+
85+
return new Rect(RealPosition - (Vec2)textSize / 2, textSize);
86+
}
87+
7388
/// <inheritdoc />
7489
public override void Draw()
7590
{

SharpEngine/Widget/LineInput.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public LineInput(Vec2 position, string text = "", string font = "", Vec2? size =
6060
FontSize = fontSize;
6161
Focused = false;
6262
}
63+
64+
/// <inheritdoc />
65+
public override Rect GetDisplayedRect() => new (RealPosition - Size / 2, Size);
6366

6467
/// <inheritdoc />
6568
public override void Update(float delta)

SharpEngine/Widget/MultiLineInput.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public MultiLineInput(Vec2 position, string text = "", string font = "", Vec2? s
5757
FontSize = fontSize;
5858
Focused = false;
5959
}
60+
61+
/// <inheritdoc />
62+
public override Rect GetDisplayedRect() => new (RealPosition - Size / 2, Size);
6063

6164
/// <inheritdoc />
6265
public override void Update(float delta)

SharpEngine/Widget/TextureButton.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public TextureButton(Vec2 position, string text = "", string font = "", string t
7777
FontSize = fontSize;
7878
_state = ButtonState.Idle;
7979
}
80+
81+
/// <inheritdoc />
82+
public override Rect GetDisplayedRect() => new (RealPosition - Size / 2, Size);
8083

8184
/// <inheritdoc />
8285
public override void Update(float delta)

SharpEngine/Widget/Widget.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,35 @@ protected Widget(Vec2 position)
7373
Position = position;
7474
}
7575

76+
/// <summary>
77+
/// Return Displayed Rect for Widget
78+
/// </summary>
79+
/// <returns>Rect</returns>
80+
public abstract Rect GetDisplayedRect();
81+
82+
/// <summary>
83+
/// Return Displayed Rect for Widget and Children
84+
/// </summary>
85+
/// <returns>Rect</returns>
86+
public Rect GetTotalDisplayedRect()
87+
{
88+
var rect = GetDisplayedRect();
89+
foreach (var child in Children)
90+
{
91+
var childRect = child.GetDisplayedRect();
92+
if (childRect.X < rect.X)
93+
rect.X = childRect.X;
94+
if (childRect.Y < rect.Y)
95+
rect.Y = childRect.Y;
96+
if (childRect.X + childRect.Width > rect.X + rect.Width)
97+
rect.Width = childRect.X + childRect.Width - rect.X;
98+
if (childRect.Y + childRect.Height > rect.Y + rect.Height)
99+
rect.Height = childRect.Y + childRect.Height - rect.Y;
100+
}
101+
102+
return rect;
103+
}
104+
76105
/// <summary>
77106
/// Get All Children of one Type
78107
/// </summary>

0 commit comments

Comments
 (0)