Skip to content

Commit 330205a

Browse files
authored
Merge pull request #478 from Newbilius/master
Optional anisotropic filtering added to component "Image"
2 parents 5c7bc37 + e47e18b commit 330205a

File tree

3 files changed

+90
-16
lines changed

3 files changed

+90
-16
lines changed

src/Myra/Graphics2D/RenderContext.cs

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,29 @@ namespace Myra.Graphics2D
2525
public enum TextureFiltering
2626
{
2727
Nearest,
28-
Linear
29-
}
28+
Linear,
29+
Anisotropic
30+
}
3031

3132
public partial class RenderContext : IDisposable
3233
{
3334
#if MONOGAME || FNA
3435
private static RasterizerState _uiRasterizerState;
35-
36-
private static RasterizerState UIRasterizerState
36+
private static SamplerState _textureFilteringAnisotropic = new SamplerState
37+
{
38+
Filter = TextureFilter.Anisotropic,
39+
AddressU = TextureAddressMode.Clamp,
40+
AddressV = TextureAddressMode.Clamp,
41+
AddressW = TextureAddressMode.Clamp,
42+
BorderColor = Color.Transparent,
43+
MaxAnisotropy = 16,
44+
MaxMipLevel = 16,
45+
MipMapLevelOfDetailBias = 0f,
46+
ComparisonFunction = CompareFunction.Never,
47+
FilterMode = TextureFilterMode.Default
48+
};
49+
50+
private static RasterizerState UIRasterizerState
3751
{
3852
get
3953
{
@@ -105,6 +119,7 @@ internal Rectangle DeviceScissor
105119
}
106120
}
107121

122+
private bool _isAnisotropicFilteringOn;
108123

109124
public Rectangle Scissor
110125
{
@@ -255,7 +270,7 @@ public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? s
255270
/// <param name="depth"></param>
256271
public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 scale, float depth = 0.0f)
257272
{
258-
SetTextureFiltering(TextureFiltering.Nearest);
273+
SetTextureFiltering(_isAnisotropicFilteringOn ? TextureFiltering.Anisotropic : TextureFiltering.Nearest);
259274
color = CrossEngineStuff.MultiplyColor(color, Opacity);
260275
scale *= Transform.Scale;
261276
rotation += Transform.Rotation;
@@ -430,7 +445,7 @@ public void DrawRichText(RichTextLayout richText, Vector2 position, Color color,
430445
public void Begin()
431446
{
432447
#if MONOGAME || FNA
433-
var samplerState = _textureFiltering == TextureFiltering.Nearest ? SamplerState.PointClamp : SamplerState.LinearClamp;
448+
var samplerState = SelectedSamplerState();
434449

435450
_renderer.Begin(SpriteSortMode.Deferred,
436451
BlendState.AlphaBlend,
@@ -439,9 +454,8 @@ public void Begin()
439454
UIRasterizerState,
440455
null);
441456
#elif STRIDE
442-
var samplerState = _textureFiltering == TextureFiltering.Nearest ?
443-
MyraEnvironment.Game.GraphicsDevice.SamplerStates.PointClamp :
444-
MyraEnvironment.Game.GraphicsDevice.SamplerStates.LinearClamp;
457+
var samplerState = SelectedSamplerState();
458+
445459
_renderer.Begin(MyraEnvironment.Game.GraphicsContext,
446460
SpriteSortMode.Deferred,
447461
BlendStates.AlphaBlend,
@@ -455,7 +469,39 @@ public void Begin()
455469
_beginCalled = true;
456470
}
457471

458-
public void End()
472+
#if MONOGAME || FNA
473+
private SamplerState SelectedSamplerState()
474+
{
475+
switch (_textureFiltering)
476+
{
477+
case TextureFiltering.Nearest:
478+
return SamplerState.PointClamp;
479+
case TextureFiltering.Linear:
480+
return SamplerState.LinearClamp;
481+
case TextureFiltering.Anisotropic:
482+
return _textureFilteringAnisotropic;
483+
default:
484+
throw new ArgumentOutOfRangeException();
485+
}
486+
}
487+
#elif STRIDE
488+
private SamplerState SelectedSamplerState()
489+
{
490+
switch (_textureFiltering)
491+
{
492+
case TextureFiltering.Nearest:
493+
return MyraEnvironment.Game.GraphicsDevice.SamplerStates.PointClamp;
494+
case TextureFiltering.Linear:
495+
return MyraEnvironment.Game.GraphicsDevice.SamplerStates.LinearClamp;
496+
case TextureFiltering.Anisotropic:
497+
return MyraEnvironment.Game.GraphicsDevice.SamplerStates.AnisotropicClamp;
498+
default:
499+
throw new ArgumentOutOfRangeException();
500+
}
501+
}
502+
#endif
503+
504+
public void End()
459505
{
460506
_renderer.End();
461507
_beginCalled = false;
@@ -489,5 +535,10 @@ public void Dispose()
489535
{
490536
ReleaseUnmanagedResources();
491537
}
492-
}
538+
539+
public void SetAnisotropicFilteringMode(bool isAnisotropicFiltering)
540+
{
541+
_isAnisotropicFilteringOn = isAnisotropicFiltering;
542+
}
543+
}
493544
}

src/Myra/Graphics2D/UI/Containers/ScrollViewer.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,13 @@ public IImage VerticalScrollKnob
135135
get; set;
136136
}
137137

138-
[Browsable(false)]
138+
[Category("Appearance")]
139+
public int ScrollMultiplier
140+
{
141+
get; set;
142+
} = 10;
143+
144+
[Browsable(false)]
139145
[Content]
140146
public override Widget Content
141147
{
@@ -395,7 +401,7 @@ public override void OnMouseWheel(float delta)
395401
return;
396402
}
397403

398-
var step = 10 * ScrollMaximum.Y / _thumbMaximumY;
404+
var step = ScrollMultiplier * ScrollMaximum.Y / _thumbMaximumY;
399405
if (delta < 0)
400406
{
401407
_scrollbarOrientation = Orientation.Vertical;

src/Myra/Graphics2D/UI/Simple/Image.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,22 @@ public class Image : Widget, IPressable
3535
{
3636
private IImage _image, _overImage, _pressedImage;
3737

38-
[Category("Appearance")]
38+
private bool _isAnisotropicFiltering = false;
39+
40+
public bool IsAnisotropicFiltering
41+
{
42+
get
43+
{
44+
return _isAnisotropicFiltering;
45+
}
46+
set
47+
{
48+
_isAnisotropicFiltering = value;
49+
InvalidateMeasure();
50+
}
51+
}
52+
53+
[Category("Appearance")]
3954
public IImage Renderable
4055
{
4156
get
@@ -164,8 +179,10 @@ public override void InternalRender(RenderContext context)
164179
bounds.Height = (int)(bounds.Width * aspect);
165180
}
166181

167-
image.Draw(context, bounds, Color);
168-
}
182+
context.SetAnisotropicFilteringMode(_isAnisotropicFiltering);
183+
image.Draw(context, bounds, Color);
184+
context.SetAnisotropicFilteringMode(false);
185+
}
169186
}
170187

171188
public void ApplyPressableImageStyle(PressableImageStyle imageStyle)

0 commit comments

Comments
 (0)