VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
Util.h
1 /*
2  * Copyright (C) 2014 Google Inc.
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 
17 #ifndef UTIL_H
18 #define UTIL_H
19 
20 #include <QPointF>
21 #include <QQmlListProperty>
22 #include <QRectF>
23 #include <QTouchEvent>
24 #include <QVariantList>
25 #include <algorithm>
26 #include <cstdlib>
27 #include "VariantConverter.h"
28 
29 class QSGTexture;
30 class QUrl;
31 class QWindow;
32 
37 namespace Util {
38 
46 template <typename T>
47 QVariantList toVariantList(const QList<T>& list) {
48  QVariantList newList;
49  newList.reserve(list.size());
50  VariantConverter<T> converter;
51  for (const T& item : list) {
52  newList.append(converter.toVariant(item));
53  }
54  return newList;
55 }
56 
64 template <typename T>
65 QList<T> toList(const QVariantList& list) {
66  QList<T> convertedList;
67  convertedList.reserve(list.size());
68  VariantConverter<T> converter;
69  for (const QVariant& v : list) {
70  convertedList.append(converter.fromVariant(v));
71  }
72  return convertedList;
73 }
74 
82 template <typename T>
83 QVariantMap toVariantMap(const QMap<QString, T> map) {
84  QVariantMap convertedMap;
85  VariantConverter<T> converter;
86  for (auto it = map.cbegin(); it != map.cend(); ++it) {
87  convertedMap.insert(it.key(), converter.toVariant(it.value()));
88  }
89  return convertedMap;
90 }
91 
99 template <typename T>
100 QMap<QString, T> toMap(const QVariantMap& map) {
101  QMap<QString, T> convertedMap;
102  VariantConverter<T> converter;
103  for (auto it = map.cbegin(); it != map.cend(); ++it) {
104  convertedMap.insert(it.key(), converter.fromVariant(it.value()));
105  }
106  return convertedMap;
107 }
108 
124 template <typename TObject, typename TData, typename TListType, TListType TObject::* TObjectField,
125  void (TObject::* TChangeFunc)() = nullptr>
127 public:
128 
132  static QQmlListProperty<TData> createList(TObject* owner) {
134  owner,
135  nullptr,
136  &QQmlListPropertyOnQList::appendFunc,
137  &QQmlListPropertyOnQList::countFunc,
138  &QQmlListPropertyOnQList::atFunc,
139  &QQmlListPropertyOnQList::clearFunc);
140  }
141 
142 private:
143  static void appendFunc(QQmlListProperty<TData>* property, TData* value) {
144  TObject* owner = static_cast<TObject*>(property->object);
145  TListType& list = (owner->*TObjectField);
146  list.append(value);
147  callChangeFunc(property);
148  }
149 
150  static int countFunc(QQmlListProperty<TData>* property) {
151  TObject* owner = static_cast<TObject*>(property->object);
152  TListType& list = (owner->*TObjectField);
153  return list.size();
154  }
155 
156  static TData* atFunc(QQmlListProperty<TData>* property, int index) {
157  TObject* owner = static_cast<TObject*>(property->object);
158  TListType& list = (owner->*TObjectField);
159  return list.at(index);
160  }
161 
162  static void clearFunc(QQmlListProperty<TData>* property) {
163  TObject* owner = static_cast<TObject*>(property->object);
164  TListType& list = (owner->*TObjectField);
165  list.clear();
166  callChangeFunc(property);
167  }
168 
169  static void callChangeFunc(QQmlListProperty<TData>* property) {
170  TObject* owner = static_cast<TObject*>(property->object);
171  if (TChangeFunc) {
172  (owner->*TChangeFunc)();
173  }
174  }
175 };
176 
183 template <typename T>
184 inline const T& clamp(const T& a, const T& min, const T& max) {
185  return std::max<T>(min, std::min<T>(max, a));
186 }
187 
195 static inline float smoothstep(float t) {
196  float t2 = t * t;
197  float t3 = t * t2;
198  float t4 = t2 * t2;
199  float t5 = t2 * t3;
200  return 6 * t5 - 15 * t4 + 10 * t3;
201 }
202 
206 static inline float qrandF() {
207  return qrand() / ((float) RAND_MAX + 1);
208 }
209 
210 
215 template <typename TObject>
216 TObject* findParentOfType(QObject* node) {
217  node = node->parent();
218  while (node) {
219  TObject* obj = qobject_cast<TObject*>(node);
220  if (obj) {
221  return obj;
222  }
223  node = node->parent();
224  }
225  return nullptr;
226 }
227 
232 QString getPathToAsset(const QString& assetPath);
233 
238 QUrl getUrlPathToAsset(const QString& assetPath);
239 
244 QString getPathToFont(const QString& fontPath);
245 
250 QString getPathToImage(const QString& imagePath);
251 
256 QString getPathToMovie(const QString& moviePath);
257 
262 QString getPathToLevel(const QString& levelPath);
263 
268 QString getPathToSound(const QString& soundPath);
269 
274 QString getPathToData(const QString& dataPath);
275 
280 QString readFileAsQString(const QString& path);
281 
286 std::string readFileAsStdString(const QString& path);
287 
296 QRectF united(const QRectF& rect, const QPointF& point);
297 
303 bool javaScriptFuncExists(QObject* object, const char* method);
304 
308 bool deviceHasTouchScreen();
309 
314 
315 } // namespace Util
316 
317 #endif // UTIL_H
const_iterator cend() const
QString getPathToSound(const QString &soundPath)
Returns the platform dependent path to the sound in soundPath.
QMap< QString, T > toMap(const QVariantMap &map)
Returns map converted to a QMap.
Definition: Util.h:100
void reserve(int alloc)
bool deviceHasTouchScreen()
Returns true if the device on which the game is running has a touch screen.
QRectF united(const QRectF &rect, const QPointF &point)
Expands a QRectF's span to include a point.
QUrl getUrlPathToAsset(const QString &assetPath)
Returns platform dependent path URL to assetPath.
QString getPathToMovie(const QString &moviePath)
Returns the platform dependent path to the movie in moviePath.
int size() const
QString getPathToData(const QString &dataPath)
Returns the platform dependent path to the data file in dataPath.
void append(const T &value)
QVariantMap toVariantMap(const QMap< QString, T > map)
Returns map converted to a QVariantMap.
Definition: Util.h:83
QList< T > toList(const QVariantList &list)
Returns list converted to a QList.
Definition: Util.h:65
QVariant toVariant(const T &value)
Returns value converted as a QVariant.
Definition: VariantConverter.h:45
QVariantList toVariantList(const QList< T > &list)
Returns list converted to a QVariantList.
Definition: Util.h:47
static float qrandF()
Returns a pseudo-random number in the range [0, 1).
Definition: Util.h:206
static float smoothstep(float t)
Computes the value of a smoothed interpolation curve between 0.0f and 1.0f.
Definition: Util.h:195
int getDeviceTouchScreenDeviceId()
Returns the device id of the touch screen on the device, or -1 if none exists.
QString readFileAsQString(const QString &path)
Returns the entire contents of a text file as a QString.
const_iterator cbegin() const
TObject * findParentOfType(QObject *node)
Returns our closest ancestor which is of type TObject.
Definition: Util.h:216
Helper class which creates QQmlListProperty objects directly on QList members fields.
Definition: Util.h:126
QString getPathToAsset(const QString &assetPath)
Returns the platform dependent path to assetPath.
static QQmlListProperty< TData > createList(TObject *owner)
Create and return a QQmlListProperty.
Definition: Util.h:132
QString getPathToImage(const QString &imagePath)
Returns the platform dependent path to the image in imagePath.
const T & clamp(const T &a, const T &min, const T &max)
Returns the value a clamped between min and max.
Definition: Util.h:184
iterator insert(const Key &key, const T &value)
bool javaScriptFuncExists(QObject *object, const char *method)
Checks if a javascript function exists.
QString getPathToFont(const QString &fontPath)
Returns the platform dependent path to the font in fontPath.
QString getPathToLevel(const QString &levelPath)
Returns the platform dependent path to the level in levelPath.
QObject * parent() const
Helper template class to convert QVariants to and from a specific type, for example, QPointFs.
Definition: VariantConverter.h:59
std::string readFileAsStdString(const QString &path)
Returns the entire contents of a text file as a std::string.