CCIMEDispatcher works on ios.

This commit is contained in:
yangws 2011-04-19 18:11:57 +08:00
parent d11bd85d00
commit 85027c40ca
8 changed files with 325 additions and 9 deletions

View File

@ -1 +1 @@
564b1b0ff02bf8c6185be8446b8ea8e156db7f6a 75af303921a155bf8589306700acea56aa270738

View File

@ -69,6 +69,14 @@ protected:
*/ */
virtual void deleteBackward() = 0; virtual void deleteBackward() = 0;
//////////////////////////////////////////////////////////////////////////
// keyboard show/hide notification
//////////////////////////////////////////////////////////////////////////
virtual void keyboardWillShow(CCRect& begin, CCRect& end) {}
virtual void keyboardDidShow(CCRect& begin, CCRect& end) {}
virtual void keyboardWillHide(CCRect& begin, CCRect& end) {}
virtual void keyboardDidHide(CCRect& begin, CCRect& end) {}
protected: protected:
CCIMEDelegate(); CCIMEDelegate();
}; };

View File

@ -25,7 +25,7 @@ THE SOFTWARE.
#ifndef __CC_IME_DISPATCHER_H__ #ifndef __CC_IME_DISPATCHER_H__
#define __CC_IME_DISPATCHER_H__ #define __CC_IME_DISPATCHER_H__
#include "CCCommon.h" #include "CCGeometry.h"
NS_CC_BEGIN; NS_CC_BEGIN;
@ -59,6 +59,14 @@ public:
*/ */
void dispatchDeleteBackward(); void dispatchDeleteBackward();
//////////////////////////////////////////////////////////////////////////
// dispatch keyboard notification
//////////////////////////////////////////////////////////////////////////
void dispatchKeyboardWillShow(CCRect& begin, CCRect& end);
void dispatchKeyboardDidShow(CCRect& begin, CCRect& end);
void dispatchKeyboardWillHide(CCRect& begin, CCRect& end);
void dispatchKeyboardDidHide(CCRect& begin, CCRect& end);
protected: protected:
friend class CCIMEDelegate; friend class CCIMEDelegate;

View File

@ -55,6 +55,8 @@ public:
void touchesEnded(CCSet *set); void touchesEnded(CCSet *set);
void touchesCancelled(CCSet *set); void touchesCancelled(CCSet *set);
void setIMEKeyboardState(bool bOpen);
static CCEGLView& sharedOpenGLView(); static CCEGLView& sharedOpenGLView();
private: private:

View File

@ -111,13 +111,25 @@ void CCEGLView::touchesCancelled(CCSet *set)
void CCEGLView::setViewPortInPoints(float x, float y, float w, float h) void CCEGLView::setViewPortInPoints(float x, float y, float w, float h)
{ {
glViewport((GLint)x, (GLint)y, (GLint)w, (GLint)h); glViewport((GLint)x, (GLint)y, (GLint)w, (GLint)h);
}
void CCEGLView::setIMEKeyboardState(bool bOpen)
{
if (bOpen)
{
[[EAGLView sharedEGLView] becomeFirstResponder];
}
else
{
[[EAGLView sharedEGLView] resignFirstResponder];
}
} }
CCEGLView& CCEGLView::sharedOpenGLView() CCEGLView& CCEGLView::sharedOpenGLView()
{ {
static CCEGLView instance; static CCEGLView instance;
return instance; return instance;
} }
} // end of namespace cocos2d; } // end of namespace cocos2d;

View File

@ -77,7 +77,7 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved.
* The view content is basically an EAGL surface you render your OpenGL scene into. * The view content is basically an EAGL surface you render your OpenGL scene into.
* Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel. * Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel.
*/ */
@interface EAGLView : UIView @interface EAGLView : UIView <UIKeyInput, UITextInput>
{ {
id <ESRenderer> renderer_; id <ESRenderer> renderer_;
EAGLContext *context_; // weak ref EAGLContext *context_; // weak ref
@ -91,12 +91,21 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved.
//fsaa addition //fsaa addition
BOOL multisampling_; BOOL multisampling_;
unsigned int requestedSamples_; unsigned int requestedSamples_;
@private @private
CFMutableDictionaryRef touchesIntergerDict; CFMutableDictionaryRef touchesIntergerDict;
unsigned int indexBitsUsed; unsigned int indexBitsUsed;
NSString * markedText_;
} }
@property(nonatomic, readonly) UITextPosition *beginningOfDocument;
@property(nonatomic, readonly) UITextPosition *endOfDocument;
@property(nonatomic, assign) id<UITextInputDelegate> inputDelegate;
@property(nonatomic, readonly) UITextRange *markedTextRange;
@property (nonatomic, copy) NSDictionary *markedTextStyle;
@property(readwrite, copy) UITextRange *selectedTextRange;
@property(nonatomic, readonly) id<UITextInputTokenizer> tokenizer;
/** creates an initializes an EAGLView with a frame and 0-bit depth buffer, and a RGB565 color buffer */ /** creates an initializes an EAGLView with a frame and 0-bit depth buffer, and a RGB565 color buffer */
+ (id) viewWithFrame:(CGRect)frame; + (id) viewWithFrame:(CGRect)frame;
/** creates an initializes an EAGLView with a frame, a color buffer format, and 0-bit depth buffer */ /** creates an initializes an EAGLView with a frame, a color buffer format, and 0-bit depth buffer */
@ -142,4 +151,5 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved.
-(int) getHeight; -(int) getHeight;
-(int) getUnUsedIndex; -(int) getUnUsedIndex;
-(void) removeUsedIndexBit:(int) index; -(void) removeUsedIndexBit:(int) index;
@end @end

View File

@ -68,6 +68,7 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved.
#import "CCDirector.h" #import "CCDirector.h"
#import "CCSet.h" #import "CCSet.h"
#import "CCTouch.h" #import "CCTouch.h"
#import "CCIMEDispatcher.h"
#import "OpenGL_Internal.h" #import "OpenGL_Internal.h"
//CLASS IMPLEMENTATIONS: //CLASS IMPLEMENTATIONS:
@ -491,4 +492,203 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES];
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->touchesCancelled(&set); cocos2d::CCDirector::sharedDirector()->getOpenGLView()->touchesCancelled(&set);
} }
#pragma mark -
#pragma mark UIView - Responder
- (BOOL)canBecomeFirstResponder
{
markedText_ = nil;
return YES;
}
#pragma mark -
#pragma mark EAGLView - UIKeyInput
- (BOOL)hasText
{
return NO;
}
- (void)insertText:(NSString *)text
{
const char * pszText = [text cStringUsingEncoding:NSUTF8StringEncoding];
cocos2d::CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(pszText, strlen(pszText));
}
- (void)deleteBackward
{
cocos2d::CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
}
#pragma mark -
#pragma mark UITextInput protocol
#pragma mark UITextInput - properties
@synthesize beginningOfDocument;
@synthesize endOfDocument;
@synthesize inputDelegate;
@synthesize markedTextRange;
@synthesize markedTextStyle;
// @synthesize selectedTextRange; // must implement
@synthesize tokenizer;
/* Text may have a selection, either zero-length (a caret) or ranged. Editing operations are
* always performed on the text from this selection. nil corresponds to no selection. */
- (void)setSelectedTextRange:(UITextRange *)aSelectedTextRange;
{
CCLOG("UITextRange:setSelectedTextRange");
}
- (UITextRange *)selectedTextRange;
{
return [[[UITextRange alloc] init] autorelease];
}
#pragma mark UITextInput - Replacing and Returning Text
- (NSString *)textInRange:(UITextRange *)range;
{
CCLOG("textInRange");
return nil;
}
- (void)replaceRange:(UITextRange *)range withText:(NSString *)theText;
{
CCLOG("replaceRange");
}
#pragma mark UITextInput - Working with Marked and Selected Text
/* If text can be selected, it can be marked. Marked text represents provisionally
* inserted text that has yet to be confirmed by the user. It requires unique visual
* treatment in its display. If there is any marked text, the selection, whether a
* caret or an extended range, always resides witihin.
*
* Setting marked text either replaces the existing marked text or, if none is present,
* inserts it from the current selection. */
- (void)setMarkedTextRange:(UITextRange *)markedTextRange;
{
CCLOG("setMarkedTextRange");
}
- (UITextRange *)markedTextRange;
{
CCLOG("markedTextRange");
return nil; // Nil if no marked text.
}
- (void)setMarkedTextStyle:(NSDictionary *)markedTextStyle;
{
CCLOG("setMarkedTextStyle");
}
- (NSDictionary *)markedTextStyle;
{
CCLOG("markedTextStyle");
return nil;
}
- (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange;
{
CCLOG("setMarkedText");
markedText_ = markedText;
}
- (void)unmarkText;
{
CCLOG("unmarkText");
if (nil == markedText_)
{
return;
}
const char * pszText = [markedText_ cStringUsingEncoding:NSUTF8StringEncoding];
cocos2d::CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(pszText, strlen(pszText));
markedText_ = nil;
}
#pragma mark Methods for creating ranges and positions.
- (UITextRange *)textRangeFromPosition:(UITextPosition *)fromPosition toPosition:(UITextPosition *)toPosition;
{
CCLOG("textRangeFromPosition");
return nil;
}
- (UITextPosition *)positionFromPosition:(UITextPosition *)position offset:(NSInteger)offset;
{
CCLOG("positionFromPosition");
return nil;
}
- (UITextPosition *)positionFromPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction offset:(NSInteger)offset;
{
CCLOG("positionFromPosition");
return nil;
}
/* Simple evaluation of positions */
- (NSComparisonResult)comparePosition:(UITextPosition *)position toPosition:(UITextPosition *)other;
{
CCLOG("comparePosition");
return 0;
}
- (NSInteger)offsetFromPosition:(UITextPosition *)from toPosition:(UITextPosition *)toPosition;
{
CCLOG("offsetFromPosition");
return 0;
}
- (UITextPosition *)positionWithinRange:(UITextRange *)range farthestInDirection:(UITextLayoutDirection)direction;
{
CCLOG("positionWithinRange");
return nil;
}
- (UITextRange *)characterRangeByExtendingPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction;
{
CCLOG("characterRangeByExtendingPosition");
return nil;
}
#pragma mark Writing direction
- (UITextWritingDirection)baseWritingDirectionForPosition:(UITextPosition *)position inDirection:(UITextStorageDirection)direction;
{
CCLOG("baseWritingDirectionForPosition");
return UITextWritingDirectionNatural;
}
- (void)setBaseWritingDirection:(UITextWritingDirection)writingDirection forRange:(UITextRange *)range;
{
CCLOG("setBaseWritingDirection");
}
#pragma mark Geometry
/* Geometry used to provide, for example, a correction rect. */
- (CGRect)firstRectForRange:(UITextRange *)range;
{
CCLOG("firstRectForRange");
return CGRectNull;
}
- (CGRect)caretRectForPosition:(UITextPosition *)position;
{
CCLOG("caretRectForPosition");
return CGRectMake(0, 0, 0, 0);
}
#pragma mark Hit testing
/* JS - Find the closest position to a given point */
- (UITextPosition *)closestPositionToPoint:(CGPoint)point;
{
CCLOG(@"closestPositionToPoint");
return nil;
}
- (UITextPosition *)closestPositionToPoint:(CGPoint)point withinRange:(UITextRange *)range;
{
CCLOG("closestPositionToPoint");
return nil;
}
- (UITextRange *)characterRangeAtPoint:(CGPoint)point;
{
CCLOG("characterRangeAtPoint");
return nil;
}
@end @end

View File

@ -176,6 +176,10 @@ void CCIMEDispatcher::removeDelegate(CCIMEDelegate* pDelegate)
} while (0); } while (0);
} }
//////////////////////////////////////////////////////////////////////////
// dispatch text message
//////////////////////////////////////////////////////////////////////////
void CCIMEDispatcher::dispatchInsertText(const char * pText, int nLen) void CCIMEDispatcher::dispatchInsertText(const char * pText, int nLen)
{ {
do do
@ -204,6 +208,78 @@ void CCIMEDispatcher::dispatchDeleteBackward()
} while (0); } while (0);
} }
//////////////////////////////////////////////////////////////////////////
// dispatch keyboard message
//////////////////////////////////////////////////////////////////////////
void CCIMEDispatcher::dispatchKeyboardWillShow(CCRect& begin, CCRect& end)
{
if (m_pImpl)
{
CCIMEDelegate * pDelegate = 0;
DelegateIter last = m_pImpl->m_DelegateList.end();
for (DelegateIter first = m_pImpl->m_DelegateList.begin(); first != last; ++first)
{
pDelegate = *(first);
if (pDelegate)
{
pDelegate->keyboardWillShow(begin, end);
}
}
}
}
void CCIMEDispatcher::dispatchKeyboardDidShow(CCRect& begin, CCRect& end)
{
if (m_pImpl)
{
CCIMEDelegate * pDelegate = 0;
DelegateIter last = m_pImpl->m_DelegateList.end();
for (DelegateIter first = m_pImpl->m_DelegateList.begin(); first != last; ++first)
{
pDelegate = *(first);
if (pDelegate)
{
pDelegate->keyboardDidShow(begin, end);
}
}
}
}
void CCIMEDispatcher::dispatchKeyboardWillHide(CCRect& begin, CCRect& end)
{
if (m_pImpl)
{
CCIMEDelegate * pDelegate = 0;
DelegateIter last = m_pImpl->m_DelegateList.end();
for (DelegateIter first = m_pImpl->m_DelegateList.begin(); first != last; ++first)
{
pDelegate = *(first);
if (pDelegate)
{
pDelegate->keyboardWillHide(begin, end);
}
}
}
}
void CCIMEDispatcher::dispatchKeyboardDidHide(CCRect& begin, CCRect& end)
{
if (m_pImpl)
{
CCIMEDelegate * pDelegate = 0;
DelegateIter last = m_pImpl->m_DelegateList.end();
for (DelegateIter first = m_pImpl->m_DelegateList.begin(); first != last; ++first)
{
pDelegate = *(first);
if (pDelegate)
{
pDelegate->keyboardDidHide(begin, end);
}
}
}
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// protected member function // protected member function
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////