Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
Constraint.cpp
1#include "Constraint.hpp"
2#include "RootDisplay.hpp"
3
15Constraint::Constraint(int flags, int padding, std::vector<Element*> targets) {
16 positioningFlags |= flags;
17 paddingOffset = padding;
18 this->targets = targets;
19}
20
21void Constraint::clearFlags() {
22 positioningFlags = 0;
23}
24
25void Constraint::addFlags(int flags) {
26 positioningFlags |= flags;
27}
28
29void Constraint::clearTargets() {
30 targets.clear();
31}
32
33void Constraint::addTarget(Element* target) {
34 targets.push_back(target);
35}
36
37void Constraint::apply(Element* element) {
38 // if the vector of targets is blank, grab the parent
39 // TODO: anything with the vector, to extract a target from it
40 auto target = element->parent;
41 int posX = 0, posY = 0;
42 int width = RootDisplay::screenWidth, height = RootDisplay::screenHeight; // default to screen size
43 if (target != NULL) {
44 posX = target->x;
45 posY = target->y;
46 width = target->width;
47 height = target->height;
48 }
49
50 // look at the flags and decide what to do
51 if (positioningFlags & ALIGN_LEFT) element->x = posX + paddingOffset;
52 if (positioningFlags & ALIGN_RIGHT) element->x = posX + width - element->width - paddingOffset;
53 if (positioningFlags & ALIGN_TOP) element->y = posY + paddingOffset;
54 if (positioningFlags & ALIGN_BOTTOM) element->y = posY + height - element->height - paddingOffset;
55
56 if (positioningFlags & ALIGN_CENTER_HORIZONTAL) element->x = posX + width / 2 - element->width / 2;
57 if (positioningFlags & ALIGN_CENTER_VERTICAL) element->y = posY + height / 2 - element->height / 2;
58
59 // some manual offset constraints, that just move the element
60 if (positioningFlags & OFFSET_LEFT) element->x += paddingOffset;
61 if (positioningFlags & OFFSET_RIGHT) element->x -= paddingOffset;
62 if (positioningFlags & OFFSET_TOP) element->y += paddingOffset;
63 if (positioningFlags & OFFSET_BOTTOM) element->y -= paddingOffset;
64
65}
Constraint(int flags, int padding=0, std::vector< Element * > targets={})
Definition: Constraint.cpp:15
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:118
Element * parent
the parent element (can sometimes be null if it isn't set)
Definition: Element.hpp:102