Skip to content
This repository has been archived by the owner on Mar 6, 2018. It is now read-only.

Commit

Permalink
Allow UI children to be scrollable-aware (#194)
Browse files Browse the repository at this point in the history
This adds a new interface IScrollableAwareChild and an accompanying IScrollableAwareSkinRenderer interface for skin renderers.  When the scrollable container goes to render it's child, it will pass an additional rectangle indicating the area which will actually end up on screen.  This allows child controls to omit draw calls which are outside of the visible scrolled area.
  • Loading branch information
hach-que committed Jun 3, 2017
1 parent 5af9c8a commit f93ba7f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 9 deletions.
1 change: 1 addition & 0 deletions Build/Projects/Protogame.definition
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@
<Compile Include="UserInterface\Control\FileSelect.cs" />
<Compile Include="UserInterface\Control\FontViewer.cs" />
<Compile Include="UserInterface\Control\Form.cs" />
<Compile Include="UserInterface\Control\IScrollableAwareChild.cs" />
<Compile Include="UserInterface\Control\Label.cs" />
<Compile Include="UserInterface\Control\Link.cs" />
<Compile Include="UserInterface\Control\LinkState.cs" />
Expand Down
40 changes: 31 additions & 9 deletions Protogame/UserInterface/Control/Container/ScrollableContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,37 @@ public virtual void Render(IRenderContext context, ISkinLayout skinLayout, ISkin

try
{
_child?.Render(
context,
skinLayout,
skinDelegator,
new Rectangle(
-(int)(ScrollX * (System.Math.Max(childWidth, realLayoutWidth) - realLayoutWidth)),
-(int)(ScrollY * (System.Math.Max(childHeight, realLayoutHeight) - realLayoutHeight)),
childWidth,
childHeight));
var scrollableChild = _child as IScrollableAwareChild;

if (scrollableChild != null)
{
scrollableChild.Render(
context,
skinLayout,
skinDelegator,
new Rectangle(
-(int)(ScrollX * (System.Math.Max(childWidth, realLayoutWidth) - realLayoutWidth)),
-(int)(ScrollY * (System.Math.Max(childHeight, realLayoutHeight) - realLayoutHeight)),
childWidth,
childHeight),
new Rectangle(
0,
0,
realLayoutWidth,
realLayoutHeight));
}
else
{
_child?.Render(
context,
skinLayout,
skinDelegator,
new Rectangle(
-(int)(ScrollX * (System.Math.Max(childWidth, realLayoutWidth) - realLayoutWidth)),
-(int)(ScrollY * (System.Math.Max(childHeight, realLayoutHeight) - realLayoutHeight)),
childWidth,
childHeight));
}
}
finally
{
Expand Down
9 changes: 9 additions & 0 deletions Protogame/UserInterface/Control/IScrollableAwareChild.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Xna.Framework;

namespace Protogame
{
public interface IScrollableAwareChild
{
void Render(IRenderContext context, ISkinLayout skinLayout, ISkinDelegator skinDelegator, Rectangle layout, Rectangle renderedLayout);
}
}
15 changes: 15 additions & 0 deletions Protogame/UserInterface/Skin/DefaultSkinDelegator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.Xna.Framework;
using Protoinject;

Expand All @@ -23,5 +24,19 @@ public DefaultSkinDelegator(IKernel kernel)
var implementation = _kernel.Get<ISkinRenderer<TContainer>>();
return implementation.MeasureText(context, text, container);
}

public void Render<TContainer>(IRenderContext renderContext, Rectangle layout, Rectangle renderedLayout, TContainer container) where TContainer : IContainer
{
var implementation = _kernel.Get<ISkinRenderer<TContainer>>();
var scrollableImplementation = implementation as IScrollableAwareSkinRenderer<TContainer>;
if (scrollableImplementation != null)
{
scrollableImplementation.Render(renderContext, layout, renderedLayout, container);
}
else
{
implementation.Render(renderContext, layout, container);
}
}
}
}
6 changes: 6 additions & 0 deletions Protogame/UserInterface/Skin/ISkinDelegator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ public interface ISkinDelegator
Rectangle layout,
TContainer container) where TContainer : IContainer;

void Render<TContainer>(
IRenderContext renderContext,
Rectangle layout,
Rectangle renderedLayout,
TContainer container) where TContainer : IContainer;

Vector2 MeasureText<TContainer>(IRenderContext context, string text, TContainer container)
where TContainer : IContainer;
}
Expand Down
9 changes: 9 additions & 0 deletions Protogame/UserInterface/Skin/ISkinRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ public interface ISkinRenderer<in TContainer> where TContainer : IContainer

Vector2 MeasureText(IRenderContext renderContext, string text, TContainer container);
}

public interface IScrollableAwareSkinRenderer<in TContainer> : ISkinRenderer<TContainer> where TContainer : IContainer
{
void Render(
IRenderContext renderContext,
Rectangle layout,
Rectangle renderedLayout,
TContainer container);
}
}

0 comments on commit f93ba7f

Please sign in to comment.