识别页面结尾的功能不会正确分割文本

时间:2016-02-20 20:45:02

标签: c++ text sfml

我试图用c ++和SFML编写一个简单的任务日志程序 - 比如RPG游戏中的任务日志。我陷入了负责在单个任务中显示所有条目的状态/功能。我希望它像电子书阅读器一样工作,这意味着在打开任务时,页面(和std :: vector)将填充所有条目,并且在任何给定时间只显示2个相邻的条目。首先,我必须设置一个功能,可以识别到达页面的水平末端,这可以正常工作

水平破坏的结果:

enter image description here

但是当我添加一个函数来识别某个条目何时到达页面的垂直末端时,就会发生这种情况

横向+垂直突破的结果:

enter image description here

这里是填写页面的函数的代码

if (!mPagesFilled)
{
    //make sure container of pages is not empty
    if(mPages.empty())
        mPages.push_back(Page());

    for (auto it : mViewedQuest->questEntries) //main loop - iterate through entries belonging to a single, selected quest
    {
        //if there is less than 100px left, add a new Page() to a vector mPages
        if (mActiveTheme->pageLayout[mLeftRight].height - mPages.back().pageSize.y < 100)
        {
            mPages.push_back(Page());
            if (mLeftRight == 0)
                mLeftRight = 1;
            else
                mLeftRight = 0;
        }

        //create and initialize the temporary PageEntry buffer object
        PageEntry entry;
        entry.header.setFont(mActiveTheme->themeFont);
        entry.header.setPosition(mActiveTheme->pageLayout[mLeftRight].left, mActiveTheme->pageLayout[mLeftRight].top + mPages.back().pageSize.y);
        entry.header.setString(it.getEntryDate());
        entry.header.setColor(sf::Color(50, 50, 50, 255));
        entry.entryHeight = entry.header.getLocalBounds().height + 20 ; //update entry height

        //set up the body of entry
        entry.body.setFont(mActiveTheme->themeFont);
        entry.body.setColor(sf::Color::Black);
        entry.body.setPosition(mActiveTheme->pageLayout[mLeftRight].left, mActiveTheme->pageLayout[mLeftRight].top + entry.entryHeight + mPages.back().pageSize.y);

        //clear the stream and the buffer string and put the iterated entry content into the stream
        ss.clear();
        mBuffer.clear();
        ss << it.entryText;

        while (ss) //loop for as long as there is something to be read from the stream
        {
            //check if we have reached the end of stream and break the loop if we did to avoid the garbage char at the end
            mBufferChar = ss.peek(); 
            if (mBufferChar == EOF)
                continue; // i know technically it should be break; but since it's EOF than it doesn't really matter i think

            //append the buffer string with the next char from the stream and update the body
            mBuffer += ss.get();
            entry.body.setString(mBuffer);

            //check if we have hit the right side of the page and add newline if we did
            if (entry.body.getLocalBounds().width > mActiveTheme->pageLayout[mLeftRight].width)
            {
                size_t it = mBuffer.find_last_of(" "); //check where was the last space in the string
                mBuffer.insert(it, "\n"); //put a newline there
                entry.body.setString(mBuffer); //update body string

                //after putting the newline update the height by one line height (represented by header height, always one line) and update pagesize
                entry.entryHeight += entry.header.getLocalBounds().height;
                mPages.back().pageSize.y += entry.entryHeight;


                //check if entry has reached the end of page
                if (mActiveTheme->pageLayout[mLeftRight].height - mPages.back().pageSize.y < 100)
                {
                    //if yes mark entry as unfinished, push it into the page, and reset the height of the buffer entry
                    entry.isFinished = false;
                    mPages.back().page.push_back(entry);
                    entry.entryHeight = 0;

                    //push a new page and toggle the pagemark
                    mPages.push_back(Page());
                    if (mLeftRight == 0)
                        mLeftRight = 1;
                    else
                        mLeftRight = 0;

                    //move the entry to the other page, clear the string buffer and the body string
                    entry.body.setPosition(mActiveTheme->pageLayout[mLeftRight].left, mActiveTheme->pageLayout[mLeftRight].top + entry.entryHeight + mPages.back().pageSize.y);
                    mBuffer.clear();
                    entry.body.setString("");
                    entry.header.setString("");
                }

            } //once entry passes these to if statements go on with the next stream chars until once again right side of the page is reached
        }

        //all stream chars read, mark entry as finished and push it to the page
        entry.isFinished = true;
        mPages.back().page.push_back(entry);

    }
    //all entries parsed, mark filled flag true
    mPagesFilled = true;
}
else
{ }

我怀疑我不得不弄乱一些东西,因为现在即使在评论出垂直断裂的部分后,结果与第二张图片非常相似,但似乎无法找到错误。快速分解:

  
      
  1. mActiveTheme-&gt; pageLayout [mLeftRight] - 这是sf :: IntRect的向量   指定所选主题中页面位置的对象   mLeftRight是一个int变量,用于跟踪当前是否为a   正在使用左页或右页

  2.   
  3. Entry对象是日期和std :: string的结构,mViewedQuest-&gt; questEntries是Entry对象的向量

  4.   
  5. mPages是PageEntry对象的向量,这些对象又是包含以下内容的结构

  6.   
PageEntry()
{

};

sf::Text header;   //header containing the date of an Entry
sf::Text body;     //entry text
int entryHeight;   //stores the current height of the object 
bool isFinished;   //indicator if an entry is split among pages or not
  
      
  1. ss是一个字符串流
  2.   
  3. pagePize in mPages.back()。pageSize是一个sf :: Vector2i,用于存储包含单个页面的所有PageEntry对象的高度。
  4.   

0 个答案:

没有答案