git.y1.nz

SameBoy

Accurate GB/GBC emulator
download: https://git.y1.nz/archives/sameboy.tar.gz
README | Files | Log | Refs | LICENSE

JoyKit/JOYEmulatedButton.m

      1 #import "JOYEmulatedButton.h"
      2 #import <AppKit/AppKit.h>
      3 
      4 @interface JOYButton ()
      5 {
      6     @public bool _state;
      7 }
      8 @end
      9 
     10 @implementation JOYEmulatedButton
     11 {
     12     uint64_t _uniqueID;
     13     JOYButtonType _type;
     14 }
     15 
     16 - (instancetype)initWithUsage:(JOYButtonUsage)usage type:(JOYButtonType)type uniqueID:(uint64_t)uniqueID;
     17 {
     18     self = [super init];
     19     self.usage = usage;
     20     _uniqueID = uniqueID;
     21     _type = type;
     22     
     23     return self;
     24 }
     25 
     26 - (uint64_t)uniqueID
     27 {
     28     return _uniqueID | (uint64_t)self.combinedIndex << 32;
     29 }
     30 
     31 - (bool)updateStateFromAxis:(JOYAxis *)axis
     32 {
     33     bool old = _state;
     34     _state = [axis value] > 0.8;
     35     return _state != old;
     36 }
     37 
     38 - (bool)updateStateFromAxes2D:(JOYAxes2D *)axes
     39 {
     40     bool old = _state;
     41     if (axes.distance < 0.5) {
     42         _state = false;
     43     }
     44     else {
     45         unsigned direction = ((unsigned)round(axes.angle / 360 * 8)) & 7;
     46         switch (self.usage) {
     47             case JOYButtonUsageDPadLeft:
     48                 _state = direction >= 3 && direction <= 5;
     49                 break;
     50             case JOYButtonUsageDPadRight:
     51                 _state = direction <= 1 || direction == 7;
     52                 break;
     53             case JOYButtonUsageDPadUp:
     54                 _state = direction >= 5;
     55                 break;
     56             case JOYButtonUsageDPadDown:
     57                 _state = direction <= 3 && direction >= 1;
     58                 break;
     59             default:
     60                 break;
     61         }
     62     }
     63     return _state != old;
     64 }
     65 
     66 - (bool)updateStateFromHat:(JOYHat *)hat
     67 {
     68     bool old = _state;
     69     if (!hat.pressed) {
     70         _state = false;
     71     }
     72     else {
     73         unsigned direction = ((unsigned)round(hat.angle / 360 * 8)) & 7;
     74         switch (self.usage) {
     75             case JOYButtonUsageDPadLeft:
     76                 _state = direction >= 3 && direction <= 5;
     77                 break;
     78             case JOYButtonUsageDPadRight:
     79                 _state = direction <= 1 || direction == 7;
     80                 break;
     81             case JOYButtonUsageDPadUp:
     82                 _state = direction >= 5;
     83                 break;
     84             case JOYButtonUsageDPadDown:
     85                 _state = direction <= 3 && direction >= 1;
     86                 break;
     87             default:
     88                 break;
     89         }
     90     }
     91     return _state != old;
     92 }
     93 
     94 - (JOYButtonType)type
     95 {
     96     return _type;
     97 }
     98 
     99 @end

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.