Game Development Reference
In-Depth Information
All controls will use a rectangle to define their bounds on screen, these bounds are useful
for calculating placement and performing hit testing to determine if the control needs to
perform some action due to input. There are two ways to specify the bounds for a con-
trol, we can specify the position and size in pixels or we can use normalized coordinates
[0.0,1.0] that represent the size of the viewport.
void control::SetPosition(const vector2& position, eUnit unit /*=Percent*/)
{
auto viewport = m_core->GetDevice()->GetViewport();
switch ( unit )
{
case Percent:
m_rectangle.SetPosition(vector2(position.x() * viewport.Width(), position.y() * viewport.Height()) );
break;
default:
case Pixels:
m_rectangle.SetPosition(position);
break;
}
Refresh();
}
When working in environments that may support multiple resolutions, or in order to port
the game onto different platforms, including mobile platforms, we may need to change the
screen resolution to conform to the hardware the game is running on. For this reason it is
usually best to specify placement coordinates in normalized coordinates, this way, if the
resolution change, you know that your control will always be placed at some percentage
of the viewport. However, in some cases it is necessary to work directly in pixels , for ex-
ample, labels in a list may use the list's screen position in pixels as their starting position,
in this case it's easier to offset each label by the font height in pixels than it would be to
convert it to normalized coordinates.
NOTE : Unityconsidersthebottom-mostscreencoordinatetobe0.0,andthetop-mostYto
be1.0.TomakeourUIsystemcompatibleacrossmultipleengines,wewillneedtoprovide
functionality thatcanperformtheappropriatecalculations dependingonwhatourplatform
requires and it should do so internally so that the user-facing side of our code API remains
the same regardless of the target platform.
On construction we will set a control's viewport to match that of the graphics device. This
makestheplacementrelativetothescreencoordinates.Wemayoverridethisviewportwith
acustom onetogiveustheability toplace controls relative toadifferent viewport thanthe
screen's.
Search WWH ::




Custom Search