parent
949a1fe9f3
commit
03cd07f6b8
@ -0,0 +1,82 @@ |
|||||||
|
#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){ |
||||||
|
vf2d finalPos=pos; |
||||||
|
geom2d::rect<float>boxRect={pos-vf2d{2,2},maxSize+vf2d{4,4}}; |
||||||
|
pge->FillRectDecal(pos-vf2d{2,2},maxSize+vf2d{4,4},VERY_DARK_GREEN); |
||||||
|
pge->DrawRectDecal(pos-vf2d{1,1},maxSize+vf2d{3,3},WHITE); |
||||||
|
pge->DrawShadowStringPropDecal(pos+vf2d{1,1},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); |
||||||
|
} |
@ -0,0 +1,32 @@ |
|||||||
|
#pragma once |
||||||
|
#include "olcPixelGameEngine.h" |
||||||
|
#include "Unit.h" |
||||||
|
#include "olcPGEX_TransformedView.h" |
||||||
|
#include "Resources.h" |
||||||
|
|
||||||
|
class Textbox{ |
||||||
|
std::string headerText=""; //If a textbox has a header, it displays at the top in a special color.
|
||||||
|
std::string text=""; |
||||||
|
std::string displayText=""; |
||||||
|
vf2d pos={}; |
||||||
|
vf2d maxSize={}; |
||||||
|
float lastLetterTime=0; |
||||||
|
float letterDisplayDelay=0.01; |
||||||
|
int textboxMarker=-1; |
||||||
|
int lastWordMarker=-1; |
||||||
|
std::string lastWord=""; |
||||||
|
std::vector<Memory> resourceCost; //If resource cost is greater than 0, shows them.
|
||||||
|
bool visible=true; |
||||||
|
public: |
||||||
|
Textbox(); |
||||||
|
void Initialize(std::string text,vf2d pos,std::string headerText="",vf2d maxSize={120,64},std::vector<Memory>resourceCost={},float letterDisplayDelay=0.01); |
||||||
|
|
||||||
|
void UpdateAndDraw(vf2d pos,PixelGameEngine*pge,Resources&resources); |
||||||
|
std::string&GetCurrentString(); |
||||||
|
void SetVisible(bool visible); |
||||||
|
private: |
||||||
|
void Update(PixelGameEngine*pge); |
||||||
|
void UpdatePosition(vf2d newPos); |
||||||
|
void Draw(PixelGameEngine*pge,Resources&resources); |
||||||
|
void SetDefaults(); |
||||||
|
}; |
Loading…
Reference in new issue