You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
VirusAttack/olcCodeJam2023Entry/Textbox.cpp

97 lines
2.4 KiB

#include "Textbox.h"
#include "olcUTIL_Geometry2D.h"
Textbox::Textbox(){};
void Textbox::Initialize(std::string text,vf2d pos,std::string headerText,vf2d maxSize,std::vector<Memory>resourceCost,float letterDisplayDelay)
{
if(GetCurrentString()!=text){ //Make sure this is actually a new textbox
SetDefaults();
this->text=text;
this->headerText=headerText;
this->maxSize=maxSize;
this->resourceCost=resourceCost;
this->letterDisplayDelay=letterDisplayDelay;
visible=true;
}
}
void Textbox::SetDefaults(){
lastLetterTime=0;
textboxMarker=-1;
lastWordMarker=-1;
lastWord="";
displayText="";
text="";
}
void Textbox::Update(PixelGameEngine*pge){
lastLetterTime-=pge->GetElapsedTime();
if(lastLetterTime<=0){
if(textboxMarker<int(text.length()-1)){
std::string tempText=displayText;
tempText+=text[textboxMarker+1];
if(pge->GetTextSizeProp(tempText).x>=maxSize.x){
displayText=displayText.substr(0,lastWordMarker);
displayText+='\n';
displayText+=lastWord;
}
if(text[textboxMarker+1]==' '||text[textboxMarker+1]=='\n'){
lastWord="";
lastWordMarker=textboxMarker+2;
} else {
lastWord+=text[textboxMarker+1];
}
displayText+=text[textboxMarker+1];
textboxMarker++;
}
lastLetterTime=letterDisplayDelay;
}
}
void Textbox::Draw(PixelGameEngine*pge,Resources&resources){
if(visible){
geom2d::rect<float>boxRect={pos-vf2d{2,2},maxSize+vf2d{4,4}};
if(boxRect.bottom().start.y>=pge->ScreenHeight()){
boxRect.pos-={0,boxRect.bottom().start.y-pge->ScreenHeight()};
}
if(boxRect.right().start.x>=pge->ScreenWidth()){
boxRect.pos-={boxRect.right().start.x-pge->ScreenWidth(),0};
}
if(boxRect.top().start.y<0){
boxRect.pos+={0,-boxRect.top().start.y};
}
if(boxRect.left().start.x<0){
boxRect.pos+={-boxRect.left().start.x,0};
}
pge->FillRectDecal(boxRect.pos,maxSize+vf2d{4,4},VERY_DARK_GREEN);
pge->DrawRectDecal(boxRect.pos+vf2d{1,1},maxSize+vf2d{3,3},WHITE);
pge->DrawShadowStringPropDecal(boxRect.pos+vf2d{3,3},displayText,{220,220,220});
}
}
void Textbox::UpdatePosition(vf2d newPos){
pos=newPos;
}
std::string&Textbox::GetCurrentString(){
return text;
}
void Textbox::SetVisible(bool visible){
this->visible=visible;
if(!visible){
SetDefaults();
}
}
void Textbox::UpdateAndDraw(vf2d pos,PixelGameEngine*pge,Resources&resources){
UpdatePosition(pos);
Update(pge);
Draw(pge,resources);
}
vf2d Textbox::GetSize(){
return maxSize;
}