Merge branch '138-toggle-free-look-with-hotkey' into main/gingo-test
[ryzomcore.git] / ryzom / client / src / r2 / tool_new_vertex.cpp
blob6d2443e522b4b47bf24fa323d27c9a175042d4a9
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdpch.h"
19 #include "tool_new_vertex.h"
20 #include "tool_draw_prim.h"
21 #include "editor.h"
22 #include "displayer_visual_group.h"
24 #include "nel/misc/i18n.h"
26 #ifdef DEBUG_NEW
27 #define new DEBUG_NEW
28 #endif
30 using namespace NLMISC;
32 namespace R2
36 // *********************************************************************************************************
37 CToolNewVertex::CToolNewVertex() : CToolChoosePos(-1, "curs_create.tga", "curs_create_vertex_invalid.tga"), _CurrEdge(-1), _CurrPos(CVector::Null)
39 lockMultiPos();
42 // *********************************************************************************************************
43 bool CToolNewVertex::isValidChoosePos(const CVector2f &pos) const
45 //H_AUTO(R2_CToolNewVertex_isValidChoosePos)
46 if (!isValid2DPos(pos)) return false;
47 // pos must be on one edge of the road
48 CInstance *selection = getEditor().getSelectedInstance();
49 if (!selection) return false;
50 IDisplayerUIHandle *miniMapHandle;
51 CInstance *underMouse = checkInstanceUnderMouse(&miniMapHandle);
52 if (underMouse != selection) return false;
53 if (miniMapHandle)
55 if (miniMapHandle->isEdge())
57 _CurrEdge = (sint) miniMapHandle->getEdgeIndex();
58 return true;
60 else
62 return false;
65 CDisplayerVisualGroup *dvg = dynamic_cast<CDisplayerVisualGroup *>(selection->getDisplayerVisual());
66 if (!dvg) return false;
67 _CurrEdge = dvg->isOnEdge(CVector2f(pos.x, pos.y));
68 _CurrPos = pos;
69 return _CurrEdge != -1;
72 // *********************************************************************************************************
73 void CToolNewVertex::commit(const NLMISC::CVector &createPosition, float /* createAngle */)
75 //H_AUTO(R2_CToolNewVertex_commit)
76 CInstance *selection = getEditor().getSelectedInstance();
77 if (!selection) return;
78 // check that there is room left for a new vertex
79 CObject *points = selection->getObjectTable()->getAttr("Points");
80 if (!points) return;
81 if (points->getSize() >= CToolDrawPrim::PrimMaxNumPoints)
83 getEditor().callEnvMethod("noMoreRoomLeftInPrimitveMsg", 0, 0);
84 return;
87 if (!selection->getDisplayerVisual()) return;
88 getDMC().newAction(NLMISC::CI18N::get(selection->isKindOf("Road") ? "uiR2EDInsertNewWayPointAction" : "uiR2EDInsertNewZoneVertexAction"));
89 CObject *wp = getDMC().newComponent(selection->isKindOf("Road") ? "WayPoint" : "RegionVertex");
90 if (!wp) return;
91 wp->setObject("Position", buildVector(CVectorD(createPosition - selection->getDisplayerVisual()->getWorldPos())));
92 if (_CurrEdge == (sint) (points->getSize() - 1))
94 getDMC().requestInsertNode(selection->getId(), "Points", -1, "", wp);
96 else
98 getDMC().requestInsertNode(selection->getId(), "Points", _CurrEdge + 1, "", wp);
100 delete wp;
103 // *********************************************************************************************************
104 const char *CToolNewVertex::getToolUIName() const
106 //H_AUTO(R2_CToolNewVertex_getToolUIName)
107 return "new_vertex";
110 // *********************************************************************************************************
111 bool CToolNewVertex::onDeleteCmd()
113 //H_AUTO(R2_CToolNewVertex_onDeleteCmd)
114 CTool::TSmartPtr hold(this);
115 getEditor().setCurrentTool(NULL);
116 return false; // don't handle event because the current zone will be deleted
122 } // R2