00001 namespace Grendel.Base {
00002 using System;
00003 using System.Diagnostics;
00004 using Grendel.Communication;
00005 using System.Collections.Generic;
00006
00010 public interface IShape : ICanvasObject {
00018 Position Location {get; set;}
00027 IShape ParentShape {get; set;}
00032 Position AbsolutePosition {get;}
00039 Color BaseColor {get; set;}
00046 int Layer{get; set;}
00052 WorldMatrix LocalTransformation {get; set;}
00057 WorldMatrix TotalTransformation {get;}
00066 void Painting();
00067 }
00068
00069
00074 public interface IInteractiveShape : IShape {
00081 Position LeftTopCorner {get;}
00088 Position RightBottomCorner {get;}
00093 void Interaction(MouseButtons button, Position location);
00098 bool IsActive {get; set;}
00099 }
00100
00106 public abstract class BaseCanvasObject : ICanvasObject {
00107 private Canvas canvas;
00108 private IList<string> comGroups;
00109
00114 public BaseCanvasObject(Canvas canvas) {
00115 this.canvas = canvas;
00116 canvas.Remember(this);
00117 comGroups = new List<string>();
00118 }
00119
00123 public Canvas Canvas {
00124 get { return canvas; }
00125 }
00126
00127 public IList<string> ComunnicationGroups {
00128 get {
00129 return comGroups;
00130 }
00131 }
00132
00133 public void AddToCommunicationGroup (string groupName) {
00134 Debug.Assert(!comGroups.Contains(groupName), "Group name is not unique");
00135 comGroups.Add(groupName);
00136 LocalCommunicator.Nexus.Subscribe(groupName, this);
00137 }
00138
00139 public void RemoveFromCommunicationGroup (string groupName) {
00140 Debug.Assert(comGroups.Contains(groupName), "Removed group name is not present");
00141 comGroups.Remove(groupName);
00142 LocalCommunicator.Nexus.SignOff(groupName, this);
00143 }
00144
00145 public virtual void MessageReceiving(object message) {
00146
00147 }
00148
00149 protected void InheritGroupsFrom (ICanvasObject templet) {
00150 foreach(string group in templet.ComunnicationGroups)
00151 comGroups.Add(group);
00152 }
00153
00154 public override string ToString () {
00155 return base.ToString() + " on " + canvas.ToString();
00156 }
00157 }
00158
00163 public abstract class BaseShape : BaseCanvasObject, IShape {
00164 private Position location;
00165 private IShape parent;
00166 private Color baseColor;
00167 private WorldMatrix matrix;
00168 private int layer;
00169
00173 public BaseShape(Canvas canvas, IShape parentShape)
00174 : base(canvas) {
00175 this.parent = parentShape;
00176 Location = Position.Origin;
00177 BaseColor = Color.Black;
00178 LocalTransformation = new WorldMatrix();
00179 layer = 0;
00180 }
00181
00185 public BaseShape(Canvas canvas) : this(canvas, null){}
00186
00187 public Position Location {
00188 get {return location;}
00189 set {location = value;}
00190 }
00191
00192 public IShape ParentShape {
00193 get { return parent; }
00194 set {parent = value;}
00195 }
00196
00197 public Position AbsolutePosition {
00198 get {
00199 return TotalTransformation.Transform(Position.Origin);
00200 }
00201 }
00202
00203 public Color BaseColor {
00204 get {
00205 return baseColor;
00206 }
00207 set {
00208 baseColor = value;
00209 }
00210 }
00211
00212 public int Layer {
00213 get { return layer; }
00214 set {
00215 if(value != layer) {
00216 Canvas.MoveToLayer(this, layer, value);
00217 layer = value;
00218 }
00219 }
00220 }
00221
00222 public WorldMatrix LocalTransformation {
00223 get {return matrix;}
00224 set {matrix = value;}
00225 }
00226
00227 public WorldMatrix TotalTransformation {
00228 get {
00229 WorldMatrix tm = new WorldMatrix(LocalTransformation);
00230 tm.Translate(Location - Position.Origin);
00231 if(ParentShape == null)
00232 return tm;
00233 else
00234 return tm.AddTransformations(ParentShape.TotalTransformation);
00235 }
00236 }
00237
00238 public abstract void Painting();
00239 }
00240
00247 public abstract class BaseInteractiveShape : BaseShape, IInteractiveShape {
00248 private bool active;
00249
00250 public BaseInteractiveShape(Canvas canvas, IShape parentShape)
00251 : base(canvas, parentShape) {
00252 active = true;
00253 }
00254
00255 public BaseInteractiveShape(Canvas canvas) : this(canvas, null){}
00256
00257 public bool IsActive {
00258 get {return active;}
00259 set {active = value;}
00260 }
00261
00262 public abstract Position LeftTopCorner {get;}
00263 public abstract Position RightBottomCorner {get;}
00264
00265 public abstract void Interaction(MouseButtons button, Position location);
00266 }
00267 }