Oolite 1.91.0.7646-241128-10e222e
Loading...
Searching...
No Matches
OOJoystickSplineAxisProfile Class Reference

#include <OOJoystickProfile.h>

+ Inheritance diagram for OOJoystickSplineAxisProfile:
+ Collaboration diagram for OOJoystickSplineAxisProfile:

Instance Methods

(id) - init
 
(void) - dealloc
 
(id) - copyWithZone:
 
(int- addControl:
 
(NSPoint) - pointAtIndex:
 
(int- countPoints
 
(void) - removeControl:
 
(void) - clearControlPoints
 
(void) - moveControl:point:
 
(double) - rawValue:
 
(double) - gradient:
 
(NSArray *) - controlPoints
 
(BOOL) - makeSegments [implementation]
 
- Instance Methods inherited from OOJoystickAxisProfile
(double) - value:
 
(double) - deadzone
 
(void) - setDeadzone:
 

Private Attributes

NSMutableArray * controlPoints
 
NSArray * segments
 

Detailed Description

Definition at line 72 of file OOJoystickProfile.h.

Method Documentation

◆ addControl:

- (int) addControl: (NSPoint) point

Definition at line 39 of file OOJoystickProfile.m.

379{
380 NSPoint left, right;
381 NSUInteger i;
382
383 if (point.x <= SPLINE_POINT_MIN_SPACING || point.x >= 1 - SPLINE_POINT_MIN_SPACING )
384 {
385 return -1;
386 }
387
388 left.x = 0.0;
389 left.y = 0.0;
390 for (i = 0; i <= [controlPoints count]; i++ )
391 {
392 if (i < [controlPoints count])
393 {
394 right = [[controlPoints objectAtIndex: i] pointValue];
395 }
396 else
397 {
398 right = NSMakePoint(1.0,1.0);
399 }
400 if ((point.x - left.x) < SPLINE_POINT_MIN_SPACING)
401 {
402 if (i == 0)
403 {
404 return -1;
405 }
406 [controlPoints replaceObjectAtIndex: i - 1 withObject: [NSValue valueWithPoint: point]];
407 [self makeSegments];
408 return i - 1;
409 }
410 if ((right.x - point.x) >= SPLINE_POINT_MIN_SPACING)
411 {
412 [controlPoints insertObject: [NSValue valueWithPoint: point] atIndex: i];
413 [self makeSegments];
414 return i;
415 }
416 left = right;
417 }
418 return -1;
419}
420
#define SPLINE_POINT_MIN_SPACING
unsigned count

◆ clearControlPoints

- (void) clearControlPoints

Definition at line 39 of file OOJoystickProfile.m.

529{
530 [controlPoints removeAllObjects];
531 [self makeSegments];
532}
533

◆ controlPoints

- (NSArray *) controlPoints

Definition at line 39 of file OOJoystickProfile.m.

448{
449 return [NSArray arrayWithArray: controlPoints];
450}
451

◆ copyWithZone:

- (id) copyWithZone: (NSZone *) zone

Reimplemented from OOJoystickAxisProfile.

Definition at line 39 of file OOJoystickProfile.m.

370{
371 OOJoystickSplineAxisProfile *copy = [[[self class] alloc] init];
372 copy->controlPoints = [controlPoints copyWithZone: zone];
373 copy->segments = [segments copyWithZone: zone];
374 return copy;
375}
376

◆ countPoints

- (int) countPoints

Definition at line 39 of file OOJoystickProfile.m.

442{
443 return [controlPoints count];
444}
445

◆ dealloc

- (void) dealloc

Definition at line 39 of file OOJoystickProfile.m.

362{
363 [controlPoints release];
364 [segments release];
365 [super dealloc];
366 return;
367}
368

◆ gradient:

- (double) gradient: (double) x

Definition at line 39 of file OOJoystickProfile.m.

612{
613 NSUInteger i;
615 for (i = 0; i < [segments count]; i++)
616 {
617 segment = [segments objectAtIndex: i];
618 if ([segment end] > x)
619 {
620 return [segment gradient:x];
621 }
622 }
623 return 1.0;
624}
625
float x

◆ init

- (id) init

Reimplemented from OOJoystickAxisProfile.

Definition at line 39 of file OOJoystickProfile.m.

351{
352 if ((self = [super init]))
353 {
354 controlPoints = [[NSMutableArray alloc] initWithCapacity: 2];
355 segments = nil;
356 [self makeSegments];
357 }
358 return self;
359}
360
return nil

◆ makeSegments

- (BOOL) makeSegments
implementation

Provided by category OOJoystickSplineAxisProfile(Private).

Definition at line 39 of file OOJoystickProfile.m.

454{
455 NSUInteger i;
456 NSPoint left, right, next;
457 double gradientleft, gradientright;
459 BOOL first_segment = YES;
460 NSMutableArray *new_segments = [NSMutableArray arrayWithCapacity: ([controlPoints count] + 1)];
461
462 left.x = 0.0;
463 left.y = 0.0;
464 if ([controlPoints count] == 0)
465 {
466 right.x = 1.0;
467 right.y = 1.0;
468 segment = [OOJoystickSplineSegment segmentWithData: left right: right];
469 [new_segments addObject:segment];
470 }
471 else
472 {
473 gradientleft = 1.0;
474 right = [[controlPoints objectAtIndex: 0] pointValue];
475 for (i = 0; i < [controlPoints count]; i++)
476 {
477 next = [self pointAtIndex: i + 1];
478 if (next.x - left.x > 0.0)
479 {
480 // we make the gradient at right equal to the gradient of a straight line between the neighcouring points
481 gradientright = (next.y - left.y)/(next.x - left.x);
482 if (first_segment)
483 {
484 segment = [OOJoystickSplineSegment segmentWithData: left right: right gradientright: gradientright];
485 }
486 else
487 {
488 segment = [OOJoystickSplineSegment segmentWithData: left right: right gradientleft: gradientleft gradientright: gradientright];
489 }
490 if (segment == nil)
491 {
492 return NO;
493 }
494 else
495 {
496 [new_segments addObject: segment];
497 gradientleft = gradientright;
498 first_segment = NO;
499 left = right;
500 }
501 }
502 right = next;
503 }
504 right.x = 1.0;
505 right.y = 1.0;
506 segment = [OOJoystickSplineSegment segmentWithData: left right: right gradientleft: gradientleft];
507 if (segment == nil)
508 {
509 return NO;
510 }
511 [new_segments addObject: segment];
512 }
513 [segments release];
514 segments = [[NSArray arrayWithArray: new_segments] retain];
515 return YES;
516}
517
id segmentWithData:right:(NSPoint left,[right] NSPoint right)
id segmentWithData:right:gradientleft:(NSPoint left,[right] NSPoint right,[gradientleft] double gradientleft)

◆ moveControl:point:

- (void) moveControl: (NSInteger) index
point: (NSPoint) point 

Definition at line 39 of file OOJoystickProfile.m.

535{
536 NSPoint left, right;
537
538 point.x = OOClamp_0_1_d(point.x);
539 point.y = OOClamp_0_1_d(point.y);
540 if (index < 0 || index >= (NSInteger)[controlPoints count])
541 {
542 return;
543 }
544 if (index == 0)
545 {
546 left.x = 0.0;
547 right.x = 0.0;
548 }
549 else
550 {
551 left = [[controlPoints objectAtIndex: (index-1)] pointValue];
552 }
553 if (index == (NSInteger)[controlPoints count] - 1)
554 {
555 right.x = 1.0;
556 right.y = 1.0;
557 }
558 else
559 {
560 right = [[controlPoints objectAtIndex: (index+1)] pointValue];
561 }
562 // preserve order of control points - if we attempt to move this control point beyond
563 // either of its neighbours, move it back inside. Also keep neighbours a distance of at least SPLINE_POINT_MIN_SPACING apart
564 if (point.x - left.x < SPLINE_POINT_MIN_SPACING)
565 {
566 point.x = left.x + SPLINE_POINT_MIN_SPACING;
567 if (right.x - point.x < SPLINE_POINT_MIN_SPACING)
568 {
569 point.x = (left.x + right.x)/2;
570 }
571 }
572 else if (right.x - point.x < SPLINE_POINT_MIN_SPACING)
573 {
574 point.x = right.x - SPLINE_POINT_MIN_SPACING;
575 if (point.x - left.x < SPLINE_POINT_MIN_SPACING)
576 {
577 point.x = (left.x + right.x)/2;
578 }
579 }
580 [controlPoints replaceObjectAtIndex: index withObject: [NSValue valueWithPoint: point]];
581 [self makeSegments];
582 return;
583}
584

◆ pointAtIndex:

- (NSPoint) pointAtIndex: (NSInteger) index

Definition at line 39 of file OOJoystickProfile.m.

422{
423 NSPoint point;
424 if (index < 0)
425 {
426 point.x = 0.0;
427 point.y = 0.0;
428 }
429 else if (index >= (NSInteger)[controlPoints count])
430 {
431 point.x = 1.0;
432 point.y = 1.0;
433 }
434 else
435 {
436 point = [[controlPoints objectAtIndex: index] pointValue];
437 }
438 return point;
439}
440

◆ rawValue:

- (double) rawValue: (double) x

Reimplemented from OOJoystickAxisProfile.

Definition at line 39 of file OOJoystickProfile.m.

586{
587 NSUInteger i;
589 double sign;
590
591 if (x < 0)
592 {
593 sign = -1.0;
594 x = -x;
595 }
596 else
597 {
598 sign = 1.0;
599 }
600 for (i = 0; i < [segments count]; i++)
601 {
602 segment = [segments objectAtIndex: i];
603 if ([segment end] > x)
604 {
605 return sign * OOClamp_0_1_d([segment value:x]);
606 }
607 }
608 return 1.0;
609}
610

◆ removeControl:

- (void) removeControl: (NSInteger) index

Definition at line 39 of file OOJoystickProfile.m.

519{
520 if (index >= 0 && index < (NSInteger)[controlPoints count])
521 {
522 [controlPoints removeObjectAtIndex: index];
523 [self makeSegments];
524 }
525 return;
526}
527

Member Data Documentation

◆ controlPoints

- (NSMutableArray*) controlPoints
private

Definition at line 75 of file OOJoystickProfile.h.

◆ segments

- (NSArray*) segments
private

Definition at line 76 of file OOJoystickProfile.h.


The documentation for this class was generated from the following files: