Skip to content

Instantly share code, notes, and snippets.

@CarloMaker
Created January 22, 2014 11:27
Show Gist options
  • Save CarloMaker/8557193 to your computer and use it in GitHub Desktop.
Save CarloMaker/8557193 to your computer and use it in GitHub Desktop.
Simple layout relative position
main(){
//load layout
XMLFile* xmlFile = cache->GetResource<XMLFile>(layoutName);
SharedPtr<UIElement> lay =GetContext()->GetSubsystem<UI>()->LoadLayout(xmlFile);
GetContext()->GetSubsystem<UI>()->GetRoot()->AddChild(lay);
Vector2 resEditor (1920,1080);// this is the resolution used in the editor - could be came from .xml
Vector2 currentResolution (1600,1080); // current resolution from EngineParameters
GuiHelper::ReDraw(lay,resEditor,currentResolution);
}
GuiHelper.h
class GUIHELPER
{
....
static void ReDraw( Urho3D::UIElement * root , Urho3D::Vector2 & layoutResolution , Urho3D::Vector2 & windowResolution );
static Urho3D::Vector2 convertFromRelative(const Urho3D::Vector2 & _point, const Urho3D::Vector2 & _view);
static Urho3D::Vector2 convertToRelative(const Urho3D::IntVector2& _coord, const Urho3D::Vector2& _view);
}
GuiHelper.cpp
void GuiHelper::ReDraw( UIElement * root , Vector2 & layoutResolution , Vector2 & windowResolution )
{
//take all elements
PODVector<UIElement*> child;
root->GetChildren(child,true);
for (unsigned int i = 0 ; i < child.Size();i++)
{
UIElement * ui= child[i];
//convert position
{
IntVector2 position =ui->GetPosition();
Vector2 rel= convertToRelative(position,rifWindows);
Vector2 abs=convertFromRelative(rel,renderWindow);
IntVector2 absPos=IntVector2( (int)abs.x_,(int)abs.y_);
ui->SetPosition(absPos);
}
//convert size
{
IntVector2 size =ui->GetSize();
Vector2 rel= convertToRelative(size,rifWindows);
Vector2 abs=convertFromRelative(rel,renderWindow);
IntVector2 absSize=IntVector2( (int)abs.x_,(int)abs.y_);
ui->SetSize(absSize);
}
}
}
Urho3D::Vector2 GuiHelper::convertFromRelative( const Vector2 & _point, const Vector2 & _view )
{
return Vector2( _point.x_ * _view.x_, _point.y_ * _view.y_);
}
Urho3D::Vector2 GuiHelper::convertToRelative( const IntVector2& _coord, const Vector2& _view )
{
return Vector2( _coord.x_ / (float)_view.x_, _coord.y_ / (float)_view.y_);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment