182 lines
6.0 KiB
C#
182 lines
6.0 KiB
C#
using System;
|
|
using System.Windows.Forms;
|
|
using System.Windows;
|
|
using SharpGL.Enumerations;
|
|
using SharpGL.Version;
|
|
using SharpGL;
|
|
using System.ComponentModel;
|
|
using Graphics;
|
|
using Graphics.Objects;
|
|
using System.Numerics;
|
|
using System.Drawing;
|
|
|
|
namespace JuicyGraphics {
|
|
class canvas : Control, ISupportInitialize {
|
|
|
|
OpenGL GL = new OpenGL();
|
|
renderCam rc;
|
|
|
|
public canvas() {
|
|
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
|
|
SetStyle(ControlStyles.UserPaint, true);
|
|
InitializeOpenGL();
|
|
rc = new renderCam(GL, this);
|
|
}
|
|
|
|
void InitializeOpenGL() {
|
|
GL.Create(OpenGLVersion.OpenGL4_3, RenderContextType.NativeWindow, Width, Height, 32, this.Handle);
|
|
GL.ShadeModel(OpenGL.GL_SMOOTH);
|
|
GL.ClearDepth(1.0f);
|
|
GL.Enable(OpenGL.GL_DEPTH_TEST);
|
|
GL.DepthFunc(OpenGL.GL_LEQUAL);
|
|
GL.Hint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_NICEST);
|
|
GL.Viewport(0, 0, Width, Height);
|
|
}
|
|
|
|
bool renderingForDesigner() {
|
|
if (this == null)
|
|
return false;
|
|
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
|
|
return true;
|
|
Control p = this.Parent;
|
|
while (p != null) {
|
|
if (p.GetType().FullName.Contains(".DesignerFrame"))
|
|
return true;
|
|
p = p.Parent;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
protected override void OnPaintBackground(PaintEventArgs e) {
|
|
if (renderingForDesigner()) { base.OnPaintBackground(e); }
|
|
return;
|
|
}
|
|
|
|
gridBackground gBg = new gridBackground();
|
|
|
|
protected override void OnPaint(PaintEventArgs e) {
|
|
if (renderingForDesigner()) {
|
|
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
|
|
SetStyle(ControlStyles.UserPaint, true);
|
|
InitializeOpenGL();
|
|
}
|
|
GL.MakeCurrent();
|
|
GL.ClearColor(0.9f, 0.9f, 0.9f, 1f);
|
|
GL.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
|
|
|
|
GL.LoadIdentity();
|
|
|
|
if (rc != null) {
|
|
GL.Color(0.8, 0.8, 0.8);
|
|
gBg.render(rc);
|
|
}
|
|
|
|
GL.Ortho(-(Width / 2160.0), Width / 2160.0,
|
|
-(Height / 2160.0), Height / 2160.0,
|
|
-1, 1);
|
|
if (rc != null)
|
|
rc.attacheMatrix();
|
|
|
|
GL.Begin(BeginMode.Triangles);
|
|
GL.Color(0.9, 0.83, 0.1);
|
|
GL.Vertex(1.0, 1.0);
|
|
GL.Vertex(-1.0, -1.0);
|
|
GL.Vertex(-1.0, 1.0);
|
|
|
|
GL.Color(0, 0, 0);
|
|
line testLine = new line(new Vector2(-0.4f, 0.4f), new Vector2(0.4f, -0.4f));
|
|
testLine.render(rc);
|
|
|
|
GL.End();
|
|
GL.Flush();
|
|
|
|
var handleDeviceContext = e.Graphics.GetHdc();
|
|
GL.Blit(handleDeviceContext);
|
|
e.Graphics.ReleaseHdc(handleDeviceContext);
|
|
}
|
|
|
|
protected override void OnSizeChanged(EventArgs e) {
|
|
if (GL.RenderContextProvider == null)
|
|
return;
|
|
GL.SetDimensions(Width, Height);
|
|
GL.Viewport(0, 0, Width, Height);
|
|
Invalidate();
|
|
}
|
|
|
|
enum mouseInteractionMode { none, left, middle, right };
|
|
mouseInteractionMode pressedMouseButton = mouseInteractionMode.none;
|
|
Point prevMousePosition = Point.Empty;
|
|
|
|
protected override void OnMouseDown(MouseEventArgs e) {
|
|
switch (e.Button) {
|
|
case MouseButtons.Left:
|
|
pressedMouseButton = mouseInteractionMode.left;
|
|
break;
|
|
case MouseButtons.Middle:
|
|
pressedMouseButton = mouseInteractionMode.middle;
|
|
break;
|
|
case MouseButtons.Right:
|
|
pressedMouseButton = mouseInteractionMode.right;
|
|
break;
|
|
}
|
|
prevMousePosition = MousePosition;
|
|
this.Focus();
|
|
}
|
|
|
|
protected override void OnMouseMove(MouseEventArgs e) {
|
|
switch (pressedMouseButton) {
|
|
case mouseInteractionMode.left:
|
|
break;
|
|
case mouseInteractionMode.middle:
|
|
rc.translation += new Vector2(MousePosition.X - prevMousePosition.X,
|
|
prevMousePosition.Y - MousePosition.Y) / 1080.0f;
|
|
Invalidate();
|
|
break;
|
|
case mouseInteractionMode.right:
|
|
break;
|
|
}
|
|
prevMousePosition = MousePosition;
|
|
}
|
|
|
|
protected override void OnMouseUp(MouseEventArgs e) {
|
|
switch (e.Button) {
|
|
case MouseButtons.Left:
|
|
if (pressedMouseButton == mouseInteractionMode.left)
|
|
pressedMouseButton = mouseInteractionMode.none;
|
|
break;
|
|
case MouseButtons.Middle:
|
|
if (pressedMouseButton == mouseInteractionMode.middle)
|
|
pressedMouseButton = mouseInteractionMode.none;
|
|
break;
|
|
case MouseButtons.Right:
|
|
if (pressedMouseButton == mouseInteractionMode.right)
|
|
pressedMouseButton = mouseInteractionMode.none;
|
|
break;
|
|
}
|
|
}
|
|
|
|
Control prevFocused = null;
|
|
|
|
protected override void OnMouseEnter(EventArgs e) {
|
|
prevFocused = Form.ActiveForm.ActiveControl;
|
|
this.Focus();
|
|
}
|
|
|
|
protected override void OnMouseLeave(EventArgs e) {
|
|
prevFocused.Focus();
|
|
}
|
|
|
|
protected override void OnMouseWheel(MouseEventArgs e) {
|
|
Form.ActiveForm.Text = e.Delta.ToString();
|
|
rc.scale *= Math.Pow(1.0008, e.Delta);
|
|
Invalidate();
|
|
}
|
|
|
|
public void BeginInit() { }
|
|
|
|
public void EndInit() {
|
|
InitializeOpenGL();
|
|
OnSizeChanged(null);
|
|
}
|
|
}
|
|
} |