2018-04-07 19:23:10 +00:00
|
|
|
#include "GridTileComponent.h"
|
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
#include "animations/LambdaAnimation.h"
|
2018-04-07 19:23:10 +00:00
|
|
|
#include "resources/TextureResource.h"
|
|
|
|
#include "ThemeData.h"
|
|
|
|
|
|
|
|
GridTileComponent::GridTileComponent(Window* window) : GuiComponent(window), mBackground(window)
|
|
|
|
{
|
|
|
|
mDefaultProperties.mSize = getDefaultTileSize();
|
|
|
|
mDefaultProperties.mPadding = Vector2f(16.0f, 16.0f);
|
|
|
|
mDefaultProperties.mImageColor = 0xAAAAAABB;
|
2020-06-21 17:35:43 +00:00
|
|
|
mDefaultProperties.mBackgroundImage = ":/graphics/frame.png";
|
2018-05-10 20:26:29 +00:00
|
|
|
mDefaultProperties.mBackgroundCornerSize = Vector2f(16 ,16);
|
|
|
|
mDefaultProperties.mBackgroundCenterColor = 0xAAAAEEFF;
|
|
|
|
mDefaultProperties.mBackgroundEdgeColor = 0xAAAAEEFF;
|
2018-04-07 19:23:10 +00:00
|
|
|
|
|
|
|
mSelectedProperties.mSize = getSelectedTileSize();
|
|
|
|
mSelectedProperties.mPadding = mDefaultProperties.mPadding;
|
|
|
|
mSelectedProperties.mImageColor = 0xFFFFFFFF;
|
2018-05-10 20:26:29 +00:00
|
|
|
mSelectedProperties.mBackgroundImage = mDefaultProperties.mBackgroundImage;
|
|
|
|
mSelectedProperties.mBackgroundCornerSize = mDefaultProperties.mBackgroundCornerSize;
|
|
|
|
mSelectedProperties.mBackgroundCenterColor = 0xFFFFFFFF;
|
|
|
|
mSelectedProperties.mBackgroundEdgeColor = 0xFFFFFFFF;
|
2018-04-07 19:23:10 +00:00
|
|
|
|
|
|
|
mImage = std::make_shared<ImageComponent>(mWindow);
|
|
|
|
mImage->setOrigin(0.5f, 0.5f);
|
|
|
|
|
|
|
|
mBackground.setOrigin(0.5f, 0.5f);
|
|
|
|
|
|
|
|
addChild(&mBackground);
|
|
|
|
addChild(&(*mImage));
|
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
mSelectedZoomPercent = 0;
|
|
|
|
|
|
|
|
setSelected(false, false);
|
2018-04-07 19:23:10 +00:00
|
|
|
setVisible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GridTileComponent::render(const Transform4x4f& parentTrans)
|
|
|
|
{
|
|
|
|
Transform4x4f trans = getTransform() * parentTrans;
|
|
|
|
|
|
|
|
if (mVisible)
|
|
|
|
renderChildren(trans);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update all the tile properties to the new status (selected or default)
|
2019-07-06 14:50:50 +00:00
|
|
|
void GridTileComponent::update(int deltaTime)
|
2018-04-07 19:23:10 +00:00
|
|
|
{
|
2019-07-06 14:50:50 +00:00
|
|
|
GuiComponent::update(deltaTime);
|
|
|
|
|
|
|
|
calcCurrentProperties();
|
2018-04-07 19:23:10 +00:00
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
mBackground.setImagePath(mCurrentProperties.mBackgroundImage);
|
2018-04-07 19:23:10 +00:00
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
mImage->setColorShift(mCurrentProperties.mImageColor);
|
|
|
|
mBackground.setCenterColor(mCurrentProperties.mBackgroundCenterColor);
|
|
|
|
mBackground.setEdgeColor(mCurrentProperties.mBackgroundEdgeColor);
|
2018-04-07 19:23:10 +00:00
|
|
|
|
|
|
|
resize();
|
|
|
|
}
|
|
|
|
|
2019-10-08 02:25:39 +00:00
|
|
|
void applyThemeToProperties(const ThemeData::ThemeElement* elem, GridTileProperties* properties)
|
2018-04-07 19:23:10 +00:00
|
|
|
{
|
|
|
|
Vector2f screen = Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
|
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
if (elem->has("size"))
|
2019-10-08 02:25:39 +00:00
|
|
|
properties->mSize = elem->get<Vector2f>("size") * screen;
|
2018-04-07 19:23:10 +00:00
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
if (elem->has("padding"))
|
2019-10-08 02:25:39 +00:00
|
|
|
properties->mPadding = elem->get<Vector2f>("padding");
|
2018-04-07 19:23:10 +00:00
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
if (elem->has("imageColor"))
|
2019-10-08 02:25:39 +00:00
|
|
|
properties->mImageColor = elem->get<unsigned int>("imageColor");
|
2018-05-10 20:26:29 +00:00
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
if (elem->has("backgroundImage"))
|
2019-10-08 02:25:39 +00:00
|
|
|
properties->mBackgroundImage = elem->get<std::string>("backgroundImage");
|
2018-04-07 19:23:10 +00:00
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
if (elem->has("backgroundCornerSize"))
|
2019-10-08 02:25:39 +00:00
|
|
|
properties->mBackgroundCornerSize = elem->get<Vector2f>("backgroundCornerSize");
|
2018-04-07 19:23:10 +00:00
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
if (elem->has("backgroundColor"))
|
|
|
|
{
|
2019-10-08 02:25:39 +00:00
|
|
|
properties->mBackgroundCenterColor = elem->get<unsigned int>("backgroundColor");
|
|
|
|
properties->mBackgroundEdgeColor = elem->get<unsigned int>("backgroundColor");
|
2019-07-06 14:50:50 +00:00
|
|
|
}
|
2018-05-10 20:26:29 +00:00
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
if (elem->has("backgroundCenterColor"))
|
2019-10-08 02:25:39 +00:00
|
|
|
properties->mBackgroundCenterColor = elem->get<unsigned int>("backgroundCenterColor");
|
2018-05-10 20:26:29 +00:00
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
if (elem->has("backgroundEdgeColor"))
|
2019-10-08 02:25:39 +00:00
|
|
|
properties->mBackgroundEdgeColor = elem->get<unsigned int>("backgroundEdgeColor");
|
2019-07-06 14:50:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GridTileComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& /*element*/, unsigned int /*properties*/)
|
|
|
|
{
|
|
|
|
Vector2f screen = Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
|
|
|
|
|
|
|
|
// Apply theme to the default gridtile
|
|
|
|
const ThemeData::ThemeElement* elem = theme->getElement(view, "default", "gridtile");
|
|
|
|
if (elem)
|
2019-10-08 02:25:39 +00:00
|
|
|
applyThemeToProperties(elem, &mDefaultProperties);
|
2018-04-07 19:23:10 +00:00
|
|
|
|
2018-05-10 20:26:29 +00:00
|
|
|
// Apply theme to the selected gridtile
|
|
|
|
// NOTE that some of the default gridtile properties influence on the selected gridtile properties
|
|
|
|
// See THEMES.md for more informations
|
2018-04-07 19:23:10 +00:00
|
|
|
elem = theme->getElement(view, "selected", "gridtile");
|
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
mSelectedProperties.mSize = getSelectedTileSize();
|
|
|
|
mSelectedProperties.mPadding = mDefaultProperties.mPadding;
|
|
|
|
mSelectedProperties.mBackgroundImage = mDefaultProperties.mBackgroundImage;
|
|
|
|
mSelectedProperties.mBackgroundCornerSize = mDefaultProperties.mBackgroundCornerSize;
|
2018-05-10 20:26:29 +00:00
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
if (elem)
|
2019-10-08 02:25:39 +00:00
|
|
|
applyThemeToProperties(elem, &mSelectedProperties);
|
2018-04-07 19:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Made this a static function because the ImageGridComponent need to know the default tile size
|
|
|
|
// to calculate the grid dimension before it instantiate the GridTileComponents
|
|
|
|
Vector2f GridTileComponent::getDefaultTileSize()
|
|
|
|
{
|
|
|
|
Vector2f screen = Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
|
|
|
|
|
|
|
|
return screen * 0.22f;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2f GridTileComponent::getSelectedTileSize() const
|
|
|
|
{
|
|
|
|
return mDefaultProperties.mSize * 1.2f;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GridTileComponent::isSelected() const
|
|
|
|
{
|
|
|
|
return mSelected;
|
|
|
|
}
|
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
void GridTileComponent::reset()
|
|
|
|
{
|
|
|
|
setImage("");
|
|
|
|
}
|
|
|
|
|
2018-04-07 19:23:10 +00:00
|
|
|
void GridTileComponent::setImage(const std::string& path)
|
|
|
|
{
|
|
|
|
mImage->setImage(path);
|
2018-06-08 09:29:52 +00:00
|
|
|
|
|
|
|
// Resize now to prevent flickering images when scrolling
|
|
|
|
resize();
|
2018-04-07 19:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GridTileComponent::setImage(const std::shared_ptr<TextureResource>& texture)
|
|
|
|
{
|
|
|
|
mImage->setImage(texture);
|
|
|
|
|
|
|
|
// Resize now to prevent flickering images when scrolling
|
|
|
|
resize();
|
|
|
|
}
|
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
void GridTileComponent::setSelected(bool selected, bool allowAnimation, Vector3f* pPosition, bool force)
|
2018-04-07 19:23:10 +00:00
|
|
|
{
|
2019-07-06 14:50:50 +00:00
|
|
|
if (mSelected == selected && !force)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-07 19:23:10 +00:00
|
|
|
mSelected = selected;
|
2019-07-06 14:50:50 +00:00
|
|
|
|
|
|
|
if (selected)
|
|
|
|
{
|
|
|
|
if (pPosition == NULL || !allowAnimation)
|
|
|
|
{
|
|
|
|
cancelAnimation(3);
|
|
|
|
|
|
|
|
this->setSelectedZoom(1);
|
|
|
|
mAnimPosition = Vector3f(0, 0, 0);
|
|
|
|
|
|
|
|
resize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mAnimPosition = Vector3f(pPosition->x(), pPosition->y(), pPosition->z());
|
|
|
|
|
|
|
|
auto func = [this](float t)
|
|
|
|
{
|
|
|
|
t -= 1; // cubic ease out
|
|
|
|
float pct = Math::lerp(0, 1, t*t*t + 1);
|
|
|
|
|
|
|
|
this->setSelectedZoom(pct);
|
|
|
|
};
|
|
|
|
|
|
|
|
cancelAnimation(3);
|
|
|
|
setAnimation(new LambdaAnimation(func, 250), 0, [this] {
|
|
|
|
this->setSelectedZoom(1);
|
|
|
|
mAnimPosition = Vector3f(0, 0, 0);
|
|
|
|
}, false, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // if (!selected)
|
|
|
|
{
|
|
|
|
if (!allowAnimation)
|
|
|
|
{
|
|
|
|
cancelAnimation(3);
|
|
|
|
this->setSelectedZoom(0);
|
|
|
|
|
|
|
|
resize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->setSelectedZoom(1);
|
|
|
|
|
|
|
|
auto func = [this](float t)
|
|
|
|
{
|
|
|
|
t -= 1; // cubic ease out
|
|
|
|
float pct = Math::lerp(0, 1, t*t*t + 1);
|
|
|
|
this->setSelectedZoom(1.0 - pct);
|
|
|
|
};
|
|
|
|
|
|
|
|
cancelAnimation(3);
|
|
|
|
setAnimation(new LambdaAnimation(func, 250), 0, [this] {
|
|
|
|
this->setSelectedZoom(0);
|
|
|
|
}, false, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GridTileComponent::setSelectedZoom(float percent)
|
|
|
|
{
|
|
|
|
if (mSelectedZoomPercent == percent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mSelectedZoomPercent = percent;
|
|
|
|
resize();
|
2018-04-07 19:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GridTileComponent::setVisible(bool visible)
|
|
|
|
{
|
|
|
|
mVisible = visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GridTileComponent::resize()
|
|
|
|
{
|
2019-07-06 14:50:50 +00:00
|
|
|
calcCurrentProperties();
|
|
|
|
|
|
|
|
mImage->setMaxSize(mCurrentProperties.mSize - mCurrentProperties.mPadding * 2);
|
|
|
|
mBackground.setCornerSize(mCurrentProperties.mBackgroundCornerSize);
|
|
|
|
mBackground.fitTo(mCurrentProperties.mSize - mBackground.getCornerSize() * 2);
|
|
|
|
}
|
2018-04-07 19:23:10 +00:00
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
unsigned int mixColors(unsigned int first, unsigned int second, float percent)
|
|
|
|
{
|
|
|
|
unsigned char alpha0 = (first >> 24) & 0xFF;
|
|
|
|
unsigned char blue0 = (first >> 16) & 0xFF;
|
|
|
|
unsigned char green0 = (first >> 8) & 0xFF;
|
|
|
|
unsigned char red0 = first & 0xFF;
|
|
|
|
|
|
|
|
unsigned char alpha1 = (second >> 24) & 0xFF;
|
|
|
|
unsigned char blue1 = (second >> 16) & 0xFF;
|
|
|
|
unsigned char green1 = (second >> 8) & 0xFF;
|
|
|
|
unsigned char red1 = second & 0xFF;
|
|
|
|
|
|
|
|
unsigned char alpha = (unsigned char)(alpha0 * (1.0 - percent) + alpha1 * percent);
|
|
|
|
unsigned char blue = (unsigned char)(blue0 * (1.0 - percent) + blue1 * percent);
|
|
|
|
unsigned char green = (unsigned char)(green0 * (1.0 - percent) + green1 * percent);
|
|
|
|
unsigned char red = (unsigned char)(red0 * (1.0 - percent) + red1 * percent);
|
|
|
|
|
|
|
|
return (alpha << 24) | (blue << 16) | (green << 8) | red;
|
2018-04-07 19:23:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-06 14:50:50 +00:00
|
|
|
void GridTileComponent::calcCurrentProperties()
|
|
|
|
{
|
|
|
|
mCurrentProperties = mSelected ? mSelectedProperties : mDefaultProperties;
|
|
|
|
|
|
|
|
float zoomPercentInverse = 1.0 - mSelectedZoomPercent;
|
|
|
|
|
|
|
|
if (mSelectedZoomPercent != 0.0f && mSelectedZoomPercent != 1.0f) {
|
|
|
|
if (mDefaultProperties.mSize != mSelectedProperties.mSize) {
|
|
|
|
mCurrentProperties.mSize = mDefaultProperties.mSize * zoomPercentInverse + mSelectedProperties.mSize * mSelectedZoomPercent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDefaultProperties.mPadding != mSelectedProperties.mPadding)
|
|
|
|
{
|
|
|
|
mCurrentProperties.mPadding = mDefaultProperties.mPadding * zoomPercentInverse + mSelectedProperties.mPadding * mSelectedZoomPercent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDefaultProperties.mImageColor != mSelectedProperties.mImageColor)
|
|
|
|
{
|
|
|
|
mCurrentProperties.mImageColor = mixColors(mDefaultProperties.mImageColor, mSelectedProperties.mImageColor, mSelectedZoomPercent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDefaultProperties.mBackgroundCornerSize != mSelectedProperties.mBackgroundCornerSize)
|
|
|
|
{
|
|
|
|
mCurrentProperties.mBackgroundCornerSize = mDefaultProperties.mBackgroundCornerSize * zoomPercentInverse + mSelectedProperties.mBackgroundCornerSize * mSelectedZoomPercent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDefaultProperties.mBackgroundCenterColor != mSelectedProperties.mBackgroundCenterColor)
|
|
|
|
{
|
|
|
|
mCurrentProperties.mBackgroundCenterColor = mixColors(mDefaultProperties.mBackgroundCenterColor, mSelectedProperties.mBackgroundCenterColor, mSelectedZoomPercent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDefaultProperties.mBackgroundEdgeColor != mSelectedProperties.mBackgroundEdgeColor)
|
|
|
|
{
|
|
|
|
mCurrentProperties.mBackgroundEdgeColor = mixColors(mDefaultProperties.mBackgroundEdgeColor, mSelectedProperties.mBackgroundEdgeColor, mSelectedZoomPercent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3f GridTileComponent::getBackgroundPosition()
|
|
|
|
{
|
|
|
|
return mBackground.getPosition() + mPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<TextureResource> GridTileComponent::getTexture()
|
|
|
|
{
|
|
|
|
if (mImage != nullptr)
|
|
|
|
return mImage->getTexture();
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
void GridTileComponent::forceSize(Vector2f size, float selectedZoom)
|
2018-04-07 19:23:10 +00:00
|
|
|
{
|
2019-07-06 14:50:50 +00:00
|
|
|
mDefaultProperties.mSize = size;
|
|
|
|
mSelectedProperties.mSize = size * selectedZoom;
|
2019-10-08 02:25:39 +00:00
|
|
|
}
|