Skip to content

Commit 65a6a33

Browse files
committed
Expose session API to scripting as tiled.session
1 parent b02fc9e commit 65a6a33

File tree

7 files changed

+188
-0
lines changed

7 files changed

+188
-0
lines changed

docs/scripting-doc/index.d.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4632,6 +4632,53 @@ declare namespace Tiled {
46324632
const Zstandard: CompressionMethod;
46334633
}
46344634

4635+
4636+
/**
4637+
* Provides access to session-specific settings. The session stores
4638+
* per-project preferences like last-used tile sizes, open files, and
4639+
* file states.
4640+
*
4641+
* Values are accessed by their string key (e.g. `"map.tileWidth"`).
4642+
*
4643+
* @since 1.12
4644+
*/
4645+
interface Session {
4646+
/**
4647+
* Path to the current session file.
4648+
*/
4649+
readonly fileName: string;
4650+
4651+
/**
4652+
* Returns the session value for the given key, or `defaultValue` if
4653+
* the key is not set.
4654+
*/
4655+
get(key: string, defaultValue?: any): any;
4656+
4657+
/**
4658+
* Sets the session value for the given key.
4659+
*/
4660+
set(key: string, value: any): void;
4661+
4662+
/**
4663+
* Returns whether the given key is present in the session.
4664+
*/
4665+
isSet(key: string): boolean;
4666+
4667+
/**
4668+
* Returns the per-file state for the given file name.
4669+
*/
4670+
fileState(fileName: string): { [key: string]: any };
4671+
4672+
/**
4673+
* Sets the per-file state for the given file name.
4674+
*/
4675+
setFileState(fileName: string, fileState: { [key: string]: any }): void;
4676+
4677+
/**
4678+
* Sets a single value in the per-file state for the given file name.
4679+
*/
4680+
setFileStateValue(fileName: string, name: string, value: any): void;
4681+
}
46354682
/**
46364683
* The `tiled` module is the main entry point and provides properties,
46374684
* functions and signals which are documented below.
@@ -4767,6 +4814,18 @@ declare namespace tiled {
47674814
*/
47684815
export const tilesetEditor: TilesetEditor;
47694816

4817+
/**
4818+
* Provides access to the current session, allowing scripts to read and
4819+
* write session-specific settings like last-used tile size.
4820+
*
4821+
* In command-line mode (`--evaluate`), no session may be available. In
4822+
* that case, methods return safe defaults (empty strings, default
4823+
* values, etc.).
4824+
*
4825+
* @since 1.12
4826+
*/
4827+
export const session: Session;
4828+
47704829
/**
47714830
* This function can be used to trigger any registered action. This
47724831
* includes most actions you would normally trigger through the menu or

src/tiled/libtilededitor.qbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ DynamicLibrary {
465465
"scriptmodule.h",
466466
"scriptprocess.cpp",
467467
"scriptprocess.h",
468+
"scriptsession.cpp",
469+
"scriptsession.h",
468470
"scriptpropertytype.cpp",
469471
"scriptpropertytype.h",
470472
"selectionrectangle.cpp",

src/tiled/scriptmodule.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "scriptfileformatwrappers.h"
3838
#include "scriptimage.h"
3939
#include "scriptmanager.h"
40+
#include "scriptsession.h"
4041
#include "tilesetdocument.h"
4142
#include "tileseteditor.h"
4243
#include "worlddocument.h"
@@ -56,6 +57,7 @@ namespace Tiled {
5657

5758
ScriptModule::ScriptModule(QObject *parent)
5859
: QObject(parent)
60+
, mSession(new ScriptSession(this))
5961
{
6062
// If the script module is only created for command-line use, there will
6163
// not be a DocumentManager instance.
@@ -240,6 +242,11 @@ MapEditor *ScriptModule::mapEditor() const
240242
return nullptr;
241243
}
242244

245+
ScriptSession *ScriptModule::session() const
246+
{
247+
return mSession;
248+
}
249+
243250
QColor ScriptModule::color(const QString &name) const
244251
{
245252
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)

src/tiled/scriptmodule.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Document;
3838
class EditableAsset;
3939
class MapEditor;
4040
class ScriptImage;
41+
class ScriptSession;
4142
class ScriptMapFormatWrapper;
4243
class ScriptTilesetFormatWrapper;
4344
class ScriptedAction;
@@ -75,6 +76,8 @@ class ScriptModule : public QObject
7576
Q_PROPERTY(Tiled::TilesetEditor *tilesetEditor READ tilesetEditor)
7677
Q_PROPERTY(QList<QObject*> worlds READ worlds)
7778

79+
Q_PROPERTY(Tiled::ScriptSession *session READ session)
80+
7881
public:
7982
ScriptModule(QObject *parent = nullptr);
8083
~ScriptModule() override;
@@ -105,6 +108,8 @@ class ScriptModule : public QObject
105108
TilesetEditor *tilesetEditor() const;
106109
MapEditor *mapEditor() const;
107110

111+
ScriptSession *session() const;
112+
108113
Q_INVOKABLE QColor color(const QString &name) const;
109114
Q_INVOKABLE QColor color(float r, float g, float b, float a = 1.0f) const;
110115
Q_INVOKABLE Tiled::FilePath filePath(const QUrl &path) const;
@@ -194,6 +199,7 @@ public slots:
194199
std::map<Id, std::unique_ptr<ScriptedTool>> mRegisteredTools;
195200

196201
QStringList mScriptArguments;
202+
ScriptSession *mSession = nullptr;
197203
};
198204

199205
inline bool ScriptModule::versionLessThan(const QString &a)

src/tiled/scriptsession.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "scriptsession.h"
2+
3+
#include "session.h"
4+
5+
namespace Tiled {
6+
7+
ScriptSession::ScriptSession(QObject *parent)
8+
: QObject(parent)
9+
{}
10+
11+
QString ScriptSession::fileName() const
12+
{
13+
if (!Session::hasCurrent())
14+
return QString();
15+
return Session::current().fileName();
16+
}
17+
18+
QVariant ScriptSession::get(const QString &key,
19+
const QVariant &defaultValue) const
20+
{
21+
if (!Session::hasCurrent())
22+
return defaultValue;
23+
24+
const QByteArray latin1Key = key.toLatin1();
25+
auto &session = Session::current();
26+
27+
if (!session.isSet(latin1Key.constData()))
28+
return defaultValue;
29+
30+
return session.get<QVariant>(latin1Key.constData());
31+
}
32+
33+
void ScriptSession::set(const QString &key, const QVariant &value)
34+
{
35+
if (!Session::hasCurrent())
36+
return;
37+
Session::current().set(key.toLatin1().constData(), value);
38+
}
39+
40+
bool ScriptSession::isSet(const QString &key) const
41+
{
42+
if (!Session::hasCurrent())
43+
return false;
44+
return Session::current().isSet(key.toLatin1().constData());
45+
}
46+
47+
QVariantMap ScriptSession::fileState(const QString &fileName) const
48+
{
49+
if (!Session::hasCurrent())
50+
return {};
51+
return Session::current().fileState(fileName);
52+
}
53+
54+
void ScriptSession::setFileState(const QString &fileName,
55+
const QVariantMap &fileState)
56+
{
57+
if (!Session::hasCurrent())
58+
return;
59+
Session::current().setFileState(fileName, fileState);
60+
}
61+
62+
void ScriptSession::setFileStateValue(const QString &fileName,
63+
const QString &name,
64+
const QVariant &value)
65+
{
66+
if (!Session::hasCurrent())
67+
return;
68+
Session::current().setFileStateValue(fileName, name, value);
69+
}
70+
71+
} // namespace Tiled
72+
73+
#include "moc_scriptsession.cpp"

src/tiled/scriptsession.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
#pragma once
3+
4+
#include <QObject>
5+
#include <QVariant>
6+
#include <QVariantMap>
7+
8+
namespace Tiled {
9+
10+
/**
11+
* Exposes the current Session to the scripting API as tiled.session.
12+
*
13+
* Provides generic get/set access to session values by their string key,
14+
* as well as access to per-file states.
15+
*/
16+
class ScriptSession : public QObject
17+
{
18+
Q_OBJECT
19+
20+
Q_PROPERTY(QString fileName READ fileName)
21+
22+
public:
23+
explicit ScriptSession(QObject *parent = nullptr);
24+
25+
QString fileName() const;
26+
27+
Q_INVOKABLE QVariant get(const QString &key,
28+
const QVariant &defaultValue = QVariant()) const;
29+
Q_INVOKABLE void set(const QString &key, const QVariant &value);
30+
Q_INVOKABLE bool isSet(const QString &key) const;
31+
32+
Q_INVOKABLE QVariantMap fileState(const QString &fileName) const;
33+
Q_INVOKABLE void setFileState(const QString &fileName,
34+
const QVariantMap &fileState);
35+
Q_INVOKABLE void setFileStateValue(const QString &fileName,
36+
const QString &name,
37+
const QVariant &value);
38+
};
39+
40+
}

src/tiled/session.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ class TILED_EDITOR_EXPORT Session : protected FileHelper
212212

213213
static Session &initialize();
214214
static Session &current();
215+
static bool hasCurrent() { return mCurrent != nullptr; }
215216
static Session &switchCurrent(const QString &fileName);
216217
static void deinitialize();
217218

0 commit comments

Comments
 (0)