1 /*
2  * tilesetmanager.h
3  * Copyright 2008-2014, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
4  * Copyright 2009, Edward Hutchins <eah1@yahoo.com>
5  *
6  * This file is part of libtiled.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  *    1. Redistributions of source code must retain the above copyright notice,
12  *       this list of conditions and the following disclaimer.
13  *
14  *    2. Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in the
16  *       documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #pragma once
31 
32 #include "tileset.h"
33 
34 #include <QObject>
35 #include <QList>
36 #include <QString>
37 
38 namespace Tiled {
39 
40 class FileSystemWatcher;
41 class TileAnimationDriver;
42 
43 /**
44  * The tileset manager keeps track of all tilesets used by loaded maps. It also
45  * watches the tileset images for changes and will attempt to reload them when
46  * they change.
47  */
48 class TILEDSHARED_EXPORT TilesetManager : public QObject
49 {
50     Q_OBJECT
51     Q_DISABLE_COPY(TilesetManager)
52 
53     TilesetManager();
54     ~TilesetManager() override;
55 
56 public:
57     static TilesetManager *instance();
58     static void deleteInstance();
59 
60     SharedTileset loadTileset(const QString &fileName, QString *error = nullptr);
61     SharedTileset findTileset(const QString &fileName) const;
62 
63     // Only meant to be used by the Tileset class
64     void addTileset(Tileset *tileset);
65     void removeTileset(Tileset *tileset);
66 
67     void reloadImages(Tileset *tileset);
68 
69     void setReloadTilesetsOnChange(bool enabled);
70     bool reloadTilesetsOnChange() const;
71 
72     void setAnimateTiles(bool enabled);
73     bool animateTiles() const;
74 
75     void advanceTileAnimations(int ms);
76     void resetTileAnimations();
77 
78     void tilesetImageSourceChanged(const Tileset &tileset,
79                                    const QUrl &oldImageSource);
80 
81 signals:
82     /**
83      * Emitted when a tileset's images have changed and views need updating.
84      */
85     void tilesetImagesChanged(Tileset *tileset);
86 
87     /**
88      * Emitted when any images of the tiles in the given \a tileset have
89      * changed as a result of playing tile animations.
90      */
91     void repaintTileset(Tileset *tileset);
92 
93 private:
94     void filesChanged(const QStringList &fileNames);
95 
96     /**
97      * The list of loaded tilesets (weak references).
98      */
99     QList<Tileset*> mTilesets;
100     FileSystemWatcher *mWatcher;
101     TileAnimationDriver *mAnimationDriver;
102     bool mReloadTilesetsOnChange;
103 
104     static TilesetManager *mInstance;
105 };
106 
reloadTilesetsOnChange()107 inline bool TilesetManager::reloadTilesetsOnChange() const
108 { return mReloadTilesetsOnChange; }
109 
110 } // namespace Tiled
111