Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
types.cpp
Go to the documentation of this file.
1
/*
2
* Copyright 2019 Twilight
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
21
22
#include <QJsonArray>
23
#include <QQmlEngine>
24
#include <
pse-common/utility.h
>
25
26
#include "
./types.h
"
27
#include "
./util/gamedata.h
"
28
29
TypeDBEntry::TypeDBEntry
() {}
30
TypeDBEntry::TypeDBEntry
(QJsonValue& data)
31
{
32
name
= data[
"name"
].toString();
33
ind
=
static_cast<
var8
>
(data[
"ind"
].toDouble());
34
readable
= data[
"readable"
].toString();
35
}
36
37
TypesDB*
TypesDB::inst
()
38
{
39
static
TypesDB* _inst =
new
TypesDB;
40
return
_inst;
41
}
42
43
const
QVector<TypeDBEntry*>
TypesDB::getStore
()
const
{
return
store; }
44
const
QHash<QString, TypeDBEntry*>
TypesDB::getInd
()
const
{
return
ind; }
45
int
TypesDB::getStoreSize
()
const
{
return
store.size(); }
46
47
TypeDBEntry
*
TypesDB::getStoreAt
(
int
idx)
const
48
{
49
if
(idx < 0 || idx >= store.size())
return
nullptr
;
50
return
store.at(idx);
51
}
52
53
TypeDBEntry
*
TypesDB::getIndAt
(
const
QString& key)
const
54
{
55
return
ind.value(key,
nullptr
);
56
}
57
58
void
TypesDB::load
()
59
{
60
static
bool
once =
false
;
61
if
(once)
return
;
62
auto
jsonData =
GameData::inst
()->
json
(
"types"
);
63
for
(QJsonValue entry : jsonData.array())
64
store.append(
new
TypeDBEntry
(entry));
65
once =
true
;
66
}
67
68
void
TypesDB::index
()
69
{
70
static
bool
once =
false
;
71
if
(once)
return
;
72
for
(
auto
* entry : store) {
73
ind.insert(entry->name, entry);
74
ind.insert(QString::number(entry->ind), entry);
75
ind.insert(entry->readable, entry);
76
}
77
once =
true
;
78
}
79
80
void
TypesDB::qmlProtect
(
const
QQmlEngine*
const
engine)
const
81
{
82
Utility::qmlProtectUtil
(
this
, engine);
83
}
84
85
void
TypesDB::qmlRegister()
const
86
{
87
static
bool
once =
false
;
88
if
(once)
return
;
89
qmlRegisterUncreatableType<TypesDB>(
"PSE.DB.TypesDB"
, 1, 0,
"TypesDB"
,
"Can't instantiate in QML"
);
90
once =
true
;
91
}
92
93
TypesDB::TypesDB()
94
{
95
qmlRegister();
96
}
GameData::json
const QJsonDocument json(const QString filename) const
Parsed document for filename.
Definition
gamedata.cpp:45
GameData::inst
static GameData * inst()
The process-wide GameData singleton.
Definition
gamedata.cpp:71
TypesDB::load
void load()
Load types from JSON.
Definition
types.cpp:58
TypesDB::getIndAt
TypeDBEntry * getIndAt(const QString &key) const
Type by name key (for QML).
Definition
types.cpp:53
TypesDB::getStoreSize
int getStoreSize() const
Type count.
Definition
types.cpp:45
TypesDB::inst
static TypesDB * inst()
< Number of types.
Definition
types.cpp:37
TypesDB::getStore
const QVector< TypeDBEntry * > getStore() const
All types.
Definition
types.cpp:43
TypesDB::getInd
const QHash< QString, TypeDBEntry * > getInd() const
Name->entry index.
Definition
types.cpp:44
TypesDB::index
void index()
Build the name->entry index.
Definition
types.cpp:68
TypesDB::qmlProtect
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition
types.cpp:80
TypesDB::getStoreAt
TypeDBEntry * getStoreAt(int idx) const
Type by store index (for QML).
Definition
types.cpp:47
Utility::qmlProtectUtil
static void qmlProtectUtil(const QObject *const obj, const QQmlEngine *const engine)
Pin obj to C++ ownership so the QML engine never garbage-collects it.
Definition
utility.cpp:63
var8
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition
types.h:124
types.h
gamedata.h
TypeDBEntry
One elemental type: its name plus the moves and Pokemon of that type.
Definition
types.h:39
TypeDBEntry::readable
QString readable
Human-readable type name.
Definition
types.h:45
TypeDBEntry::ind
var8 ind
Type index/id.
Definition
types.h:44
TypeDBEntry::TypeDBEntry
TypeDBEntry()
Empty entry.
Definition
types.cpp:29
TypeDBEntry::name
QString name
Internal type name (key).
Definition
types.h:43
utility.h
projects
db
src
pse-db
types.cpp
Generated by
1.17.0