329 lines
11 KiB
C#
329 lines
11 KiB
C#
|
//using Microsoft.VisualBasic;
|
|||
|
//using System;
|
|||
|
//using System.Collections.Generic;
|
|||
|
//using System.ComponentModel;
|
|||
|
//using System.Data;
|
|||
|
//using System.Drawing;
|
|||
|
//using System.Linq;
|
|||
|
//using System.Runtime.InteropServices;
|
|||
|
//using System.Text;
|
|||
|
//using System.Threading.Tasks;
|
|||
|
//using System.Windows.Forms;
|
|||
|
|
|||
|
//namespace LensSimulator_WinForms.Viewport
|
|||
|
//{
|
|||
|
// public partial class WorkspaceViewport : UserControl
|
|||
|
// {
|
|||
|
// PictureBox pictureBox = new PictureBox
|
|||
|
// {
|
|||
|
// Dock = DockStyle.Fill,
|
|||
|
// SizeMode = PictureBoxSizeMode.Normal, // You can change this based on your needs
|
|||
|
// BackColor = Color.White,
|
|||
|
// };
|
|||
|
|
|||
|
// private double _zoomLevel = 10;
|
|||
|
// public double ZoomLevel
|
|||
|
// {
|
|||
|
// get { return _zoomLevel; }
|
|||
|
// set { _zoomLevel = value; }
|
|||
|
// }
|
|||
|
|
|||
|
// private Point _cameraPosition;
|
|||
|
// public Point CameraPosition
|
|||
|
// {
|
|||
|
// get { return _cameraPosition; }
|
|||
|
// set { _cameraPosition = value; }
|
|||
|
// }
|
|||
|
|
|||
|
// private Pen _primaryGridPen = new Pen(Color.LightGray, 2);
|
|||
|
// private Pen _secondaryGridPen = new Pen(Color.DarkGray, 1);
|
|||
|
|
|||
|
// Bitmap bitmap;
|
|||
|
// private void DrawGrid()
|
|||
|
// {
|
|||
|
|
|||
|
// if (bitmap is not null)
|
|||
|
// {
|
|||
|
// bitmap.Dispose();
|
|||
|
// }
|
|||
|
|
|||
|
// bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
|
|||
|
|
|||
|
// using (Graphics g = Graphics.FromImage(bitmap))
|
|||
|
// {
|
|||
|
// Rectangle currentRect;
|
|||
|
|
|||
|
// currentRect = new Rectangle(new Point(0, 0), new Size(pictureBox.Width, pictureBox.Height));
|
|||
|
|
|||
|
// g.FillRectangle(Brushes.Black, currentRect);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
// int PrimarySpacing = 200;
|
|||
|
// int SecondarySpacing = 50;
|
|||
|
|
|||
|
// DrawVerticalGrid(g, SecondarySpacing, _secondaryGridPen);
|
|||
|
// DrawVerticalGrid(g, PrimarySpacing, _primaryGridPen);
|
|||
|
|
|||
|
// DrawHorizontalGrid(g, SecondarySpacing, _secondaryGridPen);
|
|||
|
// DrawHorizontalGrid(g, PrimarySpacing, _primaryGridPen);
|
|||
|
|
|||
|
// pictureBox.Image = bitmap;
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
// private void DrawVerticalGrid(Graphics g, int spacing, Pen pen)
|
|||
|
// {
|
|||
|
// Point screenCentre = new(bitmap.Width / 2, bitmap.Height / 2);
|
|||
|
|
|||
|
// double spacingForZoom = (double)spacing / _zoomLevel;
|
|||
|
|
|||
|
// int leftMostLine = (int)Math.Floor(screenCentre.X - Math.Floor((_cameraPosition.X + screenCentre.X) / spacingForZoom) * spacingForZoom);
|
|||
|
|
|||
|
|
|||
|
// double currentPosition = leftMostLine;
|
|||
|
// while (currentPosition < bitmap.Width)
|
|||
|
// {
|
|||
|
// g.DrawLine(pen, new Point((int)currentPosition, 0), new Point((int)currentPosition, bitmap.Height));
|
|||
|
// currentPosition += spacingForZoom;
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
// private void DrawHorizontalGrid(Graphics g, int spacing, Pen pen)
|
|||
|
// {
|
|||
|
// Point screenCentre = new(bitmap.Width / 2, bitmap.Height / 2);
|
|||
|
|
|||
|
// double spacingForZoom = (double)spacing / _zoomLevel;
|
|||
|
|
|||
|
// int topMostLine = (int)Math.Floor(screenCentre.Y - Math.Floor((_cameraPosition.Y + screenCentre.Y) / spacingForZoom) * spacingForZoom);
|
|||
|
|
|||
|
// double currentPosition = topMostLine;
|
|||
|
// while (currentPosition < bitmap.Height)
|
|||
|
// {
|
|||
|
// g.DrawLine(pen, new Point(0, (int)currentPosition), new Point(bitmap.Width, (int)currentPosition));
|
|||
|
// currentPosition += spacingForZoom;
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
// public WorkspaceViewport()
|
|||
|
// {
|
|||
|
// InitializeComponent();
|
|||
|
|
|||
|
// this.pictureBox.MouseWheel += new MouseEventHandler(this.WorkspaceViewport_MouseWheel);
|
|||
|
// this.pictureBox.MouseDown += new MouseEventHandler(this.WorkspaceViewport_MouseDown);
|
|||
|
// this.pictureBox.MouseUp += new MouseEventHandler(this.WorkspaceViewport_MouseUp);
|
|||
|
// this.pictureBox.MouseMove += new MouseEventHandler(this.WorkspaceViewport_MouseMove);
|
|||
|
// this.Controls.Add(pictureBox);
|
|||
|
|
|||
|
// DrawGrid();
|
|||
|
// }
|
|||
|
|
|||
|
// private void WorkspaceViewport_Resize(object sender, EventArgs e)
|
|||
|
// {
|
|||
|
// this.Invalidate();
|
|||
|
// DrawGrid();
|
|||
|
// }
|
|||
|
|
|||
|
// private bool _isMouseWhithin = false;
|
|||
|
|
|||
|
// private void WorkspaceViewport_MouseWheel(object sender, MouseEventArgs e)
|
|||
|
// {
|
|||
|
// double test = _zoomLevel + (1 * -Math.Sign(e.Delta));
|
|||
|
// if (test > 0 && test < 11)
|
|||
|
// {
|
|||
|
// _zoomLevel = test;
|
|||
|
// }
|
|||
|
|
|||
|
// DrawGrid();
|
|||
|
// this.Invalidate();
|
|||
|
|
|||
|
// }
|
|||
|
|
|||
|
// private Point _positionMiddleMouseDown;
|
|||
|
// private bool _middleMousePressed= false;
|
|||
|
// private void WorkspaceViewport_MouseDown(object sender, MouseEventArgs e)
|
|||
|
// {
|
|||
|
// if (e.Button == MouseButtons.Middle)
|
|||
|
// {
|
|||
|
// _middleMousePressed = true;
|
|||
|
// _positionMiddleMouseDown = new Point((int)(e.Location.X * _zoomLevel), (int)(e.Location.Y * _zoomLevel));
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
// private void WorkspaceViewport_MouseUp(object sender, MouseEventArgs e)
|
|||
|
// {
|
|||
|
// if (e.Button == MouseButtons.Middle)
|
|||
|
// {
|
|||
|
// _middleMousePressed = false;
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
// private void WorkspaceViewport_MouseMove(object sender, MouseEventArgs e)
|
|||
|
// {
|
|||
|
// if (_middleMousePressed)
|
|||
|
// {
|
|||
|
// Point point = e.Location;
|
|||
|
// double distanceX = (point.X * _zoomLevel) - _positionMiddleMouseDown.X;
|
|||
|
// double distanceY = (point.Y * _zoomLevel) - _positionMiddleMouseDown.Y;
|
|||
|
|
|||
|
// _cameraPosition.X += (int)distanceX;
|
|||
|
// _cameraPosition.Y += (int)distanceY;
|
|||
|
// }
|
|||
|
// }
|
|||
|
// }
|
|||
|
//}
|
|||
|
|
|||
|
using Microsoft.VisualBasic;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Data;
|
|||
|
using System.Drawing;
|
|||
|
using System.Linq;
|
|||
|
using System.Runtime.InteropServices;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Windows.Forms;
|
|||
|
|
|||
|
namespace LensSimulator_WinForms.Viewport
|
|||
|
{
|
|||
|
public partial class WorkspaceViewport : UserControl
|
|||
|
{
|
|||
|
PictureBox pictureBox = new PictureBox
|
|||
|
{
|
|||
|
Dock = DockStyle.Fill,
|
|||
|
SizeMode = PictureBoxSizeMode.Normal,
|
|||
|
BackColor = Color.White,
|
|||
|
};
|
|||
|
|
|||
|
private float _zoomLevel = 1f;
|
|||
|
public float ZoomLevel
|
|||
|
{
|
|||
|
get { return _zoomLevel; }
|
|||
|
set { _zoomLevel = value; }
|
|||
|
}
|
|||
|
|
|||
|
private PointF _cameraPosition = new PointF(0, 0);
|
|||
|
public PointF CameraPosition
|
|||
|
{
|
|||
|
get { return _cameraPosition; }
|
|||
|
set { _cameraPosition = value; }
|
|||
|
}
|
|||
|
|
|||
|
private Pen _primaryGridPen = new Pen(Color.LightGray, 2);
|
|||
|
private Pen _secondaryGridPen = new Pen(Color.DarkGray, 1);
|
|||
|
|
|||
|
Bitmap bitmap;
|
|||
|
|
|||
|
private void DrawGrid()
|
|||
|
{
|
|||
|
if (bitmap is not null)
|
|||
|
{
|
|||
|
bitmap.Dispose();
|
|||
|
}
|
|||
|
|
|||
|
bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
|
|||
|
|
|||
|
using (Graphics g = Graphics.FromImage(bitmap))
|
|||
|
{
|
|||
|
g.FillRectangle(Brushes.Black, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
|
|||
|
|
|||
|
// Determine the actual spacing based on zoom level
|
|||
|
float baseSpacing = 50;
|
|||
|
float spacing = baseSpacing;
|
|||
|
|
|||
|
while (spacing / _zoomLevel < baseSpacing)
|
|||
|
{
|
|||
|
spacing *= 10;
|
|||
|
}
|
|||
|
|
|||
|
while (spacing / _zoomLevel > 10 * baseSpacing)
|
|||
|
{
|
|||
|
spacing /= 10;
|
|||
|
}
|
|||
|
|
|||
|
DrawGrid(g, spacing, _secondaryGridPen);
|
|||
|
if (spacing / _zoomLevel <= 2 * baseSpacing)
|
|||
|
{
|
|||
|
DrawGrid(g, spacing * 10, _primaryGridPen);
|
|||
|
}
|
|||
|
|
|||
|
pictureBox.Image = bitmap;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void DrawGrid(Graphics g, float spacing, Pen pen)
|
|||
|
{
|
|||
|
float leftMostLine = _cameraPosition.X - (_cameraPosition.X % spacing);
|
|||
|
float topMostLine = _cameraPosition.Y - (_cameraPosition.Y % spacing);
|
|||
|
|
|||
|
for (float x = leftMostLine; x < bitmap.Width + _cameraPosition.X; x += spacing)
|
|||
|
{
|
|||
|
g.DrawLine(pen, x - _cameraPosition.X, 0, x - _cameraPosition.X, bitmap.Height);
|
|||
|
}
|
|||
|
|
|||
|
for (float y = topMostLine; y < bitmap.Height + _cameraPosition.Y; y += spacing)
|
|||
|
{
|
|||
|
g.DrawLine(pen, 0, y - _cameraPosition.Y, bitmap.Width, y - _cameraPosition.Y);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public WorkspaceViewport()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
|
|||
|
this.pictureBox.MouseWheel += new MouseEventHandler(this.WorkspaceViewport_MouseWheel);
|
|||
|
this.pictureBox.MouseDown += new MouseEventHandler(this.WorkspaceViewport_MouseDown);
|
|||
|
this.pictureBox.MouseUp += new MouseEventHandler(this.WorkspaceViewport_MouseUp);
|
|||
|
this.pictureBox.MouseMove += new MouseEventHandler(this.WorkspaceViewport_MouseMove);
|
|||
|
this.Controls.Add(pictureBox);
|
|||
|
|
|||
|
DrawGrid();
|
|||
|
}
|
|||
|
|
|||
|
private void WorkspaceViewport_Resize(object sender, EventArgs e)
|
|||
|
{
|
|||
|
DrawGrid();
|
|||
|
}
|
|||
|
|
|||
|
private PointF _mouseDownLocation;
|
|||
|
|
|||
|
private void WorkspaceViewport_MouseWheel(object sender, MouseEventArgs e)
|
|||
|
{
|
|||
|
_zoomLevel *= (e.Delta > 0) ? 1.1f : 0.9f;
|
|||
|
DrawGrid();
|
|||
|
}
|
|||
|
|
|||
|
private void WorkspaceViewport_MouseDown(object sender, MouseEventArgs e)
|
|||
|
{
|
|||
|
if (e.Button == MouseButtons.Middle)
|
|||
|
{
|
|||
|
_mouseDownLocation = e.Location;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void WorkspaceViewport_MouseUp(object sender, MouseEventArgs e)
|
|||
|
{
|
|||
|
if (e.Button == MouseButtons.Middle)
|
|||
|
{
|
|||
|
_cameraPosition.X += (_mouseDownLocation.X - e.Location.X) / _zoomLevel;
|
|||
|
_cameraPosition.Y += (_mouseDownLocation.Y - e.Location.Y) / _zoomLevel;
|
|||
|
_mouseDownLocation = e.Location;
|
|||
|
DrawGrid();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void WorkspaceViewport_MouseMove(object sender, MouseEventArgs e)
|
|||
|
{
|
|||
|
if (MouseButtons == MouseButtons.Middle)
|
|||
|
{
|
|||
|
_cameraPosition.X += (_mouseDownLocation.X - e.Location.X) / _zoomLevel;
|
|||
|
_cameraPosition.Y += (_mouseDownLocation.Y - e.Location.Y) / _zoomLevel;
|
|||
|
_mouseDownLocation = e.Location;
|
|||
|
DrawGrid();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|