Merge pull request #430 from Koerty/grid-partial-last-row

Partially display the last row if grid number of row isn't an integer
This commit is contained in:
John Rassa 2018-05-09 19:25:41 -04:00 committed by GitHub
commit 6469ce2cbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -66,6 +66,7 @@ private:
std::shared_ptr<TextureResource> mDefaultFolderTexture; std::shared_ptr<TextureResource> mDefaultFolderTexture;
// TILES // TILES
bool mLastRowPartial;
Vector2f mMargin; Vector2f mMargin;
Vector2f mTileSize; Vector2f mTileSize;
Vector2i mGridDimension; Vector2i mGridDimension;
@ -168,6 +169,17 @@ void ImageGridComponent<T>::render(const Transform4x4f& parentTrans)
mEntriesDirty = false; 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<GridTileComponent> selectedTile = NULL; std::shared_ptr<GridTileComponent> selectedTile = NULL;
for(auto it = mTiles.begin(); it != mTiles.end(); it++) for(auto it = mTiles.begin(); it != mTiles.end(); it++)
{ {
@ -180,6 +192,8 @@ void ImageGridComponent<T>::render(const Transform4x4f& parentTrans)
tile->render(trans); tile->render(trans);
} }
Renderer::popClipRect();
// Render the selected image on top of the others // Render the selected image on top of the others
if (selectedTile != NULL) if (selectedTile != NULL)
selectedTile->render(trans); selectedTile->render(trans);
@ -264,6 +278,10 @@ void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
// Apply size property, will trigger a call to onSizeChanged() which will build the tiles // Apply size property, will trigger a call to onSizeChanged() which will build the tiles
GuiComponent::applyTheme(theme, view, element, SIZE); GuiComponent::applyTheme(theme, view, element, SIZE);
// Trigger the call manually if the theme have no "imagegrid" element
if (!elem)
buildTiles();
} }
template<typename T> template<typename T>
@ -323,6 +341,9 @@ void ImageGridComponent<T>::buildTiles()
template<typename T> template<typename T>
void ImageGridComponent<T>::updateTiles() void ImageGridComponent<T>::updateTiles()
{ {
if (!mTiles.size())
return;
int img = getStartPosition(); int img = getStartPosition();
for(int ti = 0; ti < mTiles.size(); ti++) for(int ti = 0; ti < mTiles.size(); ti++)
@ -350,17 +371,24 @@ void ImageGridComponent<T>::updateTiles()
template<typename T> template<typename T>
int ImageGridComponent<T>::getStartPosition() const int ImageGridComponent<T>::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 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 // 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) // 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) // start = first tile of last row - nb column * (nb line - 1)
// = (E - 1) / X * X - X * (Y - 1) // = (E - 1) / X * X - X * (Y - 1)
// = X * ((E - 1) / X - Y + 1) // = X * ((E - 1) / X - Y + 1)
if(start + (mGridDimension.x() * mGridDimension.y()) >= (int)mEntries.size()) if(start + (mGridDimension.x() * (mGridDimension.y() - partialRow)) >= (int)mEntries.size())
start = mGridDimension.x() * (((int)mEntries.size() - 1) / mGridDimension.x() - mGridDimension.y() + 1); start = mGridDimension.x() * (((int)mEntries.size() - 1) / mGridDimension.x() - mGridDimension.y() + 1 + partialRow);
if(start < 0) if(start < 0)
start = 0; start = 0;
@ -376,9 +404,20 @@ void ImageGridComponent<T>::calcGridDimension()
// <=> 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 = mScrollDirection == SCROLL_VERTICALLY ? // Invert dimensions for horizontally scrolling grid
Vector2i(gridDimension.x(), gridDimension.y()) : if (mScrollDirection == SCROLL_HORIZONTALLY)
Vector2i(gridDimension.y(), gridDimension.x()); 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";
}; };