// Copyright (c) 2023 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #include "VNTextboxScroller.hpp" #include "game/DawnGame.hpp" using namespace Dawn; VNTextboxScroller::VNTextboxScroller(SceneItem *item) : SceneItemComponent(item), label(nullptr), visibleLines(4) { } void VNTextboxScroller::onStart() { assertNotNull(label); std::function x = [&]{ this->lineCurrent = 0; this->timeCharacter = 0; this->label->quadStart = 0; this->label->quadCount = 0; this->readyToClose = false; }; x(); useEvent(x, this->label->eventTextChanged); // useEffect(x, visibleLines); useEvent([&](float_t delta){ auto game = this->getGame(); this->timeCharacter += delta; if(this->hasRevealedAllCurrentCharacters()) { if(this->hasRevealedAllCharacters()) { if(!this->label->lines.empty()) { this->label->quadStart = this->label->lines[this->lineCurrent].quadStart; } this->label->quadCount = this->getTotalQuadsToBeRevealed(); if(!this->readyToClose) { this->readyToClose = true; this->eventReadyToClose.invoke(); } } else { if(game->inputManager.isPressed(INPUT_BIND_ACCEPT)) { this->lineCurrent += this->visibleLines; if(!this->label->lines.empty()) { this->label->quadStart = this->label->lines[this->lineCurrent].quadStart; } this->label->quadCount = 0; this->timeCharacter = 0.0f; this->label->textOffset = ( -this->label->lines[this->lineCurrent].position ); } } return; } auto lastTimeCharacter = mathFloor(this->timeCharacter); if(game->inputManager.isDown(INPUT_BIND_ACCEPT)) { this->timeCharacter += game->timeManager.delta * VN_TEXTBOX_SPEED_FASTER; } else { this->timeCharacter += game->timeManager.delta * VN_TEXTBOX_SPEED; } auto newCount = mathFloor(this->timeCharacter); if(newCount == this->label->quadCount) return; this->label->quadCount = mathFloor(this->timeCharacter); this->eventCharacterRevealed.invoke(); }, getScene()->eventSceneUpdate); } int32_t VNTextboxScroller::getTotalQuadsToBeRevealed() { int32_t quadsTotal = 0; for( size_t i = this->lineCurrent; i < mathMin( this->label->lines.size(), this->lineCurrent + this->visibleLines ); i++ ) { quadsTotal += this->label->lines[i].quadCount; } return quadsTotal; } bool_t VNTextboxScroller::hasRevealedAllCurrentCharacters() { return mathFloor(this->timeCharacter) >= this->getTotalQuadsToBeRevealed(); } bool_t VNTextboxScroller::hasRevealedAllCharacters() { return ( this->lineCurrent + this->visibleLines >= this->label->lines.size() ); assertUnreachable(); }