Esempio n. 1
0
        public static SkinnableControls.SkinnableControl GetPlayerControlInstanceFromTagName(String tag)
        {
            var ctype = (SkinnableControls.SkinnableControl.SemanticType)Enum.Parse(typeof(SkinnableControls.SkinnableControl.SemanticType), tag);

            SkinnableControls.SkinnableControl.SemanticTypeMeta info =
                SkinnableControls.SkinnableControl.GetPlayerControlInstanceInfo(ctype);

            SkinnableControls.SkinnableControl c = (SkinnableControls.SkinnableControl)Activator.CreateInstance(info.InstanceType, new object[] { ctype });
            return(c);
        }
        protected override void OnDragDrop(DragEventArgs e)
        {
            base.OnDragDrop(e);

            if (e.Data.GetDataPresent(typeof(SkinnableControls.SkinnableControl.SemanticType)))
            {
                // Drop dalla toolbox

                var dropInfo = ControlDropAllowed(this.PointToClient(new Point(e.X, e.Y)), true);
                if (dropInfo != null)
                {
                    SkinnableControls.SkinnableControl.SemanticType ctype =
                        (SkinnableControls.SkinnableControl.SemanticType)e.Data.GetData(typeof(SkinnableControls.SkinnableControl.SemanticType));

                    SkinnableControls.SkinnableControl.SemanticTypeMeta info =
                        SkinnableControls.SkinnableControl.GetPlayerControlInstanceInfo(ctype);

                    e.Effect = DragDropEffects.Copy;

                    // Istanziamo un nuovo oggetto del tipo draggato, e lo aggiungiamo al playerView
                    SkinnableControls.SkinnableControl c = (SkinnableControls.SkinnableControl)Activator.CreateInstance(info.InstanceType, new object[] { ctype });

                    // Inizializiamo qualche proprietà per i controlli standard
                    if (c is SkinnableControls.Button)
                    {
                        ((SkinnableControls.Button)c).Text = info.Title;
                    }
                    else if (c is SkinnableControls.ToggleButton)
                    {
                        ((SkinnableControls.ToggleButton)c).Text        = info.Title;
                        ((SkinnableControls.ToggleButton)c).CheckedText = info.Title;
                    }
                    else if (c is SkinnableControls.Label)
                    {
                        ((SkinnableControls.Label)c).Text = info.Title;
                    }

                    dropInfo.Item3.Controls.AddFirst(c);
                    var location = this.PointToClient(new Point(e.X, e.Y));
                    // Non posizioniamo sotto al mouse le coordinate (0,0) del controllo: lo spostiamo un pochino in alto e a sinistra.
                    const float mouseOffsetX = 7, mouseOffsetY = 7;
                    c.Location = new PointF(Math.Max(0, location.X - dropInfo.Item1 - mouseOffsetX), Math.Max(0, location.Y - dropInfo.Item2 - mouseOffsetY));

                    if (DesignerControlsTreeChanged != null)
                    {
                        DesignerControlsTreeChanged(this, new EventArgs());
                    }
                    this.Select(c);

                    this.Focus();
                }
            }
            else if (e.Data.GetDataPresent(typeof(SkinnableControl).FullName))
            {
                // Drop di un controllo
                var c = (SkinnableControl)e.Data.GetData(typeof(SkinnableControl).FullName);

                if (c.Parent != null)
                {
                    // Qualcuno lo ha già riaggiunto alla View prima di noi (probabilmente OnQueryContinueDrag che
                    // credeva che il dragging fosse stato annullato).
                    // Togliamolo così più sotto lo reinseriamo al posto giusto.
                    c.Parent = null;
                    this.Select(null);
                }

                var dropInfo = ControlDropAllowed(this.PointToClient(new Point(e.X, e.Y)), false);
                if (dropInfo != null)
                {
                    var newParent = dropInfo.Item3;
                    if (newParent != c)
                    {
                        newParent.Controls.AddFirst(c);
                    }
                    var location = this.PointToClient(new Point(e.X, e.Y));
                    c.Location           = new PointF(location.X - this.draggingOffset.X - dropInfo.Item1, location.Y - this.draggingOffset.Y - dropInfo.Item2);
                    this.draggingControl = null;
                    if (DesignerControlsTreeChanged != null)
                    {
                        DesignerControlsTreeChanged(this, new EventArgs());
                    }
                    this.Select(c);
                    if (SelectedObjectPropertyChanged != null)
                    {
                        SelectedObjectPropertyChanged(this, new EventArgs());
                    }
                }
            }
        }