00001
00002
00003
00004
00005
00006 namespace Grendel.Ui {
00007 using Grendel.Base;
00008
00009 public abstract class Button : BaseInteractiveShape {
00010 private string text;
00011 private double width;
00012 private double height;
00013
00014 public Button(Canvas canvas, string text, double width) : base(canvas) {
00015 this.text = text;
00016 this.width = width;
00017 this.height = 24.0;
00018 }
00019
00020 public override Position LeftTopCorner {
00021 get { return Position.Origin; }
00022 }
00023
00024 public override Position RightBottomCorner {
00025 get { return new Position(width, height); }
00026 }
00027
00028 public string Label {
00029 get {
00030 return text;
00031 }
00032 set {
00033 text = value;
00034 }
00035 }
00036
00037 public override void Painting () {
00038 Canvas.DrawRectangle(this, LeftTopCorner, RightBottomCorner, Color.Black);
00039 double shift = (width - (Font.Monospace.Size - 2) * text.Length) / 2.0;
00040 Canvas.DrawText(this, new Position(shift, 5), Label);
00041 }
00042
00043
00044 public override void Interaction (MouseButtons button, Position location) {
00045 if(button.IsPressedLeft)
00046 Action();
00047 }
00048
00049 public abstract void Action();
00050 }
00051
00052 public class CloneButton : Button, IEventListener {
00053 ICanvasCloneable pattern;
00054 MouseEventTarget oldTarget;
00055
00056 public CloneButton(Canvas canvas, string text, double width, ICanvasCloneable pattern)
00057 : base(canvas, text, width){
00058 this.pattern = pattern;
00059 }
00060
00061 public override void Action () {
00062 oldTarget = Canvas.MouseEventTarget;
00063 Canvas.MouseEventTarget = MouseEventTarget.EventListeners;
00064 Canvas.AddEventListener(this);
00065 }
00066
00067 public void MouseEventHandler (MouseButtons buttons, Position absolutePosition)
00068 {
00069 Canvas.RemoveEventListener(this);
00070 Canvas.MouseEventTarget = oldTarget;
00071 IShape shape = pattern.Clone() as IShape;
00072 if(shape != null) {
00073 shape.Location = absolutePosition;
00074 }
00075 }
00076
00077
00078 }
00079 }