Utility: Rework cursors

-Rename WindowCursor to SystemCursor
-Merged Cursor class with SystemCursor enum
This commit is contained in:
Lynix
2017-01-16 00:32:59 +01:00
parent 954298dc1e
commit f406068c45
16 changed files with 281 additions and 202 deletions

View File

@@ -30,27 +30,77 @@ namespace Nz
iconInfo.hbmMask = monoBitmap;
iconInfo.hbmColor = bitmap;
m_cursor = CreateIconIndirect(&iconInfo);
m_icon = CreateIconIndirect(&iconInfo);
DeleteObject(bitmap);
DeleteObject(monoBitmap);
if (!m_cursor)
if (!m_icon)
{
NazaraError("Failed to create cursor: " + Error::GetLastSystemError());
return false;
}
m_cursor = m_icon;
return true;
}
bool CursorImpl::Create(SystemCursor cursor)
{
if (cursor != SystemCursor_Move)
m_cursor = static_cast<HCURSOR>(LoadImage(nullptr, s_systemCursorIds[cursor], IMAGE_CURSOR, 0, 0, LR_SHARED));
else
m_cursor = nullptr;
// No need to free the cursor if shared
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms648045(v=vs.85).aspx
m_icon = nullptr;
return true;
}
void CursorImpl::Destroy()
{
DestroyIcon(m_cursor);
if (m_icon)
DestroyIcon(m_icon);
}
HCURSOR CursorImpl::GetCursor()
{
return m_cursor;
}
bool CursorImpl::Initialize()
{
return true;
}
void CursorImpl::Uninitialize()
{
}
std::array<LPTSTR, SystemCursor_Max + 1> CursorImpl::s_systemCursorIds =
{
IDC_CROSS, // SystemCursor_Crosshair
IDC_ARROW, // SystemCursor_Default
IDC_HAND, // SystemCursor_Hand
IDC_HELP, // SystemCursor_Help
IDC_SIZEALL, // SystemCursor_Move
nullptr, // SystemCursor_None
IDC_HAND, // SystemCursor_Pointer
IDC_APPSTARTING, // SystemCursor_Progress
IDC_SIZEWE, // SystemCursor_ResizeE
IDC_SIZENS, // SystemCursor_ResizeN
IDC_SIZENESW, // SystemCursor_ResizeNE
IDC_SIZENWSE, // SystemCursor_ResizeNW
IDC_SIZENS, // SystemCursor_ResizeS
IDC_SIZENWSE, // SystemCursor_ResizeSE
IDC_SIZENESW, // SystemCursor_ResizeSW
IDC_SIZEWE, // SystemCursor_ResizeW
IDC_IBEAM, // SystemCursor_Text
IDC_WAIT // SystemCursor_Wait
};
static_assert(SystemCursor_Max + 1 == 18, "System cursor array is incomplete");
}

View File

@@ -8,6 +8,8 @@
#define NAZARA_CURSORIMPL_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <array>
#include <windows.h>
namespace Nz
@@ -16,14 +18,24 @@ namespace Nz
class CursorImpl
{
friend class Cursor;
public:
bool Create(const Image& image, int hotSpotX, int hotSpotY);
bool Create(SystemCursor cursor);
void Destroy();
HCURSOR GetCursor();
private:
HICON m_cursor = nullptr;
static bool Initialize();
static void Uninitialize();
HCURSOR m_cursor = nullptr;
HICON m_icon = nullptr;
static std::array<LPTSTR, SystemCursor_Max + 1> s_systemCursorIds;
};
}

View File

@@ -4,8 +4,6 @@
// Un grand merci à Laurent Gomila pour la SFML qui m'aura bien aidé à réaliser cette implémentation
#define OEMRESOURCE
#include <Nazara/Utility/Win32/WindowImpl.hpp>
#include <Nazara/Core/ConditionVariable.hpp>
#include <Nazara/Core/Error.hpp>
@@ -38,30 +36,6 @@ namespace Nz
{
namespace
{
LPTSTR windowsCursors[] =
{
IDC_CROSS, // WindowCursor_Crosshair
IDC_ARROW, // WindowCursor_Default
IDC_HAND, // WindowCursor_Hand
IDC_HAND, // WindowCursor_Pointer
IDC_HELP, // WindowCursor_Help
IDC_SIZEALL, // WindowCursor_Move
nullptr, // WindowCursor_None
IDC_APPSTARTING, // WindowCursor_Progress
IDC_SIZENS, // WindowCursor_ResizeN
IDC_SIZENS, // WindowCursor_ResizeS
IDC_SIZENWSE, // WindowCursor_ResizeNW
IDC_SIZENWSE, // WindowCursor_ResizeSE
IDC_SIZENESW, // WindowCursor_ResizeNE
IDC_SIZENESW, // WindowCursor_ResizeSW
IDC_SIZEWE, // WindowCursor_ResizeE
IDC_SIZEWE, // WindowCursor_ResizeW
IDC_IBEAM, // WindowCursor_Text
IDC_WAIT // WindowCursor_Wait
};
static_assert(sizeof(windowsCursors)/sizeof(LPTSTR) == WindowCursor_Max+1, "Cursor type array is incomplete");
const wchar_t* className = L"Nazara Window";
WindowImpl* fullscreenWindow = nullptr;
}
@@ -321,26 +295,6 @@ namespace Nz
}
}
void WindowImpl::SetCursor(WindowCursor cursor)
{
#ifdef NAZARA_DEBUG
if (cursor > WindowCursor_Max)
{
NazaraError("Window cursor out of enum");
return;
}
#endif
if (cursor != WindowCursor_None)
m_cursor = static_cast<HCURSOR>(LoadImage(nullptr, windowsCursors[cursor], IMAGE_CURSOR, 0, 0, LR_SHARED));
else
m_cursor = nullptr;
// Pas besoin de libérer le curseur par la suite s'il est partagé
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms648045(v=vs.85).aspx
::SetCursor(m_cursor);
}
void WindowImpl::SetCursor(const Cursor& cursor)
{
m_cursor = cursor.m_impl->GetCursor();

View File

@@ -27,7 +27,7 @@ namespace Nz
class Mutex;
class Window;
#undef IsMinimized // Conflit avec la méthode du même nom
#undef IsMinimized // Conflits with windows.h redefinition
class WindowImpl
{
@@ -62,7 +62,6 @@ namespace Nz
void ProcessEvents(bool block);
void SetCursor(WindowCursor cursor);
void SetCursor(const Cursor& cursor);
void SetEventListener(bool listener);
void SetFocus();