1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-29 09:32:45 +00:00
OpenMW/apps/openmw/mwgui/journalbooks.cpp

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

330 lines
10 KiB
C++
Raw Normal View History

#include "journalbooks.hpp"
2017-07-24 13:25:01 +02:00
#include "../mwbase/environment.hpp"
#include "../mwbase/windowmanager.hpp"
2017-11-21 12:31:23 +04:00
#include <components/misc/utf8stream.hpp>
2017-07-24 13:25:01 +02:00
#include "textcolours.hpp"
namespace
{
struct AddContent
{
2013-05-06 16:04:28 +02:00
MWGui::BookTypesetter::Ptr mTypesetter;
MWGui::BookTypesetter::Style* mBodyStyle;
2013-05-06 16:04:28 +02:00
AddContent(MWGui::BookTypesetter::Ptr typesetter, MWGui::BookTypesetter::Style* body_style)
: mTypesetter(typesetter)
, mBodyStyle(body_style)
{
}
};
struct AddSpan : AddContent
{
2013-05-06 16:04:28 +02:00
AddSpan(MWGui::BookTypesetter::Ptr typesetter, MWGui::BookTypesetter::Style* body_style)
: AddContent(typesetter, body_style)
{
}
void operator()(intptr_t topicId, size_t begin, size_t end)
{
2013-05-06 16:04:28 +02:00
MWGui::BookTypesetter::Style* style = mBodyStyle;
2017-07-24 13:25:01 +02:00
const MWGui::TextColours& textColours = MWBase::Environment::get().getWindowManager()->getTextColours();
if (topicId)
2017-07-24 13:25:01 +02:00
style = mTypesetter->createHotStyle(mBodyStyle, textColours.journalLink, textColours.journalLinkOver,
textColours.journalLinkPressed, topicId);
mTypesetter->write(style, begin, end);
}
};
struct AddEntry
{
2013-05-06 16:04:28 +02:00
MWGui::BookTypesetter::Ptr mTypesetter;
MWGui::BookTypesetter::Style* mBodyStyle;
2013-05-06 16:04:28 +02:00
AddEntry(MWGui::BookTypesetter::Ptr typesetter, MWGui::BookTypesetter::Style* body_style)
: mTypesetter(typesetter)
, mBodyStyle(body_style)
{
}
2013-05-06 16:04:28 +02:00
void operator()(MWGui::JournalViewModel::Entry const& entry)
{
mTypesetter->addContent(entry.body());
entry.visitSpans(AddSpan(mTypesetter, mBodyStyle));
}
};
struct AddJournalEntry : AddEntry
{
bool mAddHeader;
2013-05-06 16:04:28 +02:00
MWGui::BookTypesetter::Style* mHeaderStyle;
2013-05-06 16:04:28 +02:00
AddJournalEntry(MWGui::BookTypesetter::Ptr typesetter, MWGui::BookTypesetter::Style* body_style,
MWGui::BookTypesetter::Style* header_style, bool add_header)
: AddEntry(typesetter, body_style)
, mAddHeader(add_header)
, mHeaderStyle(header_style)
{
}
2013-05-06 16:04:28 +02:00
void operator()(MWGui::JournalViewModel::JournalEntry const& entry)
{
if (mAddHeader)
{
mTypesetter->write(mHeaderStyle, entry.timestamp());
mTypesetter->lineBreak();
}
AddEntry::operator()(entry);
2017-08-03 20:27:14 +04:00
mTypesetter->sectionBreak(30);
}
};
struct AddTopicEntry : AddEntry
{
intptr_t mContentId;
2013-05-06 16:04:28 +02:00
MWGui::BookTypesetter::Style* mHeaderStyle;
2013-05-06 16:04:28 +02:00
AddTopicEntry(MWGui::BookTypesetter::Ptr typesetter, MWGui::BookTypesetter::Style* body_style,
MWGui::BookTypesetter::Style* header_style, intptr_t contentId)
: AddEntry(typesetter, body_style)
, mContentId(contentId)
, mHeaderStyle(header_style)
{
}
2013-05-06 16:04:28 +02:00
void operator()(MWGui::JournalViewModel::TopicEntry const& entry)
{
mTypesetter->write(mBodyStyle, entry.source());
mTypesetter->write(mBodyStyle, 0, 3); // begin
AddEntry::operator()(entry);
mTypesetter->selectContent(mContentId);
mTypesetter->write(mBodyStyle, 2, 3); // end quote
2017-08-03 20:27:14 +04:00
mTypesetter->sectionBreak(30);
}
};
struct AddTopicName : AddContent
{
2013-05-06 16:04:28 +02:00
AddTopicName(MWGui::BookTypesetter::Ptr typesetter, MWGui::BookTypesetter::Style* style)
: AddContent(typesetter, style)
{
}
2013-05-06 16:04:28 +02:00
void operator()(MWGui::JournalViewModel::Utf8Span topicName)
{
mTypesetter->write(mBodyStyle, topicName);
2017-08-03 20:27:14 +04:00
mTypesetter->sectionBreak();
}
};
struct AddQuestName : AddContent
{
2013-05-06 16:04:28 +02:00
AddQuestName(MWGui::BookTypesetter::Ptr typesetter, MWGui::BookTypesetter::Style* style)
: AddContent(typesetter, style)
{
}
2013-05-06 16:04:28 +02:00
void operator()(MWGui::JournalViewModel::Utf8Span topicName)
{
mTypesetter->write(mBodyStyle, topicName);
2017-08-03 20:27:14 +04:00
mTypesetter->sectionBreak();
}
};
}
2013-05-06 16:04:28 +02:00
namespace MWGui
{
2023-01-29 21:31:21 +01:00
MWGui::BookTypesetter::Utf8Span to_utf8_span(std::string_view text)
2014-03-17 14:15:52 +01:00
{
typedef MWGui::BookTypesetter::Utf8Point point;
2023-01-29 21:31:21 +01:00
point begin = reinterpret_cast<point>(text.data());
2014-03-17 14:15:52 +01:00
2023-01-29 21:31:21 +01:00
return MWGui::BookTypesetter::Utf8Span(begin, begin + text.length());
2014-03-17 14:15:52 +01:00
}
typedef TypesetBook::Ptr book;
JournalBooks::JournalBooks(JournalViewModel::Ptr model, ToUTF8::FromType encoding)
: mModel(model)
, mEncoding(encoding)
, mIndexPagesCount(0)
{
}
book JournalBooks::createEmptyJournalBook()
{
BookTypesetter::Ptr typesetter = createTypesetter();
2013-06-06 22:13:30 +02:00
BookTypesetter::Style* header = typesetter->createStyle("", MyGUI::Colour(0.60f, 0.00f, 0.00f));
BookTypesetter::Style* body = typesetter->createStyle("", MyGUI::Colour::Black);
typesetter->write(header, to_utf8_span("You have no journal entries!"));
typesetter->lineBreak();
typesetter->write(
body, to_utf8_span("You should have gone though the starting quest and got an initial quest."));
return typesetter->complete();
}
book JournalBooks::createJournalBook()
{
BookTypesetter::Ptr typesetter = createTypesetter();
2013-06-06 22:13:30 +02:00
BookTypesetter::Style* header = typesetter->createStyle("", MyGUI::Colour(0.60f, 0.00f, 0.00f));
BookTypesetter::Style* body = typesetter->createStyle("", MyGUI::Colour::Black);
mModel->visitJournalEntries("", AddJournalEntry(typesetter, body, header, true));
return typesetter->complete();
}
book JournalBooks::createTopicBook(uintptr_t topicId)
{
BookTypesetter::Ptr typesetter = createTypesetter();
2013-06-06 22:13:30 +02:00
BookTypesetter::Style* header = typesetter->createStyle("", MyGUI::Colour(0.60f, 0.00f, 0.00f));
BookTypesetter::Style* body = typesetter->createStyle("", MyGUI::Colour::Black);
mModel->visitTopicName(topicId, AddTopicName(typesetter, header));
intptr_t contentId = typesetter->addContent(to_utf8_span(", \""));
mModel->visitTopicEntries(topicId, AddTopicEntry(typesetter, body, header, contentId));
return typesetter->complete();
}
2023-01-29 21:31:21 +01:00
book JournalBooks::createQuestBook(std::string_view questName)
{
BookTypesetter::Ptr typesetter = createTypesetter();
2013-06-06 22:13:30 +02:00
BookTypesetter::Style* header = typesetter->createStyle("", MyGUI::Colour(0.60f, 0.00f, 0.00f));
BookTypesetter::Style* body = typesetter->createStyle("", MyGUI::Colour::Black);
AddQuestName addName(typesetter, header);
2023-01-29 21:31:21 +01:00
addName(to_utf8_span(questName));
mModel->visitJournalEntries(questName, AddJournalEntry(typesetter, body, header, false));
return typesetter->complete();
}
book JournalBooks::createTopicIndexBook()
{
bool isRussian = (mEncoding == ToUTF8::WINDOWS_1251);
BookTypesetter::Ptr typesetter = isRussian ? createCyrillicJournalIndex() : createLatinJournalIndex();
return typesetter->complete();
}
BookTypesetter::Ptr JournalBooks::createLatinJournalIndex()
{
2018-06-18 13:43:39 +04:00
BookTypesetter::Ptr typesetter = BookTypesetter::create(92, 260);
typesetter->setSectionAlignment(BookTypesetter::AlignCenter);
2018-06-18 13:43:39 +04:00
// Latin journal index always has two columns for now.
mIndexPagesCount = 2;
2018-06-18 13:43:39 +04:00
char ch = 'A';
2023-01-29 21:31:21 +01:00
std::string buffer;
BookTypesetter::Style* body = typesetter->createStyle("", MyGUI::Colour::Black);
for (int i = 0; i < 26; ++i)
{
2023-01-29 21:31:21 +01:00
buffer = "( ";
buffer += ch;
buffer += " )";
2022-09-22 21:26:05 +03:00
const MWGui::TextColours& textColours = MWBase::Environment::get().getWindowManager()->getTextColours();
BookTypesetter::Style* style = typesetter->createHotStyle(body, textColours.journalTopic,
textColours.journalTopicOver, textColours.journalTopicPressed, (Utf8Stream::UnicodeChar)ch);
if (i == 13)
typesetter->sectionBreak();
typesetter->write(style, to_utf8_span(buffer));
typesetter->lineBreak();
ch++;
2022-09-22 21:26:05 +03:00
}
return typesetter;
}
BookTypesetter::Ptr JournalBooks::createCyrillicJournalIndex()
2022-09-22 21:26:05 +03:00
{
BookTypesetter::Ptr typesetter = BookTypesetter::create(92, 260);
2018-06-18 13:43:39 +04:00
typesetter->setSectionAlignment(BookTypesetter::AlignCenter);
BookTypesetter::Style* body = typesetter->createStyle("", MyGUI::Colour::Black);
int fontHeight = MWBase::Environment::get().getWindowManager()->getFontHeight();
2018-06-18 13:43:39 +04:00
// for small font size split alphabet to two columns (2x15 characers), for big font size split it to three
// colums (3x10 characters).
int sectionBreak = 10;
mIndexPagesCount = 3;
if (fontHeight < 18)
2022-09-22 21:26:05 +03:00
{
2018-06-18 13:43:39 +04:00
sectionBreak = 15;
mIndexPagesCount = 2;
2022-09-22 21:26:05 +03:00
}
2018-06-18 13:43:39 +04:00
unsigned char ch[3] = { 0xd0, 0x90, 0x00 }; // CYRILLIC CAPITAL A is a 0xd090 in UTF-8
2023-01-29 21:31:21 +01:00
std::string buffer;
for (int i = 0; i < 32; ++i)
{
2023-01-29 21:31:21 +01:00
buffer = "( ";
buffer += ch[0];
buffer += ch[1];
buffer += " )";
Utf8Stream stream((char*)ch);
Utf8Stream::UnicodeChar first = stream.peek();
const MWGui::TextColours& textColours = MWBase::Environment::get().getWindowManager()->getTextColours();
BookTypesetter::Style* style = typesetter->createHotStyle(
body, textColours.journalTopic, textColours.journalTopicOver, textColours.journalTopicPressed, first);
2017-11-21 12:31:23 +04:00
2017-07-24 13:25:01 +02:00
ch[1]++;
2017-08-04 20:21:13 +04:00
// Words can not be started with these characters
2017-08-04 20:21:13 +04:00
if (i == 26 || i == 28)
continue;
2017-08-04 20:21:13 +04:00
if (i % sectionBreak == 0)
typesetter->sectionBreak();
2018-06-18 13:43:39 +04:00
typesetter->write(style, to_utf8_span(buffer));
typesetter->lineBreak();
2022-09-22 21:26:05 +03:00
}
return typesetter;
}
BookTypesetter::Ptr JournalBooks::createTypesetter()
{
// TODO: determine page size from layout...
2017-08-03 20:27:14 +04:00
return BookTypesetter::create(240, 320);
}
2013-05-06 16:04:28 +02:00
}