-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SUTK] Bug Fixes and Added AnchorRect to specify anchors relative to …
…the parent container
- Loading branch information
Showing
14 changed files
with
299 additions
and
27 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#pragma once | ||
|
||
#include <sutk/defines.hpp> // for SUTK_API, and Rect2D | ||
|
||
namespace SUTK | ||
{ | ||
class Container; | ||
|
||
// This class is derived from Container | ||
class SUTK_API AnchorRect | ||
{ | ||
private: | ||
Rect2Df m_rect; | ||
Rect2Df m_constraintRect; | ||
Container& m_childRect; | ||
Container& m_parentRect; | ||
AnchorRect(Container& childRect, Container& parentRect) noexcept; | ||
|
||
friend class Container; | ||
// should be called by the parent container whenever its rect is resized or changes its position | ||
void onParentResize(const Rect2Df& newRect, bool isPositionChanged, bool isSizeChanged) noexcept; | ||
// should be called by the child container whenever its rect is resized or changes its position | ||
void onChildResize(const Rect2Df& newRect, bool isPositionChanged, bool isSizeChanged) noexcept; | ||
|
||
Rect2Df getUnnormalizedAnchorRect() noexcept; | ||
void recalculateConstraints() noexcept; | ||
public: | ||
|
||
// getters | ||
Rect2Df getRect() const noexcept { return m_rect; } | ||
|
||
// setters | ||
|
||
void setRect(Rect2Df rect) noexcept; | ||
|
||
void setTopLeft(Vec2Df point) noexcept; | ||
void setBottomRight(Vec2Df point) noexcept; | ||
|
||
void setTopRight(Vec2Df point) noexcept; | ||
void setBottomLeft(Vec2Df point) noexcept; | ||
|
||
void extendTopLeft(Vec2Df disp) noexcept; | ||
void extendTopRight(Vec2Df disp) noexcept; | ||
void extendBottomRight(Vec2Df disp) noexcept; | ||
void extendBottomLeft(Vec2Df disp) noexcept; | ||
}; | ||
} |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include <sutk/AnchorRect.hpp> | ||
#include <sutk/Container.hpp> // for SUTK::Container member functions | ||
|
||
namespace SUTK | ||
{ | ||
AnchorRect::AnchorRect(Container& childRect, Container& parentRect) noexcept : m_childRect(childRect), m_parentRect(parentRect) | ||
{ | ||
// NOTE: initially the anchor rect is degenerate (width and height both is zero) and has position as { 0, 0 } | ||
|
||
recalculateConstraints(); | ||
} | ||
|
||
Rect2Df AnchorRect::getUnnormalizedAnchorRect() noexcept | ||
{ | ||
Vec2Df parentSize = m_parentRect.getSize(); | ||
// calculate unnormalized anchor rect in the parent rect's local coordinates | ||
Rect2Df anchorRect { m_rect.getPosition() * parentSize, m_rect.getSize() * parentSize }; | ||
return anchorRect; | ||
} | ||
|
||
void AnchorRect::recalculateConstraints() noexcept | ||
{ | ||
Rect2Df anchorRect = getUnnormalizedAnchorRect(); | ||
// signed difference is the constraint | ||
m_constraintRect = anchorRect - m_childRect.getRect(); | ||
} | ||
|
||
void AnchorRect::onParentResize(const Rect2Df& newRect, bool isPositionChanged, bool isSizeChanged) noexcept | ||
{ | ||
Rect2Df anchorRect = getUnnormalizedAnchorRect(); | ||
Rect2Df newChildRect = anchorRect - m_constraintRect; | ||
m_childRect.setRect(newChildRect); | ||
} | ||
|
||
void AnchorRect::onChildResize(const Rect2Df& newRect, bool isPositionChanged, bool isSizeChanged) noexcept | ||
{ | ||
recalculateConstraints(); | ||
} | ||
|
||
void AnchorRect::setRect(Rect2Df rect) noexcept | ||
{ | ||
m_rect = rect; | ||
recalculateConstraints(); | ||
} | ||
|
||
void AnchorRect::setTopLeft(Vec2Df point) noexcept | ||
{ | ||
m_rect.setTopLeft(point); | ||
recalculateConstraints(); | ||
} | ||
|
||
void AnchorRect::setBottomRight(Vec2Df point) noexcept | ||
{ | ||
m_rect.setBottomRight(point); | ||
recalculateConstraints(); | ||
} | ||
|
||
|
||
void AnchorRect::setTopRight(Vec2Df point) noexcept | ||
{ | ||
m_rect.setTopRight(point); | ||
recalculateConstraints(); | ||
} | ||
|
||
void AnchorRect::setBottomLeft(Vec2Df point) noexcept | ||
{ | ||
m_rect.setBottomLeft(point); | ||
recalculateConstraints(); | ||
} | ||
|
||
|
||
void AnchorRect::extendTopLeft(Vec2Df disp) noexcept | ||
{ | ||
m_rect.extendTopLeft(disp); | ||
recalculateConstraints(); | ||
} | ||
|
||
void AnchorRect::extendTopRight(Vec2Df disp) noexcept | ||
{ | ||
m_rect.extendTopRight(disp); | ||
recalculateConstraints(); | ||
} | ||
|
||
void AnchorRect::extendBottomRight(Vec2Df disp) noexcept | ||
{ | ||
m_rect.extendBottomRight(disp); | ||
recalculateConstraints(); | ||
} | ||
|
||
void AnchorRect::extendBottomLeft(Vec2Df disp) noexcept | ||
{ | ||
m_rect.extendBottomLeft(disp); | ||
recalculateConstraints(); | ||
} | ||
|
||
} |
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
Oops, something went wrong.