Skip to content

Commit 820d301

Browse files
committed
make MouseCursor default null, cache it in a property instead of loading it over and over again from the lookup
1 parent fd23033 commit 820d301

File tree

6 files changed

+66
-26
lines changed

6 files changed

+66
-26
lines changed

Framework/Intersect.Framework.Core/Configuration/ClientConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public void Validate()
203203
/// <summary>
204204
/// Sets a custom mouse cursor.
205205
/// </summary>
206-
public string MouseCursor { get; set; } = string.Empty;
206+
public string? MouseCursor { get; set; }
207207

208208
/// <summary>
209209
/// Determines the time it takes to fade-in or fade-out a song when no other instructions are given.

Intersect.Client.Core/Core/Graphics.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,14 @@ public static FloatRect CurrentView
121121
//Init Functions
122122
public static void InitGraphics()
123123
{
124-
Renderer?.Init();
124+
Renderer.Init();
125125
sContentManager = Globals.ContentManager;
126126
sContentManager.LoadAll();
127+
128+
// If we're going to be rendering a custom mouse cursor, hide the default one!
129+
string? mouseCursor = ClientConfiguration.Instance.MouseCursor;
130+
Renderer.CursorName = mouseCursor;
131+
127132
(GameFont, GameFontSize) = FindFont(ClientConfiguration.Instance.GameFont);
128133
(UIFont, UIFontSize) = FindFont(ClientConfiguration.Instance.UIFont);
129134
(EntityNameFont, EntityNameFontSize) = FindFont(ClientConfiguration.Instance.EntityNameFont);
@@ -617,22 +622,14 @@ public static void Render(TimeSpan deltaTime, TimeSpan totalTime)
617622
);
618623

619624
// Draw our mousecursor at the very end, but not when taking screenshots.
620-
if (!takingScreenshot && !string.IsNullOrWhiteSpace(ClientConfiguration.Instance.MouseCursor))
625+
if (!takingScreenshot && Renderer.Cursor is { } cursorTexture)
621626
{
622-
var cursorTexture = Globals.ContentManager.GetTexture(
623-
TextureType.Misc,
624-
ClientConfiguration.Instance.MouseCursor
627+
var cursorPosition = ConvertToWorldPointNoZoom(Globals.InputManager.GetMousePosition());
628+
DrawGameTexture(
629+
cursorTexture,
630+
cursorPosition.X,
631+
cursorPosition.Y
625632
);
626-
627-
if (cursorTexture is not null)
628-
{
629-
var cursorPosition = ConvertToWorldPointNoZoom(Globals.InputManager.GetMousePosition());
630-
DrawGameTexture(
631-
cursorTexture,
632-
cursorPosition.X,
633-
cursorPosition.Y
634-
);
635-
}
636633
}
637634

638635
renderer.End();

Intersect.Client.Core/MonoGame/Graphics/MonoRenderer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Globalization;
33
using System.Runtime.CompilerServices;
44
using Intersect.Client.Classes.MonoGame.Graphics;
5+
using Intersect.Client.Framework.Content;
6+
using Intersect.Client.Framework.File_Management;
57
using Intersect.Client.Framework.GenericClasses;
68
using Intersect.Client.Framework.Graphics;
79
using Intersect.Client.General;
@@ -1032,6 +1034,11 @@ public override System.Numerics.Vector2 MeasureText(string? text, IFont? font, i
10321034
private readonly Dictionary<SpriteFont, char> _defaultCharacterForSpriteFont = [];
10331035
private GameShader _basicShader;
10341036

1037+
protected override void OnCursorChanged(IGameTexture? newCursor)
1038+
{
1039+
_game.IsMouseVisible = newCursor is null;
1040+
}
1041+
10351042
private string SanitizeText(string text, SpriteFont spriteFont)
10361043
{
10371044
if (!_defaultCharacterForSpriteFont.TryGetValue(spriteFont, out var defaultCharacter))

Intersect.Client.Core/MonoGame/IntersectGame.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,8 @@ private IntersectGame(IClientContext context, Action postStartupAction)
127127
) / new Microsoft.Xna.Framework.Point(2);
128128
Window.AllowAltF4 = false;
129129

130-
// Store frequently used property values in local variables.
131-
string mouseCursor = ClientConfiguration.Instance.MouseCursor;
132-
string? updateUrl = ClientConfiguration.Instance.UpdateUrl;
133-
134-
// If we're going to be rendering a custom mouse cursor, hide the default one!
135-
if (!string.IsNullOrWhiteSpace(mouseCursor))
136-
{
137-
IsMouseVisible = false;
138-
}
139-
140130
// Reuse Updater object instead of creating a new one each time.
131+
string? updateUrl = ClientConfiguration.Instance.UpdateUrl;
141132
if (!string.IsNullOrWhiteSpace(updateUrl))
142133
{
143134
_updater ??= new Updater(updateUrl, "client/update.json", "version.client.json", 7);

Intersect.Client.Framework/Graphics/GameRenderer.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Collections.Concurrent;
22
using System.Diagnostics;
33
using System.Numerics;
4+
using Intersect.Client.Framework.Content;
5+
using Intersect.Client.Framework.File_Management;
46
using Intersect.Client.Framework.GenericClasses;
57
using Intersect.Core;
68
using Intersect.Framework.Collections;
@@ -153,6 +155,45 @@ public float Scale
153155

154156
public Resolution PreferredResolution { get; set; }
155157

158+
private string? _cursorName;
159+
private IGameTexture? _cursor;
160+
161+
public string? CursorName
162+
{
163+
get => _cursorName;
164+
set
165+
{
166+
if (value == _cursorName)
167+
{
168+
return;
169+
}
170+
171+
_cursorName = value;
172+
_cursor = string.IsNullOrWhiteSpace(_cursorName)
173+
? null
174+
: GameContentManager.Current.GetTexture(TextureType.Misc, _cursorName);
175+
OnCursorChanged(_cursor);
176+
}
177+
}
178+
179+
public IGameTexture? Cursor
180+
{
181+
get => _cursor;
182+
set
183+
{
184+
if (value == _cursor)
185+
{
186+
return;
187+
}
188+
189+
_cursor = value;
190+
_cursorName = _cursor?.Name;
191+
OnCursorChanged(_cursor);
192+
}
193+
}
194+
195+
protected abstract void OnCursorChanged(IGameTexture? newCursor);
196+
156197
/// <summary>
157198
/// Clears everything off the render target with a specified color.
158199
/// </summary>

Intersect.Client.Framework/Graphics/IGameRenderer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public interface IGameRenderer
6666
/// </summary>
6767
List<string> ValidVideoModes { get; }
6868

69+
string? CursorName { get; set; }
70+
71+
IGameTexture? Cursor { get; set; }
72+
6973
/// <summary>
7074
/// Clear the screen.
7175
/// </summary>

0 commit comments

Comments
 (0)