-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Template instances #1623
Template instances #1623
Conversation
Also add fileName member to the TemplateGroup
805e8a1
to
e5d790a
Compare
The code can now read template instances and load them into the map. I will do more testing and handle possible errors. |
e5d790a
to
0d54859
Compare
Will also handle some reading errors.
7a1fa2a
to
812d0aa
Compare
Also added a nextTemplateId property to the template to be used when saving or adding a new template.
812d0aa
to
ee489b0
Compare
e1bf477
to
89b8aeb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall, but I found some issues.
src/libtiled/mapobject.h
Outdated
|
||
CellProperty = 1 << 8, | ||
SizeProperty = 1 << 9, | ||
shapeProperty = 1 << 10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming: ShapeProperty
src/libtiled/mapwriter.cpp
Outdated
} | ||
|
||
if (id != 0) | ||
if (id != 0 && !isTemplateInstance) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Template instances do still need their ID.
src/libtiled/tidmapper.cpp
Outdated
if (it.value() == templateGroup) | ||
return it.key(); | ||
} | ||
return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function can be written as:
return mFirstTidToTemplateGroup.key(templateGroup);
It should also be marked const
.
src/libtiled/tidmapper.cpp
Outdated
|
||
ObjectTemplate *TidMapper::tidToTemplate(unsigned tid, bool &ok) const | ||
{ | ||
ObjectTemplate *objectTemplate; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be initialized to nullptr
, to ensure a consistent return value in case of failure in addition to setting ok
to false
.
src/libtiled/tidmapper.cpp
Outdated
objectTemplate = templateGroup->templateAt(index); | ||
} else { | ||
ok = false; | ||
objectTemplate = nullptr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When properly initialized, we don't need to set it to nullptr
here.
src/libtiled/mapwriter.cpp
Outdated
w.writeAttribute(QLatin1String("tid"), QString::number(tid)); | ||
} | ||
|
||
if (id != 0 && !isTemplateInstance) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Template instances need to write their ID as well.
src/libtiled/mapwriter.cpp
Outdated
w.writeAttribute(QLatin1String("visible"), QLatin1String("0")); | ||
|
||
writeProperties(w, mapObject.properties()); | ||
if (!isTemplateInstance) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Properties should always be written, since this will only include overridden values and any properties only added to the instance.
src/tiled/objecttemplatemodel.h
Outdated
inline void ObjectTemplateModel::setTemplateDocuments(const TemplateDocuments &templateDocuments) | ||
{ mTemplateDocuments = templateDocuments; } | ||
inline void ObjectTemplateModel::setTemplateDocuments(TemplateDocuments &templateDocuments) | ||
{ mTemplateDocuments = &templateDocuments; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mTemplateDocuments
should not be a pointer. Rather, just store the list here by value and when you need it in the TemplatesDock
, have it call ObjectTemplateModel::templateDocuments
and remove TemplatesDock::mTemplateDocuments
. The function should take const TemplateDocuments &templateDocuments
.
Also, since this affects the model, it should call beginResetModel
and endResetModel
, even though currently the function may only be called while the model is not yet used by a view, this could change later on.
It should probably also own the documents, so it should call qDeleteAll(mTemplateDocuments)
before the assignment, and this should be added to the destructor (instead of doing it in TemplatesDock
).
src/tiled/objecttemplatemodel.h
Outdated
|
||
inline const TemplateDocuments ObjectTemplateModel::templateDocuments() const | ||
{ return mTemplateDocuments; } | ||
{ return *mTemplateDocuments; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should return const TemplateDocuments &
.
src/tiled/templatesdock.cpp
Outdated
manager->setTemplateGroups(templateGroups); | ||
} | ||
|
||
TemplatesDock::~TemplatesDock() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coding style: {
should be on the next line.
89b8aeb
to
2b26d24
Compare
src/tiled/templatesdock.h
Outdated
@@ -49,6 +52,7 @@ private slots: | |||
void retranslateUi(); | |||
|
|||
TemplatesView *mTemplatesView; | |||
TemplateDocuments mTemplateDocuments; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems you forgot to remove this member, and the above include?
e1f5039
to
6e4f3df
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments.
src/tiled/flipmapobjects.cpp
Outdated
mMapObjects[i]->flip(mFlipDirection, mObjectsCenter); | ||
|
||
mMapObjects[i]->setPropertyChanged(MapObject::CellProperty, mNewCellStates[i]); | ||
mMapObjects[i]->setPropertyChanged(MapObject::RotationProperty, mNewCellStates[i]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mNewCellStates
should be mNewRotationStates
src/libtiled/map.cpp
Outdated
} | ||
} | ||
|
||
delete oldTemplateGroup; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe somebody should delete the old template group, but it's not the map.
src/tiled/brokenlinks.cpp
Outdated
case TilesetTileImageSource: | ||
return nullptr; | ||
case TemplateGroupReference: | ||
return _templateGroup; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, that is a lot of lines for writing:
return type == TemplateGroupReference ? _templateGroup : nullptr;
:-)
src/tiled/brokenlinks.cpp
Outdated
link._templateGroup = templateGroup; | ||
mBrokenLinks.append(link); | ||
} else { | ||
processTemplateGroup(templateGroup); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This else makes no sense, because it does essentially the same as the code above, just without the check on the file name being empty (which should be there, I guess).
src/tiled/brokenlinks.cpp
Outdated
@@ -404,6 +460,7 @@ void BrokenLinksWidget::selectionChanged() | |||
mLocateButton->setEnabled(!selection.isEmpty()); | |||
|
|||
bool isTileset = qobject_cast<TilesetDocument*>(mBrokenLinksModel->document()) != nullptr; | |||
bool isTemplateGroup = qobject_cast<TemplateGroupDocument*>(mBrokenLinksModel->document()) != nullptr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The broken links model document can never be a template group document, since we don't currently have a template group editor.
src/tiled/brokenlinks.cpp
Outdated
if (isTemplateGroup) | ||
mLocateButton->setText(tr("Locate File...")); | ||
else | ||
mLocateButton->setText(tr("Open Template Group...")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The button should always read "Locate File..." at this point, since we can't open template groups directly.
6f185c8
to
5172938
Compare
@thabetx Looks like your last commit should go to the |
Okay, I will test it on master. |
2220c71
to
419444b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Numerous small comments but overal I think it's looking pretty solid. Let's aim for merging this as soon as possible so we can have a new PR for the actual instantiation.
src/libtiled/map.cpp
Outdated
return true; | ||
} | ||
|
||
QList<Tiled::MapObject *> Map::replaceTemplateGroup(TemplateGroup *oldTemplateGroup, TemplateGroup *newTemplateGroup) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MapObject
does not need to be fully qualified here.
src/libtiled/mapobject.cpp
Outdated
@@ -33,6 +33,7 @@ | |||
#include "map.h" | |||
#include "objectgroup.h" | |||
#include "tile.h" | |||
#include "templatemanager.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can include templategroup.h
instead of templatemanager.h
, since the manager isn't actually used.
src/libtiled/mapreader.cpp
Outdated
@@ -921,41 +963,74 @@ MapObject *MapReaderPrivate::readObject() | |||
const QPointF pos(x, y); | |||
const QSizeF size(width, height); | |||
|
|||
MapObject *object = new MapObject(name, type, pos, size); | |||
MapObject *object = new MapObject; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you could still use the previous constructor, and then just initialize the flags as follows?
object->setPropertyChanged(MapObject::NameProperty, !name.isEmpty());
object->setPropertyChanged(MapObject::TypeProperty, !type.isEmpty());
object->setPropertyChanged(MapObject::SizeProperty, !size.isEmpty());
Would just be a bit shorter.
src/libtiled/tidmapper.cpp
Outdated
ok = true; | ||
|
||
if (!templateGroup->loaded()) | ||
templateGroup->updateMaxId(id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can include this in the else
of the previous condition.
src/libtiled/tidmapper.cpp
Outdated
QMapIterator<unsigned, TemplateGroup*> it(mFirstTidToTemplateGroup); | ||
|
||
while (it.hasNext()) { | ||
it.next(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation is off by 2.
for (int i = 0; i < mMapObjects.size(); ++i) { | ||
mMapObjects[i]->flip(mFlipDirection, mObjectsCenter); | ||
|
||
mMapObjects[i]->setPropertyChanged(MapObject::CellProperty, mNewCellStates[i]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little concerned about this. I think flipping some tile object is not actually expected to override the referenced tile, but just its flipped state. I think it's not very important to deal with this now, but it's something to keep in mind.
src/tiled/mainwindow.cpp
Outdated
MapDocument *mapDocument = MapDocument::load(fileName, mapFormat, &error); | ||
document = mapDocument; | ||
|
||
bool hasNonEmbeddedTemplateGroups; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable is missing an initialization. Not that it matters, because the variable isn't really used. Remove?
src/tiled/mapdocument.cpp
Outdated
auto changedObjects = mMap->replaceTemplateGroup(oldTemplateGroup, templateGroup); | ||
|
||
// Update the objects in the map scene | ||
emit(objectsChanged(changedObjects)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coding style: please use the empty emit
macro as follows:
emit objectsChanged(changedObjects);
src/tiled/templategroupdocument.cpp
Outdated
@@ -45,7 +45,7 @@ TemplateGroupDocument::TemplateGroupDocument(TemplateGroup *templateGroup, const | |||
: Document(TemplateGroupDocumentType, fileName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this fileName
also be changed to mTemplateGroup->fileName()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. The template group should always contain the file name, so the constructor arguments can be the template group only.
src/libtiled/mapobject.h
Outdated
CellProperty = 1 << 8, | ||
SizeProperty = 1 << 9, | ||
ShapeProperty = 1 << 10, | ||
RotationProperty = 1 << 11 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you've added several entries here, I'm getting warnings in MapObject::setMapObjectProperty
and MapObject::mapObjectProperty
. I think size and rotation could simply be handled, but "shape" and "cell" are too special and it should probably catch those with a Q_ASSERT(false);
.
a65dbd2
to
eff51e6
Compare
Also created the sync function which synchronizes the properties of a template instance with the base template.
eff51e6
to
92d06ca
Compare
92d06ca
to
f6251ae
Compare
This is the third checkpoint in the reusable object templates project, it will be mainly concerned with reading template instance in a map.
The format will look like this
The instance in the map file would be the same as an object except that it would have an extra attribute similar to the gid called the tid standing for template id. Template ids would work like tileset ids, the map assigns the firsttid to each template group and the template manager would resolve those ids.
If a template object has properties they will override the template.
Currently the pull request is focused on reading an instance but may be expanded to also cover creation.