Запутался с циклами
Код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public List<Vertex> _VertexList = new List<Vertex>();
public List<Edge> _EdgeList = new List<Edge>();
public List<Frame> _FrameList = new List<Frame>();
Frame _FrameFirst, _FrameLast, _Frame;
int _KeyFrameCount, _FrameCount;
List<int> _KeyFrameAdreses = new List<int>();
public bool _MousePressed;
int _X, _Y, _lastX, _lastY;
public Form1()
{
InitializeComponent();
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
foreach (Vertex vrtx in _VertexList)
{
}
_MousePressed = true;
}
private void BTN_GNRVRTX_Click(object sender, EventArgs e)
{
Vertex _new = new Vertex(0, 0, false, String.Concat("Vertex"+_VertexList.Count));
_VertexList.Add(_new);
string tmpString=null;
comboBox1.Items.Add(_new.GetVertexName(tmpString));
CB_E_VA.Items.Add(_new.GetVertexName(tmpString));
CB_E_VB.Items.Add(_new.GetVertexName(tmpString));
Graphics _graphics = panel.CreateGraphics();
Redraw();
}
void Redraw()
{
Graphics _graphics = panel.CreateGraphics();
_graphics.Clear(Color.White);
foreach (Vertex vrtx in _VertexList)
{
int x=0,y=0;
DrawVertex(_graphics, Color.Green, vrtx.GetVertexPositionX(x) + panel.Width/2, -vrtx.GetVertexPositionY(y) + panel.Height/2);
}
foreach (Edge edge in _EdgeList)
{
Vertex tmpVertexA=null, tmpVertexB=null;
DrawEdge(_graphics, Color.Gray, edge.GetVA(tmpVertexA), edge.GetVB(tmpVertexB));
}
}
void DrawVertex(Graphics g, Color c, int x, int y)
{
g.FillRectangle(new SolidBrush(c), x, y, 3, 3);
}
void DrawEdge(Graphics g, Color c, Vertex va, Vertex vb)
{
int ax = 0, ay = 0, bx = 0, by = 0;
g.DrawLine(new Pen(Color.Gray), va.GetVertexPositionX(ax) + panel.Width / 2, -va.GetVertexPositionY(ay) + panel.Height / 2, vb.GetVertexPositionX(bx) + panel.Width / 2, -vb.GetVertexPositionY(by) + panel.Height / 2);
}
private void panel_MouseMove(object sender, MouseEventArgs e)
{
if (_MousePressed)
{
_X = _lastX;
_Y = _lastY;
if (!((e.X > panel.Width) || (e.X < 0)))
_X = e.X - panel.Width / 2;
if (!((e.Y > panel.Width) || (e.Y < 0)))
_Y = -e.Y + panel.Width / 2;
foreach (Vertex vrtx in _VertexList)
{
bool act=false;
if (vrtx.GetVertexActivity(act))
vrtx.SetVertexPosition(_X, _Y);
}
Graphics _graphics = panel.CreateGraphics();
Redraw();
toolStripStatusLabel1.Text = String.Concat("X: " + _X + " Y: " + _Y);
_lastX = _X;
_lastY = _Y;
}
}
private void panel_MouseUp(object sender, MouseEventArgs e)
{
_MousePressed = false;
}
private void создатьToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void открытьToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void выходToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void инфоToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Vertex vrtx in _VertexList)
{
int x=0, y=0;
string str=null;
MessageBox.Show(String.Concat("Vertex: " + vrtx.GetVertexName(str) + " X: " + vrtx.GetVertexPositionX(x) + " Y: " + vrtx.GetVertexPositionY(y)));
}
}
private void BTN_ACTIVE_Click(object sender, EventArgs e)
{
foreach (Vertex vrtx in _VertexList)
vrtx.SetVertexActivity(false);
_VertexList[comboBox1.SelectedIndex].SetVertexActivity(true);
Graphics g = panel.CreateGraphics();
int x = 0, y = 0;
DrawVertex(g, Color.Crimson, _VertexList[comboBox1.SelectedIndex].GetVertexPositionX(x) + panel.Width / 2, -_VertexList[comboBox1.SelectedIndex].GetVertexPositionY(y) + panel.Height / 2);
}
private void BTN_DEACTIVATE_Click(object sender, EventArgs
09.07.14
e)
{
foreach (Vertex vrtx in _VertexList)
vrtx.SetVertexActivity(false);
Redraw();
}
private void BTN_GNREDGE_Click(object sender, EventArgs e)
{
if (CB_E_VA.SelectedIndex != CB_E_VB.SelectedIndex)
{
Edge _new = new Edge(_VertexList[CB_E_VA.SelectedIndex], _VertexList[CB_E_VB.SelectedIndex], false, String.Concat("Edge" + CB_E_VA.SelectedIndex + CB_E_VB.SelectedIndex));
_EdgeList.Add(_new);
string tmpString = null;
CB_EDGE.Items.Add(_new.GetEdgeName(tmpString));
Redraw();
}
else
{
MessageBox.Show("Неверный выбор вершин.");
}
}
private void BTN_DELVERTEX_Click(object sender, EventArgs e)
{
for(int i=0; i<_VertexList.Count;i++)
if (_VertexList[i].GetVertexActivity(true))
_VertexList.Remove(_VertexList[i]);
Redraw();
}
private void BTN_DELEDGE_Click(object sender, EventArgs e)
{
for (int i = 0; i < _EdgeList.Count; i++)
if (_EdgeList[i].GetEdgeActivity(true))
_EdgeList.Remove(_EdgeList[i]);
Redraw();
}
private void BTN_ACTIVATEEDGE_Click(object sender, EventArgs e)
{
foreach (Edge edge in _EdgeList)
edge.SetEdgeActivity(false);
_EdgeList[CB_EDGE.SelectedIndex].SetEdgeActivity(true);
}
private void BTN_CREATENEXTKF_Click(object sender, EventArgs e)
{
string tmpString=null;
if (_FrameList.Count == 0)
{
_Frame = new Frame(String.Concat("KeyFrame" + _KeyFrameCount), _FrameList.Count + 1, true, _VertexList, _EdgeList);
_FrameList.Add(_Frame);
CB_KeyFrames.Items.Add(_Frame.GetKeyFrameName(tmpString));
_KeyFrameAdreses.Add(_FrameList.Count - 1);
}
for (int i = 0; i < Convert.ToInt32(TB_NumberOfFrames.Text); i++)
_FrameList.Add(null);
_Frame = new Frame(String.Concat("KeyFrame" + ++_KeyFrameCount), _FrameList.Count + 1, true, _VertexList, _EdgeList);
_FrameList.Add(_Frame);
CB_KeyFrames.Items.Add(_Frame.GetKeyFrameName(tmpString));
_KeyFrameAdreses.Add(_FrameList.Count - 1);
;
}
private void BTN_OpenKeyFrame_Click(object sender, EventArgs e)
{
int tmp = _KeyFrameAdreses[CB_KeyFrames.SelectedIndex];
CB_KeyFrames.Enabled = false;
//bool btmp = _FrameList[tmp].GetFrameKeyMark(btmp);
//if (_FrameList[tmp].GetFrameKeyMark(false))
// MessageBox.Show("Произошла ошибка формирования ключевых кадров");
//else
//{
Graphics _graphics = panel.CreateGraphics();
_graphics.Clear(Color.White);
_VertexList = null;
_EdgeList = null;
_Frame = _FrameList[tmp];
List<Vertex> lv=null;
_VertexList = _Frame.GetFrameVertexes(lv);
List<Edge> ed = null;
_EdgeList = _Frame.GetFrameEdges(ed);
MessageBox.Show("POW!");
Redraw();
//}
}
private void BTN_SaveKeyFrame_Click(object sender, EventArgs e)
{
int tmp = _KeyFrameAdreses[CB_KeyFrames.SelectedIndex];
//_FrameList[tmp].SetFrameVertexes(_VertexList);
//_FrameList[tmp].SetFrameEdges(_EdgeList);
CB_KeyFrames.Enabled = true;
}
private void BTN_DelKeyFrame_Click(object sender, EventArgs e)
{
}
}
public class Vertex
{
int _posX, _posY;
bool _active;
string _name;
public Vertex(int _pX, int _pY, bool _a, string _n)
{
_posX = _pX;
_posY = _pY;
_active = _a;
_name = _n;
}
public void SetVertexPosition(int _pX, int _pY)
{
_posX = _pX;
_posY = _pY;
}
public string GetVertexName(string _n)
{
return _name;
}
public int GetVertexPositionX(int _pX)
{
return _posX;
}
public int GetVertexPositionY(int _pY)
{
return _posY;
}
public void SetVertexActivity(bool _a)
{
_active = _a;
}
public bool GetVertexActivity(bool _a)
{
return _active;
}
}
public class Edge
{
Vertex _VA, _VB;
int _posAX, _posAY, _posBX, _posBY;
bool _active;
string _name;
public Edge(Vertex _A, Vertex _B, bool _a, string _n)
{
int x = 0, y = 0;
_VA = _A;
_VB = _B;
_posAX = _A.GetVertexPositionX(x);
_posAY = _A.GetVertexPositionY(y);
_posBX = _B.GetVertexPositionX(x);
_posBY = _B.GetVertexPositionY(y);
_active = _a;
_name = _n;
}
public string GetEdgeName(string _n)
{
return
_name;
}
public Vertex GetVA(Vertex va)
{
return _VA;
}
public Vertex GetVB(Vertex vb)
{
return _VB;
}
public int GetAX(int ax)
{
return _posAX;
}
public int GetAY(int ay)
{
return _posAY;
}
public int GetBX(int bx)
{
return _posBX;
}
public int GetBY(int by)
{
return _posBY;
}
public bool GetEdgeActivity(bool a)
{
return _active;
}
public void SetEdgeActivity(bool a)
{
_active = a;
}
}
public class Frame
{
bool _keyFrame;
List<Vertex> _FrameVertexes;
List<Edge> _FrameEdges;
string _FrameName;
int _FrameNumber;
public Frame(string name, int number, bool key, List<Vertex> vertexes, List<Edge> edges)
{
_FrameName = name;
_FrameNumber = number;
_keyFrame = key;
_FrameVertexes = vertexes;
_FrameEdges = edges;
}
public string GetKeyFrameName(string n)
{
return _FrameName;
}
public bool GetFrameKeyMark(bool b)
{
return _keyFrame;
}
//public bool GetFrameRole(bool kf)
//{
// return _keyFrame;
//}
public List<Vertex> GetFrameVertexes(List<Vertex> v)
{
return _FrameVertexes;
}
public List<Edge> GetFrameEdges(List<Edge> e)
{
return _FrameEdges;
}
public void SetFrameVertexes(List<Vertex> v)
{
_FrameVertexes = v;
}
public void SetFrameEdges(List<Edge> e)
{
_FrameEdges = e;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public List<Vertex> _VertexList = new List<Vertex>();
public List<Edge> _EdgeList = new List<Edge>();
public List<Frame> _FrameList = new List<Frame>();
Frame _FrameFirst, _FrameLast, _Frame;
int _KeyFrameCount, _FrameCount;
List<int> _KeyFrameAdreses = new List<int>();
public bool _MousePressed;
int _X, _Y, _lastX, _lastY;
public Form1()
{
InitializeComponent();
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
foreach (Vertex vrtx in _VertexList)
{
}
_MousePressed = true;
}
private void BTN_GNRVRTX_Click(object sender, EventArgs e)
{
Vertex _new = new Vertex(0, 0, false, String.Concat("Vertex"+_VertexList.Count));
_VertexList.Add(_new);
string tmpString=null;
comboBox1.Items.Add(_new.GetVertexName(tmpString));
CB_E_VA.Items.Add(_new.GetVertexName(tmpString));
CB_E_VB.Items.Add(_new.GetVertexName(tmpString));
Graphics _graphics = panel.CreateGraphics();
Redraw();
}
void Redraw()
{
Graphics _graphics = panel.CreateGraphics();
_graphics.Clear(Color.White);
foreach (Vertex vrtx in _VertexList)
{
int x=0,y=0;
DrawVertex(_graphics, Color.Green, vrtx.GetVertexPositionX(x) + panel.Width/2, -vrtx.GetVertexPositionY(y) + panel.Height/2);
}
foreach (Edge edge in _EdgeList)
{
Vertex tmpVertexA=null, tmpVertexB=null;
DrawEdge(_graphics, Color.Gray, edge.GetVA(tmpVertexA), edge.GetVB(tmpVertexB));
}
}
void DrawVertex(Graphics g, Color c, int x, int y)
{
g.FillRectangle(new SolidBrush(c), x, y, 3, 3);
}
void DrawEdge(Graphics g, Color c, Vertex va, Vertex vb)
{
int ax = 0, ay = 0, bx = 0, by = 0;
g.DrawLine(new Pen(Color.Gray), va.GetVertexPositionX(ax) + panel.Width / 2, -va.GetVertexPositionY(ay) + panel.Height / 2, vb.GetVertexPositionX(bx) + panel.Width / 2, -vb.GetVertexPositionY(by) + panel.Height / 2);
}
private void panel_MouseMove(object sender, MouseEventArgs e)
{
if (_MousePressed)
{
_X = _lastX;
_Y = _lastY;
if (!((e.X > panel.Width) || (e.X < 0)))
_X = e.X - panel.Width / 2;
if (!((e.Y > panel.Width) || (e.Y < 0)))
_Y = -e.Y + panel.Width / 2;
foreach (Vertex vrtx in _VertexList)
{
bool act=false;
if (vrtx.GetVertexActivity(act))
vrtx.SetVertexPosition(_X, _Y);
}
Graphics _graphics = panel.CreateGraphics();
Redraw();
toolStripStatusLabel1.Text = String.Concat("X: " + _X + " Y: " + _Y);
_lastX = _X;
_lastY = _Y;
}
}
private void panel_MouseUp(object sender, MouseEventArgs e)
{
_MousePressed = false;
}
private void создатьToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void открытьToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void выходToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void инфоToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Vertex vrtx in _VertexList)
{
int x=0, y=0;
string str=null;
MessageBox.Show(String.Concat("Vertex: " + vrtx.GetVertexName(str) + " X: " + vrtx.GetVertexPositionX(x) + " Y: " + vrtx.GetVertexPositionY(y)));
}
}
private void BTN_ACTIVE_Click(object sender, EventArgs e)
{
foreach (Vertex vrtx in _VertexList)
vrtx.SetVertexActivity(false);
_VertexList[comboBox1.SelectedIndex].SetVertexActivity(true);
Graphics g = panel.CreateGraphics();
int x = 0, y = 0;
DrawVertex(g, Color.Crimson, _VertexList[comboBox1.SelectedIndex].GetVertexPositionX(x) + panel.Width / 2, -_VertexList[comboBox1.SelectedIndex].GetVertexPositionY(y) + panel.Height / 2);
}
private void BTN_DEACTIVATE_Click(object sender, EventArgs
09.07.14
e)
{
foreach (Vertex vrtx in _VertexList)
vrtx.SetVertexActivity(false);
Redraw();
}
private void BTN_GNREDGE_Click(object sender, EventArgs e)
{
if (CB_E_VA.SelectedIndex != CB_E_VB.SelectedIndex)
{
Edge _new = new Edge(_VertexList[CB_E_VA.SelectedIndex], _VertexList[CB_E_VB.SelectedIndex], false, String.Concat("Edge" + CB_E_VA.SelectedIndex + CB_E_VB.SelectedIndex));
_EdgeList.Add(_new);
string tmpString = null;
CB_EDGE.Items.Add(_new.GetEdgeName(tmpString));
Redraw();
}
else
{
MessageBox.Show("Неверный выбор вершин.");
}
}
private void BTN_DELVERTEX_Click(object sender, EventArgs e)
{
for(int i=0; i<_VertexList.Count;i++)
if (_VertexList[i].GetVertexActivity(true))
_VertexList.Remove(_VertexList[i]);
Redraw();
}
private void BTN_DELEDGE_Click(object sender, EventArgs e)
{
for (int i = 0; i < _EdgeList.Count; i++)
if (_EdgeList[i].GetEdgeActivity(true))
_EdgeList.Remove(_EdgeList[i]);
Redraw();
}
private void BTN_ACTIVATEEDGE_Click(object sender, EventArgs e)
{
foreach (Edge edge in _EdgeList)
edge.SetEdgeActivity(false);
_EdgeList[CB_EDGE.SelectedIndex].SetEdgeActivity(true);
}
private void BTN_CREATENEXTKF_Click(object sender, EventArgs e)
{
string tmpString=null;
if (_FrameList.Count == 0)
{
_Frame = new Frame(String.Concat("KeyFrame" + _KeyFrameCount), _FrameList.Count + 1, true, _VertexList, _EdgeList);
_FrameList.Add(_Frame);
CB_KeyFrames.Items.Add(_Frame.GetKeyFrameName(tmpString));
_KeyFrameAdreses.Add(_FrameList.Count - 1);
}
for (int i = 0; i < Convert.ToInt32(TB_NumberOfFrames.Text); i++)
_FrameList.Add(null);
_Frame = new Frame(String.Concat("KeyFrame" + ++_KeyFrameCount), _FrameList.Count + 1, true, _VertexList, _EdgeList);
_FrameList.Add(_Frame);
CB_KeyFrames.Items.Add(_Frame.GetKeyFrameName(tmpString));
_KeyFrameAdreses.Add(_FrameList.Count - 1);
;
}
private void BTN_OpenKeyFrame_Click(object sender, EventArgs e)
{
int tmp = _KeyFrameAdreses[CB_KeyFrames.SelectedIndex];
CB_KeyFrames.Enabled = false;
//bool btmp = _FrameList[tmp].GetFrameKeyMark(btmp);
//if (_FrameList[tmp].GetFrameKeyMark(false))
// MessageBox.Show("Произошла ошибка формирования ключевых кадров");
//else
//{
Graphics _graphics = panel.CreateGraphics();
_graphics.Clear(Color.White);
_VertexList = null;
_EdgeList = null;
_Frame = _FrameList[tmp];
List<Vertex> lv=null;
_VertexList = _Frame.GetFrameVertexes(lv);
List<Edge> ed = null;
_EdgeList = _Frame.GetFrameEdges(ed);
MessageBox.Show("POW!");
Redraw();
//}
}
private void BTN_SaveKeyFrame_Click(object sender, EventArgs e)
{
int tmp = _KeyFrameAdreses[CB_KeyFrames.SelectedIndex];
//_FrameList[tmp].SetFrameVertexes(_VertexList);
//_FrameList[tmp].SetFrameEdges(_EdgeList);
CB_KeyFrames.Enabled = true;
}
private void BTN_DelKeyFrame_Click(object sender, EventArgs e)
{
}
}
public class Vertex
{
int _posX, _posY;
bool _active;
string _name;
public Vertex(int _pX, int _pY, bool _a, string _n)
{
_posX = _pX;
_posY = _pY;
_active = _a;
_name = _n;
}
public void SetVertexPosition(int _pX, int _pY)
{
_posX = _pX;
_posY = _pY;
}
public string GetVertexName(string _n)
{
return _name;
}
public int GetVertexPositionX(int _pX)
{
return _posX;
}
public int GetVertexPositionY(int _pY)
{
return _posY;
}
public void SetVertexActivity(bool _a)
{
_active = _a;
}
public bool GetVertexActivity(bool _a)
{
return _active;
}
}
public class Edge
{
Vertex _VA, _VB;
int _posAX, _posAY, _posBX, _posBY;
bool _active;
string _name;
public Edge(Vertex _A, Vertex _B, bool _a, string _n)
{
int x = 0, y = 0;
_VA = _A;
_VB = _B;
_posAX = _A.GetVertexPositionX(x);
_posAY = _A.GetVertexPositionY(y);
_posBX = _B.GetVertexPositionX(x);
_posBY = _B.GetVertexPositionY(y);
_active = _a;
_name = _n;
}
public string GetEdgeName(string _n)
{
return
_name;
}
public Vertex GetVA(Vertex va)
{
return _VA;
}
public Vertex GetVB(Vertex vb)
{
return _VB;
}
public int GetAX(int ax)
{
return _posAX;
}
public int GetAY(int ay)
{
return _posAY;
}
public int GetBX(int bx)
{
return _posBX;
}
public int GetBY(int by)
{
return _posBY;
}
public bool GetEdgeActivity(bool a)
{
return _active;
}
public void SetEdgeActivity(bool a)
{
_active = a;
}
}
public class Frame
{
bool _keyFrame;
List<Vertex> _FrameVertexes;
List<Edge> _FrameEdges;
string _FrameName;
int _FrameNumber;
public Frame(string name, int number, bool key, List<Vertex> vertexes, List<Edge> edges)
{
_FrameName = name;
_FrameNumber = number;
_keyFrame = key;
_FrameVertexes = vertexes;
_FrameEdges = edges;
}
public string GetKeyFrameName(string n)
{
return _FrameName;
}
public bool GetFrameKeyMark(bool b)
{
return _keyFrame;
}
//public bool GetFrameRole(bool kf)
//{
// return _keyFrame;
//}
public List<Vertex> GetFrameVertexes(List<Vertex> v)
{
return _FrameVertexes;
}
public List<Edge> GetFrameEdges(List<Edge> e)
{
return _FrameEdges;
}
public void SetFrameVertexes(List<Vertex> v)
{
_FrameVertexes = v;
}
public void SetFrameEdges(List<Edge> e)
{
_FrameEdges = e;
}
}
}
В этом коде _VertexList хранит координаты вершин, а _FrameList - кадры. при выборе кадра и вершины в кадре и изменении её координат у меня координаты меняются по всем кадрам, а должно быть вроде только в одном. Подскажите где косяк.