Skip to content

Commit

Permalink
Change: Move cursor support slightly to allow LibRender2 to set
Browse files Browse the repository at this point in the history
  • Loading branch information
leezer3 committed Oct 8, 2023
1 parent e90be0f commit 79d370d
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 122 deletions.
7 changes: 7 additions & 0 deletions source/LibRender2/BaseRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1663,5 +1663,12 @@ public void RenderFaceImmediateMode(ObjectState State, MeshFace Face, Matrix4D m
GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();
}

/// <summary>Sets the current MouseCursor</summary>
/// <param name="newCursor">The new cursor</param>
public virtual void SetCursor(OpenTK.MouseCursor newCursor)
{

}
}
}
79 changes: 73 additions & 6 deletions source/LibRender2/Cursors/Cursor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System.Drawing;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using OpenBveApi.Interface;

namespace LibRender2.Cursors
namespace LibRender2
{
public class MouseCursor
{
Expand All @@ -24,7 +28,7 @@ public MouseCursor(BaseRenderer renderer, string fileName, Bitmap image)
if (stream != null)
{
Bitmap Plus = new Bitmap(stream);
using (var g = System.Drawing.Graphics.FromImage(Plus))
using (var g = Graphics.FromImage(Plus))
{
g.DrawImage(image, 0.0f, 0.0f, image.Width, image.Height);
var data = Plus.LockBits(new Rectangle(0, 0, Plus.Width, Plus.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Expand All @@ -35,7 +39,7 @@ public MouseCursor(BaseRenderer renderer, string fileName, Bitmap image)
else
{
Bitmap Plus = new Bitmap(OpenBveApi.Path.CombineFile(Renderer.fileSystem.GetDataFolder(), "Cursors\\Symbols\\plus.png"));
using (var g = System.Drawing.Graphics.FromImage(Plus))
using (var g = Graphics.FromImage(Plus))
{
g.DrawImage(image, 0.0f, 0.0f, image.Width, image.Height);
var data = Plus.LockBits(new Rectangle(0, 0, Plus.Width, Plus.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Expand All @@ -49,7 +53,7 @@ public MouseCursor(BaseRenderer renderer, string fileName, Bitmap image)
if (stream != null)
{
Bitmap Minus = new Bitmap(stream);
using (var g = System.Drawing.Graphics.FromImage(Minus))
using (var g = Graphics.FromImage(Minus))
{
g.DrawImage(image, 0.0f, 0.0f, image.Width, image.Height);
var data = Minus.LockBits(new Rectangle(0, 0, Minus.Width, Minus.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Expand All @@ -60,7 +64,7 @@ public MouseCursor(BaseRenderer renderer, string fileName, Bitmap image)
else
{
Bitmap Minus = new Bitmap(OpenBveApi.Path.CombineFile(Renderer.fileSystem.GetDataFolder(), "Cursors\\Symbols\\minus.png"));
using (var g = System.Drawing.Graphics.FromImage(Minus))
using (var g = Graphics.FromImage(Minus))
{
g.DrawImage(image, 0.0f, 0.0f, image.Width, image.Height);
var data = Minus.LockBits(new Rectangle(0, 0, Minus.Width, Minus.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Expand Down Expand Up @@ -89,4 +93,67 @@ public override string ToString()
return FileName;
}
}

public static class AvailableCursors
{
public static readonly List<MouseCursor> CursorList = new List<MouseCursor>();
public static OpenTK.MouseCursor CurrentCursor;
public static OpenTK.MouseCursor CurrentCursorPlus;
public static OpenTK.MouseCursor CurrentCursorMinus;
public static OpenTK.MouseCursor ScrollCursor;
internal static BaseRenderer Renderer;
public static void LoadCursorImages(BaseRenderer renderer, string CursorFolder)
{
Renderer = renderer;
if (!Directory.Exists(CursorFolder))
{
Renderer.currentHost.AddMessage(MessageType.Error, true, "Failed to load the default cursor images- Falling back to embedded.");
LoadEmbeddedCursorImages();
return;
}

string[] CursorImageFiles = Directory.GetFiles(CursorFolder);

foreach (var File in CursorImageFiles)
{
try
{
using (var Fs= new FileStream(File, FileMode.Open, FileAccess.Read))
{
if (File.EndsWith("scroll.png", StringComparison.InvariantCultureIgnoreCase))
{
Bitmap Image = new Bitmap(Fs);
MouseCursor c = new MouseCursor(Renderer, Path.GetFileName(File), Image);
ScrollCursor = c.MyCursor;
}
else
{
Bitmap Image = new Bitmap(Fs);
CursorList.Add(new MouseCursor(Renderer, Path.GetFileName(File), Image));
}


}
}
catch
{
// ignored
}
}
}

private static void LoadEmbeddedCursorImages()
{
var thisAssembly = Assembly.GetExecutingAssembly();
using (var stream = thisAssembly.GetManifestResourceStream("OpenBve.nk.png"))
{
if (stream != null)
{
Bitmap Image = new Bitmap(stream);
CursorList.Add(new MouseCursor(Renderer, "nk.png", Image));
}
}
Renderer.currentOptions.CursorFileName = "nk.png";
}
}
}
11 changes: 10 additions & 1 deletion source/LibRender2/Primitives/GLControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class GLControl
internal readonly BaseRenderer Renderer;
/// <summary>The background color for the control</summary>
public Color128 BackgroundColor;
/// <summary>The texture for the picturebox</summary>
/// <summary>The texture for the control</summary>
public Texture Texture;
/// <summary>The stored location for the control</summary>
public Vector2 Location;
Expand All @@ -23,6 +23,15 @@ protected GLControl(BaseRenderer renderer)
Renderer = renderer;
}

/// <summary>Draws the control</summary>
public abstract void Draw();

/// <summary>Passes a mouse move event to the control</summary>
/// <param name="x">The absolute screen X co-ordinate</param>
/// <param name="y">The absolute screen Y co-ordinate</param>
public virtual void MouseMove(int x, int y)
{

}
}
}
28 changes: 15 additions & 13 deletions source/LibRender2/Primitives/Textbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using OpenBveApi.Colors;
using OpenBveApi.Graphics;
using OpenBveApi.Math;
using OpenBveApi.Textures;

namespace LibRender2.Primitives
{
Expand All @@ -19,10 +18,7 @@ public class Textbox : GLControl
/// <summary>The string contents of the textbox</summary>
public string Text
{
get
{
return myText;
}
get => myText;
set
{
myText = value;
Expand All @@ -42,13 +38,7 @@ public string Text
/// <summary>Whether the textbox can scroll</summary>
public bool CanScroll;
/// <summary>Used for internal size calculations</summary>
private Vector2 internalSize
{
get
{
return CanScroll ? new Vector2(Size.X, Size.Y - 12) : Size;
}
}
private Vector2 internalSize => CanScroll ? new Vector2(Size.X, Size.Y - 12) : Size;

private List<string> WrappedLines(int width)
{
Expand Down Expand Up @@ -161,6 +151,18 @@ public override void Draw()
}
}


public override void MouseMove(int x, int y)
{
if (x > Location.X && x < Location.X + Size.X && y > Location.Y && y < Location.Y + Size.Y)
{
CurrentlySelected = true;
Renderer.SetCursor(CanScroll ? AvailableCursors.ScrollCursor : OpenTK.MouseCursor.Default);
}
else
{
Renderer.SetCursor(OpenTK.MouseCursor.Default);
CurrentlySelected = false;
}
}
}
}
3 changes: 1 addition & 2 deletions source/LibRender2/Trains/Panel/TouchElement.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using LibRender2.Cursors;
using OpenBveApi.Objects;
using OpenBveApi.Objects;

namespace LibRender2.Trains
{
Expand Down
13 changes: 2 additions & 11 deletions source/OpenBVE/Game/Menu/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public void PushMenu(MenuType type, int data = 0, bool replace = false)
if (Program.Renderer.CurrentInterface < InterfaceType.Menu)
{
// Deliberately set to the standard cursor, as touch controls may have set to something else
Program.currentGameWindow.Cursor = MouseCursor.Default;
Program.Renderer.SetCursor(MouseCursor.Default);
}
if (!isInitialized)
Init();
Expand Down Expand Up @@ -353,16 +353,7 @@ internal bool ProcessMouseMove(int x, int y)
}
if (menu.Type == MenuType.RouteList || menu.Type == MenuType.TrainList || menu.Type == MenuType.PackageInstall || menu.Type == MenuType.Packages || (int)menu.Type >= 107)
{
if (x > routeDescriptionBox.Location.X && x < routeDescriptionBox.Location.X + routeDescriptionBox.Size.X && y > routeDescriptionBox.Location.Y && y < routeDescriptionBox.Location.Y + routeDescriptionBox.Size.Y)
{
routeDescriptionBox.CurrentlySelected = true;
Program.currentGameWindow.Cursor = routeDescriptionBox.CanScroll ? Cursors.ScrollCursor : MouseCursor.Default;
}
else
{
routeDescriptionBox.CurrentlySelected = false;
Program.currentGameWindow.Cursor = MouseCursor.Default;
}
routeDescriptionBox.MouseMove(x, y);
//HACK: Use this to trigger our menu start button!
if (x > Program.Renderer.Screen.Width - 200 && x < Program.Renderer.Screen.Width - 10 && y > Program.Renderer.Screen.Height - 40 && y < Program.Renderer.Screen.Height - 10)
{
Expand Down
5 changes: 5 additions & 0 deletions source/OpenBVE/Graphics/NewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,11 @@ internal void RenderScene(double TimeElapsed, double RealTimeElapsed)
OptionLighting = true;
}

public override void SetCursor(OpenTK.MouseCursor newCursor)
{
Program.currentGameWindow.Cursor = newCursor;
}

public NewRenderer(HostInterface CurrentHost, BaseOptions CurrentOptions, FileSystem FileSystem) : base(CurrentHost, CurrentOptions, FileSystem)
{
}
Expand Down
1 change: 0 additions & 1 deletion source/OpenBVE/Graphics/Renderers/Touch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using LibRender2;
using LibRender2.Cursors;
using LibRender2.Trains;
using OpenBveApi.Interface;
using OpenBveApi.Math;
Expand Down
81 changes: 8 additions & 73 deletions source/OpenBVE/System/Cursor.cs
Original file line number Diff line number Diff line change
@@ -1,85 +1,20 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using OpenBveApi.Interface;
using OpenTK;
using LibRender2;

namespace OpenBve
{
internal static class Cursors
{
internal static readonly List<LibRender2.Cursors.MouseCursor> CursorList = new List<LibRender2.Cursors.MouseCursor>();
internal static MouseCursor CurrentCursor;
internal static MouseCursor CurrentCursorPlus;
internal static MouseCursor CurrentCursorMinus;
internal static MouseCursor ScrollCursor;

internal static void LoadCursorImages(string CursorFolder)
{
if (!Directory.Exists(CursorFolder))
{
MessageBox.Show(@"The default cursor images have been moved or deleted.", Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
LoadEmbeddedCursorImages();
return;
}

string[] CursorImageFiles = Directory.GetFiles(CursorFolder);

foreach (var File in CursorImageFiles)
{
try
{
using (var Fs= new FileStream(File, FileMode.Open, FileAccess.Read))
{
if (File.EndsWith("scroll.png", StringComparison.InvariantCultureIgnoreCase))
{
Bitmap Image = new Bitmap(Fs);
LibRender2.Cursors.MouseCursor c = new LibRender2.Cursors.MouseCursor(Program.Renderer, Path.GetFileName(File), Image);
ScrollCursor = c.MyCursor;
}
else
{
Bitmap Image = new Bitmap(Fs);
CursorList.Add(new LibRender2.Cursors.MouseCursor(Program.Renderer, Path.GetFileName(File), Image));
}


}
}
catch
{
// ignored
}
}
}

private static void LoadEmbeddedCursorImages()
{
var thisAssembly = Assembly.GetExecutingAssembly();
using (var stream = thisAssembly.GetManifestResourceStream("OpenBve.nk.png"))
{
if (stream != null)
{
Bitmap Image = new Bitmap(stream);
CursorList.Add(new LibRender2.Cursors.MouseCursor(Program.Renderer, "nk.png", Image));
}
}
Interface.CurrentOptions.CursorFileName = "nk.png";
}

internal static void ListCursors(ComboBox comboBoxCursor)
{
comboBoxCursor.Items.Clear();

//Load all available cursors
int idx = -1;
for (int i = 0; i < CursorList.Count; i++)
for (int i = 0; i < LibRender2.AvailableCursors.CursorList.Count; i++)
{
comboBoxCursor.Items.Add(CursorList[i]);
if (CursorList[i].FileName == Interface.CurrentOptions.CursorFileName)
comboBoxCursor.Items.Add(LibRender2.AvailableCursors.CursorList[i]);
if (LibRender2.AvailableCursors.CursorList[i].FileName == Interface.CurrentOptions.CursorFileName)
{
idx = i;
}
Expand All @@ -98,15 +33,15 @@ internal static void SelectedCursor(ComboBox comboboxCursor, PictureBox pictureb
{
return;
}
LibRender2.Cursors.MouseCursor c = comboboxCursor.Items[i] as LibRender2.Cursors.MouseCursor;
MouseCursor c = comboboxCursor.Items[i] as MouseCursor;
if (c == null)
{
return;
}
Interface.CurrentOptions.CursorFileName = c.FileName;
CurrentCursor = c.MyCursor;
CurrentCursorPlus = c.MyCursorPlus;
CurrentCursorMinus = c.MyCursorMinus;
LibRender2.AvailableCursors.CurrentCursor = c.MyCursor;
LibRender2.AvailableCursors.CurrentCursorPlus = c.MyCursorPlus;
LibRender2.AvailableCursors.CurrentCursorMinus = c.MyCursorMinus;
pictureboxCursor.Image = c.Image;
}
}
Expand Down

0 comments on commit 79d370d

Please sign in to comment.