From 80456f566af19d0cfbdb3da3b4e26a5b68bc62f6 Mon Sep 17 00:00:00 2001 From: Koerty Date: Wed, 9 May 2018 17:01:34 +0200 Subject: [PATCH] Partially display the last row if grid number of row isn't an integer - Also fix 2 bugs introduced by #428 - Also add log error message if grid dimensions are below 1 --- es-core/src/components/ImageGridComponent.h | 51 ++++++++++++++++++--- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/es-core/src/components/ImageGridComponent.h b/es-core/src/components/ImageGridComponent.h index 33d462c14..0e4003759 100644 --- a/es-core/src/components/ImageGridComponent.h +++ b/es-core/src/components/ImageGridComponent.h @@ -66,6 +66,7 @@ private: std::shared_ptr mDefaultFolderTexture; // TILES + bool mLastRowPartial; Vector2f mMargin; Vector2f mTileSize; Vector2i mGridDimension; @@ -168,6 +169,17 @@ void ImageGridComponent::render(const Transform4x4f& parentTrans) mEntriesDirty = false; } + // Create a clipRect to hide tiles used to buffer texture loading + float scaleX = trans.r0().x(); + float scaleY = trans.r1().y(); + + Vector2i pos((int)Math::round(trans.translation()[0]), (int)Math::round(trans.translation()[1])); + Vector2i size((int)Math::round(mSize.x() * scaleX), (int)Math::round(mSize.y() * scaleY)); + + Renderer::pushClipRect(pos, size); + + // Render all the tiles but the selected one + std::shared_ptr selectedTile = NULL; for(auto it = mTiles.begin(); it != mTiles.end(); it++) { @@ -180,6 +192,8 @@ void ImageGridComponent::render(const Transform4x4f& parentTrans) tile->render(trans); } + Renderer::popClipRect(); + // Render the selected image on top of the others if (selectedTile != NULL) selectedTile->render(trans); @@ -264,6 +278,10 @@ void ImageGridComponent::applyTheme(const std::shared_ptr& theme, // Apply size property, will trigger a call to onSizeChanged() which will build the tiles GuiComponent::applyTheme(theme, view, element, SIZE); + + // Trigger the call manually if the theme have no "imagegrid" element + if (!elem) + buildTiles(); } template @@ -323,6 +341,9 @@ void ImageGridComponent::buildTiles() template void ImageGridComponent::updateTiles() { + if (!mTiles.size()) + return; + int img = getStartPosition(); for(int ti = 0; ti < mTiles.size(); ti++) @@ -350,17 +371,24 @@ void ImageGridComponent::updateTiles() template int ImageGridComponent::getStartPosition() const { + // The "partialRow" variable exist because we want to keep the same positioning behavior in both + // case, whenever we have an integer number of rows or not (the last partial row is ignored when + // calculating position and the cursor shouldn't end up in this row when close to the end) + int partialRow = (int)mLastRowPartial; + if ((int)mEntries.size() < mGridDimension.x() * (mGridDimension.y() - (int)mLastRowPartial)) + partialRow = 0; + int cursorRow = mCursor / mGridDimension.x(); - int start = (cursorRow - (mGridDimension.y() / 2)) * mGridDimension.x(); + int start = (cursorRow - ((mGridDimension.y() - partialRow) / 2)) * mGridDimension.x(); // If we are at the end put the row as close as we can and no higher, using the following formula // Where E is the nb of entries, X the grid x dim (nb of column), Y the grid y dim (nb of line) // start = first tile of last row - nb column * (nb line - 1) // = (E - 1) / X * X - X * (Y - 1) // = X * ((E - 1) / X - Y + 1) - if(start + (mGridDimension.x() * mGridDimension.y()) >= (int)mEntries.size()) - start = mGridDimension.x() * (((int)mEntries.size() - 1) / mGridDimension.x() - mGridDimension.y() + 1); + if(start + (mGridDimension.x() * (mGridDimension.y() - partialRow)) >= (int)mEntries.size()) + start = mGridDimension.x() * (((int)mEntries.size() - 1) / mGridDimension.x() - mGridDimension.y() + 1 + partialRow); if(start < 0) start = 0; @@ -376,9 +404,20 @@ void ImageGridComponent::calcGridDimension() // <=> COLUMNS = (GRID_SIZE + MARGIN) / (TILE_SIZE + MARGIN) Vector2f gridDimension = (mSize + mMargin) / (mTileSize + mMargin); - mGridDimension = mScrollDirection == SCROLL_VERTICALLY ? - Vector2i(gridDimension.x(), gridDimension.y()) : - Vector2i(gridDimension.y(), gridDimension.x()); + // Invert dimensions for horizontally scrolling grid + if (mScrollDirection == SCROLL_HORIZONTALLY) + gridDimension = Vector2f(gridDimension.y(), gridDimension.x()); + + mLastRowPartial = Math::floorf(gridDimension.y()) != gridDimension.y(); + + // Ceil y dim so we can display partial last row + mGridDimension = Vector2i(gridDimension.x(), Math::ceilf(gridDimension.y())); + + // Grid dimension validation + if (mGridDimension.x() < 1) + LOG(LogError) << "Theme defined grid X dimension below 1"; + if (mGridDimension.y() < 1) + LOG(LogError) << "Theme defined grid Y dimension below 1"; };