From f9468010067463b3e1239f341defce5d905312a6 Mon Sep 17 00:00:00 2001 From: Koerty Date: Mon, 9 Apr 2018 00:58:30 +0200 Subject: [PATCH] Add scroll direction to the grid Vertical by default, but can be set to horizontal by the theme --- THEMES.md | 2 ++ es-core/src/ThemeData.cpp | 3 +- es-core/src/components/ImageGridComponent.h | 40 ++++++++++++++++----- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/THEMES.md b/THEMES.md index edc0ab310..f44baaaf1 100644 --- a/THEMES.md +++ b/THEMES.md @@ -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 diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index 6b39f3a35..49052fe2a 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -28,7 +28,8 @@ std::map> The { "imagegrid", { { "pos", NORMALIZED_PAIR }, { "size", NORMALIZED_PAIR }, - { "margin", NORMALIZED_PAIR } } }, + { "margin", NORMALIZED_PAIR }, + { "scrollDirection", STRING } } }, { "gridtile", { { "size", NORMALIZED_PAIR }, { "padding", NORMALIZED_PAIR }, diff --git a/es-core/src/components/ImageGridComponent.h b/es-core/src/components/ImageGridComponent.h index 68029eeec..cf1b72d13 100644 --- a/es-core/src/components/ImageGridComponent.h +++ b/es-core/src/components/ImageGridComponent.h @@ -6,6 +6,12 @@ #include "resources/TextureResource.h" #include "GridTileComponent.h" +enum ScrollDirection +{ + SCROLL_VERTICALLY, + SCROLL_HORIZONTALLY +}; + struct ImageGridData { std::shared_ptr texture; @@ -51,7 +57,9 @@ private: // <=> 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(); @@ -71,6 +79,8 @@ private: std::vector< std::shared_ptr > mTiles; std::shared_ptr mTheme; + + ScrollDirection mScrollDirection; }; template @@ -85,6 +95,8 @@ ImageGridComponent::ImageGridComponent(Window* window) : IList @@ -105,13 +117,13 @@ bool ImageGridComponent::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()) { @@ -178,9 +190,14 @@ void ImageGridComponent::applyTheme(const std::shared_ptr& 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("margin") * screen; - if (elem && elem->has("margin")) - mMargin = elem->get("margin") * screen; + if (elem->has("scrollDirection")) + mScrollDirection = (ScrollDirection)(elem->get("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 @@ -219,6 +236,8 @@ void ImageGridComponent::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++) { @@ -227,7 +246,12 @@ void ImageGridComponent::buildImages() // Create tiles auto tile = std::make_shared(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("");