Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

This simple loopdeck plugin fills each LED with a 64x64 fragment of the screen, starting…

  1. User

    This simple loopdeck plugin fills each LED with a 64x64 fragment of the screen, starting from the top-left of the cursor. Pressing any button right now just starts or stops it. Update this so pressing the bottom-left button zooms out, and pressing the bottom-right button zooms in.

    /// DisplayController.cs ///
    namespace Loupedeck.LoupeUtilPlugin
    {
        using System;
        using System.Collections.Generic;
        using System.Drawing;
        using System.Linq;
        using System.Threading;
        using System.Windows.Forms;
    
        public class DisplayController : PluginDynamicFolder
        {
            private readonly TotalCanvas canvas = new TotalCanvas();
            private volatile Boolean threadRunning = false;
            private Thread thread;
    
            public DisplayController()
            {
                this.canvas.UpdateGraphics(graphics => graphics.CopyFromScreen(new Point(Control.MousePosition.X, Control.MousePosition.Y), new Point(0,0), new Size(TotalCanvas.WIDTH, TotalCanvas.HEIGHT)));
                this.thread = new Thread(this.UpdateThread);
                this.DisplayName = "Magnifier";
                this.GroupName = "System";
            }
    
            public override PluginDynamicFolderNavigation GetNavigationArea(DeviceType deviceType) => PluginDynamicFolderNavigation.ButtonArea;
            
            public override IEnumerable<String> GetButtonPressActionNames(DeviceType deviceType)
            {
                return new[]
                {
                    PluginDynamicFolder.NavigateUpActionName,
                    //this.CreateCommandName("Magnify (0,0)"),
                    this.CreateCommandName("Magnify (1,0)"),
                    this.CreateCommandName("Magnify (2,0)"),
                    this.CreateCommandName("Magnify (3,0)"),
                    this.CreateCommandName("Magnify (4,0)"),
                    this.CreateCommandName("Magnify (0,1)"),
                    this.CreateCommandName("Magnify (1,1)"),
                    this.CreateCommandName("Magnify (2,1)"),
                    this.CreateCommandName("Magnify (3,1)"),
                    this.CreateCommandName("Magnify (4,1)"),
                    this.CreateCommandName("Magnify (0,2)"),
                    this.CreateCommandName("Magnify (1,2)"),
                    this.CreateCommandName("Magnify (2,2)"),
                    this.CreateCommandName("Magnify (3,2)"),
                    this.CreateCommandName("Magnify (4,2)"),
                };
            }
    
            public override BitmapImage GetCommandImage(String actionParameter, PluginImageSize imageSize)
            {
                var arr = actionParameter.Where(Char.IsDigit).ToArray();
                var x = arr[0] - 48;
                var y = arr[1] - 48;
                BitmapImage image = this.canvas.GetImage()[x + y * 5];
                using (var bitmapBuilder = new BitmapBuilder(imageSize))
                {
                    bitmapBuilder.SetBackgroundImage(image);
                    return bitmapBuilder.ToImage();
                }
            }
    
            public override void RunCommand(String commandParameter)
            {
                if (this.threadRunning)
                {
                    this.threadRunning = false;
                }
                else
                {
                    this.thread = new Thread(this.UpdateThread);
                    this.threadRunning = true;
                    this.thread.Start();
                }
            }
    
            // Used to run an update thread for the screen
            public void UpdateThread()
            {
                while(this.threadRunning)
                {
                    this.canvas.UpdateGraphics(graphics => graphics.CopyFromScreen(new Point(Control.MousePosition.X, Control.MousePosition.Y), new Point(0,0), new Size(TotalCanvas.WIDTH, TotalCanvas.HEIGHT)));
                    // this.CommandImageChanged("Magnify (0,0)");
                    this.CommandImageChanged("Magnify (1,0)");
                    this.CommandImageChanged("Magnify (2,0)");
                    this.CommandImageChanged("Magnify (3,0)");
                    this.CommandImageChanged("Magnify (4,0)");
                    this.CommandImageChanged("Magnify (0,1)");
                    this.CommandImageChanged("Magnify (1,1)");
                    this.CommandImageChanged("Magnify (2,1)");
                    this.CommandImageChanged("Magnify (3,1)");
                    this.CommandImageChanged("Magnify (4,1)");
                    this.CommandImageChanged("Magnify (0,2)");
                    this.CommandImageChanged("Magnify (1,2)");
                    this.CommandImageChanged("Magnify (2,2)");
                    this.CommandImageChanged("Magnify (3,2)");
                    this.CommandImageChanged("Magnify (4,2)");
                    // TODO: this.ActionImageChanged(); // Notify the Loupedeck service that the command display name and/or image has changed.
                    Thread.Sleep(30);
                }
            }
        }
    }
    
    /// ButtonCanvas.cs ///
    namespace Loupedeck.LoupeUtilPlugin
    {
        using System;
        using System.Drawing;
        using System.IO;
    
        public class ButtonCanvas
        {
            public const Int32 WIDTH = 80;
            public const Int32 HEIGHT = 80;
            
            private readonly Bitmap bmp = new Bitmap(WIDTH, HEIGHT);
            private readonly Object lockObject = new Object();
            // private readonly Region clipRegion = new Region(new Rectangle(0, 0, WIDTH, HEIGHT));
    
            // Allows arbitrary modification of the internal graphics
            public void UpdateGraphics(Action<Graphics> action)
            {
                lock (this.lockObject)
                {
                    using (var graphics = Graphics.FromImage(this.bmp))
                    {
                        // graphics.Clip = this.clipRegion;
                        action(graphics);
                    }
                }
            }
    
            // Convenience wrapper for Graphics.FillRectangle with default parameters set to exactly cover the entire image.
            public void FillRectangle(Brush brush, Int32 x = 0, Int32 y = 0, Int32 width = WIDTH, Int32 height = HEIGHT) => this.UpdateGraphics(graphics => graphics.FillRectangle(brush, x, y, width, height));
    
            // Set a specific pixel value
            // x must be in the range of 0-79
            // y must be in the range of 0-79
            public void SetPixel(Int32 x, Int32 y, Color color) 
            {
                lock(this.lockObject)
                {
                    this.bmp.SetPixel(x, y, color);
                }
            }
    
            // Create a renderable image from this canvas
            public BitmapImage CreateImage()
            {
                using (var stream = new MemoryStream())
                {
                    lock (this.lockObject)
                    {
                        this.bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    }
                    return BitmapImage.FromArray(stream.ToArray());
                }
            }
        }
    }
    
    /// TotalCanvas.cs ///
    namespace Loupedeck.LoupeUtilPlugin
    {
        using System;
        using System.Collections.Generic;
        using System.Drawing;
        using System.IO;
    
        public class TotalCanvas
        {
            public const Int32 WIDTH = 80 * 5;
            public const Int32 HEIGHT = 80 * 3;
            
            private readonly Bitmap bmp = new Bitmap(WIDTH, HEIGHT);
            private readonly Object lockObject = new Object();
            private List<BitmapImage> images = new List<BitmapImage>();
            // private readonly Region clipRegion = new Region(new Rectangle(0, 0, WIDTH, HEIGHT));
    
            // Allows arbitrary modification of the internal graphics
            public void UpdateGraphics(Action<Graphics> action)
            {
                lock (this.lockObject)
                {
                    using (var graphics = Graphics.FromImage(this.bmp))
                    {
                        // graphics.Clip = this.clipRegion;
                        action(graphics);
                        var imageList = new List<BitmapImage>();
                        for (var y = 0; y < 3; y++)
                        {
                            for (var x = 0; x < 5; x++)
                            {
                                lock (this.lockObject)
                                {
                                    var sourceData = this.bmp.LockBits
                                    (
                                        new Rectangle(x * 80, y * 80, 80, 80),
                                        System.Drawing.Imaging.ImageLockMode.ReadOnly,
                                        this.bmp.PixelFormat
                                    );
                                    using(var tile = new Bitmap
                                    (
                                        sourceData.Width,
                                        sourceData.Height,
                                        sourceData.Stride,
                                        sourceData.PixelFormat,
                                        sourceData.Scan0))
                                    {
                                        using (var stream = new MemoryStream())
                                        {
                                            tile.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                                            imageList.Add(BitmapImage.FromArray(stream.ToArray()));
                                        }
                                    }
                                    this.bmp.UnlockBits(sourceData);
                                }
                            }
                        }
                        this.images = imageList;
                    }
                }
            }
    
            // Create a renderable image from this canvas
            public List<BitmapImage> GetImage() => this.images;
        }
    }
    
  2. Assistant

    To implement zoom in and zoom out functionality when pressing the bottom-left and bottom-right buttons, we'll add a zoom level variable, update the area being captured from the screen based on this zoom level, and adjust the behavior of RunCommand to change the zoom level when the specific buttons are pressed.

    Here's how you can update your code:

    1. Add a Zoom Level Variable:

    First, add a double variable zoomLevel to your DisplayController class to keep track of the current zoom level.

    private double zoomLevel = 1.0;
    

    2. Initialize the Update Thread in the Constructor:

    Start the update thread when the DisplayController is initialized.

    public DisplayController()
    {
        this.canvas.UpdateGraphics(graphics => graphics.CopyFromScreen(new Point(Control.MousePosition.X, Control.MousePosition.Y), new Point(0,0), new Size(TotalCanvas.WIDTH, TotalCanvas.HEIGHT)));
        this.threadRunning = true;
        this.thread = new Thread(this.UpdateThread);
        this.thread.Start();
        this.DisplayName = "Magnifier";
        this.GroupName = "System";
    }
    

    3. Update the RunCommand Method:

    Modify the RunCommand method to adjust the zoomLevel when the bottom-left ("Magnify (0,2)") or bottom-right ("Magnify (4,2)") button is pressed.

    public override void RunCommand(String commandParameter)
    {
        if (commandParameter == this.CreateCommandName("Magnify (0,2)"))
        {
            // Zoom out
            this.zoomLevel /= 1.1;
            if (this.zoomLevel < 0.5)
            {
                this.zoomLevel = 0.5; // Minimum zoom level
            }
        }
        else if (commandParameter == this.CreateCommandName("Magnify (4,2)"))
        {
            // Zoom in
            this.zoomLevel *= 1.1;
            if (this.zoomLevel > 5.0)
            {
                this.zoomLevel = 5.0; // Maximum zoom level
            }
        }
        // You can handle other buttons here if needed
    }
    

    4. Modify the UpdateThread Method:

    Adjust the area being captured from the screen based on the zoomLevel. We'll capture a source rectangle that's inversely proportional to the zoom level (larger area for zoom out, smaller area for zoom in), ensuring the captured area is centered around the mouse position.

    public void UpdateThread()
    {
        while(this.threadRunning)
        {
            int sourceWidth = (int)(TotalCanvas.WIDTH / this.zoomLevel);
            int sourceHeight = (int)(TotalCanvas.HEIGHT / this.zoomLevel);
            int sourceX = Control.MousePosition.X - sourceWidth / 2;
            int sourceY = Control.MousePosition.Y - sourceHeight / 2;
    
            using (var sourceBmp = new Bitmap(sourceWidth, sourceHeight))
            {
                using (var g = Graphics.FromImage(sourceBmp))
                {
                    g.CopyFromScreen(sourceX, sourceY, 0, 0, new Size(sourceWidth, sourceHeight));
                }
                this.canvas.UpdateGraphics(graphics =>
                {
                    graphics.DrawImage(sourceBmp, new Rectangle(0, 0, TotalCanvas.WIDTH, TotalCanvas.HEIGHT));
                });
            }
    
            // Update images on buttons
            foreach (var cmd in this.GetButtonPressActionNames(this.DeviceType))
            {
                this.CommandImageChanged(cmd);
            }
            Thread.Sleep(30);
        }
    }
    

    5. Ensure All Buttons Are Included:

    Make sure that "Magnify (0,2)" and "Magnify (4,2)" are included in your button action names.

    public override IEnumerable<String> GetButtonPressActionNames(DeviceType deviceType)
    {
        return new[]
        {
            PluginDynamicFolder.NavigateUpActionName,
            this.CreateCommandName("Magnify (0,0)"),
            this.CreateCommandName("Magnify (1,0)"),
            this.CreateCommandName("Magnify (2,0)"),
            this.CreateCommandName("Magnify (3,0)"),
            this.CreateCommandName("Magnify (4,0)"),
            this.CreateCommandName("Magnify (0,1)"),
            this.CreateCommandName("Magnify (1,1)"),
            this.CreateCommandName("Magnify (2,1)"),
            this.CreateCommandName("Magnify (3,1)"),
            this.CreateCommandName("Magnify (4,1)"),
            this.CreateCommandName("Magnify (0,2)"),
            this.CreateCommandName("Magnify (1,2)"),
            this.CreateCommandName("Magnify (2,2)"),
            this.CreateCommandName("Magnify (3,2)"),
            this.CreateCommandName("Magnify (4,2)"),
        };
    }
    

    6. Adjust the GetCommandImage Method:

    The GetCommandImage method extracts the coordinates from the actionParameter. Ensure it correctly parses the coordinates if you have actions like "Magnify (0,0)".

    public override BitmapImage GetCommandImage(String actionParameter, PluginImageSize imageSize)
    {
        var arr = actionParameter.Where(Char.IsDigit).ToArray();
        var x = arr[0] - '0';
        var y = arr[1] - '0';
        BitmapImage image = this.canvas.GetImage()[x + y * 5];
        using (var bitmapBuilder = new BitmapBuilder(imageSize))
        {
            bitmapBuilder.SetBackgroundImage(image);
            return bitmapBuilder.ToImage();
        }
    }
    

    7. Update the TotalCanvas Class:

    Ensure that the TotalCanvas class can handle the updated graphics correctly, especially when you change the size of the images being processed.

    public void UpdateGraphics(Action<Graphics> action)
    {
        lock (this.lockObject)
        {
            using (var graphics = Graphics.FromImage(this.bmp))
            {
                action(graphics);
                var imageList = new List<BitmapImage>();
                for (var y = 0; y < 3; y++)
                {
                    for (var x = 0; x < 5; x++)
                    {
                        var tileRect = new Rectangle(x * 80, y * 80, 80, 80);
                        using (var tile = this.bmp.Clone(tileRect, this.bmp.PixelFormat))
                        {
                            using (var stream = new MemoryStream())
                            {
                                tile.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                                imageList.Add(BitmapImage.FromArray(stream.ToArray()));
                            }
                        }
                    }
                }
                this.images = imageList;
            }
        }
    }
    

    8. Handle Proper Disposal of Resources:

    Ensure that all Bitmap and Graphics objects are properly disposed of to prevent memory leaks.

    Complete Updated DisplayController.cs:

    Here is the updated DisplayController.cs with all the changes:

    namespace Loupedeck.LoupeUtilPlugin
    {
        using System;
        using System.Collections.Generic;
        using System.Drawing;
        using System.Linq;
        using System.Threading;
        using System.Windows.Forms;
    
        public class DisplayController : PluginDynamicFolder
        {
            private readonly TotalCanvas canvas = new TotalCanvas();
            private volatile Boolean threadRunning = true;
            private Thread thread;
            private double zoomLevel = 1.0;
    
            public DisplayController()
            {
                this.canvas.UpdateGraphics(graphics => graphics.CopyFromScreen(new Point(Control.MousePosition.X, Control.MousePosition.Y), new Point(0,0), new Size(TotalCanvas.WIDTH, TotalCanvas.HEIGHT)));
                this.thread = new Thread(this.UpdateThread);
                this.threadRunning = true;
                this.thread.Start();
                this.DisplayName = "Magnifier";
                this.GroupName = "System";
            }
    
            public override PluginDynamicFolderNavigation GetNavigationArea(DeviceType deviceType) => PluginDynamicFolderNavigation.ButtonArea;
            
            public override IEnumerable<String> GetButtonPressActionNames(DeviceType deviceType)
            {
                return new[]
                {
                    PluginDynamicFolder.NavigateUpActionName,
                    this.CreateCommandName("Magnify (0,0)"),
                    this.CreateCommandName("Magnify (1,0)"),
                    this.CreateCommandName("Magnify (2,0)"),
                    this.CreateCommandName("Magnify (3,0)"),
                    this.CreateCommandName("Magnify (4,0)"),
                    this.CreateCommandName("Magnify (0,1)"),
                    this.CreateCommandName("Magnify (1,1)"),
                    this.CreateCommandName("Magnify (2,1)"),
                    this.CreateCommandName("Magnify (3,1)"),
                    this.CreateCommandName("Magnify (4,1)"),
                    this.CreateCommandName("Magnify (0,2)"),
                    this.CreateCommandName("Magnify (1,2)"),
                    this.CreateCommandName("Magnify (2,2)"),
                    this.CreateCommandName("Magnify (3,2)"),
                    this.CreateCommandName("Magnify (4,2)"),
                };
            }
    
            public override BitmapImage GetCommandImage(String actionParameter, PluginImageSize imageSize)
            {
                var arr = actionParameter.Where(Char.IsDigit).ToArray();
                var x = arr[0] - '0';
                var y = arr[1] - '0';
                BitmapImage image = this.canvas.GetImage()[x + y * 5];
                using (var bitmapBuilder = new BitmapBuilder(imageSize))
                {
                    bitmapBuilder.SetBackgroundImage(image);
                    return bitmapBuilder.ToImage();
                }
            }
    
            public override void RunCommand(String commandParameter)
            {
                if (commandParameter == this.CreateCommandName("Magnify (0,2)"))
                {
                    // Zoom out
                    this.zoomLevel /= 1.1;
                    if (this.zoomLevel < 0.5)
                    {
                        this.zoomLevel = 0.5; // Minimum zoom level
                    }
                }
                else if (commandParameter == this.CreateCommandName("Magnify (4,2)"))
                {
                    // Zoom in
                    this.zoomLevel *= 1.1;
                    if (this.zoomLevel > 5.0)
                    {
                        this.zoomLevel = 5.0; // Maximum zoom level
                    }
                }
                // You can handle other buttons here if needed
            }
    
            public void UpdateThread()
            {
                while(this.threadRunning)
                {
                    int sourceWidth = (int)(TotalCanvas.WIDTH / this.zoomLevel);
                    int sourceHeight = (int)(TotalCanvas.HEIGHT / this.zoomLevel);
                    int sourceX = Control.MousePosition.X - sourceWidth / 2;
                    int sourceY = Control.MousePosition.Y - sourceHeight / 2;
    
                    using (var sourceBmp = new Bitmap(sourceWidth, sourceHeight))
                    {
                        using (var g = Graphics.FromImage(sourceBmp))
                        {
                            g.CopyFromScreen(sourceX, sourceY, 0, 0, new Size(sourceWidth, sourceHeight));
                        }
                        this.canvas.UpdateGraphics(graphics =>
                        {
                            graphics.DrawImage(sourceBmp, new Rectangle(0, 0, TotalCanvas.WIDTH, TotalCanvas.HEIGHT));
                        });
                    }
    
                    // Update images on buttons
                    foreach (var cmd in this.GetButtonPressActionNames(this.DeviceType))
                    {
                        this.CommandImageChanged(cmd);
                    }
                    Thread.Sleep(30);
                }
            }
        }
    }
    

    Note: Remember to dispose of any disposable objects like Bitmap and Graphics instances to prevent memory leaks. The using statements in the code help with that.

    Explanation:

    • Zoom Level Adjustment: When the bottom-left or bottom-right button is pressed, the zoomLevel is decreased or increased, respectively, simulating a zoom out or zoom in effect.

    • Dynamic Screen Capture: The UpdateThread method dynamically adjusts the area of the screen being captured based on the current zoomLevel, ensuring that the magnified area is centered around the mouse pointer.

    • Image Updates: After updating the canvas, the images on the buttons are refreshed to reflect the new magnification by calling this.CommandImageChanged(cmd) for each button.

    With these changes, pressing the bottom-left button will zoom out, and pressing the bottom-right button will zoom in, providing the desired functionality in your LoopDeck plugin.