ES-DE/es-core/src/components/FlexboxComponent.h

102 lines
2.9 KiB
C
Raw Normal View History

// SPDX-License-Identifier: MIT
//
// EmulationStation Desktop Edition
// FlexboxComponent.h
//
// Flexbox layout component.
//
#ifndef ES_CORE_COMPONENTS_FLEXBOX_COMPONENT_H
#define ES_CORE_COMPONENTS_FLEXBOX_COMPONENT_H
#include "GuiComponent.h"
2021-10-23 17:08:32 +00:00
#include "Window.h"
#include "components/ImageComponent.h"
class FlexboxComponent : public GuiComponent
{
public:
2021-10-23 17:08:32 +00:00
struct FlexboxItem {
// Optional label, mostly a convenience for the calling class to track items.
std::string label;
// Main image that governs grid sizing and placement.
ImageComponent baseImage{nullptr};
// Optional overlay image that can be sized and positioned relative to the base image.
ImageComponent overlayImage{nullptr};
bool visible = false;
};
FlexboxComponent(Window* window, std::vector<FlexboxItem>& items);
2021-09-13 23:01:46 +00:00
// Getters/setters for the layout.
const std::string& getDirection() const { return mDirection; }
void setDirection(const std::string& direction)
{
assert(direction == "row" || direction == "column");
mDirection = direction;
}
const std::string& getAlignment() const { return mAlignment; }
void setAlignment(const std::string& value)
{
assert(value == "left" || value == "right");
mAlignment = value;
mLayoutValid = false;
}
unsigned int getLines() const { return mLines; }
void setLines(unsigned int value)
{
mLines = value;
mLayoutValid = false;
}
unsigned int getItemsPerLine() const { return mItemsPerLine; }
void setItemsPerLine(unsigned int value)
{
mItemsPerLine = value;
mLayoutValid = false;
}
const std::string& getItemPlacement() const { return mItemPlacement; }
void setItemPlacement(const std::string& value)
{
assert(value == "start" || value == "center" || value == "end" || value == "stretch");
mItemPlacement = value;
mLayoutValid = false;
}
const glm::vec2& getItemMargin() const { return mItemMargin; }
2021-10-23 17:08:32 +00:00
void setItemMargin(glm::vec2 value);
const glm::vec2& getOverlayPosition() const { return mOverlayPosition; }
2021-10-23 17:08:32 +00:00
void setOverlayPosition(glm::vec2 position) { mOverlayPosition = position; }
float getOverlaySize() const { return mOverlaySize; }
void setOverlaySize(float size) { mOverlaySize = size; }
void onSizeChanged() override { mLayoutValid = false; }
2021-09-13 23:01:46 +00:00
void render(const glm::mat4& parentTrans) override;
private:
// Calculate flexbox layout.
2021-09-13 23:01:46 +00:00
void computeLayout();
2021-10-23 17:08:32 +00:00
std::vector<FlexboxItem>& mItems;
// Layout options.
std::string mDirection;
std::string mAlignment;
unsigned int mLines;
unsigned int mItemsPerLine;
std::string mItemPlacement;
2021-09-13 23:01:46 +00:00
glm::vec2 mItemMargin;
2021-10-23 17:08:32 +00:00
glm::vec2 mOverlayPosition;
float mOverlaySize;
2021-09-23 22:05:32 +00:00
bool mLayoutValid;
};
#endif // ES_CORE_COMPONENTS_FLEXBOX_COMPONENT_H