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
This commit is contained in:
Koerty 2018-05-09 17:01:34 +02:00
parent ff9715dbac
commit 80456f566a

View file

@ -66,6 +66,7 @@ private:
std::shared_ptr<TextureResource> mDefaultFolderTexture;
// TILES
bool mLastRowPartial;
Vector2f mMargin;
Vector2f mTileSize;
Vector2i mGridDimension;
@ -168,6 +169,17 @@ void ImageGridComponent<T>::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<GridTileComponent> selectedTile = NULL;
for(auto it = mTiles.begin(); it != mTiles.end(); it++)
{
@ -180,6 +192,8 @@ void ImageGridComponent<T>::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<T>::applyTheme(const std::shared_ptr<ThemeData>& 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<typename T>
@ -323,6 +341,9 @@ void ImageGridComponent<T>::buildTiles()
template<typename T>
void ImageGridComponent<T>::updateTiles()
{
if (!mTiles.size())
return;
int img = getStartPosition();
for(int ti = 0; ti < mTiles.size(); ti++)
@ -350,17 +371,24 @@ void ImageGridComponent<T>::updateTiles()
template<typename T>
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 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<T>::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";
};