Ver Fonte

Chain Control GuiEditor Support

This code makes the Chain Control function better in the GuiEditor and adds a few new callbacks for when objects move and children are rearragened.
Peter Robinson há 2 anos atrás
pai
commit
b16c1d42dc

+ 1 - 1
editor/GuiEditor/scripts/GuiEditorBrain.cs

@@ -41,7 +41,7 @@ function GuiEditorBrain::onControlDropped(%this, %payload, %position)
    %this.setFirstResponder();
    %this.postEvent("AddControl", %payload);
     %this.postEvent("Inspect", %payload);
-   %this.schedule(100, "finishControlDropped", %payload, %x, %y);
+   %this.schedule(40, "finishControlDropped", %payload, %x, %y);
 }
 
 function GuiEditorBrain::finishControlDropped(%this, %payload, %x, %y)

+ 54 - 4
engine/source/gui/containers/guiChainCtrl.cc

@@ -50,9 +50,29 @@ void GuiChainCtrl::initPersistFields()
 }
 
 //------------------------------------------------------------------------------
+void GuiChainCtrl::inspectPreApply()
+{
+	mPrevIsVertical = mIsVertical;
+	Parent::inspectPreApply();
+}
 
 void GuiChainCtrl::inspectPostApply()
 {
+	if (mPrevIsVertical != mIsVertical && smDesignTime)
+	{
+		//Since we're in the editor, we'll swap x and y, then calculateExtent will fix one of them.
+		iterator i;
+		for (i = begin(); i != end(); i++)
+		{
+			GuiControl* ctrl = static_cast<GuiControl*>(*i);
+			if (ctrl->isVisible())
+			{
+				S32 temp = ctrl->mBounds.point.x;
+				ctrl->mBounds.point.x = ctrl->mBounds.point.y;
+				ctrl->mBounds.point.y = temp;
+			}
+		}
+	}
 	calculateExtent();
 	Parent::inspectPostApply();
 }
@@ -61,7 +81,20 @@ void GuiChainCtrl::inspectPostApply()
 
 void GuiChainCtrl::childResized(GuiControl *child)
 {
-	calculateExtent();
+	calculateExtent(smDesignTime);
+	Parent::childResized(child);
+}
+
+void GuiChainCtrl::childMoved(GuiControl* child)
+{
+	calculateExtent(smDesignTime);
+	Parent::childMoved(child);
+}
+
+void GuiChainCtrl::childrenReordered()
+{
+	calculateExtent(smDesignTime);
+	Parent::childrenReordered();
 }
 
 void GuiChainCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
@@ -92,6 +125,12 @@ void GuiChainCtrl::onChildAdded(GuiControl *child)
 	{
 		child->setVertSizing(vertResizeTop);
 	}
+
+	if (smDesignTime)
+	{
+		child->mBounds.point = Point2I::Zero;
+	}
+
 	Parent::onChildAdded(child);
 	calculateExtent();
 }
@@ -101,13 +140,24 @@ void GuiChainCtrl::onChildRemoved(SimObject *child)
 	calculateExtent();
 }
 
-void GuiChainCtrl::calculateExtent()
+void GuiChainCtrl::calculateExtent(bool holdLength)
 {
 	Point2I offset = Point2I(mBounds.point.Zero);
 	Point2I extent = Point2I(getExtent());
 	RectI innerRect = getInnerRect(offset, extent, NormalState, mProfile);
 
 	S32 length = positionChildren(innerRect);
+	S32 oldLength = mIsVertical ? innerRect.extent.y : innerRect.extent.x;
+
+	if (smDesignTime)
+	{
+		length += 20;//This gives some space to add new controls.
+	}
+
+	if (holdLength && oldLength > length)
+	{
+		length = oldLength;
+	}
 
 	if (!mIsVertical)
 	{
@@ -139,7 +189,7 @@ S32 GuiChainCtrl::positionChildren(RectI &innerRect)
 	for (i = begin(); i != end(); i++)
 	{
 		GuiControl *ctrl = static_cast<GuiControl *>(*i);
-		if (ctrl->isVisible())
+		if (ctrl->isVisible() || smDesignTime)
 		{
 			if (length != 0)
 			{
@@ -154,7 +204,7 @@ S32 GuiChainCtrl::positionChildren(RectI &innerRect)
 			{
 				childPos.y = length;
 			}
-			ctrl->setPosition(childPos);
+			ctrl->mBounds.point = childPos;
 			length += mIsVertical ? ctrl->getExtent().y : ctrl->getExtent().x;
 		}
 	}

+ 5 - 1
engine/source/gui/containers/guiChainCtrl.h

@@ -17,19 +17,23 @@ class GuiChainCtrl : public GuiControl
 {
 private:
 	typedef GuiControl Parent;
+	bool mPrevIsVertical;
 
 protected:
 	S32 mChildSpacing;
 	bool mIsVertical;
 
-	virtual void calculateExtent();
+	virtual void calculateExtent(bool holdLength = false);
 	virtual S32 positionChildren(RectI &innerRect);
 
 public:
 	GuiChainCtrl();
 
 	void childResized(GuiControl *child);
+	void childMoved(GuiControl* child);
+	void childrenReordered();
 	void resize(const Point2I &newPosition, const Point2I &newExtent);
+	void inspectPreApply();
 	void inspectPostApply();
 	void onChildAdded(GuiControl *child);
 	void onChildRemoved(SimObject *child);

+ 31 - 1
engine/source/gui/guiControl.cc

@@ -413,6 +413,7 @@ void GuiControl::resize(const Point2I &newPosition, const Point2I &newExtent)
 	Point2I oldExtent = mBounds.extent;
 
     //force center if using center positioning
+	Point2I oldPosition = mBounds.point;
     Point2I actualNewPosition = Point2I(newPosition);
     GuiControl* parent = getParent();
     if (parent)
@@ -429,6 +430,7 @@ void GuiControl::resize(const Point2I &newPosition, const Point2I &newExtent)
 
    // only do the child control resizing stuff if you really need to.
    bool extentChanged = (actualNewExtent != oldExtent);
+   bool positionChanged = (actualNewPosition != oldPosition);
 
    if (extentChanged) {
       //call set update both before and after
@@ -450,8 +452,16 @@ void GuiControl::resize(const Point2I &newPosition, const Point2I &newExtent)
 		  Con::executef(this, 2, "onResize");
 	  }
    }
-   else {
+   if(positionChanged) 
+   {
       mBounds.point = actualNewPosition;
+	  if(parent)
+		parent->childMoved(this);
+
+	  if (isMethod("onMoved"))
+	  {
+		  Con::executef(this, 2, "onMoved");
+	  }
    }
 }
 void GuiControl::setPosition( const Point2I &newPosition )
@@ -499,6 +509,26 @@ void GuiControl::childResized(GuiControl *child)
 	}
 }
 
+void GuiControl::childMoved(GuiControl* child)
+{
+	// Default to do nothing. Do not call resize from here as it will create an infinite loop.
+
+	if (isMethod("onChildMoved"))
+	{
+		Con::executef(this, 3, "onChildMoved", child->getIdString());
+	}
+}
+
+void GuiControl::childrenReordered()
+{
+	// Default to do nothing.
+
+	if (isMethod("onChildrenReordered"))
+	{
+		Con::executef(this, 2, "onChildrenReordered");
+	}
+}
+
 void GuiControl::parentResized(const Point2I &oldParentExtent, const Point2I &newParentExtent)
 {
    Point2I newPosition = getPosition();

+ 7 - 0
engine/source/gui/guiControl.h

@@ -445,6 +445,13 @@ public:
     /// @param   child   Child object
     virtual void childResized(GuiControl *child);
 
+	/// Called when a child control of the object is moved
+	/// @param   child   Child object
+	virtual void childMoved(GuiControl* child);
+
+	/// Called when the children of this control may have been reordered
+	virtual void childrenReordered();
+
     /// Called when this objects parent is resized
     /// @param   oldParentExtent   The old size of the parent object
     /// @param   newParentExtent   The new size of the parent object

+ 1 - 1
engine/source/gui/guiControl_ScriptBinding.h

@@ -247,7 +247,7 @@ ConsoleMethodWithDocs( GuiControl, setPositionGlobal, ConsoleVoid, 4, 4, (int x,
    lPosOffset.x += object->mBounds.point.x;
    lPosOffset.y += object->mBounds.point.y;
 
-   object->mBounds.set(lPosOffset,object->mBounds.extent);
+   object->resize(lPosOffset,object->mBounds.extent);
 }
 
 /*! Sets the current control position in local space

+ 19 - 5
engine/source/gui/guiTreeViewCtrl.cc

@@ -50,12 +50,13 @@ GuiTreeViewCtrl::TreeItem* GuiTreeViewCtrl::grabItemPtr(S32 index)
 	// Range Check
 	if (index >= mItems.size() || index < 0)
 	{
-		Con::warnf("GuiListBoxCtrl::grabItemPtr - index out of range!");
+		Con::warnf("GuiTreeViewCtrl::grabItemPtr - index out of range!");
 		return nullptr;
 	}
 
 	// Grab our item
-	return dynamic_cast<GuiTreeViewCtrl::TreeItem*>(mItems[index]);
+	TreeItem* treeItem = dynamic_cast<GuiTreeViewCtrl::TreeItem*>(mItems[index]);
+	return treeItem;
 }
 
 void GuiTreeViewCtrl::initPersistFields()
@@ -93,7 +94,10 @@ void GuiTreeViewCtrl::onTouchUp(const GuiEvent& event)
 	if (mDragActive && mIsDragLegal)
 	{
 		TreeItem* dragItem = grabItemPtr(mDragIndex);
-		dragItem->isOpen = true;
+		if (mReorderMethod == ReorderMethod::Below && dragItem->isOpen)
+		{
+			mReorderMethod = ReorderMethod::Insert;
+		}
 		SimGroup* target = static_cast<SimGroup*>(mReorderMethod == ReorderMethod::Insert ? dragItem->itemData : dragItem->trunk->itemData);
 		
 		if (!target)
@@ -117,6 +121,11 @@ void GuiTreeViewCtrl::onTouchUp(const GuiEvent& event)
 				checkItem = grabItemPtr(index);
 			}
 		}
+		else
+		{
+			dragItem->isOpen = true;
+		}
+
 		for (S32 i = mItems.size() - 1; i >= 0; i--)
 		{
 			TreeItem* treeItem = dynamic_cast<TreeItem*>(mItems[i]);
@@ -135,6 +144,11 @@ void GuiTreeViewCtrl::onTouchUp(const GuiEvent& event)
 			SimGroup* group = obj->getGroup();
 			group->bringObjectToFront(obj);
 		}
+		GuiControl* control = static_cast<GuiControl*>(mReorderMethod != ReorderMethod::Insert ? dragItem->trunk->itemData : dragItem->itemData);
+		if (control)
+		{
+			control->childrenReordered();
+		}
 		refreshTree();
 	}
 	mDragActive = false;
@@ -389,8 +403,8 @@ void GuiTreeViewCtrl::addBranches(TreeItem* treeItem, SimObject* obj, U16 level)
 		for(auto sub : *setObj)
 		{
 			StringTableEntry text = getObjectText(sub);
-			S32 id = addItemWithID(text, sub->getId(), sub);
-			TreeItem* branch = grabItemPtr(id);
+			S32 index = addItemWithID(text, sub->getId(), sub);
+			TreeItem* branch = grabItemPtr(index);
 			branch->level = level;
 			branch->trunk = treeItem;
 			treeItem->branchList.push_back(branch);