2013-12-01 01:04:46 +00:00
|
|
|
#pragma once
|
2017-10-31 17:12:50 +00:00
|
|
|
#ifndef ES_CORE_COMPONENTS_IMAGE_GRID_COMPONENT_H
|
|
|
|
#define ES_CORE_COMPONENTS_IMAGE_GRID_COMPONENT_H
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-04-15 12:29:02 +00:00
|
|
|
#include "Log.h"
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/IList.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "resources/TextureResource.h"
|
2018-04-07 19:23:10 +00:00
|
|
|
#include "GridTileComponent.h"
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-04-08 22:58:30 +00:00
|
|
|
enum ScrollDirection
|
|
|
|
{
|
|
|
|
SCROLL_VERTICALLY,
|
|
|
|
SCROLL_HORIZONTALLY
|
|
|
|
};
|
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
struct ImageGridData
|
|
|
|
{
|
2018-06-08 09:29:52 +00:00
|
|
|
std::string texturePath;
|
2014-02-08 03:45:28 +00:00
|
|
|
};
|
|
|
|
|
2013-12-01 01:04:46 +00:00
|
|
|
template<typename T>
|
2014-02-13 23:10:28 +00:00
|
|
|
class ImageGridComponent : public IList<ImageGridData, T>
|
2013-12-01 01:04:46 +00:00
|
|
|
{
|
2014-02-17 17:40:31 +00:00
|
|
|
protected:
|
|
|
|
using IList<ImageGridData, T>::mEntries;
|
2018-04-15 13:20:49 +00:00
|
|
|
using IList<ImageGridData, T>::mScrollTier;
|
2014-02-17 17:40:31 +00:00
|
|
|
using IList<ImageGridData, T>::listUpdate;
|
|
|
|
using IList<ImageGridData, T>::listInput;
|
|
|
|
using IList<ImageGridData, T>::listRenderTitleOverlay;
|
|
|
|
using IList<ImageGridData, T>::getTransform;
|
|
|
|
using IList<ImageGridData, T>::mSize;
|
|
|
|
using IList<ImageGridData, T>::mCursor;
|
|
|
|
using IList<ImageGridData, T>::Entry;
|
|
|
|
using IList<ImageGridData, T>::mWindow;
|
|
|
|
|
2013-12-01 01:04:46 +00:00
|
|
|
public:
|
2014-02-17 17:40:31 +00:00
|
|
|
using IList<ImageGridData, T>::size;
|
|
|
|
using IList<ImageGridData, T>::isScrolling;
|
|
|
|
using IList<ImageGridData, T>::stopScrolling;
|
|
|
|
|
2013-12-01 01:04:46 +00:00
|
|
|
ImageGridComponent(Window* window);
|
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
void add(const std::string& name, const std::string& imagePath, const T& obj);
|
2013-12-01 01:04:46 +00:00
|
|
|
|
|
|
|
bool input(InputConfig* config, Input input) override;
|
|
|
|
void update(int deltaTime) override;
|
2017-10-28 20:24:35 +00:00
|
|
|
void render(const Transform4x4f& parentTrans) override;
|
2018-03-31 13:59:14 +00:00
|
|
|
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) override;
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-04-12 08:25:11 +00:00
|
|
|
void onSizeChanged() override;
|
|
|
|
inline void setCursorChangedCallback(const std::function<void(CursorState state)>& func) { mCursorChangedCallback = func; }
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-04-12 08:25:11 +00:00
|
|
|
protected:
|
|
|
|
virtual void onCursorChanged(const CursorState& state) override;
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-04-12 08:25:11 +00:00
|
|
|
private:
|
|
|
|
// TILES
|
|
|
|
void buildTiles();
|
|
|
|
void updateTiles();
|
2018-04-15 13:20:49 +00:00
|
|
|
void updateTileAtPos(int tilePos, int imgPos, int bufferTop, int bufferBot);
|
2018-04-12 08:25:11 +00:00
|
|
|
int getStartPosition() const;
|
|
|
|
void calcGridDimension();
|
2018-03-22 12:52:13 +00:00
|
|
|
|
2018-04-12 08:25:11 +00:00
|
|
|
// IMAGES & ENTRIES
|
2018-04-15 13:20:49 +00:00
|
|
|
const int texBuffersBehind[4] = { 1, 1, 1, 1 };
|
|
|
|
const int texBuffersForward[4] = { 1, 2, 3, 3 };
|
2013-12-01 01:04:46 +00:00
|
|
|
bool mEntriesDirty;
|
2018-04-15 13:20:49 +00:00
|
|
|
int mLastCursor;
|
2018-06-08 09:29:52 +00:00
|
|
|
std::string mDefaultGameTexture;
|
|
|
|
std::string mDefaultFolderTexture;
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-04-12 08:25:11 +00:00
|
|
|
// TILES
|
2018-05-09 15:01:34 +00:00
|
|
|
bool mLastRowPartial;
|
2018-03-22 07:03:12 +00:00
|
|
|
Vector2f mMargin;
|
2018-04-07 19:23:10 +00:00
|
|
|
Vector2f mTileSize;
|
|
|
|
Vector2i mGridDimension;
|
|
|
|
std::shared_ptr<ThemeData> mTheme;
|
2018-04-12 08:25:11 +00:00
|
|
|
std::vector< std::shared_ptr<GridTileComponent> > mTiles;
|
2018-04-08 22:58:30 +00:00
|
|
|
|
2018-04-12 08:25:11 +00:00
|
|
|
// MISCELLANEOUS
|
2018-04-08 22:58:30 +00:00
|
|
|
ScrollDirection mScrollDirection;
|
2018-04-12 08:25:11 +00:00
|
|
|
std::function<void(CursorState state)> mCursorChangedCallback;
|
2013-12-01 01:04:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2014-02-17 17:40:31 +00:00
|
|
|
ImageGridComponent<T>::ImageGridComponent(Window* window) : IList<ImageGridData, T>(window)
|
2013-12-01 01:04:46 +00:00
|
|
|
{
|
2018-03-22 07:03:12 +00:00
|
|
|
Vector2f screen = Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
|
|
|
|
|
2013-12-01 01:04:46 +00:00
|
|
|
mEntriesDirty = true;
|
2018-04-15 13:20:49 +00:00
|
|
|
mLastCursor = 0;
|
2018-07-25 13:19:41 +00:00
|
|
|
mDefaultGameTexture = ":/cartridge.svg";
|
2018-07-25 12:52:13 +00:00
|
|
|
mDefaultFolderTexture = ":/folder.svg";
|
2018-03-22 07:03:12 +00:00
|
|
|
|
2018-04-07 19:23:10 +00:00
|
|
|
mSize = screen * 0.80f;
|
|
|
|
mMargin = screen * 0.07f;
|
|
|
|
mTileSize = GridTileComponent::getDefaultTileSize();
|
|
|
|
|
2018-04-08 22:58:30 +00:00
|
|
|
mScrollDirection = SCROLL_VERTICALLY;
|
2013-12-01 01:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2014-02-08 03:45:28 +00:00
|
|
|
void ImageGridComponent<T>::add(const std::string& name, const std::string& imagePath, const T& obj)
|
2013-12-01 01:04:46 +00:00
|
|
|
{
|
2014-02-17 17:40:31 +00:00
|
|
|
typename IList<ImageGridData, T>::Entry entry;
|
2014-02-08 03:45:28 +00:00
|
|
|
entry.name = name;
|
|
|
|
entry.object = obj;
|
2018-06-08 09:29:52 +00:00
|
|
|
entry.data.texturePath = imagePath;
|
2018-04-15 12:29:02 +00:00
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
static_cast<IList< ImageGridData, T >*>(this)->add(entry);
|
2013-12-01 01:04:46 +00:00
|
|
|
mEntriesDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool ImageGridComponent<T>::input(InputConfig* config, Input input)
|
|
|
|
{
|
|
|
|
if(input.value != 0)
|
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2i dir = Vector2i::Zero();
|
2013-12-01 01:04:46 +00:00
|
|
|
if(config->isMappedTo("up", input))
|
2018-04-08 22:58:30 +00:00
|
|
|
dir[1 ^ mScrollDirection] = -1;
|
2013-12-01 01:04:46 +00:00
|
|
|
else if(config->isMappedTo("down", input))
|
2018-04-08 22:58:30 +00:00
|
|
|
dir[1 ^ mScrollDirection] = 1;
|
2013-12-01 01:04:46 +00:00
|
|
|
else if(config->isMappedTo("left", input))
|
2018-04-08 22:58:30 +00:00
|
|
|
dir[0 ^ mScrollDirection] = -1;
|
2013-12-01 01:04:46 +00:00
|
|
|
else if(config->isMappedTo("right", input))
|
2018-04-08 22:58:30 +00:00
|
|
|
dir[0 ^ mScrollDirection] = 1;
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
if(dir != Vector2i::Zero())
|
2013-12-01 01:04:46 +00:00
|
|
|
{
|
2018-04-07 19:23:10 +00:00
|
|
|
listInput(dir.x() + dir.y() * mGridDimension.x());
|
2013-12-01 01:04:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(config->isMappedTo("up", input) || config->isMappedTo("down", input) || config->isMappedTo("left", input) || config->isMappedTo("right", input))
|
|
|
|
{
|
2014-02-08 02:15:48 +00:00
|
|
|
stopScrolling();
|
2013-12-01 01:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GuiComponent::input(config, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void ImageGridComponent<T>::update(int deltaTime)
|
|
|
|
{
|
2014-02-08 02:15:48 +00:00
|
|
|
listUpdate(deltaTime);
|
2018-04-07 19:23:10 +00:00
|
|
|
|
|
|
|
for(auto it = mTiles.begin(); it != mTiles.end(); it++)
|
|
|
|
(*it)->update();
|
2013-12-01 01:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2017-10-28 20:24:35 +00:00
|
|
|
void ImageGridComponent<T>::render(const Transform4x4f& parentTrans)
|
2013-12-01 01:04:46 +00:00
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
Transform4x4f trans = getTransform() * parentTrans;
|
2013-12-01 01:04:46 +00:00
|
|
|
|
|
|
|
if(mEntriesDirty)
|
|
|
|
{
|
2018-04-12 08:25:11 +00:00
|
|
|
updateTiles();
|
2013-12-01 01:04:46 +00:00
|
|
|
mEntriesDirty = false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 15:01:34 +00:00
|
|
|
// 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
|
2018-04-07 19:23:10 +00:00
|
|
|
std::shared_ptr<GridTileComponent> selectedTile = NULL;
|
|
|
|
for(auto it = mTiles.begin(); it != mTiles.end(); it++)
|
2013-12-01 01:04:46 +00:00
|
|
|
{
|
2018-04-07 19:23:10 +00:00
|
|
|
std::shared_ptr<GridTileComponent> tile = (*it);
|
2018-04-12 08:25:11 +00:00
|
|
|
|
|
|
|
// If it's the selected image, keep it for later, otherwise render it now
|
2018-04-07 19:23:10 +00:00
|
|
|
if(tile->isSelected())
|
|
|
|
selectedTile = tile;
|
2018-03-22 07:03:12 +00:00
|
|
|
else
|
2018-04-07 19:23:10 +00:00
|
|
|
tile->render(trans);
|
2013-12-01 01:04:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-09 15:01:34 +00:00
|
|
|
Renderer::popClipRect();
|
|
|
|
|
2018-03-22 07:03:12 +00:00
|
|
|
// Render the selected image on top of the others
|
2018-04-07 19:23:10 +00:00
|
|
|
if (selectedTile != NULL)
|
|
|
|
selectedTile->render(trans);
|
2018-03-22 07:03:12 +00:00
|
|
|
|
2018-04-15 13:20:49 +00:00
|
|
|
listRenderTitleOverlay(trans);
|
|
|
|
|
2014-02-17 17:40:31 +00:00
|
|
|
GuiComponent::renderChildren(trans);
|
2013-12-01 01:04:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 13:59:14 +00:00
|
|
|
template<typename T>
|
|
|
|
void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties)
|
|
|
|
{
|
2018-04-30 18:59:05 +00:00
|
|
|
// Apply theme to GuiComponent but not size property, which will be applied at the end of this function
|
2018-05-19 01:59:44 +00:00
|
|
|
GuiComponent::applyTheme(theme, view, element, properties ^ ThemeFlags::SIZE);
|
2018-04-07 19:23:10 +00:00
|
|
|
|
|
|
|
// Keep the theme pointer to apply it on the tiles later on
|
|
|
|
mTheme = theme;
|
|
|
|
|
|
|
|
Vector2f screen = Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
|
|
|
|
|
|
|
|
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "imagegrid");
|
2018-04-08 22:58:30 +00:00
|
|
|
if (elem)
|
|
|
|
{
|
|
|
|
if (elem->has("margin"))
|
|
|
|
mMargin = elem->get<Vector2f>("margin") * screen;
|
2018-04-07 19:23:10 +00:00
|
|
|
|
2018-04-08 22:58:30 +00:00
|
|
|
if (elem->has("scrollDirection"))
|
|
|
|
mScrollDirection = (ScrollDirection)(elem->get<std::string>("scrollDirection") == "horizontal");
|
2018-04-15 12:29:02 +00:00
|
|
|
|
|
|
|
if (elem->has("gameImage"))
|
|
|
|
{
|
|
|
|
std::string path = elem->get<std::string>("gameImage");
|
|
|
|
|
|
|
|
if (!ResourceManager::getInstance()->fileExists(path))
|
|
|
|
LOG(LogWarning) << "Could not replace default game image, check path: " << path;
|
|
|
|
else
|
|
|
|
{
|
2018-06-08 09:29:52 +00:00
|
|
|
std::string oldDefaultGameTexture = mDefaultGameTexture;
|
|
|
|
mDefaultGameTexture = path;
|
2018-04-15 12:29:02 +00:00
|
|
|
|
|
|
|
// mEntries are already loaded at this point,
|
|
|
|
// so we need to update them with new game image texture
|
|
|
|
for (auto it = mEntries.begin(); it != mEntries.end(); it++)
|
|
|
|
{
|
2018-06-08 09:29:52 +00:00
|
|
|
if ((*it).data.texturePath == oldDefaultGameTexture)
|
|
|
|
(*it).data.texturePath = mDefaultGameTexture;
|
2018-04-15 12:29:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elem->has("folderImage"))
|
|
|
|
{
|
|
|
|
std::string path = elem->get<std::string>("folderImage");
|
|
|
|
|
|
|
|
if (!ResourceManager::getInstance()->fileExists(path))
|
|
|
|
LOG(LogWarning) << "Could not replace default folder image, check path: " << path;
|
|
|
|
else
|
|
|
|
{
|
2018-06-08 09:29:52 +00:00
|
|
|
std::string oldDefaultFolderTexture = mDefaultFolderTexture;
|
|
|
|
mDefaultFolderTexture = path;
|
2018-04-15 12:29:02 +00:00
|
|
|
|
|
|
|
// mEntries are already loaded at this point,
|
|
|
|
// so we need to update them with new folder image texture
|
|
|
|
for (auto it = mEntries.begin(); it != mEntries.end(); it++)
|
|
|
|
{
|
2018-06-08 09:29:52 +00:00
|
|
|
if ((*it).data.texturePath == oldDefaultFolderTexture)
|
|
|
|
(*it).data.texturePath = mDefaultFolderTexture;
|
2018-04-15 12:29:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-08 22:58:30 +00:00
|
|
|
}
|
2018-04-07 19:23:10 +00:00
|
|
|
|
|
|
|
// We still need to manually get the grid tile size here,
|
|
|
|
// so we can recalculate the new grid dimension, and THEN (re)build the tiles
|
|
|
|
elem = theme->getElement(view, "default", "gridtile");
|
|
|
|
|
|
|
|
mTileSize = elem && elem->has("size") ?
|
|
|
|
elem->get<Vector2f>("size") * screen :
|
|
|
|
GridTileComponent::getDefaultTileSize();
|
|
|
|
|
2018-04-30 18:59:05 +00:00
|
|
|
// Apply size property, will trigger a call to onSizeChanged() which will build the tiles
|
2018-05-19 01:59:44 +00:00
|
|
|
GuiComponent::applyTheme(theme, view, element, ThemeFlags::SIZE);
|
2018-05-09 15:01:34 +00:00
|
|
|
|
|
|
|
// Trigger the call manually if the theme have no "imagegrid" element
|
|
|
|
if (!elem)
|
|
|
|
buildTiles();
|
2018-03-31 13:59:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-01 01:04:46 +00:00
|
|
|
template<typename T>
|
2018-04-12 08:25:11 +00:00
|
|
|
void ImageGridComponent<T>::onSizeChanged()
|
2013-12-01 01:04:46 +00:00
|
|
|
{
|
2018-04-12 08:25:11 +00:00
|
|
|
buildTiles();
|
|
|
|
updateTiles();
|
2013-12-01 01:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2018-04-12 08:25:11 +00:00
|
|
|
void ImageGridComponent<T>::onCursorChanged(const CursorState& state)
|
2013-12-01 01:04:46 +00:00
|
|
|
{
|
2018-04-12 08:25:11 +00:00
|
|
|
updateTiles();
|
|
|
|
|
|
|
|
if(mCursorChangedCallback)
|
|
|
|
mCursorChangedCallback(state);
|
2013-12-01 01:04:46 +00:00
|
|
|
}
|
|
|
|
|
2018-04-07 19:23:10 +00:00
|
|
|
// Create and position tiles (mTiles)
|
2013-12-01 01:04:46 +00:00
|
|
|
template<typename T>
|
2018-04-12 08:25:11 +00:00
|
|
|
void ImageGridComponent<T>::buildTiles()
|
2013-12-01 01:04:46 +00:00
|
|
|
{
|
2018-04-07 19:23:10 +00:00
|
|
|
mTiles.clear();
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-04-30 18:59:05 +00:00
|
|
|
calcGridDimension();
|
|
|
|
|
2018-04-07 19:23:10 +00:00
|
|
|
Vector2f tileDistance = mTileSize + mMargin;
|
2018-06-02 22:57:02 +00:00
|
|
|
Vector2f bufferSize = Vector2f(mScrollDirection == SCROLL_HORIZONTALLY ? tileDistance.x() * texBuffersForward[3] : 0,
|
|
|
|
mScrollDirection == SCROLL_VERTICALLY ? tileDistance.y() * texBuffersForward[3] : 0);
|
|
|
|
Vector2f startPosition = mTileSize / 2 - bufferSize;
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-04-08 22:58:30 +00:00
|
|
|
int X, Y;
|
|
|
|
|
2018-03-22 07:03:12 +00:00
|
|
|
// Layout tile size and position
|
2018-04-07 19:23:10 +00:00
|
|
|
for(int y = 0; y < mGridDimension.y(); y++)
|
2013-12-01 01:04:46 +00:00
|
|
|
{
|
2018-04-07 19:23:10 +00:00
|
|
|
for(int x = 0; x < mGridDimension.x(); x++)
|
2013-12-01 01:04:46 +00:00
|
|
|
{
|
2018-03-22 07:03:12 +00:00
|
|
|
// Create tiles
|
2018-04-07 19:23:10 +00:00
|
|
|
auto tile = std::make_shared<GridTileComponent>(mWindow);
|
|
|
|
|
2018-04-08 22:58:30 +00:00
|
|
|
// In Vertical mod, tiles are ordered from left to right, then from top to bottom
|
|
|
|
// In Horizontal mod, tiles are ordered from top to bottom, then from left to right
|
|
|
|
X = mScrollDirection == SCROLL_VERTICALLY ? x : y;
|
|
|
|
Y = mScrollDirection == SCROLL_VERTICALLY ? y : x;
|
|
|
|
|
|
|
|
tile->setPosition(X * tileDistance.x() + startPosition.x(), Y * tileDistance.y() + startPosition.y());
|
2018-04-07 19:23:10 +00:00
|
|
|
tile->setOrigin(0.5f, 0.5f);
|
|
|
|
tile->setImage("");
|
|
|
|
|
|
|
|
if (mTheme)
|
|
|
|
tile->applyTheme(mTheme, "grid", "gridtile", ThemeFlags::ALL);
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-04-07 19:23:10 +00:00
|
|
|
mTiles.push_back(tile);
|
2013-12-01 01:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 08:25:11 +00:00
|
|
|
template<typename T>
|
|
|
|
void ImageGridComponent<T>::updateTiles()
|
|
|
|
{
|
2018-05-09 15:01:34 +00:00
|
|
|
if (!mTiles.size())
|
|
|
|
return;
|
|
|
|
|
2018-04-15 13:20:49 +00:00
|
|
|
// Stop updating the tiles at highest scroll speed
|
|
|
|
if (mScrollTier == 3)
|
2018-04-12 08:25:11 +00:00
|
|
|
{
|
2018-04-15 13:20:49 +00:00
|
|
|
for (int ti = 0; ti < mTiles.size(); ti++)
|
2018-04-12 08:25:11 +00:00
|
|
|
{
|
2018-04-15 13:20:49 +00:00
|
|
|
std::shared_ptr<GridTileComponent> tile = mTiles.at(ti);
|
|
|
|
|
2018-04-12 08:25:11 +00:00
|
|
|
tile->setSelected(false);
|
2018-04-15 13:20:49 +00:00
|
|
|
tile->setImage(mDefaultGameTexture);
|
2018-04-12 08:25:11 +00:00
|
|
|
tile->setVisible(false);
|
|
|
|
}
|
2018-04-15 13:20:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-12 08:25:11 +00:00
|
|
|
|
2018-04-15 13:20:49 +00:00
|
|
|
// 1 if scrolling down, -1 if scrolling up
|
|
|
|
int scrollDirection = mCursor >= mLastCursor ? 1 : -1;
|
|
|
|
|
|
|
|
// If going down, update from top to bottom
|
|
|
|
// If going up, update from bottom to top
|
|
|
|
int ti = scrollDirection == 1 ? 0 : mTiles.size() - 1;
|
|
|
|
int end = scrollDirection == 1 ? mTiles.size() : -1;
|
|
|
|
|
|
|
|
int img = getStartPosition();
|
|
|
|
if (scrollDirection == -1)
|
|
|
|
img += mTiles.size() - 1;
|
|
|
|
|
|
|
|
// Calculate buffer size depending on scroll speed and direction
|
|
|
|
int bufferBehind = (texBuffersForward[3] - texBuffersBehind[mScrollTier]) * mGridDimension.x();
|
|
|
|
int bufferForward = (texBuffersForward[3] - texBuffersForward[mScrollTier]) * mGridDimension.x();
|
|
|
|
|
|
|
|
int bufferTop = scrollDirection == 1 ? bufferBehind : bufferForward;
|
|
|
|
int bufferBot = scrollDirection == 1 ? bufferForward : bufferBehind;
|
|
|
|
|
|
|
|
// Update the tiles
|
|
|
|
while (ti != end)
|
|
|
|
{
|
|
|
|
updateTileAtPos(ti, img, bufferTop, bufferBot);
|
2018-04-12 08:25:11 +00:00
|
|
|
|
2018-04-15 13:20:49 +00:00
|
|
|
ti += scrollDirection;
|
|
|
|
img += scrollDirection;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLastCursor = mCursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void ImageGridComponent<T>::updateTileAtPos(int tilePos, int imgPos, int bufferTop, int bufferBot)
|
|
|
|
{
|
|
|
|
std::shared_ptr<GridTileComponent> tile = mTiles.at(tilePos);
|
|
|
|
|
|
|
|
// If we have more tiles than we have to display images on screen, hide them
|
|
|
|
if(imgPos < 0 || imgPos >= size()
|
|
|
|
|| tilePos < bufferTop || tilePos >= mTiles.size() - bufferBot) // Same for tiles out of the buffer
|
|
|
|
{
|
|
|
|
tile->setSelected(false);
|
|
|
|
tile->setImage("");
|
|
|
|
tile->setVisible(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tile->setSelected(imgPos == mCursor);
|
|
|
|
tile->setVisible(true);
|
2018-06-08 09:29:52 +00:00
|
|
|
|
|
|
|
std::string imagePath = mEntries.at(imgPos).data.texturePath;
|
|
|
|
if (ResourceManager::getInstance()->fileExists(imagePath))
|
|
|
|
{
|
|
|
|
tile->setImage(imagePath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// FileType::FOLDER = 2, but FileData is our template parameter T,
|
|
|
|
// so we don't want to bring that dependence to FileData here
|
|
|
|
if (mEntries.at(imgPos).object->getType() == 2)
|
|
|
|
tile->setImage(mDefaultFolderTexture);
|
|
|
|
else
|
|
|
|
tile->setImage(mDefaultGameTexture);
|
|
|
|
}
|
2018-04-12 08:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 19:23:10 +00:00
|
|
|
// Return the starting position (the number of the game which will be displayed on top left of the screen)
|
2013-12-01 01:04:46 +00:00
|
|
|
template<typename T>
|
2018-04-12 08:25:11 +00:00
|
|
|
int ImageGridComponent<T>::getStartPosition() const
|
2013-12-01 01:04:46 +00:00
|
|
|
{
|
2018-05-09 15:01:34 +00:00
|
|
|
// 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;
|
|
|
|
|
2018-04-07 19:23:10 +00:00
|
|
|
int cursorRow = mCursor / mGridDimension.x();
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-05-09 15:01:34 +00:00
|
|
|
int start = (cursorRow - ((mGridDimension.y() - partialRow) / 2)) * mGridDimension.x();
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-04-15 13:20:49 +00:00
|
|
|
// Number of tiles which are just used as a buffer for texture loading
|
|
|
|
int bufferSize = texBuffersForward[3] * mGridDimension.x();
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-04-15 13:20:49 +00:00
|
|
|
if(start + (mGridDimension.x() * (mGridDimension.y() - partialRow)) >= (int)mEntries.size() + bufferSize)
|
|
|
|
{
|
|
|
|
// 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)
|
|
|
|
start = mGridDimension.x() * (((int)mEntries.size() - 1) / mGridDimension.x() - mGridDimension.y() + 1 + partialRow) + bufferSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(start < -bufferSize)
|
|
|
|
{
|
|
|
|
start = -bufferSize;
|
|
|
|
}
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-04-07 19:23:10 +00:00
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2018-04-12 08:25:11 +00:00
|
|
|
// Calculate how much tiles of size mTileSize we can fit in a grid of size mSize using a margin of size mMargin
|
2018-04-07 19:23:10 +00:00
|
|
|
template<typename T>
|
2018-04-12 08:25:11 +00:00
|
|
|
void ImageGridComponent<T>::calcGridDimension()
|
2018-04-07 19:23:10 +00:00
|
|
|
{
|
2018-04-12 08:25:11 +00:00
|
|
|
// GRID_SIZE = COLUMNS * TILE_SIZE + (COLUMNS - 1) * MARGIN
|
|
|
|
// <=> COLUMNS = (GRID_SIZE + MARGIN) / (TILE_SIZE + MARGIN)
|
|
|
|
Vector2f gridDimension = (mSize + mMargin) / (mTileSize + mMargin);
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2018-05-09 15:01:34 +00:00
|
|
|
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()));
|
|
|
|
|
2018-06-02 22:57:02 +00:00
|
|
|
// Invert dimensions for horizontally scrolling grid
|
|
|
|
if (mScrollDirection == SCROLL_HORIZONTALLY)
|
|
|
|
mGridDimension = Vector2i(mGridDimension.y(), mGridDimension.x());
|
|
|
|
|
2018-05-09 15:01:34 +00:00
|
|
|
// 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";
|
2018-04-15 13:20:49 +00:00
|
|
|
|
|
|
|
// Add extra tiles to both side depending on max texture buffer
|
|
|
|
mGridDimension.y() += texBuffersForward[3] * 2;
|
2018-04-12 08:25:11 +00:00
|
|
|
};
|
2013-12-01 01:04:46 +00:00
|
|
|
|
2017-10-31 17:12:50 +00:00
|
|
|
|
|
|
|
#endif // ES_CORE_COMPONENTS_IMAGE_GRID_COMPONENT_H
|