Skip to content

Commit

Permalink
Add scroll direction to the grid
Browse files Browse the repository at this point in the history
Vertical by default, but can be set to horizontal by the theme
  • Loading branch information
benjdero committed Apr 13, 2018
1 parent 9adb0d0 commit f946801
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
2 changes: 2 additions & 0 deletions THEMES.md
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@ Can be created as an extra.
* `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.
* `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
Expand Down
3 changes: 2 additions & 1 deletion es-core/src/ThemeData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>> The
{ "imagegrid", {
{ "pos", NORMALIZED_PAIR },
{ "size", NORMALIZED_PAIR },
{ "margin", NORMALIZED_PAIR } } },
{ "margin", NORMALIZED_PAIR },
{ "scrollDirection", STRING } } },
{ "gridtile", {
{ "size", NORMALIZED_PAIR },
{ "padding", NORMALIZED_PAIR },
Expand Down
40 changes: 32 additions & 8 deletions es-core/src/components/ImageGridComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include "resources/TextureResource.h"
#include "GridTileComponent.h"

enum ScrollDirection
{
SCROLL_VERTICALLY,
SCROLL_HORIZONTALLY
};

struct ImageGridData
{
std::shared_ptr<TextureResource> texture;
Expand Down Expand Up @@ -51,7 +57,9 @@ class ImageGridComponent : public IList<ImageGridData, T>
// <=> COLUMNS = (GRID_SIZE + MARGIN) / (TILE_SIZE + MARGIN)
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();
Expand All @@ -71,6 +79,8 @@ class ImageGridComponent : public IList<ImageGridData, T>
std::vector< std::shared_ptr<GridTileComponent> > mTiles;

std::shared_ptr<ThemeData> mTheme;

ScrollDirection mScrollDirection;
};

template<typename T>
Expand All @@ -85,6 +95,8 @@ ImageGridComponent<T>::ImageGridComponent(Window* window) : IList<ImageGridData,
mTileSize = GridTileComponent::getDefaultTileSize();

calcGridDimension();

mScrollDirection = SCROLL_VERTICALLY;
}

template<typename T>
Expand All @@ -105,13 +117,13 @@ bool ImageGridComponent<T>::input(InputConfig* config, Input input)
{
Vector2i dir = Vector2i::Zero();
if(config->isMappedTo("up", input))
dir[1] = -1;
dir[1 ^ mScrollDirection] = -1;
else if(config->isMappedTo("down", input))
dir[1] = 1;
dir[1 ^ mScrollDirection] = 1;
else if(config->isMappedTo("left", input))
dir[0] = -1;
dir[0 ^ mScrollDirection] = -1;
else if(config->isMappedTo("right", input))
dir[0] = 1;
dir[0 ^ mScrollDirection] = 1;

if(dir != Vector2i::Zero())
{
Expand Down Expand Up @@ -178,9 +190,14 @@ void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
Vector2f screen = Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());

const ThemeData::ThemeElement* elem = theme->getElement(view, element, "imagegrid");
if (elem)
{
if (elem->has("margin"))
mMargin = elem->get<Vector2f>("margin") * screen;

if (elem && elem->has("margin"))
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,
// so we can recalculate the new grid dimension, and THEN (re)build the tiles
Expand Down Expand Up @@ -219,6 +236,8 @@ void ImageGridComponent<T>::buildImages()
Vector2f startPosition = mTileSize / 2;
Vector2f tileDistance = mTileSize + mMargin;

int X, Y;

// Layout tile size and position
for(int y = 0; y < mGridDimension.y(); y++)
{
Expand All @@ -227,7 +246,12 @@ void ImageGridComponent<T>::buildImages()
// Create tiles
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->setImage("");

Expand Down

0 comments on commit f946801

Please sign in to comment.