Skip to content

Commit 59b2d1e

Browse files
committed
Vector3 RotatedBy and RotatedByDegrees now preserve a vector's Z value
Added FlatRedBallServices.ReplaceTextureCalled event ReplaceTexture automatically replaces AnimationChains textures. Fixed replace file call from live edit not properly sending files Improved file reloading during game running Added better error message on NaN velocity Added GumIdb.ReplaceTexture
1 parent 88ef6c6 commit 59b2d1e

File tree

8 files changed

+179
-35
lines changed

8 files changed

+179
-35
lines changed

Engines/FlatRedBallXNA/FlatRedBall/ExtensionMethods/Vector3ExtensionMethods.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,13 @@ public static Vector3 RotatedBy(this Vector3 vector3, float radiansToRotateBy)
160160
}
161161
else
162162
{
163+
var oldZ = vector3.Z;
164+
163165
var existingAngle = vector3.Angle().Value;
164166
var newAngle = existingAngle + radiansToRotateBy;
165-
return FromAngle(newAngle) * vector3.Length();
167+
var toReturn = FromAngle(newAngle) * vector3.Length();
168+
toReturn.Z = oldZ;
169+
return toReturn;
166170
}
167171
}
168172

@@ -174,9 +178,13 @@ public static Vector3 RotatedByDegrees(this Vector3 vector3, float degreesToRota
174178
}
175179
else
176180
{
181+
var oldZ = vector3.Z;
182+
177183
var existingAngle = vector3.AngleDegrees().Value;
178184
var newAngle = existingAngle + degreesToRotateBy;
179-
return FromAngleDegrees(newAngle) * vector3.Length();
185+
var toReturn = FromAngleDegrees(newAngle) * vector3.Length();
186+
toReturn.Z = oldZ;
187+
return toReturn;
180188
}
181189
}
182190

Engines/FlatRedBallXNA/FlatRedBall/FlatRedBallServices.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ public static string EncryptionKey
270270
public static event EventHandler Suspending;
271271
public static event EventHandler Unsuspending;
272272

273+
public static event Action<Texture2D, Texture2D> ReplaceTextureCalled;
274+
273275
#endregion
274276

275277
#region Event Methods
@@ -1055,6 +1057,8 @@ public static void ReplaceTexture(Texture2D oldTexture, Texture2D newTexture)
10551057
// Replace the texture for all objects managed by all of the managers
10561058
SpriteManager.ReplaceTexture(oldTexture, newTexture);
10571059
TextManager.ReplaceTexture(oldTexture, newTexture);
1060+
1061+
ReplaceTextureCalled?.Invoke(oldTexture, newTexture);
10581062
}
10591063

10601064
#region XML Docs

Engines/FlatRedBallXNA/FlatRedBall/PositionedObject.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,22 @@ public virtual void TimedActivity(float secondDifference,
21262126
}
21272127
#endregion
21282128

2129+
2130+
#if DEBUG
2131+
if(float.IsNaN(Velocity.X))
2132+
{
2133+
throw new Exception("Bad VelocityX value");
2134+
}
2135+
if (float.IsNaN(Velocity.Y))
2136+
{
2137+
throw new Exception("Bad VelocityY value");
2138+
}
2139+
if (float.IsNaN(Velocity.Z))
2140+
{
2141+
throw new Exception("Bad VelocityZ value");
2142+
}
2143+
#endif
2144+
21292145
Position.X += (float)(Velocity.X * secondDifference + Acceleration.X * secondDifferenceSquaredDividedByTwo);
21302146
Position.Y += (float)(Velocity.Y * secondDifference + Acceleration.Y * secondDifferenceSquaredDividedByTwo);
21312147
Position.Z += (float)(Velocity.Z * secondDifference + Acceleration.Z * secondDifferenceSquaredDividedByTwo);

Engines/FlatRedBallXNA/FlatRedBall/SpriteManager.cs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,39 +2644,15 @@ public static void ShuffleInternalLists()
26442644
#endregion
26452645
public static void ReplaceTexture(Texture2D oldTexture, Texture2D newTexture)
26462646
{
2647-
float oldScaleX = 0;
2648-
float oldScaleY = 0;
26492647

26502648
foreach (Sprite sprite in mAutomaticallyUpdatedSprites)
26512649
{
2652-
if (sprite.Texture == oldTexture)
2653-
{
2654-
// This could change the PixelSize. The user may have set the PixelSize,
2655-
// but then overrode it, so let's preserve it.
2656-
oldScaleX = sprite.ScaleX;
2657-
oldScaleY = sprite.ScaleY;
2658-
2659-
sprite.Texture = newTexture;
2660-
2661-
sprite.ScaleX = oldScaleX;
2662-
sprite.ScaleY = oldScaleY;
2663-
}
2650+
ReplaceTexture(oldTexture, newTexture, sprite);
26642651
}
26652652

26662653
foreach (Sprite sprite in mManuallyUpdatedSprites)
26672654
{
2668-
if (sprite.Texture == oldTexture)
2669-
{
2670-
// This could change the PixelSize. The user may have set the PixelSize,
2671-
// but then overrode it, so let's preserve it.
2672-
oldScaleX = sprite.ScaleX;
2673-
oldScaleY = sprite.ScaleY;
2674-
2675-
sprite.Texture = newTexture;
2676-
2677-
sprite.ScaleX = oldScaleX;
2678-
sprite.ScaleY = oldScaleY;
2679-
}
2655+
ReplaceTexture(oldTexture, newTexture, sprite);
26802656
}
26812657

26822658

@@ -2705,6 +2681,38 @@ public static void ReplaceTexture(Texture2D oldTexture, Texture2D newTexture)
27052681

27062682
}
27072683

2684+
private static void ReplaceTexture(Texture2D oldTexture, Texture2D newTexture, Sprite sprite)
2685+
{
2686+
float oldScaleX = 0;
2687+
float oldScaleY = 0;
2688+
2689+
if (sprite.Texture == oldTexture)
2690+
{
2691+
// This could change the PixelSize. The user may have set the PixelSize,
2692+
// but then overrode it, so let's preserve it.
2693+
oldScaleX = sprite.ScaleX;
2694+
oldScaleY = sprite.ScaleY;
2695+
2696+
sprite.Texture = newTexture;
2697+
2698+
sprite.ScaleX = oldScaleX;
2699+
sprite.ScaleY = oldScaleY;
2700+
}
2701+
if (sprite.AnimationChains != null)
2702+
{
2703+
foreach (var animation in sprite.AnimationChains)
2704+
{
2705+
foreach (var frame in animation)
2706+
{
2707+
if (frame.Texture == oldTexture)
2708+
{
2709+
frame.Texture = newTexture;
2710+
}
2711+
}
2712+
}
2713+
}
2714+
}
2715+
27082716
#region Sort Methods
27092717

27102718
static public void SortTexturesSecondary()

FRBDK/Glue/GameCommunicationPlugin/GlueControl/Embedded/CommandReceiver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ private static void HandleDto(Dtos.ForceReloadFileDto dto)
13791379

13801380
if (file != null)
13811381
{
1382-
GlobalContent.Reload(dto);
1382+
GlobalContent.Reload(file);
13831383
}
13841384

13851385

FRBDK/Glue/GameCommunicationPlugin/GlueControl/Managers/RefreshManager.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,15 @@ await TaskManager.Self.WaitForTaskToFinish(
218218
try
219219
{
220220
var extension = fileName.Extension;
221-
var shouldReloadFile = extension == "csv";
221+
var shouldReloadFile =
222+
extension == "csv" ||
223+
extension == "png";
224+
222225

223226
var shouldReloadScreen = false;
224227

228+
229+
225230
if (shouldReloadFile)
226231
{
227232
printOutput($"Sending force reload for file: {strippedName}");
@@ -237,7 +242,11 @@ await TaskManager.Self.WaitForTaskToFinish(
237242

238243
// Typically localization is applied in custom code, so we can't
239244
// apply these changes without reloading the screen
240-
shouldReloadScreen = dto.IsLocalizationDatabase;
245+
shouldReloadScreen = dto.IsLocalizationDatabase ||
246+
// FRB tries to update files, but .achx files don't
247+
// update their references which causes disposed errors.
248+
// Therefore, we're going to reload the screen on .png reload
249+
extension == "png";
241250
}
242251
else
243252
{
@@ -251,7 +260,10 @@ await TaskManager.Self.WaitForTaskToFinish(
251260
GlueCommands.Self.PrintOutput($"Telling game to restart screen {DateTime.Now}");
252261

253262
var dto = new RestartScreenDto();
254-
dto.ReloadGlobalContent = isGlobalContent;
263+
// If we do this, we invalidate all global content including
264+
// content that may be handled outside of the global content files
265+
// which causes crashes. Instead, we should rely on reload file calls
266+
//dto.ReloadGlobalContent = isGlobalContent;
255267
await CommandSender.Self.Send(dto);
256268
}
257269
handled = true;

FRBDK/Glue/Glue/CodeGeneration/ReferencedFileSaveCodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ public static void GetReload(ReferencedFileSave referencedFile, IElement contain
10041004
{
10051005
codeBlock.Line($"var oldTexture = {referencedFile.GetInstanceName()};");
10061006
GetInitializationForReferencedFile(referencedFile, container, codeBlock, loadType);
1007-
codeBlock.Line($"FlatRedBall.SpriteManager.ReplaceTexture(oldTexture, {referencedFile.GetInstanceName()});");
1007+
codeBlock.Line($"FlatRedBall.FlatRedBallServices.ReplaceTexture(oldTexture, {referencedFile.GetInstanceName()});");
10081008
}
10091009
else if (ati?.CustomReloadFunc != null)
10101010
{

FRBDK/Glue/GumPlugin/GumPlugin/Embedded/GumIdb.cs

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public static void StaticInitialize(string projectFileName)
135135
if (mManagers != null) return;
136136
////////////////End Early Out//////////////////////////
137137

138-
if(FlatRedBall.Content.ContentManager.CreateLoadSections)
138+
if (FlatRedBall.Content.ContentManager.CreateLoadSections)
139139
{
140140
FlatRedBall.Performance.Measurement.Section.GetAndStartContextAndTime("Loading Gum project " + projectFileName);
141141
}
@@ -650,6 +650,8 @@ RenderingLibrary.Graphics.IVisible RenderingLibrary.Graphics.IVisible.Parent
650650
}
651651
}
652652

653+
#endregion
654+
653655
// temporary to allow Glue to attach. Eventually I'll change how Glue generates this code
654656
public void CopyAbsoluteToRelative()
655657
{
@@ -661,6 +663,100 @@ public void AttachTo(PositionedObject newParent, bool throwaway)
661663
Parent = newParent;
662664
}
663665

664-
#endregion
666+
public void ReplaceTexture(Texture2D oldTexture, Texture2D newTexture)
667+
{
668+
var layers = mManagers.Renderer.Layers;
669+
670+
foreach (var layer in layers)
671+
{
672+
foreach (var renderable in layer.Renderables)
673+
{
674+
ReplaceTextureOnRenderable(oldTexture, newTexture, renderable);
675+
}
676+
}
677+
}
678+
679+
private static void ReplaceTextureOnRenderable(Texture2D oldTexture, Texture2D newTexture, RenderingLibrary.Graphics.IRenderableIpso renderable)
680+
{
681+
ReplaceTexturesOnThisRenderable(oldTexture, newTexture, renderable);
682+
if(renderable is GraphicalUiElement gue && gue.RenderableComponent != null)
683+
{
684+
ReplaceTexturesOnThisRenderable(oldTexture, newTexture, gue.RenderableComponent);
685+
}
686+
687+
if (renderable.Children != null)
688+
{
689+
foreach (var child in renderable.Children)
690+
{
691+
ReplaceTextureOnRenderable(oldTexture, newTexture, child);
692+
}
693+
}
694+
}
695+
696+
private static void ReplaceTexturesOnThisRenderable(Texture2D oldTexture, Texture2D newTexture, RenderingLibrary.Graphics.IRenderable renderable)
697+
{
698+
if (renderable is RenderingLibrary.Graphics.Sprite asSprite)
699+
{
700+
if (asSprite.Texture == oldTexture)
701+
{
702+
asSprite.Texture = newTexture;
703+
}
704+
}
705+
else if (renderable is RenderingLibrary.Graphics.NineSlice asNineSlice)
706+
{
707+
if (asNineSlice.TopLeftTexture == oldTexture)
708+
{
709+
asNineSlice.TopLeftTexture = newTexture;
710+
}
711+
if (asNineSlice.TopTexture == oldTexture)
712+
{
713+
asNineSlice.TopTexture = newTexture;
714+
}
715+
if (asNineSlice.TopRightTexture == oldTexture)
716+
{
717+
asNineSlice.TopRightTexture = newTexture;
718+
}
719+
if (asNineSlice.RightTexture == oldTexture)
720+
{
721+
asNineSlice.RightTexture = newTexture;
722+
}
723+
if (asNineSlice.BottomRightTexture == oldTexture)
724+
{
725+
asNineSlice.BottomRightTexture = newTexture;
726+
}
727+
if (asNineSlice.BottomTexture == oldTexture)
728+
{
729+
asNineSlice.BottomTexture = newTexture;
730+
}
731+
if (asNineSlice.BottomLeftTexture == oldTexture)
732+
{
733+
asNineSlice.BottomLeftTexture = newTexture;
734+
}
735+
if (asNineSlice.LeftTexture == oldTexture)
736+
{
737+
asNineSlice.LeftTexture = newTexture;
738+
}
739+
if (asNineSlice.CenterTexture == oldTexture)
740+
{
741+
asNineSlice.CenterTexture = newTexture;
742+
}
743+
}
744+
else if (renderable is RenderingLibrary.Graphics.Text asText)
745+
{
746+
var bitmapFont = asText.BitmapFont;
747+
if (bitmapFont != null)
748+
{
749+
750+
for (int i = 0; i < bitmapFont.Textures.Length; i++)
751+
{
752+
if (bitmapFont.Textures[i] == oldTexture)
753+
{
754+
bitmapFont.Textures[i] = newTexture;
755+
}
756+
}
757+
758+
}
759+
}
760+
}
665761
}
666762
}

0 commit comments

Comments
 (0)