Added support for custom cursor/icon
This commit is contained in:
61
src/Nazara/Utility/Cursor.cpp
Normal file
61
src/Nazara/Utility/Cursor.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine".
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/Cursor.hpp>
|
||||
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
#include <Nazara/Utility/Win32/CursorImpl.hpp>
|
||||
#elif defined(NAZARA_PLATFORM_LINUX)
|
||||
#include <Nazara/Utility/Linux/CursorImpl.hpp>
|
||||
#else
|
||||
#error Lack of implementation: Cursor
|
||||
#endif
|
||||
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
NzCursor::NzCursor() :
|
||||
m_impl(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
NzCursor::~NzCursor()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
bool NzCursor::Create(const NzImage& cursor, int hotSpotX, int hotSpotY)
|
||||
{
|
||||
Destroy();
|
||||
|
||||
m_impl = new NzCursorImpl;
|
||||
if (!m_impl->Create(cursor, hotSpotX, hotSpotY))
|
||||
{
|
||||
NazaraError("Failed to create cursor implementation");
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzCursor::Create(const NzImage& cursor, const NzVector2i& hotSpot)
|
||||
{
|
||||
return Create(cursor, hotSpot.x, hotSpot.y);
|
||||
}
|
||||
|
||||
void NzCursor::Destroy()
|
||||
{
|
||||
if (m_impl)
|
||||
{
|
||||
m_impl->Destroy();
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool NzCursor::IsValid() const
|
||||
{
|
||||
return m_impl != nullptr;
|
||||
}
|
||||
56
src/Nazara/Utility/Icon.cpp
Normal file
56
src/Nazara/Utility/Icon.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine".
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/Icon.hpp>
|
||||
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
#include <Nazara/Utility/Win32/IconImpl.hpp>
|
||||
#elif defined(NAZARA_PLATFORM_LINUX)
|
||||
#include <Nazara/Utility/Linux/IconImpl.hpp>
|
||||
#else
|
||||
#error Lack of implementation: Icon
|
||||
#endif
|
||||
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
NzIcon::NzIcon() :
|
||||
m_impl(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
NzIcon::~NzIcon()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
bool NzIcon::Create(const NzImage& icon)
|
||||
{
|
||||
Destroy();
|
||||
|
||||
m_impl = new NzIconImpl;
|
||||
if (!m_impl->Create(icon))
|
||||
{
|
||||
NazaraError("Failed to create cursor implementation");
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzIcon::Destroy()
|
||||
{
|
||||
if (m_impl)
|
||||
{
|
||||
m_impl->Destroy();
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool NzIcon::IsValid() const
|
||||
{
|
||||
return m_impl != nullptr;
|
||||
}
|
||||
52
src/Nazara/Utility/Win32/CursorImpl.cpp
Normal file
52
src/Nazara/Utility/Win32/CursorImpl.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine".
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/Win32/CursorImpl.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Utility/Image.hpp>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
bool NzCursorImpl::Create(const NzImage& cursor, int hotSpotX, int hotSpotY)
|
||||
{
|
||||
NzImage windowsCursor(cursor);
|
||||
if (!windowsCursor.Convert(nzPixelFormat_BGRA8))
|
||||
{
|
||||
NazaraError("Failed to convert cursor to BGRA8");
|
||||
return false;
|
||||
}
|
||||
|
||||
HBITMAP bitmap = CreateBitmap(windowsCursor.GetWidth(), windowsCursor.GetHeight(), 1, 32, windowsCursor.GetConstPixels());
|
||||
HBITMAP monoBitmap = CreateBitmap(windowsCursor.GetWidth(), windowsCursor.GetHeight(), 1, 1, nullptr);
|
||||
|
||||
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms648052(v=vs.85).aspx
|
||||
ICONINFO iconInfo;
|
||||
iconInfo.fIcon = FALSE;
|
||||
iconInfo.xHotspot = hotSpotX;
|
||||
iconInfo.yHotspot = hotSpotY;
|
||||
iconInfo.hbmMask = monoBitmap;
|
||||
iconInfo.hbmColor = bitmap;
|
||||
|
||||
m_cursor = CreateIconIndirect(&iconInfo);
|
||||
|
||||
DeleteObject(bitmap);
|
||||
DeleteObject(monoBitmap);
|
||||
|
||||
if (!m_cursor)
|
||||
{
|
||||
NazaraError("Failed to create cursor: " + NzGetLastSystemError());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzCursorImpl::Destroy()
|
||||
{
|
||||
DestroyIcon(m_cursor);
|
||||
}
|
||||
|
||||
HCURSOR NzCursorImpl::GetCursor()
|
||||
{
|
||||
return m_cursor;
|
||||
}
|
||||
26
src/Nazara/Utility/Win32/CursorImpl.hpp
Normal file
26
src/Nazara/Utility/Win32/CursorImpl.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine".
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_CURSORIMPL_HPP
|
||||
#define NAZARA_CURSORIMPL_HPP
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
class NzImage;
|
||||
|
||||
class NzCursorImpl
|
||||
{
|
||||
public:
|
||||
bool Create(const NzImage& image, int hotSpotX, int hotSpotY);
|
||||
void Destroy();
|
||||
|
||||
HCURSOR GetCursor();
|
||||
|
||||
private:
|
||||
HICON m_cursor = nullptr;
|
||||
};
|
||||
|
||||
#endif // NAZARA_CURSORIMPL_HPP
|
||||
49
src/Nazara/Utility/Win32/IconImpl.cpp
Normal file
49
src/Nazara/Utility/Win32/IconImpl.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine".
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Utility/Win32/IconImpl.hpp>
|
||||
#include <Nazara/Utility/Image.hpp>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
bool NzIconImpl::Create(const NzImage& icon)
|
||||
{
|
||||
NzImage windowsIcon(icon);
|
||||
if (!windowsIcon.Convert(nzPixelFormat_BGRA8))
|
||||
{
|
||||
NazaraError("Failed to convert icon to BGRA8");
|
||||
return false;
|
||||
}
|
||||
|
||||
HBITMAP bitmap = CreateBitmap(windowsIcon.GetWidth(), windowsIcon.GetHeight(), 1, 32, windowsIcon.GetConstPixels());
|
||||
HBITMAP monoBitmap = CreateBitmap(windowsIcon.GetWidth(), windowsIcon.GetHeight(), 1, 1, nullptr);
|
||||
|
||||
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms648052(v=vs.85).aspx
|
||||
ICONINFO iconInfo;
|
||||
iconInfo.fIcon = TRUE;
|
||||
iconInfo.hbmMask = monoBitmap;
|
||||
iconInfo.hbmColor = bitmap;
|
||||
|
||||
m_icon = CreateIconIndirect(&iconInfo);
|
||||
|
||||
DeleteObject(bitmap);
|
||||
DeleteObject(monoBitmap);
|
||||
|
||||
if (!m_icon)
|
||||
{
|
||||
NazaraError("Failed to create icon: " + NzGetLastSystemError());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NzIconImpl::Destroy()
|
||||
{
|
||||
DestroyIcon(m_icon);
|
||||
}
|
||||
|
||||
HICON NzIconImpl::GetIcon()
|
||||
{
|
||||
return m_icon;
|
||||
}
|
||||
26
src/Nazara/Utility/Win32/IconImpl.hpp
Normal file
26
src/Nazara/Utility/Win32/IconImpl.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine".
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_ICONIMPL_HPP
|
||||
#define NAZARA_ICONIMPL_HPP
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
class NzImage;
|
||||
|
||||
class NzIconImpl
|
||||
{
|
||||
public:
|
||||
bool Create(const NzImage& image);
|
||||
void Destroy();
|
||||
|
||||
HICON GetIcon();
|
||||
|
||||
private:
|
||||
HICON m_icon = nullptr;
|
||||
};
|
||||
|
||||
#endif // NAZARA_ICONIMPL_HPP
|
||||
@@ -12,6 +12,11 @@
|
||||
#include <Nazara/Core/Thread.hpp>
|
||||
#include <Nazara/Core/ThreadCondition.hpp>
|
||||
#include <Nazara/Utility/Config.hpp>
|
||||
#include <Nazara/Utility/Cursor.hpp>
|
||||
#include <Nazara/Utility/Image.hpp>
|
||||
#include <Nazara/Utility/Icon.hpp>
|
||||
#include <Nazara/Utility/Win32/CursorImpl.hpp>
|
||||
#include <Nazara/Utility/Win32/IconImpl.hpp>
|
||||
#include <cstdio>
|
||||
#include <windowsx.h>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
@@ -274,9 +279,6 @@ bool NzWindowImpl::IsVisible() const
|
||||
|
||||
void NzWindowImpl::SetCursor(nzWindowCursor cursor)
|
||||
{
|
||||
// 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
|
||||
|
||||
switch (cursor)
|
||||
{
|
||||
case nzWindowCursor_Crosshair:
|
||||
@@ -337,9 +339,16 @@ void NzWindowImpl::SetCursor(nzWindowCursor cursor)
|
||||
break;
|
||||
}
|
||||
|
||||
// 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 NzWindowImpl::SetCursor(const NzCursor& cursor)
|
||||
{
|
||||
m_cursor = cursor.m_impl->GetCursor();
|
||||
}
|
||||
|
||||
void NzWindowImpl::SetEventListener(bool listener)
|
||||
{
|
||||
if (m_ownsWindow)
|
||||
@@ -365,6 +374,14 @@ void NzWindowImpl::SetFocus()
|
||||
SetForegroundWindow(m_handle);
|
||||
}
|
||||
|
||||
void NzWindowImpl::SetIcon(const NzIcon& icon)
|
||||
{
|
||||
HICON iconHandle = icon.m_impl->GetIcon();
|
||||
|
||||
SendMessage(m_handle, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(iconHandle));
|
||||
SendMessage(m_handle, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(iconHandle));
|
||||
}
|
||||
|
||||
void NzWindowImpl::SetMaximumSize(int width, int height)
|
||||
{
|
||||
RECT rect = {0, 0, width, height};
|
||||
|
||||
@@ -57,8 +57,10 @@ class NzWindowImpl : NzNonCopyable
|
||||
bool IsVisible() const;
|
||||
|
||||
void SetCursor(nzWindowCursor cursor);
|
||||
void SetCursor(const NzCursor& cursor);
|
||||
void SetEventListener(bool listener);
|
||||
void SetFocus();
|
||||
void SetIcon(const NzIcon& icon);
|
||||
void SetMaximumSize(int width, int height);
|
||||
void SetMinimumSize(int width, int height);
|
||||
void SetPosition(int x, int y);
|
||||
|
||||
@@ -5,11 +5,18 @@
|
||||
#include <Nazara/Utility/Window.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/LockGuard.hpp>
|
||||
#include <Nazara/Utility/Cursor.hpp>
|
||||
#include <Nazara/Utility/Image.hpp>
|
||||
#include <Nazara/Utility/Icon.hpp>
|
||||
#include <stdexcept>
|
||||
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
#include <Nazara/Utility/Win32/CursorImpl.hpp>
|
||||
#include <Nazara/Utility/Win32/IconImpl.hpp>
|
||||
#include <Nazara/Utility/Win32/WindowImpl.hpp>
|
||||
#elif defined(NAZARA_PLATFORM_LINUX)
|
||||
#include <Nazara/Utility/Linux/CursorImpl.hpp>
|
||||
#include <Nazara/Utility/Linux/IconImpl.hpp>
|
||||
#include <Nazara/Utility/Linux/WindowImpl.hpp>
|
||||
#else
|
||||
#error Lack of implementation: Window
|
||||
@@ -305,6 +312,20 @@ void NzWindow::SetCursor(nzWindowCursor cursor)
|
||||
m_impl->SetCursor(cursor);
|
||||
}
|
||||
|
||||
void NzWindow::SetCursor(const NzCursor& cursor)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!cursor.IsValid())
|
||||
{
|
||||
NazaraError("Cursor is not valid");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (m_impl)
|
||||
m_impl->SetCursor(cursor);
|
||||
}
|
||||
|
||||
void NzWindow::SetEventListener(bool listener)
|
||||
{
|
||||
if (!m_impl)
|
||||
@@ -337,6 +358,20 @@ void NzWindow::SetFocus()
|
||||
m_impl->SetFocus();
|
||||
}
|
||||
|
||||
void NzWindow::SetIcon(const NzIcon& icon)
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!icon.IsValid())
|
||||
{
|
||||
NazaraError("Icon is not valid");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (m_impl)
|
||||
m_impl->SetIcon(icon);
|
||||
}
|
||||
|
||||
void NzWindow::SetMaximumSize(const NzVector2i& maxSize)
|
||||
{
|
||||
if (m_impl)
|
||||
|
||||
Reference in New Issue
Block a user