Skip to content

Commit 8bd48b9

Browse files
committed
Minor improvements
1 parent 59a3252 commit 8bd48b9

File tree

3 files changed

+42
-46
lines changed

3 files changed

+42
-46
lines changed

shared/PluginHwCursor.cpp

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,67 @@
1111

1212
// ----------------------------------------------------------------------------
1313

14-
// This corresponds to the name of the Lua file (plugin_hwcursor.lua)
15-
// where the prefix 'CoronaPluginLuaLoad' is prepended.
1614
CORONA_EXPORT int CoronaPluginLuaLoad_plugin_hwcursor(lua_State *);
1715

1816
// ----------------------------------------------------------------------------
1917

2018
CORONA_EXPORT
2119
int luaopen_plugin_hwcursor(lua_State *L) {
22-
23-
lua_CFunction factory = Corona::Lua::Open <CoronaPluginLuaLoad_plugin_hwcursor>;
24-
int result = CoronaLibraryNewWithFactory(L, factory, NULL, NULL);
25-
20+
21+
lua_CFunction factory = Corona::Lua::Open<CoronaPluginLuaLoad_plugin_hwcursor>;
22+
int result = CoronaLibraryNewWithFactory(L, factory, NULL, NULL);
23+
2624
if(result) {
2725
const luaL_Reg kFunctions[] = {
2826
{"initPlugin", initPlugin},
27+
{"freePlugin", freePlugin},
2928
{"loadCursor", loadCursor},
3029
{"freeCursor", freeCursor},
3130
{"showCursor", showCursor},
3231
{"hideCursor", hideCursor},
3332
{"resetCursor", resetCursor},
3433
{NULL, NULL}
3534
};
36-
35+
3736
luaL_register(L, NULL, kFunctions);
3837
}
39-
40-
return result;
38+
39+
return result;
4140
}
4241

4342
// ----------------------------------------------------------------------------
4443

4544
HWND windowHandle;
46-
WNDPROC prevWndProc;
4745
HCURSOR currentCursor;
4846
bool cursorHidden = false;
4947

5048
// ----------------------------------------------------------------------------
5149

52-
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
53-
LRESULT result;
54-
55-
if(uMsg == WM_SETCURSOR) {
56-
if (currentCursor && !cursorHidden) {
57-
SetCursor(currentCursor);
58-
result = 1;
59-
}
60-
}
61-
else {
62-
if(uMsg == WM_NCDESTROY) {
63-
currentCursor = NULL;
50+
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
51+
if(uMsg == WM_SETCURSOR) {
52+
if(currentCursor && !cursorHidden) {
53+
SetCursor(currentCursor);
54+
return true;
6455
}
65-
result = CallWindowProc(prevWndProc, hwnd, uMsg, wParam, lParam);
66-
}
67-
68-
return result;
56+
}
57+
else if(uMsg == WM_DESTROY || uMsg == WM_NCDESTROY) {
58+
RemoveWindowSubclass(windowHandle, &WindowProc, uIdSubclass);
59+
}
60+
return DefSubclassProc(hwnd, uMsg, wParam, lParam);
6961
}
7062

7163
// ----------------------------------------------------------------------------
7264

7365
static int initPlugin(lua_State *L) {
7466
windowHandle = GetForegroundWindow();
75-
prevWndProc = (WNDPROC)SetWindowLongPtr(windowHandle, GWLP_WNDPROC, (LONG_PTR)&WindowProc);
67+
SetWindowSubclass(windowHandle, &WindowProc, 1, 0);
68+
return 0;
69+
}
70+
71+
// ----------------------------------------------------------------------------
72+
73+
static int freePlugin(lua_State *L) {
74+
RemoveWindowSubclass(windowHandle, &WindowProc, 1);
7675
return 0;
7776
}
7877

@@ -94,20 +93,20 @@ static int freeCursor(lua_State *L) {
9493
// ----------------------------------------------------------------------------
9594

9695
static int showCursor(lua_State *L) {
97-
if(cursorHidden) {
98-
ShowCursor(true);
99-
cursorHidden = false;
100-
}
96+
if(cursorHidden) {
97+
ShowCursor(true);
98+
cursorHidden = false;
99+
}
101100
return 0;
102101
}
103102

104103
// ----------------------------------------------------------------------------
105104

106105
static int hideCursor(lua_State *L) {
107-
if(!cursorHidden) {
108-
ShowCursor(false);
109-
cursorHidden = true;
110-
}
106+
if(!cursorHidden) {
107+
ShowCursor(false);
108+
cursorHidden = true;
109+
}
111110
return 0;
112111
}
113112

@@ -116,4 +115,4 @@ static int hideCursor(lua_State *L) {
116115
static int resetCursor(lua_State *L) {
117116
currentCursor = NULL;
118117
return 0;
119-
}
118+
}

shared/PluginHwCursor.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//////////////////////////////////////////////////////////////////////////////
22
//
3-
// This file is part of the Corona game engine.
4-
// For overview and more information on licensing please refer to README.md
5-
// Home page: https://github.com/coronalabs/corona
6-
// Contact: support@coronalabs.com
3+
// PluginHwCursor.h
74
//
85
//////////////////////////////////////////////////////////////////////////////
96

@@ -12,16 +9,15 @@
129

1310
#include <string>
1411
#include <Windows.h>
12+
#include <commctrl.h>
1513
#include <stringapiset.h>
1614

1715
#include <CoronaLua.h>
1816
#include <CoronaMacros.h>
1917

2018
// ----------------------------------------------------------------------------
2119

22-
// This corresponds to the name of the library, e.g. [Lua] require "plugin.hwcursor"
23-
// where the '.' is replaced with '_'
24-
CORONA_EXPORT int luaopen_plugin_hwcursor( lua_State *L );
20+
CORONA_EXPORT int luaopen_plugin_hwcursor(lua_State *L);
2521

2622
// ----------------------------------------------------------------------------
2723

@@ -30,6 +26,7 @@ CORONA_EXPORT int luaopen_plugin_hwcursor( lua_State *L );
3026
// ----------------------------------------------------------------------------
3127

3228
static int initPlugin(lua_State *L);
29+
static int freePlugin(lua_State *L);
3330
static int loadCursor(lua_State *L);
3431
static int freeCursor(lua_State *L);
3532
static int showCursor(lua_State *L);
@@ -44,6 +41,6 @@ std::wstring s2ws(const std::string &s) {
4441
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
4542
std::wstring buf;
4643
buf.resize(len);
47-
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, const_cast<wchar_t *>(buf.c_str()), len);
44+
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, const_cast<wchar_t*>(buf.c_str()), len);
4845
return buf;
49-
}
46+
}

win32/Plugin.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
9191
</ClCompile>
9292
<Link>
93-
<AdditionalDependencies>$(CORONA_ROOT)\Corona\win\lib\*.lib;%(AdditionalDependencies)</AdditionalDependencies>
93+
<AdditionalDependencies>$(CORONA_ROOT)\Corona\win\lib\*.lib;Comctl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
9494
<GenerateDebugInformation>true</GenerateDebugInformation>
9595
<SubSystem>Windows</SubSystem>
9696
<OptimizeReferences>true</OptimizeReferences>

0 commit comments

Comments
 (0)