1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-21 04:39:42 +00:00

simple scroll text parsing (tags are ignored until now)

This commit is contained in:
scrawl 2012-05-08 00:39:52 +02:00
parent 3c69d26571
commit cd351ba3af
7 changed files with 160 additions and 9 deletions

View file

@ -26,6 +26,7 @@ add_openmw_dir (mwgui
layouts text_input widgets race class birth review window_manager console dialogue
dialogue_history window_base stats_window messagebox journalwindow charactercreation
map_window window_pinnable_base cursorreplace tooltips scrollwindow bookwindow list
formatting
)
add_openmw_dir (mwdialogue

View file

@ -1,5 +1,7 @@
#include "bookwindow.hpp"
#include "formatting.hpp"
#include "../mwbase/environment.hpp"
#include "../mwinput/inputmanager.hpp"
#include "../mwsound/soundmanager.hpp"
@ -30,6 +32,12 @@ void BookWindow::open (MWWorld::Ptr book)
MWBase::Environment::get().getSoundManager()->playSound3D (book, "book open", 1.0, 1.0);
mBook = book;
ESMS::LiveCellRef<ESM::Book, MWWorld::RefData> *ref =
mBook.get<ESM::Book>();
//BookTextParser parser;
//parser.parse(ref->base->text, 0, 0);
}
void BookWindow::onCloseButtonClicked (MyGUI::Widget* _sender)

View file

@ -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;
}

View file

@ -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

View file

@ -1,5 +1,7 @@
#include "scrollwindow.hpp"
#include "formatting.hpp"
#include "../mwbase/environment.hpp"
#include "../mwinput/inputmanager.hpp"
#include "../mwworld/actiontake.hpp"
@ -10,6 +12,8 @@ using namespace MWGui;
ScrollWindow::ScrollWindow (WindowManager& parWindowManager) :
WindowBase("openmw_scroll_layout.xml", parWindowManager)
{
getWidget(mTextView, "TextView");
getWidget(mCloseButton, "CloseButton");
mCloseButton->eventMouseButtonClick += MyGUI::newDelegate(this, &ScrollWindow::onCloseButtonClicked);
@ -24,6 +28,19 @@ void ScrollWindow::open (MWWorld::Ptr scroll)
MWBase::Environment::get().getSoundManager()->playSound3D (scroll, "scroll", 1.0, 1.0);
mScroll = scroll;
ESMS::LiveCellRef<ESM::Book, MWWorld::RefData> *ref =
mScroll.get<ESM::Book>();
BookTextParser parser;
MyGUI::IntSize size = parser.parse(ref->base->text, mTextView, 390);
if (size.height > mTextView->getSize().height)
mTextView->setCanvasSize(size);
else
mTextView->setCanvasSize(390, mTextView->getSize().height);
mTextView->setViewOffset(MyGUI::IntPoint(0,0));
}
void ScrollWindow::onCloseButtonClicked (MyGUI::Widget* _sender)

View file

@ -20,6 +20,7 @@ namespace MWGui
private:
MyGUI::Button* mCloseButton;
MyGUI::Button* mTakeButton;
MyGUI::ScrollView* mTextView;
MWWorld::Ptr mScroll;
};

View file

@ -17,15 +17,6 @@
<Widget type="ScrollView" skin="MW_ScrollView" position="60 130 410 300" name="TextView">
<Property key="CanvasSize" value="410 900"/>
<Widget type="EditBox" skin="NormalText" position="0 0 380 900">
<Property key="Caption" value="fisdj fjidifj dij fdijf idj fjidi jfdij fidj fidj ifjdi fj idjfisdj fjidifj dij fdijf idj fjidi jfdij fidj fidj ifjdi fj idjfisdj fjidifj dij fdijf idj fjidi jfdij fidj fidj ifjdi fj idjfisdj fjidifj dij fdijf idj fjidi jfdij fidj fidj ifjdi fj idjfisdj fjidifj dij fdijf idj fjidi jfdij fidj fidj ifjdi fj idjfisdj fjidifj dij fdijf idj fjidi jfdij fidj fidj ifjdi fj idjfisdj fjidifj dij fdijf idj fjidi jfdij fidj fidj ifjdi fj idjfisdj fjidifj dij fdijf idj fjidi jfdij fidj fidj ifjdi fj idjfisdj fjidifj dij fdijf idj fjidi jfdij fidj fidj ifjdi fj idjfisdj fjidifj dij fdijf idj fjidi jfdij fidj fidj ifjdi fj idj"/>
<Property key="WordWrap" value="true"/>
<Property key="TextColour" value="0 0 0"/>
<Property key="TextAlign" value="ALIGN_LEFT ALIGN_TOP"/>
<Property key="Static" value="true"/>
<Property key="VisibleVScroll" value="1"/>
<Property key="NeedMouse" value="0"/>
</Widget>
</Widget>
</Widget>