| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364 |
- #include "2d/editorToy/EditorToySceneWindow.h"
- #include "console/console.h"
- #include "console/consoleTypes.h"
- #include "graphics/dgl.h"
- #include "platform/event.h"
- #include "2d/editorToy/EditorToyTool.h"
- #include "2d/editorToy/EditorToySceneWindow_ScriptBinding.h"
- //-----------------------------------------------------------------------
- IMPLEMENT_CONOBJECT(EditorToySceneWindow);
- //-----------------------------------------------------------------------
- bool EditorToySceneWindow::inNut(const Point2I &pt, S32 x, S32 y)
- {
- //S32 checkSize = mNutSize << 1;
- // check area relative to nut size.
- Point2I cTL(-(mNutSize *2), -(mNutSize * 2));
- Point2I cBR((mNutSize * 2), (mNutSize * 2));
- S32 dx = pt.x - x;
- S32 dy = pt.y - y;
- return dx <= cBR.x && dx >= cTL.x && dy <= cBR.y && dy >= cTL.y;
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::updatedSelection()
- {
- mSelSet.clear();
- Vector<SceneObject*>::iterator i;
- for (i = mSelectedObjs.begin(); i != mSelectedObjs.end(); i++)
- {
- mSelSet.addObject(*i);
- }
- }
- EditorToySceneWindow::EditorToySceneWindow()
- : mGridSnap(1.0f,1.0f),
- mSnapThreshold(0.2f),
- mSceneCamPos(0.0f, 0.0f),
- mSceneCamSize(16.0f, 9.0f),
- mDragBegin(0,0)
- {
- VECTOR_SET_ASSOCIATION(mSelectedObjs);
- VECTOR_SET_ASSOCIATION(mDragBeginPoints);
-
- mActive = true;
- mDragBeginPoints.clear();
- mSelectedObjs.clear();
- mMouseMode = Selecting;
- mSizeMode = SizingNone;
- mDropType = DropAtScreenCenter;
- mActiveTool = nullptr;
- mNutSize = 5;
- mDefaultCursor = NULL;
- mLeftRightCursor = NULL;
- mUpDownCursor = NULL;
- mMoveCursor = NULL;
- mAllCam = true;
- mDrawCam = true;
- mDrawAspectSafety = true;
- mDrawSaftey = true;
- }
- bool EditorToySceneWindow::onAdd()
- {
- if (!Parent::onAdd())
- return false;
- mTrash.registerObject();
- mSelSet.registerObject();
- mUndoManager.registerObject();
- return true;
- }
- void EditorToySceneWindow::onRemove()
- {
- Parent::onRemove();
- mTrash.unregisterObject();
- mSelSet.unregisterObject();
- mUndoManager.unregisterObject();
- }
- //-----------------------------------------------------------------------
- EditorToySceneWindow::~EditorToySceneWindow()
- {
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::setEditScene(Scene* pScene)
- {
- Parent::setScene(pScene);
- if (getScene())
- getScene()->setIsEditorScene(true);
- mCurrentScene = pScene;
- // find out if these vars are set.
- const char* pos = mCurrentScene->getDataField("CamPos", NULL);
- // if not load some defaults.
- if (Utility::mGetStringElementCount(pos) != 2)
- {
- mCurrentScene->setDataField("CamPos", NULL, "0.0 0.0");
- pos = "0.0 0.0";
- }
- const char* size = mCurrentScene->getDataField("CamSize", NULL);
- if (Utility::mGetStringElementCount(size) != 2)
- {
- mCurrentScene->setDataField("CamSize", NULL, "16.0 9.0");
- size = "16.0 9.0";
- }
- mSceneCamPos = Utility::mGetStringElementVector(pos);
- mSceneCamSize = Utility::mGetStringElementVector(size);
- mSceneCamRect = updateSceneCamera();
- }
- //-----------------------------------------------------------------------
- RectI EditorToySceneWindow::updateSceneCamera()
- {
- Vector2 uppLeft = mSceneCamPos - (mSceneCamSize * 0.5f);
- Vector2 lowRight = mSceneCamPos + (mSceneCamSize * 0.5f);
- sceneToWindowPoint(uppLeft, uppLeft);
- sceneToWindowPoint(lowRight, lowRight);
-
- Point2I winUppLeft = localToGlobalCoord(Point2I(uppLeft.x, uppLeft.y));
- Point2I winLowRight = localToGlobalCoord(Point2I(lowRight.x, lowRight.y));
- return RectI((S32)winUppLeft.x, (S32)winUppLeft.y,
- S32(winLowRight.x - winUppLeft.x),
- S32(winLowRight.y - winUppLeft.y));
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::resetEditScene()
- {
- if (getScene())
- getScene()->setIsEditorScene(false);
- Parent::resetScene();
- }
- //-----------------------------------------------------------------------
- S32 EditorToySceneWindow::getNutHit(const Point2I &pt, const RectI &box)
- {
- S32 lx = box.point.x, rx = box.point.x + box.extent.x - 1;
- S32 cx = (lx + rx) >> 1;
- S32 ty = box.point.y, by = box.point.y + box.extent.y - 1;
- S32 cy = (ty + by) >> 1;
- if (inNut(pt, lx, ty))
- return SizingLeft | SizingTop;
- if (inNut(pt, rx, ty))
- return SizingRight | SizingTop;
- if (inNut(pt, lx, by))
- return SizingLeft | SizingBottom;
- if (inNut(pt, rx, by))
- return SizingRight | SizingBottom;
-
- return SizingNone;
- }
- S32 EditorToySceneWindow::getDirNutHit(const Point2I & pt, SceneObject * obj)
- {
- // vars
- Vector2 pos = obj->getPosition();
- Vector2 size = obj->getSize();
- F32 ang = obj->getAngle();
- // right
- Vector2 right = right.getVecFromAng(pos, ang, (size.x*0.5f));
- // top
- Vector2 top = top.getVecFromAng(pos, ang + mDegToRad(90.0f), (size.y*0.5f));
- // left
- Vector2 left = left.getVecFromAng(pos, ang - mDegToRad(180.0f), (size.x*0.5f));
- // bottom
- Vector2 bottom = bottom.getVecFromAng(pos, ang - mDegToRad(90.0f), (size.y*0.5f));
- // cardinal
- Vector2 r;
- Vector2 l;
- Vector2 t;
- Vector2 b;
- // cardinal
- sceneToWindowPoint(right, r);
- sceneToWindowPoint(left, l);
- sceneToWindowPoint(top, t);
- sceneToWindowPoint(bottom, b);
- F32 deg = mRadToDeg(ang);
- if (deg == 90.0f)
- {
- if (inNut(pt, (S32)(l.x), (S32)(l.y)))
- return SizingBottom;
- if (inNut(pt, (S32)(r.x), (S32)(r.y)))
- return SizingTop;
- if (inNut(pt, (S32)(b.x), (S32)(b.y)))
- return SizingRight;
- if (inNut(pt, (S32)(t.x), (S32)(t.y)))
- return SizingLeft;
- }
- else if (deg == 180.0f || deg == -180.0f)
- {
- if (inNut(pt, (S32)(l.x), (S32)(l.y)))
- return SizingRight;
- if (inNut(pt, (S32)(r.x), (S32)(r.y)))
- return SizingLeft;
- if (inNut(pt, (S32)(b.x), (S32)(b.y)))
- return SizingTop;
- if (inNut(pt, (S32)(t.x), (S32)(t.y)))
- return SizingBottom;
- }
- else if (deg == -90.0f)
- {
- if (inNut(pt, (S32)(l.x), (S32)(l.y)))
- return SizingTop;
- if (inNut(pt, (S32)(r.x), (S32)(r.y)))
- return SizingBottom;
- if (inNut(pt, (S32)(b.x), (S32)(b.y)))
- return SizingLeft;
- if (inNut(pt, (S32)(t.x), (S32)(t.y)))
- return SizingRight;
- }
- else
- {
- if (inNut(pt, (S32)(l.x), (S32)(l.y)))
- return SizingLeft;
- if (inNut(pt, (S32)(r.x), (S32)(r.y)))
- return SizingRight;
- if (inNut(pt, (S32)(b.x), (S32)(b.y)))
- return SizingBottom;
- if (inNut(pt, (S32)(t.x), (S32)(t.y)))
- return SizingTop;
- }
- return SizingNone;
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::drawNut(const Point2I & nut, ColorI & outlineColor, ColorI & nutColor)
- {
- dglDrawCircleFill(nut,(F32)mNutSize, nutColor);
- dglDrawCircle(nut, (F32)mNutSize, outlineColor);
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::drawSizingNuts(SceneObject* obj, ColorI &outlineColor, ColorI &nutColor)
- {
- // vars
- // there has to be a better way...
- Vector2 pos = obj->getPosition();
- Vector2 size = obj->getSize();
- F32 ang = obj->getAngle();
- // right
- Vector2 right = right.getVecFromAng(pos, ang, (size.x*0.5f));
- Vector2 topRight = topRight.getVecFromAng(right, (ang + 1.5708f), (size.y*0.5f));
- // top
- Vector2 top = top.getVecFromAng(pos, (ang + 1.5708f), (size.y*0.5f));
- Vector2 topLeft = topLeft.getVecFromAng(top, ((ang + 1.5708f) + 1.5708f), (size.x*0.5f));
- // left
- Vector2 left = left.getVecFromAng(pos, ang + 3.14159f, (size.x*0.5f));
- Vector2 bottomLeft = bottomLeft.getVecFromAng(left, (ang - 1.5708f), (size.y*0.5f));
- // bottom
- Vector2 bottom = bottom.getVecFromAng(pos, ang - 1.5708f, (size.y*0.5f));
- Vector2 bottomRight = bottomRight.getVecFromAng(bottom, ang, (size.x*0.5f));
- // cardinal
- Vector2 r;
- Vector2 l;
- Vector2 t;
- Vector2 b;
- // corners
- Vector2 tr;
- Vector2 tl;
- Vector2 bl;
- Vector2 br;
- // cardinal
- sceneToWindowPoint(right, r);
- sceneToWindowPoint(left, l);
- sceneToWindowPoint(top, t);
- sceneToWindowPoint(bottom, b);
- // corners
- sceneToWindowPoint(topRight, tr);
- sceneToWindowPoint(topLeft, tl);
- sceneToWindowPoint(bottomLeft, bl);
- sceneToWindowPoint(bottomRight, br);
- // draw lines
- dglDrawLine((S32)(tr.x), (S32)(tr.y), (S32)(tl.x), (S32)(tl.y), outlineColor );
- dglDrawLine((S32)(tl.x), (S32)(tl.y), (S32)(bl.x), (S32)(bl.y),outlineColor );
- dglDrawLine((S32)(bl.x), (S32)(bl.y), (S32)(br.x), (S32)(br.y),outlineColor );
- dglDrawLine((S32)(br.x), (S32)(br.y), (S32)(tr.x), (S32)(tr.y), outlineColor );
- drawNut(Point2I((S32)(r.x), (S32)r.y), outlineColor, nutColor);
- drawNut(Point2I((S32)(l.x), (S32)l.y), outlineColor, nutColor);
- drawNut(Point2I((S32)(t.x), (S32)(t.y)), outlineColor, nutColor);
- drawNut(Point2I((S32)(b.x), (S32)(b.y)), outlineColor, nutColor);
- }
- bool EditorToySceneWindow::getRotateNutHit(const Point2I & pt, SceneObject* obj)
- {
- // vars
- Vector2 pos = obj->getPosition();
- Vector2 size = obj->getSize();
- F32 ang = obj->getAngle();
- // right
- Vector2 edge = edge.getVecFromAng(pos, ang, (size.x*0.5f));
- Vector2 rotate = rotate.getVecFromAng(edge, ang, (size.x*0.5f));
- Vector2 r;
- sceneToWindowPoint(rotate, r);
- return inNut(pt, (S32)(r.x), (S32)(r.y));
- }
- void EditorToySceneWindow::drawRotateNuts(SceneObject* obj, ColorI &outlineColor, ColorI &nutColor)
- {
- // vars
- Vector2 pos = obj->getPosition();
- Vector2 size = obj->getSize();
- F32 ang = obj->getAngle();
- // right
- Vector2 edge = edge.getVecFromAng(pos,ang,(size.x*0.5f));
- Vector2 rotate = rotate.getVecFromAng(edge,ang,(size.x*0.5f));
- Vector2 e;
- Vector2 r;
- sceneToWindowPoint(edge, e);
- sceneToWindowPoint(rotate, r);
- // draw lines
- dglDrawLine((S32)(e.x), (S32)(e.y), (S32)(r.x), (S32)(r.y), outlineColor);
- drawNut(Point2I((S32)(r.x), (S32)r.y), outlineColor, nutColor);
- }
- //-----------------------------------------------------------------------
- // Sizing Cursors
- bool EditorToySceneWindow::initCursors()
- {
- if (mMoveCursor == NULL || mUpDownCursor == NULL || mLeftRightCursor == NULL || mDefaultCursor == NULL)
- {
- SimObject *obj;
- obj = Sim::findObject("MoveCursor");
- mMoveCursor = dynamic_cast<GuiCursor*>(obj);
- obj = Sim::findObject("UpDownCursor");
- mUpDownCursor = dynamic_cast<GuiCursor*>(obj);
- obj = Sim::findObject("LeftRightCursor");
- mLeftRightCursor = dynamic_cast<GuiCursor*>(obj);
- obj = Sim::findObject("DefaultCursor");
- mDefaultCursor = dynamic_cast<GuiCursor*>(obj);
- obj = Sim::findObject("MoveCursor");
- mMoveCursor = dynamic_cast<GuiCursor*>(obj);
- return(mMoveCursor != NULL && mUpDownCursor != NULL && mLeftRightCursor != NULL && mDefaultCursor != NULL && mMoveCursor != NULL);
- }
- else
- return(true);
- }
- void EditorToySceneWindow::getCursor(GuiCursor *&cursor, bool &showCursor, const GuiEvent &gEvt)
- {
- showCursor = true;
- SceneObject *obj;
- Point2I mousePos = globalToLocalCoord(gEvt.mousePoint);
- // first see if we hit a sizing knob on the currently selected control...
- if (mSelectedObjs.size() == 1 && initCursors() == true)
- {
- obj = mSelectedObjs.first();
- F32 deg = mRadToDeg(obj->getAngle());
- EditorToySceneWindow::SizingMode sizeMode = (EditorToySceneWindow::SizingMode)getDirNutHit(mousePos, obj);
- if (mMouseMode == SizingSelection)
- {
- if (mSizeMode == SizingLeft || mSizeMode == SizingRight)
- cursor = mLeftRightCursor;
- else if (mSizeMode == SizingTop || mSizeMode == SizingBottom)
- cursor = mUpDownCursor;
- else
- cursor = NULL;
- }
- else
- {
- // Check for current mouse position after checking for actual sizing mode
- if (sizeMode == SizingLeft || sizeMode == SizingRight)
- cursor = mLeftRightCursor;
- else if (sizeMode == SizingTop || sizeMode == SizingBottom)
- cursor = mUpDownCursor;
- else
- cursor = NULL;
- }
- }
- if (mMouseMode == MovingSelection && cursor == NULL)
- cursor = mMoveCursor;
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::getDragRect(RectI &box)
- {
- // draw the box in gui space. worldQuery to convert to scene.
- box.point.x = getMin(mLastMousePt.x, mSelectionAnchor.x);
- box.extent.x = getMax(mLastMousePt.x, mSelectionAnchor.x) - box.point.x + 1;
- box.point.y = getMin(mLastMousePt.y, mSelectionAnchor.y);
- box.extent.y = getMax(mLastMousePt.y, mSelectionAnchor.y) - box.point.y + 1;
- }
- //-----------------------------------------------------------------------
- RectI EditorToySceneWindow::objWindowBounds(const SceneObject* obj)
- {
- const RectF objAABBRect = obj->getAABBRectangle();
- Vector2 upLeft = Vector2(objAABBRect.point.x, objAABBRect.point.y + objAABBRect.extent.y);
- Vector2 lowRight = Vector2(objAABBRect.point.x + objAABBRect.extent.x, objAABBRect.point.y);
- Vector2 winUpLeft;
- Vector2 winLowRight;
- sceneToWindowPoint(upLeft, winUpLeft);
- sceneToWindowPoint(lowRight, winLowRight);
- return RectI(S32(winUpLeft.x), S32(winUpLeft.y),
- S32(winLowRight.x - winUpLeft.x),
- S32(winLowRight.y - winUpLeft.y));
- }
- //-----------------------------------------------------------------------
- Point2I EditorToySceneWindow::objWindowPos(const SceneObject * obj)
- {
- Vector2 pos = obj->getPosition();
- Vector2 winPos;
- sceneToWindowPoint(pos, winPos);
- return Point2I((S32)winPos.x,(S32)winPos.y);
- }
- //-----------------------------------------------------------------------
- bool EditorToySceneWindow::selectionContains(SceneObject *obj)
- {
- Vector<SceneObject*>::iterator i;
- for (i = mSelectedObjs.begin(); i != mSelectedObjs.end(); i++)
- {
- if (obj == *i)
- return true;
- }
- return false;
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::onTouchDown(const GuiEvent& gEvt)
- {
- if (!mCurrentScene)
- {
- //no scene, pass to parent and do nothing!
- Parent::onTouchDown(gEvt);
- return;
- }
- if (mActiveTool)
- {
- // if we have an active tool, give it the touch event.
- mActiveTool->onTouchDown(gEvt);
- return;
- }
- mouseLock();
- setFirstResponder();
- Point2I ctOff;
- Point2I oExt;
- SceneObject* obj;
- mLastMousePt = globalToLocalCoord(gEvt.mousePoint);
- Vector2 sceneMousePt;
- windowToScenePoint(mLastMousePt, sceneMousePt);
-
- WorldQuery* mWQ = mCurrentScene->getWorldQuery(true);
- WorldQueryFilter mQF(MASK_ALL, MASK_ALL, true, false, true, true);
- mWQ->setQueryFilter(mQF);
- mWQ->anyQueryPoint(sceneMousePt);
- U32 resCount = mWQ->getQueryResultsCount();
- typeWorldQueryResultVector& queryResults = mWQ->getQueryResults();
- // only apply the obj if we actually clicked something.
- if (resCount > 0)
- {
- obj = queryResults[0].mpSceneObject;
- }
- // If we have an object selected already, test sizing.
- if (mSelectedObjs.size() == 1)
- {
- SceneObject* obj = mSelectedObjs.first();
- RectI objBox = objWindowBounds(obj);
- RectI box(objBox.point.x, objBox.point.y, objBox.extent.x, objBox.extent.y);
- mSizeMode = (EditorToySceneWindow::SizingMode)getDirNutHit(mLastMousePt, obj);
- if (mSizeMode != SizingNone)
- {
- mMouseMode = SizingSelection;
- // for the undo
- Con::executef(this, 2, "onPreEdit", Con::getIntArg(getSelectionSet().getId()));
- return;
- }
- else if (getRotateNutHit(mLastMousePt,obj))
- {
- mMouseMode = RotateSelection;
- mStartAngle = obj->getAngle();
- mStartVector = sceneMousePt - obj->getPosition();
- // for the undo
- Con::executef(this, 2, "onPreEdit", Con::getIntArg(getSelectionSet().getId()));
- return;
- }
- }
- // handle movement for objs in selection.
- if (resCount > 0)
- {
- if (selectionContains(obj))
- {
- if (gEvt.modifier & SI_SHIFT)
- {
- Vector<SceneObject*>::iterator i;
- for (i = mSelectedObjs.begin(); i != mSelectedObjs.end(); i++)
- {
- if (*i == obj)
- {
- Con::executef(this, 2, "onRemoveSelected", Con::getIntArg(obj->getId()));
- mSelectedObjs.erase(i);
- break;
- }
- }
- mMouseMode = Selecting;
- }
- else
- {
- mDragBegin = gEvt.mousePoint;
- mDragBeginPoints.reserve(mSelectedObjs.size());
- // do this whether its 1 object or 100
- Vector<SceneObject*>::iterator i;
- for (i = mSelectedObjs.begin(); i != mSelectedObjs.end(); i++)
- {
- mDragBeginPoints.push_back((*i)->getPosition());
- }
- Vector2 pos = obj->getPosition();
- if(mSelectedObjs.size() > 1)
- mOffset = mSelCenter - sceneMousePt;
- else
- mOffset = pos - sceneMousePt;
-
- mMouseMode = MovingSelection;
- //undo
- Con::executef(this, 2, "onPreEdit", Con::getIntArg(getSelectionSet().getId()));
- }
- }
- else if(gEvt.modifier & SI_SHIFT)
- {
- mSelectedObjs.push_back(obj);
- Con::executef(this, 2, "onAddSelected", Con::getIntArg(obj->getId()));
- // update bounds.
- updateSelectionBounds();
- }
- else
- {
- //clear and select the object clicked
- mSelectedObjs.clear();
- mSelectedObjs.push_back(obj);
- }
- }
- else
- {
- // if no modifier clear the selection.
- if (!(gEvt.modifier & SI_SHIFT))
- {
- Con::executef(this, 1, "onClearSelected");
- mSelectedObjs.clear();
- }
- // start drgging
- // save this, we need it later.
- mSelectionAnchor = mLastMousePt;
- mMouseMode = DragSelection;
- }
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::onTouchUp(const GuiEvent& gEvt)
- {
- if (!mCurrentScene)
- {
- //no scene do nothing!
- Parent::onTouchUp(gEvt);
- return;
- }
- if (mActiveTool)
- {
- // if we have an active tool, give it the touch event.
- mActiveTool->onTouchUp(gEvt);
- return;
- }
- mouseUnlock();
- // house keeping
- mDragBegin.set(0, 0);
- mDragBeginPoints.clear();
- mLastMousePt = globalToLocalCoord(gEvt.mousePoint);
- if (mMouseMode == DragSelection)
- {
- RectI drag;
- getDragRect(drag);
- Vector2 dragPoint = drag.point;
- Vector2 dragUp;
- dragUp.x = dragPoint.x + drag.extent.x;
- dragUp.y = dragPoint.y + drag.extent.y;
- Vector2 sPt;
- Vector2 sUp;
- windowToScenePoint(dragPoint, sPt);
- windowToScenePoint(dragUp, sUp);
- // Build our area query
- WorldQuery* mWQ = mCurrentScene->getWorldQuery(true);
- WorldQueryFilter mQF(MASK_ALL, MASK_ALL, true, false, true, true);
- mWQ->setQueryFilter(mQF);
- mWQ->anyQueryArea(sPt,sUp);
- for (U32 i = 0; i < mWQ->getQueryResultsCount(); i++)
- {
- SceneObject *obj = mWQ->getQueryResults().at(i).mpSceneObject;;
- mSelectedObjs.push_back(obj);
- }
- updateSelectionBounds();
- }
- // we selected 1 item
- if(mSelectedObjs.size() == 1)
- Con::executef(this, 2, "onSelect", Con::getIntArg(mSelectedObjs[0]->getId()));
- // undo manager for move, size and rotate
- if(mMouseMode == SizingSelection || mMouseMode == MovingSelection || mMouseMode == RotateSelection)
- Con::executef(this, 2, "onPostEdit", Con::getIntArg(getSelectionSet().getId()));
- setFirstResponder();
- mMouseMode = Selecting;
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::onTouchDragged(const GuiEvent& gEvt)
- {
- if (!mCurrentScene)
- {
- //no scene do nothing!
- Parent::onTouchDragged(gEvt);
- return;
- }
- if (mActiveTool)
- {
- // if we have an active tool, give it the touch event.
- mActiveTool->onTouchDragged(gEvt);
- return;
- }
- // some vars
- Point2I curPoint = this->globalToLocalCoord(gEvt.mousePoint);
- Vector2 sceneMousePt;
- windowToScenePoint(curPoint, sceneMousePt);
- SceneObject *obj;
- if (mSelectedObjs.size() > 0)
- {
- obj = mSelectedObjs.first();
- // if our obj is locked, don't do anything!
- if (obj->isLocked())
- return;
- }
-
- if (mMouseMode == SizingSelection)
- {
- Vector2 objPos = obj->getRenderPosition();
- Vector2 objSize = obj->getSize();
- bool ar = true;
- bool fP = false;
- // if shift, no ar kept.
- if ((gEvt.modifier & SI_SHIFT))
- ar = false;
- // if ctrl, scale from center, no pos change, size axis.
- if ((gEvt.modifier & SI_LCTRL))
- {
- fP = true;
- ar = false;
- }
- // if alt, scale from center, no pos change.
- if ((gEvt.modifier & SI_LALT))
- {
- fP = true;
- ar = true;
- }
- scaleObject(objSize, objPos, sceneMousePt, ar, fP);
- }
- else if (mMouseMode == MovingSelection && mSelectedObjs.size())
- {
- moveObject(sceneMousePt + mOffset);
- }
- else if (mMouseMode == RotateSelection)
- {
- rotateObject(sceneMousePt - obj->getPosition(), mStartVector,mStartAngle);
- }
- else
- {
- mLastMousePt = curPoint;
- }
- }
- void EditorToySceneWindow::rotateObject(Vector2 mousePos, Vector2 origVec, F32 origAngle)
- {
- // init vars
- SceneObject* obj = mSelectedObjs.first();
- // some snapping
- F32 snapThresh = mDegToRad(10.0f);
- F32 snapDegrees = mDegToRad(90.0f);
- F32 oldAng = mAtan(origVec.x, origVec.y) + mDegToRad(origAngle);
- F32 ang = mAtan(mousePos.x, mousePos.y);
- F32 closeAng = mFloor((ang / snapDegrees) + 0.5f) * snapDegrees;
- if (mFabs(ang - closeAng) < snapThresh)
- {
- ang = closeAng;
- }
- if (ang > mDegToRad(359.0f))
- {
- ang = 0.0f;
- }
- obj->setAngle(ang);
- }
- void EditorToySceneWindow::moveObject(Vector2 newPos)
- {
- if (mGridSnap.x)
- {
- F32 closeX = mFloor((newPos.x / mGridSnap.x) + 0.5f) * mGridSnap.x;
- if (mFabs(newPos.x - closeX) < mSnapThreshold)
- newPos.x = closeX;
- }
- if (mGridSnap.y)
- {
- F32 closeY = mFloor((newPos.y / mGridSnap.y) + 0.5f) * mGridSnap.y;
- if (mFabs(newPos.y - closeY) < mSnapThreshold)
- newPos.y = closeY;
- }
- // moving multiple objects needs to be offset from center.
- if (mSelectedObjs.size() > 1)
- {
- Vector<SceneObject*>::iterator i;
- for (i = mSelectedObjs.begin(); i != mSelectedObjs.end(); i++)
- {
- (*i)->setPosition(((*i)->getPosition() - mSelCenter) + newPos);
- }
- }
- else
- {
- Vector<SceneObject*>::iterator i = mSelectedObjs.begin();
- (*i)->setPosition(newPos);
- }
- }
- void EditorToySceneWindow::scaleObject(Vector2 size, Vector2 pos, Vector2 mousePos, bool maintainAr, bool fixedPos)
- {
- // init vars
- SceneObject* obj = mSelectedObjs.first();
- Vector2 sizeDelt = Vector2(0.0, 0.0);
- Vector2 newPos = pos;
- Vector2 newSize = size;
- bool flipX = false;
- bool flipY = false;
- F32 ang = mRadToDeg(obj->getAngle());
- if (ang != 90.0f && ang != -180.0f && ang != 0.0f && ang != 180.0f && ang != -90.0f)
- {
- //maintainAr = true;
- fixedPos = true;
- }
- if (mGridSnap.x)
- {
- F32 closeX = mFloor((mousePos.x / mGridSnap.x) + 0.5f) * mGridSnap.x;
- if (mFabs(mousePos.x - closeX) < mSnapThreshold)
- mousePos.x = closeX;
- }
- if (mGridSnap.y)
- {
- F32 closeY = mFloor((mousePos.y / mGridSnap.y) + 0.5f) * mGridSnap.y;
- if (mFabs(mousePos.y - closeY) < mSnapThreshold)
- mousePos.y = closeY;
- }
- if (ang == 90.0f || ang == -90.0f)
- {
- if (mSizeMode == SizingLeft)
- {
- sizeDelt.x = mousePos.x - (pos.x - (size.y * 0.5f));
- newSize.y -= sizeDelt.x;
- newPos.x += sizeDelt.x * 0.5f;
- }
- else if (mSizeMode == SizingRight)
- {
- sizeDelt.x = mousePos.x - (pos.x + (size.y * 0.5f));
- newSize.y += sizeDelt.x;
- newPos.x += sizeDelt.x * 0.5f;
- }
- if (mSizeMode == SizingTop)
- {
- sizeDelt.y = mousePos.y - (pos.y + (size.x * 0.5f));
- newSize.x += sizeDelt.y;
- newPos.y += sizeDelt.y * 0.5f;
- }
- else if (mSizeMode == SizingBottom)
- {
- sizeDelt.y = mousePos.y - (pos.y - (size.x * 0.5f));
- newSize.x -= sizeDelt.y;
- newPos.y += sizeDelt.y * 0.5f;
- }
- // flip the sizing calcs.
- if (newSize.y < 0.0f)
- {
- if (mSizeMode == SizingLeft)
- {
- mSizeMode = SizingRight;
- }
- else if (mSizeMode == SizingRight)
- {
- mSizeMode = SizingLeft;
- }
- flipX = true;
- newSize.y = -newSize.y;
- }
- if (newSize.x < 0.0f)
- {
- if (mSizeMode == SizingTop)
- {
- mSizeMode = SizingBottom;
- }
- else if (mSizeMode == SizingBottom)
- {
- mSizeMode = SizingTop;
- }
- flipY = true;
- newSize.x = -newSize.x;
- }
- }
- else
- {
- if (mSizeMode == SizingLeft)
- {
- sizeDelt.x = mousePos.x - (pos.x - (size.x * 0.5f));
- newSize.x -= sizeDelt.x;
- newPos.x += sizeDelt.x * 0.5f;
- }
- else if (mSizeMode == SizingRight)
- {
- sizeDelt.x = mousePos.x - (pos.x + (size.x * 0.5f));
- newSize.x += sizeDelt.x;
- newPos.x += sizeDelt.x * 0.5f;
- }
- if (mSizeMode == SizingTop)
- {
- sizeDelt.y = mousePos.y - (pos.y + (size.y * 0.5f));
- newSize.y += sizeDelt.y;
- newPos.y += sizeDelt.y * 0.5f;
- }
- else if (mSizeMode == SizingBottom)
- {
- sizeDelt.y = mousePos.y - (pos.y - (size.y * 0.5f));
- newSize.y -= sizeDelt.y;
- newPos.y += sizeDelt.y * 0.5f;
- }
- // flip the sizing calcs.
- if (newSize.x < 0.0f)
- {
- if (mSizeMode == SizingLeft)
- {
- mSizeMode = SizingRight;
- }
- else if (mSizeMode == SizingRight)
- {
- mSizeMode = SizingLeft;
- }
- flipX = true;
- newSize.x = -newSize.x;
- }
- if (newSize.y < 0.0f)
- {
- if (mSizeMode == SizingTop)
- {
- mSizeMode = SizingBottom;
- }
- else if (mSizeMode == SizingBottom)
- {
- mSizeMode = SizingTop;
- }
- flipY = true;
- newSize.y = -newSize.y;
- }
- }
-
- //-----------------------
- if (maintainAr)
- {
- F32 oldAr = size.x / size.y;
- F32 newAr = newSize.x / newSize.y;
- Vector2 prevResize = newSize;
- if (newAr < oldAr)
- {
- if ((newSize.x < size.x))
- {
- newSize.y *= newAr / oldAr;
- }
- else
- {
- newSize.x *= oldAr / newAr;
- }
- }
- else
- {
- if ((newSize.y < size.y) )
- {
- newSize.x *= oldAr / newAr;
- }
- else
- {
- newSize.y *= newAr / oldAr;
- }
- }
- if (ang == 90.0f || ang == -90.0f)
- {
- if (mSizeMode == SizingLeft)
- {
- newPos.x -= (newSize.y - prevResize.y) * 0.5f;
- }
- else if (mSizeMode == SizingRight)
- {
- newPos.x += (newSize.y - prevResize.y) * 0.5f;
- }
- if (mSizeMode == SizingTop)
- {
- newPos.y -= (newSize.x - prevResize.x) * 0.5f;
- }
- else if (mSizeMode == SizingBottom)
- {
- newPos.y += (newSize.x - prevResize.x) * 0.5f;
- }
- }
- else
- {
- if (mSizeMode == SizingLeft)
- {
- newPos.x -= (newSize.x - prevResize.x) * 0.5f;
- }
- else if (mSizeMode == SizingRight)
- {
- newPos.x += (newSize.x - prevResize.x) * 0.5f;
- }
- if (mSizeMode == SizingTop)
- {
- newPos.y += (newSize.y - prevResize.y) * 0.5f;
- }
- else if (mSizeMode == SizingBottom)
- {
- newPos.y -= (newSize.y - prevResize.y) * 0.5f;
- }
- }
- }
- if (fixedPos)
- {
- obj->setPosition(pos);
- }
- else
- {
- obj->setPosition(newPos);
- }
- if (newSize != size)
- {
- obj->setSize(newSize);
- }
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::onTouchMove(const GuiEvent& gEvt)
- {
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::onRightMouseUp(const GuiEvent& gEvt)
- {
- if (!mCurrentScene)
- {
- //no scene do nothing!
- Parent::onRightMouseUp(gEvt);
- return;
- }
- if (mActiveTool)
- {
- // if we have an active tool, give it the touch event.
- mActiveTool->onRightMouseUp(gEvt);
- return;
- }
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::onRightMouseDown(const GuiEvent& gEvt)
- {
- if (!mCurrentScene)
- {
- //no scene do nothing!
- Parent::onRightMouseDown(gEvt);
- return;
- }
- if (mActiveTool)
- {
- // if we have an active tool, give it the touch event.
- mActiveTool->onRightMouseDown(gEvt);
- return;
- }
- // some vars
- Point2I curPoint = this->globalToLocalCoord(gEvt.mousePoint);
- windowToScenePoint(curPoint, mMouseCamDown);
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::onRightMouseDragged(const GuiEvent& gEvt)
- {
- if (!mCurrentScene)
- {
- //no scene do nothing!
- Parent::onRightMouseDragged(gEvt);
- return;
- }
- if (mActiveTool)
- {
- // if we have an active tool, give it the touch event.
- mActiveTool->onRightMouseDragged(gEvt);
- return;
- }
- // some vars
- Point2I curPoint = this->globalToLocalCoord(gEvt.mousePoint);
- Vector2 sceneMousePt;
- windowToScenePoint(curPoint, sceneMousePt);
- Vector2 move = mMouseCamDown - sceneMousePt;
- Vector2 newPos = getCameraPosition() + move;
- setCameraPosition(newPos);
- }
- bool EditorToySceneWindow::onMouseWheelDown(const GuiEvent & gEvt)
- {
- // Call Parent.
- Parent::onMouseWheelDown(gEvt);
- F32 zoom = getCameraZoom();
- if (zoom < 0.1f)
- return false;
- F32 amt = -10 * 0.005f * zoom;
- setCameraZoom(zoom + amt);
- return true;
- }
- bool EditorToySceneWindow::onMouseWheelUp(const GuiEvent & gEvt)
- {
- // Call Parent.
- Parent::onMouseWheelUp(gEvt);
- F32 zoom = getCameraZoom();
- F32 amt = 10 * 0.005f * zoom;
- setCameraZoom(zoom + amt);
- return true;
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::updateSelectionBounds()
- {
- // we want everything to be smaller.
- Point2I point(S32_MAX, S32_MAX);
- // we want everything to be bigger.
- Point2I extentPos(S32_MIN, S32_MIN);
- Vector<SceneObject*>::iterator i;
- for (i = mSelectedObjs.begin(); i != mSelectedObjs.end(); i++)
- {
- RectI bounds = objWindowBounds((*i));
- //bottom right
- Point2I bottomRight((bounds.point.x + bounds.extent.x), (bounds.point.y + bounds.extent.y));
- if (bounds.point.x < point.x)
- point.x = bounds.point.x;
- if (bounds.point.y < point.y)
- point.y = bounds.point.y;
- if (bottomRight.x > extentPos.x)
- extentPos.x = bottomRight.x;
- if (bottomRight.y > extentPos.y)
- extentPos.y = bottomRight.y;
- }
- Point2I extent = extentPos - point;
- mSelBox = RectI(point.x, point.y, extent.x, extent.y);
- mSelCenter = getSelectionCenter();
- }
- Vector2 EditorToySceneWindow::getSelectionCenter()
- {
- Vector2 center(mSelBox.point.x + (mSelBox.extent.x * 0.5f), mSelBox.point.y + (mSelBox.extent.y * 0.5f));
- Vector2 sceneCenter;
- windowToScenePoint(center, sceneCenter);
- return sceneCenter;
- }
- void EditorToySceneWindow::onRender(Point2I offset, const RectI &updateRect)
- {
- // let parent do its thing or you wont see anything
- Parent::onRender(offset, updateRect);
- if (mCurrentScene)
- {
- if (mAllCam)
- {
- // render camera.
- RectI c;
- c = updateSceneCamera();
- Point2I inset(c.extent.x * 0.1, c.extent.y * 0.1);
- if (mDrawAspectSafety)
- {
- // Aspect ratio percent
- c.inset(-inset.x, -inset.y);
- dglDrawRect(c, ColorI(255, 0, 0, 100));
- // ordinary cam
- c.inset(inset.x, inset.y);
- }
- if (mDrawCam)
- {
- dglDrawRect(c, ColorI(255, 255, 255, 255));
- }
- if (mDrawSaftey)
- {
- // 90% inside safety
- c.inset(inset.x, inset.y);
- dglDrawRect(c, ColorI(0, 255, 0, 100));
- }
- }
- bool multi = mSelectedObjs.size() > 1;
- if (multi)
- {
- //RectI b = windowSelectionBounds();
- updateSelectionBounds();
- mSelBox.point += offset;
- dglDrawRect(mSelBox, ColorI(255, 255, 255, 255));
- }
- Vector<SceneObject*>::iterator i;
- for (i = mSelectedObjs.begin(); i != mSelectedObjs.end(); i++)
- {
- SceneObject* obj = (*i);
- ColorI nutColor = multi ? ColorI(255, 255, 255) : ColorI(100, 100, 200);
- ColorI outlineColor = ColorI(255, 255, 255);
- // only draw rotate when we are not in multi
- // draw rotate first so its under other nuts.
- if (!multi)
- {
- drawRotateNuts(obj, outlineColor, nutColor);
- }
- drawSizingNuts(obj, outlineColor, nutColor);
- }
-
- if (mMouseMode == DragSelection)
- {
- RectI b;
- getDragRect(b);
- mSelBox.point += offset;
- dglDrawRect(b, ColorI(255, 255, 255, 255));
- }
- }
- if (mCurrentScene && (mGridSnap.x && mGridSnap.y) &&
- (mMouseMode == MovingSelection || mMouseMode == SizingSelection))
- {
- RectF area = getCameraRenderArea();
- U32 maxdot = (U32)(area.extent.x / mGridSnap.x) * (U32)(area.extent.y / mGridSnap.y);
- Vector2 curDot;
- Point2F areaEnd(area.point.x + area.extent.x, area.point.y + area.extent.y);
- area.point.x = mGridSnap.x * ((S32)(area.point.x / mGridSnap.x));
- area.point.y = mGridSnap.y * ((S32)(area.point.y / mGridSnap.y));
- //vector<F32> verts;
- for (F32 ix = area.point.x; ix < areaEnd.x; ix += mGridSnap.x)
- {
- for (F32 iy = area.point.y; iy < areaEnd.y; iy += mGridSnap.y)
- {
- curDot.Set(ix, iy);
- sceneToWindowPoint(curDot, curDot);
- curDot = (Vector2)localToGlobalCoord(Point2I(S32(curDot.x), S32(curDot.y)));
- //verts.push_back(curDot.x);
- //verts.push_back(curDot.y);
- // the commented out method for doing this is a lot faster
- // but it crashes if there are too many, slow is better than
- // crashing, especially just in an editor.
- dglDrawDot((Point2F)curDot, ColorI(255, 255, 255, 128));
- }
- }
- //U32 num = verts.size();
- //glEnableClientState(GL_VERTEX_ARRAY);
- //glVertexPointer(2, GL_FLOAT, 0, verts.data());
- //glDrawArrays(GL_POINTS, 0, num);
- //glDisableClientState(GL_VERTEX_ARRAY);
- }
- }
- void EditorToySceneWindow::resize(const Point2I &newPosition, const Point2I &newExtent)
- {
- Point2F extent = getCameraArea().extent;
- F32 aR = (F32)newExtent.x / (F32)newExtent.y;
- F32 localAr = extent.x / extent.y;
- extent.x = extent.y * aR;
- Parent::resize(newPosition, newExtent);
- Parent::setCameraPosition(getCameraPosition());
- Parent::setCameraSize(Vector2(extent.x, extent.y));
- }
- //-----------------------------------------------------------------------
- void EditorToySceneWindow::setTool(EditorToyTool * tool)
- {
- if (mActiveTool)
- mActiveTool->onDeactivate();
- mActiveTool = tool;
- if (mActiveTool)
- mActiveTool->onActivate(this);
- }
- //-----------------------------------------------------------------------
- // Required so editor can re-handle ontouch
- void EditorToySceneWindow::deactivateTool()
- {
- mActiveTool->onDeactivate();
- mActiveTool = nullptr;
- }
- //-----------------------------------------------------------------------
|