AGE:编写您自己的图形库






4.59/5 (8投票s)
2006年12月17日
4分钟阅读

41569

363
如何为 AGE 引擎编写自定义 GraphicItem 库
引言
本文介绍如何为AGE图形引擎编写图形项库。
背景
AGE“仅仅”是一个允许您在运行时管理图形项的引擎,但这些项并非内置于其中,因为您可以完全自由地创建自己的项以在AGE图形文档中使用。
现在我将解释如何创建一个图形项以供AGE使用,您会发现这真的很简单(比解释花费的时间还少)。
让我们做一个菱形(嘿,这只是一个例子!)。
第一步:开始之前我们需要什么。
- 创建一个库项目。
- 在您的项目中添加对引擎程序集的引用:NeoDataType.Graphic.dll。
- 添加命名空间的导入。
// c#
using NeoDataType.Documents;
using NeoDataType.Graphic;
' Vb.Net
Imports NeoDataType.Documents
Imports NeoDataType.Graphic
第二步:项。
// Let's define the custom item
[SaveThisClass]
public class Diamond : GraphicItem
{
// Let's define all the properties that
// describe the diamond:
// the color used by the pen
Color _foreColor = Color.Blue;
[SaveThisProperty(typeof(ColorFormatter))]
public Color ForeColor
{
get{ return _foreColor; }
set
{
_foreColor = value;
PropertyChanged();
}
}
// The constructor.
// We'll use it to instantiate a proper Painter.
public Diamond()
{
Painter = new DiamondPainter();
}
}
第三步:绘制器。
// define the diamond painter
public class DiamondPainter : Painter
{
Point[] _points;
// optimize drawings performances computing coordinates
// only if required.
protected override void OnItemBoundsChanged()
{
base.OnItemBoundsChanged();
_points = new Point[]
{
GetPoint(0.5, 0),
GetPoint(1, 0.5),
GetPoint(0.5, 1),
GetPoint(0, 0.5)
};
}
// how the diamond is painted...
protected override void Paint(Graphics g)
{
Diamond diamond = (Diamond)Item;
Pen pen = new Pen(diamond.ForeColor);
g.DrawPolygon(pen, _points);
pen.Dispose();
}
}
好的……让我解释一下
- 如果希望将项保存到磁盘并与文档一起保存,则需要导入
NeoDataType.Documents
命名空间。如果您计划在不保存/加载项的情况下在运行时完成所有图形操作,则不需要此项,也不需要
SaveThisClassAttribute
和SaveThisPropertyAttribute
。 SaveThisClassAttribute
和SaveThisPropertyAttribute
定义了在保存文档时必须将类或属性写入文件。在此示例中,
Color
不是IConvertible
对象,因此不能像Int32
、Double
、String
或Enum
值那样自动保存。因此,我们需要指定一个格式化器。格式化器是一个实现
NeoDataType.Documents.IObjectFormatter
的类,它将Color
转换为string
表示形式,并且能够将string
重新转换回给定的Color
。但我们很幸运,因为ColorFormatter
和FontFormatter
内置在NeoDataType.Graphic.dll中。set
属性中的PropertyChanged()
告诉画布刷新,因为影响项外观的属性已更改。- 当项的位置或大小发生变化时,会调用
Painter.OnItemBoundsChanged()
。您可以使用此方法进行所有必要的编译,并且绘制过程可以避免在每次绘制画布时都执行此操作。当然,您可以在Painter.Paint()
方法中完成所有操作,但这样可以减少资源浪费。Painter.GetPoint()
和Painter.GetSize()
返回相对于项边界的Point
或Size
。Painter.FlipPoint()
和Painter.FlipRectangle()
返回相对于项边界翻转的Point
或Rectangle
。您可以通过x和y坐标的一个0到1之间的因子来指示一个点在边界内的位置。
在本篇文章的示例中,顶角位于点(0.5; 0)。
此时,我们的项已完全正常工作:现在我们可以编译程序集,转到设计器并将库添加到文档中(或以编程方式myDocument.LoadLibrary(libraryPath)
)。但正如您所见……我们没有菱形的图标!
第四步:项图标。
图标必须是16 x 16的.ico文件,如果图标大小不同,它将被调整大小。
引擎会将其作为嵌入式资源查找,名称为AssemblyName.ToolboxIcons.ItemTypeName.ico
,其中AssemblyName
是项目属性中定义的程序集名称,ItemName
是相关GraphicItem
的名称(在此例中为Diamond
)。
因此,在本例中,使用Visual Studio或其免费版本,程序集名称必须符合以下之一:
正确示例
Assembly Name: MyAssembly.Name
Default Namespace: MyAssembly.Name
Icon Folder: ToolboxIcons
Icon Name: Diamond.ico
Assembly Name: MyAssembly.Name
Default Namespace: MyAssembly
Icon Folder: Name.ToolboxIcons
Icon Name: Diamond.ico
错误示例
Assembly Name: MyAssembly.Name
Default Namespace: MyAssembly.Name.AndMore
Icon Folder: ToolboxIcons
Icon Name: Diamond.ico
Assembly Name: MyAssembly.Name
Default Namespace: Something.Else
Icon Folder: ToolboxIcons
Icon Name: Diamond.ico
对于程序集名称和默认Namespace
,您可以检查项目属性。
一些提示和建议
- 请注意,如果您没有为
myDocument.LoadLibrary(libraryPath)
指定完整路径,而只指定了文件名,那么库将首先在文档文件所在的文件夹中搜索,然后在应用程序文件夹中名为“Libraries”的子文件夹中搜索。只有库文件名会保存到文档中作为引用。 - 绘制器类应命名为
<PaintedItem>Painter
,其中它是绘制项的名称(这是显而易见的)。在本例中是“ Diamond
”。
您如何做出贡献?
如果您认为本文和发布的项目有趣或有用,您可以以多种方式帮助我维护它
- 将您的项库发送给我(我将以您的名义发布)
- 告诉我本文是否清晰或缺少内容。
- 为本文投票
- 访问我的网站(www.neodatatype.net)
- 访问我的网站并做一些小贡献(这将是极大的鼓励)