Skip to content

Commit b0bd28b

Browse files
committed
support modify graph preview
1 parent ee5ca94 commit b0bd28b

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

clmath/Graph.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using Silk.NET.Maths;
1+
using Silk.NET.GLFW;
2+
using Silk.NET.Input;
3+
using Silk.NET.Maths;
24
using Silk.NET.OpenGL;
35
using Silk.NET.Windowing;
6+
using Silk.NET.Windowing.Glfw;
47

58
namespace clmath;
69

@@ -38,8 +41,8 @@ public sealed class GraphWindow : IDisposable
3841

3942
private uint ax_vao;
4043
private uint ax_vbo;
41-
private readonly double scaleX = 15;
42-
private readonly double scaleY = 6;
44+
private double scaleX = 15;
45+
private double scaleY = 6;
4346
private uint shaders;
4447
private readonly Component[] x;
4548

@@ -48,7 +51,7 @@ static GraphWindow()
4851
AssemblyDir = new FileInfo(typeof(GraphWindow).Assembly.Location).Directory!;
4952
}
5053

51-
public GraphWindow(params Component[] fx)
54+
public unsafe GraphWindow(params Component[] fx)
5255
{
5356
if (fx.Length > maxFuncs)
5457
{
@@ -58,7 +61,6 @@ public GraphWindow(params Component[] fx)
5861

5962
this.fx = fx.ToList();
6063
window = Window.Create(WindowOptions.Default);
61-
window.Title = "2D Graph";
6264

6365
var fxn = fx.Length;
6466
ctx = new MathContext[fxn];
@@ -73,14 +75,35 @@ public GraphWindow(params Component[] fx)
7375
ctx[i].var[key] = x[i] = new Component { type = Component.Type.Num, arg = (double)0 };
7476
}
7577

78+
window.Title = "2D Graph";
7679
window.Load += Load;
7780
window.FramebufferResize += Resize;
7881
window.Render += Render;
7982
window.Closing += Dispose;
83+
window.Initialize();
84+
foreach (var keyboard in window.CreateInput().Keyboards)
85+
keyboard.KeyDown += KeyDown;
8086
window.Run();
8187
}
8288

89+
private void KeyDown(IKeyboard keyboard, Key key, int _)
90+
{
91+
if (key == Key.Escape)
92+
window.Close();
93+
const double delta = 0.5;
94+
if (key == Key.Keypad2 && scaleY > delta)
95+
scaleY -= delta;
96+
if (key == Key.Keypad8)
97+
scaleY += delta;
98+
if (key == Key.Keypad4 && scaleX > delta)
99+
scaleX -= delta;
100+
if (key == Key.Keypad6)
101+
scaleX += delta;
102+
InitGraphCurve();
103+
}
104+
83105
private IWindow window { get; }
106+
84107
private GL gl { get; set; }
85108

86109
public void Dispose()

clmath/clmath.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
</PropertyGroup>
2121

2222
<ItemGroup>
23-
<PackageReference Include="Antlr4.Runtime.Standard" Version="4.11.1"/>
24-
<PackageReference Include="Silk.NET.OpenGL" Version="2.16.0"/>
25-
<PackageReference Include="Silk.NET.Windowing" Version="2.16.0"/>
23+
<PackageReference Include="Antlr4.Runtime.Standard" Version="4.11.1" />
24+
<PackageReference Include="Silk.NET.Input" Version="2.16.0" />
25+
<PackageReference Include="Silk.NET.OpenGL" Version="2.16.0" />
26+
<PackageReference Include="Silk.NET.Windowing" Version="2.16.0" />
2627
</ItemGroup>
2728

2829
<ItemGroup>

0 commit comments

Comments
 (0)