93 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			93 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| // 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<void()> 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->readyToClose) {
 | |
|           this->readyToClose = true;
 | |
|           this->eventReadyToClose.invoke();
 | |
|         }
 | |
|       } else {
 | |
|         if(game->inputManager.isPressed(INPUT_BIND_ACCEPT)) {
 | |
|           this->lineCurrent += this->visibleLines;
 | |
|           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<int32_t>(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<int32_t>(this->timeCharacter);
 | |
|     if(newCount == this->label->quadCount) return;
 | |
|     this->label->quadCount = mathFloor<int32_t>(this->timeCharacter);
 | |
|     this->eventCharacterRevealed.invoke();
 | |
|   }, getScene()->eventSceneUpdate);
 | |
| }
 | |
| 
 | |
| bool_t VNTextboxScroller::hasRevealedAllCurrentCharacters() {
 | |
|   int32_t quadsTotal = 0;
 | |
|   for(
 | |
|     size_t i = this->lineCurrent;
 | |
|     i < mathMin<size_t>(
 | |
|       this->label->lines.size(),
 | |
|       this->lineCurrent + this->visibleLines
 | |
|     );
 | |
|     i++
 | |
|   ) {
 | |
|     quadsTotal += this->label->lines[i].quadCount;
 | |
|   }
 | |
|   return mathFloor<int32_t>(this->timeCharacter) >= quadsTotal;
 | |
| }
 | |
| 
 | |
| bool_t VNTextboxScroller::hasRevealedAllCharacters() {
 | |
|   return (
 | |
|     this->lineCurrent + this->visibleLines >=
 | |
|     this->label->lines.size()
 | |
|   );
 | |
|   assertUnreachable();
 | |
| } |