<commit_before>﻿// Copyright (c) <NAME>. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.MatrixExtensions;
using Avalonia.Media;
using Avalonia.VisualTree;
using Draw2D.ViewModels;

namespace Draw2D.Editor
{
    public class ZoomState : AvaloniaObject
    {
        private double _zoomSpeed = 1.2;
        private double _zoomX = 1.0;
        private double _zoomY = 1.0;
        private double _offsetX = 0.0;
        private double _offsetY = 0.0;

        public static readonly DirectProperty<ZoomState, double> ZoomSpeedProperty =
           AvaloniaProperty.RegisterDirect<ZoomState, double>(nameof(ZoomSpeed), o => o.ZoomSpeed, (o, v) => o.ZoomSpeed = v);

        public static readonly DirectProperty<ZoomState, double> ZoomXProperty =
           AvaloniaProperty.RegisterDirect<ZoomState, double>(nameof(ZoomX), o => o.ZoomX, (o, v) => o.ZoomX = v);

        public static readonly DirectProperty<ZoomState, double> ZoomYProperty =
           AvaloniaProperty.RegisterDirect<ZoomState, double>(nameof(ZoomY), o => o.ZoomY, (o, v) => o.ZoomY = v);

        public static readonly DirectProperty<ZoomState, double> OffsetXProperty =
           AvaloniaProperty.RegisterDirect<ZoomState, double>(nameof(OffsetX), o => o.OffsetX, (o, v) => o.OffsetX = v);

        public static readonly DirectProperty<ZoomState, double> OffsetYProperty =
           AvaloniaProperty.RegisterDirect<ZoomState, double>(nameof(OffsetY), o => o.OffsetY, (o, v) => o.OffsetY = v);

        public double ZoomSpeed
        {
            get { return _zoomSpeed; }
            set { SetAndRaise(ZoomSpeedProperty, ref _zoomSpeed, value); }
        }

        public double ZoomX
        {
            get { return _zoomX; }
            set { SetAndRaise(ZoomXProperty, ref _zoomX, value); }
        }

        public double ZoomY
        {
            get { return _zoomY; }
            set { SetAndRaise(ZoomYProperty, ref _zoomY, value); }
        }

        public double OffsetX
        {
            get { return _offsetX; }
            set { SetAndRaise(OffsetXProperty, ref _offsetX, value); }
        }

        public double OffsetY
        {
            get { return _offsetY; }
            set { SetAndRaise(OffsetYProperty, ref _offsetY, value); }
        }

        public bool IsPanning { get; set; }

        public Action Capture { get; set; }

        public Action Release { get; set; }

        public Func<bool> IsCaptured { get; set; }

        public Action Redraw { get; set; }

        private Matrix CurrentMatrix { get; set; }

        private Point PanPosition { get; set; }

        public void Wheel(double delta, double x, double y)
        {
            ZoomDeltaTo(delta, x, y);
            Invalidate(true);
        }

        public void Pressed(double x, double y)
        {
            if (IsCaptured() == false && IsPanning == false)
            {
                IsPanning = true;
                Capture();
                StartPan(x, y);
                Invalidate(true);
            }
        }

        public void Released(double x, double y)
        {
            if (IsPanning == true)
            {
                Release();
                Invalidate(true);
                IsPanning = false;
            }
        }

        public void Moved(double x, double y)
        {
            if (IsPanning == true)
            {
                PanTo(x, y);
                Invalidate(true);
            }
        }

        public void Invalidate(bool redraw)
        {
            ZoomX = CurrentMatrix.M11;
            ZoomY = CurrentMatrix.M22;
            OffsetX = CurrentMatrix.M31;
            OffsetY = CurrentMatrix.M32;
            if (redraw)
            {
                Redraw();
            }
        }

        public void ZoomTo(double zoom, double x, double y)
        {
            CurrentMatrix = MatrixHelper.ScaleAtPrepend(CurrentMatrix, zoom, zoom, x, y);
        }

        public void ZoomDeltaTo(double delta, double x, double y)
        {
            ZoomTo(delta > 0 ? ZoomSpeed : 1 / ZoomSpeed, x, y);
        }

        public void StartPan(double x, double y)
        {
            PanPosition = new Point(x, y);
        }

        public void PanTo(double x, double y)
        {
            double dx = x - PanPosition.X;
            double dy = y - PanPosition.Y;
            Point delta = new Point(dx, dy);
            PanPosition = new Point(x, y);
            CurrentMatrix = MatrixHelper.TranslatePrepend(CurrentMatrix, delta.X, delta.Y);
        }

        public void Reset()
        {
            CurrentMatrix = Matrix.Identity;
        }

        public void Center(double panelWidth, double panelHeight, double elementWidth, double elementHeight)
        {
            double ox = (panelWidth - elementWidth) / 2;
            double oy = (panelHeight - elementHeight) / 2;
            CurrentMatrix = new Matrix(1.0, 0.0, 0.0, 1.0, ox, oy);
        }

        public void Fill(double panelWidth, double panelHeight, double elementWidth, double elementHeight)
        {
            double zx = panelWidth / elementWidth;<commit_msg>Update ZoomState.cs<commit_after>﻿// Copyright (c) <NAME>. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.MatrixExtensions;
using Avalonia.Media;
using Avalonia.VisualTree;
using Draw2D.ViewModels;

namespace Draw2D.Editor
{
    public class ZoomState : AvaloniaObject
    {
        private double _zoomSpeed = 1.2;
        private double _zoomX = 1.0;
        private double _zoomY = 1.0;
        private double _offsetX = 0.0;
        private double _offsetY = 0.0;

        public static readonly DirectProperty<ZoomState, double> ZoomSpeedProperty =
           AvaloniaProperty.RegisterDirect<ZoomState, double>(nameof(ZoomSpeed), o => o.ZoomSpeed, (o, v) => o.ZoomSpeed = v);

        public static readonly DirectProperty<ZoomState, double> ZoomXProperty =
           AvaloniaProperty.RegisterDirect<ZoomState, double>(nameof(ZoomX), o => o.ZoomX, (o, v) => o.ZoomX = v);

        public static readonly DirectProperty<ZoomState, double> ZoomYProperty =
           AvaloniaProperty.RegisterDirect<ZoomState, double>(nameof(ZoomY), o => o.ZoomY, (o, v) => o.ZoomY = v);

        public static readonly DirectProperty<ZoomState, double> OffsetXProperty =
           AvaloniaProperty.RegisterDirect<ZoomState, double>(nameof(OffsetX), o => o.OffsetX, (o, v) => o.OffsetX = v);

        public static readonly DirectProperty<ZoomState, double> OffsetYProperty =
           AvaloniaProperty.RegisterDirect<ZoomState, double>(nameof(OffsetY), o => o.OffsetY, (o, v) => o.OffsetY = v);

        public double ZoomSpeed
        {
            get { return _zoomSpeed; }
            set { SetAndRaise(ZoomSpeedProperty, ref _zoomSpeed, value); }
        }

        public double ZoomX
        {
            get { return _zoomX; }
            set { SetAndRaise(ZoomXProperty, ref _zoomX, value); }
        }

        public double ZoomY
        {
            get { return _zoomY; }
            set { SetAndRaise(ZoomYProperty, ref _zoomY, value); }
        }

        public double OffsetX
        {
            get { return _offsetX; }
            set { SetAndRaise(OffsetXProperty, ref _offsetX, value); }
        }

        public double OffsetY
        {
            get { return _offsetY; }
            set { SetAndRaise(OffsetYProperty, ref _offsetY, value); }
        }

        public bool IsPanning { get; set; }

        public Action Capture { get; set; }

        public Action Release { get; set; }

        public Func<bool> IsCaptured { get; set; }

        public Action Redraw { get; set; }

        private Matrix CurrentMatrix { get; set; }

        private Point PanPosition { get; set; }

        public void Wheel(double delta, double x, double y)
        {
            ZoomDeltaTo(delta, x, y);
            Invalidate(true);
        }

        public void Pressed(double x, double y)
        {
            if (IsCaptured() == false && IsPanning == false)
            {
                IsPanning = true;
                Capture();
                StartPan(x, y);
                Invalidate(true);
            }
        }

        public void Released(double x, double y)
        {
            if (IsPanning == true)
            {
                Release();
                Invalidate(true);
                IsPanning = false;
            }
        }

        public void Moved(double x, double y)
        {
            if (IsPanning == true)
            {
                PanTo(x, y);
                Invalidate(true);
            }
        }

        public void Invalidate(bool redraw)
        {
            ZoomX = CurrentMatrix.M11;
            ZoomY = CurrentMatrix.M22;
            OffsetX = CurrentMatrix.M31;
            OffsetY = CurrentMatrix.M32;
            if (redraw)
            {
                Redraw();
            }
        }

        public void ZoomTo(double zoom, double x, double y)
        {
            CurrentMatrix = MatrixHelper.ScaleAtPrepend(CurrentMatrix, zoom, zoom, x, y);
        }

        public void ZoomDeltaTo(double delta, double x, double y)
        {
            ZoomTo(delta > 0 ? ZoomSpeed : 1 / ZoomSpeed, x, y);
        }

        public void StartPan(double x, double y)
        {
            PanPosition = new Point(x, y);
        }

        public void PanTo(double x, double y)
        {
            double dx = x - PanPosition.X;
            double dy = y - PanPosition.Y;
            Point delta = new Point(dx, dy);
            PanPosition = new Point(x, y);
            CurrentMatrix = MatrixHelper.TranslatePrepend(CurrentMatrix, delta.X, delta.Y);
        }

        public void Reset()
        {
            CurrentMatrix = new Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
        }

        public void Center(double panelWidth, double panelHeight, double elementWidth, double elementHeight)
        {
            double ox = (panelWidth - elementWidth) / 2;
            double oy = (panelHeight - elementHeight) / 2;
            CurrentMatrix = new Matrix(1.0, 0.0, 0.0, 1.0, ox, oy);
        }

        public void Fill(double panelWidth, double panelHeight, double elementWidth, double elementHeight)
        {
            double zx = panelWidth / elementWidth;
{'fraction_non_alphanumeric': 0.09336865008040866, 'fraction_numerical': 0.007189480654621133, 'mean_word_length': 1.7968253968253969, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4203567', 'n_tokens_mistral': 3054, 'n_tokens_neox': 2913, 'n_words': 820}