Added a metadata field to GuiComponent.

Also did some general code cleanup of GuiComponent.
This commit is contained in:
Leon Styhre 2022-01-22 21:17:28 +01:00
parent 2aabe2eef3
commit d04a49957c
2 changed files with 45 additions and 27 deletions

View file

@ -24,10 +24,17 @@ GuiComponent::GuiComponent()
, mSaturation {1.0f} , mSaturation {1.0f}
, mColorShift {0} , mColorShift {0}
, mColorShiftEnd {0} , mColorShiftEnd {0}
, mColorOriginalValue {0}
, mColorChangedValue {0}
, mPosition {0.0f, 0.0f, 0.0f} , mPosition {0.0f, 0.0f, 0.0f}
, mOrigin {0.0f, 0.0f} , mOrigin {0.0f, 0.0f}
, mRotationOrigin {0.5f, 0.5f} , mRotationOrigin {0.5f, 0.5f}
, mSize {0.0f, 0.0f} , mSize {0.0f, 0.0f}
, mRotation {0.0f}
, mScale {1.0f}
, mDefaultZIndex {0.0f}
, mZIndex {0.0f}
, mScrollHide {false}
, mIsProcessing {false} , mIsProcessing {false}
, mVisible {true} , mVisible {true}
, mEnabled {true} , mEnabled {true}
@ -120,7 +127,7 @@ void GuiComponent::setSize(const float w, const float h)
onSizeChanged(); onSizeChanged();
} }
glm::vec2 GuiComponent::getCenter() const const glm::vec2 GuiComponent::getCenter() const
{ {
return glm::vec2 {mPosition.x - (getSize().x * mOrigin.x) + getSize().x / 2.0f, return glm::vec2 {mPosition.x - (getSize().x * mOrigin.x) + getSize().x / 2.0f,
mPosition.y - (getSize().y * mOrigin.y) + getSize().y / 2.0f}; mPosition.y - (getSize().y * mOrigin.y) + getSize().y / 2.0f};
@ -162,7 +169,7 @@ void GuiComponent::sortChildren()
}); });
} }
int GuiComponent::getChildIndex() const const int GuiComponent::getChildIndex() const
{ {
std::vector<GuiComponent*>::iterator it = std::vector<GuiComponent*>::iterator it =
std::find(getParent()->mChildren.begin(), getParent()->mChildren.end(), this); std::find(getParent()->mChildren.begin(), getParent()->mChildren.end(), this);
@ -235,7 +242,7 @@ void GuiComponent::setAnimation(Animation* anim,
delete oldAnim; delete oldAnim;
} }
bool GuiComponent::stopAnimation(unsigned char slot) const bool GuiComponent::stopAnimation(unsigned char slot)
{ {
assert(slot < MAX_ANIMATIONS); assert(slot < MAX_ANIMATIONS);
if (mAnimationMap[slot]) { if (mAnimationMap[slot]) {
@ -248,7 +255,7 @@ bool GuiComponent::stopAnimation(unsigned char slot)
} }
} }
bool GuiComponent::cancelAnimation(unsigned char slot) const bool GuiComponent::cancelAnimation(unsigned char slot)
{ {
assert(slot < MAX_ANIMATIONS); assert(slot < MAX_ANIMATIONS);
if (mAnimationMap[slot]) { if (mAnimationMap[slot]) {
@ -262,7 +269,7 @@ bool GuiComponent::cancelAnimation(unsigned char slot)
} }
} }
bool GuiComponent::finishAnimation(unsigned char slot) const bool GuiComponent::finishAnimation(unsigned char slot)
{ {
assert(slot < MAX_ANIMATIONS); assert(slot < MAX_ANIMATIONS);
AnimationController* anim = mAnimationMap[slot]; AnimationController* anim = mAnimationMap[slot];
@ -280,7 +287,7 @@ bool GuiComponent::finishAnimation(unsigned char slot)
} }
} }
bool GuiComponent::advanceAnimation(unsigned char slot, unsigned int time) const bool GuiComponent::advanceAnimation(unsigned char slot, unsigned int time)
{ {
assert(slot < MAX_ANIMATIONS); assert(slot < MAX_ANIMATIONS);
AnimationController* anim = mAnimationMap[slot]; AnimationController* anim = mAnimationMap[slot];

View file

@ -93,27 +93,30 @@ public:
virtual void onSizeChanged() {} virtual void onSizeChanged() {}
virtual glm::vec2 getRotationSize() const { return getSize(); } virtual glm::vec2 getRotationSize() const { return getSize(); }
float getRotation() const { return mRotation; } const float getRotation() const { return mRotation; }
void setRotation(float rotation) { mRotation = rotation; } void setRotation(float rotation) { mRotation = rotation; }
void setRotationDegrees(float rotation) void setRotationDegrees(float rotation)
{ {
setRotation(static_cast<float>(glm::radians(rotation))); setRotation(static_cast<float>(glm::radians(rotation)));
} }
float getScale() const { return mScale; } const float getScale() const { return mScale; }
void setScale(float scale) { mScale = scale; } void setScale(float scale) { mScale = scale; }
float getZIndex() const { return mZIndex; } const float getZIndex() const { return mZIndex; }
void setZIndex(float zIndex) { mZIndex = zIndex; } void setZIndex(float zIndex) { mZIndex = zIndex; }
float getDefaultZIndex() const { return mDefaultZIndex; } const float getDefaultZIndex() const { return mDefaultZIndex; }
void setDefaultZIndex(float zIndex) { mDefaultZIndex = zIndex; } void setDefaultZIndex(float zIndex) { mDefaultZIndex = zIndex; }
bool isVisible() const { return mVisible; } const bool isVisible() const { return mVisible; }
void setVisible(bool visible) { mVisible = visible; } void setVisible(bool visible) { mVisible = visible; }
const bool getScrollHide() { return mScrollHide; }
void setScrollHide(bool state) { mScrollHide = state; }
// Returns the center point of the image (takes origin into account). // Returns the center point of the image (takes origin into account).
glm::vec2 getCenter() const; const glm::vec2 getCenter() const;
void setParent(GuiComponent* parent) { mParent = parent; } void setParent(GuiComponent* parent) { mParent = parent; }
GuiComponent* getParent() const { return mParent; } GuiComponent* getParent() const { return mParent; }
@ -122,18 +125,21 @@ public:
void removeChild(GuiComponent* cmp); void removeChild(GuiComponent* cmp);
void clearChildren() { mChildren.clear(); } void clearChildren() { mChildren.clear(); }
void sortChildren(); void sortChildren();
unsigned int getChildCount() const { return static_cast<int>(mChildren.size()); } const unsigned int getChildCount() const { return static_cast<int>(mChildren.size()); }
int getChildIndex() const; const int getChildIndex() const;
GuiComponent* getChild(unsigned int i) const { return mChildren.at(i); } GuiComponent* getChild(unsigned int i) const { return mChildren.at(i); }
// Animation will be automatically deleted when it completes or is stopped. // Animation will be automatically deleted when it completes or is stopped.
bool isAnimationPlaying(unsigned char slot) const { return mAnimationMap[slot] != nullptr; } const bool isAnimationPlaying(unsigned char slot) const
bool isAnimationReversed(unsigned char slot) const {
return mAnimationMap[slot] != nullptr;
}
const bool isAnimationReversed(unsigned char slot) const
{ {
assert(mAnimationMap[slot] != nullptr); assert(mAnimationMap[slot] != nullptr);
return mAnimationMap[slot]->isReversed(); return mAnimationMap[slot]->isReversed();
} }
int getAnimationTime(unsigned char slot) const const int getAnimationTime(unsigned char slot) const
{ {
assert(mAnimationMap[slot] != nullptr); assert(mAnimationMap[slot] != nullptr);
return mAnimationMap[slot]->getTime(); return mAnimationMap[slot]->getTime();
@ -143,15 +149,15 @@ public:
std::function<void()> finishedCallback = nullptr, std::function<void()> finishedCallback = nullptr,
bool reverse = false, bool reverse = false,
unsigned char slot = 0); unsigned char slot = 0);
bool stopAnimation(unsigned char slot); const bool stopAnimation(unsigned char slot);
// Like stopAnimation, but doesn't call finishedCallback - only removes the animation, leaving // Like stopAnimation, but doesn't call finishedCallback - only removes the animation, leaving
// things in their current state. Returns true if successful (an animation was in this slot). // things in their current state. Returns true if successful (an animation was in this slot).
bool cancelAnimation(unsigned char slot); const bool cancelAnimation(unsigned char slot);
// Calls update(1.f) and finishedCallback, then deletes the animation - basically skips // Calls update(1.f) and finishedCallback, then deletes the animation - basically skips
// to the end. Returns true if successful (an animation was in this slot). // to the end. Returns true if successful (an animation was in this slot).
bool finishAnimation(unsigned char slot); const bool finishAnimation(unsigned char slot);
// Returns true if successful (an animation was in this slot). // Returns true if successful (an animation was in this slot).
bool advanceAnimation(unsigned char slot, unsigned int time); const bool advanceAnimation(unsigned char slot, unsigned int time);
void stopAllAnimations(); void stopAllAnimations();
void cancelAllAnimations(); void cancelAllAnimations();
@ -181,6 +187,9 @@ public:
virtual bool getEnabled() { return mEnabled; } virtual bool getEnabled() { return mEnabled; }
virtual void setEnabled(bool state) { mEnabled = state; } virtual void setEnabled(bool state) { mEnabled = state; }
std::string getMetadataField() { return mMetadataField; }
void setMetadataField(const std::string& text) { mMetadataField = text; }
virtual std::shared_ptr<Font> getFont() const { return nullptr; } virtual std::shared_ptr<Font> getFont() const { return nullptr; }
const glm::mat4& getTransform(); const glm::mat4& getTransform();
@ -228,7 +237,7 @@ public:
virtual HelpStyle getHelpStyle() { return HelpStyle(); } virtual HelpStyle getHelpStyle() { return HelpStyle(); }
// Returns true if the component is busy doing background processing (e.g. HTTP downloads). // Returns true if the component is busy doing background processing (e.g. HTTP downloads).
bool isProcessing() const { return mIsProcessing; } const bool isProcessing() const { return mIsProcessing; }
const static unsigned char MAX_ANIMATIONS = 4; const static unsigned char MAX_ANIMATIONS = 4;
@ -242,6 +251,8 @@ protected:
GuiComponent* mParent; GuiComponent* mParent;
std::vector<GuiComponent*> mChildren; std::vector<GuiComponent*> mChildren;
std::string mMetadataField;
unsigned char mOpacity; unsigned char mOpacity;
unsigned int mColor; unsigned int mColor;
float mSaturation; float mSaturation;
@ -256,12 +267,12 @@ protected:
glm::vec2 mRotationOrigin; glm::vec2 mRotationOrigin;
glm::vec2 mSize; glm::vec2 mSize;
float mRotation = 0.0; float mRotation;
float mScale = 1.0; float mScale;
float mDefaultZIndex;
float mDefaultZIndex = 0; float mZIndex;
float mZIndex = 0;
bool mScrollHide;
bool mIsProcessing; bool mIsProcessing;
bool mVisible; bool mVisible;
bool mEnabled; bool mEnabled;