@ -4,7 +4,7 @@
Textbox : : Textbox ( ) { } ;
Textbox : : Textbox ( ) { } ;
void Textbox : : Initialize ( std : : string text , vf2d pos , std : : string headerText , vf2d maxSize , std : : vector < Memory > resourceCost , float letterDisplayDelay )
void Textbox : : Initialize ( std : : string text , vf2d pos , std : : string headerText , Renderable * boxImg , vf2d maxSize , Audio * dialogSound , std : : vector < Memory > resourceCost , float letterDisplayDelay )
{
{
if ( GetCurrentString ( ) ! = text ) { //Make sure this is actually a new textbox
if ( GetCurrentString ( ) ! = text ) { //Make sure this is actually a new textbox
SetDefaults ( ) ;
SetDefaults ( ) ;
@ -13,6 +13,14 @@ void Textbox::Initialize(std::string text,vf2d pos,std::string headerText,vf2d m
this - > maxSize = maxSize ;
this - > maxSize = maxSize ;
this - > resourceCost = resourceCost ;
this - > resourceCost = resourceCost ;
this - > letterDisplayDelay = letterDisplayDelay ;
this - > letterDisplayDelay = letterDisplayDelay ;
this - > boxImg = boxImg ;
if ( this - > dialogSound ! = nullptr ) {
this - > dialogSound - > Stop ( soundHandle ) ;
}
if ( dialogSound ! = nullptr ) {
this - > dialogSound = dialogSound ;
soundHandle = this - > dialogSound - > PlayCentered ( 1 , audioVolume , true ) ;
}
visible = true ;
visible = true ;
}
}
}
}
@ -28,11 +36,20 @@ void Textbox::SetDefaults(){
displayHeaderText = " " ;
displayHeaderText = " " ;
text = " " ;
text = " " ;
headerText = " " ;
headerText = " " ;
audioVolume = 0 ;
continueWordTimer = 0 ;
}
}
void Textbox : : Update ( PixelGameEngine * pge ) {
void Textbox : : Update ( PixelGameEngine * pge ) {
if ( ! visible ) return ;
if ( ! visible ) {
if ( dialogSound ! = nullptr ) {
audioVolume = std : : max ( 0.f , audioVolume - pge - > GetElapsedTime ( ) * 2 ) ;
dialogSound - > SetVolume ( soundHandle , audioVolume ) ;
}
return ;
}
lastLetterTime - = pge - > GetElapsedTime ( ) ;
lastLetterTime - = pge - > GetElapsedTime ( ) ;
continueWordTimer + = pge - > GetElapsedTime ( ) ;
if ( lastLetterTime < = 0 ) {
if ( lastLetterTime < = 0 ) {
if ( textboxMarker < int ( text . length ( ) - 1 ) ) {
if ( textboxMarker < int ( text . length ( ) - 1 ) ) {
std : : string tempText = displayText ;
std : : string tempText = displayText ;
@ -61,6 +78,19 @@ void Textbox::Update(PixelGameEngine*pge){
WrapText ( tempHeaderText , headerText , displayHeaderText , lastHeaderWordMarker , lastHeaderWord ) ;
WrapText ( tempHeaderText , headerText , displayHeaderText , lastHeaderWordMarker , lastHeaderWord ) ;
}
}
textboxMarker + + ;
textboxMarker + + ;
if ( dialogSound ! = nullptr ) {
audioVolume = std : : min ( 1.f , audioVolume + 0.01f * 2 ) ;
dialogSound - > SetVolume ( soundHandle , audioVolume ) ;
audioFadeOutDelay = 2 ;
}
} else {
if ( dialogSound ! = nullptr ) {
audioFadeOutDelay = std : : max ( 0.f , audioFadeOutDelay - 0.01f ) ;
if ( audioFadeOutDelay < = 0 ) {
audioVolume = std : : max ( 0.f , audioVolume - 0.01f * 2 ) ;
dialogSound - > SetVolume ( soundHandle , audioVolume ) ;
}
}
}
}
maxSize . y = std : : max ( maxSize . y , float ( pge - > GetTextSizeProp ( displayHeaderText ) . y + pge - > GetTextSizeProp ( displayText ) . y ) ) ;
maxSize . y = std : : max ( maxSize . y , float ( pge - > GetTextSizeProp ( displayHeaderText ) . y + pge - > GetTextSizeProp ( displayText ) . y ) ) ;
lastLetterTime = letterDisplayDelay ;
lastLetterTime = letterDisplayDelay ;
@ -70,29 +100,46 @@ void Textbox::Update(PixelGameEngine*pge){
void Textbox : : Draw ( PixelGameEngine * pge , Resources & resources , std : : map < Image , std : : unique_ptr < Renderable > > & IMAGES , int totalUsedMemory , int memoryLimit ) {
void Textbox : : Draw ( PixelGameEngine * pge , Resources & resources , std : : map < Image , std : : unique_ptr < Renderable > > & IMAGES , int totalUsedMemory , int memoryLimit ) {
if ( visible ) {
if ( visible ) {
geom2d : : rect < float > box Rect = { pos - vf2d { 3 , 3 } , maxSize + vf2d { 6 , 6 } } ;
geom2d : : rect < float > bounding Rect = { pos - vf2d { 3 , 3 } , maxSize + vf2d { 6 , 6 } } ;
if ( resourceCost . size ( ) > 0 ) {
if ( resourceCost . size ( ) > 0 ) {
boxRect . size . x + = 36 ;
boundingRect . size . x + = 36 ;
boxRect . size . y = std : : max ( 36.f , boxRect . size . y ) ;
boundingRect . size . y = std : : max ( 36.f , boundingRect . size . y ) ;
}
if ( boxImg ! = nullptr ) {
boundingRect . size . x + = 26 ;
}
if ( boundingRect . bottom ( ) . start . y > = pge - > ScreenHeight ( ) ) {
boundingRect . pos - = { 0 , boundingRect . bottom ( ) . start . y - pge - > ScreenHeight ( ) } ;
}
if ( boundingRect . right ( ) . start . x > = pge - > ScreenWidth ( ) ) {
boundingRect . pos - = { boundingRect . right ( ) . start . x - pge - > ScreenWidth ( ) , 0 } ;
}
if ( boundingRect . top ( ) . start . y < 0 ) {
boundingRect . pos + = { 0 , - boundingRect . top ( ) . start . y } ;
}
}
if ( boxRect . bottom ( ) . start . y > = pge - > ScreenHeight ( ) ) {
if ( boundingRect . left ( ) . start . x < 0 ) {
boxRect . pos - = { 0 , boxRect . bottom ( ) . start . y - pge - > ScreenHeight ( ) } ;
boundingRect . pos + = { - boundingRect . left ( ) . start . x , 0 } ;
}
}
if ( boxRect . right ( ) . start . x > = pge - > ScreenWidth ( ) ) {
geom2d : : rect < float > textboxRect = boundingRect ;
boxRect . pos - = { boxRect . right ( ) . start . x - pge - > ScreenWidth ( ) , 0 } ;
if ( boxImg ! = nullptr ) {
textboxRect . pos . x + = 26 ;
textboxRect . size . x - = 50 ;
}
}
if ( boxRect . top ( ) . start . y < 0 ) {
pge - > FillRectDecal ( boundingRect . pos , maxSize + vf2d { 6 , 6 } , backCol ) ;
boxRect . pos + = { 0 , - boxRect . top ( ) . start . y } ;
pge - > DrawRectDecal ( boundingRect . pos + vf2d { 1 , 1 } , maxSize + vf2d { 4 , 4 } , WHITE ) ;
pge - > DrawDecal ( boundingRect . pos + vf2d { 3 , 3 } , IMAGES [ MATRIX ] - > Decal ( ) , { 0.375 , 0.375 } , DARK_GREY ) ;
if ( boxImg ! = nullptr ) {
pge - > DrawDecal ( boundingRect . pos + vf2d { 3 , 3 } , boxImg - > Decal ( ) ) ;
}
}
if ( boxRect . left ( ) . start . x < 0 ) {
pge - > DrawRectDecal ( boundingRect . pos + vf2d { 3 , 3 } , { 24 , 24 } , YELLOW ) ;
boxRect . pos + = { - boxRect . left ( ) . start . x , 0 } ;
pge - > DrawShadowStringPropDecal ( textboxRect . pos + vf2d { 3 , 3 } , displayHeaderText , { 245 , 218 , 66 } ) ;
pge - > DrawShadowStringPropDecal ( textboxRect . pos + vf2d { 3.f , float ( 3 + pge - > GetTextSizeProp ( displayHeaderText ) . y ) } , displayText , { 220 , 220 , 220 } ) ;
if ( textboxMarker = = int ( text . length ( ) - 1 ) ) {
std : : string continueText = " Click to Continue " ;
pge - > DrawShadowStringPropDecal ( textboxRect . pos + textboxRect . size - vf2d ( pge - > GetTextSizeProp ( continueText ) ) * vf2d { 0.6 , 1 } - vf2d { 5 , - 9 } , continueText , { 255 , 255 , 255 , uint8_t ( abs ( sin ( 2 * continueWordTimer ) ) * 255 ) } , { 0 , 0 , 0 , uint8_t ( abs ( sin ( 2 * continueWordTimer ) ) * 255 ) } , { 0.6 , 1 } ) ;
}
}
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 ) {
if ( resourceCost . size ( ) > 0 ) {
geom2d : : rect < float > resourceBoxRect = { boxRect . pos + vf2d { 6 + maxSize . x , 0 } , { 36 , 42 } } ;
geom2d : : rect < float > resourceBoxRect = { textboxRect . pos + vf2d { 6 + maxSize . x , 0 } , { 36 , 42 } } ;
pge - > FillRectDecal ( resourceBoxRect . pos , resourceBoxRect . size , backCol ) ;
pge - > FillRectDecal ( resourceBoxRect . pos , resourceBoxRect . size , backCol ) ;
pge - > DrawRectDecal ( resourceBoxRect . pos + vf2d { 1 , 1 } , resourceBoxRect . size - vf2d { 2 , 2 } , WHITE ) ;
pge - > DrawRectDecal ( resourceBoxRect . pos + vf2d { 1 , 1 } , resourceBoxRect . size - vf2d { 2 , 2 } , WHITE ) ;
vf2d contentPos = resourceBoxRect . pos + vf2d { 3 , 3 } ;
vf2d contentPos = resourceBoxRect . pos + vf2d { 3 , 3 } ;