From deeaf438c7914493d55b53b36efa9cd1532b6e7d Mon Sep 17 00:00:00 2001 From: Koerty Date: Thu, 12 Apr 2018 10:25:11 +0200 Subject: [PATCH] Clean ImageGridComponent code - Separated private methods and attributes in 3 groups : - images & entries - tiles - miscellaneous - Renamed some methods : - buildImages -> buildTiles - updateImages -> updateTiles - Moved private methods implementation so they respect the same order as their declaration - Changed some methods internal variable names and other smalls clean --- es-core/src/components/ImageGridComponent.h | 128 ++++++++++---------- 1 file changed, 67 insertions(+), 61 deletions(-) diff --git a/es-core/src/components/ImageGridComponent.h b/es-core/src/components/ImageGridComponent.h index cf1b72d13..0e46f0ab0 100644 --- a/es-core/src/components/ImageGridComponent.h +++ b/es-core/src/components/ImageGridComponent.h @@ -39,48 +39,39 @@ public: ImageGridComponent(Window* window); void add(const std::string& name, const std::string& imagePath, const T& obj); - - void onSizeChanged() override; bool input(InputConfig* config, Input input) override; void update(int deltaTime) override; void render(const Transform4x4f& parentTrans) override; virtual void applyTheme(const std::shared_ptr& theme, const std::string& view, const std::string& element, unsigned int properties) override; + void onSizeChanged() override; + inline void setCursorChangedCallback(const std::function& func) { mCursorChangedCallback = func; } +protected: + virtual void onCursorChanged(const CursorState& state) override; + private: - // Calculate how much tiles of size mTileSize we can fit in a grid of size mSize using a margin of size mMargin - void calcGridDimension() - { - // GRID_SIZE = COLUMNS * TILE_SIZE + (COLUMNS - 1) * MARGIN - // <=> 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()); - }; - - int getStartPosition(); - void buildImages(); - void updateImages(); - - virtual void onCursorChanged(const CursorState& state); - - std::function mCursorChangedCallback; + // TILES + void buildTiles(); + void updateTiles(); + int getStartPosition() const; + void calcGridDimension(); + // IMAGES & ENTRIES bool mEntriesDirty; + // TILES Vector2f mMargin; Vector2f mTileSize; Vector2i mGridDimension; - + std::shared_ptr mTheme; std::vector< std::shared_ptr > mTiles; - std::shared_ptr mTheme; - + // MISCELLANEOUS ScrollDirection mScrollDirection; + std::function mCursorChangedCallback; }; template @@ -156,16 +147,17 @@ void ImageGridComponent::render(const Transform4x4f& parentTrans) if(mEntriesDirty) { - buildImages(); - updateImages(); + buildTiles(); + updateTiles(); mEntriesDirty = false; } std::shared_ptr selectedTile = NULL; 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 tile = (*it); + + // If it's the selected image, keep it for later, otherwise render it now if(tile->isSelected()) selectedTile = tile; else @@ -211,25 +203,25 @@ void ImageGridComponent::applyTheme(const std::shared_ptr& theme, calcGridDimension(); } +template +void ImageGridComponent::onSizeChanged() +{ + buildTiles(); + updateTiles(); +} + template void ImageGridComponent::onCursorChanged(const CursorState& state) { - updateImages(); + updateTiles(); if(mCursorChangedCallback) mCursorChangedCallback(state); } -template -void ImageGridComponent::onSizeChanged() -{ - buildImages(); - updateImages(); -} - // Create and position tiles (mTiles) template -void ImageGridComponent::buildImages() +void ImageGridComponent::buildTiles() { mTiles.clear(); @@ -263,9 +255,38 @@ void ImageGridComponent::buildImages() } } +template +void ImageGridComponent::updateTiles() +{ + if(mTiles.empty()) + buildTiles(); + + int img = getStartPosition(); + + for(int ti = 0; ti < mTiles.size(); ti++) + { + std::shared_ptr 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) template -int ImageGridComponent::getStartPosition() +int ImageGridComponent::getStartPosition() const { int cursorRow = mCursor / mGridDimension.x(); @@ -285,33 +306,18 @@ int ImageGridComponent::getStartPosition() 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 -void ImageGridComponent::updateImages() +void ImageGridComponent::calcGridDimension() { - if(mTiles.empty()) - buildImages(); + // GRID_SIZE = COLUMNS * TILE_SIZE + (COLUMNS - 1) * MARGIN + // <=> 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 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