Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose basic properties of Tiled projects to scripting #3622

Merged
merged 13 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Unreleased

* Scripting: Added read-only access to Project properties (by dogboydog, #3622)
* Fixed object labels to adjust to application font changes
* Fixed grid rendering for odd Hex Side Length values (#3623)
* Fixed tile stamp getting messed up on staggered maps in some cases (#3431)
Expand Down
41 changes: 41 additions & 0 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,39 @@ declare class TiledObject {
resolvedProperties(): TiledObjectProperties;
}

/**
* A Tiled project file primarily defines the list of folders containing
* the assets belonging to that project.
*
* @since 1.11
*/
declare class Project {
/**
* A project-specific directory where you can put Tiled extensions.
*
* It defaults to "extensions", so when you have a directory called
* “extensions” alongside your project file it will be picked up
* automatically. The directory is loaded in addition to the global
* extensions.
*/
readonly extensionsPath: string;

/**
* Path to the file where automapping rules are stored for this project.
*/
readonly automappingRulesFile: string;

/**
* An array of folders containing the assets belonging to the project
*/
readonly folders: string[];

/**
* The path to the .tiled-project file.
*/
readonly fileName: string;
}

/**
* Defines the font used to render objects which have {@link MapObject.shape}
* set to {@link MapObject.Text}.
Expand Down Expand Up @@ -3559,6 +3592,14 @@ declare namespace tiled {
*/
export let activeAsset: Asset | null;

/**
* Currently opened project. If no project is open, the properties of the
* project will be blank.
*
* @since 1.11
*/
export const project: Project;

/**
* List of currently opened {@link Asset | assets}.
*/
Expand Down
54 changes: 54 additions & 0 deletions src/tiled/editableproject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* editableproject.cpp
* Copyright 2023, Chris Boehm AKA dogboydog
* Copyright 2023, Thorbjørn Lindeijer <[email protected]>
*
* This file is part of Tiled.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "editableproject.h"

namespace Tiled {

EditableProject::EditableProject(Project *project, QObject *parent)
: QObject(parent)
, mProject(project)
{
}

QString EditableProject::extensionsPath() const
{
return mProject->mExtensionsPath;
}

QString EditableProject::automappingRulesFile() const
{
return mProject->mAutomappingRulesFile;
}

QString EditableProject::fileName() const
{
return mProject->fileName();
}

QStringList EditableProject::folders() const
{
return mProject->folders();
}

} // namespace Tiled

#include "moc_editableproject.cpp"
53 changes: 53 additions & 0 deletions src/tiled/editableproject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* editableproject.h
* Copyright 2023, Chris Boehm AKA dogboydog
* Copyright 2023, Thorbjørn Lindeijer <[email protected]>
*
* This file is part of Tiled.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "project.h"

#include <QObject>

namespace Tiled {

class EditableProject : public QObject
{
Q_OBJECT

Q_PROPERTY(QString extensionsPath READ extensionsPath)
Q_PROPERTY(QString automappingRulesFile READ automappingRulesFile)
Q_PROPERTY(QString fileName READ fileName)
Q_PROPERTY(QStringList folders READ folders)

public:
EditableProject(Project *project, QObject *parent = nullptr);

QString extensionsPath() const;
QString automappingRulesFile() const;
QString fileName() const;
QStringList folders() const;
dogboydog marked this conversation as resolved.
Show resolved Hide resolved

private:
Project *mProject;
};

} // namespace Tiled

Q_DECLARE_METATYPE(Tiled::EditableProject*)
2 changes: 2 additions & 0 deletions src/tiled/libtilededitor.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ DynamicLibrary {
"editableobject.h",
"editableobjectgroup.cpp",
"editableobjectgroup.h",
"editableproject.cpp",
"editableproject.h",
"editableselectedarea.cpp",
"editableselectedarea.h",
"editabletile.cpp",
Expand Down
7 changes: 7 additions & 0 deletions src/tiled/scriptmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ QList<QObject *> ScriptModule::openAssets() const
return assets;
}

EditableProject *ScriptModule::project()
{
if (!mEditableProject)
mEditableProject = new EditableProject(&ProjectManager::instance()->project(), this);
return mEditableProject;
}

TilesetEditor *ScriptModule::tilesetEditor() const
{
if (auto documentManager = DocumentManager::maybeInstance())
Expand Down
5 changes: 5 additions & 0 deletions src/tiled/scriptmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "id.h"
#include "issuesdock.h"
#include "properties.h"
#include "editableproject.h"

#include <QJSValue>
#include <QObject>
Expand Down Expand Up @@ -69,6 +70,7 @@ class ScriptModule : public QObject

Q_PROPERTY(Tiled::EditableAsset *activeAsset READ activeAsset WRITE setActiveAsset NOTIFY activeAssetChanged)
Q_PROPERTY(QList<QObject*> openAssets READ openAssets)
Q_PROPERTY(Tiled::EditableProject *project READ project)

Q_PROPERTY(Tiled::MapEditor *mapEditor READ mapEditor)
Q_PROPERTY(Tiled::TilesetEditor *tilesetEditor READ tilesetEditor)
Expand Down Expand Up @@ -98,6 +100,8 @@ class ScriptModule : public QObject

QList<QObject*> openAssets() const;

EditableProject *project();

TilesetEditor *tilesetEditor() const;
MapEditor *mapEditor() const;

Expand Down Expand Up @@ -172,6 +176,7 @@ public slots:
std::map<Id, std::unique_ptr<ScriptedTool>> mRegisteredTools;

QStringList mScriptArguments;
EditableProject *mEditableProject = nullptr;
};

inline bool ScriptModule::versionLessThan(const QString &a)
Expand Down