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

Allows extending of the polyline #1683

Merged
merged 12 commits into from
Jan 16, 2018
Merged

Allows extending of the polyline #1683

merged 12 commits into from
Jan 16, 2018

Conversation

ketanhwr
Copy link
Contributor

@ketanhwr ketanhwr commented Aug 11, 2017

#157

TODOs:

  • Don't stop extending after one point. Keep extending until right-click or escape key is encountered.

@@ -566,6 +596,59 @@ void EditPolygonTool::updateMovingItems(const QPointF &pos,
}
}

void EditPolygonTool::addNode(const QPointF &pos,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name addNode does not seem to reflect what the function is doing. I think updateAddingNode would be better, but it still leaves two kinds of terminology for the same thing. Since you're already using the mode Extending and have mExtendingFirst, I would suggest to rename all the other functions as follows:

  • updateExtending
  • finishExtending
  • cancelExtending

void addNode(const QPointF &pos, Qt::KeyboardModifiers modifiers);

void finishAddingNode();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coding style: I'd suggest to leave out these empty lines so that the related functions are grouped together.

@@ -98,6 +108,7 @@ private slots:
QPointF mStart;
QPoint mScreenStart;
Qt::KeyboardModifiers mModifiers;
bool mExtendingFirst;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this variable isn't expected to be used before being set, but please anyway initialize the variable.

@Ablu
Copy link
Contributor

Ablu commented Aug 18, 2017

Hm. Could you rebase this instead of merging the master? For example you reordered the WangTool declaration as it is.

@ketanhwr
Copy link
Contributor Author

Wait, I'll rebase this.

mOverlayPolygonItem->setPolygon(next);

if (mExtending)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coding style: since the body wraps multiple lines, this should have braces.

@@ -75,15 +82,24 @@ void CreateMultipointObjectTool::mousePressedWhileCreatingObject(QGraphicsSceneM
QPolygonF next = mOverlayPolygonObject->polygon();

// If the last position is still the same, ignore the click
if (next.last() == current.last())
if (next.last() == current.last() && !mExtending)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this check irrelevant while extending? I think it was introduced to avoid inserting overlapping points in case the user double-clicks. It seems to me like it would be useful to check the first or last point, depending on mExtendingFirst.

@@ -66,6 +69,8 @@ class CreateObjectTool : public AbstractObjectTool
ObjectGroupItem *mObjectGroupItem;
MapObjectItem *mNewMapObjectItem;
MapObjectItem *mOverlayPolygonItem;
bool mExtending;
bool mExtendingFirst;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't in general extend an object so I'd prefer these variables to be in the CreateMultipointObjectTool. At least mExtendingFirst could easily be moved there since it isn't used in the base class. Regarding mExtending, are there any functions that couldn't be just overridden?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried overriding the functions cancelNewMapObject() and finishNewMapObject(), but apparently, the parent function gets called only and not the child one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes no sense, so I quickly searched where these functions are being called.

Turns out it's because the CreatePolylineObjectTool is explicitly calling the functions of the CreateObjectTool class here:

void CreatePolylineObjectTool::finishNewMapObject()
{
    if (mNewMapObjectItem->mapObject()->polygon().size() >= 2)
        CreateObjectTool::finishNewMapObject();
    else
        CreateObjectTool::cancelNewMapObject();
}

I see no reason why it should be doing that. There are no existing overrides that would need to be skipped. So please just remove the CreateObjectTool:: part there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right 🤦‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed CreateObjectTool:: but added CreateMultipointObjectTool::, because without any className::, their was some runtime error (the window wasn't responding at all). I wasn't able to understand why this happened.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I missed it too, but this is obviously because we're in CreatePolylineObjectTool::finishNewMapObject there, so we should at least avoid calling that exact function recursively.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right 🤦‍♂️

@@ -673,6 +677,17 @@ void MapEditor::setWangFill(bool value)
mBucketFillTool->setWangFill(value);
}

void MapEditor::extend(MapObjectItem *mapObjectItem, bool extendingFirst)
{
setSelectedTool(mPolylineObjectsTool);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using this instead of mToolManager->selectTool(mPolylineObjectsTool) makes the tool switch without this being visible on the "Tools" tool bar. Was that a conscious decision? I think it wouldn't hurt for the user to see the active tool changing. Same for extendingFinished.

Copy link
Member

@bjorn bjorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like I got interrupted since I just found these comments still pending. :-/

@@ -88,7 +88,6 @@ void CreateObjectTool::keyPressed(QKeyEvent *event)
case Qt::Key_Escape:
if (mNewMapObjectItem) {
cancelNewMapObject();
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't remove this return, since it will cause the event to be ignored.

@@ -203,6 +202,7 @@ void CreateObjectTool::finishNewMapObject()
}

MapObject *newMapObject = mNewMapObjectItem->mapObject();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No point in adding a newline here.

@@ -52,7 +52,7 @@ MapObject *CreatePolygonObjectTool::createNewMapObject()
void CreatePolygonObjectTool::finishNewMapObject()
{
if (mNewMapObjectItem->mapObject()->polygon().size() >= 3)
CreateObjectTool::finishNewMapObject();
finishNewMapObject();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be CreateMultipointObjectTool::finishNewMapObject(); due to the problem you noticed in the polyline object tool.

mExtendingFirst = extendingFirst;

mNewMapObjectItem = mapObjectItem;
mOverlayPolygonObject = mapObjectItem->mapObject()->clone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overlay polygon object is created already in the constructor of the CreateMultipointObjectTool, so you can't just assign another object to it (causes leaking of the other object). I think you could just use the object that is already there, since it's only for showing a polygon preview anyway.

@ketanhwr
Copy link
Contributor Author

After we start extending the polyline, since we've shifted to the Create Polyline Tool, the color changes back to blue. Is this fine?

The mExtendingFirst variable wasn't being reset.
The shape and position were not being set on the overlay object when
extending a polyline, so it only worked when those were still set from
the last created polyline.
@bjorn
Copy link
Member

bjorn commented Sep 6, 2017

After we start extending the polyline, since we've shifted to the Create Polyline Tool, the color changes back to blue. Is this fine?

I hoped it may be fine, but while checking it out it felt strange and it'd probably be better for just the new segment to be blue.

I've pushed two bugfixes for things I noticed while testing.

In addition to not coloring the whole line blue, I think we need to find a way to have the new segment show up immediately. Currently, you have to move your mouse at least once after clicking "Extend Polyline" to make the new segment show up.

@ketanhwr
Copy link
Contributor Author

ketanhwr commented Sep 6, 2017

So probably, when the user clicks on the first/last node, the extending should begin?

@bjorn
Copy link
Member

bjorn commented Sep 6, 2017

So probably, when the user clicks on the first/last node, the extending should begin?

Well, then it would work mostly like Vectr, right? But if it's just activated on click, then we can't switch tools because we'd need it to not interfere with just trying to select some nodes.

Or, it would need to work like Inkscape, where the nodes to click for extending the polyline are only visible in the "Insert Polyline" tool.

Or we find some way to place the new node based on the current mouse position before we get the next mouse move event.

@ketanhwr
Copy link
Contributor Author

@bjorn what exactly was wrong here? I'd like to get this one merged.

@bjorn
Copy link
Member

bjorn commented Dec 14, 2017

@ketanhwr The main issues I seem to have ran into was that you need to move the mouse for the line to show up when extending the polyline, and that the whole thing turns blue rather than just the new line.

For the overal implementation, I'd have to take a detailed look again and consider alternatives, but right now I'm trying to finish some stuff people have been paying for. Either this week or the next I'm also going to look at making some improvements to the polygon editing in general.

@ketanhwr
Copy link
Contributor Author

Alright 🙂

Conflicts:
	src/tiled/editpolygontool.cpp
@bjorn
Copy link
Member

bjorn commented Jan 12, 2018

Hmm, I realized another problem with this implementation. Since it goes through the MapEditor, the "Extend Polyline" action does nothing when used in the Tile Collision Editor, unless we duplicate the same connection setup in the TileCollisionDock.

I'm considering a rather big rewrite of the whole polygon editing functionality, or at least to re-think it all from scratch based on the wishlist of functionality, because of the difficulties with putting such small functions on top of the current implementation.

@ketanhwr
Copy link
Contributor Author

I'm considering a rather big rewrite of the whole polygon editing functionality, or at least to re-think it all from scratch based on the wishlist of functionality, because of the difficulties with putting such small functions on top of the current implementation.

Interested in helping with this.

bjorn added 5 commits January 16, 2018 12:57
This way, the "Extend Polyline" action also works in the Tile Collision
Editor.

Also, it no longer appears in the Template view, because with the
current implementation it can't work there since the "Insert Polyline"
tool is not available there.
Don't switch to EditPolygonTool when user selected another tool. This
isn't what the user wants and it broke stuff.
There appears to be no difference between finishing or canceling the
extending of a polyline. In both cases we switch back to the "Edit
Polygons" tool and in both cases we should also make sure the object we
were extending is selected.

There shouldn't actually be a reason to touch the selection at all when
starting or stopping to extend a polyline, but currently the selection
outline does not update as expected while extending a polyline.
@bjorn
Copy link
Member

bjorn commented Jan 16, 2018

A few more fixes later, I think we can merge this, but I'll still be looking at redoing the general setup.

Interested in helping with this.

That is appreciated, but I'm under some time pressure to improve this aspect of Tiled and I don't currently see how we can do this together.

@bjorn bjorn merged commit 5dc2194 into mapeditor:master Jan 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants