Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
tileset.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 "./tileset.h"
27#include "./util/gamedata.h"
28
31{
32 name = data["name"].toString();
33 type = data["type"].toString();
34 nameAlias = data["nameAlias"].toString();
35 typeAlias = data["typeAlias"].toString();
36
37 ind = static_cast<var8>(data["ind"].toDouble());
38 grass = static_cast<var8>(data["grass"].toDouble());
39 bank = static_cast<var8>(data["bank"].toDouble());
40 blockPtr = static_cast<var16>(data["blockPtr"].toDouble());
41 gfxPtr = static_cast<var16>(data["gfxPtr"].toDouble());
42 collPtr = static_cast<var16>(data["collPtr"].toDouble());
43
44 QJsonValue talkArr = data["talk"].toArray();
45 for (var8 i = 0; i < talkCount; ++i)
46 talk[i] = static_cast<var8>(talkArr[i].toDouble());
47}
48
50{
51 if (type == "Outdoor") return TilesetType::OUTDOOR;
52 if (type == "Cave") return TilesetType::CAVE;
54}
55
56TilesetDB* TilesetDB::inst()
57{
58 static TilesetDB* _inst = new TilesetDB;
59 return _inst;
60}
61
62const QVector<TilesetDBEntry*> TilesetDB::getStore() const { return store; }
63const QHash<QString, TilesetDBEntry*> TilesetDB::getInd() const { return ind; }
64int TilesetDB::getStoreSize() const { return store.size(); }
65
67{
68 if (idx < 0 || idx >= store.size()) return nullptr;
69 return store.at(idx);
70}
71
72TilesetDBEntry* TilesetDB::getIndAt(const QString& key) const
73{
74 return ind.value(key, nullptr);
75}
76
78{
79 static bool once = false;
80 if (once) return;
81 auto jsonData = GameData::inst()->json("tileset");
82 for (QJsonValue entry : jsonData.array())
83 store.append(new TilesetDBEntry(entry));
84 once = true;
85}
86
88{
89 static bool once = false;
90 if (once) return;
91 for (auto* entry : store) {
92 ind.insert(entry->name, entry);
93 ind.insert(entry->nameAlias, entry);
94 }
95 once = true;
96}
97
98void TilesetDB::qmlProtect(const QQmlEngine* const engine) const
99{
100 Utility::qmlProtectUtil(this, engine);
101}
102
103void TilesetDB::qmlRegister() const
104{
105 static bool once = false;
106 if (once) return;
107 qmlRegisterUncreatableType<TilesetDB>("PSE.DB.TilesetDB", 1, 0, "TilesetDB", "Can't instantiate in QML");
108 once = true;
109}
110
111TilesetDB::TilesetDB()
112{
113 qmlRegister();
114}
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
TilesetDBEntry * getStoreAt(int idx) const
Tileset by store index (for QML).
Definition tileset.cpp:66
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition tileset.cpp:98
const QHash< QString, TilesetDBEntry * > getInd() const
Name->entry index.
Definition tileset.cpp:63
const QVector< TilesetDBEntry * > getStore() const
All tilesets.
Definition tileset.cpp:62
void load()
Load tilesets from JSON.
Definition tileset.cpp:77
TilesetDBEntry * getIndAt(const QString &key) const
Tileset by name key (for QML).
Definition tileset.cpp:72
static TilesetDB * inst()
< Number of tilesets.
Definition tileset.cpp:56
int getStoreSize() const
Tileset count.
Definition tileset.cpp:64
void index()
Build the name->entry index.
Definition tileset.cpp:87
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
var16e var16
Everyday 16-bit alias. Exact width to avoid the "fastest" widening bug.
Definition types.h:125
One tileset definition: its type, graphics/block/collision pointers, etc.
Definition tileset.h:45
var16 collPtr
Collision pointer.
Definition tileset.h:62
QString nameAlias
Friendly name.
Definition tileset.h:51
QString typeAlias
Friendly type label.
Definition tileset.h:52
TilesetType typeAsEnum() const
type parsed to the TilesetType enum.
Definition tileset.cpp:49
QString type
Behaviour type as a string.
Definition tileset.h:50
var8 talk[3]
Talk-over tile ids.
Definition tileset.h:57
TilesetDBEntry()
Empty entry.
Definition tileset.cpp:29
var16 blockPtr
Blocks pointer.
Definition tileset.h:60
var8 bank
Bank holding GFX + blocks.
Definition tileset.h:59
var8 grass
Grass tile id.
Definition tileset.h:58
var8 ind
Tileset index.
Definition tileset.h:56
var16 gfxPtr
Graphics pointer.
Definition tileset.h:61
QString name
Internal tileset name (key).
Definition tileset.h:49
constexpr var8 talkCount
Number of "talk-over" tile slots per tileset.
Definition tileset.h:33
TilesetType
The three tileset behaviour categories.
Definition tileset.h:31