Ejemplo n.º 1
0
 static CursorTexture loadStatic(this IRenderDevice renderDevice, string resource, int idealSize)
 {
     using (var stm = openResource(resource))
         using (var unzip = new GZipStream(stm, CompressionMode.Decompress))
             using (var file = new CursorFile(unzip))
             {
                 int index = findBestCursor(file, idealSize);
                 return(file.load(renderDevice, index));
             }
 }
Ejemplo n.º 2
0
        protected override void OnStart(AppHost host)
        {
            var sampleButton = new LayoutFarm.CustomWidgets.Box(30, 30);

            host.AddChild(sampleButton);

            sampleButton.MouseDown += ((s, e2) =>
            {
                //click to create custom cursor
                if (!_showCustomCursor)
                {
                    if (_myCursor == null)
                    {
                        using (MemBitmap temp = new MemBitmap(16, 16))
                            using (Tools.BorrowAggPainter(temp, out AggPainter p))
                            {
                                //1. create a simple bitmap for our cursor
                                p.Clear(Color.FromArgb(0, Color.White));
                                p.FillRect(1, 1, 10, 10, Color.FromArgb(150, Color.Green));
                                p.FillRect(3, 3, 4, 4, Color.Yellow);
                                //-------
                                //create cursor file

                                CursorFile curFile = new CursorFile();

                                var iconBmp = new WindowBitmap(temp.Width, temp.Height);
                                iconBmp.RawBitmapData = MemBitmap.CopyImgBuffer(temp);

                                curFile.AddBitmap(iconBmp, 1, 1); //add bitmap with hotspot
                                curFile.Save("myicon.cur");       //save to temp file
                                _myCursor = UIPlatform.CreateCursor(new CursorRequest("myicon.cur", 16));
                            }
                    }
                    e2.CustomMouseCursor = _myCursor;
                    _showCustomCursor = true;
                }
                else
                {
                    _showCustomCursor = false;
                    e2.MouseCursorStyle = MouseCursorStyle.Default;
                }
            });

            sampleButton.MouseMove += ((s, e2) =>
            {
                if (_showCustomCursor && _myCursor != null)
                {
                    e2.CustomMouseCursor = _myCursor;
                }
            });
        }
Ejemplo n.º 3
0
        public unsafe Cursor(FileSystemEntry entry, float windowScale)
        {
            var cursorFile = CursorFile.FromFileSystemEntry(entry);

            _animationFrames = cursorFile.AnimationFrames;

            _surfaces = new Sdl2Interop.SDL_Surface[cursorFile.Images.Length];
            _cursors  = new Sdl2Interop.SDL_Cursor[cursorFile.Images.Length];

            for (var i = 0; i < cursorFile.Images.Length; i++)
            {
                var image = cursorFile.Images[i];

                var width  = (int)image.Width;
                var height = (int)image.Height;

                Sdl2Interop.SDL_Surface surface;
                if (windowScale == 1.0f)
                {
                    fixed(byte *pixelsPtr = image.PixelsBgra)
                    {
                        surface = Sdl2Interop.SDL_CreateRGBSurfaceWithFormatFrom(
                            pixelsPtr,
                            width,
                            height,
                            32,
                            width * 4,
                            Sdl2Interop.SDL_PixelFormat.SDL_PIXELFORMAT_ABGR8888);
                    }
                }
                else
                {
                    var scaledWidth  = (int)(windowScale * width);
                    var scaledHeight = (int)(windowScale * height);

                    var scaledImage = Image.LoadPixelData <Bgra32>(image.PixelsBgra, width, height);
                    scaledImage.Mutate(x => x.Resize(scaledWidth, scaledHeight));

                    if (!scaledImage.TryGetSinglePixelSpan(out Span <Bgra32> pixelSpan))
                    {
                        throw new InvalidOperationException("Unable to get image pixelspan.");
                    }

                    fixed(void *pin = &MemoryMarshal.GetReference(pixelSpan))
                    {
                        surface = Sdl2Interop.SDL_CreateRGBSurfaceWithFormatFrom(
                            (byte *)pin,
                            scaledWidth,
                            scaledHeight,
                            32,
                            scaledWidth * 4,
                            Sdl2Interop.SDL_PixelFormat.SDL_PIXELFORMAT_ABGR8888);
                    }
                }

                AddDisposeAction(() => Sdl2Interop.SDL_FreeSurface(surface));

                _surfaces[i] = surface;

                var cursor = Sdl2Interop.SDL_CreateColorCursor(
                    _surfaces[i],
                    (int)(image.HotspotX * windowScale),
                    (int)(image.HotspotY * windowScale));

                AddDisposeAction(() => Sdl2Interop.SDL_FreeCursor(cursor));

                _cursors[i] = cursor;
            }
        }
Ejemplo n.º 4
0
 static int findBestCursor(CursorFile file, int size)
 {
     return(file.images.minIndex(ii => Math.Abs(ii.size.cx - size)));
 }
Ejemplo n.º 5
0
        public unsafe Cursor(FileSystemEntry entry, GameWindow window)
        {
            var cursorFile = CursorFile.FromFileSystemEntry(entry);

            _animationFrames = cursorFile.AnimationFrames;

            _surfaces = new Sdl2Interop.SDL_Surface[cursorFile.Images.Length];
            _cursors  = new Sdl2Interop.SDL_Cursor[cursorFile.Images.Length];

            var windowScale = window.WindowScale;

            for (var i = 0; i < cursorFile.Images.Length; i++)
            {
                var image = cursorFile.Images[i];

                var width  = (int)image.Width;
                var height = (int)image.Height;

                fixed(byte *pixelsPtr = image.PixelsBgra)
                {
                    var surface = Sdl2Interop.SDL_CreateRGBSurfaceWithFormatFrom(
                        pixelsPtr,
                        width,
                        height,
                        32,
                        width * 4,
                        Sdl2Interop.SDL_PixelFormat.SDL_PIXELFORMAT_ABGR8888);

                    if (windowScale != 1.0f)
                    {
                        var scaledWidth  = (int)(windowScale * width);
                        var scaledHeight = (int)(windowScale * height);

                        var scaledSurface = Sdl2Interop.SDL_CreateRGBSurfaceWithFormat(
                            0,
                            scaledWidth,
                            scaledHeight,
                            32,
                            Sdl2Interop.SDL_PixelFormat.SDL_PIXELFORMAT_ABGR8888);

                        Sdl2Interop.SDL_BlitScaled(
                            surface,
                            new Sdl2Interop.SDL_Rect(0, 0, width, height),
                            scaledSurface,
                            new Sdl2Interop.SDL_Rect(0, 0, scaledWidth, scaledHeight));

                        Sdl2Interop.SDL_FreeSurface(surface);

                        surface = scaledSurface;
                    }

                    AddDisposeAction(() => Sdl2Interop.SDL_FreeSurface(surface));

                    _surfaces[i] = surface;
                }

                var cursor = Sdl2Interop.SDL_CreateColorCursor(
                    _surfaces[i],
                    (int)(image.HotspotX * windowScale),
                    (int)(image.HotspotY * windowScale));

                AddDisposeAction(() => Sdl2Interop.SDL_FreeCursor(cursor));

                _cursors[i] = cursor;
            }
        }