mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2024-11-23 04:42:32 +00:00
[qwaq] Add Listener and ListenerGroup delegates
Simple single parameter (caller) direct messages.
This commit is contained in:
parent
01835cbd94
commit
8c3cf83f02
3 changed files with 96 additions and 0 deletions
|
@ -28,6 +28,7 @@ qwaq_app_dat_src= \
|
|||
qwaq-draw.r \
|
||||
qwaq-garray.r \
|
||||
qwaq-group.r \
|
||||
qwaq-listener.r \
|
||||
qwaq-rect.r \
|
||||
qwaq-screen.r \
|
||||
qwaq-textcontext.r \
|
||||
|
|
29
ruamoko/qwaq/qwaq-listener.h
Normal file
29
ruamoko/qwaq/qwaq-listener.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
#ifndef __qwaq_listener_h
|
||||
#define __qwaq_listener_h
|
||||
|
||||
#include <Object.h>
|
||||
|
||||
@class Array;
|
||||
|
||||
@interface Listener : Object
|
||||
{
|
||||
id responder;
|
||||
SEL message;
|
||||
IMP imp;
|
||||
}
|
||||
-initWithResponder: (id) responder message: (SEL) message;
|
||||
-(void) respond: (id) caller;
|
||||
-(BOOL) matchResponder: (id) responder message: (SEL) message;
|
||||
@end
|
||||
|
||||
@interface ListenerGroup : Object
|
||||
{
|
||||
Array *listeners;
|
||||
}
|
||||
-init;
|
||||
-addListener: (id) responder message: (SEL) message;
|
||||
-removeListener: (id) responder message: (SEL) message;
|
||||
-(void) respond: (id) caller;
|
||||
@end
|
||||
|
||||
#endif//__qwaq_listener_h
|
66
ruamoko/qwaq/qwaq-listener.r
Normal file
66
ruamoko/qwaq/qwaq-listener.r
Normal file
|
@ -0,0 +1,66 @@
|
|||
#include <Array.h>
|
||||
|
||||
#include "qwaq-listener.h"
|
||||
|
||||
@class Array;
|
||||
|
||||
@implementation Listener
|
||||
-initWithResponder: (id) responder message: (SEL) message
|
||||
{
|
||||
if (!(self = [super init])) {
|
||||
return nil;
|
||||
}
|
||||
self.responder = responder;
|
||||
self.message = message;
|
||||
imp = [responder instanceMethodForSelector: message];
|
||||
return self;
|
||||
}
|
||||
|
||||
-(void)respond: (id) caller
|
||||
{
|
||||
imp (responder, message, caller);
|
||||
}
|
||||
|
||||
-(BOOL) matchResponder: (id) responder message: (SEL) message
|
||||
{
|
||||
return self.responder == responder && self.message == message;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation ListenerGroup : Object
|
||||
-init
|
||||
{
|
||||
if (!(self = [super init])) {
|
||||
return nil;
|
||||
}
|
||||
listeners = [[Array alloc] retain];
|
||||
return self;
|
||||
}
|
||||
|
||||
-addListener: (id) responder message: (SEL) message
|
||||
{
|
||||
Listener *listener = [[Listener alloc] initWithResponder: responder
|
||||
message: message];
|
||||
if (listener) {
|
||||
[listeners addObject: listener];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
-removeListener: (id) responder message: (SEL) message
|
||||
{
|
||||
for (int i = [listeners count]; i-- > 0; ) {
|
||||
Listener *l = [listeners objectAtIndex: i];
|
||||
if ([l matchResponder: responder message: message]) {
|
||||
[listeners removeObjectAtIndex: i];
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
-(void)respond: (id) caller
|
||||
{
|
||||
[listeners makeObjectsPerformSelector: @selector (respond:)
|
||||
withObject: caller];
|
||||
}
|
||||
@end
|
Loading…
Reference in a new issue