Jump to content

Clipboard access from plugins


MJW

Recommended Posts

I'm updating a plugin that uses the clipboard, and I believe I read fairly recently that there were new PDN routines for accessing it. However I did several forum searches, and couldn't find anything. Could someone either give me a link, or better yet, provide a description of the new methods.

Link to comment
Share on other sites

Just let CodeLab write the code for you.

 

CodeLab: File > New, choose the Clipboard Effect choose your canvas. Generate code to see what you get.

 

image.png

 

// Name:
// Submenu:
// Author:
// Title:
// Version:
// Desc:
// Keywords:
// URL:
// Help:
#region UICode
#endregion

// Working surface
Surface wrk = null;

private Surface clipboardSurface = null;
private bool readClipboard = false;

protected override void OnDispose(bool disposing)
{
    if (disposing)
    {
        // Release any surfaces or effects you've created
        wrk?.Dispose(); wrk = null;
        clipboardSurface?.Dispose(); clipboardSurface = null;
    }

    base.OnDispose(disposing);
}

// This single-threaded function is called after the UI changes and before the Render function is called
// The purpose is to prepare anything you'll need in the Render function
void PreRender(Surface dst, Surface src)
{
    if (wrk == null)
    {
        wrk = new Surface(src.Size);
    }

    if (!readClipboard)
    {
        readClipboard = true;
        clipboardSurface = Services.GetService<IClipboardService>().TryGetSurface();
    }
    // Copy from the Clipboard to the wrk surface
    for (int y = 0; y < wrk.Size.Height; y++)
    {
        if (IsCancelRequested) return;
        for (int x = 0; x < wrk.Size.Width; x++)
        {
            if (clipboardSurface != null)
            {
                //wrk[x,y] = clipboardSurface.GetBilinearSample(x, y);
                //wrk[x,y] = clipboardSurface.GetBilinearSampleClamped(x, y);
                wrk[x,y] = clipboardSurface.GetBilinearSampleWrapped(x, y);
            }
            else
            {
                wrk[x,y] = Color.Transparent;
            }
        }
    }
}

// Here is the main multi-threaded render function
// The dst canvas is broken up into rectangles and
// your job is to write to each pixel of that rectangle
void Render(Surface dst, Surface src, Rectangle rect)
{

    // Step through each row of the current rectangle
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        // Step through each pixel on the current row of the rectangle
        for (int x = rect.Left; x < rect.Right; x++)
        {
            ColorBgra SrcPixel = src[x,y];
            ColorBgra WrkPixel = wrk[x,y];
            ColorBgra DstPixel = dst[x,y];

            ColorBgra CurrentPixel = WrkPixel;

            // TODO: Add additional pixel processing code here


            dst[x,y] = CurrentPixel;
        }
    }
}

 

  • Like 1
  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...