1#include "Constraint.hpp"
2#include "RootDisplay.hpp"
18 positioningFlags |= flags;
19 paddingOffset = padding;
20 this->targets = targets;
23void Constraint::clearFlags() {
27void Constraint::addFlags(
int flags) {
28 positioningFlags |= flags;
31void Constraint::clearTargets() {
35void Constraint::addTarget(Element* target) {
36 targets.push_back(target);
39void Constraint::apply(Element* element) {
41 auto target = targets.empty() ? element->parent : targets[0];
42 auto isSpecificTarget = !targets.empty();
43 int posX = 0, posY = 0;
44 int width = RootDisplay::screenWidth, height = RootDisplay::screenHeight;
46 float effectiveScale = element->getEffectiveScale();
48 auto paddingOffset = this->paddingOffset;
55 width = target->width;
56 height = target->height;
58 if (isSpecificTarget) {
60 paddingOffset = -paddingOffset;
63 if (positioningFlags & ALIGN_TOP) posY -= (int)(element->height * effectiveScale);
64 if (positioningFlags & ALIGN_LEFT) posX -= (int)(element->width * effectiveScale);
66 if (positioningFlags & ALIGN_BOTTOM) posY += (int)(target->height * effectiveScale);
67 if (positioningFlags & ALIGN_RIGHT) posX += (int)(target->width * effectiveScale);
72 int scaledPadding = (int)(paddingOffset * effectiveScale);
75 if (positioningFlags & ALIGN_LEFT) element->x = posX + scaledPadding;
76 if (positioningFlags & ALIGN_RIGHT) element->x = posX + width - (int)(element->width * effectiveScale) - scaledPadding;
77 if (positioningFlags & ALIGN_TOP) element->y = posY + scaledPadding;
78 if (positioningFlags & ALIGN_BOTTOM) element->y = posY + height - (int)(element->height * effectiveScale) - scaledPadding;
81 if (positioningFlags & ALIGN_CENTER_HORIZONTAL) element->x = width / 2 - (int)(element->width * effectiveScale) / 2;
82 if (positioningFlags & ALIGN_CENTER_VERTICAL) element->y = height / 2 - (int)(element->height * effectiveScale) / 2;
85 if (positioningFlags & OFFSET_LEFT) element->x += paddingOffset;
86 if (positioningFlags & OFFSET_RIGHT) element->x -= paddingOffset;
87 if (positioningFlags & OFFSET_TOP) element->y += paddingOffset;
88 if (positioningFlags & OFFSET_BOTTOM) element->y -= paddingOffset;
Constraint(int flags, int padding=0, std::vector< Element * > targets={})