From 6a2ccc4745baaea81748b0173da0cd54a92fac3f Mon Sep 17 00:00:00 2001 From: J-D-K Date: Mon, 11 Jun 2018 19:04:00 -0400 Subject: [PATCH] Add buggy text wrapping --- inc/gfx.h | 1 + inc/util.h | 3 +++ src/data.cpp | 2 +- src/file.cpp | 3 +++ src/gfx.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- src/ui.cpp | 10 ++++++---- src/util.cpp | 28 +++++++++++++++++++++++++++- 7 files changed, 82 insertions(+), 7 deletions(-) diff --git a/inc/gfx.h b/inc/gfx.h index 251600d..872a0a6 100644 --- a/inc/gfx.h +++ b/inc/gfx.h @@ -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 diff --git a/inc/util.h b/inc/util.h index 36d4124..c55da6a 100644 --- a/inc/util.h +++ b/inc/util.h @@ -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 diff --git a/src/data.cpp b/src/data.cpp index f633188..a8f5fe4 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -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++) diff --git a/src/file.cpp b/src/file.cpp index 0864d5e..06d9b05 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -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(); diff --git a/src/gfx.cpp b/src/gfx.cpp index 33cc633..5ec6825 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -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; diff --git a/src/ui.cpp b/src/ui.cpp index 23d77ac..7b3fe86 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -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); diff --git a/src/util.cpp b/src/util.cpp index 5b578b0..1c976b3 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -4,6 +4,7 @@ #include #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; + } }