This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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::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 * Min(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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 setElement(UIElement* new_element, const Vector2& new_ratio); | |
void setElement(UIElement* new_element); | |
UIElement* getElement() { return element; } | |
protected: | |
SharedPtr<UIElement> element; | |
Vector2 ratio; | |
void updateSize(); | |
void HandleParentResized(StringHash eventType, VariantMap& eventData); | |
}; | |
#endif//guard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 "RelativeFontSize.hpp" | |
#include <Urho3D/UI/UI.h> | |
#include <Urho3D/UI/Text.h> | |
#include <Urho3D/UI/UIEvents.h> | |
RelativeFontSize::RelativeFontSize(Context* context) : | |
Object(context) | |
{ | |
} | |
void RelativeFontSize::setScale(const float new_scale) | |
{ | |
scale = new_scale; | |
updateSize(); | |
} | |
void RelativeFontSize::setText(Text * new_text, const float new_scale) | |
{ | |
scale = new_scale; | |
setText(new_text); | |
} | |
void RelativeFontSize::setText(Text * new_text) | |
{ | |
text = new_text; | |
SubscribeToEvent(text->GetParent(), E_RESIZED, URHO3D_HANDLER(RelativeFontSize, HandleParentResized)); | |
updateSize(); | |
} | |
void RelativeFontSize::updateSize() | |
{ | |
text->SetFont(text->GetFont(), int(text->GetParent()->GetHeight() * scale)); | |
} | |
// | |
// HandleParentResized | |
// | |
void RelativeFontSize::HandleParentResized(StringHash eventType, VariantMap & eventData) | |
{ | |
if (text.Null()) | |
return; | |
updateSize(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 RelativeFontSize_hpp | |
#define RelativeFontSize_hpp | |
#include <Urho3D/Urho3D.h> | |
#include <Urho3D/Core/Context.h> | |
namespace Urho3D | |
{ | |
class Text; | |
} | |
using namespace Urho3D; | |
// Automatically resize a Text element's font size relative to its parent's height | |
class RelativeFontSize : public Object | |
{ | |
URHO3D_OBJECT(RelativeFontSize, Object) | |
public: | |
RelativeFontSize(Context* context); | |
void setScale(const float new_scale); | |
float getScale() { return scale; } | |
void setText(Text* new_text, const float new_scale); | |
void setText(Text* new_text); | |
Text* getText() { return text; } | |
protected: | |
SharedPtr<Text> text; | |
float scale = 1.f; | |
void updateSize(); | |
void HandleParentResized(StringHash eventType, VariantMap& eventData); | |
}; | |
#endif//include guard |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment