TWP: Add HUD fade animation

This commit is contained in:
scemino 2024-01-28 23:14:33 +01:00 committed by Eugene Sandulenko
parent 243776e27c
commit a912e659e3
5 changed files with 79 additions and 15 deletions

View File

@ -154,7 +154,7 @@ void Hud::drawCore(Math::Matrix4 trsf) {
const SpriteSheetFrame &backingFrame = gameSheet->frameTable[classic ? "ui_backing_tall" : "ui_backing"];
Texture *gameTexture = g_engine->_resManager.texture(gameSheet->meta.image);
float alpha = 0.33f; // prefs(UiBackingAlpha);
g_engine->getGfx().drawSprite(backingFrame.frame, *gameTexture, Color(0, 0, 0, alpha), trsf);
g_engine->getGfx().drawSprite(backingFrame.frame, *gameTexture, Color(0, 0, 0, alpha*getAlpha()), trsf);
bool verbHlt = ConfMan.getBool("invertVerbHighlight");
Color verbHighlight = verbHlt ? Color() : slot->verbUiColors.verbHighlight;
@ -185,17 +185,45 @@ void Hud::drawCore(Math::Matrix4 trsf) {
if (_mouseClick && over) {
_verb = verb;
}
drawSprite(verbFrame, verbTexture, color, trsf);
drawSprite(verbFrame, verbTexture, Color::withAlpha(color, getAlpha()), trsf);
}
}
g_engine->getGfx().use(saveShader);
_over = isOver;
}
void Hud::update(Math::Vector2d pos, Object *hotspot, bool mouseClick) {
void Hud::update(float elapsed, Math::Vector2d pos, Object *hotspot, bool mouseClick) {
_mousePos = Math::Vector2d(pos.getX(), SCREEN_HEIGHT - pos.getY());
_defaultVerbId = !hotspot ? 0 : hotspot->defaultVerbId();
_mouseClick = mouseClick;
_fadeTime += elapsed;
if (_fadeTime > 2.f) {
_fadeTime = 2.f;
if (!_fadeIn) {
setVisible(false);
}
}
if (_fadeIn) {
float alpha = MIN(_fadeTime, 2.0f) / 2.0f;
setAlpha(alpha);
} else {
float alpha = MAX(2.0f - _fadeTime, 0.0f) / 2.0f;
setAlpha(alpha);
}
}
void Hud::setVisible(bool visible) {
if (_fadeIn != visible) {
_fadeIn = visible;
if (visible) {
Node::setVisible(visible);
}
_fadeTime = 0;
}
}
} // namespace Twp

View File

@ -116,7 +116,9 @@ public:
void init();
ActorSlot *actorSlot(Object *actor);
bool isOver() const { return _over; }
void update(Math::Vector2d pos, Object *hotspot, bool mouseClick);
void update(float elapsed, Math::Vector2d pos, Object *hotspot, bool mouseClick);
void setVisible(bool visible) override;
private:
virtual void drawCore(Math::Matrix4 trsf) override final;
@ -132,6 +134,8 @@ public:
bool _mouseClick = false;
bool _over = false;
int _defaultVerbId = 0;
float _fadeTime = 0.f;
bool _fadeIn = false;
};
} // namespace Twp

View File

@ -275,7 +275,8 @@ void Anim::setAnim(const ObjectAnimation *anim, float fps, bool loop, bool insta
_frameDuration = 1.0 / _getFps(fps, anim->fps);
_loop = loop || anim->loop;
_instant = instant;
if(_obj) setVisible(Twp::find(_obj->_hiddenLayers, _anim->name) == (size_t)-1);
if (_obj)
setVisible(Twp::find(_obj->_hiddenLayers, _anim->name) == (size_t)-1);
clear();
for (size_t i = 0; i < _anim->layers.size(); i++) {
@ -537,8 +538,8 @@ void Inventory::drawArrows(Math::Matrix4 trsf) {
Math::Matrix4 tDn(trsf);
tDn.translate(Math::Vector3d(SCREEN_WIDTH / 2.f + ARROWWIDTH / 2.f + MARGIN, 0.5f * ARROWHEIGHT, 0.f));
drawSprite(*arrowUp, texture, Color::withAlpha(_verbNormal, alphaUp), tUp);
drawSprite(*arrowDn, texture, Color::withAlpha(_verbNormal, alphaDn), tDn);
drawSprite(*arrowUp, texture, Color::withAlpha(_verbNormal, alphaUp * getAlpha()), tUp);
drawSprite(*arrowDn, texture, Color::withAlpha(_verbNormal, alphaDn * getAlpha()), tDn);
}
void Inventory::drawBack(Math::Matrix4 trsf) {
@ -553,7 +554,7 @@ void Inventory::drawBack(Math::Matrix4 trsf) {
for (int i = 0; i < 4; i++) {
Math::Matrix4 t(trsf);
t.translate(Math::Vector3d(offsetX, offsetY, 0.f));
drawSprite(*back, texture, _backColor, t);
drawSprite(*back, texture, Color::withAlpha(_backColor, getAlpha()), t);
offsetX += back->sourceSize.getX() + BACKOFFSET;
}
@ -562,7 +563,7 @@ void Inventory::drawBack(Math::Matrix4 trsf) {
for (int i = 0; i < 4; i++) {
Math::Matrix4 t(trsf);
t.translate(Math::Vector3d(offsetX, offsetY, 0.f));
drawSprite(*back, texture, _backColor, t);
drawSprite(*back, texture, Color::withAlpha(_backColor, getAlpha()), t);
offsetX += back->sourceSize.getX() + BACKOFFSET;
}
}
@ -589,7 +590,7 @@ void Inventory::drawItems(Math::Matrix4 trsf) {
}
float s = obj->getScale();
Twp::scale(t, Math::Vector2d(s, s));
drawSprite(*itemFrame, texture, Color(), t);
drawSprite(*itemFrame, texture, Color::withAlpha(Color(), getAlpha()), t);
}
}
}
@ -604,6 +605,22 @@ void Inventory::drawCore(Math::Matrix4 trsf) {
void Inventory::update(float elapsed, Object *actor, Color backColor, Color verbNormal) {
_jiggleTime += 10.f * elapsed;
_fadeTime += elapsed;
if (_fadeTime > 2.f) {
_fadeTime = 2.f;
if (!_fadeIn) {
setVisible(false);
}
}
if (_fadeIn) {
float alpha = MIN(_fadeTime, 2.0f) / 2.0f;
setAlpha(alpha);
} else {
float alpha = MAX(2.0f - _fadeTime, 0.0f) / 2.0f;
setAlpha(alpha);
}
// udate colors
_actor = actor;
@ -630,7 +647,7 @@ void Inventory::update(float elapsed, Object *actor, Color backColor, Color verb
for (int i = 0; i < NUMOBJECTS; i++) {
const Common::Rect &item = _itemRects[i];
if (item.contains(scrPos.getX(), scrPos.getY())) {
size_t index = _actor->_inventoryOffset * NUMOBJECTSBYROW + i;
size_t index = _actor->_inventoryOffset * NUMOBJECTSBYROW + i;
if (index < _actor->_inventory.size())
_obj = _actor->_inventory[index];
break;
@ -644,6 +661,16 @@ void Inventory::update(float elapsed, Object *actor, Color backColor, Color verb
}
}
void Inventory::setVisible(bool visible) {
if (_fadeIn != visible) {
_fadeIn = visible;
if (visible) {
Node::setVisible(visible);
}
_fadeTime = 0;
}
}
SentenceNode::SentenceNode() : Node("Sentence") {
_zOrder = -100;
}

View File

@ -45,7 +45,7 @@ public:
void setName(const Common::String &name) { _name = name; }
const Common::String &getName() const { return _name; }
void setVisible(bool visible) { _visible = visible; }
virtual void setVisible(bool visible) { _visible = visible; }
bool isVisible() const { return _visible; }
// Adds new child in current node.
@ -297,6 +297,8 @@ public:
Object* getObject() const { return _obj; }
Math::Vector2d getPos(Object* inv) const;
void setVisible(bool visible) override;
private:
virtual void drawCore(Math::Matrix4 trsf) override final;
void drawArrows(Math::Matrix4 trsf);
@ -313,6 +315,8 @@ private:
Common::Rect _arrowUpRect;
Common::Rect _arrowDnRect;
float _jiggleTime = 0.f;
float _fadeTime = 0.f;
bool _fadeIn = false;
};
class SentenceNode: public Node {

View File

@ -438,9 +438,10 @@ void TwpEngine::update(float elapsed) {
}
_inputState.setHotspot(_noun1 != nullptr);
_hud.setVisible(_inputState.getInputActive() && _inputState.getInputVerbsActive() && _dialog.getState() == DialogState::None);
bool hudVisible = _inputState.getInputActive() && _inputState.getInputVerbsActive() && _dialog.getState() == DialogState::None && !_cutscene;
_hud.setVisible(hudVisible);
_sentence.setVisible(_hud.isVisible());
_uiInv.setVisible(_hud.isVisible() && !_cutscene);
_uiInv.setVisible(hudVisible);
_actorSwitcher.setVisible((_dialog.getState() == DialogState::None) && !_cutscene);
// Common::String cursortxt = Common::String::format("%s (%d, %d) - (%d, %d)", cursorText().c_str(), (int)roomPos.getX(), (int)roomPos.getY(), (int)scrPos.getX(), (int)scrPos.getY());
//_sentence.setText(cursortxt.c_str());
@ -545,7 +546,7 @@ void TwpEngine::update(float elapsed) {
if (!_actor) {
_uiInv.update(elapsed);
} else {
_hud.update(scrPos, _noun1, _cursor.isLeftDown());
_hud.update(elapsed, scrPos, _noun1, _cursor.isLeftDown());
VerbUiColors *verbUI = &_hud.actorSlot(_actor)->verbUiColors;
_uiInv.update(elapsed, _actor, verbUI->inventoryBackground, verbUI->verbNormal);
}