scroll window image tags working

This commit is contained in:
scrawl 2012-05-09 01:34:32 +02:00
parent cd351ba3af
commit 8e7ab09a25
4 changed files with 93 additions and 41 deletions

View file

@ -54,6 +54,8 @@ void BookWindow::onTakeButtonClicked (MyGUI::Widget* _sender)
MWWorld::ActionTake take(mBook); MWWorld::ActionTake take(mBook);
take.execute(); take.execute();
/// \todo what about scripts?
MWBase::Environment::get().getInputManager()->setGuiMode (GM_Game); MWBase::Environment::get().getInputManager()->setGuiMode (GM_Game);
} }

View file

@ -10,6 +10,7 @@ MyGUI::IntSize BookTextParser::parse(std::string text, MyGUI::Widget* parent, co
{ {
mParent = parent; mParent = parent;
mWidth = width; mWidth = width;
mHeight = 0;
assert(mParent); assert(mParent);
while (mParent->getChildCount()) while (mParent->getChildCount())
@ -29,16 +30,58 @@ MyGUI::IntSize BookTextParser::parse(std::string text, MyGUI::Widget* parent, co
while (text[text.size()-1] == '\n') while (text[text.size()-1] == '\n')
text.erase(text.size()-1); text.erase(text.size()-1);
return parseSubText(text, -1, MyGUI::Align::Left | MyGUI::Align::Top); parseSubText(text);
return MyGUI::IntSize(mWidth, mHeight);
} }
MyGUI::IntSize BookTextParser::parseSubText(std::string text, int textSize, MyGUI::Align textAlign) void BookTextParser::parseImage(std::string tag)
{ {
MyGUI::IntSize size(mWidth,0); int src_start = tag.find("SRC=")+5;
std::string image = tag.substr(src_start, tag.find('"', src_start)-src_start);
// fix texture extension to .dds
if (image.size() > 4)
{
image[image.size()-3] = 'd';
image[image.size()-2] = 'd';
image[image.size()-1] = 's';
}
int width_start = tag.find("WIDTH=")+7;
int width = boost::lexical_cast<int>(tag.substr(width_start, tag.find('"', width_start)-width_start));
int height_start = tag.find("HEIGHT=")+8;
int height = boost::lexical_cast<int>(tag.substr(height_start, tag.find('"', height_start)-height_start));
MyGUI::ImageBox* box = mParent->createWidget<MyGUI::ImageBox> ("ImageBox",
MyGUI::IntCoord(0, mHeight, width, height), MyGUI::Align::Left | MyGUI::Align::Top,
mParent->getName() + boost::lexical_cast<std::string>(mParent->getChildCount()));
box->setImageTexture("bookart\\" + image);
box->setProperty("NeedMouse", "false");
mWidth = std::max(mWidth, width);
mHeight += height;
}
void BookTextParser::parseSubText(std::string text)
{
if (text[0] == '<')
{
if (text.find('>') == std::string::npos)
throw std::runtime_error("BookTextParser Error: Tag is not terminated");
if (text.size() > 4 && text.substr(0, 4) == "<IMG")
{
parseImage(text.substr(0, text.find('>')));
}
text.erase(0, text.find('>')+1);
}
bool tagFound = false; bool tagFound = false;
std::string realText; // real text, without tags std::string realText; // real text, without tags
for (unsigned int i=0; i<text.size(); ++i) unsigned int i=0;
for (; i<text.size(); ++i)
{ {
char c = text[i]; char c = text[i];
if (c == '<') if (c == '<')
@ -57,16 +100,6 @@ MyGUI::IntSize BookTextParser::parseSubText(std::string text, int textSize, MyGU
else else
{ {
tagFound = true; 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; break;
} }
} }
@ -74,22 +107,23 @@ MyGUI::IntSize BookTextParser::parseSubText(std::string text, int textSize, MyGU
realText += c; realText += c;
} }
if (!tagFound)
{
MyGUI::EditBox* box = mParent->createWidget<MyGUI::EditBox>("NormalText", MyGUI::EditBox* box = mParent->createWidget<MyGUI::EditBox>("NormalText",
MyGUI::IntCoord(0, size.height, mWidth, 24), MyGUI::Align::Left | MyGUI::Align::Top, MyGUI::IntCoord(0, mHeight, mWidth, 24), MyGUI::Align::Left | MyGUI::Align::Top,
mParent->getName() + boost::lexical_cast<std::string>(mParent->getChildCount())); mParent->getName() + boost::lexical_cast<std::string>(mParent->getChildCount()));
box->setProperty("Static", "true"); box->setProperty("Static", "true");
box->setProperty("MultiLine", "true"); box->setProperty("MultiLine", "true");
box->setProperty("WordWrap", "true"); box->setProperty("WordWrap", "true");
box->setProperty("NeedMouse", "false"); box->setProperty("NeedMouse", "false");
box->setMaxTextLength(realText.size()); box->setMaxTextLength(realText.size());
box->setTextAlign(textAlign); box->setTextAlign(mTextStyle.mTextAlign);
box->setTextColour(MyGUI::Colour(0,0,0)); box->setTextColour(mTextStyle.mColour);
box->setFontName(mTextStyle.mFont);
box->setCaption(realText); box->setCaption(realText);
box->setSize(box->getSize().width, box->getTextSize().height); box->setSize(box->getSize().width, box->getTextSize().height);
size += MyGUI::IntSize(0, box->getTextSize().height); mHeight += box->getTextSize().height;
}
return size; if (tagFound)
{
parseSubText(text.substr(i, text.size()));
}
} }

View file

@ -7,6 +7,22 @@
namespace MWGui namespace MWGui
{ {
struct TextStyle
{
TextStyle() :
mColour(0,0,0)
, mFont("Default")
, mTextSize(16)
, mTextAlign(MyGUI::Align::Left | MyGUI::Align::Top)
{
}
MyGUI::Colour mColour;
std::string mFont;
int mTextSize;
MyGUI::Align mTextAlign;
};
/// \brief utilities for parsing book/scroll text as mygui widgets /// \brief utilities for parsing book/scroll text as mygui widgets
class BookTextParser class BookTextParser
{ {
@ -21,17 +37,15 @@ namespace MWGui
MyGUI::IntSize parse(std::string text, MyGUI::Widget* parent, const int width); MyGUI::IntSize parse(std::string text, MyGUI::Widget* parent, const int width);
protected: protected:
/** void parseSubText(std::string text);
* @param text to parse
* @param text size (-1 means default) void parseImage(std::string tag);
* @param text align
* @return size of the created widgets
*/
MyGUI::IntSize parseSubText(std::string text, int textSize, MyGUI::Align textAlign);
private: private:
MyGUI::Widget* mParent; MyGUI::Widget* mParent;
int mWidth; int mWidth; // maximum width
int mHeight; // current height
TextStyle mTextStyle;
}; };
} }

View file

@ -36,9 +36,9 @@ void ScrollWindow::open (MWWorld::Ptr scroll)
MyGUI::IntSize size = parser.parse(ref->base->text, mTextView, 390); MyGUI::IntSize size = parser.parse(ref->base->text, mTextView, 390);
if (size.height > mTextView->getSize().height) if (size.height > mTextView->getSize().height)
mTextView->setCanvasSize(size); mTextView->setCanvasSize(MyGUI::IntSize(410, size.height));
else else
mTextView->setCanvasSize(390, mTextView->getSize().height); mTextView->setCanvasSize(410, mTextView->getSize().height);
mTextView->setViewOffset(MyGUI::IntPoint(0,0)); mTextView->setViewOffset(MyGUI::IntPoint(0,0));
} }
@ -57,5 +57,7 @@ void ScrollWindow::onTakeButtonClicked (MyGUI::Widget* _sender)
MWWorld::ActionTake take(mScroll); MWWorld::ActionTake take(mScroll);
take.execute(); take.execute();
/// \todo what about scripts?
MWBase::Environment::get().getInputManager()->setGuiMode (GM_Game); MWBase::Environment::get().getInputManager()->setGuiMode (GM_Game);
} }