Skip to content

Commit c2bd23a

Browse files
authored
Windows/macOS: Added native.setProperty("mouseCursor", cursorName) Api (coronalabs#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.
1 parent 15f3460 commit c2bd23a

File tree

4 files changed

+1768
-1616
lines changed

4 files changed

+1768
-1616
lines changed

platform/mac/GLView.mm

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ - (id)initWithFrame:(NSRect)frameRect
245245
fRuntime = NULL;
246246
fDelegate = nil;
247247
[self initCommon];
248-
fCursorRects = [[NSMutableArray alloc] initWithCapacity:10];
248+
fCursorRects = [[NSMutableArray alloc] initWithCapacity:18];
249249

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

11561156
NSCursor *cursor = [NSCursor currentSystemCursor];
1157-
1157+
11581158
if (strcasecmp(cursorName, "arrow") == 0)
11591159
{
11601160
cursor = [NSCursor arrowCursor];
@@ -1166,6 +1166,10 @@ -(void) setCursor:(const char *) cursorName forRect:(NSRect) bounds
11661166
else if (strcasecmp(cursorName, "openHand") == 0)
11671167
{
11681168
cursor = [NSCursor openHandCursor];
1169+
}
1170+
else if (strcasecmp(cursorName, "pointingHand") == 0)
1171+
{
1172+
cursor = [NSCursor pointingHandCursor];
11691173
}
11701174
else if (strcasecmp(cursorName, "crosshair") == 0)
11711175
{
@@ -1175,30 +1179,69 @@ -(void) setCursor:(const char *) cursorName forRect:(NSRect) bounds
11751179
{
11761180
cursor = [NSCursor operationNotAllowedCursor];
11771181
}
1178-
else if (strcasecmp(cursorName, "pointingHand") == 0)
1182+
else if (strcasecmp(cursorName, "beam") == 0)
11791183
{
1180-
cursor = [NSCursor pointingHandCursor];
1184+
cursor = [NSCursor IBeamCursor];
11811185
}
1182-
else
1186+
else if (strcasecmp(cursorName, "resizeRight") == 0)
11831187
{
1184-
// Remove any rect with these bounds
1185-
int currIdx = 0;
1186-
for (CursorRect *cr in fCursorRects)
1187-
{
1188-
if (NSEqualRects(cr.rect, bounds))
1189-
{
1190-
[fCursorRects removeObjectAtIndex:currIdx];
1191-
[self.window invalidateCursorRectsForView:self];
1192-
break;
1193-
}
1194-
++currIdx;
1195-
}
1196-
1197-
return;
1188+
cursor = [NSCursor resizeRightCursor];
1189+
}
1190+
else if (strcasecmp(cursorName, "resizeLeft") == 0)
1191+
{
1192+
cursor = [NSCursor resizeLeftCursor];
1193+
}
1194+
else if (strcasecmp(cursorName, "resizeLeftRight") == 0)
1195+
{
1196+
cursor = [NSCursor resizeLeftRightCursor];
1197+
}
1198+
else if (strcasecmp(cursorName, "resizeUp") == 0)
1199+
{
1200+
cursor = [NSCursor resizeUpCursor];
1201+
}
1202+
else if (strcasecmp(cursorName, "resizeDown") == 0)
1203+
{
1204+
cursor = [NSCursor resizeDownCursor];
1205+
}
1206+
else if (strcasecmp(cursorName, "resizeUpDown") == 0)
1207+
{
1208+
cursor = [NSCursor resizeUpDownCursor];
1209+
}
1210+
else if (strcasecmp(cursorName, "disappearingItem") == 0)
1211+
{
1212+
cursor = [NSCursor disappearingItemCursor];
1213+
}
1214+
else if (strcasecmp(cursorName, "beamHorizontal") == 0)
1215+
{
1216+
cursor = [NSCursor IBeamCursorForVerticalLayout];
1217+
}
1218+
else if (strcasecmp(cursorName, "dragLink") == 0)
1219+
{
1220+
cursor = [NSCursor dragLinkCursor];
1221+
}
1222+
else if (strcasecmp(cursorName, "dragCopy") == 0)
1223+
{
1224+
cursor = [NSCursor dragCopyCursor];
1225+
}
1226+
else if (strcasecmp(cursorName, "contextMenu") == 0)
1227+
{
1228+
cursor = [NSCursor contextualMenuCursor];
11981229
}
11991230

1231+
// Remove any rect with these bounds
1232+
int currIdx = 0;
1233+
for (CursorRect *cr in fCursorRects)
1234+
{
1235+
if (NSEqualRects(cr.rect, bounds))
1236+
{
1237+
[fCursorRects removeObjectAtIndex:currIdx];
1238+
[self.window invalidateCursorRectsForView:self];
1239+
break;
1240+
}
1241+
++currIdx;
1242+
}
1243+
12001244
[fCursorRects addObject:[[[CursorRect alloc] initWithRect:bounds cursor:cursor] autorelease]];
1201-
12021245
[self.window invalidateCursorRectsForView:self];
12031246
}
12041247

platform/mac/Rtt_MacPlatform.mm

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,34 @@ -(void)alertDidEnd:(NSAlert *)alertView returnCode:(NSInteger)returnCode context
19611961
CoronaLuaWarning(L, "native.setProperty(\"mouseCursorVisible\", mode) expected a boolean parameter but got a %s", lua_typename(L, lua_type(L, valueIndex)));
19621962
}
19631963
}
1964+
else if (Rtt_StringCompare(key, "mouseCursor") == 0)
1965+
{
1966+
if (lua_type(L, valueIndex) == LUA_TSTRING)
1967+
{
1968+
auto requestedStyle = lua_tostring(L, valueIndex);
1969+
NSRect cursorRect = NSMakeRect(0, 0, fView.frame.size.width, fView.frame.size.height);
1970+
const char* validStyles[] = {
1971+
"arrow", "closedHand", "openHand", "pointingHand", "crosshair",
1972+
"notAllowed", "beam", "resizeRight", "resizeLeft",
1973+
"resizeLeftRight", "resizeUp", "resizeDown", "resizeUpDown",
1974+
"disappearingItem", "beamHorizontal", "dragLink", "dragCopy", "contextMenu",
1975+
NULL
1976+
};
1977+
1978+
for (unsigned long i = 0; i < sizeof(validStyles) / sizeof(const char*); i++)
1979+
{
1980+
if (Rtt_StringCompare(requestedStyle, validStyles[i]) == 0)
1981+
{
1982+
[fView setCursor:requestedStyle forRect:cursorRect];
1983+
break;
1984+
}
1985+
}
1986+
}
1987+
else
1988+
{
1989+
CoronaLuaWarning(L, "native.setProperty(\"mouseCursor\", cursor) expected a string parameter but got a %s", lua_typename(L, lua_type(L, valueIndex)));
1990+
}
1991+
}
19641992
else if (Rtt_StringCompare(key, "preferredScreenEdgesDeferringSystemGestures")==0 ||
19651993
Rtt_StringCompare(key, "prefersHomeIndicatorAutoHidden")==0 ||
19661994
Rtt_StringCompare(key, "androidSystemUiVisibility")==0 ||

platform/windows/Corona.Native.Library.Win32/Rtt/Rtt_WinInputDeviceManager.h

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,36 @@ namespace Rtt
3131
namespace Rtt
3232
{
3333

34-
/// <summary>
35-
/// Manages Windows input devices such as a mouse or keyboard and dispatches their input events
36-
/// to the Corona runtime's Lua state.
37-
/// </summary>
38-
class WinInputDeviceManager : public PlatformInputDeviceManager
39-
{
40-
Rtt_CLASS_NO_COPIES(WinInputDeviceManager)
41-
42-
public:
34+
/// <summary>
35+
/// Manages Windows input devices such as a mouse or keyboard and dispatches their input events
36+
/// to the Corona runtime's Lua state.
37+
/// </summary>
38+
class WinInputDeviceManager : public PlatformInputDeviceManager
39+
{
40+
Rtt_CLASS_NO_COPIES(WinInputDeviceManager)
41+
42+
public:
4343
#pragma region Public CursorStyle Enum
4444
/// <summary>
4545
/// <para>Indicates the type of mouse cursor to be used such as kDefaultArrow, kPointingHand, etc.</para>
4646
/// <para>Intended to be passed to the WinInputDeviceManager class' SetCursor() metohd.</para>
4747
/// </summary>
4848
enum class CursorStyle : WORD
4949
{
50+
kAppStarting = (WORD)IDC_APPSTARTING,
5051
kDefaultArrow = (WORD)IDC_ARROW,
51-
kHelp = (WORD)IDC_HELP,
52+
kCrosshair = (WORD)IDC_CROSS,
5253
kPointingHand = (WORD)IDC_HAND,
54+
kHelp = (WORD)IDC_HELP,
5355
kIBeam = (WORD)IDC_IBEAM,
54-
kCrosshair = (WORD)IDC_CROSS,
5556
kSlashedCircle = (WORD)IDC_NO,
56-
kMove = (WORD)IDC_SIZEALL
57+
kMove = (WORD)IDC_SIZEALL,
58+
kSizeNorthEastSouthWest = (WORD)IDC_SIZENESW,
59+
kSizeNorthSouth = (WORD)IDC_SIZENS,
60+
kSizeNorthWestSouthEast = (WORD)IDC_SIZENWSE,
61+
kSizeWestEast = (WORD)IDC_SIZEWE,
62+
kUpArrow = (WORD)IDC_UPARROW,
63+
kHourGlass = (WORD)IDC_WAIT,
5764
};
5865

5966
#pragma endregion
@@ -123,7 +130,7 @@ class WinInputDeviceManager : public PlatformInputDeviceManager
123130

124131
#pragma endregion
125132

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

137144
#pragma endregion
138145

139-
private:
146+
private:
140147
#pragma region Private Constants
141148
enum
142149
{
@@ -199,7 +206,7 @@ class WinInputDeviceManager : public PlatformInputDeviceManager
199206
/// <param name="sender">The InputDeviceMonitor instance that raised this event.</param>
200207
/// <param name="arguments">Provides information about the newly discovered input device.</param>
201208
void OnDiscoveredDevice(
202-
Interop::Input::InputDeviceMonitor& sender, Interop::Input::InputDeviceInterfaceEventArgs& arguments);
209+
Interop::Input::InputDeviceMonitor& sender, Interop::Input::InputDeviceInterfaceEventArgs& arguments);
203210

204211
/// <summary>Called when the rendering surface has received a Windows message.</summary>
205212
/// <param name="sender">Reference to the window/control that received the Windows message.</param>
@@ -223,9 +230,9 @@ class WinInputDeviceManager : public PlatformInputDeviceManager
223230
/// </param>
224231
/// <param name="mouseButtonFlags">The WParam data provided by the Windows mouse message.</param>
225232
void OnReceivedMouseEvent(
226-
Rtt::MouseEvent::MouseEventType eventType, POINT& point,
227-
float scrollWheelDeltaX, float scrollWheelDeltaY, WPARAM mouseButtonFlags);
228-
233+
Rtt::MouseEvent::MouseEventType eventType, POINT& point,
234+
float scrollWheelDeltaX, float scrollWheelDeltaY, WPARAM mouseButtonFlags);
235+
229236
/// <summary>
230237
/// <para>To be called when a mouse/touch event has been received.</para>
231238
/// <para>Dispatches the given data as a Corona "touch" event to Lua.</para>
@@ -239,7 +246,7 @@ class WinInputDeviceManager : public PlatformInputDeviceManager
239246
/// </param>
240247
/// <param name="phase">The touch phase such as kBegan, kMoved, or kEnded.</param>
241248
void OnReceivedTouchEvent(
242-
uint32_t touchIndex, POINT currentPosition, POINT startPosition, Rtt::TouchEvent::Phase phase);
249+
uint32_t touchIndex, POINT currentPosition, POINT startPosition, Rtt::TouchEvent::Phase phase);
243250

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

348355
#pragma endregion
349-
};
356+
};
350357

351358
} // namespace Rtt

0 commit comments

Comments
 (0)