Add buggy text wrapping

This commit is contained in:
J-D-K
2018-06-11 19:04:00 -04:00
parent 5a0808ab01
commit 6a2ccc4745
7 changed files with 82 additions and 7 deletions

View File

@@ -13,6 +13,7 @@ namespace gfx
void clearConsoleColor(const uint32_t& clr);
void drawText(const std::string& str, unsigned x, unsigned y, const uint32_t& sz, const uint32_t& clr);
unsigned getTextWidth(const std::string& str, const uint32_t& sz);
void drawRectangle(uint32_t x, uint32_t y, const uint32_t& width, const uint32_t& height, const uint32_t& clr);
class tex

View File

@@ -8,5 +8,8 @@ namespace util
std::string getDateTime();
void makeTitleDir(data::user& u, data::titledata& t);
std::string getTitleDir(data::user& u, data::titledata& t);
//Just returns string with '\n' inserted
std::string getWrappedString(const std::string& s, const unsigned& sz, const unsigned& maxWidth);
}
#endif // UTIL_H

View File

@@ -24,7 +24,7 @@ bool isVerboten(char t)
return false;
}
const std::string safeString(const std::string& s)
std::string safeString(const std::string& s)
{
std::string ret = "";
for(unsigned i = 0; i < s.length(); i++)

View File

@@ -7,6 +7,7 @@
#include "data.h"
#include "gfx.h"
#include "ui.h"
#include "util.h"
#define BUFF_SIZE 512 * 1024
@@ -90,6 +91,7 @@ namespace fs
ui::progBar prog(fileSize);
std::string copyString = "Copying " + from + "...";
copyString = util::getWrappedString(copyString, 64, 1136);
for(unsigned i = 0; i < fileSize; )
{
hidScanInput();
@@ -129,6 +131,7 @@ namespace fs
ui::progBar prog(fileSize);
std::string copyString = "Copying " + from + "...";
copyString = util::getWrappedString(copyString, 64, 1136);
for(unsigned i = 0; i < fileSize; )
{
hidScanInput();

View File

@@ -95,6 +95,7 @@ namespace gfx
gfxWaitForVsync();
}
//switch-portlibs examples.
void drawGlyph(FT_Bitmap& bmp, uint32_t *frameBuf, unsigned x, unsigned y, const uint32_t& clr)
{
uint32_t frameX, frameY, tmpX, tmpY;
@@ -126,6 +127,7 @@ namespace gfx
void drawText(const std::string& str, unsigned x, unsigned y, const uint32_t& sz, const uint32_t& clr)
{
//This offset needs to be fixed better
y = y + 27;
uint32_t tmpX = x;
FT_Error ret = 0;
@@ -138,7 +140,7 @@ namespace gfx
uint32_t *frameBuffer = (uint32_t *)gfxGetFramebuffer(&frameBufWidth, NULL);
uint8_t tmpStr[512];
uint8_t tmpStr[1024];
sprintf((char *)tmpStr, "%s", str.c_str());
for(unsigned i = 0; i < str.length(); )
@@ -171,6 +173,44 @@ namespace gfx
}
}
unsigned getTextWidth(const std::string& str, const uint32_t& sz)
{
unsigned width = 0;
uint32_t unitCount = 0, tmpChr = 0;
FT_UInt glyphIndex = 0;
FT_GlyphSlot slot = face->glyph;
FT_Error ret = 0;
uint8_t tmpstr[1024];
sprintf((char *)tmpstr, "%s", str.c_str());
FT_Set_Char_Size(face, 0, 8 * sz, 300, 300);
for(unsigned i = 0; i < str.length(); )
{
unitCount = decode_utf8(&tmpChr, &tmpstr[i]);
if(unitCount <= 0)
break;
i += unitCount;
glyphIndex = FT_Get_Char_Index(face, tmpChr);
ret = FT_Load_Glyph(face, glyphIndex, FT_LOAD_DEFAULT);
if(ret == 0)
{
ret = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
}
if(ret)
return 0;
width += slot->advance.x >> 6;
}
return width;
}
void clearConsoleColor(const uint32_t& clr)
{
uint8_t r, g, b;

View File

@@ -159,10 +159,10 @@ namespace ui
gfx::drawRectangle(96, 400, 1088, 64, 0x000000FF);
gfx::drawRectangle(96, 400, (uint32_t)width, 64, 0x00CC00FF);
char tmp[64];
sprintf(tmp, "%u / %u", (unsigned)prog, (unsigned)max);
//char tmp[64];
//sprintf(tmp, "%u / %u", (unsigned)prog, (unsigned)max);
gfx::drawText(text, 80, 256, 64, 0x000000FF);
gfx::drawText(tmp, 80, 320, 64, 0x000000FF);
//gfx::drawText(tmp, 80, 320, 64, 0x000000FF);
}
key::key(const std::string& txt, const char& _let, const unsigned& _txtSz, const unsigned& _x, const unsigned& _y, const unsigned& _w, const unsigned& _h)
@@ -266,7 +266,7 @@ namespace ui
//spc key
key shift("Shift", ' ', 64, 16, 544, 128, 80);
key space("Spc", ' ', 64, 240, 640, 800, 80);
key space(" ", ' ', 64, 240, 640, 800, 80);
key bckSpc("Back", ' ', 64, 1120, 256, 128, 80);
key enter("Entr", ' ', 64, 1120, 352, 128, 80);
key cancel("Cancel", ' ', 64, 1120, 448, 128, 80);
@@ -285,6 +285,8 @@ namespace ui
void keyboard::draw()
{
titleMenu.print(16, 88, 364);
folderMenu.print(390, 88, 874);
gfx::drawRectangle(0, 176, 1280, 64, 0xFFFFFFFF);
gfx::drawRectangle(0, 240, 1280, 480, 0x3B3B3BFF);

View File

@@ -4,6 +4,7 @@
#include <sys/stat.h>
#include "data.h"
#include "gfx.h"
namespace util
{
@@ -26,8 +27,33 @@ namespace util
mkdir(path.c_str(), 777);
}
const std::string getTitleDir(data::user& u, data::titledata& t)
std::string getTitleDir(data::user& u, data::titledata& t)
{
return std::string(u.getUsername() + "/" + t.getTitleSafe() + "/");
}
std::string getWrappedString(const std::string& s, const unsigned& sz, const unsigned& maxWidth)
{
if(gfx::getTextWidth(s, sz) < maxWidth)
return s;
std::string ret = "", tmp = "";
unsigned first = 0, lastSpace = 0;
for(unsigned i = 0; i < s.length(); i++)
{
tmp += s[i];
if(s[i] == ' ')
lastSpace = i;
if(gfx::getTextWidth(tmp, sz) > maxWidth)
{
tmp.assign(s, first, lastSpace);
first = lastSpace + 1;
ret += tmp + "\n";
}
}
return ret;
}
}