Merge branch '138-toggle-free-look-with-hotkey' into main/gingo-test
[ryzomcore.git] / ryzom / client / src / r2 / tool_select_rotate.cpp
blob56884a52bd507ebac041d84489380ddd4863a662
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 "editor.h"
20 #include "tool_select_rotate.h"
21 #include "tool_select_move.h"
23 #include "../net_manager.h"
24 #include "../motion/user_controls.h"
25 #include "../interface_v3/interface_manager.h"
26 #include "../entity_cl.h"
27 #include "../entities.h"
28 #include "displayer_visual.h"
31 #ifdef DEBUG_NEW
32 #define new DEBUG_NEW
33 #endif
35 using namespace NLMISC;
37 namespace R2
40 // ***************************************************************
41 CToolSelectRotate::CToolSelectRotate()
43 _StartAngle = 0;
44 _MouseStartX = -1;
45 _State = Idle;
49 // ***************************************************************
50 void CToolSelectRotate::updateAction(CInstance &instance)
52 //H_AUTO(R2_CToolSelectRotate_updateAction)
53 CEntityCL *entity = instance.getEntity();
54 if (entity)
56 setMouseCursor("r2ed_tool_rotating.tga");
57 setEntityAngle(*entity, instance, (float) ((getMouseX() - _MouseStartX) * NLMISC::Pi / 180) + _StartAngle);
62 // ***************************************************************
63 void CToolSelectRotate::setRotateInProgress(bool rotateInProgress, CInstance &instance)
65 //H_AUTO(R2_CToolSelectRotate_setRotateInProgress)
66 CDisplayerVisual *dv = instance.getDisplayerVisual();
67 if (dv) dv->setRotateInProgress(rotateInProgress);
70 // ***************************************************************
71 void CToolSelectRotate::beginAction(CInstance &instance)
73 //H_AUTO(R2_CToolSelectRotate_beginAction)
74 _MouseStartX = getMouseX();
75 _StartAngle = (float) instance.getObjectTable()->toNumber("Angle");
76 setRotateInProgress(true, instance);
79 // ***************************************************************
80 void CToolSelectRotate::cancelAction(CInstance &instance)
82 //H_AUTO(R2_CToolSelectRotate_cancelAction)
83 CEntityCL *entity = instance.getEntity();
84 nlassert(entity);
85 getEditor().requestRollbackLocalNode(instance.getId(), "Angle");
86 setRotateInProgress(false, instance);
89 // ***************************************************************
90 void CToolSelectRotate::commitAction(CInstance &instance)
92 //H_AUTO(R2_CToolSelectRotate_commitAction)
93 getDMC().newAction(CI18N::get("uiR2EDRotateAction") + instance.getDisplayName());
94 // nothing to do, entity already has good angle
95 getEditor().requestCommitLocalNode(instance.getId(), "Angle");
96 setRotateInProgress(false, instance);
99 // ***************************************************************
100 void CToolSelectRotate::setEntityAngle(CEntityCL &/* entity */, CInstance &instance, float angle)
102 //H_AUTO(R2_CToolSelectRotate_setEntityAngle)
103 CObjectNumber *angleObject = new CObjectNumber(angle);
104 getEditor().requestSetLocalNode(instance.getId(), "Angle", angleObject);
105 delete angleObject;
108 // ***************************************************************
109 bool CToolSelectRotate::isActionPossibleOn(const CInstance &instance) const
111 //H_AUTO(R2_CToolSelectRotate_isActionPossibleOn)
112 CInstance &mutableInstance = const_cast<CInstance &>(instance);
113 CDisplayerVisual *dv = mutableInstance.getDisplayerVisual();
114 if (dv && dv->getActualDisplayMode() != CDisplayerVisual::DisplayModeVisible)
116 return false;
118 if (instance.getEntity() != NULL)
120 return !instance.getClass()["NameToProp"]["Angle"].isNil();
122 return false;
125 // ***************************************************************
126 bool CToolSelectRotate::onMouseLeftButtonDown()
128 //H_AUTO(R2_CToolSelectRotate_onMouseLeftButtonDown)
129 bool result = CToolMaintainedAction::onMouseLeftButtonDown();
130 if (!result) return false;
131 if (_State == ActionNotPossible)
133 CTool::TSmartPtr holder(this);
134 cancel();
135 // for ergonomy, switch to the 'move' tool
136 getEditor().setCurrentTool(new CToolSelectMove);
137 return getEditor().getCurrentTool()->onMouseLeftButtonDown();
139 return true;
143 /////////////////////
144 // ACTION HANDLERS //
145 /////////////////////
148 * Make the select/rotate tool current
150 class CAHSelectRotate : public IActionHandler
152 virtual void execute(CCtrlBase * /* pCaller */, const std::string &/* sParams */)
154 getEditor().setCurrentTool(new CToolSelectRotate);
157 REGISTER_ACTION_HANDLER(CAHSelectRotate, "r2ed_select_rotate");
159 } // R2