private int mNextPreferedActiveConnectionIndex = 0; // the prefered active connection index according to the brick library

        #endregion Fields

        #region Constructors

        public AddConnectGroup(LayerBrick layer, string partNumber, int wantedConnexion)
        {
            // save the layer and construct the group
            mBrickLayer = layer;
            mGroup = new Layer.Group(partNumber);
            LayerBrick.Brick selectedBrick = layer.getConnectableBrick();

            // get the flat list of bricks from the hierarchical group
            mBricksInTheGroup = mGroup.getAllLeafItems();

            // check if we can attach the group to the unique selected object
            if (selectedBrick != null)
            {
                // find the brick of the group that will be connected to the selected brick
                LayerBrick.Brick brickToConnect = null;
                if (selectedBrick.HasConnectionPoint)
                    brickToConnect = findBrickToConnectAdnSetBestConnectionPointIndex(selectedBrick, ref wantedConnexion);

                // check if the selected brick has connection point
                if (brickToConnect != null && brickToConnect.HasConnectionPoint)
                {
                    // after setting the active connection point index from which this brick will be attached,
                    // get the prefered index from the library
                    mNextPreferedActiveConnectionIndex = BrickLibrary.Instance.getConnectionNextPreferedIndex(partNumber, wantedConnexion);

                    // Compute the orientation of the bricks
                    float newOrientation = AddConnectBrick.sGetOrientationOfConnectedBrick(selectedBrick, brickToConnect);
                    newOrientation -= brickToConnect.Orientation;
                    // Rotate all the bricks of the group first before translating
                    RotateBrickOnPivotBrick rotateBricksAction = new RotateBrickOnPivotBrick(layer, mBricksInTheGroup, newOrientation, brickToConnect);
                    rotateBricksAction.MustUpdateBrickConnectivity = false;
                    rotateBricksAction.redo();

                    // compute the translation to add to all the bricks
                    PointF translation = new PointF(selectedBrick.ActiveConnectionPosition.X - brickToConnect.ActiveConnectionPosition.X,
                                                    selectedBrick.ActiveConnectionPosition.Y - brickToConnect.ActiveConnectionPosition.Y);
                    mGroup.translate(translation);
                }
                else
                {
                    PointF position = selectedBrick.Position;
                    position.X += selectedBrick.DisplayArea.Width;
                    mGroup.Position = position;
                }

                // set the index of the brick in the list just after the selected brick
                mInsertIndex = layer.BrickList.IndexOf(selectedBrick) + 1;
            }
        }
示例#2
0
        private int mNextPreferedActiveConnectionIndex = 0; // the prefered active connection index according to the brick library

        /// <summary>
        /// Add a new named group which has the specified partNumber on the specifier layer, and connected it to
        /// the specified selectedItem (part or group) using the specified wanted connection for that new part.
        /// </summary>
        /// <param name="layer">The layer on which to add the group, and in which the selectedItem is selected</param>
        /// <param name="selectedItem">The single selected item (this can be a single part or a single group). This parameter cannot be null.</param>
        /// <param name="partNumber">The number of the named group to add</param>
        /// <param name="wantedConnexion">The connection index of the group to add that should be used to connect to the selected item, or -1 if you don't care.</param>
        public AddConnectGroup(LayerBrick layer, Layer.LayerItem selectedItem, string partNumber, int wantedConnexion)
        {
            // the selected item should not be null
            System.Diagnostics.Debug.Assert(selectedItem != null);

            // save the layer and construct the group
            mBrickLayer = layer;
            mGroup      = new Layer.Group(partNumber);
            // get the flat list of bricks from the hierarchical group
            mBricksInTheGroup = mGroup.getAllLeafItems();

            // get the connectable brick among the selection, and also the selected item (in case the selected item is a single group without connections points)
            LayerBrick.Brick selectedBrick = layer.getConnectableBrick();
            LayerBrick.Brick brickToConnectInAddedGroup = null;

            // check if we can attach the group to the unique selected object
            if ((selectedBrick != null) && selectedBrick.HasConnectionPoint)
            {
                // find the brick of the group that will be connected to the selected brick
                brickToConnectInAddedGroup = findBrickToConnectAndSetBestConnectionPointIndex(selectedItem, ref wantedConnexion);
            }

            // check if the brick to connect is valid and has connection point
            if ((brickToConnectInAddedGroup != null) && brickToConnectInAddedGroup.HasConnectionPoint)
            {
                // after setting the active connection point index from which this brick will be attached,
                // get the prefered index from the library
                mNextPreferedActiveConnectionIndex = BrickLibrary.Instance.getConnectionNextPreferedIndex(partNumber, wantedConnexion);

                // Compute the orientation of the bricks
                float newOrientation = AddConnectBrick.sGetOrientationOfConnectedBrick(selectedBrick, brickToConnectInAddedGroup);
                newOrientation -= brickToConnectInAddedGroup.Orientation;
                // Rotate all the bricks of the group first before translating
                RotateBrickOnPivotBrick rotateBricksAction = new RotateBrickOnPivotBrick(layer, mBricksInTheGroup, newOrientation, brickToConnectInAddedGroup);
                rotateBricksAction.MustUpdateBrickConnectivity = false;
                rotateBricksAction.redo();

                // compute the translation to add to all the bricks
                PointF translation = new PointF(selectedBrick.ActiveConnectionPosition.X - brickToConnectInAddedGroup.ActiveConnectionPosition.X,
                                                selectedBrick.ActiveConnectionPosition.Y - brickToConnectInAddedGroup.ActiveConnectionPosition.Y);
                mGroup.translate(translation);
            }
            else
            {
                // and just compute the position to the right of the selected item
                PointF position = selectedItem.Position;
                position.X     += selectedItem.DisplayArea.Width;
                mGroup.Position = position;

                // the reassing the selected brick with the first brick of the group if the selected item is a group
                // so that the brick index can correctly be set
                if (selectedItem.IsAGroup)
                {
                    selectedBrick = (selectedItem as Layer.Group).getAllLeafItems()[0] as LayerBrick.Brick;
                }
                else
                {
                    selectedBrick = selectedItem as LayerBrick.Brick;
                }
            }

            // set the index of the group in the list just after the selected brick
            if (selectedBrick != null)
            {
                mInsertIndex = layer.BrickList.IndexOf(selectedBrick) + 1;
            }
        }