Simple GUI Library
anchor_pane.h
Go to the documentation of this file.
1/**
2 * @author Nikita Mochalov (github.com/tralf-strues)
3 * @file anchor_pane.h
4 * @date 2021-12-01
5 *
6 * @copyright Copyright (c) 2021
7 */
8
9#pragma once
10
11#include <unordered_map>
12#include "layered_container.h"
13
14namespace Sgl
15{
16 /**
17 * @brief Allows the edges of components to be anchored to anchor pane's edges.
18 *
19 * You can set constraints for a child using set<Side>Anchor() method, which accepts
20 * an integer offset. This child's <Side> will be anchored to the pane's <Side> with
21 * this offset.
22 *
23 * If offset is equal to @ref ANCHOR_NOT_ENABLED, then it is interpreted as
24 * there's no anchor attached to this edge.
25 *
26 * You can use @ref setHorizontalRelative() or @ref setVerticalRelative() methods to
27 * enable relative offsets. This means that the enabled offsets (if one of the opposite
28 * offsets is not enabled, then does nothing) are interpreted as integer weights of
29 * respective offsets. In this case the child is set to its preffered size in this
30 * direction, and the free space in this direction is split according to weighted
31 * offsets. For example, imagine that anchor pane's height is set to 1280, child's
32 * preffered height is 280 and the top and bottom anchors are equal to 1 and 4
33 * respectively. In this case the free vertical 1000 pixels will be split as following:
34 * absolute top offset = 1000 * 1 / (1 + 4) = 200,
35 * absolute bottom offset = 1000 * 4 / (1 + 4) = 800.
36 */
38 {
39 public:
40 static constexpr int32_t ANCHOR_NOT_ENABLED = -1;
41
42 struct Anchors
43 {
44 int32_t topAnchor = ANCHOR_NOT_ENABLED;
45 int32_t rightAnchor = ANCHOR_NOT_ENABLED;
46 int32_t bottomAnchor = ANCHOR_NOT_ENABLED;
47 int32_t leftAnchor = ANCHOR_NOT_ENABLED;
48 };
49
51 {
52 Anchors anchors;
53 bool isHorizontalRelative = false;
54 bool isVerticalRelative = false;
55 };
56
57 public:
58 virtual ~AnchorPane() override = default;
59
60 Constraints getConstraints(Component* child) const;
61 bool hasConstraints(Component* child) const;
62
63 void setHorizontalRelativePositioning(Component* child, bool enabled);
64 void setVerticalRelativePositioning(Component* child, bool enabled);
65
66 bool isHorizontalRelativePositioning(Component* child) const;
67 bool isVerticalRelativePositioning(Component* child) const;
68
69 void setTopAnchor(Component* child, int32_t anchor);
70 void setRightAnchor(Component* child, int32_t anchor);
71 void setBottomAnchor(Component* child, int32_t anchor);
72 void setLeftAnchor(Component* child, int32_t anchor);
73
74 int32_t getTopAnchor(Component* child) const;
75 int32_t getRightAnchor(Component* child) const;
76 int32_t getBottomAnchor(Component* child) const;
77 int32_t getLeftAnchor(Component* child) const;
78
79 private:
80 std::unordered_map<Component*, Constraints> m_Constraints;
81
82 private:
83 virtual void layoutChildren() override;
84
85 virtual int32_t computeCustomPrefWidth(int32_t height = -1) const override;
86 virtual int32_t computeCustomPrefHeight(int32_t width = -1) const override;
87
88 virtual int32_t computeCustomMinWidth(int32_t height = -1) const override;
89 virtual int32_t computeCustomMinHeight(int32_t width = -1) const override;
90 };
91}
Allows the edges of components to be anchored to anchor pane's edges.
Definition: anchor_pane.h:38