TUIControl tracking and events #103

Closed
avaidyam opened this Issue Apr 20, 2012 · 2 comments

2 participants

@avaidyam

I've patched up some more of the event handling that UIControl does into TUIControl... as I don't have a git fork for TwUI, I'm going to just post the update here:

- (void)mouseDown:(NSEvent *)event {
    if(!_controlFlags.disabled) {
        [super mouseDown:event];

        BOOL track = [self beginTrackingWithTouch:event withEvent:event];
        if(track && !_controlFlags.tracking) {
            [self _stateWillChange];
            _controlFlags.tracking = 1;
            [self _stateDidChange];
        } else if(!track && _controlFlags.tracking) {
            [self _stateWillChange];
            _controlFlags.tracking = 0;
            [self _stateDidChange];
        }

        if(_controlFlags.tracking) {            
            TUIControlEvents currentEvents = TUIControlEventTouchDown;
            if([event clickCount] < 2)
                currentEvents |= TUIControlEventTouchDownRepeat;

            [self sendActionsForControlEvents:currentEvents];
            [self setNeedsDisplay];
        }
    }
}

- (void)mouseDragged:(NSEvent *)event {
    if(!_controlFlags.disabled) {
        [super mouseDragged:event];

        if(_controlFlags.tracking) {
            BOOL track = [self continueTrackingWithTouch:event withEvent:event];
            if(track && !_controlFlags.tracking) {
                [self _stateWillChange];
                _controlFlags.tracking = 1;
                [self _stateDidChange];
            } else if(!track && _controlFlags.tracking) {
                [self _stateWillChange];
                _controlFlags.tracking = 0;
                [self _stateDidChange];
            }

            if(_controlFlags.tracking) {
                TUIControlEvents currentEvents = (([self eventInside:event])? 
                                                 TUIControlEventTouchDragInside : 
                                                 TUIControlEventTouchDragOutside);

                [self sendActionsForControlEvents:currentEvents];
                [self setNeedsDisplay];
            }
        }
    }
}

- (void)mouseUp:(NSEvent *)event {
    if(!_controlFlags.disabled) {
        [super mouseUp:event];

        if(_controlFlags.tracking) {
            [self endTrackingWithTouch:event withEvent:event];

            TUIControlEvents currentEvents = (([self eventInside:event])? 
                                             TUIControlEventTouchUpInside : 
                                             TUIControlEventTouchUpOutside);

            [self sendActionsForControlEvents:currentEvents];
            [self setNeedsDisplay];

            [self _stateWillChange];
            _controlFlags.tracking = 0;
            [self _stateDidChange];
        }
    }
}

// Support tracking cancelation.
- (void)willMoveToSuperview:(TUIView *)newSuperview {
    if(!_controlFlags.disabled && _controlFlags.tracking) {
        [self _stateWillChange];
        _controlFlags.tracking = 0;
        [self _stateDidChange];

        [self cancelTrackingWithEvent:nil];
        [self setNeedsDisplay];
    }
}

- (void)willMoveToWindow:(TUINSWindow *)newWindow {
    if(!_controlFlags.disabled && _controlFlags.tracking) {
        [self _stateWillChange];
        _controlFlags.tracking = 0;
        [self _stateDidChange];

        [self cancelTrackingWithEvent:nil];
        [self setNeedsDisplay];
    }
}

- (BOOL)beginTrackingWithTouch:(NSEvent *)touch withEvent:(NSEvent *)event {
    // Override.
    return YES;
}

- (BOOL)continueTrackingWithTouch:(NSEvent *)touch withEvent:(NSEvent *)event {
    // Override.
    return YES;
}

- (void)endTrackingWithTouch:(NSEvent *)touch withEvent:(NSEvent *)event {
    // Override.
}

- (void)cancelTrackingWithEvent:(NSEvent *)event {
    // Override.
}
@joshaber
Twitter, Inc. member

Could you give me a bit more info on what this fixes?

@avaidyam

This doesn't "fix" anything per-se. This bit of code effectively allowed me to copy-paste-rename nearly all iOS controls and have them work near damn perfectly (before fixes that made them work perfectly, that is) on TwUI. It's just an iOS compatibility sort of thing, but it makes writing controls a lot easier.

@avaidyam avaidyam closed this Sep 4, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment