Skip to content

Commit 99fbe9b

Browse files
fix bugs
1 parent c4b9769 commit 99fbe9b

File tree

12 files changed

+88
-16
lines changed

12 files changed

+88
-16
lines changed

Flamui.Components/Ui.Button.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static bool Button(this Ui ui, string text, bool primary = false, bool fo
1616
}
1717
else
1818
{
19-
btn.BorderWidth(1).BorderColor(ColorPalette.BorderColor).Color(C.Transparent);
19+
btn.BorderWidth(1).BorderColor(ColorPalette.BorderColor).Color(ColorPalette.BackgroundColor);
2020
}
2121

2222
if (btn.HasFocusWithin)

Flamui/ArenaList.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,21 @@ private unsafe void ResizeIfNeeded(int neededCapacity)
5353

5454
if (neededCapacity >= Capacity)
5555
{
56+
var newCapacity = Math.Max(neededCapacity, _backingSlice.Length * 2);
57+
5658
//if there hasn't been another allocation on the arena, we don't need to allocate a new slice, we can just "extend" the current one
5759

5860
//todo, this isn't really elegant or easy to understand, would be better to have a method directly on the
5961
//arena, that can tell you if a slice is at the end of the arena, and therefore allows *zero* cost resize
6062
if (_backingSliceAllocateNum == arena.AllocNum)
6163
{
62-
arena.AllocateSlice<T>(_backingSlice.Length);
63-
_backingSlice = new Slice<T>(_backingSlice.Items, _backingSlice.Length * 2);
64+
arena.AllocateSlice<T>(newCapacity - _backingSlice.Length);
65+
_backingSlice = new Slice<T>(_backingSlice.Items, newCapacity);
6466
_backingSliceAllocateNum = arena.AllocNum;
6567
}
6668
else
6769
{
68-
var newSlice = arena.AllocateSlice<T>(Capacity * 2);
70+
var newSlice = arena.AllocateSlice<T>(newCapacity);
6971
_backingSliceAllocateNum = arena.AllocNum;
7072

7173
_backingSlice.Span.CopyTo(newSlice.Span);

Flamui/ArenaString.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public void Add<T>(T val)
2525
bool b => b.ToArenaString(),
2626
char c => c.ToArenaString(),
2727
string s => s,
28+
float f => f.ToArenaString(),
29+
double d => d.ToArenaString(),
2830
_ => throw new Exception($"{typeof(T)} is currently not supported :(")
2931
};
3032
Add(arenaString);
@@ -70,6 +72,28 @@ public static ArenaString ToArenaString(this bool i)
7072
throw new Exception("Error!!");
7173
}
7274

75+
public static ArenaString ToArenaString(this float f)
76+
{
77+
var buffer = Ui.Arena.AllocateSlice<char>(20);
78+
if (f.TryFormat(buffer.Span, out var charsWritten))
79+
{
80+
return new ArenaString(buffer.SubSlice(0, charsWritten));
81+
}
82+
83+
throw new Exception("Error!!");
84+
}
85+
86+
public static ArenaString ToArenaString(this double f)
87+
{
88+
var buffer = Ui.Arena.AllocateSlice<char>(20);
89+
if (f.TryFormat(buffer.Span, out var charsWritten))
90+
{
91+
return new ArenaString(buffer.SubSlice(0, charsWritten));
92+
}
93+
94+
throw new Exception("Error!!");
95+
}
96+
7397
public static unsafe ArenaString ToArenaString(this char i)
7498
{
7599
var buffer = Ui.Arena.Allocate(i);

Flamui/Drawing/FontLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public unsafe AtlasGlyphInfo FindGlyphEntry(char c, float resolutionMultiplier)
152152

153153
var bitmapSpan = new Span<byte>((void*)GlyphTempMemory, 100 * 100);
154154

155-
Console.WriteLine($"glyph cache miss: {c}:{resolutionMultiplier}, inserting into slot {entry.SlotNumber}");
155+
// Console.WriteLine($"glyph cache miss: {c}:{resolutionMultiplier}, inserting into slot {entry.SlotNumber}");
156156

157157
// for (var i = 0; i < bitmapSpan.Length; i++) //correct upwards for clearer text, not sure why we need to do it...
158158
// {

Flamui/Drawing/GlCanvas.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,24 +181,25 @@ private void DrawGlyph(FontAtlas fontAtlas, AtlasGlyphInfo atlasGlyphInfo, GpuTe
181181
MeshBuilder.AddTriangle(bottomRight, bottomLeft, topLeft);
182182
}
183183

184-
public void ClipRect(float x, float y, float width, float height)
184+
public void ClipRect(float x, float y, float width, float height, ClipMode clipMode)
185185
{
186186
//todo, we can to more efficient clipping if it isn't a rounded rect....
187-
ClipRoundedRect(x, y, width, height, 0);
187+
ClipRoundedRect(x, y, width, height, 0, clipMode);
188188
}
189189

190190
public void ClearClip()
191191
{
192192
Flush();
193193

194194
_renderer.Gl.StencilMask(0xFF);
195+
_renderer.Gl.StencilFunc(StencilFunction.Equal, 1, 0xFF);
195196
_renderer.Gl.ClearStencil(1);
196197
_renderer.Gl.Clear(ClearBufferMask.StencilBufferBit);
197198
_renderer.Gl.StencilMask(0x00);
198199

199200
}
200201

201-
public void ClipRoundedRect(float x, float y, float width, float height, float radius)
202+
public void ClipRoundedRect(float x, float y, float width, float height, float radius, ClipMode clipMode)
202203
{
203204
//magic code that I don't really understand...
204205

@@ -226,7 +227,15 @@ public void ClipRoundedRect(float x, float y, float width, float height, float r
226227
_renderer.Gl.ColorMask(true, true, true, true);
227228
_renderer.Gl.DepthMask(true);
228229

229-
_renderer.Gl.StencilFunc(StencilFunction.Equal, 1, 0xFF); //switch to Notequal, if we want to invert the clip
230+
231+
if (clipMode == ClipMode.OnlyDrawWithin)
232+
{
233+
_renderer.Gl.StencilFunc(StencilFunction.Equal, 1, 0xFF);
234+
}else if (clipMode == ClipMode.OnlyDrawOutside)
235+
{
236+
_renderer.Gl.StencilFunc(StencilFunction.Notequal, 1, 0xFF);
237+
}
238+
230239
_renderer.Gl.StencilMask(0x00); //disables writing to the stencil buffer
231240
//_renderer.Gl.Disable(EnableCap.StencilTest);
232241

Flamui/RenderContext.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
namespace Flamui;
88

9+
public enum ClipMode : byte
10+
{
11+
12+
OnlyDrawWithin,
13+
OnlyDrawOutside
14+
}
15+
916
public class RenderContext
1017
{
1118
public Stack<int> ZIndexes = new();
@@ -84,12 +91,13 @@ public void AddVectorGraphics(int vgId, Slice<byte> vgData, Bounds bounds)
8491
Add(cmd);
8592
}
8693

87-
public void PushClip(Bounds bounds, float radius = 0)
94+
public void PushClip(Bounds bounds, ClipMode clipMode, float radius = 0)
8895
{
8996
var cmd = new Command();
9097
cmd.Bounds = bounds;
9198
cmd.Radius = radius;
9299
cmd.Type = CommandType.ClipRect;
100+
cmd.ClipMode = clipMode;
93101

94102
ClipStack.Push(cmd);
95103

@@ -255,9 +263,9 @@ public void Rerender(Renderer renderer)
255263
break;
256264
case CommandType.ClipRect:
257265
if(command.Radius == 0)
258-
canvas.ClipRect(command.Bounds.X, command.Bounds.Y, command.Bounds.W, command.Bounds.H);
266+
canvas.ClipRect(command.Bounds.X, command.Bounds.Y, command.Bounds.W, command.Bounds.H, command.ClipMode);
259267
else
260-
canvas.ClipRoundedRect(command.Bounds.X, command.Bounds.Y, command.Bounds.W, command.Bounds.H, command.Radius);
268+
canvas.ClipRoundedRect(command.Bounds.X, command.Bounds.Y, command.Bounds.W, command.Bounds.H, command.Radius, command.ClipMode);
261269
break;
262270
case CommandType.Text:
263271
canvas.Paint.Font = new ScaledFont(command.Font.Get(), command.FontSize);

Flamui/Renderable.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public struct Command : IEquatable<Command>
147147
public Bitmap Bitmap;
148148
public int VGId;
149149
public Slice<byte> VGData;
150+
public ClipMode ClipMode;
150151

151152
public bool Equals(Command other)
152153
{

Flamui/TGALoader.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ public int Stride()
3838
public void CopyTo(Bitmap bitmap)
3939
{
4040
if (Width > bitmap.Width || Height > bitmap.Height)
41-
throw new Exception("Bitmap is too large :(");
41+
{
42+
Console.WriteLine("VG too large :(");
43+
return;
44+
}
4245

4346
bitmap.Data.MemZero();
4447

Flamui/UiElements/FlexContainerRenderer.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public static void Render(RenderContext renderContext, FlexContainer flexContain
4545
{
4646
float borderRadius = flexContainer.Info.Radius + flexContainer.Info.BorderWidth;
4747

48+
var needsClip = flexContainer.Info.Color == null || flexContainer.Info.Color.Value.Alpha != 255;
49+
50+
if (needsClip)
51+
{
52+
renderContext.PushClip(flexContainer.Rect.ToBounds(offset), ClipMode.OnlyDrawOutside, flexContainer.Info.Radius);
53+
}
54+
4855
var bounds = new Bounds
4956
{
5057
X = offset.X - flexContainer.Info.BorderWidth,
@@ -53,6 +60,11 @@ public static void Render(RenderContext renderContext, FlexContainer flexContain
5360
H = flexContainer.Rect.Height + 2 * flexContainer.Info.BorderWidth,
5461
};
5562
renderContext.AddRect(bounds, flexContainer, borderColor, borderRadius);
63+
64+
if (needsClip)
65+
{
66+
renderContext.PopClip();
67+
}
5668
}
5769

5870
if (flexContainer.Info.Color is { } color)
@@ -83,7 +95,7 @@ public static void Render(RenderContext renderContext, FlexContainer flexContain
8395

8496
if (NeedsClip(flexContainer.Info))
8597
{
86-
renderContext.PushClip(flexContainer.Rect.ToBounds(offset), flexContainer.Info.Radius);
98+
renderContext.PushClip(flexContainer.Rect.ToBounds(offset), ClipMode.OnlyDrawWithin, flexContainer.Info.Radius);
8799
}
88100

89101
if (flexContainer.Info.ScrollConfigY.CanScroll || flexContainer.Info.ScrollConfigX.CanScroll)

Flamui/UiWindow.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,12 @@ public UiWindow(IWindow window, FlamuiComponent rootComponent, IServiceProvider
8787
window.Render += OnRender;
8888
}
8989

90+
public double DeltaTime;
91+
9092
private void OnRender(double obj)
9193
{
94+
DeltaTime = obj;
95+
9296
if (!isInitialized)
9397
return;
9498

0 commit comments

Comments
 (0)