Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
Public Member Functions | Public Attributes | List of all members
Constraint Class Reference
Collaboration diagram for Constraint:
Collaboration graph
[legend]

Public Member Functions

 Constraint (int flags, int padding=0, std::vector< Element * > targets={})
 
void clearFlags ()
 
void addFlags (int flags)
 
void clearTargets ()
 
void addTarget (Element *target)
 
void update ()
 
void apply (Element *element)
 

Public Attributes

int positioningFlags = 0
 
std::vector< Element * > targets
 
int paddingOffset = 0
 

Detailed Description

Definition at line 24 of file Constraint.hpp.

Constructor & Destructor Documentation

◆ Constraint()

Constraint::Constraint ( int  flags,
int  padding = 0,
std::vector< Element * >  targets = {} 
)

Constraints are used to define the positioning of an element relative to other elements. Currently only within another element or padding from a parent edge is supported. TODO: support for positioning relative to non-overlapping elements (eg. the space between)

If no targets are specified, the constraint will be relative to the parent element.

Parameters
flagsthe positioning flags to use
targetsthe elements to position relative to

Definition at line 15 of file Constraint.cpp.

15 {
16 positioningFlags |= flags;
17 paddingOffset = padding;
18 this->targets = targets;
19}

Member Function Documentation

◆ addFlags()

void Constraint::addFlags ( int  flags)

Definition at line 25 of file Constraint.cpp.

25 {
26 positioningFlags |= flags;
27}

◆ addTarget()

void Constraint::addTarget ( Element target)

Definition at line 33 of file Constraint.cpp.

33 {
34 targets.push_back(target);
35}

◆ apply()

void Constraint::apply ( Element element)

Definition at line 37 of file Constraint.cpp.

37 {
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}
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

◆ clearFlags()

void Constraint::clearFlags ( )

Definition at line 21 of file Constraint.cpp.

21 {
22 positioningFlags = 0;
23}

◆ clearTargets()

void Constraint::clearTargets ( )

Definition at line 29 of file Constraint.cpp.

29 {
30 targets.clear();
31}

Member Data Documentation

◆ paddingOffset

int Constraint::paddingOffset = 0

Definition at line 42 of file Constraint.hpp.

◆ positioningFlags

int Constraint::positioningFlags = 0

Definition at line 36 of file Constraint.hpp.

◆ targets

std::vector<Element*> Constraint::targets

Definition at line 39 of file Constraint.hpp.


The documentation for this class was generated from the following files: