Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
abstractrandomstring.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 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
22
23#include <QVector>
24#include <QJsonArray>
25#include <QtMath>
26#include <QQmlEngine>
27
28#include "../util/gamedata.h"
29#include <pse-common/random.h>
30#include <pse-common/utility.h>
31
33{
34 store.clear();
35
36 // Grab Event Pokemon Data
37 auto jsonData = GameData::inst()->json(fileName);
38
39 // Go through each event Pokemon
40 for(QJsonValue jsonEntry : jsonData.array())
41 {
42 // Add to array
43 store.append(jsonEntry.toString());
44 }
45
46 listChanged();
47}
48
49void AbstractRandomString::qmlProtect(const QQmlEngine* const engine) const
50{
51 Utility::qmlProtectUtil(this, engine);
52}
53
59
60const QVector<QString> AbstractRandomString::getStore() const
61{
62 return store;
63}
64
66{
67 return store.size();
68}
69
70const QString AbstractRandomString::getStoreAt(const int ind) const
71{
72 // Guard both ends -- a negative ind (e.g. QML's -1 "no selection") must not
73 // reach store.at(). Matches the canonical guard used by the DB store accessors.
74 if(ind < 0 || ind >= store.size())
75 return QString();
76
77 return store.at(ind);
78}
79
81{
82 int index = Random::inst()->rangeExclusive(0, store.size());
83 QString ret = store.at(index);
84 store.removeAt(index);
85
86 if(store.size() == 0)
87 load();
88 else
89 listChanged();
90
91 return ret;
92}
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
const QVector< QString > getStore() const
All strings.
QString randomExample()
A random string from the list.
AbstractRandomString(const QString fileName)
int getStoreSize() const
String count.
const QString getStoreAt(const int ind) const
String at ind (for QML).
const QString fileName
Asset path (set by the subclass).
void load()
Load the strings from fileName.
QVector< QString > store
The loaded strings.
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 Random * inst()
< Convenience 50% coin flip (integer path), readable from QML.
Definition random.cpp:31
int rangeExclusive(const int start, const int end) const
Random integer in the half-open interval [start, end).
Definition random.cpp:53
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