OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
documentmenu.cpp
Go to the documentation of this file.
1#include "documentmenu.h"
2
4#include "world/objects/npc.h"
5#include "utils/gthfont.h"
6#include "utils/keycodec.h"
7#include "gothic.h"
8
9using namespace Tempest;
10
12 :keycodec(key) {
13 setFocusPolicy(NoFocus);
14 cursor = Resources::loadTexture("U.TGA");
15 }
16
18 document = doc;
19 active = true;
20 update();
21 }
22
24 if(!active)
25 return;
26 active=false;
27
28 if(auto pl = Gothic::inst().player())
29 pl->setInteraction(nullptr);
30 }
31
32void DocumentMenu::keyDownEvent(KeyEvent &e) {
33 if(!active){
34 e.ignore();
35 return;
36 }
37
38 if(e.key!=Event::K_ESCAPE && keycodec.tr(e)!=KeyCodec::Inventory){
39 e.ignore();
40 return;
41 }
42 close();
43 }
44
45void DocumentMenu::keyUpEvent(KeyEvent&) {
46 }
47
48void DocumentMenu::paintEvent(PaintEvent &e) {
49 if(!active)
50 return;
51
52 const float scale = Gothic::interfaceScale(this);
53 float mw = 0, mh = 0;
54 for(auto& i:document.pages){
55 auto back = Resources::loadTexture((i.flg&F_Backgr) ? i.img : document.img);
56 if(!back)
57 continue;
58 mw += float(back->w())*scale;
59 mh = std::max(mh,float(back->h())*scale);
60 }
61
62 float k = std::min(1.f,float(800.f+float(document.margins.xMargin()))*scale/std::max(mw,1.f));
63
64 int x = (w()-int(k*mw))/2, y = (h()-int(mh))/2;
65 auto pl = Gothic::inst().player();
66
67 Painter p(e);
68 for(auto& i:document.pages) {
69 const GthFont* fnt = nullptr;
70 if(i.flg&F_Font)
71 fnt = &Resources::font(i.font, Resources::FontType::Normal, scale); else
72 fnt = &Resources::font(document.font, Resources::FontType::Normal, scale);
73 if(fnt==nullptr)
74 fnt = &Resources::font(scale);
75 auto back = Resources::loadTexture((i.flg&F_Backgr) ? i.img : document.img);
76 if(!back)
77 continue;
78
79 auto mgr = (i.flg&F_Margin) ? i.margins : document.margins;
80 mgr.left = int(float(mgr.left) *scale);
81 mgr.right = int(float(mgr.right) *scale);
82 mgr.top = int(float(mgr.top) *scale);
83 mgr.bottom = int(float(mgr.bottom)*scale);
84
85 const int w = int(k*scale*float(back->w()));
86 const int h = int( scale*float(back->h()));
87
88 p.setBrush(*back);
89 p.drawRect(x,y,w,h,
90 0,0,back->w(),back->h());
91
92 p.setBrush(Color(0.04f,0.04f,0.04f,1));
93
94 fnt->drawText(p,x+mgr.left,
95 y+mgr.top+fnt->pixelSize(),
96 w - mgr.xMargin(),
97 h - mgr.yMargin(),
98 i.text, Tempest::AlignLeft);
99
100 if(document.showPlayer && cursor!=nullptr && pl!=nullptr) {
101 auto pos = pl->position();
102 float wx = (pos.x-float(document.wbounds.x))/float(document.wbounds.w);
103 float wy = (pos.z-float(document.wbounds.y))/float(document.wbounds.h);
104
105 p.setBrush(*cursor);
106 int cx = x+int(wx*float(w));
107 int cy = y+int(wy*float(h));
108
109 p.pushState();
110 p.translate(cx,cy);
111 p.rotate(-pl->rotation());
112 p.drawRect(-cursor->w()/2,-cursor->h()/2, cursor->w(),cursor->h());
113 p.popState();
114 }
115 x+=w;
116 }
117 }
void keyDownEvent(Tempest::KeyEvent &e)
void keyUpEvent(Tempest::KeyEvent &e)
void paintEvent(Tempest::PaintEvent &e)
DocumentMenu(const KeyCodec &key)
void show(const Show &doc)
static float interfaceScale(const Tempest::Widget *w)
Definition gothic.cpp:471
static Gothic & inst()
Definition gothic.cpp:249
Npc * player()
Definition gothic.cpp:313
void drawText(Tempest::Painter &p, int x, int y, int w, int h, std::string_view txt, Tempest::AlignFlag align, int firstLine=0) const
int pixelSize() const
Definition gthfont.cpp:18
@ Inventory
Definition keycodec.h:27
Action tr(Tempest::KeyEvent const &e) const
Definition keycodec.cpp:107
static const GthFont & font(const float scale)
static const Tempest::Texture2d * loadTexture(std::string_view name, bool forceMips=false)
Tempest::Rect wbounds
Tempest::Margin margins
std::vector< Page > pages