/* Copyright (c) 2023 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "imageio/ImageIO.h" #include "shell/Shell.h" #include "uitoolkit/UIToolkitCursor.h" #include "stem_core.h" #include #include static void (* cursorChangeCallback)(void); static unsigned int mouseDeltaStack; static ShellCursorID loadCursor(const char * data1x, unsigned int dataSize1x, const char * data2x, unsigned int dataSize2x, unsigned int hotspotX, unsigned int hotspotY) { BitmapImage * image1x = ImageIO_readImageData(data1x, dataSize1x, BITMAP_PIXEL_FORMAT_RGBA_8888, false); BitmapImage * image2x = ImageIO_readImageData(data2x, dataSize2x, BITMAP_PIXEL_FORMAT_RGBA_8888, false); struct ShellCustomCursorImage representations[2] = { {image1x->width, image1x->height, image1x->pixels, 1.0f}, {image2x->width, image2x->height, image2x->pixels, 2.0f} }; ShellCursorID cursorID = Shell_registerCustomCursor(2, representations, hotspotX, hotspotY); BitmapImage_dispose(image1x); BitmapImage_dispose(image2x); return cursorID; } #define EMBED_EXTERN(symbol_name) \ extern const char EMBEDDATA_##symbol_name##_1x_png[], EMBEDDATA_##symbol_name##_2x_png[]; \ extern unsigned int EMBEDSIZE_##symbol_name##_1x_png, EMBEDSIZE_##symbol_name##_2x_png; #define CURSOR_DATA(name) EMBEDDATA_##name##_1x_png, EMBEDSIZE_##name##_1x_png, EMBEDDATA_##name##_2x_png, EMBEDSIZE_##name##_2x_png #define loadEmbeddedCursor(name, index, hotspot_x, hotspot_y) { \ EMBED_EXTERN(name) \ builtInCursors[index] = loadCursor(CURSOR_DATA(name), hotspot_x, hotspot_y); \ } ShellCursorID UIToolkit_getBuiltInCursor(UIToolkit_builtInCursor cursor) { static ShellCursorID builtInCursors[8]; if ((unsigned int) cursor < sizeof_count(builtInCursors)) { if (builtInCursors[0] == 0) { loadEmbeddedCursor(resize_topleft, 0, 12, 12); loadEmbeddedCursor(resize_top, 1, 12, 12); loadEmbeddedCursor(resize_topright, 2, 12, 12); loadEmbeddedCursor(resize_left, 3, 12, 12); loadEmbeddedCursor(resize_right, 4, 12, 12); loadEmbeddedCursor(resize_bottomleft, 5, 12, 12); loadEmbeddedCursor(resize_bottom, 6, 12, 12); loadEmbeddedCursor(resize_bottomright, 7, 12, 12); } return builtInCursors[cursor]; } return ShellCursor_arrow; } void UIToolkit_cursorChangeFunc(void (* callback)(void)) { cursorChangeCallback = callback; } void UIToolkit_elementCursorChanged(void) { if (cursorChangeCallback != NULL) { cursorChangeCallback(); } } void UIToolkit_pushMouseDeltaMode(void) { if (mouseDeltaStack == 0) { Shell_setMouseDeltaMode(true); } mouseDeltaStack++; } void UIToolkit_popMouseDeltaMode(void) { assert(mouseDeltaStack > 0); mouseDeltaStack--; if (mouseDeltaStack == 0) { Shell_setMouseDeltaMode(false); } }