forked from stuntrally/stuntrally3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppGui_Util.cpp
256 lines (219 loc) · 7.26 KB
/
AppGui_Util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "pch.h"
#include "AppGui.h"
#include "settings.h"
#include "HlmsPbs2.h"
#include <OgreCommon.h>
#include <OgreRoot.h>
#include <OgreItem.h>
#include <OgreHlms.h>
#include <OgreHlmsPbs.h>
#include <OgreHlmsPbsDatablock.h>
#include <OgreHlmsDatablock.h>
#include <OgreHlmsUnlitDatablock.h>
#include <OgreHlmsManager.h>
#include <SDL_events.h>
using namespace Ogre;
using namespace std;
// ⛓️ util 🌐 Wireframe
//----------------------------------------------------------------
void AppGui::SetWireframe()
{
bool b = bWireframe;
SetWireframe( HLMS_PBS, b ); // 3d all
// SetWireframe( HLMS_UNLIT, b ); // 2d particles, Gui
SetWireframe( HLMS_USER3, b ); // terrain
}
void AppGui::SetWireframe(HlmsTypes type, bool wire)
{
HlmsMacroblock mb;
mb.mPolygonMode = wire ? PM_WIREFRAME : PM_SOLID;
Hlms *hlms = mRoot->getHlmsManager()->getHlms( type );
const auto &dbs = hlms->getDatablockMap();
for (auto it = dbs.begin(); it != dbs.end(); ++it)
{
// String name = itor->second.name;
// if( !StringUtil::endsWith(name, "_TrueTypeFont", false)) // Gui font
auto db = it->second.datablock;
if (db) db->setMacroblock( mb );
}
}
// ⛓️ util wrap Texture filtering
//-----------------------------------------------------------------------------------
void AppGui::SetTexWrap(HlmsTypes type, String name, bool wrap)
{
HlmsSamplerblock sb;
InitTexFiltUV(&sb, wrap);
Hlms *hlms = mRoot->getHlmsManager()->getHlms( type );
if (type == HLMS_PBS)
{ HlmsPbsDatablock *db = static_cast<HlmsPbsDatablock*>(hlms->getDatablock( name ));
if (db) db->setSamplerblock( PBSM_DIFFUSE, sb );
}
else if (type == HLMS_UNLIT)
{ HlmsUnlitDatablock *db = static_cast<HlmsUnlitDatablock*>(hlms->getDatablock( name ));
if (db) db->setSamplerblock( PBSM_DIFFUSE, sb );
}
}
void AppGui::SetTexWrap(Item* it, bool wrap)
{
SetAnisotropy(it);
return; //! ** wrap in .json, no need-
HlmsSamplerblock sb;
InitTexFiltUV(&sb, wrap);
assert( dynamic_cast< HlmsPbsDatablock *>( it->getSubItem(0)->getDatablock() ) );
HlmsPbsDatablock *db = static_cast< HlmsPbsDatablock *>( it->getSubItem(0)->getDatablock() );
for (int n=0; n < NUM_PBSM_SOURCES; ++n) // all
db->setSamplerblock( n, sb );
}
void AppGui::SetAnisotropy(Item* it)
{
assert( dynamic_cast< HlmsPbsDatablock *>( it->getSubItem(0)->getDatablock() ) );
HlmsPbsDatablock *db = static_cast< HlmsPbsDatablock *>( it->getSubItem(0)->getDatablock() );
for (int n=0; n < NUM_PBSM_SOURCES; ++n) // all
{
const HlmsSamplerblock *s = db->getSamplerblock( n );
if (s)
{
HlmsSamplerblock sb( *s );
InitTexFilt(&sb);
db->setSamplerblock( n, sb );
}
}
}
void AppGui::SetAnisotropy()
{
return; //! ** drops road UV wrap on load!
Hlms *hlms = mRoot->getHlmsManager()->getHlms( HLMS_PBS );
const auto& dbs = hlms->getDatablockMap();
for (auto it = dbs.begin(); it != dbs.end(); ++it)
{
auto* db = static_cast< HlmsPbsDatablock *>(it->second.datablock);
// if (db)
for (int n=0; n < NUM_PBSM_SOURCES; ++n) // all
{
const HlmsSamplerblock *s = db->getSamplerblock( n );
if (s)
{
HlmsSamplerblock sb( *s );
InitTexFilt(&sb);
db->setSamplerblock( n, sb );
}
}
}
}
void AppGui::InitTexFilt(HlmsSamplerblock* sb)
{
FilterOptions mia, mip;
switch (pSet->g.tex_filt)
{
case 3: mia = FO_ANISOTROPIC; mip = FO_ANISOTROPIC; break; // full anisotropic
case 2: mia = FO_ANISOTROPIC; mip = FO_LINEAR; break; // anisotropic
case 1: mia = FO_LINEAR; mip = FO_LINEAR; break; // trilinear
case 0: mia = FO_LINEAR; mip = FO_POINT; break; // bilinear
}
sb->mMinFilter = mia; sb->mMagFilter = mia;
sb->mMipFilter = mip;
sb->mMaxAnisotropy = pSet->g.anisotropy;
}
void AppGui::InitTexFiltUV(HlmsSamplerblock* sb, bool wrap)
{
InitTexFilt(sb);
auto w = wrap ? TAM_WRAP : TAM_CLAMP;
sb->mU = w; sb->mV = w; sb->mW = w;
}
// ed selected
void AppGui::UpdSelectGlow(Renderable *rend, bool selected)
{
rend->setCustomParameter(HlmsPbs2::selected_glow, Vector4(selected ? 1 : 0, 0,0,0));
HlmsDatablock *datablock = rend->getDatablock();
Hlms *hlms = datablock->getCreator();
if( hlms->getType() == HLMS_PBS )
{
assert( dynamic_cast<HlmsPbs2*>( hlms ) );
HlmsPbs2 *myHlmsPbs = static_cast<HlmsPbs2*>( hlms );
uint32 hash, casterHash;
myHlmsPbs->CalculateHashFor( rend, hash, casterHash );
rend->_setHlmsHashes( hash, casterHash );
}
}
// fix rtt for vulkan
void AppGui::BarrierResolve(TextureGpu* tex)
{
RenderSystem *rs = mSceneMgr->getDestinationRenderSystem();
rs->endCopyEncoder();
BarrierSolver &barrierSolver = rs->getBarrierSolver();
ResourceTransitionArray resTrans;
barrierSolver.resolveTransition( resTrans, tex, ResourceLayout::Texture,
ResourceAccess::Read, 1u << GPT_FRAGMENT_PROGRAM );
rs->executeResourceTransition( resTrans );
}
// Gui Input utils
//-----------------------------------------------------------------------------------
std::vector<unsigned long> AppGui::utf8ToUnicode(const string& utf8)
{
std::vector<unsigned long> unicode;
size_t i = 0;
while (i < utf8.size())
{
unsigned long uni; size_t todo;
unsigned char ch = utf8[i++];
if (ch <= 0x7F){ uni = ch; todo = 0; }
else if (ch <= 0xBF){ throw logic_error("not a UTF-8 string"); }
else if (ch <= 0xDF){ uni = ch&0x1F; todo = 1; }
else if (ch <= 0xEF){ uni = ch&0x0F; todo = 2; }
else if (ch <= 0xF7){ uni = ch&0x07; todo = 3; }
else { throw logic_error("not a UTF-8 string"); }
for (size_t j = 0; j < todo; ++j)
{
if (i == utf8.size()) throw logic_error("not a UTF-8 string");
unsigned char ch = utf8[i++];
if (ch < 0x80 || ch > 0xBF) throw logic_error("not a UTF-8 string");
uni <<= 6;
uni += ch & 0x3F;
}
if (uni >= 0xD800 && uni <= 0xDFFF) throw logic_error("not a UTF-8 string");
if (uni > 0x10FFFF) throw logic_error("not a UTF-8 string");
unicode.push_back(uni);
}
return unicode;
}
MyGUI::MouseButton AppGui::sdlButtonToMyGUI(/*Uint8*/int button)
{
switch (button)
{
case SDL_BUTTON_LEFT: return MyGUI::MouseButton::Left;
case SDL_BUTTON_RIGHT: return MyGUI::MouseButton::Right;
case SDL_BUTTON_MIDDLE: return MyGUI::MouseButton::Middle;
default: return MyGUI::MouseButton::Left;
}
}
// gui key utils
//-----------------------------------------------------------------------------------
MyGUI::KeyCode AppGui::SDL2toGUIKey(SDL_Keycode code)
{
MyGUI::KeyCode kc = MyGUI::KeyCode::None;
auto key = mKeyMap.find(code);
if (key != mKeyMap.end())
kc = key->second;
return kc;
}
void AppGui::SetupKeysForGUI()
{
mKeyMap.clear();
mKeyMap[SDLK_HOME] = MyGUI::KeyCode::Home;
mKeyMap[SDLK_END] = MyGUI::KeyCode::End;
mKeyMap[SDLK_PAGEUP] = MyGUI::KeyCode::PageUp;
mKeyMap[SDLK_PAGEDOWN] = MyGUI::KeyCode::PageDown;
mKeyMap[SDLK_UP] = MyGUI::KeyCode::ArrowUp;
mKeyMap[SDLK_DOWN] = MyGUI::KeyCode::ArrowDown;
mKeyMap[SDLK_LEFT] = MyGUI::KeyCode::ArrowLeft;
mKeyMap[SDLK_RIGHT] = MyGUI::KeyCode::ArrowRight;
mKeyMap[SDLK_DELETE] = MyGUI::KeyCode::Delete;
mKeyMap[SDLK_BACKSPACE] = MyGUI::KeyCode::Backspace;
mKeyMap[SDLK_SPACE] = MyGUI::KeyCode::Space;
mKeyMap[SDLK_ESCAPE] = MyGUI::KeyCode::Escape;
mKeyMap[SDLK_INSERT] = MyGUI::KeyCode::Insert;
mKeyMap[SDLK_RETURN] = MyGUI::KeyCode::Return;
// mKeyMap[SDLK_KP_0] = MyGUI::KeyCode::Numpad0;
// mKeyMap[SDLK_KP_ENTER] = MyGUI::KeyCode::NumpadEnter;
// Don't need more, rest comes in textInput
}