Skip to main content

History: iOS Input via UIView

Source of version: 2

Copy to clipboard
            {TOC(maxdepth="1",pagename="iOS: Getting Input via UIView")}{TOC}


{CODE(caption="UIInputPimpl.h",wrap="0",colors="c")}#ifndef UIInputPimpl_h
#define UIInputPimpl_h

class UIInputPimpl
{  
public:
    UIInputPimpl ( void );
    virtual ~UIInputPimpl( void );
	
    void _setInputEnabled( bool value );

    virtual void _notifyTouchBegan( float absX, float absY ) = 0;
    virtual void _notifyTouchMoved( float absX, float absY, float relX, float relY ) = 0;
    virtual void _notifyTouchEnded( float absX, float absY ) = 0;
    virtual void _notifyTouchCancelled( float absX, float absY ) = 0;

protected:
    void _init( int inputAreaX, int inputAreaY, int inputAreaWidth, int inputAreaHeight );
	
private:
    void * self;
    void * window;
};

////////////////////////////////////////////////////////////////////////

#endif{CODE}

{CODE(caption="iOS_UIInputImpl.h",colors="c")}#ifndef iOS_UIInputImpl_h
#define iOS_UIInputImpl_h

#import "UIInputPimpl.h"
#import <UIKit/UIKit.h>

////////////////////////////////////////////////////////////////////////

@interface iOS_UIInputImpl : UIView
{
    UIInputPimpl * _pimpl;
}

- (void) setPimpl :(UIInputPimpl *)pimpl;

@end

////////////////////////////////////////////////////////////////////////

#endif{CODE}

{CODE(caption="iOS_UIInputImpl.mm",colors="c")}#import "UIInputPimpl.h"
#import "iOS_UIInputImpl.h"

////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
@interface iOS_UIInputImpl()

@end
////////////////////////////////////////////////////////////////////////


@implementation iOS_UIInputImpl


////////////////////////////////////////////////////////////////////////

UIInputPimpl::UIInputPimpl( void )
    : self( NULL )
{    }

////////////////////////////////////////////////////////////////////////

UIInputPimpl::~UIInputPimpl( void )
{
    if ( self )
    {
        [(id)self removeFromSuperview];
    }
    if ( window )
    {
        [(id)window dealloc];
    }
    if ( self )
    {
        [(id)self dealloc];
        self = NULL;
    }
}

////////////////////////////////////////////////////////////////////////

void UIInputPimpl::_init ( int inputAreaX, int inputAreaY, int inputAreaWidth, int inputAreaHeight )
{
    CGRect viewRect = CGRectMake( inputAreaX, inputAreaY, inputAreaWidth, inputAreaHeight );
    self  = [[iOS_UIInputImpl alloc] initWithFrame:viewRect];

    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    ((UIWindow*)window).windowLevel = UIWindowLevelStatusBar;
    ((UIWindow*)window).hidden = NO;
    ((UIWindow*)window).backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
    [(id)window makeKeyAndVisible];
    [(id)window addSubview:(UIView*)self];

    [(id)self setPimpl:this];
}

////////////////////////////////////////////////////////////////////////

void UIInputPimpl::_setInputEnabled( bool value )
{
    ((UIView*)self).userInteractionEnabled = value;
}

////////////////////////////////////////////////////////////////////////






////////////////////////////////////////////////////////////////////////

-(void) setPimpl:(UIInputPimpl *)pimpl
{
    _pimpl = pimpl;
}

////////////////////////////////////////////////////////////////////////

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for ( UITouch * touch in touches )
    {
        CGPoint p = [touch locationInView:self];
        _pimpl->_notifyTouchBegan( p.x, p.y );
    }
}

////////////////////////////////////////////////////////////////////////

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for ( UITouch * touch in touches )
    {
        CGPoint p0 = [touch previousLocationInView:self];
        CGPoint p1 = [touch locationInView:self];
        _pimpl->_notifyTouchMoved( p1.x, p1.y, p1.x - p0.x, p1.y - p0.y );
    }	
}

////////////////////////////////////////////////////////////////////////

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    for ( UITouch * touch in touches )
    {
        CGPoint p = [touch locationInView:self];
        _pimpl->_notifyTouchEnded( p.x, p.y );
    }
}

////////////////////////////////////////////////////////////////////////

-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    for ( UITouch * touch in touches )
    {
        CGPoint p = [touch locationInView:self];
        _pimpl->_notifyTouchCancelled( p.x, p.y );
    }
}

////////////////////////////////////////////////////////////////////////

- (void) dealloc
{
    [super dealloc];
}

@end{CODE}

{CODE(caption="UIInput.h",colors="c")}#ifndef UIInput_h
#define UIInput_h

#include "UIInputPimpl.h"

class UIInput : private UIInputPimpl
{  
public:
    UIInput( void );
    virtual ~UIInput( void );

    void setInputEnabled( bool value );

    virtual void _notifyTouchBegan( float absX, float absY );
    virtual void _notifyTouchMoved( float absX, float absY, float relX, float relY );
    virtual void _notifyTouchEnded( float absX, float absY );
    virtual void _notifyTouchCancelled( float absX, float absY );
};

////////////////////////////////////////////////////////////////////////


#endif{CODE}


{CODE(caption="UIInput.cpp",colors="c")}
#include <Ogre.h>

////////////////////////////////////////////////////////////////////////

UIInput::UIInput( void )
{
    _init( 0, 0, 1024, 768 );
}

////////////////////////////////////////////////////////////////////////

UIInput::~UIInput( void )
{    }

////////////////////////////////////////////////////////////////////////

void UIInput::setInputEnabled( bool value )
{
    _setInputEnabled( value );
}

////////////////////////////////////////////////////////////////////////

void UIInput::_notifyTouchBegan( float absX, float absY )
{
    // Dispatch event or handle internally.
}

////////////////////////////////////////////////////////////////////////

void UIInput::_notifyTouchMoved( float absX, float absY, float relX, float relY )
{
    // Dispatch event or handle internally.
}

////////////////////////////////////////////////////////////////////////

void UIInput::_notifyTouchEnded( float absX, float absY )
{
    // Dispatch event or handle internally.
}

////////////////////////////////////////////////////////////////////////

void UIInput::_notifyTouchCancelled( float absX, float absY )
{
    // Dispatch event or handle internally.
}
{CODE}
        

History

Information Version
Thu 27 of Sep, 2012 20:25 GMT-0000 aleroy Updated with an improved version of the code. 4
Mon 09 of Apr, 2012 16:06 GMT-0000 Arkiruthis Minor edit. UIInput.cpp code just needs the missing #include at the top. 3
Tue 03 of Apr, 2012 11:19 GMT-0000 aleroy 2
Tue 03 of Apr, 2012 03:35 GMT-0000 aleroy Initial draft; still needs comments/explanations. I'll try to get to this in the next couple days, but feel free to edit if you get there before I do ... oh, and I'd also like to see gesture and orien 1