Skip to content

Commit cc3c426

Browse files
filipe-lemoshuangwei1024
authored andcommitted
Add GLView::setCursor for desktop platforms (cocos2d#19129)
* Add `GLView::setCursor` for desktop platforms * Add test for custom mouse cursor
1 parent 826b52d commit cc3c426

File tree

8 files changed

+130
-6
lines changed

8 files changed

+130
-6
lines changed

build/cocos2d_tests.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,9 @@
11291129
52B47A341A534B2B004E4C60 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52B47A331A534B2B004E4C60 /* Security.framework */; };
11301130
52B47A351A53A43A004E4C60 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52B47A331A534B2B004E4C60 /* Security.framework */; };
11311131
53367D561DC22891005862DE /* WindowTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 53367D541DC22891005862DE /* WindowTest.cpp */; };
1132+
53E6A008218FA06E002BE515 /* InputTest in Resources */ = {isa = PBXBuildFile; fileRef = 53E6A007218FA06D002BE515 /* InputTest */; };
1133+
53E6A009218FA06E002BE515 /* InputTest in Resources */ = {isa = PBXBuildFile; fileRef = 53E6A007218FA06D002BE515 /* InputTest */; };
1134+
53E6A00A218FA06E002BE515 /* InputTest in Resources */ = {isa = PBXBuildFile; fileRef = 53E6A007218FA06D002BE515 /* InputTest */; };
11321135
59620E8F1921E5CF002021B6 /* Bug-Child.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 59620E8D1921E5CF002021B6 /* Bug-Child.cpp */; };
11331136
59620E901921E5CF002021B6 /* Bug-Child.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 59620E8D1921E5CF002021B6 /* Bug-Child.cpp */; };
11341137
59E170151AB42EB10007F2BF /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 826294421AAF071500CB7CF7 /* Security.framework */; };
@@ -2302,6 +2305,7 @@
23022305
52B47A331A534B2B004E4C60 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/Security.framework; sourceTree = DEVELOPER_DIR; };
23032306
53367D541DC22891005862DE /* WindowTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WindowTest.cpp; sourceTree = "<group>"; };
23042307
53367D551DC22891005862DE /* WindowTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WindowTest.h; sourceTree = "<group>"; };
2308+
53E6A007218FA06D002BE515 /* InputTest */ = {isa = PBXFileReference; lastKnownFileType = folder; name = InputTest; path = "../tests/cpp-tests/Resources/InputTest"; sourceTree = "<group>"; };
23052309
59620E8D1921E5CF002021B6 /* Bug-Child.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "Bug-Child.cpp"; sourceTree = "<group>"; };
23062310
59620E8E1921E5CF002021B6 /* Bug-Child.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Bug-Child.h"; sourceTree = "<group>"; };
23072311
5EBEECAE1995247000429821 /* DrawNode3D.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = DrawNode3D.cpp; path = Sprite3DTest/DrawNode3D.cpp; sourceTree = "<group>"; };
@@ -3976,6 +3980,7 @@
39763980
1AC35CBC18CED84500F37B72 /* hd */,
39773981
1AC35CBD18CED84500F37B72 /* Hello.png */,
39783982
1AC35CBE18CED84500F37B72 /* Images */,
3983+
53E6A007218FA06D002BE515 /* InputTest */,
39793984
15B3709219EE5D1000ABE682 /* Manifests */,
39803985
5046AB5A1AF2C4180060550B /* Materials */,
39813986
1AC35CC118CED84500F37B72 /* Misc */,
@@ -5699,6 +5704,7 @@
56995704
5046AB5B1AF2C4180060550B /* Materials in Resources */,
57005705
B63993311A49359F00B07923 /* Particle3D in Resources */,
57015706
1AC35CEC18CED84500F37B72 /* fonts in Resources */,
5707+
53E6A008218FA06E002BE515 /* InputTest in Resources */,
57025708
460629781CDB4DBA00B44058 /* ccs-res in Resources */,
57035709
1AC35CCA18CED84500F37B72 /* animations in Resources */,
57045710
1AC35CEA18CED84500F37B72 /* fileLookup.plist in Resources */,
@@ -5776,6 +5782,7 @@
57765782
507B41211C31BEA60067B53E /* animations in Resources */,
57775783
507B41221C31BEA60067B53E /* cocosvideo.mp4 in Resources */,
57785784
507B41231C31BEA60067B53E /* Sprite3DTest in Resources */,
5785+
53E6A00A218FA06E002BE515 /* InputTest in Resources */,
57795786
507B41251C31BEA60067B53E /* hd in Resources */,
57805787
507B41271C31BEA60067B53E /* Misc in Resources */,
57815788
507B41281C31BEA60067B53E /* Hello.png in Resources */,
@@ -5949,6 +5956,7 @@
59495956
1AC35CE918CED84500F37B72 /* extensions in Resources */,
59505957
3E2BDAD219BEA3E20055CDCD /* audio in Resources */,
59515958
5046AB5C1AF2C4180060550B /* Materials in Resources */,
5959+
53E6A009218FA06E002BE515 /* InputTest in Resources */,
59525960
C08689C318D370C90093E810 /* background.caf in Resources */,
59535961
15B3709419EE5D1000ABE682 /* Manifests in Resources */,
59545962
1AC35D0B18CED84500F37B72 /* zwoptex in Resources */,

cocos/platform/CCGLView.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ class CC_DLL GLView : public Ref
195195
*/
196196
virtual float getFrameZoomFactor() const { return 1.0; }
197197

198+
/**
199+
* Sets the cursor for the window with custom image.
200+
*
201+
* @param filename A path to image file, e.g., "cursors/custom.png".
202+
* @param hotspot Cursor hotspot, as a anchor point, default is top left (0, 1)
203+
*/
204+
virtual void setCursor(const std::string& filename, Vec2 hotspot = Vec2::ANCHOR_TOP_LEFT) {}
205+
206+
/**
207+
* Sets the cursor for the window back to default.
208+
*/
209+
virtual void setDefaultCursor() {}
210+
198211
/**
199212
* Hide or Show the mouse cursor if there is one.
200213
*
@@ -363,7 +376,7 @@ class CC_DLL GLView : public Ref
363376

364377
/** Set window icon (implemented for windows and linux).
365378
*
366-
* @param filename A path to image file, e.g., "icons/cusom.png".
379+
* @param filename A path to image file, e.g., "icons/custom.png".
367380
*/
368381
virtual void setIcon(const std::string& filename) const {};
369382

cocos/platform/desktop/CCGLViewImpl-desktop.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ THE SOFTWARE.
3939
#include "base/ccUtils.h"
4040
#include "base/ccUTF8.h"
4141
#include "2d/CCCamera.h"
42-
#if CC_ICON_SET_SUPPORT
4342
#include "platform/CCImage.h"
44-
#endif /* CC_ICON_SET_SUPPORT */
4543

4644
NS_CC_BEGIN
4745

@@ -207,6 +205,7 @@ GLViewImpl::GLViewImpl(bool initglfw)
207205
, _monitor(nullptr)
208206
, _mouseX(0.0f)
209207
, _mouseY(0.0f)
208+
, _cursor(nullptr)
210209
{
211210
_viewName = "cocos2dx";
212211
g_keyCodeMap.clear();
@@ -504,6 +503,37 @@ void GLViewImpl::setDefaultIcon() const {
504503
}
505504
#endif /* CC_ICON_SET_SUPPORT */
506505

506+
void GLViewImpl::setCursor(const std::string& filename, Vec2 hotspot) {
507+
508+
if (_cursor) {
509+
glfwDestroyCursor(_cursor);
510+
_cursor = nullptr;
511+
}
512+
513+
Image* ccImage = new (std::nothrow) Image();
514+
if (ccImage && ccImage->initWithImageFile(filename)) {
515+
GLFWimage image;
516+
image.width = ccImage->getWidth();
517+
image.height = ccImage->getHeight();
518+
image.pixels = ccImage->getData();
519+
_cursor = glfwCreateCursor(&image, (int)(hotspot.x * image.width), (int)((1.0f - hotspot.y) * image.height));
520+
if (_cursor) {
521+
glfwSetCursor(_mainWindow, _cursor);
522+
}
523+
}
524+
CC_SAFE_DELETE(ccImage);
525+
}
526+
527+
void GLViewImpl::setDefaultCursor() {
528+
529+
if (_cursor) {
530+
glfwDestroyCursor(_cursor);
531+
_cursor = nullptr;
532+
}
533+
534+
glfwSetCursor(_mainWindow, NULL);
535+
}
536+
507537
void GLViewImpl::setCursorVisible( bool isVisible )
508538
{
509539
if( _mainWindow == NULL )

cocos/platform/desktop/CCGLViewImpl-desktop.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,26 @@ class CC_DLL GLViewImpl : public GLView
102102
virtual void setDefaultIcon() const override;
103103
#endif /* CC_ICON_SET_SUPPORT */
104104

105-
/*
106-
* Set zoom factor for frame. This method is for debugging big resolution (e.g.new ipad) app on desktop.
105+
/**
106+
* Sets the cursor for the window with custom image.
107107
*/
108-
void setFrameZoomFactor(float zoomFactor) override;
108+
virtual void setCursor(const std::string& filename, Vec2 hotspot = Vec2::ANCHOR_TOP_LEFT) override;
109+
110+
/**
111+
* Sets the cursor for the window back to default.
112+
*/
113+
virtual void setDefaultCursor() override;
114+
109115
/**
110116
* Hide or Show the mouse cursor if there is one.
111117
*/
112118
virtual void setCursorVisible(bool isVisible) override;
119+
120+
/*
121+
* Set zoom factor for frame. This method is for debugging big resolution (e.g.new ipad) app on desktop.
122+
*/
123+
void setFrameZoomFactor(float zoomFactor) override;
124+
113125
/** Retina support is disabled by default
114126
* @note This method is only available on Mac.
115127
*/
@@ -169,6 +181,8 @@ class CC_DLL GLViewImpl : public GLView
169181
float _mouseX;
170182
float _mouseY;
171183

184+
GLFWcursor* _cursor;
185+
172186
friend class GLFWEventHandler;
173187

174188
public:

tests/cpp-tests/Classes/InputTest/MouseTest.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ MouseTests::MouseTests()
3232
{
3333
ADD_TEST_CASE(MouseEventTest);
3434
ADD_TEST_CASE(HideMouseTest);
35+
ADD_TEST_CASE(CursorTest);
3536
}
3637

3738
//------------------------------------------------------------------
@@ -146,3 +147,46 @@ std::string HideMouseTest::subtitle() const
146147
return "Click to hide mouse";
147148
}
148149

150+
//------------------------------------------------------------------
151+
//
152+
// CursorTest
153+
//
154+
//------------------------------------------------------------------
155+
156+
CursorTest::CursorTest()
157+
{
158+
_cursor = 0;
159+
_lis = EventListenerMouse::create();
160+
_lis->onMouseDown = [this](Event* e){
161+
_cursor = (_cursor + 1) % 3;
162+
switch (_cursor) {
163+
case 1:
164+
Director::getInstance()->getOpenGLView()->setCursor("InputTest/cursor1.png");
165+
break;
166+
case 2:
167+
Director::getInstance()->getOpenGLView()->setCursor("InputTest/cursor2.png", Point::ANCHOR_MIDDLE);
168+
break;
169+
default:
170+
Director::getInstance()->getOpenGLView()->setDefaultCursor();
171+
break;
172+
}
173+
};
174+
175+
_eventDispatcher->addEventListenerWithSceneGraphPriority(_lis, this);
176+
}
177+
178+
CursorTest::~CursorTest()
179+
{
180+
_eventDispatcher->removeEventListener(_lis);
181+
}
182+
183+
std::string CursorTest::title() const
184+
{
185+
return "Custom Mouse Cursor";
186+
}
187+
188+
std::string CursorTest::subtitle() const
189+
{
190+
return "Click to change cursor";
191+
}
192+

tests/cpp-tests/Classes/InputTest/MouseTest.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,19 @@ class HideMouseTest : public BaseMouseTest
7171
cocos2d::EventListenerMouse* _lis;
7272
};
7373

74+
class CursorTest : public BaseMouseTest
75+
{
76+
public:
77+
CREATE_FUNC(CursorTest);
78+
CursorTest();
79+
~CursorTest();
80+
81+
virtual std::string title() const override;
82+
virtual std::string subtitle() const override;
83+
84+
private:
85+
int _cursor;
86+
cocos2d::EventListenerMouse* _lis;
87+
};
88+
7489
#endif
760 Bytes
Loading
233 Bytes
Loading

0 commit comments

Comments
 (0)