Skip to content

Instantly share code, notes, and snippets.

@Enhex
Created July 13, 2016 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Enhex/357c0fe028afe086c976688d677326f4 to your computer and use it in GitHub Desktop.
Save Enhex/357c0fe028afe086c976688d677326f4 to your computer and use it in GitHub Desktop.
/*
The MIT License (MIT)
Copyright (c) 2016 Yehonatan Ballas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "AspectRatioElement.hpp"
#include <Urho3D/UI/UI.h>
#include <Urho3D/UI/UIEvents.h>
AspectRatioElement::AspectRatioElement(Context* context) :
Object(context)
{
}
void AspectRatioElement::setRatio(const Vector2 & new_ratio)
{
ratio = new_ratio;
updateSize();
}
void AspectRatioElement::setFitInside(const bool new_fit_inside)
{
fit_inside = new_fit_inside;
updateSize();
}
void AspectRatioElement::setElement(UIElement * new_element, const Vector2 & new_ratio)
{
ratio = new_ratio;
setElement(new_element);
}
void AspectRatioElement::setElement(UIElement * new_element)
{
element = new_element;
SubscribeToEvent(element->GetParent(), E_RESIZED, URHO3D_HANDLER(AspectRatioElement, HandleParentResized));
updateSize();
}
void AspectRatioElement::updateSize()
{
if (element.Null())
return;
auto parent_size_int = element->GetParent()->GetSize();
auto parent_size = Vector2((float)parent_size_int.x_, (float)parent_size_int.y_);
auto scale = parent_size / ratio;
auto size = ratio * (fit_inside ? Min(scale.x_, scale.y_) : Max(scale.x_, scale.y_));
element->SetSize((int)size.x_, (int)size.y_);
}
//
// HandleRootResized
//
void AspectRatioElement::HandleParentResized(StringHash eventType, VariantMap & eventData)
{
if (element.Null())
return;
updateSize();
}
/*
The MIT License (MIT)
Copyright (c) 2016 Yehonatan Ballas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef AspectRatioElement_hpp
#define AspectRatioElement_hpp
#include <Urho3D/Urho3D.h>
#include <Urho3D/Core/Context.h>
namespace Urho3D
{
class UIElement;
}
using namespace Urho3D;
// Automatically resize a UI element to maintain an aspect ratio with maximum size inside its parent
class AspectRatioElement : public Object
{
URHO3D_OBJECT(AspectRatioElement, Object)
public:
AspectRatioElement(Context* context);
// Set width:height ratio
void setRatio(const Vector2& new_ratio);
Vector2 getRatio() { return ratio; }
void setFitInside(const bool new_fit_inside);
bool getFitInside() { return fit_inside; }
void setElement(UIElement* new_element, const Vector2& new_ratio);
void setElement(UIElement* new_element);
UIElement* getElement() { return element; }
protected:
SharedPtr<UIElement> element;
Vector2 ratio;
bool fit_inside = true;
void updateSize();
void HandleParentResized(StringHash eventType, VariantMap& eventData);
};
#endif//guard
#include "RelativeSizeElement.hpp"
#include <Urho3D/UI/UI.h>
#include <Urho3D/UI/Text.h>
#include <Urho3D/UI/UIEvents.h>
RelativeSizeElement::RelativeSizeElement(Context* context) :
Object(context)
{
}
void RelativeSizeElement::setScale(const Vector2& new_scale)
{
scale = new_scale;
updateSize();
}
void RelativeSizeElement::setElement(UIElement * new_element, const Vector2& new_scale)
{
scale = new_scale;
setElement(new_element);
}
void RelativeSizeElement::setElement(UIElement * new_element)
{
element = new_element;
SubscribeToEvent(element->GetParent(), E_RESIZED, URHO3D_HANDLER(RelativeSizeElement, HandleParentResized));
updateSize();
}
void RelativeSizeElement::updateSize()
{
auto& parent_size = element->GetParent()->GetSize();
if (scale.x_ >= 0)
element->SetWidth(int(parent_size.x_ * scale.x_));
if (scale.y_ >= 0)
element->SetHeight(int(parent_size.y_ * scale.y_));
}
//
// HandleParentResized
//
void RelativeSizeElement::HandleParentResized(StringHash eventType, VariantMap & eventData)
{
if (element.Null())
return;
updateSize();
}
/*
The MIT License (MIT)
Copyright (c) 2016 Yehonatan Ballas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef RelativeSizeElement_hpp
#define RelativeSizeElement_hpp
#include <Urho3D/Urho3D.h>
#include <Urho3D/Core/Context.h>
namespace Urho3D
{
class UIElement;
}
using namespace Urho3D;
// Automatically resize a Text element's font size relative to its parent's height
class RelativeSizeElement : public Object
{
URHO3D_OBJECT(RelativeSizeElement, Object)
public:
RelativeSizeElement(Context* context);
void setScale(const Vector2& new_scale);
Vector2 getScale() { return scale; }
void setElement(UIElement* new_element, const Vector2& new_scale);
void setElement(UIElement* new_element);
UIElement* getElement() { return element; }
protected:
SharedPtr<UIElement> element;
Vector2 scale = Vector2(1.f, 1.f);
void updateSize();
void HandleParentResized(StringHash eventType, VariantMap& eventData);
};
#endif//guard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment