![]() |
Platforms, Frameworks & Libraries »
.NET Framework »
General
Intermediate
License: The Code Project Open License (CPOL)
AGE, Another Graphic Engine in .NETBy Fabio ZanettaA library that allows some GDI+ manipulation at runtime in an easy way |
VB, C# 2.0, Windows, .NET 2.0VS2005, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This library lets you manipulate graphic objects in your app at runtime.
Sometime ago, I read that someone had asked for a way to drag items over a panel. Since I had the same problem (all began with the designer of MyNeoReport project) I tried to solve this in a generic way, so that the system could be used in different applications without efforts.
AGE supports items dragging, snap-to-grid, multiselection, layers and much more.
AGE is growing fast; if I were you I would keep an eye on this project.
GraphicItem: The item to be drawnPainter: The class that does the "dirty work" of paintingGraphicDocument: The class that collects all the items to be drawn on a single surfaceCanvas: The control that shows the drawingQ: Why do I need an RendererBase and why don't I put the Render() method directly to the GraphicItem class?
A: Because I need more abstraction.
In this way I separate the logical item from the actor of the rendering.
I can virtually have the same renderer for different items or I can choose a different renderer for a single item.
Q: Why is the GraphicDocument a separate class from Canvas? I could add all the items to draw directly to the Canvas.
A: Same reason of the first question: the GraphicDocument is the logical items aggregation, it is the document, the canvas is the surface on which the document is drawn.
And again, in this way, I could have the same document open on more canvas.
Q: How can I make my own graphic document?
A: Ok, let's define a complete new custom document:
class CustomDocument : GraphicDocument
{
// define here your document properties
}
// define a custom item: an ellipse.
class Ellipse : GraphicItem
{
Color _foreColor = Color.Blue;
public Ellipse()
{
Painter = new EllipsePainter();
}
public Color ForeColor
{
get{ return _foreColor; }
set{ _foreColor = value; }
}
}
// define the ellipse painter
class EllipsePainter : Painter
{
// how the ellipse is painted...
protected override void Paint(Graphics g)
{
Ellipse ellipse = (Ellipse)Item;
Pen pen = new Pen(ellipse.ForeColor);
g.DrawEllipse(pen, Item.Bounds);
pen.Dispose();
}
}
...
canvas1.Document = new CustomDocument();
// add a new ellipse to the default layer
canvas1.Document.AddItem(new Ellipse());
From release 1.4.0.0, you can also use ScriptAge to define your custom graphic item at runtime:
ScriptedItem triangle = new ScriptedItem();
// Of course: it doesn't make much sense to hard code the script :)
// Rather you would load it from a separate text file or a string resource.
triangle.Script = "stroke: Polygon\n",
"pen: #7070ff, 2!\n",
"brush: gradient, #0000ff, #000000ff, (0; 0.5) (1; 1)\n",
"points: (0; 0.5) (1; 0.0) (1; 1)\n"
See the References section below for more details.
If you think this article and the project published are interesting and/or useful, you can help me maintain it in many ways:
Thanks to Itai Bar-Haim for the help and enhancements provided.
ItemEnter/ItemLeave eventsitems libraryGraphicItem [Browsable(false)]Min/MaxSizeSelectionPainter to customize the rendering of the selection "border" of selected itemsLoadFrom() and SaveTo() working on Stream, so you can include the document as an assembly resourceI need some help... any suggestions?
For any questions or suggestions, please go to my Web site forum.
I'm sorry, this release is not 100% compatible with 1.4.0.0 C but, you should not have problems with it. I changed some property names, so I chose to change the version number, and you need to recompile applications that use AGE engine.
If you have some documents to be loaded, you must edit them with Notepad and replace the properties that were renamed (listed below).
GraphicItem.RotationCenter, GraphicItem.RotationCenterRelativeTo that let you define the center of the rotation angleStroke.RotationCenter, with ScriptAGE you can define a rotation point for each single strokeCanvas QueryCursor event that lets you know about the cursor state and lets you set your custom cursorTheme and DefaultTheme classes that let you define the canvas appearance (Thanks to Itai Bar-Haim)Canvas.Zoom property (Thanks to Itai Bar-Haim)Canvas.Cursor is now readonly and reflects the cursor state at runtimeGraphicItem.Angle were renamed in GraphicItem.RotationAngleStroke.Angle were renamed in Stroke.RotationAngleCanvas.GridSize was renamed to Canvas.SnapGridSize to avoid conflicts with the Visual Studio designer GridSize propertyTreeView (not implemented TreeView.Sort(), TreeView.NodeMouseDoubleClick event, TreeView.NodeMouseClick() event)In this release, I enhanced ScriptAge again.
ScriptAge is 100% compatible with the 1.4.0.0 release.
path, radial, elliptic brush typesPaintStroke classPaintStroke.ParseToken() methodPathStroke classFrom now, I'll mark the AGE engine with the AssemblyDescriptionAttribute to indicate the sub-release. This is because from 1.4.0.0, I signed the assembly with a strong name and I can no longer modify the release number for compatibility reasons.
In this release, I enhanced ScriptAge (see the References section).
ScriptAge is 100% compatible with the 1.4.0.0 release.
Canvas fixesCanvas ItemMouseEnter and ItemMouseExit eventsCanvas at design-timeThis release let me add an heavy improvement to the flexibility of this library allowing you to define an item directly at runtime.

ScriptedItem class that lets you define the graphic with a little scriptThis time, the major change is the possibility to write custom items into separate libraries that can be loaded by the document at runtime.
virtual GraphicItem.PointIsOverMe() that lets you define if a point lies over the itemSize GraphicItem.MinSize that lets you define the minimum size of an itemclass SelectionPainter and GraphicItem.SelectionPainter that lets you customize the selection border of your itemvirtual Document.LoadLibrary() that lets you import GraphicItems from external DLLsGraphicLayer Canvas.ActiveLayerbool Canvas.AllowUserEditItems and bool Canvas.AllowUserScrollCanvas that lets you decide if the user can manipulate canvas content and howPainter.GetPoint(), Painter.GetSize(), Painter.FlipPoint() and Painter.FlipRectangle()Painter.OnItemBoundsChanged()This release focuses on layers feature. It was a real challenge and it is still in beta (It works! I'm thinking of a more OOP solution).
Document.Groups that define item groups and the wrapper GraphicDocument.LayersContextMenuStrip from canvas because is not a flexible solution for the applications that implement canvas and it is not supported by MonoCanvas.OffsetX and Canvas.OffsetY are now Point Canvas.OffsetRendererBase was renamed in Painter, so it is more similar to the framework namingThis release focuses on the ability of saving the document on disk, but there were dozens of collateral enhancement on this:
DocumentDocumentItemThis lets you derive what kind of document you want and lets you save it to disk without additional code.
GraphicDocumentnow inherits fromDocumentandGraphicIteminherits fromDocumentItem
canvas Document.OnAddingToDocument(DocumentActionEventArgs e)
Document.OnRemovingFromDocument(DocumentActionEventArgs e)
Document.OnAddedToDocument(DocumentActionEventArgs e)
Document.OnRemovedFromDocument(DocumentActionEventArgs e)
DocumentItem.OnAddingToDocument(DocumentActionEventArgs e)
DocumentItem.OnAddedToDocument(DocumentActionEventArgs e)
DocumentItem.OnRemovingFromDocument(DocumentActionEventArgs e)
DocumentItem.OnRemovedFromDocument(DocumentActionEventArgs e)
GraphicDocument.SaveImage(string fileName, ImageFormat format)
GraphicDocument.GetBounds()
GraphicDocument.GraphicItems // this is a subset of Document.Items
Document.SaveTo(string fileName)
Document.LoadFrom(string fileName)
Document.OnLoadingDocument()
Document.OnSavingDocument()
Document.OnDocumentLoaded()
Document.OnDocumentSaved()
Renderer.RenderType enum: now it is the Canvas that decides how and what to renderCanvas.DrawInvisibleItemsCanvas.DrawDocumentBoundsDocument.Items is now a GraphicItemCollection Canvas.SelectedItems is now a GraphicItemCollection Canvas.ItemMouseDoubleClick event is now Canvas.ItemDoubleClick Canvas.ItemClick event
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 10 May 2007 Editor: Deeksha Shenoy |
Copyright 2006 by Fabio Zanetta Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |