Game Development Reference
In-Depth Information
1.5A LIGNMENT
Whenworkinginuserinterfaces,onethingthatcomesupoftenistheneedtoalignelements
to one another. This functionality is driven by a set of straightforward equations that modify
the position and size of one element to that of another, yet all too often in practice, many
programmers write the alignment code in-place as needed, don't do this! Any time we im-
plement well defined solutions to problems, we need to provide these solutions as tools that
are available to anyone that may need them, including our future selves.
We will implement alignment of rectangles, as most, if not all of our user interface elements
may be bound by a rectangle.
1.5.1 Alignment of Two Rectangles
Given two rectangles, we will define one to be the fixed rectangle, this is the rectangle we
will align other rectangles to. And a floating rectangle, or several, these are the rectangles
that will be moved in relation to the fixed one.
Wewillcreateaclassthatwillprovideallthedifferenttypesofalignment,wewillconstruct
this class with the two rectangles, the invariable “fixed” rectangle, and one or more “float-
ing” rectangles that we will move to align. A good way to implement this class is using
enum flags that will allow us to combine different alignments when needed, in order to get
a rectangle that is aligned to the bottom right, we would combine the flags Bottom | Right.
The class will work directly using references to the rectangles we wish to affect, we keep a
reference to a list of floating rectangles and a constant fixed rectangle. The main part of the
align object is the Apply function in which we apply the alignment to the rectangles.
Search WWH ::




Custom Search