正如Lucius所述,创建一个Container类是最好的解决方案。 目前我正在为XBox开发一个UI应用程序。因此,我需要类似定位引擎的东西,一切都是相对的,所以我不需要每次都计算像素。
我所做的是创建一个容器类,它包含大致如下属性:
VectorTopLeft (Which the element which contains a Container object uses for drawing)
VectorTopRight
VectorBottomLeft
VectorBottomRight
Align (Enum: Right, Center, Left)
VerticalAlign (Enum: Top, Middle, Bottom)
NewRow (bool)
PreviousContainer (Container)
ParentContainer (Container)
Width (Getter)
Height (Getter)
PercentageHeight (getter/setter) (Percentage of the height of the parent container)
PercentageWidth (getter/setter) (Percentage of the width of the parent container)
PixelHeight (getter/setter) (Absolute height in pixels)
PixelWidth (getter/setter) (Absolute width in pixels)
AspectRatio: Used for setting the width to a ratio of the height, usefull for different screen aspects (4/3 or 16/9 for example)
MarginLeft
MarginRight
MarginTop
MarginBottom
以下载体包括边缘,这些都是由对准过程中使用的载体。
AbsoluteVectorTopLeft
AbsoluteVectorTopRight
AbsoluteVectorBottomLeft
AbsoluteVectorBottomRight
以下尺寸属性还包括利润,有用的计算剩余大小
AbsoluteWidth (getter)
AbsoluteHeight (getter)
然后一些标志,其如果有什么重要的变化,和矢量/大小的东西需要被置为true,重新计算。
对齐的东西是非常复杂的,因为它使用递归,并且还调用先前的容器函数将所有东西都移到正确的位置。
newrow属性告诉系统它需要在父容器的新行处启动该元素,并用于保持垂直对齐。
该系统可能有一些小缺陷,但在这一刻,它作为我所有与GUI相关的定位工具的魅力,并且它的工作速度非常快!