/// <summary> ///empty base class containing State methods to override /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class State { //keeps state of each button protected Mediator med; public State(Mediator md) { med = md; //save reference to mediator } public virtual void mouseDown(int x, int y) { } public virtual void mouseUp(int x, int y) { } public virtual void mouseDrag(int x, int y) { } public virtual void selectOne(Drawing d) { } }
/// <summary> /// Summary description for RectState. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class RectState : State { public RectState(Mediator md) : base(md) { } //----- public override void mouseDown(int x, int y) { VisRectangle vr = new VisRectangle(x, y); med.addDrawing(vr); } }
/// <summary> /// Summary description for StateManager. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class StateManager { private State currentState; private RectState rState; private ArrowState aState; private CircleState cState; private FillState fState; /// <summary> /// /// </summary> /// <param name="med"></param> public StateManager(Mediator med) { //create an instance of each state rState = new RectState(med); cState = new CircleState(med); aState = new ArrowState(med); fState = new FillState(med); //and initialize them //set default state currentState = aState; } /// <summary> /// These methods are called when the toolbuttons are clicked /// </summary> public void setRect() { currentState = rState; } /// <summary> /// /// </summary> public void setCircle() { currentState = cState; } /// <summary> /// /// </summary> public void setFill() { currentState = fState; } /// <summary> /// /// </summary> public void setArrow() { currentState = aState; } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void mouseDown(int x, int y) { currentState.mouseDown(x, y); } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void mouseUp(int x, int y) { currentState.mouseUp(x, y); } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void mouseDrag(int x, int y) { currentState.mouseDrag(x, y); } /// <summary> /// /// </summary> /// <param name="d"></param> public void selectOne(Drawing d) { currentState.selectOne(d); } }
/// <summary> /// interface defining Drawing object /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public interface Drawing { void setSelected(bool b); void draw(Graphics g); void move(int xpt, int ypt); bool contains(int x, int y); void setFill(bool b); Rectangle getRects(); void setRects(Rectangle rect); }
/// <summary> /// Summary description for Rectangle. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class Rectangle { /// <summary> /// /// </summary> private int xp, yp, wr, hr; /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="w"></param> /// <param name="h"></param> public Rectangle(int x, int y, int w, int h) { xp = x; yp = y; wr = w; hr = h; } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="w"></param> /// <param name="h"></param> public Rectangle(float x, float y, float w, float h) { xp = (int)x; yp = (int)y; wr = (int)w; hr = (int)h; } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public bool contains(int x, int y) { bool cn = xp <= x && x <= xp + wr; cn = cn && yp <= y && y <= yp + hr; return cn; } /// <summary> /// /// </summary> public int x { get { return xp; } set { xp = value; } } /// <summary> /// /// </summary> public int y { get { return yp; } set { yp = value; } } /// <summary> /// /// </summary> public int w { get { return wr; } set { wr = value; } } /// <summary> /// /// </summary> public int h { get { return hr; } set { hr = value; } } }
/// <summary> /// Summary description for VisCircle. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class VisCircle : VisRectangle { private int r; /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public VisCircle(int x, int y) : base(x, y) { r = 15; w = 30; h = 30; saveAsRect(); } /// <summary> /// /// </summary> /// <param name="g"></param> public override void draw(Graphics g) { if (filled) { g.FillEllipse(rBrush, x, y, w, h); } g.DrawEllipse(bPen, x, y, w, h); if (selected) { drawHandles(g); } } }
/// <summary> /// Summary description for Memento. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public interface Memento { void restore(); }
/// <summary> /// Summary description for Mediator.\ /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class Mediator { private bool startRect; private int selectedIndex; private RectButton rectb; private bool dSelected; private ArrayList drawings; private ArrayList undoList; private RectButton rButton; private FillButton filButton; private CircleButton circButton; private PickButton arrowButton; private PictureBox canvas; private int selectedDrawing; private StateManager stMgr; /// <summary> /// /// </summary> /// <param name="pic"></param> public Mediator(PictureBox pic) { startRect = false; dSelected = false; drawings = new ArrayList(); undoList = new ArrayList(); stMgr = new StateManager(this); canvas = pic; selectedDrawing = -1; } /// <summary> /// /// </summary> public void startRectangle() { stMgr.setRect(); arrowButton.setSelected(false); circButton.setSelected(false); filButton.setSelected(false); } /// <summary> /// /// </summary> public void startCircle() { stMgr.setCircle(); rectb.setSelected(false); arrowButton.setSelected(false); filButton.setSelected(false); } /// <summary> /// /// </summary> public void startFill() { stMgr.setFill(); rectb.setSelected(false); circButton.setSelected(false); arrowButton.setSelected(false); if (selectedDrawing >= 0) { stMgr.selectOne(getDrawing(selectedDrawing)); FillMemento m = new FillMemento(selectedDrawing, this); undoList.Add(m); } repaint(); } /// <summary> /// /// </summary> public void startArrow() { stMgr.setArrow(); rectb.setSelected(false); circButton.setSelected(false); filButton.setSelected(false); } /// <summary> /// /// </summary> /// <returns></returns> public Drawing getSelected() { return (Drawing)drawings[selectedDrawing]; } /// <summary> /// /// </summary> /// <param name="index"></param> public void setSelected(int index) { dSelected = true; selectedDrawing = index; Drawing d = getDrawing(index); d.setSelected(true); repaint(); } /// <summary> /// /// </summary> public void fillSelected() { if (dSelected) { Drawing d = (Drawing)drawings[selectedDrawing]; d.setFill(true); FillMemento m = new FillMemento(selectedDrawing, this); undoList.Add(m); } } /// <summary> /// /// </summary> public void undo() { rectb.setSelected(false); circButton.setSelected(false); arrowButton.setSelected(false); if (undoList.Count > 0) { //get last element in undo list object obj = undoList[undoList.Count - 1]; undoList.RemoveAt(undoList.Count - 1); //get the Memento Memento m = (Memento)obj; m.restore(); //and restore the old position repaint(); } } /// <summary> /// /// </summary> /// <returns></returns> public ArrayList getDrawings() { return drawings; } /// <summary> /// /// </summary> /// <param name="a"></param> /// <returns></returns> public Drawing getDrawing(int a) { if (a < drawings.Count) return (Drawing)drawings[a]; else return null; } /// <summary> /// /// </summary> /// <param name="d"></param> public void addDrawing(Drawing d) { drawings.Add(d); DrawInstance intc = new DrawInstance(drawings.Count - 1, this); undoList.Add(intc); } /// <summary> /// /// </summary> public void clear() { drawings = new ArrayList(); undoList = new ArrayList(); dSelected = false; selectedDrawing = 0; repaint(); } /// <summary> /// /// </summary> public void unpick() { dSelected = false; if (selectedDrawing >= 0) { Drawing d = getDrawing(selectedDrawing); if (d != null) { d.setSelected(false); } selectedDrawing = -1; repaint(); } } /// <summary> /// /// </summary> private void repaint() { canvas.Refresh(); } /// <summary> /// /// </summary> /// <param name="rb"></param> public void registerRectButton(RectButton rb) { rectb = rb; } /// <summary> /// /// </summary> /// <param name="fb"></param> public void registerFillButton(FillButton fb) { filButton = fb; } /// <summary> /// /// </summary> /// <param name="ab"></param> public void registerArrowButton(PickButton ab) { arrowButton = ab; } /// <summary> /// /// </summary> /// <param name="cb"></param> public void registerCircleButton(CircleButton cb) { circButton = cb; } /// <summary> /// /// </summary> /// <param name="a"></param> public void removeDrawing(int a) { drawings.RemoveAt(a); } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public int findDrawing(int x, int y) { int i = 0; bool found = false; int index = -1; while (i < drawings.Count && !found) { Drawing d = getDrawing(i); if (d.contains(x, y)) { index = i; found = true; } i++; } return index; } /// <summary> /// /// </summary> public void rememberPosition() { if (dSelected) { Drawing d = (Drawing)drawings[selectedDrawing]; Memento m = new DrawMemento(d); undoList.Add(m); } } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void mouseUp(int x, int y) { stMgr.mouseUp(x, y); } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void mouseDown(int x, int y) { stMgr.mouseDown(x, y); repaint(); } /// <summary> /// / /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void mouseDrag(int x, int y) { stMgr.mouseDrag(x, y); repaint(); } /// <summary> /// /// </summary> /// <param name="g"></param> public void reDraw(Graphics g) { foreach (object obj in drawings) { Drawing d = (Drawing)obj; d.draw(g); } } }
/// <summary> /// Summary description for FillState. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class FillState : State { public FillState(Mediator md) : base(md) { } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public override void mouseDown(int x, int y) { //Fill drawing if you click inside one int i = med.findDrawing(x, y); if (i >= 0) { Drawing d = med.getDrawing(i); d.setFill(true); //fill drawing } } /// <summary> /// /// </summary> /// <param name="d"></param> public override void selectOne(Drawing d) { //fill drawing if selected d.setFill(true); } }
/// <summary> /// Summary description for FillMemento. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class FillMemento : Memento { private int index; private Mediator med; /// <summary> /// /// </summary> /// <param name="dindex"></param> /// <param name="md"></param> public FillMemento(int dindex, Mediator md) { index = dindex; med = md; } /// <summary> /// /// </summary> public void restore() { Drawing d = med.getDrawing(index); d.setFill(false); } }
/// <summary> /// save the state of a visual rectangle /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class DrawMemento : Memento { private int x, y, w, h; private Rectangle rect; private Drawing visDraw; /// <summary> /// /// </summary> /// <param name="d"></param> public DrawMemento(Drawing d) { visDraw = d; rect = visDraw.getRects(); x = rect.x; y = rect.y; w = rect.w; h = rect.h; } /// <summary> /// /// </summary> public void restore() { //restore the state of a drawing object rect.x = x; rect.y = y; rect.h = h; rect.w = w; visDraw.setRects(rect); } }
/// <summary> /// Summary description for DrawInstance. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class DrawInstance : Memento { private int intg; private Mediator med; /// <summary> /// /// </summary> /// <param name="intg"></param> /// <param name="md"></param> public DrawInstance(int intg, Mediator md) { this.intg = intg; med = md; } /// <summary> /// /// </summary> public int integ { get { return intg; } } /// <summary> /// /// </summary> public void restore() { med.removeDrawing(intg); } }
/// <summary> /// Summary description for Command. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public interface Command { void Execute(); }
/// <summary> /// Summary description for CircleState. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class CircleState : State { public CircleState(Mediator md) : base(md) { } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public override void mouseDown(int x, int y) { VisCircle c = new VisCircle(x, y); med.addDrawing(c); } } }
/// <summary> /// Summary description for ArrowState. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class ArrowState : State { /// <summary> /// /// </summary> /// <param name="md"></param> public ArrowState(Mediator md) : base(md) { } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public override void mouseDown(int x, int y) { med.unpick(); med.startArrow(); int i = med.findDrawing(x, y); if (i >= 0) { med.setSelected(i); med.rememberPosition(); } } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public override void mouseDrag(int x, int y) { int i = med.findDrawing(x, y); if (i >= 0) { Drawing d = med.getDrawing(i); d.move(x, y); } } }
/// <summary> /// Summary description for CircleButton. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class CircleButton : ComdToolBarButton { /// <summary> /// /// </summary> /// <param name="md"></param> public CircleButton(Mediator md) : base("Circle", md) { this.DisplayStyle = ToolStripItemDisplayStyle.Text; // // this.Style=ToolBarButtonStyle.ToggleButton ; med.registerCircleButton(this); } /// <summary> /// / /// </summary> public override void Execute() { med.startCircle(); } }
/// <summary> /// Summary description for ClearButton. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class ClearButton : ComdToolBarButton { public ClearButton(Mediator md) : base("Clear", md) { } public override void Execute() { med.clear(); } }
/// <summary> /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class ComdToolBarButton : ToolStripButton, Command { protected Mediator med; protected bool selected; /// <summary> /// /// </summary> /// <param name="caption"></param> /// <param name="md"></param> public ComdToolBarButton(string caption, Mediator md) { InitializeComponent(); med = md; this.Text = caption; } /// <summary> /// /// </summary> /// <param name="b"></param> public void setSelected(bool b) { selected = b; if (!selected) this.selected = false; //this.Pushed = false; } /// <summary> /// /// </summary> public virtual void Execute() { } /// <summary> /// /// </summary> public ComdToolBarButton() { InitializeComponent(); } /// <summary> /// /// </summary> /// <param name="container"></param> public ComdToolBarButton(IContainer container) { container.Add(this); InitializeComponent(); } }
/// <summary> /// Summary description for UndoButton. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class UndoButton : ComdToolBarButton { public UndoButton(Mediator md) : base("Undo", md) { } public override void Execute() { med.undo(); } }
/// <summary> /// Summary description for RectButton. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class RectButton : ComdToolBarButton { public RectButton(Mediator md) : base("Rectangle", md) { //this.Style =ToolBarButtonStyle.ToggleButton ; this.DisplayStyle = ToolStripItemDisplayStyle.Text; med.registerRectButton(this); } public override void Execute() { med.startRectangle(); } }
/// <summary> /// Summary description for PickButton. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class PickButton : ComdToolBarButton { public PickButton(Mediator md) : base("Select", md) { //this.Style = ToolBarButtonStyle.ToggleButton ; this.DisplayStyle = ToolStripItemDisplayStyle.Text; med.registerArrowButton(this); } public override void Execute() { med.startArrow(); } }
/// <summary> /// Summary description for FillButton. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class FillButton : ComdToolBarButton { public FillButton(Mediator md) : base("Fill", md) { this.DisplayStyle = ToolStripItemDisplayStyle.Text; //this.Style = ToolBarButtonStyle.ToggleButton ; med.registerFillButton(this); } public override void Execute() { med.startFill(); } }
/// <summary> /// Summary description for VisRectangle. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class VisRectangle : Drawing { protected int x, y, w, h; private const int VSIZE = 30; private const int HSIZE = 40; private Rectangle rect; protected bool selected; protected bool filled; protected Pen bPen; protected SolidBrush bBrush, rBrush; /// <summary> /// /// </summary> /// <param name="xp"></param> /// <param name="yp"></param> public VisRectangle(int xp, int yp) { x = xp; y = yp; w = HSIZE; h = VSIZE; saveAsRect(); bPen = new Pen(Color.Black); bBrush = new SolidBrush(Color.Black); rBrush = new SolidBrush(Color.Red); } /// <summary> /// used by Memento for saving and restoring state /// </summary> /// <returns></returns> public Rectangle getRects() { return rect; } /// <summary> /// /// </summary> /// <param name="value"></param> public void setRects(Rectangle value) { x = value.x; y = value.y; w = value.w; h = value.h; saveAsRect(); } /// <summary> /// /// </summary> /// <param name="b"></param> public void setSelected(bool b) { selected = b; } /// <summary> /// move to new position /// </summary> /// <param name="xp"></param> /// <param name="yp"></param> public void move(int xp, int yp) { x = xp; y = yp; saveAsRect(); } /// <summary> /// /// </summary> /// <param name="g"></param> public virtual void draw(Graphics g) { //draw rectangle g.DrawRectangle(bPen, x, y, w, h); if (filled) g.FillRectangle(rBrush, x, y, w, h); drawHandles(g); } /// <summary> /// /// </summary> /// <param name="g"></param> public void drawHandles(Graphics g) { if (selected) { //draw handles g.FillRectangle(bBrush, x + w / 2, y - 2, 4, 4); g.FillRectangle(bBrush, x - 2, y + h / 2, 4, 4); g.FillRectangle(bBrush, x + (w / 2), y + h - 2, 4, 4); g.FillRectangle(bBrush, x + (w - 2), y + (h / 2), 4, 4); } } /// <summary> /// return whether point is inside rectangle /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public bool contains(int x, int y) { return rect.contains(x, y); } /// <summary> /// create Rectangle object from new position /// </summary> protected void saveAsRect() { rect = new Rectangle(x, y, w, h); } /// <summary> /// /// </summary> /// <param name="b"></param> public void setFill(bool b) { filled = b; } }
调用:
/// <summary> /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class StatePatternForm : Form { private Mediator med; private CircleButton circButton; private RectButton rctButton; private PickButton arowButton; private FillButton flButton; private UndoButton undoB; private ClearButton clrb; /// <summary> /// /// </summary> private void init() { //create a Mediator med = new Mediator(pic); //create the buttons rctButton = new RectButton(med); arowButton = new PickButton(med); circButton = new CircleButton(med); flButton = new FillButton(med); undoB = new UndoButton(med); clrb = new ClearButton(med); //add the buttons into the toolbar tBar.Items.Add(arowButton); tBar.Items.Add(rctButton); tBar.Items.Add(circButton); tBar.Items.Add(flButton); //include a separator //ToolBarButton sep = new ToolBarButton(); //sep.Style = ToolBarButtonStyle.Separator; ComdToolBarButton sep = new ComdToolBarButton(); sep.DisplayStyle = ToolStripItemDisplayStyle.Text;// ToolBarButtonStyle.Separator; sep.Text = "Separator"; tBar.Items.Add(sep); tBar.Items.Add(undoB); tBar.Items.Add(clrb); } /// <summary> /// /// </summary> public StatePatternForm() { InitializeComponent(); init(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void StatePatternForm_Load(object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pic_MouseDown(object sender, MouseEventArgs e) { med.mouseDown(e.X, e.Y); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pic_MouseMove(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.None) { med.mouseDrag(e.X, e.Y); } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pic_MouseUp(object sender, MouseEventArgs e) { med.mouseUp(e.X, e.Y); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pic_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; med.reDraw(g); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tBar_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { Command comd = (Command)e.ClickedItem; comd.Execute(); } }
输出:
标签:med,int,void,private,Patterns,State,CSharp,public From: https://www.cnblogs.com/geovindu/p/16746438.html