Created
July 9, 2012 14:33
-
-
Save mantognini/3076898 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SFML/Graphics.hpp> | |
#include "StandardCursor.hpp" | |
int main (int argc, const char * argv[]) | |
{ | |
// Create the main window | |
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window"); | |
sf::Clock cursorClock; | |
bool cursorSet = false; | |
// Start the game loop | |
while (window.isOpen()) | |
{ | |
if (!cursorSet and cursorClock.getElapsedTime() > sf::microseconds(100)) { | |
sfp::StandardCursor cursor(sfp::StandardCursor::HAND); | |
cursor.set(window.getSystemHandle()); | |
cursorSet = true; | |
} | |
// Process events | |
sf::Event event; | |
while (window.pollEvent(event)) | |
{ | |
// Close window || Escape pressed : exit | |
if (event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)) { | |
window.close(); | |
} | |
} | |
// Clear screen | |
window.clear(); | |
// Update the window | |
window.display(); | |
} | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef STANDARDCURSOR_HPP | |
#define STANDARDCURSOR_HPP | |
#include <SFML/System.hpp> | |
#include <SFML/Window.hpp> | |
#if defined(SFML_SYSTEM_WINDOWS) | |
#include <windows.h> | |
#elif defined(SFML_SYSTEM_LINUX) | |
#include <X11/cursorfont.h> | |
#include <X11/Xlib.h> | |
#elif defined(SFML_SYSTEM_MACOS) | |
// No include in this hpp file | |
#else | |
#error This OS is not yet supported by the cursor library. | |
#endif | |
namespace sfp | |
{ | |
class StandardCursor | |
{ | |
public: | |
enum TYPE{ WAIT, TEXT, NORMAL, HAND /*,...*/ }; | |
StandardCursor(const TYPE t); | |
void set(const sf::WindowHandle& aWindowHandle) const; | |
~StandardCursor(); | |
private: | |
#ifdef SFML_SYSTEM_WINDOWS | |
HCURSOR Cursor; /*Type of the Cursor with Windows*/ | |
#elif defined(SFML_SYSTEM_LINUX) | |
XID Cursor; | |
Display* display; | |
#elif defined(SFML_SYSTEM_MACOS) | |
TYPE cursorType; | |
#endif | |
}; | |
} | |
#endif // STANDARDCURSOR_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "StandardCursor.hpp" | |
#import <Cocoa/Cocoa.h> | |
sfp::StandardCursor::StandardCursor(const sfp::StandardCursor::TYPE t) | |
: cursorType(t) | |
{ | |
/* that's it */ | |
} | |
void sfp::StandardCursor::set(const sf::WindowHandle& aWindowHandle) const | |
{ | |
id handle = (id)aWindowHandle; | |
// Get the content view of the handle | |
NSView* view = nil; | |
if ([handle isKindOfClass:[NSView class]]) { | |
view = handle; | |
} else { | |
NSWindow* nswindow = handle; | |
view = nswindow.contentView; | |
} | |
NSCursor* defaultCursor = [NSCursor currentCursor]; | |
// Choose the correct cursor | |
NSCursor* cursor = nil; | |
switch (cursorType) | |
{ | |
case sfp::StandardCursor::WAIT : | |
cursor = [NSCursor crosshairCursor]; // There is not really a "wait" cursor on Mac OS X | |
break; | |
case sfp::StandardCursor::HAND : | |
cursor = [NSCursor pointingHandCursor]; | |
break; | |
case sfp::StandardCursor::NORMAL : | |
cursor = [NSCursor arrowCursor]; | |
break; | |
case sfp::StandardCursor::TEXT : | |
cursor = [NSCursor IBeamCursor]; | |
break; | |
//For more cursor options on Mac OS X go here: | |
// https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/nscursor_Class/Reference/Reference.html | |
} | |
// Set up Cocoa cursor managment : | |
NSRect rect = view.frame; | |
// The new cursor | |
[view addCursorRect:rect cursor:cursor]; | |
[cursor setOnMouseEntered:YES]; | |
// Restore the default cursror when the mouse leaves the view area | |
[view addCursorRect:rect cursor:defaultCursor]; | |
[defaultCursor setOnMouseExited:YES]; | |
NSPoint mousePositionInWindow = [[view window] mouseLocationOutsideOfEventStream]; | |
NSPoint mousePositionInView = [view convertPoint:mousePositionInWindow | |
fromView:nil]; // i.e. from the window. | |
// If the mouse cursor is in the view we set it; | |
// otherwise it will be automatically set when the cursor enter the view area | |
if ([view mouse:mousePositionInView inRect:view.frame]) { | |
[cursor set]; | |
} | |
} | |
sfp::StandardCursor::~StandardCursor() | |
{ | |
/* no memory to free */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment