make direction and align an enum.

more in line defaults in constructor.
getter/setter in header

Signed-off-by: Sophia Hadash <sophiahadash@gmail.com>
This commit is contained in:
shadash 2021-10-02 21:58:04 +02:00 committed by Sophia Hadash
parent 9d23d124d4
commit 519644f66c
2 changed files with 55 additions and 73 deletions

View file

@ -10,7 +10,6 @@
#include "components/FlexboxComponent.h"
#include <numeric>
#include <utility>
#include "Settings.h"
#include "ThemeData.h"
#include "resources/TextureResource.h"
@ -20,47 +19,12 @@ FlexboxComponent::FlexboxComponent(Window* window)
, mDirection(DEFAULT_DIRECTION)
, mAlign(DEFAULT_ALIGN)
, mItemsPerLine(DEFAULT_ITEMS_PER_LINE)
, mItemMargin({DEFAULT_MARGIN_X, DEFAULT_MARGIN_Y})
, mItemWidth(DEFAULT_ITEM_SIZE_X)
, mLayoutValid(false)
{
// Initialize item margins.
mItemMargin = glm::vec2{DEFAULT_MARGIN_X, DEFAULT_MARGIN_Y};
// Layout validity
mLayoutValid = false;
}
// Getters/Setters for rendering options.
void FlexboxComponent::setDirection(std::string value)
{
mDirection = std::move(value);
mLayoutValid = false;
}
std::string FlexboxComponent::getDirection() { return mDirection; }
void FlexboxComponent::setAlign(std::string value)
{
mAlign = std::move(value);
mLayoutValid = false;
}
std::string FlexboxComponent::getAlign() { return mAlign; }
void FlexboxComponent::setItemsPerLine(unsigned int value)
{
mItemsPerLine = value;
mLayoutValid = false;
}
unsigned int FlexboxComponent::getItemsPerLine() { return mItemsPerLine; }
void FlexboxComponent::setItemMargin(glm::vec2 value)
{
mItemMargin = value;
mLayoutValid = false;
}
glm::vec2 FlexboxComponent::getItemMargin() { return mItemMargin; }
void FlexboxComponent::setItemWidth(float value)
{
mItemWidth = value;
mLayoutValid = false;
}
float FlexboxComponent::getItemWidth() { return mItemWidth; }
void FlexboxComponent::onSizeChanged() { mLayoutValid = false; }
void FlexboxComponent::computeLayout()
@ -76,7 +40,7 @@ void FlexboxComponent::computeLayout()
glm::ivec2 directionRow = {0, 1};
// Change direction.
if (mDirection == DIRECTION_COLUMN) {
if (mDirection == Direction::row) {
directionLine = {0, 1};
directionRow = {1, 0};
}
@ -97,14 +61,14 @@ void FlexboxComponent::computeLayout()
int n = mChildren.size();
int nLines =
std::max(1, static_cast<int>(std::ceil(n / std::max(1, static_cast<int>(mItemsPerLine)))));
float lineWidth =
(mDirection == "row" ? (maxItemSize.y + mItemMargin.y) : (maxItemSize.x + mItemMargin.x));
float lineWidth = (mDirection == Direction::row ? (maxItemSize.y + mItemMargin.y) :
(maxItemSize.x + mItemMargin.x));
float anchorXStart = anchorX;
float anchorYStart = anchorY;
// Compute total container size.
glm::vec2 totalSize = {-mItemMargin.x, -mItemMargin.y};
if (mDirection == "row") {
if (mDirection == Direction::row) {
totalSize.x += (mItemMargin.x + mItemWidth) * mItemsPerLine;
totalSize.y += (mItemMargin.y + maxItemSize.y) * nLines;
}
@ -123,15 +87,15 @@ void FlexboxComponent::computeLayout()
float y = anchorY - anchorOriginY * size.y;
// Apply alignment
if (mAlign == ITEM_ALIGN_END) {
if (mAlign == Align::end) {
x += directionLine.x == 0 ? (maxItemSize.x - size.x) : 0;
y += directionLine.y == 0 ? (maxItemSize.y - size.y) : 0;
}
else if (mAlign == ITEM_ALIGN_CENTER) {
else if (mAlign == Align::center) {
x += directionLine.x == 0 ? (maxItemSize.x - size.x) / 2 : 0;
y += directionLine.y == 0 ? (maxItemSize.y - size.y) / 2 : 0;
}
else if (mAlign == ITEM_ALIGN_STRETCH && mDirection == "row") {
else if (mAlign == Align::stretch && mDirection == Direction::row) {
child->setSize(child->getSize().x, maxItemSize.y);
}
@ -194,10 +158,15 @@ void FlexboxComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
return;
if (properties & DIRECTION && elem->has("direction"))
mDirection = elem->get<std::string>("direction");
mDirection =
elem->get<std::string>("direction") == "row" ? Direction::row : Direction::column;
if (elem->has("align"))
mAlign = elem->get<std::string>("align");
if (elem->has("align")) {
const auto a = elem->get<std::string>("align");
mAlign = (a == "start" ?
Align::start :
(a == "end" ? Align::end : (a == "center" ? Align::center : Align::stretch)));
}
if (elem->has("itemsPerLine"))
mItemsPerLine = elem->get<float>("itemsPerLine");

View file

@ -13,40 +13,53 @@
#include "GuiComponent.h"
#include "renderers/Renderer.h"
// Definitions for the option values.
#define DIRECTION_ROW "row"
#define DIRECTION_COLUMN "column"
#define ITEM_ALIGN_START "start"
#define ITEM_ALIGN_END "end"
#define ITEM_ALIGN_CENTER "center"
#define ITEM_ALIGN_STRETCH "stretch"
// Default values.
#define DEFAULT_DIRECTION DIRECTION_ROW
#define DEFAULT_ALIGN ITEM_ALIGN_CENTER
#define DEFAULT_DIRECTION Direction::row
#define DEFAULT_ALIGN Align::center
#define DEFAULT_ITEMS_PER_LINE 4
#define DEFAULT_MARGIN_X 10.0f
#define DEFAULT_MARGIN_Y 10.0f
#define DEFAULT_ITEM_SIZE_X 64.0f
class TextureResource;
class FlexboxComponent : public GuiComponent
{
public:
FlexboxComponent(Window* window);
enum class Direction : char { row, column };
enum class Align : char { start, end, center, stretch };
explicit FlexboxComponent(Window* window);
// Getters/Setters for rendering options.
void setDirection(std::string value);
std::string getDirection();
void setAlign(std::string value);
std::string getAlign();
void setItemsPerLine(unsigned int value);
unsigned int getItemsPerLine();
void setItemMargin(glm::vec2 value);
glm::vec2 getItemMargin();
void setItemWidth(float value);
float getItemWidth();
[[nodiscard]] Direction getDirection() const { return mDirection; };
void setDirection(Direction value)
{
mDirection = value;
mLayoutValid = false;
};
[[nodiscard]] Align getAlign() const { return mAlign; };
void setAlign(Align value)
{
mAlign = value;
mLayoutValid = false;
};
[[nodiscard]] unsigned int getItemsPerLine() const { return mItemsPerLine; };
void setItemsPerLine(unsigned int value)
{
mItemsPerLine = value;
mLayoutValid = false;
};
[[nodiscard]] glm::vec2 getItemMargin() const { return mItemMargin; };
void setItemMargin(glm::vec2 value)
{
mItemMargin = value;
mLayoutValid = false;
};
[[nodiscard]] float getItemWidth() const { return mItemWidth; };
void setItemWidth(float value)
{
mItemWidth = value;
mLayoutValid = false;
};
void onSizeChanged() override;
void render(const glm::mat4& parentTrans) override;
@ -61,8 +74,8 @@ private:
void computeLayout();
// Rendering options.
std::string mDirection;
std::string mAlign;
Direction mDirection;
Align mAlign;
unsigned int mItemsPerLine;
glm::vec2 mItemMargin;
float mItemWidth;