Skip to content

Commit

Permalink
started implemented scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
FlurinBruehwiler committed Oct 6, 2023
1 parent 91a5586 commit 6093893
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 21 deletions.
4 changes: 4 additions & 0 deletions ImSharpUI.Sample/EventLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public void RunMainThread()
{
Console.WriteLine(e.key.keysym.scancode.ToString());
}
else if (e.type == SDL_EventType.SDL_MOUSEWHEEL)
{
GetWindow(e.wheel.windowID).Events.Enqueue(e);
}
}

SDL_Quit();
Expand Down
15 changes: 12 additions & 3 deletions ImSharpUI.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@
var added = false;
Task.Run(() =>
{
eventLoop.Windows.Add(new Window(windowHandle));
added = true;
eventLoop.RunRenderThread();
try
{
eventLoop.Windows.Add(new Window(windowHandle));
added = true;
eventLoop.RunRenderThread();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
});
while (!added)
{
Expand Down
25 changes: 18 additions & 7 deletions ImSharpUI.Sample/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,25 @@ public class Sample

public void Build()
{
DivStart().Color(255, 0, 0);
DivStart(out var div).Color(0, 255, 0).WidthFraction(50).HeightFraction(50).Radius(20);
if (div.IsHovered)
div.Color(0, 200, 0);
DivStart().Color(255, 0, 0).Dir(Dir.Horizontal).Padding(10);
DivStart().Gap(10);
DivStart(out var div).Color(0, 255, 0).Radius(20);
if (div.IsHovered)
div.Color(0, 200, 0);
DivEnd();
DivStart().Color(255, 255, 0).Padding(20).Gap(10);
Checkbox();
DropDown();
DivEnd();
DivEnd();
DivStart().Color(255, 255, 0).WidthFraction(50).HeightFraction(50).Padding(20).Gap(10);
Checkbox();
DropDown();
DivStart().Scroll().HeightFraction(70);
foreach (var x in Enumerable.Range(0, 30))
{
//ToDo remove memory allocation
DivStart(x.ToString()).Height(20).Color(10, 200, 100);
Text(x.ToString());
DivEnd();
}
DivEnd();
DivEnd();
}
Expand Down
47 changes: 40 additions & 7 deletions ImSharpUI.Sample/UiElements/UiContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IUiContainerBuilder
{
public IUiContainerBuilder Color(byte red, byte green, byte blue, byte alpha = 255);
public IUiContainerBuilder BorderColor(byte red, byte green, byte blue, byte alpha = 255);
public IUiContainerBuilder Scroll();
public IUiContainerBuilder Center();
public IUiContainerBuilder Width(float width);
public IUiContainerBuilder Height(float height);
Expand Down Expand Up @@ -164,6 +165,12 @@ public IUiContainerBuilder PaddingHorizontal(int paddingHorizontal)
return this;
}

public IUiContainerBuilder Scroll()
{
PCanScroll = true;
return this;
}

public IUiContainerBuilder PaddingVertical(int paddingVertical)
{
PQuadrant = PQuadrant with { Top = paddingVertical, Bottom = paddingVertical };
Expand Down Expand Up @@ -209,6 +216,7 @@ public IUiContainerBuilder Gap(int gap)
public bool IsHovered { get; set; }
public bool IsActive { get; set; }
public bool PCanScroll { get; set; }
public float ScrollPos { get; set; }

public override void Render(SKCanvas canvas)
{
Expand Down Expand Up @@ -259,20 +267,40 @@ public override void Render(SKCanvas canvas)
}
}

if (PCanScroll)
{
canvas.ClipRect(SKRect.Create(PComputedX, PComputedY, PComputedWidth, PComputedHeight));
}

foreach (var childElement in Children)
{
childElement.Render(canvas);
}

}

public override void Layout()
public override void Layout(Window window)
{
ComputeSize();
ComputeSize(out var totalChildSize);

if (PCanScroll)
{
if (totalChildSize > PComputedHeight)
{
ScrollPos = Math.Clamp(ScrollPos + window.ScrollDelta * 20, 0, totalChildSize - PComputedHeight);

}
else
{
ScrollPos = 0;
}
}

ComputePosition();

foreach (var childElement in Children)
{
childElement.Layout();
childElement.Layout(window);
}
}

Expand Down Expand Up @@ -325,15 +353,17 @@ public T AddChild<T>(UiElementId uiElementId) where T : UiElement, new()
return newChild;
}

private void ComputeSize()
private void ComputeSize(out float totalChildSize)
{
totalChildSize = 0;

switch (PDir)
{
case EnumDir.Horizontal or EnumDir.RowReverse:
ComputeRowSize();
break;
case EnumDir.Vertical or EnumDir.ColumnReverse:
ComputeColumnSize();
ComputeColumnSize(out totalChildSize);
break;
}
}
Expand Down Expand Up @@ -431,8 +461,10 @@ private float GetItemCrossAxisLength(UiElement item)
}


private void ComputeColumnSize()
private void ComputeColumnSize(out float totalChildrenSize)
{
totalChildrenSize = 0;

var remainingSize = RemainingMainAxisFixedSize();

var totalPercentage = 0f;
Expand Down Expand Up @@ -480,6 +512,7 @@ private void ComputeColumnSize()
item.PWidth.Value * 0.01),
_ => throw new ArgumentOutOfRangeException()
};
totalChildrenSize += item.PComputedHeight;
}
}

Expand Down Expand Up @@ -598,7 +631,7 @@ private float RemainingMainAxisSize()

private void RenderFlexStart()
{
var mainOffset = 0f;
var mainOffset = -ScrollPos;

foreach (var child in Children)
{
Expand Down
2 changes: 1 addition & 1 deletion ImSharpUI.Sample/UiElements/UiElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class UiElement

public float PComputedY { get; set; }
public abstract void Render(SKCanvas canvas);
public abstract void Layout();
public abstract void Layout(Window window);
}

public record struct UiElementId(string Key, string Path, int Line);
2 changes: 1 addition & 1 deletion ImSharpUI.Sample/UiElements/UiSvg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override void Render(SKCanvas canvas)
canvas.DrawPicture(SSvgCache[Src].Picture, ref matrix);
}

public override void Layout()
public override void Layout(Window window)
{

}
Expand Down
2 changes: 1 addition & 1 deletion ImSharpUI.Sample/UiElements/UiText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override void Render(SKCanvas canvas)
canvas.DrawText(Content, actualX, actualY, paint);
}

public override void Layout()
public override void Layout(Window window)
{
}

Expand Down
15 changes: 14 additions & 1 deletion ImSharpUI.Sample/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ public void Update()
_rootContainer.PComputedWidth = width;
_rootContainer.PComputedHeight = height;

_rootContainer.Layout();
_rootContainer.Layout(this);

ScrollDelta = 0;

_rootContainer.Render(surface.Canvas);

Expand All @@ -131,9 +133,20 @@ private void HandleEvents()
{
HandleMouseMove(e.motion);
}
else if (e.type == SDL_EventType.SDL_MOUSEWHEEL)
{
HandleScroll(e.wheel);
}
}
}

public int ScrollDelta { get; set; }

private void HandleScroll(SDL_MouseWheelEvent wheelEvent)
{
ScrollDelta -= wheelEvent.y;
}

private void HandleMouseClick(SDL_MouseMotionEvent eventMotion)
{
var div = ActualHitTest(_rootContainer, eventMotion.x, eventMotion.y);
Expand Down

0 comments on commit 6093893

Please sign in to comment.