simple scroll text parsing (tags are ignored until now)
parent
3c69d26571
commit
cd351ba3af
@ -0,0 +1,95 @@
|
||||
#include "formatting.hpp"
|
||||
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/property_tree/xml_parser.hpp>
|
||||
|
||||
using namespace MWGui;
|
||||
|
||||
MyGUI::IntSize BookTextParser::parse(std::string text, MyGUI::Widget* parent, const int width)
|
||||
{
|
||||
mParent = parent;
|
||||
mWidth = width;
|
||||
|
||||
assert(mParent);
|
||||
while (mParent->getChildCount())
|
||||
{
|
||||
MyGUI::Gui::getInstance().destroyWidget(mParent->getChildAt(0));
|
||||
}
|
||||
|
||||
boost::algorithm::replace_all(text, "<BR>", "\n");
|
||||
|
||||
// remove leading newlines
|
||||
//while (text[0] == '\n')
|
||||
// text.erase(0);
|
||||
|
||||
// remove trailing " and newlines
|
||||
if (text[text.size()-1] == '\"')
|
||||
text.erase(text.size()-1);
|
||||
while (text[text.size()-1] == '\n')
|
||||
text.erase(text.size()-1);
|
||||
|
||||
return parseSubText(text, -1, MyGUI::Align::Left | MyGUI::Align::Top);
|
||||
}
|
||||
|
||||
MyGUI::IntSize BookTextParser::parseSubText(std::string text, int textSize, MyGUI::Align textAlign)
|
||||
{
|
||||
MyGUI::IntSize size(mWidth,0);
|
||||
|
||||
bool tagFound = false;
|
||||
std::string realText; // real text, without tags
|
||||
for (unsigned int i=0; i<text.size(); ++i)
|
||||
{
|
||||
char c = text[i];
|
||||
if (c == '<')
|
||||
{
|
||||
if (text[i+1] == '/') // ignore closing tags
|
||||
{
|
||||
while (c != '>')
|
||||
{
|
||||
if (i >= text.size())
|
||||
throw std::runtime_error("BookTextParser Error: Tag is not terminated");
|
||||
++i;
|
||||
c = text[i];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
tagFound = true;
|
||||
while (c != '>')
|
||||
{
|
||||
if (i >= text.size())
|
||||
throw std::runtime_error("BookTextParser Error: Tag is not terminated");
|
||||
|
||||
c = text[++i];
|
||||
}
|
||||
++i;
|
||||
/// \todo parse tags
|
||||
size += parseSubText(text.substr(i, text.size()), textSize, textAlign);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
realText += c;
|
||||
}
|
||||
|
||||
if (!tagFound)
|
||||
{
|
||||
MyGUI::EditBox* box = mParent->createWidget<MyGUI::EditBox>("NormalText",
|
||||
MyGUI::IntCoord(0, size.height, mWidth, 24), MyGUI::Align::Left | MyGUI::Align::Top,
|
||||
mParent->getName() + boost::lexical_cast<std::string>(mParent->getChildCount()));
|
||||
box->setProperty("Static", "true");
|
||||
box->setProperty("MultiLine", "true");
|
||||
box->setProperty("WordWrap", "true");
|
||||
box->setProperty("NeedMouse", "false");
|
||||
box->setMaxTextLength(realText.size());
|
||||
box->setTextAlign(textAlign);
|
||||
box->setTextColour(MyGUI::Colour(0,0,0));
|
||||
box->setCaption(realText);
|
||||
box->setSize(box->getSize().width, box->getTextSize().height);
|
||||
size += MyGUI::IntSize(0, box->getTextSize().height);
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
#ifndef MWGUI_FORMATTING_H
|
||||
#define MWGUI_FORMATTING_H
|
||||
|
||||
#include <MyGUI.h>
|
||||
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
|
||||
namespace MWGui
|
||||
{
|
||||
/// \brief utilities for parsing book/scroll text as mygui widgets
|
||||
class BookTextParser
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Parse markup as MyGUI widgets
|
||||
* @param markup to parse
|
||||
* @param parent for the created widgets
|
||||
* @param maximum width
|
||||
* @return size of the created widgets
|
||||
*/
|
||||
MyGUI::IntSize parse(std::string text, MyGUI::Widget* parent, const int width);
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @param text to parse
|
||||
* @param text size (-1 means default)
|
||||
* @param text align
|
||||
* @return size of the created widgets
|
||||
*/
|
||||
MyGUI::IntSize parseSubText(std::string text, int textSize, MyGUI::Align textAlign);
|
||||
|
||||
private:
|
||||
MyGUI::Widget* mParent;
|
||||
int mWidth;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue