Skip to content

Commit

Permalink
Windows/macOS: Added native.setProperty("mouseCursor", cursorName) Api (
Browse files Browse the repository at this point in the history
#83)

* WIN: Added native.setProperty("mouseCursor") API

- Windows: Added `native.setProperty("mouseCursor", cursorName) Api. It allows you to change between all of the supported OS cursors on Windows.

* MAC: Added native.setProperty("mouseCursor") API

- MAC: Added `native.setProperty("mouseCursor", cursorName) Api. It allows you to change between all of the supported OS cursors on MacOS.
  • Loading branch information
DanS2D committed Jun 7, 2020
1 parent 15f3460 commit c2bd23a
Show file tree
Hide file tree
Showing 4 changed files with 1,768 additions and 1,616 deletions.
83 changes: 63 additions & 20 deletions platform/mac/GLView.mm
Expand Up @@ -245,7 +245,7 @@ - (id)initWithFrame:(NSRect)frameRect
fRuntime = NULL;
fDelegate = nil;
[self initCommon];
fCursorRects = [[NSMutableArray alloc] initWithCapacity:10];
fCursorRects = [[NSMutableArray alloc] initWithCapacity:18];

sendAllMouseEvents = YES;
inFullScreenTransition = NO;
Expand Down Expand Up @@ -1154,7 +1154,7 @@ -(void) setCursor:(const char *) cursorName forRect:(NSRect) bounds
// NSDEBUG(@"GLView:setCursor: %@", NSStringFromRect(bounds));

NSCursor *cursor = [NSCursor currentSystemCursor];

if (strcasecmp(cursorName, "arrow") == 0)
{
cursor = [NSCursor arrowCursor];
Expand All @@ -1166,6 +1166,10 @@ -(void) setCursor:(const char *) cursorName forRect:(NSRect) bounds
else if (strcasecmp(cursorName, "openHand") == 0)
{
cursor = [NSCursor openHandCursor];
}
else if (strcasecmp(cursorName, "pointingHand") == 0)
{
cursor = [NSCursor pointingHandCursor];
}
else if (strcasecmp(cursorName, "crosshair") == 0)
{
Expand All @@ -1175,30 +1179,69 @@ -(void) setCursor:(const char *) cursorName forRect:(NSRect) bounds
{
cursor = [NSCursor operationNotAllowedCursor];
}
else if (strcasecmp(cursorName, "pointingHand") == 0)
else if (strcasecmp(cursorName, "beam") == 0)
{
cursor = [NSCursor pointingHandCursor];
cursor = [NSCursor IBeamCursor];
}
else
else if (strcasecmp(cursorName, "resizeRight") == 0)
{
// Remove any rect with these bounds
int currIdx = 0;
for (CursorRect *cr in fCursorRects)
{
if (NSEqualRects(cr.rect, bounds))
{
[fCursorRects removeObjectAtIndex:currIdx];
[self.window invalidateCursorRectsForView:self];
break;
}
++currIdx;
}

return;
cursor = [NSCursor resizeRightCursor];
}
else if (strcasecmp(cursorName, "resizeLeft") == 0)
{
cursor = [NSCursor resizeLeftCursor];
}
else if (strcasecmp(cursorName, "resizeLeftRight") == 0)
{
cursor = [NSCursor resizeLeftRightCursor];
}
else if (strcasecmp(cursorName, "resizeUp") == 0)
{
cursor = [NSCursor resizeUpCursor];
}
else if (strcasecmp(cursorName, "resizeDown") == 0)
{
cursor = [NSCursor resizeDownCursor];
}
else if (strcasecmp(cursorName, "resizeUpDown") == 0)
{
cursor = [NSCursor resizeUpDownCursor];
}
else if (strcasecmp(cursorName, "disappearingItem") == 0)
{
cursor = [NSCursor disappearingItemCursor];
}
else if (strcasecmp(cursorName, "beamHorizontal") == 0)
{
cursor = [NSCursor IBeamCursorForVerticalLayout];
}
else if (strcasecmp(cursorName, "dragLink") == 0)
{
cursor = [NSCursor dragLinkCursor];
}
else if (strcasecmp(cursorName, "dragCopy") == 0)
{
cursor = [NSCursor dragCopyCursor];
}
else if (strcasecmp(cursorName, "contextMenu") == 0)
{
cursor = [NSCursor contextualMenuCursor];
}

// Remove any rect with these bounds
int currIdx = 0;
for (CursorRect *cr in fCursorRects)
{
if (NSEqualRects(cr.rect, bounds))
{
[fCursorRects removeObjectAtIndex:currIdx];
[self.window invalidateCursorRectsForView:self];
break;
}
++currIdx;
}

[fCursorRects addObject:[[[CursorRect alloc] initWithRect:bounds cursor:cursor] autorelease]];

[self.window invalidateCursorRectsForView:self];
}

Expand Down
28 changes: 28 additions & 0 deletions platform/mac/Rtt_MacPlatform.mm
Expand Up @@ -1961,6 +1961,34 @@ -(void)alertDidEnd:(NSAlert *)alertView returnCode:(NSInteger)returnCode context
CoronaLuaWarning(L, "native.setProperty(\"mouseCursorVisible\", mode) expected a boolean parameter but got a %s", lua_typename(L, lua_type(L, valueIndex)));
}
}
else if (Rtt_StringCompare(key, "mouseCursor") == 0)
{
if (lua_type(L, valueIndex) == LUA_TSTRING)
{
auto requestedStyle = lua_tostring(L, valueIndex);
NSRect cursorRect = NSMakeRect(0, 0, fView.frame.size.width, fView.frame.size.height);
const char* validStyles[] = {
"arrow", "closedHand", "openHand", "pointingHand", "crosshair",
"notAllowed", "beam", "resizeRight", "resizeLeft",
"resizeLeftRight", "resizeUp", "resizeDown", "resizeUpDown",
"disappearingItem", "beamHorizontal", "dragLink", "dragCopy", "contextMenu",
NULL
};

for (unsigned long i = 0; i < sizeof(validStyles) / sizeof(const char*); i++)
{
if (Rtt_StringCompare(requestedStyle, validStyles[i]) == 0)
{
[fView setCursor:requestedStyle forRect:cursorRect];
break;
}
}
}
else
{
CoronaLuaWarning(L, "native.setProperty(\"mouseCursor\", cursor) expected a string parameter but got a %s", lua_typename(L, lua_type(L, valueIndex)));
}
}
else if (Rtt_StringCompare(key, "preferredScreenEdgesDeferringSystemGestures")==0 ||
Rtt_StringCompare(key, "prefersHomeIndicatorAutoHidden")==0 ||
Rtt_StringCompare(key, "androidSystemUiVisibility")==0 ||
Expand Down
Expand Up @@ -31,29 +31,36 @@ namespace Rtt
namespace Rtt
{

/// <summary>
/// Manages Windows input devices such as a mouse or keyboard and dispatches their input events
/// to the Corona runtime's Lua state.
/// </summary>
class WinInputDeviceManager : public PlatformInputDeviceManager
{
Rtt_CLASS_NO_COPIES(WinInputDeviceManager)

public:
/// <summary>
/// Manages Windows input devices such as a mouse or keyboard and dispatches their input events
/// to the Corona runtime's Lua state.
/// </summary>
class WinInputDeviceManager : public PlatformInputDeviceManager
{
Rtt_CLASS_NO_COPIES(WinInputDeviceManager)

public:
#pragma region Public CursorStyle Enum
/// <summary>
/// <para>Indicates the type of mouse cursor to be used such as kDefaultArrow, kPointingHand, etc.</para>
/// <para>Intended to be passed to the WinInputDeviceManager class' SetCursor() metohd.</para>
/// </summary>
enum class CursorStyle : WORD
{
kAppStarting = (WORD)IDC_APPSTARTING,
kDefaultArrow = (WORD)IDC_ARROW,
kHelp = (WORD)IDC_HELP,
kCrosshair = (WORD)IDC_CROSS,
kPointingHand = (WORD)IDC_HAND,
kHelp = (WORD)IDC_HELP,
kIBeam = (WORD)IDC_IBEAM,
kCrosshair = (WORD)IDC_CROSS,
kSlashedCircle = (WORD)IDC_NO,
kMove = (WORD)IDC_SIZEALL
kMove = (WORD)IDC_SIZEALL,
kSizeNorthEastSouthWest = (WORD)IDC_SIZENESW,
kSizeNorthSouth = (WORD)IDC_SIZENS,
kSizeNorthWestSouthEast = (WORD)IDC_SIZENWSE,
kSizeWestEast = (WORD)IDC_SIZEWE,
kUpArrow = (WORD)IDC_UPARROW,
kHourGlass = (WORD)IDC_WAIT,
};

#pragma endregion
Expand Down Expand Up @@ -123,7 +130,7 @@ class WinInputDeviceManager : public PlatformInputDeviceManager

#pragma endregion

protected:
protected:
#pragma region Protected Methods
/// <summary>Called when this device manager needs a new input device object to be created.</summary>
/// <param name="descriptor">Unique descriptor used to identify the new input device.</param>
Expand All @@ -136,7 +143,7 @@ class WinInputDeviceManager : public PlatformInputDeviceManager

#pragma endregion

private:
private:
#pragma region Private Constants
enum
{
Expand Down Expand Up @@ -199,7 +206,7 @@ class WinInputDeviceManager : public PlatformInputDeviceManager
/// <param name="sender">The InputDeviceMonitor instance that raised this event.</param>
/// <param name="arguments">Provides information about the newly discovered input device.</param>
void OnDiscoveredDevice(
Interop::Input::InputDeviceMonitor& sender, Interop::Input::InputDeviceInterfaceEventArgs& arguments);
Interop::Input::InputDeviceMonitor& sender, Interop::Input::InputDeviceInterfaceEventArgs& arguments);

/// <summary>Called when the rendering surface has received a Windows message.</summary>
/// <param name="sender">Reference to the window/control that received the Windows message.</param>
Expand All @@ -223,9 +230,9 @@ class WinInputDeviceManager : public PlatformInputDeviceManager
/// </param>
/// <param name="mouseButtonFlags">The WParam data provided by the Windows mouse message.</param>
void OnReceivedMouseEvent(
Rtt::MouseEvent::MouseEventType eventType, POINT& point,
float scrollWheelDeltaX, float scrollWheelDeltaY, WPARAM mouseButtonFlags);
Rtt::MouseEvent::MouseEventType eventType, POINT& point,
float scrollWheelDeltaX, float scrollWheelDeltaY, WPARAM mouseButtonFlags);

/// <summary>
/// <para>To be called when a mouse/touch event has been received.</para>
/// <para>Dispatches the given data as a Corona "touch" event to Lua.</para>
Expand All @@ -239,7 +246,7 @@ class WinInputDeviceManager : public PlatformInputDeviceManager
/// </param>
/// <param name="phase">The touch phase such as kBegan, kMoved, or kEnded.</param>
void OnReceivedTouchEvent(
uint32_t touchIndex, POINT currentPosition, POINT startPosition, Rtt::TouchEvent::Phase phase);
uint32_t touchIndex, POINT currentPosition, POINT startPosition, Rtt::TouchEvent::Phase phase);

/// <summary>
/// <para>Extract the mouse x/y coordinate from the given Windows message LPARAM.</para>
Expand Down Expand Up @@ -346,6 +353,6 @@ class WinInputDeviceManager : public PlatformInputDeviceManager
CursorStyle fCursorStyle;

#pragma endregion
};
};

} // namespace Rtt

0 comments on commit c2bd23a

Please sign in to comment.