cocos2d-x-3_1_1-GLFW-Joystick.patch

v3.1.1 GLFW joystick patch - Sheado, 2014-06-04 00:32

Download (10.9 kB)

cocos2d-x-3.1.1-sheado/cocos/base/CCEvent.h 2014-06-03 15:03:26.000000000 -0700
49 49
        ACCELERATION,
50 50
        MOUSE,
51 51
        FOCUS,
52
        CUSTOM
52
        CUSTOM,
53
        JOYSTICK
53 54
    };
54 55
    
55 56
protected:
cocos2d-x-3.1.1-sheado/cocos/base/CCEventDispatcher.cpp 2014-06-03 15:03:47.000000000 -0700
31 31
#include "base/CCEventListenerKeyboard.h"
32 32
#include "base/CCEventListenerCustom.h"
33 33
#include "base/CCEventListenerFocus.h"
34
#include "base/EventListenerJoystick.h"
34 35

  
35 36
#include "2d/CCScene.h"
36 37
#include "base/CCDirector.h"
......
94 95
            // return UNKNOWN instead.
95 96
            CCASSERT(false, "Don't call this method if the event is for touch.");
96 97
            break;
98
        case Event::Type::JOYSTICK:
99
            ret = EventListenerJoystick::LISTENER_ID;
100
            break;
97 101
        default:
98 102
            CCASSERT(false, "Invalid type!");
99 103
            break;
cocos2d-x-3.1.1-sheado/cocos/base/CCEventListener.h 2014-06-03 15:04:02.000000000 -0700
55 55
        MOUSE,
56 56
        ACCELERATION,
57 57
        FOCUS,
58
        CUSTOM
58
        CUSTOM,
59
        JOYSTICK
59 60
    };
60 61

  
61 62
    typedef std::string ListenerID;
cocos2d-x-3.1.1-sheado/cocos/base/EventJoystick.cpp 2014-05-22 13:36:20.000000000 -0700
1
//
2
//  EventJoystick.cpp
3
//  cocos2d_libs
4
//
5
//  Created by Chad Ata on 5/21/14.
6
//
7
//
8

  
9
#include "EventJoystick.h"
10
#include "cocos2d.h"
11
NS_CC_BEGIN
12
EventJoystick::EventJoystick()
13
    : Event(Type::JOYSTICK)
14
{
15
    _isPresent = false;
16
    name = NULL;
17
    buttonValuesCount = 0;
18
    buttonValues = NULL;
19
    axesCount = 0;
20
    axes = NULL;
21

  
22
}
23

  
24
void EventJoystick::setPresent( bool present )
25
{
26
    this->_isPresent = present;
27
}
28

  
29
void EventJoystick::setName( const char* n )
30
{
31
    this->name = n;
32
}
33

  
34
void EventJoystick::setId(int i)
35
{
36
    this->id = i;
37
}
38

  
39
void EventJoystick::setButtonValues( int count, const unsigned char* bv )
40
{
41
    this->buttonValuesCount = count;
42
    this->buttonValues = bv;
43
}
44

  
45
void EventJoystick::setAxes( int count, const float* a )
46
{
47
    this->axesCount = count;
48
    this->axes = a;
49
}
50

  
51
bool EventJoystick::isPresent()
52
{
53
    return _isPresent;
54
}
55

  
56
const char* EventJoystick::getName()
57
{
58
    return name;
59
}
60

  
61
int EventJoystick::getId()
62
{
63
    return id;
64
}
65

  
66
const unsigned char* EventJoystick::getButtonValues( int* count )
67
{
68
    *count = buttonValuesCount;
69
    return buttonValues;
70
}
71

  
72
const float* EventJoystick::getAxes( int* count )
73
{
74
    *count = axesCount;
75
    return axes;
76
}
77

  
78
NS_CC_END
cocos2d-x-3.1.1-sheado/cocos/base/EventJoystick.h 2014-05-22 13:35:33.000000000 -0700
1
//
2
//  EventJoystick.h
3
//  cocos2d_libs
4
//
5
//  Created by Chad Ata on 5/21/14.
6
//
7
//
8

  
9
#ifndef __cocos2d_libs__EventJoystick__
10
#define __cocos2d_libs__EventJoystick__
11
#include "base/CCEvent.h"
12
#include "glfw3.h"
13

  
14
NS_CC_BEGIN
15
/**
16
 * Data returned in this class is based off of the GLFW joystick API
17
 * http://www.glfw.org/docs/latest/group__input.html#gaffcbd9ac8ee737fcdd25475123a3c790
18
 */
19
class EventJoystick : public Event
20
{
21
public:
22
    enum BUTTON_STATES
23
    {
24
        RELEASED = GLFW_RELEASE
25
    };
26
    
27
    EventJoystick();
28
    
29
    void setPresent( bool present );
30
    void setName( const char* name );
31
    void setId( int id );
32
    void setButtonValues( int count, const unsigned char* buttonValues );
33
    void setAxes( int count, const float* axes );
34
    
35
    bool isPresent();
36
    const char* getName();
37
    int getId();
38
    const unsigned char* getButtonValues( int* count );
39
    const float* getAxes( int* count );
40
    
41
private:
42
    bool _isPresent;
43
    const char* name;
44
    int id;
45
    int buttonValuesCount;
46
    const unsigned char* buttonValues;
47
    // e.g. ps3: left: x,y right:x,y x (left)-1<=x<=1(right) (up)-1<=y<=1(down)
48
    int axesCount;
49
    const float* axes;
50
};
51
NS_CC_END
52
#endif /* defined(__cocos2d_libs__EventJoystick__) */
cocos2d-x-3.1.1-sheado/cocos/base/EventListenerJoystick.cpp 2014-05-22 13:46:08.000000000 -0700
1
//
2
//  EventListenerJoystick.cpp
3
//  cocos2d_libs
4
//
5
//  Created by Chad Ata on 5/21/14.
6
//
7
//
8

  
9
#include "EventListenerJoystick.h"
10
#include "cocos2d.h"
11
NS_CC_BEGIN
12

  
13
const std::string EventListenerJoystick::LISTENER_ID = "__cc_joystick";
14

  
15
bool EventListenerJoystick::checkAvailable()
16
{
17
    return true;
18
}
19

  
20
EventListenerJoystick* EventListenerJoystick::clone()
21
{
22
    auto ret = new EventListenerJoystick();
23
    if (ret && ret->init())
24
    {
25
        ret->autorelease();
26
        ret->onEvent = onEvent;
27
    }
28
    else
29
    {
30
        CC_SAFE_DELETE(ret);
31
    }
32
    return ret;
33
}
34
EventListenerJoystick* EventListenerJoystick::create()
35
{
36
    auto ret = new EventListenerJoystick();
37
    if (ret && ret->init())
38
    {
39
        ret->autorelease();
40
    }
41
    else
42
    {
43
        CC_SAFE_DELETE(ret);
44
    }
45
    return ret;
46
}
47

  
48

  
49
EventListenerJoystick::EventListenerJoystick()
50
: onEvent(nullptr)
51
{
52
}
53

  
54
bool EventListenerJoystick::init()
55
{
56
    auto listener = [this](Event* event){
57
                if(onEvent != nullptr)
58
                    onEvent(event);
59
    };
60
    
61
    if (EventListener::init(Type::JOYSTICK, LISTENER_ID, listener))
62
    {
63
        return true;
64
    }
65
    
66
    return false;
67
}
68

  
69
NS_CC_END
cocos2d-x-3.1.1-sheado/cocos/base/EventListenerJoystick.h 2014-05-22 13:46:08.000000000 -0700
1
//
2
//  EventListenerJoystick.h
3
//  cocos2d_libs
4
//
5
//  Created by Chad Ata on 5/21/14.
6
//
7
//
8

  
9
#ifndef __cocos2d_libs__EventListenerJoystick__
10
#define __cocos2d_libs__EventListenerJoystick__
11

  
12
#include "base/CCEventListener.h"
13
#include "base/EventJoystick.h"
14

  
15
NS_CC_BEGIN
16

  
17
class Event;
18

  
19
class EventListenerJoystick : public EventListener
20
{
21
public:
22
    static const std::string LISTENER_ID;
23
    static EventListenerJoystick* create();
24

  
25
    /// Overrides
26
    virtual EventListenerJoystick* clone() override;
27
    virtual bool checkAvailable() override;
28

  
29
    std::function<void(Event* event)> onEvent;
30

  
31
private:
32
    EventListenerJoystick();
33
    bool init();
34
};
35

  
36
NS_CC_END
37

  
38

  
39

  
40
#endif /* defined(__cocos2d_libs__EventListenerJoystick__) */
cocos2d-x-3.1.1-sheado/cocos/cocos2d.h 2014-06-03 15:09:59.000000000 -0700
71 71
#include "base/CCEventListenerMouse.h"
72 72
#include "base/CCEventMouse.h"
73 73
#include "base/CCEventAcceleration.h"
74
#include "base/EventListenerJoystick.h"
74 75
#include "base/CCEventListenerAcceleration.h"
75 76
#include "base/CCEventCustom.h"
76 77
#include "base/CCEventListenerCustom.h"
cocos2d-x-3.1.1-sheado/cocos/platform/desktop/CCGLView.cpp 2014-06-03 15:02:12.000000000 -0700
29 29
#include "base/CCEventDispatcher.h"
30 30
#include "base/CCEventKeyboard.h"
31 31
#include "base/CCEventMouse.h"
32
#include "base/EventJoystick.h"
32 33
#include "base/CCIMEDispatcher.h"
33 34

  
34 35
#include <unordered_map>
......
350 351
    glfwSetWindowPosCallback(_mainWindow, GLFWEventHandler::onGLFWWindowPosCallback);
351 352
    glfwSetFramebufferSizeCallback(_mainWindow, GLFWEventHandler::onGLFWframebuffersize);
352 353
    glfwSetWindowSizeCallback(_mainWindow, GLFWEventHandler::onGLFWWindowSizeFunCallback);
353

  
354
    
354 355
    setFrameSize(rect.size.width, rect.size.height);
355 356

  
356 357
    // check OpenGL version at first
......
434 435
void GLView::pollEvents()
435 436
{
436 437
    glfwPollEvents();
438
    pollJoystickEvent(GLFW_JOYSTICK_1);
439
    pollJoystickEvent(GLFW_JOYSTICK_2);
437 440
}
438 441

  
442
// TODO - joystick- optimize - cache joystick availability and name
443
// TODO - joystick - add key mapping support - follow SDL's example so that we can pull default mapping when publishing on Steam
444
void GLView::pollJoystickEvent(int id)
445
{
446
    int count = 0;
447
    bool isPresent = glfwJoystickPresent(id);
448
    EventJoystick event;
449
    if( isPresent )
450
    {
451
        event.setPresent(true);
452
        event.setId(id);
453
        const char* name = glfwGetJoystickName(id);
454
        event.setName(name);
455
        const unsigned char* values = glfwGetJoystickButtons(id, &count);
456
        event.setButtonValues(count, values);
457
        // ps3: left: x,y right:x,y x (left)-1<=x<=1(right) (up)-1<=y<=1(down)
458
        const float* axes = glfwGetJoystickAxes(id, &count);
459
        event.setAxes(count, axes);
460
    }
461
    Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
462
}
439 463

  
440 464
void GLView::enableRetina(bool enabled)
441 465
{
cocos2d-x-3.1.1-sheado/cocos/platform/desktop/CCGLView.h 2014-06-03 15:18:46.000000000 -0700
118 118

  
119 119
private:
120 120
    CC_DISALLOW_COPY_AND_ASSIGN(GLView);
121
    void pollJoystickEvent(int id);
121 122
};
122 123

  
123 124
NS_CC_END   // end of namespace   cocos2d