Add scroll direction to the grid

Vertical by default, but can be set to horizontal by the theme
This commit is contained in:
Koerty 2018-04-09 00:58:30 +02:00
parent 9adb0d0c51
commit f946801006
3 changed files with 36 additions and 9 deletions

View file

@ -578,6 +578,8 @@ Can be created as an extra.
* `size` - type: NORMALIZED_PAIR. * `size` - type: NORMALIZED_PAIR.
- The size of the grid. Take care the selected tile can go out of the grid size, so don't position the grid too close to another element or the screen border. - The size of the grid. Take care the selected tile can go out of the grid size, so don't position the grid too close to another element or the screen border.
* `margin` - type: NORMALIZED_PAIR. * `margin` - type: NORMALIZED_PAIR.
* `scrollDirection` - type: STRING.
- `vertical` by default, can also be set to `horizontal`. Not that in `horizontal` mod, the tiles are ordered from top to bottom, then from left to right.
#### gridtile #### gridtile

View file

@ -28,7 +28,8 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>> The
{ "imagegrid", { { "imagegrid", {
{ "pos", NORMALIZED_PAIR }, { "pos", NORMALIZED_PAIR },
{ "size", NORMALIZED_PAIR }, { "size", NORMALIZED_PAIR },
{ "margin", NORMALIZED_PAIR } } }, { "margin", NORMALIZED_PAIR },
{ "scrollDirection", STRING } } },
{ "gridtile", { { "gridtile", {
{ "size", NORMALIZED_PAIR }, { "size", NORMALIZED_PAIR },
{ "padding", NORMALIZED_PAIR }, { "padding", NORMALIZED_PAIR },

View file

@ -6,6 +6,12 @@
#include "resources/TextureResource.h" #include "resources/TextureResource.h"
#include "GridTileComponent.h" #include "GridTileComponent.h"
enum ScrollDirection
{
SCROLL_VERTICALLY,
SCROLL_HORIZONTALLY
};
struct ImageGridData struct ImageGridData
{ {
std::shared_ptr<TextureResource> texture; std::shared_ptr<TextureResource> texture;
@ -51,7 +57,9 @@ private:
// <=> COLUMNS = (GRID_SIZE + MARGIN) / (TILE_SIZE + MARGIN) // <=> COLUMNS = (GRID_SIZE + MARGIN) / (TILE_SIZE + MARGIN)
Vector2f gridDimension = (mSize + mMargin) / (mTileSize + mMargin); Vector2f gridDimension = (mSize + mMargin) / (mTileSize + mMargin);
mGridDimension = Vector2i(gridDimension.x(), gridDimension.y()); mGridDimension = mScrollDirection == SCROLL_VERTICALLY ?
Vector2i(gridDimension.x(), gridDimension.y()) :
Vector2i(gridDimension.y(), gridDimension.x());
}; };
int getStartPosition(); int getStartPosition();
@ -71,6 +79,8 @@ private:
std::vector< std::shared_ptr<GridTileComponent> > mTiles; std::vector< std::shared_ptr<GridTileComponent> > mTiles;
std::shared_ptr<ThemeData> mTheme; std::shared_ptr<ThemeData> mTheme;
ScrollDirection mScrollDirection;
}; };
template<typename T> template<typename T>
@ -85,6 +95,8 @@ ImageGridComponent<T>::ImageGridComponent(Window* window) : IList<ImageGridData,
mTileSize = GridTileComponent::getDefaultTileSize(); mTileSize = GridTileComponent::getDefaultTileSize();
calcGridDimension(); calcGridDimension();
mScrollDirection = SCROLL_VERTICALLY;
} }
template<typename T> template<typename T>
@ -105,13 +117,13 @@ bool ImageGridComponent<T>::input(InputConfig* config, Input input)
{ {
Vector2i dir = Vector2i::Zero(); Vector2i dir = Vector2i::Zero();
if(config->isMappedTo("up", input)) if(config->isMappedTo("up", input))
dir[1] = -1; dir[1 ^ mScrollDirection] = -1;
else if(config->isMappedTo("down", input)) else if(config->isMappedTo("down", input))
dir[1] = 1; dir[1 ^ mScrollDirection] = 1;
else if(config->isMappedTo("left", input)) else if(config->isMappedTo("left", input))
dir[0] = -1; dir[0 ^ mScrollDirection] = -1;
else if(config->isMappedTo("right", input)) else if(config->isMappedTo("right", input))
dir[0] = 1; dir[0 ^ mScrollDirection] = 1;
if(dir != Vector2i::Zero()) if(dir != Vector2i::Zero())
{ {
@ -178,10 +190,15 @@ void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
Vector2f screen = Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight()); Vector2f screen = Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "imagegrid"); const ThemeData::ThemeElement* elem = theme->getElement(view, element, "imagegrid");
if (elem)
if (elem && elem->has("margin")) {
if (elem->has("margin"))
mMargin = elem->get<Vector2f>("margin") * screen; mMargin = elem->get<Vector2f>("margin") * screen;
if (elem->has("scrollDirection"))
mScrollDirection = (ScrollDirection)(elem->get<std::string>("scrollDirection") == "horizontal");
}
// We still need to manually get the grid tile size here, // We still need to manually get the grid tile size here,
// so we can recalculate the new grid dimension, and THEN (re)build the tiles // so we can recalculate the new grid dimension, and THEN (re)build the tiles
elem = theme->getElement(view, "default", "gridtile"); elem = theme->getElement(view, "default", "gridtile");
@ -219,6 +236,8 @@ void ImageGridComponent<T>::buildImages()
Vector2f startPosition = mTileSize / 2; Vector2f startPosition = mTileSize / 2;
Vector2f tileDistance = mTileSize + mMargin; Vector2f tileDistance = mTileSize + mMargin;
int X, Y;
// Layout tile size and position // Layout tile size and position
for(int y = 0; y < mGridDimension.y(); y++) for(int y = 0; y < mGridDimension.y(); y++)
{ {
@ -227,7 +246,12 @@ void ImageGridComponent<T>::buildImages()
// Create tiles // Create tiles
auto tile = std::make_shared<GridTileComponent>(mWindow); auto tile = std::make_shared<GridTileComponent>(mWindow);
tile->setPosition(x * tileDistance.x() + startPosition.x(), y * tileDistance.y() + startPosition.y()); // In Vertical mod, tiles are ordered from left to right, then from top to bottom
// In Horizontal mod, tiles are ordered from top to bottom, then from left to right
X = mScrollDirection == SCROLL_VERTICALLY ? x : y;
Y = mScrollDirection == SCROLL_VERTICALLY ? y : x;
tile->setPosition(X * tileDistance.x() + startPosition.x(), Y * tileDistance.y() + startPosition.y());
tile->setOrigin(0.5f, 0.5f); tile->setOrigin(0.5f, 0.5f);
tile->setImage(""); tile->setImage("");