Separate GetTextSize and GetWrappedTextSize functions for clarity. Fix sizing comparisons for wrapping text. Fixed issue with menu destinations not properly being assigned on certain buttons.

This commit is contained in:
2023-12-21 10:26:18 -06:00
parent d29f7d47bd
commit a26d86d0b0
11 changed files with 99 additions and 40 deletions
+75 -18
View File
@@ -1120,8 +1120,10 @@ namespace olc
void DrawShadowString(const olc::vi2d& pos, const std::string& sText, Pixel col = olc::WHITE, const Pixel shadowCol = olc::BLACK, const olc::vf2d& scale = { 1.0f, 1.0f },const float shadowSizeFactor=1);
void DrawStringProp(int32_t x, int32_t y, const std::string& sText, Pixel col = olc::WHITE, uint32_t scale = 1);
void DrawShadowStringProp(const olc::vi2d& pos, const std::string& sText, Pixel col = olc::WHITE, const Pixel shadowCol = olc::BLACK, const olc::vf2d& scale = { 1.0f, 1.0f },const float shadowSizeFactor=1);
olc::vi2d GetTextSize(std::string_view s,const int width=std::numeric_limits<int>::max());
olc::vi2d GetTextSizeProp(std::string_view s,const int width=std::numeric_limits<int>::max());
olc::vi2d GetTextSize(std::string_view s);
olc::vi2d GetTextSizeProp(std::string_view s);
olc::vi2d GetWrappedTextSize(std::string_view s,const float width=std::numeric_limits<int>::max(),const float scale=1);
olc::vi2d GetWrappedTextSizeProp(std::string_view s,const float width=std::numeric_limits<int>::max(),const float scale=1);
void DrawString(const olc::vi2d& pos, const std::string& sText, Pixel col = olc::WHITE, uint32_t scale = 1);
void DrawString(int32_t x, int32_t y, const std::string& sText, Pixel col = olc::WHITE, uint32_t scale = 1);
@@ -3431,7 +3433,7 @@ namespace olc
int32_t ox = (c - 32) % 16;
int32_t oy = (c - 32) / 16;
planningMarker.x += 8.0f * scale.x;
if(planningMarker.x>=width){
if(planningMarker.x>width){
if(drawingMarker.x==0){ //The text has overflowed a full line, so we have to dump the line.
for(PixelGameEngine::StringDecalData&letter:letters){
DrawPartialDecal(pos + drawingMarker, fontRenderable.Decal(), letter.sourcePos, letter.sourceSize, scale, letter.col);
@@ -3536,7 +3538,7 @@ namespace olc
int32_t ox = (c - 32) % 16;
int32_t oy = (c - 32) / 16;
planningMarker.x += float(vFontSpacing[c - 32].y) * scale.x;
if(planningMarker.x>=width){
if(planningMarker.x>width){
if(drawingMarker.x==0){ //The text has overflowed a full line, so we have to dump the line.
for(PixelGameEngine::StringDecalData&letter:letters){
DrawPartialDecal(pos + drawingMarker, GetFontDecal(), letter.sourcePos, letter.sourceSize, scale, letter.col);
@@ -3764,10 +3766,11 @@ namespace olc
DrawRotatedStringPropDecal(pos, sText,fAngle,center,col,scale);
}
olc::vi2d PixelGameEngine::GetTextSize(std::string_view s,const int width)
olc::vi2d PixelGameEngine::GetWrappedTextSize(std::string_view s,const float width,const float scale)
{
float adjustedWidth=width/scale;
int lettersWidth=0;
int maxWidth=0;
float maxWidth=0;
bool wrappingOccurred=false;
olc::vf2d planningMarker = { 0.0f, 0.0f };
olc::vf2d drawingMarker = { 0.0f, 0.0f };
@@ -3786,7 +3789,7 @@ namespace olc
if(wrappingOccurred){
if(c!=' '&&c!='\t'){
wrappingOccurred=false;
maxWidth=std::max(maxWidth,int(drawingMarker.x));
maxWidth=std::max(maxWidth,drawingMarker.x);
}else{
continue;
}
@@ -3817,11 +3820,11 @@ namespace olc
else
{
planningMarker.x += 8.0f;
if(planningMarker.x>=width){
if(planningMarker.x>adjustedWidth){
if(drawingMarker.x==0){ //The text has overflowed a full line, so we have to dump the line.
drawingMarker.x+=lettersWidth;
lettersWidth=0;
maxWidth=std::max(maxWidth,int(drawingMarker.x));
maxWidth=std::max(maxWidth,drawingMarker.x);
}
planningMarker.x = 0; planningMarker.y += 8.0f;
drawingMarker=planningMarker;
@@ -3832,8 +3835,33 @@ namespace olc
}
}
drawingMarker.x += lettersWidth;
maxWidth=std::max(maxWidth,int(drawingMarker.x));
return {maxWidth,int(planningMarker.y+8)};
return vi2d(vf2d{maxWidth,planningMarker.y+8}*scale);
}
olc::vi2d PixelGameEngine::GetTextSize(std::string_view s)
{
olc::vi2d size = { 0,1 };
olc::vi2d pos = { 0,1 };
for (int skip=0;auto c : s)
{
if(skip){
skip--;
continue;
}
if(c=='\r')continue; //Ignore carriage returns. Stupid Linux.
if (c == '\n') { pos.y++; pos.x = 0; }
else if (c == '\t') { pos.x += nTabSizeInSpaces; }
else if(c<0)continue;
else if (c=='#')
{
skip=6;
continue;
}
else pos.x++;
size.x = std::max(size.x, pos.x);
size.y = std::max(size.y, pos.y);
}
return size * 8;
}
void PixelGameEngine::DrawString(const olc::vi2d& pos, const std::string& sText, Pixel col, uint32_t scale)
@@ -3925,10 +3953,39 @@ namespace olc
SetPixelMode(m);
}
olc::vi2d PixelGameEngine::GetTextSizeProp(std::string_view s,const int width)
olc::vi2d PixelGameEngine::GetTextSizeProp(std::string_view s)
{
olc::vi2d size = { 0,1 };
olc::vi2d pos = { 0,1 };
for (int skip=0;auto c : s)
{
if(skip){
skip--;
continue;
}
if(c=='\r')continue; //Ignore carriage returns. Stupid Linux.
if (c == '\n') { pos.y += 1; pos.x = 0; }
else if (c == '\t') { pos.x += nTabSizeInSpaces * 8; }
else if(c<0)continue;
else if (c=='#')
{
skip=6;
continue;
}
else pos.x += vFontSpacing[c - 32].y;
size.x = std::max(size.x, pos.x);
size.y = std::max(size.y, pos.y);
}
size.y *= 8;
return size;
}
olc::vi2d PixelGameEngine::GetWrappedTextSizeProp(std::string_view s,const float width,const float scale)
{
float adjustedWidth=width/scale;
int lettersWidth=0;
int maxWidth=0;
float maxWidth=0;
bool wrappingOccurred=false;
olc::vf2d planningMarker = { 0.0f, 0.0f };
olc::vf2d drawingMarker = { 0.0f, 0.0f };
@@ -3943,7 +4000,7 @@ namespace olc
drawingMarker.x+=lettersWidth;
lettersWidth=0;
wrappingOccurred=false;
maxWidth=std::max(maxWidth,int(drawingMarker.x));
maxWidth=std::max(maxWidth,drawingMarker.x);
}
if(wrappingOccurred){
if(c!=' '&&c!='\t'){
@@ -3978,11 +4035,11 @@ namespace olc
else
{
planningMarker.x += vFontSpacing[c-32].y;
if(planningMarker.x>=width){
if(planningMarker.x>adjustedWidth){
if(drawingMarker.x==0){ //The text has overflowed a full line, so we have to dump the line.
drawingMarker.x+=lettersWidth;
lettersWidth=0;
maxWidth=std::max(maxWidth,int(drawingMarker.x));
maxWidth=std::max(maxWidth,drawingMarker.x);
}
planningMarker.x = 0; planningMarker.y += 8.0f;
drawingMarker=planningMarker;
@@ -3993,8 +4050,8 @@ namespace olc
}
}
drawingMarker.x += lettersWidth;
maxWidth=std::max(maxWidth,int(drawingMarker.x));
return {maxWidth,int(planningMarker.y+8)};
maxWidth=std::max(maxWidth,drawingMarker.x);
return vi2d(vf2d{maxWidth,planningMarker.y+8}*scale);
}
void PixelGameEngine::DrawStringProp(const olc::vi2d& pos, const std::string& sText, Pixel col, uint32_t scale)