Skip to content

Commit f4ec019

Browse files
author
ands
committed
Added support for OpenTK.GLControl and a corresponding example project
1 parent 3a6d01d commit f4ec019

19 files changed

Lines changed: 1074 additions & 46 deletions

GLGUI/GLGUI.Example/GLGUI.Example.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@
3232
</PropertyGroup>
3333
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
3434
<ItemGroup>
35+
<Reference Include="OpenTK.GLControl, Version=1.1.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
36+
<SpecificVersion>False</SpecificVersion>
37+
<HintPath>..\bin\OpenTK.GLControl.dll</HintPath>
38+
</Reference>
3539
<Reference Include="System.Core" />
3640
<Reference Include="System" />
3741
<Reference Include="System.Drawing" />
38-
<Reference Include="System.Windows.Forms" />
3942
<Reference Include="OpenTK">
4043
<HintPath>..\bin\OpenTK.dll</HintPath>
4144
</Reference>
45+
<Reference Include="System.Windows.Forms" />
4246
</ItemGroup>
4347
<ItemGroup>
4448
<Compile Include="LineWriter.cs" />

GLGUI/GLGUI.Example/Program.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Windows.Forms;
32
using System.Threading;
43

54
namespace GLGUI.Example
@@ -9,8 +8,6 @@ public static class Program
98
[STAThread]
109
public static int Main(string[] args)
1110
{
12-
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
13-
Application.ThreadException += new ThreadExceptionEventHandler(OnGuiUnhandedException);
1411
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
1512

1613
new MainForm().Run();
@@ -23,7 +20,7 @@ private static void HandleUnhandledException(Object o)
2320

2421
if (e != null)
2522
{
26-
MessageBox.Show(e.ToString());
23+
Console.WriteLine(e.ToString());
2724
}
2825
}
2926

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{BE449F2C-0E37-4D16-9E5B-D30D3041759B}</ProjectGuid>
9+
<OutputType>WinExe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>GLGUI.GLControlExample</RootNamespace>
12+
<AssemblyName>GLGUI.GLControlExample</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>..\bin\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>..\bin\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="OpenTK">
38+
<HintPath>..\bin\OpenTK.dll</HintPath>
39+
</Reference>
40+
<Reference Include="OpenTK.GLControl">
41+
<HintPath>..\bin\OpenTK.GLControl.dll</HintPath>
42+
</Reference>
43+
<Reference Include="System" />
44+
<Reference Include="System.Core" />
45+
<Reference Include="System.Drawing" />
46+
<Reference Include="System.Windows.Forms" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="MainForm.cs">
50+
<SubType>Form</SubType>
51+
</Compile>
52+
<Compile Include="LineWriter.cs" />
53+
<Compile Include="GuiControl.cs">
54+
<SubType>UserControl</SubType>
55+
</Compile>
56+
<Compile Include="Program.cs" />
57+
<Compile Include="Properties\AssemblyInfo.cs" />
58+
<EmbeddedResource Include="Properties\Resources.resx">
59+
<Generator>ResXFileCodeGenerator</Generator>
60+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
61+
<SubType>Designer</SubType>
62+
</EmbeddedResource>
63+
<Compile Include="Properties\Resources.Designer.cs">
64+
<AutoGen>True</AutoGen>
65+
<DependentUpon>Resources.resx</DependentUpon>
66+
</Compile>
67+
<None Include="Properties\Settings.settings">
68+
<Generator>SettingsSingleFileGenerator</Generator>
69+
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
70+
</None>
71+
<Compile Include="Properties\Settings.Designer.cs">
72+
<AutoGen>True</AutoGen>
73+
<DependentUpon>Settings.settings</DependentUpon>
74+
<DesignTimeSharedInput>True</DesignTimeSharedInput>
75+
</Compile>
76+
</ItemGroup>
77+
<ItemGroup>
78+
<ProjectReference Include="..\GLGUI\GLGUI.csproj">
79+
<Project>{D0281455-4FAF-4AF6-ABE4-8F9E7A25828D}</Project>
80+
<Name>GLGUI</Name>
81+
</ProjectReference>
82+
</ItemGroup>
83+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
84+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
85+
Other similar extension points exist, see Microsoft.Common.targets.
86+
<Target Name="BeforeBuild">
87+
</Target>
88+
<Target Name="AfterBuild">
89+
</Target>
90+
-->
91+
</Project>
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
using System;
2+
using System.Drawing;
3+
using OpenTK;
4+
using OpenTK.Graphics;
5+
using OpenTK.Graphics.OpenGL;
6+
using System.Windows.Forms;
7+
using System.Diagnostics;
8+
9+
namespace GLGUI.GLControlExample
10+
{
11+
public class GuiControl : OpenTK.GLControl
12+
{
13+
GLGui glgui;
14+
GLLabel fpsLabel;
15+
GLLabel console;
16+
LineWriter consoleWriter;
17+
18+
Stopwatch stopwatch;
19+
int fpsCounter = 0;
20+
int fpsSecond = 1;
21+
double time = 0.0;
22+
23+
public GuiControl() : base(new GraphicsMode(new ColorFormat(8, 8, 8, 8), 24, 0, 4))
24+
{
25+
consoleWriter = new LineWriter();
26+
Console.SetOut(consoleWriter);
27+
Console.SetError(consoleWriter);
28+
29+
this.Load += OnLoad;
30+
}
31+
32+
private void OnLoad(object sender, EventArgs e)
33+
{
34+
VSync = false; // vsync is nice, but you can't really measure performance while it's on
35+
36+
glgui = new GLGui(this);
37+
38+
var mainAreaControl = glgui.Add(new GLGroupLayout(glgui) { Size = new Size(ClientSize.Width, ClientSize.Height - 200), Anchor = GLAnchorStyles.All });
39+
// change background color:
40+
var mainSkin = mainAreaControl.Skin;
41+
mainSkin.BackgroundColor = glgui.Skin.FormActive.BackgroundColor;
42+
mainSkin.BorderColor = glgui.Skin.FormActive.BorderColor;
43+
mainAreaControl.Skin = mainSkin;
44+
45+
var consoleScrollControl = glgui.Add(new GLScrollableControl(glgui) { Outer = new Rectangle(0, ClientSize.Height - 200, ClientSize.Width, 200), Anchor = GLAnchorStyles.Left | GLAnchorStyles.Right | GLAnchorStyles.Bottom });
46+
console = consoleScrollControl.Add(new GLLabel(glgui) { AutoSize = true });
47+
48+
fpsLabel = mainAreaControl.Add(new GLLabel(glgui) { Location = new Point(10, 10), AutoSize = true });
49+
// change font and background color:
50+
var skin = fpsLabel.SkinEnabled;
51+
skin.Font = new GLFont(new Font("Arial", 12.0f));
52+
skin.BackgroundColor = glgui.Skin.TextBoxActive.BackgroundColor;
53+
fpsLabel.SkinEnabled = skin;
54+
55+
var helloWorldForm = mainAreaControl.Add(new GLForm(glgui) { Title = "Hello World", Outer = new Rectangle(50, 100, 200, 150), AutoSize = false });
56+
helloWorldForm.Add(new GLForm(glgui) { Title = "Hello Form", Outer = new Rectangle(100, 32, 100, 100), AutoSize = false })
57+
.MouseMove += (s, w) => Console.WriteLine(w.Position.ToString());
58+
59+
var flow = helloWorldForm.Add(new GLFlowLayout(glgui) { FlowDirection = GLFlowDirection.BottomUp, Location = new Point(10, 10), Size = helloWorldForm.InnerSize, AutoSize = true });
60+
for (int i = 0; i < 5; i++)
61+
flow.Add(new GLButton(glgui) { Text = "Button" + i, Size = new Size(150, 0) })
62+
.Click += (s, w) => Console.WriteLine(s + " pressed.");
63+
flow.Add(new GLButton(glgui) { Text = "Hide Cursor", Size = new Size(150, 0) })
64+
.Click += (s, w) => glgui.Cursor = GLCursor.None;
65+
66+
var loremIpsumForm = mainAreaControl.Add(new GLForm(glgui) { Title = "Lorem Ipsum", Location = new Point(600, 100), Size = new Size(300, 200) });
67+
loremIpsumForm.Add(new GLTextBox(glgui) {
68+
Text = "Lorem ipsum dolor sit amet,\nconsetetur sadipscing elitr,\nsed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,\nsed diam voluptua.\n\nAt vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.",
69+
Multiline = true,
70+
WordWrap = true,
71+
Outer = new Rectangle(4, 4, loremIpsumForm.Inner.Width - 8, loremIpsumForm.Inner.Height - 8),
72+
Anchor = GLAnchorStyles.All
73+
}).Changed += (s, w) => Console.WriteLine(s + " text length: " + ((GLTextBox)s).Text.Length);
74+
75+
var fixedSizeForm = mainAreaControl.Add(new GLForm(glgui) { Title = "Fixed size Form", Location = new Point(64, 300), Size = new Size(100, 200), AutoSize = true });
76+
var fooBarFlow = fixedSizeForm.Add(new GLFlowLayout(glgui) { FlowDirection = GLFlowDirection.TopDown, AutoSize = true });
77+
fooBarFlow.Add(new GLCheckBox(glgui) { Text = "CheckBox A", AutoSize = true })
78+
.Changed += (s, w) => Console.WriteLine(s + ": " + ((GLCheckBox)s).Checked);
79+
fooBarFlow.Add(new GLCheckBox(glgui) { Text = "CheckBox B", AutoSize = true })
80+
.Changed += (s, w) => Console.WriteLine(s + ": " + ((GLCheckBox)s).Checked);
81+
fooBarFlow.Add(new GLCheckBox(glgui) { Text = "Totally different CheckBox", AutoSize = true })
82+
.Changed += (s, w) => Console.WriteLine(s + ": " + ((GLCheckBox)s).Checked);
83+
fooBarFlow.Add(new GLCheckBox(glgui) { Text = "Go away!", AutoSize = true })
84+
.Changed += (s, w) => Console.WriteLine(s + ": " + ((GLCheckBox)s).Checked);
85+
86+
for (int i = 0; i < 3; i++)
87+
{
88+
var viewportForm = mainAreaControl.Add(new GLForm(glgui) { Title = "Cube", Location = new Point(300 + i * 16, 64 + i * 16), Size = new Size(200, 200) });
89+
viewportForm.Add(new GLViewport(glgui) { Size = viewportForm.InnerSize, Anchor = GLAnchorStyles.All })
90+
.RenderViewport += OnRenderViewport;
91+
}
92+
93+
stopwatch = new Stopwatch();
94+
Resize += (s, ev) => GL.Viewport(ClientSize);
95+
Paint += OnRender;
96+
97+
stopwatch.Start();
98+
Application.Idle += (s, ev) => Invalidate();
99+
}
100+
101+
private void OnRender(object sender, PaintEventArgs e)
102+
{
103+
stopwatch.Stop();
104+
double delta = stopwatch.Elapsed.TotalMilliseconds * 0.001;
105+
stopwatch.Restart();
106+
time += delta;
107+
108+
if (time >= fpsSecond)
109+
{
110+
fpsLabel.Text = string.Format("Application: {0:0}FPS. GLGUI: {1:0.0}ms", fpsCounter, glgui.RenderDuration);
111+
fpsCounter = 0;
112+
fpsSecond++;
113+
}
114+
115+
if (consoleWriter.Changed)
116+
{
117+
console.Text = string.Join("\n", consoleWriter.Lines);
118+
consoleWriter.Changed = false;
119+
}
120+
121+
glgui.Render();
122+
SwapBuffers();
123+
124+
fpsCounter++;
125+
}
126+
127+
// draws a simple colored cube in a GLViewport control
128+
private void OnRenderViewport(object sender, double deltaTime)
129+
{
130+
var viewport = (GLViewport)sender;
131+
132+
GL.Enable(EnableCap.DepthTest);
133+
GL.ClearColor(0, 0, 0, 1);
134+
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
135+
136+
GL.MatrixMode(MatrixMode.Projection);
137+
var proj = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90.0f), viewport.AspectRatio, 1.0f, 100.0f);
138+
GL.LoadMatrix(ref proj);
139+
GL.MatrixMode(MatrixMode.Modelview);
140+
GL.LoadIdentity();
141+
142+
GL.Translate(0, 0, -2.0f);
143+
GL.Rotate(time * 100.0f, 1, 0, 0);
144+
GL.Rotate(time * 42.0f, 0, 1, 0);
145+
146+
GL.Begin(PrimitiveType.Quads);
147+
GL.Color3(1.0, 0.0, 0.0);
148+
GL.Vertex3(0.5, -0.5, -0.5);
149+
GL.Color3(0.0, 1.0, 0.0);
150+
GL.Vertex3(0.5, 0.5, -0.5);
151+
GL.Color3(0.0, 0.0, 1.0);
152+
GL.Vertex3(-0.5, 0.5, -0.5);
153+
GL.Color3(1.0, 0.0, 1.0);
154+
GL.Vertex3(-0.5, -0.5, -0.5);
155+
156+
GL.Color3(1.0, 1.0, 1.0);
157+
GL.Vertex3(0.5, -0.5, 0.5);
158+
GL.Vertex3(0.5, 0.5, 0.5);
159+
GL.Vertex3(-0.5, 0.5, 0.5);
160+
GL.Vertex3(-0.5, -0.5, 0.5);
161+
162+
GL.Color3(1.0, 0.0, 1.0);
163+
GL.Vertex3(0.5, -0.5, -0.5);
164+
GL.Vertex3(0.5, 0.5, -0.5);
165+
GL.Vertex3(0.5, 0.5, 0.5);
166+
GL.Vertex3(0.5, -0.5, 0.5);
167+
168+
GL.Color3(0.0, 1.0, 0.0);
169+
GL.Vertex3(-0.5, -0.5, 0.5);
170+
GL.Vertex3(-0.5, 0.5, 0.5);
171+
GL.Vertex3(-0.5, 0.5, -0.5);
172+
GL.Vertex3(-0.5, -0.5, -0.5);
173+
174+
GL.Color3(0.0, 0.0, 1.0);
175+
GL.Vertex3(0.5, 0.5, 0.5);
176+
GL.Vertex3(0.5, 0.5, -0.5);
177+
GL.Vertex3(-0.5, 0.5, -0.5);
178+
GL.Vertex3(-0.5, 0.5, 0.5);
179+
180+
GL.Color3(1.0, 0.0, 0.0);
181+
GL.Vertex3(0.5, -0.5, -0.5);
182+
GL.Vertex3(0.5, -0.5, 0.5);
183+
GL.Vertex3(-0.5, -0.5, 0.5);
184+
GL.Vertex3(-0.5, -0.5, -0.5);
185+
GL.End();
186+
187+
GL.Disable(EnableCap.DepthTest);
188+
}
189+
190+
protected override bool IsInputKey(Keys key)
191+
{
192+
switch (key)
193+
{
194+
case Keys.Up:
195+
case Keys.Down:
196+
case Keys.Right:
197+
case Keys.Left:
198+
case Keys.Tab:
199+
return true;
200+
}
201+
return base.IsInputKey(key);
202+
}
203+
}
204+
}
205+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text;
4+
5+
namespace GLGUI.GLControlExample
6+
{
7+
public class LineWriter : TextWriter
8+
{
9+
public List<string> Lines;
10+
public bool Changed = false;
11+
private StringBuilder currentLine;
12+
13+
public LineWriter()
14+
{
15+
this.Lines = new List<string>(1024);
16+
this.currentLine = new StringBuilder(256);
17+
}
18+
19+
public override void Write(char value)
20+
{
21+
if (value == '\n')
22+
Flush();
23+
else
24+
currentLine.Append(value);
25+
}
26+
27+
public override void Flush()
28+
{
29+
Lines.Add(currentLine.ToString());
30+
if (Lines.Count > 1024)
31+
Lines.RemoveAt(0);
32+
currentLine.Clear();
33+
Changed = true;
34+
}
35+
36+
public override Encoding Encoding
37+
{
38+
get { return System.Text.Encoding.UTF8; }
39+
}
40+
41+
public void Clear()
42+
{
43+
Lines.Clear();
44+
currentLine.Clear();
45+
Changed = true;
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)