/
baseguiapp.cpp
136 lines (115 loc) · 3.74 KB
/
baseguiapp.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
/** @file baseguiapp.cpp Base class for GUI applications.
*
* @authors Copyright (c) 2014 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/BaseGuiApp"
#include "de/VRConfig"
#include <de/Function>
#include <de/ArrayValue>
#include <de/DictionaryValue>
#include <de/NativeFont>
#include <QFontDatabase>
namespace de {
static Value *Function_App_LoadFont(Context &, Function::ArgumentValues const &args)
{
try
{
// Try to load the specific font.
Block data(App::rootFolder().locate<File const>(args.at(0)->asText()));
int id;
id = QFontDatabase::addApplicationFontFromData(data);
if(id < 0)
{
LOG_RES_WARNING("Failed to load font:");
}
else
{
LOG_RES_VERBOSE("Loaded font: %s") << args.at(0)->asText();
//qDebug() << args.at(0)->asText();
//qDebug() << "Families:" << QFontDatabase::applicationFontFamilies(id);
}
}
catch(Error const &er)
{
LOG_RES_WARNING("Failed to load font:\n") << er.asText();
}
return 0;
}
static Value *Function_App_AddFontMapping(Context &, Function::ArgumentValues const &args)
{
// arg 0: family name
// arg 1: dictionary with [Text style, Number weight] => Text fontname
// styles: regular, italic
// weight: 0-99 (25=light, 50=normal, 75=bold)
NativeFont::StyleMapping mapping;
DictionaryValue const &dict = args.at(1)->as<DictionaryValue>();
DENG2_FOR_EACH_CONST(DictionaryValue::Elements, i, dict.elements())
{
NativeFont::Spec spec;
ArrayValue const &key = i->first.value->as<ArrayValue>();
if(key.at(0).asText() == "italic")
{
spec.style = NativeFont::Italic;
}
spec.weight = roundi(key.at(1).asNumber());
mapping.insert(spec, i->second->asText());
}
NativeFont::defineMapping(args.at(0)->asText(), mapping);
return 0;
}
DENG2_PIMPL_NOREF(BaseGuiApp)
{
Binder binder;
QScopedPointer<PersistentState> uiState;
GLShaderBank shaders;
WaveformBank waveforms;
VRConfig vr;
};
BaseGuiApp::BaseGuiApp(int &argc, char **argv)
: GuiApp(argc, argv), d(new Instance)
{
// Override the system locale (affects number/time formatting).
QLocale::setDefault(QLocale("en_US.UTF-8"));
d->binder.init(scriptSystem().nativeModule("App"))
<< DENG2_FUNC (App_AddFontMapping, "addFontMapping", "family" << "mappings")
<< DENG2_FUNC (App_LoadFont, "loadFont", "fileName");
}
void BaseGuiApp::initSubsystems(SubsystemInitFlags flags)
{
GuiApp::initSubsystems(flags);
d->uiState.reset(new PersistentState("UIState"));
}
BaseGuiApp &BaseGuiApp::app()
{
return static_cast<BaseGuiApp &>(App::app());
}
PersistentState &BaseGuiApp::persistentUIState()
{
return *app().d->uiState;
}
GLShaderBank &BaseGuiApp::shaders()
{
return app().d->shaders;
}
WaveformBank &BaseGuiApp::waveforms()
{
return app().d->waveforms;
}
VRConfig &BaseGuiApp::vr()
{
return app().d->vr;
}
} // namespace de