Some code cleanup.

This commit is contained in:
Leon Styhre 2021-03-23 22:01:47 +01:00
parent e6c3d82a9e
commit cf2b9534e8
4 changed files with 37 additions and 39 deletions

View file

@ -118,7 +118,7 @@ void GridTileComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
// Apply theme to the selected gridtile. Note that some of the default gridtile // Apply theme to the selected gridtile. Note that some of the default gridtile
// properties have influence on the selected gridtile properties. // properties have influence on the selected gridtile properties.
// See THEMES.md for more informations. // See THEMES.md for more information.
elem = theme->getElement(view, "selected", "gridtile"); elem = theme->getElement(view, "selected", "gridtile");
mSelectedProperties.mSize = getSelectedTileSize(); mSelectedProperties.mSize = getSelectedTileSize();
@ -130,8 +130,6 @@ void GridTileComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
applyThemeToProperties(elem, &mSelectedProperties); applyThemeToProperties(elem, &mSelectedProperties);
} }
// Made this a static function because the ImageGridComponent needs to know the default tile
// max size to calculate the grid dimension before it instantiates the GridTileComponents.
Vector2f GridTileComponent::getDefaultTileSize() Vector2f GridTileComponent::getDefaultTileSize()
{ {
Vector2f screen = Vector2f(static_cast<float>(Renderer::getScreenWidth()), Vector2f screen = Vector2f(static_cast<float>(Renderer::getScreenWidth()),
@ -171,8 +169,8 @@ void GridTileComponent::setImage(const std::shared_ptr<TextureResource>& texture
resize(); resize();
} }
void GridTileComponent::setSelected(bool selected, bool allowAnimation, void GridTileComponent::setSelected(
Vector3f* pPosition, bool force) bool selected, bool allowAnimation, Vector3f* pPosition, bool force)
{ {
if (mSelected == selected && !force) if (mSelected == selected && !force)
return; return;
@ -204,7 +202,6 @@ void GridTileComponent::setSelected(bool selected, bool allowAnimation,
}, false, 3); }, false, 3);
} }
} }
// If (!selected).
else { else {
if (!allowAnimation) { if (!allowAnimation) {
cancelAnimation(3); cancelAnimation(3);
@ -247,9 +244,9 @@ void GridTileComponent::resize()
{ {
calcCurrentProperties(); calcCurrentProperties();
mImage->setMaxSize(mCurrentProperties.mSize - mCurrentProperties.mPadding * 2); mImage->setMaxSize(mCurrentProperties.mSize - mCurrentProperties.mPadding * 2.0f);
mBackground.setCornerSize(mCurrentProperties.mBackgroundCornerSize); mBackground.setCornerSize(mCurrentProperties.mBackgroundCornerSize);
mBackground.fitTo(mCurrentProperties.mSize - mBackground.getCornerSize() * 2); mBackground.fitTo(mCurrentProperties.mSize - mBackground.getCornerSize() * 2.0f);
} }
unsigned int mixColors(unsigned int first, unsigned int second, float percent) unsigned int mixColors(unsigned int first, unsigned int second, float percent)

View file

@ -72,8 +72,8 @@ void ImageComponent::resize()
// ratios), it can cause cutoff when the aspect ratio breaks. // ratios), it can cause cutoff when the aspect ratio breaks.
// So we always make sure the resultant height is an integer to make sure cutoff doesn't // So we always make sure the resultant height is an integer to make sure cutoff doesn't
// happen, and scale width from that (you'll see this scattered throughout the function). // happen, and scale width from that (you'll see this scattered throughout the function).
// It's important to use the floorf rather than round for this, as we never want to round // It's important to use floorf rather than round for this, as we never want to round up
// up since that can lead to the cutoff just described. // since that can lead to the cutoff just described.
if (mTargetIsMax) { if (mTargetIsMax) {
mSize = textureSize; mSize = textureSize;

View file

@ -9,11 +9,11 @@
#ifndef ES_CORE_COMPONENTS_IMAGE_GRID_COMPONENT_H #ifndef ES_CORE_COMPONENTS_IMAGE_GRID_COMPONENT_H
#define ES_CORE_COMPONENTS_IMAGE_GRID_COMPONENT_H #define ES_CORE_COMPONENTS_IMAGE_GRID_COMPONENT_H
#include "Log.h"
#include "animations/LambdaAnimation.h" #include "animations/LambdaAnimation.h"
#include "components/IList.h" #include "components/IList.h"
#include "resources/TextureResource.h" #include "resources/TextureResource.h"
#include "GridTileComponent.h" #include "GridTileComponent.h"
#include "Log.h"
#define EXTRAITEMS 2 #define EXTRAITEMS 2
@ -334,8 +334,8 @@ void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
std::string oldDefaultGameTexture = mDefaultGameTexture; std::string oldDefaultGameTexture = mDefaultGameTexture;
mDefaultGameTexture = path; mDefaultGameTexture = path;
// mEntries are already loaded at this point, so we need to // mEntries are already loaded at this point, so we need to update them with
// update them with the new game image texture. // the new game image texture.
for (auto it = mEntries.begin(); it != mEntries.end(); it++) { for (auto it = mEntries.begin(); it != mEntries.end(); it++) {
if ((*it).data.texturePath == oldDefaultGameTexture) if ((*it).data.texturePath == oldDefaultGameTexture)
(*it).data.texturePath = mDefaultGameTexture; (*it).data.texturePath = mDefaultGameTexture;
@ -353,8 +353,8 @@ void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
std::string oldDefaultFolderTexture = mDefaultFolderTexture; std::string oldDefaultFolderTexture = mDefaultFolderTexture;
mDefaultFolderTexture = path; mDefaultFolderTexture = path;
// mEntries are already loaded at this point, so we need to // mEntries are already loaded at this point, so we need to update them with
// update them with new folder image texture. // the new folder image texture.
for (auto it = mEntries.begin(); it != mEntries.end(); it++) { for (auto it = mEntries.begin(); it != mEntries.end(); it++) {
if ((*it).data.texturePath == oldDefaultFolderTexture) if ((*it).data.texturePath == oldDefaultFolderTexture)
(*it).data.texturePath = mDefaultFolderTexture; (*it).data.texturePath = mDefaultFolderTexture;
@ -363,8 +363,8 @@ void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
} }
} }
// We still need to manually get the grid tile size here, so we can recalculate // We still need to manually get the grid tile size here, so we can recalculate the new
// the new grid dimension, and THEN (re)build the tiles. // grid dimension, and then (re)build the tiles.
elem = theme->getElement(view, "default", "gridtile"); elem = theme->getElement(view, "default", "gridtile");
mTileSize = elem && elem->has("size") ? mTileSize = elem && elem->has("size") ?
@ -374,7 +374,7 @@ void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
// Apply size property which will trigger a call to onSizeChanged() which will build the tiles. // Apply size property which will trigger a call to onSizeChanged() which will build the tiles.
GuiComponent::applyTheme(theme, view, element, ThemeFlags::SIZE); GuiComponent::applyTheme(theme, view, element, ThemeFlags::SIZE);
// Trigger the call manually if the theme have no "imagegrid" element. // Trigger the call manually if the theme has no "imagegrid" element.
if (!elem) if (!elem)
buildTiles(); buildTiles();
} }
@ -405,7 +405,7 @@ void ImageGridComponent<T>::onCursorChanged(const CursorState& state)
int dimScrollable = (isVertical() ? mGridDimension.y() : mGridDimension.x()) - 2 * EXTRAITEMS; int dimScrollable = (isVertical() ? mGridDimension.y() : mGridDimension.x()) - 2 * EXTRAITEMS;
int dimOpposite = isVertical() ? mGridDimension.x() : mGridDimension.y(); int dimOpposite = isVertical() ? mGridDimension.x() : mGridDimension.y();
int centralCol = static_cast<int>((dimScrollable - 0.5) / 2); int centralCol = static_cast<int>((static_cast<float>(dimScrollable) - 0.5f) / 2.0f);
int maxCentralCol = dimScrollable / 2; int maxCentralCol = dimScrollable / 2;
int oldCol = (mLastCursor / dimOpposite); int oldCol = (mLastCursor / dimOpposite);
@ -418,9 +418,9 @@ void ImageGridComponent<T>::onCursorChanged(const CursorState& state)
float startPos = 0; float startPos = 0;
float endPos = 1; float endPos = 1;
if (((GuiComponent*)this)->isAnimationPlaying(2)) { if (reinterpret_cast<GuiComponent*>(this)->isAnimationPlaying(2)) {
startPos = 0; startPos = 0;
((GuiComponent*)this)->cancelAnimation(2); reinterpret_cast<GuiComponent*>(this)->cancelAnimation(2);
updateTiles(direction, false, false); updateTiles(direction, false, false);
} }
@ -507,13 +507,14 @@ void ImageGridComponent<T>::onCursorChanged(const CursorState& state)
if (!moveCamera) if (!moveCamera)
return; return;
t -= 1; // cubic ease out t -= 1; // Cubic ease out.
float pct = Math::lerp(0, 1, t*t*t + 1); float pct = Math::lerp(0, 1, t*t*t + 1);
t = startPos * (1.0f - pct) + endPos * pct; t = startPos * (1.0f - pct) + endPos * pct;
mCamera = t; mCamera = t;
}; };
((GuiComponent*)this)->setAnimation(new LambdaAnimation(func, 250), 0, [this, direction] { reinterpret_cast<GuiComponent*>(this)->setAnimation(
new LambdaAnimation(func, 250), 0, [this, direction] {
mCamera = 0; mCamera = 0;
updateTiles(direction, false); updateTiles(direction, false);
}, false, 2); }, false, 2);
@ -537,10 +538,10 @@ void ImageGridComponent<T>::buildTiles()
Vector2f tileDistance = mTileSize + mMargin; Vector2f tileDistance = mTileSize + mMargin;
if (mAutoLayout.x() != 0 && mAutoLayout.y() != 0) { if (mAutoLayout.x() != 0.0f && mAutoLayout.y() != 0.0f) {
auto x = (mSize.x() - (mMargin.x() * (mAutoLayout.x() - 1)) - mPadding.x() - auto x = (mSize.x() - (mMargin.x() * (mAutoLayout.x() - 1.0f)) - mPadding.x() -
mPadding.z()) / static_cast<int>(mAutoLayout.x()); mPadding.z()) / static_cast<int>(mAutoLayout.x());
auto y = (mSize.y() - (mMargin.y() * (mAutoLayout.y() - 1)) - mPadding.y() - auto y = (mSize.y() - (mMargin.y() * (mAutoLayout.y() - 1.0f)) - mPadding.y() -
mPadding.w()) / static_cast<int>(mAutoLayout.y()); mPadding.w()) / static_cast<int>(mAutoLayout.y());
mTileSize = Vector2f(x, y); mTileSize = Vector2f(x, y);
@ -548,7 +549,7 @@ void ImageGridComponent<T>::buildTiles()
} }
bool vert = isVertical(); bool vert = isVertical();
Vector2f startPosition = mTileSize / 2; Vector2f startPosition = mTileSize / 2.0f;
startPosition += mPadding.v2(); startPosition += mPadding.v2();
int X; int X;
@ -560,8 +561,8 @@ void ImageGridComponent<T>::buildTiles()
// Create tiles. // Create tiles.
auto tile = std::make_shared<GridTileComponent>(mWindow); auto tile = std::make_shared<GridTileComponent>(mWindow);
// In Vertical mod, tiles are ordered from left to right, then from top to bottom. // In Vertical mode, 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. // In Horizontal mode, tiles are ordered from top to bottom, then from left to right.
X = vert ? x : y - EXTRAITEMS; X = vert ? x : y - EXTRAITEMS;
Y = vert ? y - EXTRAITEMS : x; Y = vert ? y - EXTRAITEMS : x;
@ -600,7 +601,7 @@ void ImageGridComponent<T>::updateTiles(bool ascending, bool allowAnimation,
return; return;
} }
// Temporary store previous texture so they can't be unloaded. // Temporarily store the previous textures so that they can't be unloaded.
std::vector<std::shared_ptr<TextureResource>> previousTextures; std::vector<std::shared_ptr<TextureResource>> previousTextures;
for (int ti = 0; ti < static_cast<int>(mTiles.size()); ti++) { for (int ti = 0; ti < static_cast<int>(mTiles.size()); ti++) {
std::shared_ptr<GridTileComponent> tile = mTiles.at(ti); std::shared_ptr<GridTileComponent> tile = mTiles.at(ti);
@ -643,7 +644,7 @@ void ImageGridComponent<T>::updateTileAtPos(int tilePos, int imgPos,
imgPos -= static_cast<int>(mEntries.size()); imgPos -= static_cast<int>(mEntries.size());
} }
// If we have more tiles than we have to display images on screen, hide them. // If we have more tiles than we can display on screen, hide them.
// Same for tiles out of the buffer. // Same for tiles out of the buffer.
if (imgPos < 0 || imgPos >= size() || tilePos < 0 || if (imgPos < 0 || imgPos >= size() || tilePos < 0 ||
tilePos >= static_cast<int>(mTiles.size())) { tilePos >= static_cast<int>(mTiles.size())) {
@ -683,8 +684,8 @@ void ImageGridComponent<T>::updateTileAtPos(int tilePos, int imgPos,
} }
} }
// Calculate how much tiles of size mTileSize we can fit in a grid of size mSize using // Calculate how many tiles of size mTileSize we can fit in a grid of size mSize using
// a margin of size mMargin. // a margin size of mMargin.
template<typename T> template<typename T>
void ImageGridComponent<T>::calcGridDimension() void ImageGridComponent<T>::calcGridDimension()
{ {
@ -709,7 +710,7 @@ void ImageGridComponent<T>::calcGridDimension()
LOG(LogError) << "Theme defined grid Y dimension below 1"; LOG(LogError) << "Theme defined grid Y dimension below 1";
} }
// Add extra tiles to both sides: Add EXTRAITEMS before, EXTRAITEMS after. // Add extra tiles to both sides.
if (isVertical()) if (isVertical())
mGridDimension.y() += 2 * EXTRAITEMS; mGridDimension.y() += 2 * EXTRAITEMS;
else else

View file

@ -127,10 +127,10 @@ void NinePatchComponent::render(const Transform4x4f& parentTrans)
// We have reached full opacity, so disable the opacity shader and set // We have reached full opacity, so disable the opacity shader and set
// the vertex opacity to 1.0. // the vertex opacity to 1.0.
mVertices[0].shaders ^= Renderer::SHADER_OPACITY; mVertices[0].shaders ^= Renderer::SHADER_OPACITY;
mVertices[0].opacity = 1.0; mVertices[0].opacity = 1.0f;
} }
mTexture->bind(); mTexture->bind();
Renderer::drawTriangleStrips(&mVertices[0], 6*9, trans); Renderer::drawTriangleStrips(&mVertices[0], 6 * 9, trans);
} }
renderChildren(trans); renderChildren(trans);
@ -144,10 +144,10 @@ void NinePatchComponent::onSizeChanged()
void NinePatchComponent::fitTo(Vector2f size, Vector3f position, Vector2f padding) void NinePatchComponent::fitTo(Vector2f size, Vector3f position, Vector2f padding)
{ {
size += padding; size += padding;
position[0] -= padding.x() / 2; position[0] -= padding.x() / 2.0f;
position[1] -= padding.y() / 2; position[1] -= padding.y() / 2.0f;
setSize(size + mCornerSize * 2); setSize(size + mCornerSize * 2.0f);
setPosition(position.x() + Math::lerp(-mCornerSize.x(), mCornerSize.x(), mOrigin.x()), setPosition(position.x() + Math::lerp(-mCornerSize.x(), mCornerSize.x(), mOrigin.x()),
position.y() + Math::lerp(-mCornerSize.y(), mCornerSize.y(), mOrigin.y())); position.y() + Math::lerp(-mCornerSize.y(), mCornerSize.y(), mOrigin.y()));
} }