diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index 29bc86fe3..d7c229cf9 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -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 diff --git a/apps/openmw/mwgui/bookwindow.cpp b/apps/openmw/mwgui/bookwindow.cpp index 4b6082b81..3491e6918 100644 --- a/apps/openmw/mwgui/bookwindow.cpp +++ b/apps/openmw/mwgui/bookwindow.cpp @@ -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 *ref = + mBook.get(); + + //BookTextParser parser; + //parser.parse(ref->base->text, 0, 0); } void BookWindow::onCloseButtonClicked (MyGUI::Widget* _sender) diff --git a/apps/openmw/mwgui/formatting.cpp b/apps/openmw/mwgui/formatting.cpp new file mode 100644 index 000000000..547c12c35 --- /dev/null +++ b/apps/openmw/mwgui/formatting.cpp @@ -0,0 +1,95 @@ +#include "formatting.hpp" + +#include +#include +#include + +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, "
", "\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()) + 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("NormalText", + MyGUI::IntCoord(0, size.height, mWidth, 24), MyGUI::Align::Left | MyGUI::Align::Top, + mParent->getName() + boost::lexical_cast(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; +} diff --git a/apps/openmw/mwgui/formatting.hpp b/apps/openmw/mwgui/formatting.hpp new file mode 100644 index 000000000..b60137f35 --- /dev/null +++ b/apps/openmw/mwgui/formatting.hpp @@ -0,0 +1,38 @@ +#ifndef MWGUI_FORMATTING_H +#define MWGUI_FORMATTING_H + +#include + +#include + +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 diff --git a/apps/openmw/mwgui/scrollwindow.cpp b/apps/openmw/mwgui/scrollwindow.cpp index a5135722f..babf439ac 100644 --- a/apps/openmw/mwgui/scrollwindow.cpp +++ b/apps/openmw/mwgui/scrollwindow.cpp @@ -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 *ref = + mScroll.get(); + + 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) diff --git a/apps/openmw/mwgui/scrollwindow.hpp b/apps/openmw/mwgui/scrollwindow.hpp index e4968995e..918a3d3ef 100644 --- a/apps/openmw/mwgui/scrollwindow.hpp +++ b/apps/openmw/mwgui/scrollwindow.hpp @@ -20,6 +20,7 @@ namespace MWGui private: MyGUI::Button* mCloseButton; MyGUI::Button* mTakeButton; + MyGUI::ScrollView* mTextView; MWWorld::Ptr mScroll; }; diff --git a/files/mygui/openmw_scroll_layout.xml b/files/mygui/openmw_scroll_layout.xml index 8641a0511..0f4a0be3e 100644 --- a/files/mygui/openmw_scroll_layout.xml +++ b/files/mygui/openmw_scroll_layout.xml @@ -17,15 +17,6 @@ - - - - - - - - -