Esempio n. 1
0
        static Transform2d Zoom(Transform2d transform, bool zoomIn, bool affectX, bool affectY, Vector2 fixPosition)
        {
            // adjust the offset such that the window stays centered on the same graph position
            var fixPositionBase = transform.Revert(fixPosition);

            var targetTransform = new Transform2d(transform);

            if (affectX)
            {
                targetTransform.scale.x *= zoomIn ? 3f / 2 : 2f / 3;
            }
            if (affectY)
            {
                targetTransform.scale.y *= zoomIn ? 3f / 2 : 2f / 3;
            }

            var newfixPosition = targetTransform.Apply(fixPositionBase);

            targetTransform.translation += fixPosition - newfixPosition;
            return(targetTransform);
        }
Esempio n. 2
0
 public Rect GetViewRect(Rect source)
 {
     return(transform.Revert(source));
 }
Esempio n. 3
0
        public void HandleEvent(Event ev)
        {
            switch (ev.rawType)
            {
            case EventType.mouseUp:
                draggedEntity       = null;
                selectionRectActive = false;
                parent.RepaintView();
                break;
            }

            switch (ev.type)
            {
            case EventType.mouseDown:

                // clicked on minimap -> focos camera on click pos
                if (Settings.Instance.showMinimap && minimapRect.Contains(ev.mousePosition))
                {
                    SetCenter(minimapTransform.Revert(ev.mousePosition));
                    break;
                }

                var clickEntity = GetEntityAtPosition(ev.mousePosition);
                if (clickEntity != null)
                {
                    foreach (var source in dragEdgeSource)
                    {
                        parent.GetBackend().CreateRelation(source, clickEntity);
                    }
                    dragEdgeSource = new HashSet <T>();

                    if (ev.button == 0)                                 // left click
                    {
                        draggedEntity = clickEntity;

                        // update selection
                        bool controlHeld = (ev.modifiers & EventModifiers.Control) != 0;
                        if (controlHeld)
                        {
                            if (entitySelection.Contains(clickEntity))
                            {
                                entitySelection.Remove(clickEntity);
                            }
                            else
                            {
                                entitySelection.Add(clickEntity);
                            }
                        }
                        else
                        {
                            entitySelection = new HashSet <T>(new[] { clickEntity });
                        }

                        OnEntitySelectionChange();
                        parent.RepaintView();
                    }
                }
                else                         // clickEntity == null
                {
                    var clickEdge = GetEdgeAtPosition(ev.mousePosition);
                    if (clickEdge == null)                               // clickEdge == null && clickEntity == null
                    {
                        if (ev.button != 1)                              // not a right click
                        {
                            bool controlHeld = (ev.modifiers & EventModifiers.Control) != 0;
                            if (!controlHeld)
                            {
                                entitySelection = new HashSet <T>();
                            }

                            // stop edge creation
                            dragEdgeSource = new HashSet <T>();

                            // start rect selection
                            selectionRectActive = true;
                            selectionRectOrigin = ev.mousePosition;

                            parent.RepaintView();
                        }
                    }
                }
                break;

            case EventType.ContextClick:
                var ccEntity = GetEntityAtPosition(ev.mousePosition);
                if (ccEntity != null)
                {
                    if (!entitySelection.Contains(ccEntity))
                    {
                        entitySelection = new HashSet <T>()
                        {
                            ccEntity
                        }
                    }
                    ;

                    HandleEntityContextClick(entitySelection);
                    Event.current.Use();
                    break;
                }

                var ccEdge = GetEdgeAtPosition(ev.mousePosition);
                if (ccEdge != null)
                {
                    HandleRelationContextClick(ccEdge);
                    Event.current.Use();
                    break;
                }
                break;

            case EventType.MouseMove:

                // update hover item and repaint if it changed
                var  newHoverEntity   = GetEntityAtPosition(ev.mousePosition);
                var  newHoverRelation = (newHoverEntity != null) ? null : GetEdgeAtPosition(ev.mousePosition);
                bool hoverChange      = newHoverEntity != hoverEntity || newHoverRelation != hoverRelation;
                hoverEntity   = newHoverEntity;
                hoverRelation = newHoverRelation;

                bool doRepaint = hoverChange;                           // movement over the hover-item requires a repainted tooltip

                // also repaint if we are dragging an edge
                doRepaint |= dragEdgeSource.Any();

                if (doRepaint)
                {
                    parent.RepaintView();
                }
                break;

            case EventType.MouseUp:
                draggedEntity       = null;
                selectionRectActive = false;
                parent.RepaintView();
                break;

            case EventType.MouseDrag:

                if (draggedEntity != null)
                {
                    parent.MoveEntity(draggedEntity, transform.RevertScale(ev.delta));
                }
                else if (selectionRectActive)                           // update selection
                {
                    // GetBounds ensures that xmin < xmax and ymin < ymax
                    var selectionRect       = Util.GetBounds(new[] { ev.mousePosition, selectionRectOrigin });
                    var touchedEntityBounds = entityDrawerBounds.Where(pair => selectionRect.Intersects(pair.Value));
                    entitySelection = new HashSet <T>(touchedEntityBounds.Select(pair => pair.Key));
                    OnEntitySelectionChange();
                }
                else
                {
                    Shift(ev.delta);
                }

                parent.RepaintView();
                break;

            case EventType.ScrollWheel:
                bool xZoom  = (ev.modifiers & EventModifiers.Control) == 0;
                bool yZoom  = (ev.modifiers & EventModifiers.Shift) == 0;
                bool zoomIn = Settings.Instance.invertZoom ? ev.delta.y > 0 : ev.delta.y < 0;

                var targetTransform = Zoom(transform, zoomIn, xZoom, yZoom, ev.mousePosition);
                Tweener.gen.Add(new Tween <Transform2d>(t => transform = t, 0.1f, TweenUtil.Transform2(transform, targetTransform, TwoValueEasing.Linear)));                               //.MoveTransform2dTo(transform, t=>Zoom(t, zoomIn, xZoom, yZoom, ev.mousePosition), 0.1f, true);

                ev.Use();
                parent.RepaintView();
                break;
            }
        }