Line data Source code
1 : /**
2 : * @file menus.cpp
3 : * @brief UI triggers for menu functionality
4 : *
5 : * automatic triggers for certain UIs in game
6 : * does not implement menus, just some low level bindings for main menu functionality
7 : */
8 :
9 : #include "../libprimis-headers/cube.h"
10 :
11 : #include "control.h"
12 : #include "menus.h"
13 : #include "ui.h"
14 :
15 : #include "world/octaedit.h"
16 :
17 : //internally relevant functionality
18 : namespace
19 : {
20 : struct Change final
21 : {
22 : int type;
23 : const char *desc;
24 :
25 : Change() {}
26 1 : Change(int type, const char *desc) : type(type), desc(desc) {}
27 :
28 0 : bool operator==(const Change & c) const
29 : {
30 0 : return type == c.type && std::strcmp(desc, c.desc) == 0;
31 : }
32 : };
33 : std::vector<Change> needsapply;
34 :
35 : VARP(applydialog, 0, 1, 1);
36 :
37 : //when 1: change UI shows up whenever a pending change is added
38 : //when 0: change UI does not appear and applychanges must be used manually
39 : VAR(showchanges, 0, 1, 1);
40 :
41 : /**
42 : * @brief Applies enqueued changes that require reloading parts of the engine
43 : *
44 : * These are settings that cannot be applied immediately and require reloading
45 : * parts of the engine (disrupting the currently rendered scene). Settings that
46 : * require resetting the GL configuration, the shader configuration or sound
47 : * configuration are added to the change buffer and are flushed when this function
48 : * is called.
49 : */
50 1 : void applychanges()
51 : {
52 1 : int changetypes = 0;
53 2 : for(const Change &i : needsapply)
54 : {
55 1 : changetypes |= i.type;
56 : }
57 1 : if(changetypes&Change_Graphics)
58 : {
59 1 : execident("resetgl");
60 : }
61 0 : else if(changetypes&Change_Shaders)
62 : {
63 0 : execident("resetshaders");
64 : }
65 1 : if(changetypes&Change_Sound)
66 : {
67 0 : execident("resetsound");
68 : }
69 1 : }
70 :
71 : /**
72 : * @brief returns if there are pending changes or not enqueued
73 : *
74 : * If idx is specified, and specifies a valid pending change index, prints out
75 : * the description of that pending change to CubeScript. If no idx is specified,
76 : * prints out how many changes there are enqueued to CubeScript.
77 : *
78 : * @param idx index to query, or nullptr if querying number of pending changes
79 : */
80 1 : void pendingchanges(const int *idx)
81 : {
82 1 : if(idx)
83 : {
84 1 : if((needsapply.size()) > static_cast<uint>(*idx))
85 : {
86 0 : result(needsapply.at(*idx).desc);
87 : }
88 1 : else if(*idx < 0)
89 : {
90 1 : intret(needsapply.size());
91 : }
92 : }
93 1 : }
94 : }
95 :
96 : //externally relevant functionality
97 :
98 : //toggles if the main menu is shown
99 : VAR(mainmenu, 1, 1, 0);
100 :
101 1 : void addchange(const char *desc, int type)
102 : {
103 1 : if(!applydialog)
104 : {
105 0 : return;
106 : }
107 1 : for(const Change &i : needsapply)
108 : {
109 0 : if(!std::strcmp(i.desc, desc))
110 : {
111 0 : return;
112 : }
113 : }
114 1 : needsapply.emplace_back(type, desc);
115 1 : if(showchanges)
116 : {
117 1 : UI::showui("changes");
118 : }
119 : }
120 :
121 0 : void notifywelcome()
122 : {
123 0 : UI::hideui("servers");
124 0 : }
125 :
126 0 : void clearchanges(int type)
127 : {
128 0 : for(int i = needsapply.size(); --i >=0;) //note reverse iteration
129 : {
130 0 : Change &c = needsapply[i];
131 0 : if(c.type&type)
132 : {
133 0 : c.type &= ~type;
134 0 : if(!c.type)
135 : {
136 0 : auto it = std::find(needsapply.begin(), needsapply.end(), needsapply[i]);
137 0 : needsapply.erase(it);
138 : }
139 : }
140 : }
141 0 : if(needsapply.empty())
142 : {
143 0 : UI::hideui("changes");
144 : }
145 0 : }
146 :
147 0 : void clearmainmenu()
148 : {
149 0 : showchanges = 1;
150 0 : if(mainmenu && multiplayer)
151 : {
152 0 : mainmenu = 0;
153 0 : UI::hideui(nullptr);
154 : }
155 0 : }
156 :
157 1 : void initmenuscmds()
158 : {
159 1 : addcommand("applychanges", reinterpret_cast<identfun>(applychanges), "", Id_Command);
160 1 : addcommand("pendingchanges", reinterpret_cast<identfun>(pendingchanges), "b", Id_Command);
161 1 : }
|