mirror of
https://github.com/RetroDECK/ES-DE.git
synced 2025-01-17 22:55:38 +00:00
Merge pull request #411 from Koerty/grid-clean-imagegrid-code
Clean ImageGridComponent code
This commit is contained in:
commit
7c396541d4
|
@ -40,47 +40,38 @@ public:
|
||||||
|
|
||||||
void add(const std::string& name, const std::string& imagePath, const T& obj);
|
void add(const std::string& name, const std::string& imagePath, const T& obj);
|
||||||
|
|
||||||
void onSizeChanged() override;
|
|
||||||
|
|
||||||
bool input(InputConfig* config, Input input) override;
|
bool input(InputConfig* config, Input input) override;
|
||||||
void update(int deltaTime) override;
|
void update(int deltaTime) override;
|
||||||
void render(const Transform4x4f& parentTrans) override;
|
void render(const Transform4x4f& parentTrans) override;
|
||||||
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) override;
|
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) override;
|
||||||
|
|
||||||
|
void onSizeChanged() override;
|
||||||
|
|
||||||
inline void setCursorChangedCallback(const std::function<void(CursorState state)>& func) { mCursorChangedCallback = func; }
|
inline void setCursorChangedCallback(const std::function<void(CursorState state)>& func) { mCursorChangedCallback = func; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void onCursorChanged(const CursorState& state) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Calculate how much tiles of size mTileSize we can fit in a grid of size mSize using a margin of size mMargin
|
// TILES
|
||||||
void calcGridDimension()
|
void buildTiles();
|
||||||
{
|
void updateTiles();
|
||||||
// GRID_SIZE = COLUMNS * TILE_SIZE + (COLUMNS - 1) * MARGIN
|
int getStartPosition() const;
|
||||||
// <=> COLUMNS = (GRID_SIZE + MARGIN) / (TILE_SIZE + MARGIN)
|
void calcGridDimension();
|
||||||
Vector2f gridDimension = (mSize + mMargin) / (mTileSize + mMargin);
|
|
||||||
|
|
||||||
mGridDimension = mScrollDirection == SCROLL_VERTICALLY ?
|
|
||||||
Vector2i(gridDimension.x(), gridDimension.y()) :
|
|
||||||
Vector2i(gridDimension.y(), gridDimension.x());
|
|
||||||
};
|
|
||||||
|
|
||||||
int getStartPosition();
|
|
||||||
void buildImages();
|
|
||||||
void updateImages();
|
|
||||||
|
|
||||||
virtual void onCursorChanged(const CursorState& state);
|
|
||||||
|
|
||||||
std::function<void(CursorState state)> mCursorChangedCallback;
|
|
||||||
|
|
||||||
|
// IMAGES & ENTRIES
|
||||||
bool mEntriesDirty;
|
bool mEntriesDirty;
|
||||||
|
|
||||||
|
// TILES
|
||||||
Vector2f mMargin;
|
Vector2f mMargin;
|
||||||
Vector2f mTileSize;
|
Vector2f mTileSize;
|
||||||
Vector2i mGridDimension;
|
Vector2i mGridDimension;
|
||||||
|
std::shared_ptr<ThemeData> mTheme;
|
||||||
std::vector< std::shared_ptr<GridTileComponent> > mTiles;
|
std::vector< std::shared_ptr<GridTileComponent> > mTiles;
|
||||||
|
|
||||||
std::shared_ptr<ThemeData> mTheme;
|
// MISCELLANEOUS
|
||||||
|
|
||||||
ScrollDirection mScrollDirection;
|
ScrollDirection mScrollDirection;
|
||||||
|
std::function<void(CursorState state)> mCursorChangedCallback;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -156,16 +147,17 @@ void ImageGridComponent<T>::render(const Transform4x4f& parentTrans)
|
||||||
|
|
||||||
if(mEntriesDirty)
|
if(mEntriesDirty)
|
||||||
{
|
{
|
||||||
buildImages();
|
buildTiles();
|
||||||
updateImages();
|
updateTiles();
|
||||||
mEntriesDirty = false;
|
mEntriesDirty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
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++)
|
||||||
{
|
{
|
||||||
// If it's the selected image, keep it for later, otherwise render it now
|
|
||||||
std::shared_ptr<GridTileComponent> tile = (*it);
|
std::shared_ptr<GridTileComponent> tile = (*it);
|
||||||
|
|
||||||
|
// If it's the selected image, keep it for later, otherwise render it now
|
||||||
if(tile->isSelected())
|
if(tile->isSelected())
|
||||||
selectedTile = tile;
|
selectedTile = tile;
|
||||||
else
|
else
|
||||||
|
@ -211,25 +203,25 @@ void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
|
||||||
calcGridDimension();
|
calcGridDimension();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void ImageGridComponent<T>::onSizeChanged()
|
||||||
|
{
|
||||||
|
buildTiles();
|
||||||
|
updateTiles();
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void ImageGridComponent<T>::onCursorChanged(const CursorState& state)
|
void ImageGridComponent<T>::onCursorChanged(const CursorState& state)
|
||||||
{
|
{
|
||||||
updateImages();
|
updateTiles();
|
||||||
|
|
||||||
if(mCursorChangedCallback)
|
if(mCursorChangedCallback)
|
||||||
mCursorChangedCallback(state);
|
mCursorChangedCallback(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void ImageGridComponent<T>::onSizeChanged()
|
|
||||||
{
|
|
||||||
buildImages();
|
|
||||||
updateImages();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create and position tiles (mTiles)
|
// Create and position tiles (mTiles)
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void ImageGridComponent<T>::buildImages()
|
void ImageGridComponent<T>::buildTiles()
|
||||||
{
|
{
|
||||||
mTiles.clear();
|
mTiles.clear();
|
||||||
|
|
||||||
|
@ -263,9 +255,38 @@ void ImageGridComponent<T>::buildImages()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void ImageGridComponent<T>::updateTiles()
|
||||||
|
{
|
||||||
|
if(mTiles.empty())
|
||||||
|
buildTiles();
|
||||||
|
|
||||||
|
int img = getStartPosition();
|
||||||
|
|
||||||
|
for(int ti = 0; ti < mTiles.size(); ti++)
|
||||||
|
{
|
||||||
|
std::shared_ptr<GridTileComponent> tile = mTiles.at(ti);
|
||||||
|
|
||||||
|
// If we have more tiles than we have to display images on screen, hide them
|
||||||
|
if(img >= size())
|
||||||
|
{
|
||||||
|
tile->setSelected(false);
|
||||||
|
tile->setImage("");
|
||||||
|
tile->setVisible(false);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
tile->setSelected(img == mCursor);
|
||||||
|
tile->setImage(mEntries.at(img).data.texture);
|
||||||
|
tile->setVisible(true);
|
||||||
|
|
||||||
|
img++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Return the starting position (the number of the game which will be displayed on top left of the screen)
|
// Return the starting position (the number of the game which will be displayed on top left of the screen)
|
||||||
template<typename T>
|
template<typename T>
|
||||||
int ImageGridComponent<T>::getStartPosition()
|
int ImageGridComponent<T>::getStartPosition() const
|
||||||
{
|
{
|
||||||
int cursorRow = mCursor / mGridDimension.x();
|
int cursorRow = mCursor / mGridDimension.x();
|
||||||
|
|
||||||
|
@ -285,33 +306,18 @@ int ImageGridComponent<T>::getStartPosition()
|
||||||
return start;
|
return start;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculate how much tiles of size mTileSize we can fit in a grid of size mSize using a margin of size mMargin
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void ImageGridComponent<T>::updateImages()
|
void ImageGridComponent<T>::calcGridDimension()
|
||||||
{
|
{
|
||||||
if(mTiles.empty())
|
// GRID_SIZE = COLUMNS * TILE_SIZE + (COLUMNS - 1) * MARGIN
|
||||||
buildImages();
|
// <=> COLUMNS = (GRID_SIZE + MARGIN) / (TILE_SIZE + MARGIN)
|
||||||
|
Vector2f gridDimension = (mSize + mMargin) / (mTileSize + mMargin);
|
||||||
|
|
||||||
int pos = getStartPosition();
|
mGridDimension = mScrollDirection == SCROLL_VERTICALLY ?
|
||||||
|
Vector2i(gridDimension.x(), gridDimension.y()) :
|
||||||
|
Vector2i(gridDimension.y(), gridDimension.x());
|
||||||
|
};
|
||||||
|
|
||||||
for(int img = 0; img < mTiles.size(); img++)
|
|
||||||
{
|
|
||||||
std::shared_ptr<GridTileComponent> tile = mTiles.at(img);
|
|
||||||
|
|
||||||
// If we have more tiles than we have to display images on screen, hide them
|
|
||||||
if(pos >= size())
|
|
||||||
{
|
|
||||||
tile->setSelected(false);
|
|
||||||
tile->setImage("");
|
|
||||||
tile->setVisible(false);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
tile->setSelected(pos == mCursor);
|
|
||||||
tile->setImage(mEntries.at(pos).data.texture);
|
|
||||||
tile->setVisible(true);
|
|
||||||
|
|
||||||
pos++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // ES_CORE_COMPONENTS_IMAGE_GRID_COMPONENT_H
|
#endif // ES_CORE_COMPONENTS_IMAGE_GRID_COMPONENT_H
|
||||||
|
|
Loading…
Reference in a new issue