using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Windows .Forms;
using Microsoft .Win32;
using MoveGraphLibrary;
namespace GraphicalPrimitives
{
public partial class Form_Spots : Form
{
int m_version = 620;
const string strAddRegKey = "Form_Spots";
Mover mover;
Point ptMouse_Down, ptMouse_Up;
List<SpotCC> spots = new List<SpotCC> ();
Random rand;
int nSeed;
TextM info = null;
string infotext = "All spots are movable.\n" +
"At the initial moment of the movement (left button press)\n" +
"the cursor is moved to the central point of the pressed spot.";
bool bRestore = false;
// --------------------------------------
public Form_Spots ()
{
InitializeComponent ();
mover = new Mover (this);
rand = Auxi_Common .RandomByCurTime (out nSeed);
}
// ------------------------------------------------- OnLoad
private void OnLoad (object sender, EventArgs e)
{
RestoreFromRegistry ();
if (!bRestore)
{
int minR = SpotCC .MinRadius;
int maxR = SpotCC .MaxRadius;
for (int i = 0; i < maxR - minR + 1; i++)
{
SpotCC spot = new SpotCC (this, mover, RandomPoint, minR + i, Auxi_Colours .ColorPredefined (i));
spots .Add (spot);
}
info = new TextM (this, new Point (300, 60), infotext);
info .BackColor = Color .LightYellow;
}
mover .Clear ();
foreach (SpotCC spot in spots)
{
mover .Insert (0, spot);
}
mover .Insert (0, info);
}
// ------------------------------------------------- OnFormClosing
private void OnFormClosing (object sender, FormClosingEventArgs e)
{
SaveIntoRegistry ();
}
// ------------------------------------------------- RandomPoint
private PointF RandomPoint
{
get
{
int sidespace = (Math .Min (ClientSize .Width, ClientSize .Height) >= 200) ? 40 : 0;
return (new PointF (rand .Next (nSeed) % (ClientSize .Width - 2 * sidespace) + sidespace,
rand .Next (nSeed) % (ClientSize .Height - 2 * sidespace) + sidespace));
}
}
// ------------------------------------------------- OnPaint
private void OnPaint (object sender, PaintEventArgs e)
{
Graphics grfx = e .Graphics;
foreach (SpotCC spot in spots)
{
spot .Draw (grfx);
}
info .Draw (grfx);
}
// ------------------------------------------------- OnMouseDown
private void OnMouseDown (object sender, MouseEventArgs e)
{
ptMouse_Down = e .Location;
if (mover .Catch (e .Location, e .Button))
{
GraphicalObject grobj = mover .CaughtSource;
if (e .Button == MouseButtons .Left)
{
if (grobj is SpotCC)
{
(grobj as SpotCC) .StartMoving ();
}
}
}
}
// ------------------------------------------------- OnMouseUp
private void OnMouseUp (object sender, MouseEventArgs e)
{
ptMouse_Up = e .Location;
if (mover .Release ())
{
GraphicalObject grobj = mover .WasCaughtSource;
if (grobj is SpotCC && e .Button == MouseButtons .Right && Auxi_Geometry .Distance (ptMouse_Down, ptMouse_Up) <= 3)
{
TuningSpot (grobj as SpotCC);
}
}
}
// ------------------------------------------------- OnMouseMove
private void OnMouseMove (object sender, MouseEventArgs e)
{
if (mover .Move (e .Location))
{
Invalidate ();
}
}
// ------------------------------------------------- OnMouseDoubleClick
private void OnMouseDoubleClick (object sender, MouseEventArgs e)
{
if (mover .Release () && e .Button == MouseButtons .Left && mover .WasCaughtSource is SpotCC)
{
TuningSpot (mover .WasCaughtSource as SpotCC);
}
}
// ------------------------------------------------- TuningSpot
private void TuningSpot (SpotCC spot)
{
Form_Tuning_Spot form = new Form_Tuning_Spot (PointToScreen (Point .Round (spot .Center)), "Modify spot", spot .Radius, spot .Color);
if (DialogResult .OK == form .ShowDialog ())
{
spot .Color = form .Color;
spot .Radius = form .Radius;
Invalidate ();
}
}
const string nameMain = "Main";
// ------------------------------------------------- SaveIntoRegistry
private void SaveIntoRegistry ()
{
string strRegKey = Form_Main .strRegKey + strAddRegKey;
RegistryKey regkey = null;
try
{
regkey = Registry .CurrentUser .CreateSubKey (strRegKey);
if (regkey != null)
{
string [] strs = {m_version .ToString (), // 0
ClientSize .Width .ToString (), // 1
ClientSize .Height .ToString (), // 2
spots .Count .ToString () }; // 3
regkey .SetValue (nameMain, strs, RegistryValueKind .MultiString);
for (int i = 0; i < spots .Count; i++)
{
spots [i] .IntoRegistry (regkey, "spot_" + i .ToString ());
}
info .IntoRegistry (regkey, "Info");
}
}
catch
{
}
finally
{
if (regkey != null) regkey .Close ();
}
}
// ------------------------------------------------- RestoreFromRegistry
private void RestoreFromRegistry ()
{
string namekey = Auxi_DLL_Internal .PathDefinedByExecutable ();
namekey += strAddRegKey;
RegistryKey regkey = null;
try
{
regkey = Registry .CurrentUser .OpenSubKey (namekey);
if (regkey != null)
{
string [] strs = (string []) regkey .GetValue (nameMain);
if (strs != null && strs .Length == 4 && Convert .ToInt32 (strs [0]) >= 620)
{
ClientSize = Auxi_Convert .ToSize (strs, 1);
int nSpots = Convert .ToInt32 (strs [3]);
for (int i = 0; i < nSpots; i++)
{
SpotCC spot = SpotCC .FromRegistry (this, mover, regkey, "spot_" + i .ToString ());
if (spot != null)
{
spots .Add (spot);
}
}
info = TextM .FromRegistry (this, regkey, "Info");
if (spots .Count == nSpots && info != null)
{
bRestore = true;
}
}
regkey .Close ();
}
}
catch
{
}
finally
{
if (regkey != null) regkey .Close ();
}
}
}
}