Fixed composed character problem. Fixed casts. Changed window observer.

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1061 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
ceylo 2009-03-23 12:43:38 +00:00
parent 6b6375abf9
commit b7ead65a60
2 changed files with 68 additions and 87 deletions

View File

@ -277,59 +277,17 @@ static GLContext *sharedCtx = nil;
} }
////////////////////////////////////////////////////////////
/// Notification method receiver when the window gains focus
////////////////////////////////////////////////////////////
- (void)windowDidBecomeMain:(NSNotification *)notification
{
sf::Event ev;
ev.Type = sf::Event::GainedFocus;
[self pushEvent:ev];
}
////////////////////////////////////////////////////////////
/// Notification method receiver when the window loses focus
////////////////////////////////////////////////////////////
- (void)windowDidResignMain:(NSNotification *)notification
{
sf::Event ev;
ev.Type = sf::Event::LostFocus;
[self pushEvent:ev];
}
////////////////////////////////////////////////////////////
/// Notification method receiver when the window closes
////////////////////////////////////////////////////////////
- (void)windowWillClose:(NSNotification *)notification
{
sf::Event ev;
ev.Type = sf::Event::Closed;
[self pushEvent:ev];
}
////////////////////////////////////////////////////////////
/// Notification method receiver when the window finish moving
////////////////////////////////////////////////////////////
- (void)windowDidMove:(NSNotification *)notification
{
NSWindow *sender = [notification object];
if (!([sender styleMask] & NSTitledWindowMask))
[sender center];
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Receiver method called when a key is pressed /// Receiver method called when a key is pressed
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
- (void)keyDown:(NSEvent *)theEvent - (void)keyDown:(NSEvent *)theEvent
{ {
assert(myDelegate != NULL); assert(myDelegate != NULL);
NSText *field = [[self window] fieldEditor:YES forObject:nil];
[field interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
[field setString:@""];
myDelegate->HandleKeyDown(theEvent); myDelegate->HandleKeyDown(theEvent);
} }
@ -672,7 +630,7 @@ static GLContext *sharedCtx = nil;
if (self) if (self)
{ {
if (myWindow) { if (window) {
myWindow = (GLWindow *)[window retain]; myWindow = (GLWindow *)[window retain];
} else { } else {
assert(title != nil); assert(title != nil);
@ -764,25 +722,25 @@ static GLContext *sharedCtx = nil;
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
// We want to know when our window got the focus // We want to know when our window got the focus
[nc addObserver:myView [nc addObserver:self
selector:@selector(windowDidBecomeMain:) selector:@selector(windowDidBecomeMain:)
name:NSWindowDidBecomeMainNotification name:NSWindowDidBecomeMainNotification
object:myWindow]; object:myWindow];
// We want to know when our window lost the focus // We want to know when our window lost the focus
[nc addObserver:myView [nc addObserver:self
selector:@selector(windowDidResignMain:) selector:@selector(windowDidResignMain:)
name:NSWindowDidResignMainNotification name:NSWindowDidResignMainNotification
object:myWindow]; object:myWindow];
// We want to know when the user closes the window // We want to know when the user closes the window
[nc addObserver:myView [nc addObserver:self
selector:@selector(windowWillClose:) selector:@selector(windowWillClose:)
name:NSWindowWillCloseNotification name:NSWindowWillCloseNotification
object:myWindow]; object:myWindow];
// I want to re-center the window if it's a full screen one and moved by Spaces // I want to re-center the window if it's a full screen one and moved by Spaces
[nc addObserver:myView [nc addObserver:self
selector:@selector(windowDidMove:) selector:@selector(windowDidMove:)
name:NSWindowDidMoveNotification name:NSWindowDidMoveNotification
object:myWindow]; object:myWindow];
@ -857,6 +815,7 @@ static GLContext *sharedCtx = nil;
// Remove the notification observer // Remove the notification observer
if (myView) if (myView)
[[NSNotificationCenter defaultCenter] removeObserver:myView]; [[NSNotificationCenter defaultCenter] removeObserver:myView];
[[NSNotificationCenter defaultCenter] removeObserver:self];
// Close the window // Close the window
[self show:false]; [self show:false];
@ -1013,5 +972,53 @@ static GLContext *sharedCtx = nil;
[myView flushBuffer]; [myView flushBuffer];
} }
////////////////////////////////////////////////////////////
/// Notification method receiver when the window gains focus
////////////////////////////////////////////////////////////
- (void)windowDidBecomeMain:(NSNotification *)notification
{
sf::Event ev;
ev.Type = sf::Event::GainedFocus;
[myView pushEvent:ev];
}
////////////////////////////////////////////////////////////
/// Notification method receiver when the window loses focus
////////////////////////////////////////////////////////////
- (void)windowDidResignMain:(NSNotification *)notification
{
sf::Event ev;
ev.Type = sf::Event::LostFocus;
[myView pushEvent:ev];
}
////////////////////////////////////////////////////////////
/// Notification method receiver when the window closes
////////////////////////////////////////////////////////////
- (void)windowWillClose:(NSNotification *)notification
{
sf::Event ev;
ev.Type = sf::Event::Closed;
[myView pushEvent:ev];
}
////////////////////////////////////////////////////////////
/// Notification method receiver when the window finish moving
////////////////////////////////////////////////////////////
- (void)windowDidMove:(NSNotification *)notification
{
NSWindow *sender = [notification object];
if (!([sender styleMask] & NSTitledWindowMask))
[sender center];
}
@end @end

View File

@ -193,7 +193,7 @@ void WindowImplCocoa::HandleNotifiedEvent(Event& event)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void WindowImplCocoa::HandleKeyDown(void *eventRef) void WindowImplCocoa::HandleKeyDown(void *eventRef)
{ {
NSEvent *event = static_cast <NSEvent *> (eventRef); NSEvent *event = reinterpret_cast <NSEvent *> (eventRef);
Event sfEvent; Event sfEvent;
unichar chr = 0, rawchr = 0; unichar chr = 0, rawchr = 0;
@ -212,13 +212,14 @@ void WindowImplCocoa::HandleKeyDown(void *eventRef)
if (!myUseKeyRepeat && [event isARepeat]) if (!myUseKeyRepeat && [event isARepeat])
return; return;
#if 1
// Is it also a text event ? // Is it also a text event ?
if (IsTextEvent(event)) { if (IsTextEvent(event)) {
// buffer for the UTF-32 characters // buffer for the UTF-32 characters
Uint32 utf32Characters[2] = {0}; Uint32 utf32Characters[2] = {0};
// convert the characters // convert the characters
// note: using CFString in order to keep compatibility with Mac OS X 10.4
// (NSUTF32StringEncoding only defined since Mac OS X 10.5)
if (!CFStringGetCString ((CFStringRef)[event characters], if (!CFStringGetCString ((CFStringRef)[event characters],
(char *)utf32Characters, (char *)utf32Characters,
sizeof(utf32Characters), sizeof(utf32Characters),
@ -228,7 +229,7 @@ void WindowImplCocoa::HandleKeyDown(void *eventRef)
if ([[event characters] lengthOfBytesUsingEncoding:NSUTF8StringEncoding]) if ([[event characters] lengthOfBytesUsingEncoding:NSUTF8StringEncoding])
utf8Char = [[event characters] UTF8String]; utf8Char = [[event characters] UTF8String];
std::cerr << "Error while converting the character to UTF32 : " std::cerr << "Error while converting character to UTF32 : "
<< ((utf8Char) ? utf8Char : "(undefined)") << std::endl; << ((utf8Char) ? utf8Char : "(undefined)") << std::endl;
} }
else else
@ -239,33 +240,6 @@ void WindowImplCocoa::HandleKeyDown(void *eventRef)
SendEvent(sfEvent); SendEvent(sfEvent);
} }
} }
#else
// Is it also a text event ?
if (IsTextEvent(event)) {
static NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:1];
sfEvent.Type = Event::TextEntered;
sfEvent.Text.Unicode = chr;
NSText *field = [myWindow fieldEditor:YES forObject:nil];
[arr addObject:event];
[field interpretKeyEvents:arr];
if ([[field string] length]) {
unichar unichr = [[field string] characterAtIndex:0];
sfEvent.Text.Unicode = unichr;
SendEvent(sfEvent);
unichar str[2] = {unichr, 0};
NSLog(@"Char::%@", [NSString stringWithCharacters:str length:2]);
[field setString:@""];
[arr removeAllObjects];
}
}
#endif
// Anyway it's also a KeyPressed event // Anyway it's also a KeyPressed event
sfEvent.Type = Event::KeyPressed; sfEvent.Type = Event::KeyPressed;
@ -291,7 +265,7 @@ void WindowImplCocoa::HandleKeyDown(void *eventRef)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void WindowImplCocoa::HandleKeyUp(void *eventRef) void WindowImplCocoa::HandleKeyUp(void *eventRef)
{ {
NSEvent *event = static_cast <NSEvent *> (eventRef); NSEvent *event = reinterpret_cast <NSEvent *> (eventRef);
Event sfEvent; Event sfEvent;
unsigned mods = [event modifierFlags]; unsigned mods = [event modifierFlags];
@ -326,7 +300,7 @@ void WindowImplCocoa::HandleKeyUp(void *eventRef)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void WindowImplCocoa::HandleModifierKey(void *eventRef) void WindowImplCocoa::HandleModifierKey(void *eventRef)
{ {
NSEvent *event = static_cast <NSEvent *> (eventRef); NSEvent *event = reinterpret_cast <NSEvent *> (eventRef);
Event sfEvent; Event sfEvent;
unsigned mods = [event modifierFlags]; unsigned mods = [event modifierFlags];
@ -374,7 +348,7 @@ void WindowImplCocoa::HandleModifierKey(void *eventRef)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void WindowImplCocoa::HandleMouseDown(void *eventRef) void WindowImplCocoa::HandleMouseDown(void *eventRef)
{ {
NSEvent *event = static_cast <NSEvent *> (eventRef); NSEvent *event = reinterpret_cast <NSEvent *> (eventRef);
Event sfEvent; Event sfEvent;
NSPoint loc = {0, 0}; NSPoint loc = {0, 0};
unsigned mods = [event modifierFlags]; unsigned mods = [event modifierFlags];
@ -425,7 +399,7 @@ void WindowImplCocoa::HandleMouseDown(void *eventRef)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void WindowImplCocoa::HandleMouseUp(void *eventRef) void WindowImplCocoa::HandleMouseUp(void *eventRef)
{ {
NSEvent *event = static_cast <NSEvent *> (eventRef); NSEvent *event = reinterpret_cast <NSEvent *> (eventRef);
Event sfEvent; Event sfEvent;
NSPoint loc = {0, 0}; NSPoint loc = {0, 0};
unsigned mods = [event modifierFlags]; unsigned mods = [event modifierFlags];
@ -508,7 +482,7 @@ void WindowImplCocoa::HandleMouseMove(void *eventRef)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void WindowImplCocoa::HandleMouseWheel(void *eventRef) void WindowImplCocoa::HandleMouseWheel(void *eventRef)
{ {
NSEvent *event = static_cast <NSEvent *> (eventRef); NSEvent *event = reinterpret_cast <NSEvent *> (eventRef);
// SFML uses integer values for delta but Cocoa uses float and it is mostly fewer than 1.0 // SFML uses integer values for delta but Cocoa uses float and it is mostly fewer than 1.0
// Therefore I chose to add the float value to a 'wheel status' and // Therefore I chose to add the float value to a 'wheel status' and