Draft: Added getPixelUnderCursor for MacOS_X #111397

Open
Caleb-Cleavinger wants to merge 5 commits from Caleb-Cleavinger/blender:CC111303 into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
4 changed files with 59 additions and 2 deletions

View File

@ -62,6 +62,8 @@ class GHOST_System : public GHOST_ISystem {
*/
virtual uint64_t getMilliSeconds() const;
/**
* Installs a timer.
*

View File

@ -116,6 +116,13 @@ class GHOST_SystemCocoa : public GHOST_System {
*/
GHOST_IWindow *getWindowUnderCursor(int32_t x, int32_t y);
/**
* Get the color of the pixel at the current mouse cursor location
* \param r_color: returned sRGB float colors
* \return Success value (true == successful and supported by platform)
*/
GHOST_TSuccess getPixelAtCursor(float r_color[3]) const;
/***************************************************************************************
* Event management functionality
***************************************************************************************/

View File

@ -821,6 +821,49 @@ GHOST_IWindow *GHOST_SystemCocoa::getWindowUnderCursor(int32_t x, int32_t y)
return m_windowManager->getWindowAssociatedWithOSWindow((void *)nswindow);
}
GHOST_TSuccess GHOST_SystemCocoa::getPixelAtCursor(float r_color[3]) const
{
@autoreleasepool {
CGPoint cursorPosition = CGEventGetLocation(CGEventCreate(NULL));
cursorPosition.y = mainScreen().frame.y - cursorPosition.y + 20;
// Create rect to get color at location
CGRect rect = CGRectMake(cursorPosition.x, cursorPosition.y, 1, 1);
// Create Image from rect
CGImageRef image = CGDisplayCreateImageForRect(CGMainDisplayID(), rect);
if (image == nil) {
return GHOST_kFailure;
}
// Create bitmap from image & free it
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image];
if (bitmap == nil) {
return GHOST_kFailure;
}
CGImageRelease(image);
// Get color from bitmap
NSColor *color = [bitmap colorAtX:0 y:0];
if (color == nil) {
return GHOST_kFailure;
}
CGFloat red, green, blue, alpha;
[color getRed:&red green:&green blue:&blue alpha:&alpha];
r_color[0] = (float)red;
r_color[1] = (float)green;
r_color[2] = (float)blue;
[bitmap release];
return GHOST_kSuccess;
}
}
/**
* \note returns coordinates in Cocoa screen coordinates.
*/
@ -921,8 +964,6 @@ GHOST_TCapabilityFlag GHOST_SystemCocoa::getCapabilities() const
~(
/* Cocoa has no support for a primary selection clipboard. */
GHOST_kCapabilityPrimaryClipboard |
/* Cocoa has no support for sampling colors from the desktop. */
GHOST_kCapabilityDesktopSample |
/* This Cocoa back-end has not yet implemented image copy/paste. */
GHOST_kCapabilityClipboardImages));
}

View File

@ -1,3 +1,4 @@
#include "GHOST_SystemWayland.hh"
/* SPDX-FileCopyrightText: 2020-2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
@ -6286,6 +6287,12 @@ GHOST_TSuccess GHOST_SystemWayland::setCursorPosition(const int32_t x, const int
return GHOST_kFailure;
}
GHOST_TSuccess GHOST_SystemWayland::getPixelAtCursor(float r_color[3]) const
{
wl_display *wlDisplay = display_->wl.display;
}
void GHOST_SystemWayland::getMainDisplayDimensions(uint32_t &width, uint32_t &height) const
{
#ifdef USE_EVENT_BACKGROUND_THREAD