1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "tool_choose_pos.h"
24 #include "../interface_v3/interface_manager.h"
25 #include "../global.h"
26 #include "nel/gui/group_tree.h"
27 #include "../interface_v3/group_map.h"
28 #include "../landscape_poly_drawer.h"
30 #include "../entities.h"
32 #include "nel/misc/vector.h"
33 #include "nel/misc/quat.h"
34 #include "nel/misc/aabbox.h"
35 #include "nel/3d/u_instance.h"
37 #include "game_share/object.h"
39 #include "r2_config.h"
45 using namespace NLPACS
;
46 using namespace NLMISC
;
52 // ***************************************************************
53 CToolChoosePos::CToolChoosePos(sint ghostSlot
,
54 const std::string
&cursValid
,
55 const std::string
&cursInvalid
,
56 const std::vector
<CPolygon2D
> &polyList
,
57 const CPrimLook
&polyValidLook
,
58 const CPrimLook
&polyInvalidLook
61 _CursValid
= cursValid
;
62 _CursInvalid
= cursInvalid
;
63 _GhostSlot
= ghostSlot
,
65 _WantedAction
= NoAction
;
66 _WarningNoMeshOrSkeletonShown
= false;
67 CEntityCL
*entity
= getGhost();
68 _LocalSelectBoxSize
= 1;
71 entity
->getMeshDefaultScale(_DefaultScale
);
72 if (!entity
->skeleton())
74 const CVector
&hs
= getEditor().getLocalSelectBox(*entity
).getHalfSize();
75 _LocalSelectBoxSize
= maxof(fabsf(hs
.x
), fabsf(hs
.y
), fabsf(hs
.z
));
80 _DefaultScale
.set(1.f
, 1.f
, 1.f
);
83 _MultiPosLocked
= false;
84 _BadPlaceDecal
.setTexture("*accessibility_texture*", true, true, false);
85 _TestDecal
.setTexture("encyclopedia_box_zo.tga", true, true, false);
88 _PolyRender
.resize(polyList
.size());
89 _PolyValidLook
= polyValidLook
;
90 _PolyInvalidLook
= polyInvalidLook
;
91 CGroupMap
*gm
= getWorldMap();
92 for (uint k
= 0; k
< _PolyRender
.size(); ++k
)
94 _PolyRender
[k
].setActive(false);
97 gm
->addDeco(&_PolyRender
[k
]);
102 // ***************************************************************
103 void CToolChoosePos::showPolys(bool visible
)
105 //H_AUTO(R2_CToolChoosePos_showPolys)
106 for (uint k
= 0; k
< _PolyRender
.size(); ++k
)
108 _PolyRender
[k
].setActive(visible
);
112 // ***************************************************************
113 void CToolChoosePos::enableMultiPos(const std::string
&cursValidMulti
/*="curs_create_multi.tga"*/)
115 //H_AUTO(R2_CToolChoosePos_enableMultiPos)
117 _CursValidMulti
= cursValidMulti
;
120 // ***************************************************************
121 CToolChoosePos::~CToolChoosePos()
124 CGroupMap
*gm
= getWorldMap();
127 for (uint k
= 0; k
< _PolyRender
.size(); ++k
)
129 _PolyRender
[k
].setActive(false);
130 gm
->removeDeco(&_PolyRender
[k
]);
135 // ***************************************************************
136 CEntityCL
*CToolChoosePos::getGhost()
138 //H_AUTO(R2_CToolChoosePos_getGhost)
139 if (_GhostSlot
== -1) return NULL
;
140 return EntitiesMngr
.entity(_GhostSlot
);
143 // ***************************************************************
144 void CToolChoosePos::hideMapSelectionAxis()
146 //H_AUTO(R2_CToolChoosePos_hideMapSelectionAxis)
147 CGroupMap
*worldMap
= getWorldMap();
148 if (worldMap
) worldMap
->setSelectionAxis(false);
151 // ***************************************************************
152 void CToolChoosePos::updateInvalidCursorOnUI()
154 //H_AUTO(R2_CToolChoosePos_updateInvalidCursorOnUI)
155 setMouseCursor(DEFAULT_CURSOR
);
158 // ***************************************************************
159 void CToolChoosePos::updateBeforeRender()
161 //H_AUTO(R2_CToolChoosePos_updateBeforeRender)
162 CGroupMap
*worldMap
= getWorldMap();
163 CEntityCL
*entity
= getGhost();
165 DT
= RZ_TIME_TO_BECOME_TRANSPARENT_IN_SECOND
; // don't want a transition
168 uint32 oldOpacity
= CEntityCL::getOpacityMin();
169 CEntityCL::setOpacityMin(DEFAULT_ENTITY_MIN_OPACITY
);
170 entity
->makeTransparent(true);
171 CEntityCL::setOpacityMin(oldOpacity
);
174 // Build vector for direction pointed by mouse in world
175 sint32 mouseX
, mouseY
;
176 getMousePos(mouseX
, mouseY
);
177 if (!isInScreen(mouseX
, mouseY
) || (isMouseOnUI() && !isMouseOnWorldMap()))
179 if (worldMap
) worldMap
->setSelectionAxis(false);
184 updateInvalidCursorOnUI();
189 CTool::CWorldViewRay worldViewRay
;
191 computeWorldViewRay(mouseX
, mouseY
, worldViewRay
);
193 CVector entityPos
; // the pos where the ghost will be shown
194 CVector inter
; // intersection of view ray with landscape
196 CVector scale
= _DefaultScale
;
197 TRayIntersectionType rayIntersectionType
= computeLandscapeRayIntersection(worldViewRay
, inter
);
198 switch(rayIntersectionType
)
202 // no collision, can't drop entity
203 entityPos
= worldViewRay
.Origin
+ 3.f
* worldViewRay
.Dir
;
204 // change the scale so that the entity has always the same size on screen
205 float refScale
= CV_FloatingShapeRefScale
.get();
206 if (refScale
== 0.f
) refScale
= 1.f
;
207 if (_LocalSelectBoxSize
!= 0.f
)
209 scale
*= refScale
/ _LocalSelectBoxSize
;
211 if (worldMap
) worldMap
->setSelectionAxis(false);
217 _CreatePosition
= entityPos
;
218 _Valid
= isValidChoosePos(inter
); // good pos to drop entity
219 if (worldMap
) worldMap
->setSelectionAxis(true, inter
);
224 if (worldMap
) worldMap
->setSelectionAxis(true, inter
);
231 // compute front vector
232 CVector front
= - MainCam
.getMatrix().getJ();
235 // compute angle around Z
236 _CreateAngle
= (float) atan2(front
.y
, front
.x
);
241 if (worldViewRay
.OnMiniMap
&& rayIntersectionType
== NoIntersection
)
245 else if (!entity
->skeleton())
248 nlwarning("Selected entity for the 'create' tool has no skeleton");
252 //bool loading = true;
253 NL3D::UInstance inst
= entity
->instance();
256 if (!entity
->instances().empty())
258 inst
= entity
->instances()[0].Current
;
259 //loading = !entity->instances()[0].Loading.empty() && entity->instances()[0].Current.empty();
264 if (!_WarningNoMeshOrSkeletonShown
)
266 nlwarning("Selected entity for the 'create' tool has no instance");
267 _WarningNoMeshOrSkeletonShown
= true;
275 CQuat
frontQuat(CVector::K
, _CreateAngle
- (float) (NLMISC::Pi
/ 2));
276 inst
.setRotQuat(frontQuat
);
277 inst
.setPos(entityPos
);
278 inst
.setScale(scale
);
279 mat
= inst
.getMatrix();
285 entity
->updateVisible(T1
, NULL
);
286 entity
->updatePos(T1
, NULL
);
289 CMatrix skelMatrix = entity->skeleton()->getMatrix();
290 // relative position to skeleton root
291 CVector skelRootRelativePos = entity->skeleton()->getMatrix().getPos() - entity->pos().asVector();
292 // combine quat for front face xform & anim quat
293 CQuat frontQuat(CVector::K, _CreateAngle);
294 entity->skeleton()->setRotQuat(frontQuat * entity->skeleton()->getRotQuat());
295 entity->skeleton()->setPos(entityPos + skelRootRelativePos);
296 mat.setRot(frontQuat);
297 mat.setPos(entityPos);
299 // relative position to skeleton root
300 CVector skelRootRelativePos
= entity
->skeleton()->getMatrix().getPos() - entity
->pos().asVector();
301 // combine quat for front face xform & anim quat
302 CQuat
frontQuat(CVector::K
, _CreateAngle
);
303 entity
->skeleton()->setRotQuat(frontQuat
* entity
->skeleton()->getRotQuat());
305 frontMat
.setRot(frontQuat
);
306 entity
->skeleton()->setPos(entityPos
+ frontMat
* skelRootRelativePos
);
307 /*mat.setRot(frontQuat);
308 mat.setPos(entityPos);*/
311 // see if all pos are accessible and update the _Valid flag
312 // NB NICO : THE FOLLOWING CODE IS WORKING BUT
313 // see with others if it's useful to check this type of collisions
314 // finally -> since check is never done, limited/no interest ...
316 if (shown && entity->getPrimitive())
320 if (entity->getPrimitive()->getPrimitiveType() == UMovePrimitive::_2DOrientedBox)
322 entity->getPrimitive()->getSize(w, h);
326 w = h = 2.f * entity->getPrimitive()->getRadius();
328 // transform from [0, 1] x [0, 1] to local bbox
329 CMatrix unitBoxToLocalPrim;
330 unitBoxToLocalPrim.setScale(CVector(w, h, 1.f));
331 //unitBoxToLocalBox.setPos(CVector(localBox.getMin().x / favoid0(w), localBox.getMin().y / favoid0(h), 1.f);
332 unitBoxToLocalPrim.setPos(CVector(- 0.5f * w, - 0.5f * h, 0.f));
333 CMatrix worldMat = mat * unitBoxToLocalPrim;
334 // test if place is valid
336 poly.Vertices.resize(4);
337 poly.Vertices[0].set(0.f, 0.f, 0.f);
338 poly.Vertices[1].set(1.f, 0.f, 0.f);
339 poly.Vertices[2].set(1.f, 1.f, 0.f);
340 poly.Vertices[3].set(0.f, 1.f, 0.f);
341 CPolygon2D poly2D(poly, worldMat);
343 if (getEditor().getIslandCollision().isValidPoly(poly2D))
345 _TestDecal.setWorldMatrix(worldMat);
346 _TestDecal.addToRenderList();
351 _BadPlaceDecal.setWorldMatrix(worldMat);
352 _BadPlaceDecal.setDiffuse(getInvalidPosColor());
353 _BadPlaceDecal.setCustomUVMatrix(true, getEditor().getIslandCollision().getWorldToAccessibilityTexMat(true));
354 _BadPlaceDecal.addToRenderList();
360 // additionnal polygons
361 if (!_PolyRender
.empty() && _PolyRender
[0].getActive())
363 static NLMISC::CPolygon2D tmpPoly
;
364 std::vector
<NLMISC::CVector2f
> &verts
= tmpPoly
.Vertices
;
365 nlassert(_PolyRender
.size() == _PolyList
.size());
366 CIslandCollision
&ic
= getEditor().getIslandCollision();
367 for (uint k
= 0; k
< _PolyRender
.size(); ++k
)
369 verts
.resize(_PolyList
[k
].Vertices
.size());
370 for (uint l
= 0; l
< verts
.size(); ++l
)
372 verts
[l
].set(_PolyList
[k
].Vertices
[l
].x
+ _CreatePosition
.x
,
373 _PolyList
[k
].Vertices
[l
].y
+ _CreatePosition
.y
);
375 bool validPoly
= ic
.isValidPoly(tmpPoly
); // Test all position because entities may be created in that zone
378 // also test zone vertices because in the end, a valid zone is a zone for which all vertices
380 for (uint l
= 0; validPoly
&& l
< verts
.size(); ++l
)
382 if (!ic
.isValidPos(verts
[l
])) validPoly
= false;
386 if (!validPoly
) _Valid
= false;
389 bool validPoly = true;
390 for (uint l = 0; l < verts.size() && validPoly; ++l)
392 if (!ic.isValidSegment(verts[l], verts[(l + 1) % verts.size()]))
399 _PolyRender
[k
].setLook(validPoly
? _PolyValidLook
: _PolyInvalidLook
);
400 _PolyRender
[k
].setVertices(verts
);
401 _PolyRender
[k
].addDecalsToRenderList();
403 if (_PolyRender
[k
].getLook().Shape
== CPrimLook::ClosedPolyLine
)
405 NLMISC::CAABBox bbox
;
406 static volatile bool showPoly
= true;
409 CLandscapePolyDrawer::getInstance().computeBBoxFromPolygon(tmpPoly
, bbox
);
410 CLandscapePolyDrawer::getInstance().addPoly(tmpPoly
, _PolyRender
[k
].getLook().EdgeLook
.WorldMapColor
, bbox
);
412 _PolyRender
[k
].setWorldMapPolyColor(_PolyRender
[k
].getLook().EdgeLook
.WorldMapColor
);
416 // change mouse depending on result
417 if (!isMouseOnUI() || isMouseOnWorldMap())
422 if (_MultiPos
&& isShiftDown() && !_MultiPosLocked
)
424 setMouseCursor(_CursValidMulti
);
428 setMouseCursor(_CursValid
);
433 setMouseCursor(_CursInvalid
);
438 setMouseCursor(_CursValid
);
442 // ***************************************************************
443 void CToolChoosePos::updateAfterRender()
445 //H_AUTO(R2_CToolChoosePos_updateAfterRender)
446 CTool::TSmartPtr
oldThis(this); // may be changed during commit
447 switch(_WantedAction
)
452 hideMapSelectionAxis();
455 // signal deriver that a position has been chosen
456 commit(_CreatePosition
, _CreateAngle
);
457 if (stopAfterCommit() && (!_MultiPos
|| !isShiftDown()) && !_MultiPosLocked
)
459 if (_GhostSlot
!= -1)
461 //EntitiesMngr.remove(_GhostSlot, true);
462 CEntityCL
*ghost
= EntitiesMngr
.entity(_GhostSlot
);
465 // just hide the slot because creation may happen just after, so keep
466 // a ref on textures and shape
470 getEditor().setCurrentTool(NULL
);
474 _WantedAction
= NoAction
;
482 getEditor().setCurrentTool(NULL
);
495 // ***************************************************************
496 void CToolChoosePos::removeGhostSlot()
498 //H_AUTO(R2_CToolChoosePos_removeGhostSlot)
499 if (_GhostSlot
!= - 1 && getGhost())
501 CEntityCL
*ghost
= EntitiesMngr
.entity(_GhostSlot
);
504 // just hide the slot because creation may happen just after, so keep
505 // a ref on textures and shape (this avoid a release from memory)
507 ghost
->displayable(false);
509 //EntitiesMngr.remove(_GhostSlot, true); // remove the ghost
514 // ***************************************************************
515 bool CToolChoosePos::onMouseLeftButtonClicked()
517 //H_AUTO(R2_CToolChoosePos_onMouseLeftButtonClicked)
520 _WantedAction
= SelectPos
;
525 // ***************************************************************
526 bool CToolChoosePos::onMouseRightButtonClicked()
528 //H_AUTO(R2_CToolChoosePos_onMouseRightButtonClicked)
529 _WantedAction
= Cancel
;
533 // ***************************************************************
534 void CToolChoosePos::cancel()
536 //H_AUTO(R2_CToolChoosePos_cancel)
538 // TODO nico : potential crash here if called just before render (in updateBeforeRender)
539 // TODO nico : rethink the deferred action stuff to avoid this possible case
540 hideMapSelectionAxis();