mapeditor / tiled

Flexible level editor
https://www.mapeditor.org/
Other
11.27k stars 1.76k forks source link

Scripting : Make MapEditor.currentWangSet and MapEditor.currentWangColorIndex writeable #4105

Open dogboydog opened 3 days ago

dogboydog commented 3 days ago

Fix #4101

currentWangSet setter: seems to be working:

tiled.mapEditor.currentWangSet = tiled.activeAsset.tilesets[0].wangSets[0] 

Currently crashing, I'm doing something wrong setting the currentWangColorIndex :

tiled.mapEditor.currentWangColorIndex  = 2 

I didn't see an existing public method to change the selection so I tried to make one based on reading the rest of the code in wangdock.cpp . Maybe I missed something existing or just have a logic error in my setter. Visually at least, it seems to change the color selection though.

Causes crash: image

TODO (@dogboydog )

bjorn commented 3 days ago

The error message is from QSortFilterProxyModel, and it says the QModelIndex you're using is from the wrong model. That's because mWangColorModel->colorIndex returns an index in the source model, which still needs to be mapped to the model used by the view (the mWangColorFilterModel).

I don't recall now what this sort-filter-model is used for (I couldn't immediately find a reason for it to exist in the current code). The easiest thing to do for now is to call mWangColorFilterModel->mapFromSource(index), as done in a slightly mode convoluted way in WangDock::editWangColorName.

Hmm, in fact, you could likely adjust WangDock::onColorCaptured, which is basically doing what you're after with WangDock::setCurrentWangColor.

Just note that there's an assert in WangColorModel::colorIndex, which should not be possible to trigger from script. Potentially we could just turn the assert into a regular condition and return QModelIndex() in that case.

dogboydog commented 2 days ago

Thanks, I think I implemented what you suggested. It's working without errors now. I updated the scripting docs and NEWS.md

https://github.com/user-attachments/assets/4225ce99-23ad-4255-ae55-e7b7478b32e3