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.
160 lines
5.5 KiB
160 lines
5.5 KiB
#include "Textbox.h"
|
|
#include "olcUTIL_Geometry2D.h"
|
|
#include "util.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="";
|
|
lastHeaderWordMarker=-1;
|
|
lastHeaderWord="";
|
|
displayText="";
|
|
displayHeaderText="";
|
|
text="";
|
|
headerText="";
|
|
}
|
|
|
|
void Textbox::Update(PixelGameEngine*pge){
|
|
if(!visible)return;
|
|
lastLetterTime-=pge->GetElapsedTime();
|
|
if(lastLetterTime<=0){
|
|
if(textboxMarker<int(text.length()-1)){
|
|
std::string tempText=displayText;
|
|
tempText+=text[textboxMarker+1];
|
|
|
|
auto WrapText=[&](std::string&tempText,std::string&text,std::string&displayText,int&lastWordMarker,std::string&lastWord){
|
|
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];
|
|
};
|
|
|
|
WrapText(tempText,text,displayText,lastWordMarker,lastWord);
|
|
|
|
if(textboxMarker<int(headerText.length()-1)){
|
|
std::string tempHeaderText=displayHeaderText;
|
|
tempHeaderText+=headerText[textboxMarker+1];
|
|
WrapText(tempHeaderText,headerText,displayHeaderText,lastHeaderWordMarker,lastHeaderWord);
|
|
}
|
|
textboxMarker++;
|
|
}
|
|
maxSize.y=std::max(maxSize.y,float(pge->GetTextSizeProp(displayHeaderText).y+pge->GetTextSizeProp(displayText).y));
|
|
lastLetterTime=letterDisplayDelay;
|
|
}
|
|
}
|
|
|
|
|
|
void Textbox::Draw(PixelGameEngine*pge,Resources&resources,std::map<Image,std::unique_ptr<Renderable>>&IMAGES){
|
|
if(visible){
|
|
geom2d::rect<float>boxRect={pos-vf2d{3,3},maxSize+vf2d{6,6}};
|
|
if(resourceCost.size()>0){
|
|
boxRect.size.x+=24;
|
|
boxRect.size.y=std::max(36.f,boxRect.size.y);
|
|
}
|
|
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{6,6},backCol);
|
|
pge->DrawRectDecal(boxRect.pos+vf2d{1,1},maxSize+vf2d{4,4},WHITE);
|
|
pge->DrawShadowStringPropDecal(boxRect.pos+vf2d{3,3},displayHeaderText,{245, 218, 66});
|
|
pge->DrawShadowStringPropDecal(boxRect.pos+vf2d{3.f,float(3+pge->GetTextSizeProp(displayHeaderText).y)},displayText,{220,220,220});
|
|
if(resourceCost.size()>0){
|
|
geom2d::rect<float>resourceBoxRect={boxRect.pos+vf2d{6+maxSize.x,6},{36,36}};
|
|
pge->FillRectDecal(resourceBoxRect.pos,resourceBoxRect.size,backCol);
|
|
pge->DrawRectDecal(resourceBoxRect.pos+vf2d{1,1},resourceBoxRect.size-vf2d{2,2},WHITE);
|
|
vf2d contentPos=resourceBoxRect.pos+vf2d{3,3};
|
|
|
|
Pixel drawcol=GREY,shadowCol={66, 164, 245};
|
|
|
|
auto DrawResourceAmount=[&](int index,int cost,int resourceAmt){
|
|
drawcol=resourceAmt>=cost?GREY:RED;
|
|
shadowCol=drawcol==GREY?VERY_DARK_BLUE:VERY_DARK_RED;
|
|
vf2d textScale = {0.4,0.4};
|
|
std::string displayString = std::to_string(resourceAmt)+" / ";
|
|
if(resourceAmt<cost){
|
|
pge->FillRectDecal(contentPos+vf2d{0,float(index*6)},vf2d{resourceBoxRect.size.x-6,6},{160,0,0,192});
|
|
}
|
|
pge->DrawShadowStringDecal(contentPos+vf2d{12.f,float(index*6)+1},displayString,drawcol,shadowCol,textScale,0.6);
|
|
pge->DrawShadowStringDecal(contentPos+vf2d{12.f+pge->GetTextSize(displayString).x*textScale.x+2,float(index*6)+1},std::to_string(cost),drawcol,shadowCol,{0.4,0.4},0.6);
|
|
};
|
|
|
|
DrawResourceAmount(0,util::GetHealthCost(resourceCost),resources.health);
|
|
DrawResourceAmount(1,util::GetAtkSpdCost(resourceCost),resources.atkSpd);
|
|
DrawResourceAmount(2,util::GetMoveSpdCost(resourceCost),resources.moveSpd);
|
|
DrawResourceAmount(3,util::GetRangeCost(resourceCost),resources.range);
|
|
DrawResourceAmount(4,util::GetProcedureCost(resourceCost),resources.procedure);
|
|
|
|
pge->DrawDecal(contentPos+vf2d{0,0*6},IMAGES[RESOURCE]->Decal(),{0.5,0.5},CONSTANT::HEALTH_COLOR);
|
|
pge->DrawDecal(contentPos+vf2d{0,1*6},IMAGES[RESOURCE]->Decal(),{0.5,0.5},CONSTANT::ATKSPD_COLOR);
|
|
pge->DrawDecal(contentPos+vf2d{0,2*6},IMAGES[RESOURCE]->Decal(),{0.5,0.5},CONSTANT::MOVESPD_COLOR);
|
|
pge->DrawDecal(contentPos+vf2d{0,3*6},IMAGES[RESOURCE]->Decal(),{0.5,0.5},CONSTANT::RANGE_COLOR);
|
|
pge->DrawDecal(contentPos+vf2d{0,4*6},IMAGES[RESOURCE]->Decal(),{0.5,0.5},CONSTANT::PROCEDURE_COLOR);
|
|
}
|
|
}
|
|
}
|
|
|
|
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,std::map<Image,std::unique_ptr<Renderable>>&IMAGES){
|
|
UpdatePosition(pos);
|
|
Update(pge);
|
|
Draw(pge,resources,IMAGES);
|
|
}
|
|
|
|
vf2d Textbox::GetSize(){
|
|
return maxSize;
|
|
}
|
|
|
|
void Textbox::SetBackgroundColor(Pixel col){
|
|
backCol=col;
|
|
}
|
|
|
|
bool Textbox::IsVisible(){
|
|
return visible;
|
|
} |