OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
menuroot.cpp
Go to the documentation of this file.
1#include "menuroot.h"
2
3#include <Tempest/Log>
4
5#include "ui/gamemenu.h"
6#include "gothic.h"
7
8using namespace Tempest;
9
11 :keyCodec(keyCodec) {
12 setCursorShape(CursorShape::Hidden);
13
14 Gothic::inst().onSettingsChanged.bind(this,&MenuRoot::initSettings);
15 initSettings();
16 }
17
19 Gothic::inst().onSettingsChanged.ubind(this,&MenuRoot::initSettings);
20 }
21
22void MenuRoot::initSettings() {
23 const auto lang = Gothic::inst().settingsGetI("GAME", "language");
24 if(lang==vmLang && vm!=nullptr)
25 return;
26
27 // clear pointer to vm-objects
28 for(auto& i:menuStack)
29 i->resetVm(nullptr);
30 if(current!=nullptr)
31 current->resetVm(nullptr);
32
33 vmLang = lang;
34 vm = Gothic::inst().createPhoenixVm("MENU.DAT", ScriptLang(vmLang));
35 vm->register_external("apply_options_video",[](){
36 Log::d("TODO: apply_options_video");
37 });
38
39 for(auto& i:menuStack)
40 i->resetVm(vm.get());
41 if(current!=nullptr)
42 current->resetVm(vm.get());
43
44 update();
45 }
46
51
52void MenuRoot::setMenu(std::string_view menuEv, KeyCodec::Action key) {
53 if(vm->find_symbol_by_name(menuEv) == nullptr){
54 Log::e("invalid menu-id: ",menuEv);
55 return;
56 }
57 setMenu(new GameMenu(*this,keyCodec,*vm,menuEv,key));
58 }
59
61 removeAllWidgets();
62 if(w)
63 addWidget(w);
64 current=w;
65 }
66
68 if(widgetsCount()>0) {
69 takeWidget(current);
70 menuStack.emplace_back(current);
71 }
72 if(w) {
73 addWidget(w);
74 w->onTick();
75 }
76 current = w;
77 }
78
80 if(menuStack.size()==0) {
81 if(Gothic::inst().isInGame() || Gothic::inst().checkLoading()!=Gothic::LoadState::Idle) {
82 current=nullptr;
83 removeAllWidgets();
84 owner()->setFocus(true);
85 }
86 return;
87 }
88 GameMenu* w = menuStack.back().release();
89
90 removeAllWidgets();
91 addWidget(w);
92 current=w;
93 current->onTick();
94
95 menuStack.pop_back();
96 }
97
99 menuStack.clear();
100 if(Gothic::inst().isInGame() || Gothic::inst().checkLoading()!=Gothic::LoadState::Idle) {
101 current=nullptr;
102 removeAllWidgets();
103 }
104 }
105
106bool MenuRoot::isActive() const {
107 return current!=nullptr;
108 }
109
110void MenuRoot::setPlayer(const Npc &pl) {
111 if(current!=nullptr)
112 current->setPlayer(pl);
113 }
114
116 if(current!=nullptr)
117 current->processMusicTheme();
118 }
119
121 showVersionHint = s;
122 update();
123 }
124
126 return showVersionHint;
127 }
128
129void MenuRoot::mouseDownEvent(MouseEvent& event) {
130 if(current!=nullptr) {
131 if(event.button==Event::ButtonRight) {
132 popMenu();
133 } else {
135 }
136 } else {
137 event.ignore();
138 }
139 }
140
141void MenuRoot::mouseUpEvent(MouseEvent&) {
142 }
143
144void MenuRoot::mouseWheelEvent(MouseEvent &event) {
145 if(current!=nullptr) {
146 if(event.delta>0)
147 current->onKeyboard(KeyCodec::Forward); else
148 if(event.delta<0)
149 current->onKeyboard(KeyCodec::Back);
150 } else {
151 event.ignore();
152 }
153 }
154
155void MenuRoot::keyRepeatEvent(Tempest::KeyEvent &e) {
156 if(current==nullptr) {
157 e.ignore();
158 return;
159 }
160
161 if(e.key == Event::K_A || e.key == Event::K_Left)
162 current->onKeyboard(KeyCodec::Left);
163 else if(e.key == Event::K_D || e.key == Event::K_Right)
164 current->onKeyboard(KeyCodec::Right);
165 else if(e.key == Event::K_W || e.key == Event::K_Up)
167 else if(e.key == Event::K_S || e.key == Event::K_Down)
168 current->onKeyboard(KeyCodec::Back);
169 }
170
171void MenuRoot::keyDownEvent(KeyEvent &e) {
172 const float scale = Gothic::interfaceScale(this);
173 size_t sz = std::extent_v<decltype(cheatCode)>;
174 for(size_t i=1; i<sz; ++i)
175 cheatCode[i-1] = cheatCode[i];
176 cheatCode[sz-1] = e.key;
177 if(cheatCode[0]==Event::K_M &&
178 cheatCode[1]==Event::K_A &&
179 cheatCode[2]==Event::K_R &&
180 cheatCode[3]==Event::K_V &&
181 cheatCode[4]==Event::K_I &&
182 cheatCode[5]==Event::K_N) {
184 auto& fnt = Resources::font(scale);
185 Gothic::inst().onPrintScreen("MARVIN-MODE",2,4, 1,fnt);
186 }
187
188 if(cheatCode[sz-2]==Event::K_4 &&
189 cheatCode[sz-1]==Event::K_2) {
191 auto& fnt = Resources::font(scale);
192 Gothic::inst().onPrintScreen("WHAT WAS THE QUESTION?",2,4, 1,fnt);
193 }
194
195 if(current!=nullptr) {
196 if(e.key==Event::K_W || e.key==Event::K_Up)
198 else if(e.key==Event::K_S || e.key==Event::K_Down)
199 current->onKeyboard(KeyCodec::Back);
200 else if(e.key==Event::K_A || e.key==Event::K_Left)
201 current->onKeyboard(KeyCodec::Left);
202 else if(e.key==Event::K_D || e.key==Event::K_Right)
203 current->onKeyboard(KeyCodec::Right);
204 else if(e.key==Event::K_Return)
206 else if(e.key==Event::K_Delete)
207 current->onKeyboard(KeyCodec::K_Del);
208 else if(e.key==Event::K_ESCAPE || keyCodec.tr(e)==current->keyClose())
209 popMenu();
210 }
211 }
void setPlayer(const Npc &pl)
void onKeyboard(KeyCodec::Action k)
Definition gamemenu.cpp:594
void resetVm(zenkit::DaedalusVm *vm)
Definition gamemenu.cpp:318
void onTick()
Definition gamemenu.cpp:627
KeyCodec::Action keyClose() const
Definition gamemenu.h:34
void processMusicTheme()
Definition gamemenu.cpp:652
Tempest::Signal< void()> onSettingsChanged
Definition gothic.h:182
Tempest::Signal< void(std::string_view, int, int, int, const GthFont &)> onPrintScreen
Definition gothic.h:173
static float interfaceScale(const Tempest::Widget *w)
Definition gothic.cpp:471
static Gothic & inst()
Definition gothic.cpp:249
std::unique_ptr< zenkit::DaedalusVm > createPhoenixVm(std::string_view datFile, const ScriptLang lang=ScriptLang::NONE)
Definition gothic.cpp:711
static int settingsGetI(std::string_view sec, std::string_view name)
Definition gothic.cpp:807
void setMarvinEnabled(bool m)
Definition gothic.cpp:467
@ ActionGeneric
Definition keycodec.h:45
@ Forward
Definition keycodec.h:37
Action tr(Tempest::KeyEvent const &e) const
Definition keycodec.cpp:107
void popMenu()
Definition menuroot.cpp:79
void mouseDownEvent(Tempest::MouseEvent &event) override
Definition menuroot.cpp:129
void setPlayer(const Npc &pl)
Definition menuroot.cpp:110
MenuRoot(KeyCodec &keyCodec)
Definition menuroot.cpp:10
void processMusicTheme()
Definition menuroot.cpp:115
bool isActive() const
Definition menuroot.cpp:106
void setMainMenu()
Definition menuroot.cpp:47
void pushMenu(GameMenu *w)
Definition menuroot.cpp:67
void mouseUpEvent(Tempest::MouseEvent &event) override
Definition menuroot.cpp:141
void showVersion(bool s)
Definition menuroot.cpp:120
~MenuRoot() override
Definition menuroot.cpp:18
void closeAll()
Definition menuroot.cpp:98
void keyDownEvent(Tempest::KeyEvent &event) override
Definition menuroot.cpp:171
void mouseWheelEvent(Tempest::MouseEvent &event) override
Definition menuroot.cpp:144
void keyRepeatEvent(Tempest::KeyEvent &event) override
Definition menuroot.cpp:155
void setMenu(std::string_view menu, KeyCodec::Action key=KeyCodec::Escape)
Definition menuroot.cpp:52
bool hasVersionLine() const
Definition menuroot.cpp:125
Definition npc.h:25
static const GthFont & font(const float scale)
std::string_view menuMain
Definition constants.h:6
ScriptLang
Definition constants.h:518