gravity.ui.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <planet/ui/gravity.hpp>


namespace {
    std::pair<float, float>
            spacing(float left,
                    float right,
                    float const width,
                    bool const at_left,
                    bool const at_right) {
        if (at_left) {
            if (at_right) {
                auto const padding = (right - left - width) / 2;
                left += padding;
                right -= padding;
            } else {
                right = left + width;
            }
        } else if (at_right) {
            left = right - width;
        }
        return {left, right};
    }
}


planet::affine::rectangle2d planet::ui::within(
        gravity const g,
        affine::rectangle2d const &o,
        affine::extents2d const &i) {
    auto [left, right] =
            spacing(o.top_left.x(), o.top_left.x() + o.extents.width, i.width,
                    g bitand gravity::left, g bitand gravity::right);
    auto [top, bottom] =
            spacing(o.top_left.y(), o.top_left.y() + o.extents.height, i.height,
                    g bitand gravity::top, g bitand gravity::bottom);
    return {{left, top}, affine::point2d{right, bottom}};
}