1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-09 12:42:11 +00:00
OpenMW/apps/openmw/mwgui/bookwindow.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

222 lines
7.3 KiB
C++
Raw Normal View History

2012-05-03 01:33:33 +00:00
#include "bookwindow.hpp"
#include <MyGUI_InputManager.h>
2015-01-10 01:50:43 +00:00
#include <MyGUI_TextBox.h>
#include <components/esm3/loadbook.hpp>
2023-10-03 00:04:18 +00:00
#include <components/esm4/loadbook.hpp>
2014-02-23 19:11:05 +00:00
#include "../mwbase/environment.hpp"
#include "../mwbase/windowmanager.hpp"
2015-08-21 09:12:39 +00:00
#include "../mwmechanics/actorutil.hpp"
#include "../mwworld/actiontake.hpp"
#include "../mwworld/class.hpp"
2012-05-23 10:23:35 +00:00
#include "formatting.hpp"
2012-05-10 09:03:27 +00:00
2013-04-17 22:56:48 +00:00
namespace MWGui
2012-05-03 01:33:33 +00:00
{
2013-04-17 22:56:48 +00:00
BookWindow::BookWindow()
: BookWindowBase("openmw_book.layout")
, mCurrentPage(0)
2013-04-17 22:56:48 +00:00
, mTakeButtonShow(true)
, mTakeButtonAllowed(true)
{
getWidget(mCloseButton, "CloseButton");
mCloseButton->eventMouseButtonClick += MyGUI::newDelegate(this, &BookWindow::onCloseButtonClicked);
2013-04-17 22:56:48 +00:00
getWidget(mTakeButton, "TakeButton");
mTakeButton->eventMouseButtonClick += MyGUI::newDelegate(this, &BookWindow::onTakeButtonClicked);
2013-04-17 22:56:48 +00:00
getWidget(mNextPageButton, "NextPageBTN");
mNextPageButton->eventMouseButtonClick += MyGUI::newDelegate(this, &BookWindow::onNextPageButtonClicked);
2013-04-17 22:56:48 +00:00
getWidget(mPrevPageButton, "PrevPageBTN");
mPrevPageButton->eventMouseButtonClick += MyGUI::newDelegate(this, &BookWindow::onPrevPageButtonClicked);
2012-05-10 09:03:27 +00:00
2013-04-17 22:56:48 +00:00
getWidget(mLeftPageNumber, "LeftPageNumber");
getWidget(mRightPageNumber, "RightPageNumber");
2012-05-10 09:03:27 +00:00
2013-04-17 22:56:48 +00:00
getWidget(mLeftPage, "LeftPage");
getWidget(mRightPage, "RightPage");
2012-05-03 01:33:33 +00:00
adjustButton("CloseButton");
adjustButton("TakeButton");
adjustButton("PrevPageBTN");
float scale = adjustButton("NextPageBTN");
2013-05-08 11:55:15 +00:00
mLeftPage->setNeedMouseFocus(true);
mLeftPage->eventMouseWheel += MyGUI::newDelegate(this, &BookWindow::onMouseWheel);
mRightPage->setNeedMouseFocus(true);
mRightPage->eventMouseWheel += MyGUI::newDelegate(this, &BookWindow::onMouseWheel);
mNextPageButton->eventKeyButtonPressed += MyGUI::newDelegate(this, &BookWindow::onKeyButtonPressed);
mPrevPageButton->eventKeyButtonPressed += MyGUI::newDelegate(this, &BookWindow::onKeyButtonPressed);
mTakeButton->eventKeyButtonPressed += MyGUI::newDelegate(this, &BookWindow::onKeyButtonPressed);
mCloseButton->eventKeyButtonPressed += MyGUI::newDelegate(this, &BookWindow::onKeyButtonPressed);
2013-05-08 11:55:15 +00:00
if (mNextPageButton->getSize().width == 64)
{
// english button has a 7 pixel wide strip of garbage on its right edge
mNextPageButton->setSize(64 - 7, mNextPageButton->getSize().height);
mNextPageButton->setImageCoord(
MyGUI::IntCoord(0, 0, (64 - 7) * scale, mNextPageButton->getSize().height * scale));
2013-05-08 11:55:15 +00:00
}
2013-04-17 22:56:48 +00:00
center();
}
void BookWindow::onMouseWheel(MyGUI::Widget* _sender, int _rel)
{
if (_rel < 0)
nextPage();
else
prevPage();
}
2013-04-17 22:56:48 +00:00
void BookWindow::clearPages()
2012-05-10 09:03:27 +00:00
{
2013-04-17 22:56:48 +00:00
mPages.clear();
2012-05-10 09:03:27 +00:00
}
void BookWindow::setPtr(const MWWorld::Ptr& book)
2013-04-17 22:56:48 +00:00
{
2023-10-03 00:04:18 +00:00
if (book.isEmpty() || (book.getType() != ESM::REC_BOOK && book.getType() != ESM::REC_BOOK4))
2023-08-20 13:27:00 +00:00
throw std::runtime_error("Invalid argument in BookWindow::setPtr");
2013-04-17 22:56:48 +00:00
mBook = book;
MWWorld::Ptr player = MWMechanics::getPlayer();
bool showTakeButton = book.getContainerStore() != &player.getClass().getContainerStore(player);
2013-04-17 22:56:48 +00:00
clearPages();
mCurrentPage = 0;
2012-05-10 09:03:27 +00:00
2023-10-03 00:04:18 +00:00
const std::string* text;
if (book.getType() == ESM::REC_BOOK)
text = &book.get<ESM::Book>()->mBase->mText;
else
text = &book.get<ESM4::Book>()->mBase->mText;
bool shrinkTextAtLastTag = book.getType() == ESM::REC_BOOK;
2014-09-19 22:11:04 +00:00
Formatting::BookFormatter formatter;
2023-10-03 00:04:18 +00:00
mPages = formatter.markupToWidget(mLeftPage, *text, shrinkTextAtLastTag);
formatter.markupToWidget(mRightPage, *text, shrinkTextAtLastTag);
2012-05-10 09:03:27 +00:00
2013-04-17 22:56:48 +00:00
updatePages();
2015-03-11 19:33:55 +00:00
setTakeButtonShow(showTakeButton);
MWBase::Environment::get().getWindowManager()->setKeyFocusWidget(mCloseButton);
2013-04-17 22:56:48 +00:00
}
2013-04-17 22:56:48 +00:00
void BookWindow::setTakeButtonShow(bool show)
{
mTakeButtonShow = show;
mTakeButton->setVisible(mTakeButtonShow && mTakeButtonAllowed);
}
void BookWindow::onKeyButtonPressed(MyGUI::Widget* sender, MyGUI::KeyCode key, MyGUI::Char character)
{
if (key == MyGUI::KeyCode::ArrowUp)
prevPage();
else if (key == MyGUI::KeyCode::ArrowDown)
nextPage();
}
2013-04-17 22:56:48 +00:00
void BookWindow::setInventoryAllowed(bool allowed)
{
mTakeButtonAllowed = allowed;
mTakeButton->setVisible(mTakeButtonShow && mTakeButtonAllowed);
}
2013-04-17 22:56:48 +00:00
void BookWindow::onCloseButtonClicked(MyGUI::Widget* sender)
{
2017-09-23 10:18:39 +00:00
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Book);
2013-04-17 22:56:48 +00:00
}
2013-04-17 22:56:48 +00:00
void BookWindow::onTakeButtonClicked(MyGUI::Widget* sender)
{
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2022-09-25 11:17:09 +00:00
MWBase::Environment::get().getWindowManager()->playSound(ESM::RefId::stringRefId("Item Book Up"));
2013-04-17 22:56:48 +00:00
MWWorld::ActionTake take(mBook);
2015-08-21 09:12:39 +00:00
take.execute(MWMechanics::getPlayer());
2013-04-17 22:56:48 +00:00
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Book);
}
2013-04-17 22:56:48 +00:00
void BookWindow::onNextPageButtonClicked(MyGUI::Widget* sender)
2012-05-10 09:03:27 +00:00
{
2013-06-13 05:50:07 +00:00
nextPage();
2012-05-10 09:03:27 +00:00
}
2012-05-03 01:33:33 +00:00
2013-04-17 22:56:48 +00:00
void BookWindow::onPrevPageButtonClicked(MyGUI::Widget* sender)
2012-05-10 09:03:27 +00:00
{
2013-06-13 05:50:07 +00:00
prevPage();
2012-05-10 09:03:27 +00:00
}
2013-04-17 22:56:48 +00:00
void BookWindow::updatePages()
2012-05-10 09:03:27 +00:00
{
mLeftPageNumber->setCaption(MyGUI::utility::toString(mCurrentPage * 2 + 1));
mRightPageNumber->setCaption(MyGUI::utility::toString(mCurrentPage * 2 + 2));
2013-04-17 22:56:48 +00:00
MyGUI::Widget* focus = MyGUI::InputManager::getInstance().getKeyFocusWidget();
bool nextPageVisible = (mCurrentPage + 1) * 2 < mPages.size();
mNextPageButton->setVisible(nextPageVisible);
bool prevPageVisible = mCurrentPage != 0;
mPrevPageButton->setVisible(prevPageVisible);
if (focus == mNextPageButton && !nextPageVisible && prevPageVisible)
MWBase::Environment::get().getWindowManager()->setKeyFocusWidget(mPrevPageButton);
else if (focus == mPrevPageButton && !prevPageVisible && nextPageVisible)
MWBase::Environment::get().getWindowManager()->setKeyFocusWidget(mNextPageButton);
2014-09-19 22:11:04 +00:00
if (mPages.empty())
return;
MyGUI::Widget* paper;
paper = mLeftPage->getChildAt(0);
paper->setCoord(paper->getPosition().left, -mPages[mCurrentPage * 2].first, paper->getWidth(),
mPages[mCurrentPage * 2].second);
paper = mRightPage->getChildAt(0);
if ((mCurrentPage + 1) * 2 <= mPages.size())
{
paper->setCoord(paper->getPosition().left, -mPages[mCurrentPage * 2 + 1].first, paper->getWidth(),
mPages[mCurrentPage * 2 + 1].second);
paper->setVisible(true);
}
else
{
paper->setVisible(false);
}
2012-05-10 09:03:27 +00:00
}
2013-04-17 22:56:48 +00:00
2013-06-13 05:50:07 +00:00
void BookWindow::nextPage()
{
2013-06-13 05:52:26 +00:00
if ((mCurrentPage + 1) * 2 < mPages.size())
2013-06-13 05:50:07 +00:00
{
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2022-09-25 11:17:09 +00:00
MWBase::Environment::get().getWindowManager()->playSound(ESM::RefId::stringRefId("book page2"));
2013-06-13 05:50:07 +00:00
++mCurrentPage;
updatePages();
}
}
void BookWindow::prevPage()
{
2013-06-13 05:52:26 +00:00
if (mCurrentPage > 0)
2013-06-13 05:50:07 +00:00
{
MWBase::Environment::get().getWindowManager()->playSound(ESM::RefId::stringRefId("book page"));
2013-06-13 05:50:07 +00:00
--mCurrentPage;
updatePages();
}
}
2013-05-08 11:55:15 +00:00
}