Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ANSH3LL committed Sep 16, 2021
1 parent 59a3252 commit 8bd48b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 46 deletions.
71 changes: 35 additions & 36 deletions shared/PluginHwCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,67 @@

// ----------------------------------------------------------------------------

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

// ----------------------------------------------------------------------------

CORONA_EXPORT
int luaopen_plugin_hwcursor(lua_State *L) {

lua_CFunction factory = Corona::Lua::Open <CoronaPluginLuaLoad_plugin_hwcursor>;
int result = CoronaLibraryNewWithFactory(L, factory, NULL, NULL);

lua_CFunction factory = Corona::Lua::Open<CoronaPluginLuaLoad_plugin_hwcursor>;
int result = CoronaLibraryNewWithFactory(L, factory, NULL, NULL);
if(result) {
const luaL_Reg kFunctions[] = {
{"initPlugin", initPlugin},
{"freePlugin", freePlugin},
{"loadCursor", loadCursor},
{"freeCursor", freeCursor},
{"showCursor", showCursor},
{"hideCursor", hideCursor},
{"resetCursor", resetCursor},
{NULL, NULL}
};

luaL_register(L, NULL, kFunctions);
}

return result;
return result;
}

// ----------------------------------------------------------------------------

HWND windowHandle;
WNDPROC prevWndProc;
HCURSOR currentCursor;
bool cursorHidden = false;

// ----------------------------------------------------------------------------

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
LRESULT result;

if(uMsg == WM_SETCURSOR) {
if (currentCursor && !cursorHidden) {
SetCursor(currentCursor);
result = 1;
}
}
else {
if(uMsg == WM_NCDESTROY) {
currentCursor = NULL;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
if(uMsg == WM_SETCURSOR) {
if(currentCursor && !cursorHidden) {
SetCursor(currentCursor);
return true;
}
result = CallWindowProc(prevWndProc, hwnd, uMsg, wParam, lParam);
}

return result;
}
else if(uMsg == WM_DESTROY || uMsg == WM_NCDESTROY) {
RemoveWindowSubclass(windowHandle, &WindowProc, uIdSubclass);
}
return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}

// ----------------------------------------------------------------------------

static int initPlugin(lua_State *L) {
windowHandle = GetForegroundWindow();
prevWndProc = (WNDPROC)SetWindowLongPtr(windowHandle, GWLP_WNDPROC, (LONG_PTR)&WindowProc);
SetWindowSubclass(windowHandle, &WindowProc, 1, 0);
return 0;
}

// ----------------------------------------------------------------------------

static int freePlugin(lua_State *L) {
RemoveWindowSubclass(windowHandle, &WindowProc, 1);
return 0;
}

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

static int showCursor(lua_State *L) {
if(cursorHidden) {
ShowCursor(true);
cursorHidden = false;
}
if(cursorHidden) {
ShowCursor(true);
cursorHidden = false;
}
return 0;
}

// ----------------------------------------------------------------------------

static int hideCursor(lua_State *L) {
if(!cursorHidden) {
ShowCursor(false);
cursorHidden = true;
}
if(!cursorHidden) {
ShowCursor(false);
cursorHidden = true;
}
return 0;
}

Expand All @@ -116,4 +115,4 @@ static int hideCursor(lua_State *L) {
static int resetCursor(lua_State *L) {
currentCursor = NULL;
return 0;
}
}
15 changes: 6 additions & 9 deletions shared/PluginHwCursor.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//////////////////////////////////////////////////////////////////////////////
//
// This file is part of the Corona game engine.
// For overview and more information on licensing please refer to README.md
// Home page: https://github.com/coronalabs/corona
// Contact: [email protected]
// PluginHwCursor.h
//
//////////////////////////////////////////////////////////////////////////////

Expand All @@ -12,16 +9,15 @@

#include <string>
#include <Windows.h>
#include <commctrl.h>
#include <stringapiset.h>

#include <CoronaLua.h>
#include <CoronaMacros.h>

// ----------------------------------------------------------------------------

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

// ----------------------------------------------------------------------------

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

static int initPlugin(lua_State *L);
static int freePlugin(lua_State *L);
static int loadCursor(lua_State *L);
static int freeCursor(lua_State *L);
static int showCursor(lua_State *L);
Expand All @@ -44,6 +41,6 @@ std::wstring s2ws(const std::string &s) {
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
std::wstring buf;
buf.resize(len);
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, const_cast<wchar_t *>(buf.c_str()), len);
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, const_cast<wchar_t*>(buf.c_str()), len);
return buf;
}
}
2 changes: 1 addition & 1 deletion win32/Plugin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile>
<Link>
<AdditionalDependencies>$(CORONA_ROOT)\Corona\win\lib\*.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>$(CORONA_ROOT)\Corona\win\lib\*.lib;Comctl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem>
<OptimizeReferences>true</OptimizeReferences>
Expand Down

0 comments on commit 8bd48b9

Please sign in to comment.