Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
sprites.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 "./sprites.h"
27#include "./util/gamedata.h"
28
31{
32 name = data["name"].toString();
33 ind = static_cast<var8>(data["ind"].toDouble());
34}
35
36SpritesDB* SpritesDB::inst()
37{
38 static SpritesDB* _inst = new SpritesDB;
39 return _inst;
40}
41
42const QVector<SpriteDBEntry*> SpritesDB::getStore() const { return store; }
43const QHash<QString, SpriteDBEntry*> SpritesDB::getInd() const { return ind; }
44int SpritesDB::getStoreSize() const { return store.size(); }
45
47{
48 if (idx < 0 || idx >= store.size()) return nullptr;
49 return store.at(idx);
50}
51
52SpriteDBEntry* SpritesDB::getIndAt(const QString& key) const
53{
54 return ind.value(key, nullptr);
55}
56
58{
59 static bool once = false;
60 if (once) return;
61 auto jsonData = GameData::inst()->json("sprites");
62 for (QJsonValue entry : jsonData.array())
63 store.append(new SpriteDBEntry(entry));
64 once = true;
65}
66
68{
69 static bool once = false;
70 if (once) return;
71 for (auto* entry : store) {
72 ind.insert(entry->name, entry);
73 ind.insert(QString::number(entry->ind), entry);
74 }
75 once = true;
76}
77
78void SpritesDB::qmlProtect(const QQmlEngine* const engine) const
79{
80 Utility::qmlProtectUtil(this, engine);
81}
82
83void SpritesDB::qmlRegister() const
84{
85 static bool once = false;
86 if (once) return;
87 qmlRegisterUncreatableType<SpritesDB>("PSE.DB.SpritesDB", 1, 0, "SpritesDB", "Can't instantiate in QML");
88 once = true;
89}
90
91SpritesDB::SpritesDB()
92{
93 qmlRegister();
94}
const QJsonDocument json(const QString filename) const
Parsed document for filename.
Definition gamedata.cpp:45
static GameData * inst()
The process-wide GameData singleton.
Definition gamedata.cpp:71
static SpritesDB * inst()
< Number of sprites.
Definition sprites.cpp:36
SpriteDBEntry * getIndAt(const QString &key) const
Sprite by name key (for QML).
Definition sprites.cpp:52
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition sprites.cpp:78
const QHash< QString, SpriteDBEntry * > getInd() const
Name->entry index.
Definition sprites.cpp:43
void load()
Load sprites from JSON.
Definition sprites.cpp:57
const QVector< SpriteDBEntry * > getStore() const
All sprites.
Definition sprites.cpp:42
void index()
Build the name->entry index.
Definition sprites.cpp:67
int getStoreSize() const
Sprite count.
Definition sprites.cpp:44
SpriteDBEntry * getStoreAt(int idx) const
Sprite by store index (for QML).
Definition sprites.cpp:46
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
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition types.h:124
One sprite definition: its name/picture-id and the maps that use it.
Definition sprites.h:38
var8 ind
Picture id.
Definition sprites.h:43
QString name
Sprite name (key).
Definition sprites.h:42
SpriteDBEntry()
Empty entry.
Definition sprites.cpp:29