Skip to content

Commit

Permalink
better debug resizing window tools
Browse files Browse the repository at this point in the history
saint11 committed Oct 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent a8f1cb7 commit f29d381
Showing 3 changed files with 55 additions and 21 deletions.
43 changes: 24 additions & 19 deletions src/Murder.Editor/Systems/EditorSystem.cs
Original file line number Diff line number Diff line change
@@ -116,7 +116,20 @@ public void DrawGui(RenderContext render, Context context)
ImGui.SameLine();
if (ImGui.Button("4x"))
{
ResizeWindow(3, render);
ResizeWindow(4, render);
}

ImGui.SameLine();
if (ImGui.Button("1080p"))
{
ResizeWindow(render, new Point(1920, 1080));
}


ImGui.SameLine();
if (ImGui.Button("1440p"))
{
ResizeWindow(render, new Point(2560, 1440));
}

ImGui.SeparatorText("Time");
@@ -237,25 +250,9 @@ public void DrawGui(RenderContext render, Context context)
ImGui.SameLine();
if (ImGui.Button("Resize Window")) // "Resize Window
{
// Parse the string to get the width and height
// First we remove any letters and other symbols, like [ ]
string clean = _windowSize.Replace("px", "").Replace("[", "").Replace("]", "");
string[] parts = clean.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

if (parts.Length == 2)
{
if (int.TryParse(parts[0], out int width) && int.TryParse(parts[1], out int height))
{
_windowWidth = width;
_windowHeight = height;
}
}

Game.Instance.Fullscreen = false;
Point windowSize = new Point(_windowWidth, _windowHeight);
Game.Instance.SetWindowSize(windowSize, false);
Game.Instance.GraphicsDeviceManager.ApplyChanges();
render.RefreshWindow(Game.GraphicsDevice, windowSize, new Point(render.Viewport.NativeResolution.X, render.Viewport.NativeResolution.Y), Game.Profile.ResizeStyle);
Point windowSize = Calculator.ParsePixelSize(_windowSize);
ResizeWindow(render, windowSize);
}

if (ImGui.Button("Refresh Resolution"))
@@ -304,6 +301,14 @@ public void DrawGui(RenderContext render, Context context)

}

private static void ResizeWindow(RenderContext render, Point windowSize)
{
Game.Instance.Fullscreen = false;
Game.Instance.SetWindowSize(windowSize, false);
Game.Instance.GraphicsDeviceManager.ApplyChanges();
render.RefreshWindow(Game.GraphicsDevice, windowSize, new Point(Game.Profile.GameWidth, Game.Profile.GameHeight), Game.Profile.ResizeStyle);
}

private static void ResizeWindow(float scale, RenderContext render)
{
Point windowSize = (new Vector2(render.Viewport.NativeResolution.X, render.Viewport.NativeResolution.Y) * scale).Point();
4 changes: 2 additions & 2 deletions src/Murder/Core/Input/PlayerInput.cs
Original file line number Diff line number Diff line change
@@ -236,9 +236,9 @@ public bool Released(int button)
return _buttons[button].Released;
}

public bool Pressed(Keys enter)
public bool Pressed(Keys key)
{
return Keyboard.GetState().IsKeyDown(enter);
return Keyboard.GetState().IsKeyDown(key) && !_previousKeyboardState.IsKeyDown(key);
}

public bool PressedAndConsume(int button)
29 changes: 29 additions & 0 deletions src/Murder/Utilities/Calculator.cs
Original file line number Diff line number Diff line change
@@ -785,5 +785,34 @@ public static byte MultiplyUnsigned8Bit(byte a, int b)
return (byte)((v >> 8) + v >> 8);
}
#endregion

#region Strings

public static Point ParsePixelSize(string size)
{
// Remove any non-numeric characters except commas, spaces, and 'x'
string clean = new string(size
.Where(c => char.IsDigit(c) || c == ',' || c == ' ' || c == 'x')
.ToArray());

// Replace 'x' with a comma to standardize the format
clean = clean.Replace('x', ',').Replace(" ", ",");

// Split into width and height
string[] parts = clean.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

if (parts.Length == 2)
{
if (int.TryParse(parts[0], out int width) && int.TryParse(parts[1], out int height))
{
return new Point(width, height);
}
}

// Return a default size in case of failure
return new Point(800, 600); // Or handle the error as needed
}

#endregion
}
}

0 comments on commit f29d381

Please sign in to comment.