Class: SDL2::Event

Inherits:
Object
  • Object
show all
Defined in:
event.c,
event.c

Overview

This class represents SDL's events.

All events are represented by the instance of subclasses of this class.

Introduction of event subsystem

Event handling allows your application to receive input from the user. Event handling is initialized (along with video) with a call to:

SDL2.init(SDL2::INIT_VIDEO|SDL2::INIT_EVENTS)

Internally, SDL stores all the events waiting to be handled in an event queue. Using methods like Event.poll, you can observe and handle input events.

The queue is conceptually a sequence of objects of SDL2::Event. You can read an event from the queue with Event.poll and you can process the information from the object.

Note: peep and wait will be implemented later.

Direct Known Subclasses

ControllerAxisMotion, ControllerButton, ControllerDevice, JoyAxisMotion, JoyBallMotion, JoyButton, JoyDevice, JoyHatMotion, Keyboard, MouseButton, MouseMotion, MouseWheel, Quit, SysWM, TextEditing, TextInput, Window

Defined Under Namespace

Classes: ControllerAxisMotion, ControllerButton, ControllerButtonDown, ControllerButtonUp, ControllerDevice, ControllerDeviceAdded, ControllerDeviceRemapped, ControllerDeviceRemoved, JoyAxisMotion, JoyBallMotion, JoyButton, JoyButtonDown, JoyButtonUp, JoyDevice, JoyDeviceAdded, JoyDeviceRemoved, JoyHatMotion, KeyDown, KeyUp, Keyboard, MouseButton, MouseButtonDown, MouseButtonUp, MouseMotion, MouseWheel, Quit, SysWM, TextEditing, TextInput, Window

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Integer) timestamp

timestamp of the event

Returns:

  • (Integer)

- (Integer) type

SDL's internal event type enum

Returns:

  • (Integer)

Class Method Details

+ (bool) enabled=(bool)

Set wheter the event is enable

This method is only available for subclasses of SDL2::Event corresponding to SDL's event types.

Examples:

disable mouse wheel events

SDL2::Event::MouseWheel.enable = false

Parameters:

  • bool (Boolean)

    true for enabling the event.

Returns:

  • (bool)

See Also:



154
155
156
157
158
159
160
161
# File 'event.c', line 154

static VALUE Event_s_set_enable(VALUE self, VALUE val)
{
    VALUE event_type = rb_iv_get(self, "event_type");
    if (event_type == Qnil) 
        rb_raise(rb_eArgError, "You cannot enable %s directly", rb_class2name(self));
    SDL_EventState(NUM2INT(event_type), RTEST(val) ? SDL_ENABLE : SDL_DISABLE);
    return val;
}

+ (Boolean) enabled?

Get whether the event is enabled.

This method is available for subclasses of SDL2::Event corresponding to SDL's event types.

Returns:

  • (Boolean)

See Also:

  • enabled=


129
130
131
132
133
134
135
136
137
# File 'event.c', line 129

static VALUE Event_s_enabled_p(VALUE self)
{
    VALUE event_type = rb_iv_get(self, "event_type");
    if (event_type == Qnil) {
        rb_warn("You cannot enable %s directly", rb_class2name(self));
        return Qfalse;
    }
    return INT2BOOL(SDL_EventState(NUM2INT(event_type), SDL_QUERY) == SDL_ENABLE);
}

+ (SDL2::Event?) poll

Poll for currently pending events.

Returns:

  • (SDL2::Event)

    next event from the queue

  • (nil)

    the queue is empty



111
112
113
114
115
116
117
118
119
# File 'event.c', line 111

static VALUE Event_s_poll(VALUE self)
{
    SDL_Event ev;
    if (SDL_PollEvent(&ev)) {
        return Event_new(&ev);
    } else {
        return Qnil;
    }
}

Instance Method Details

- (String) inspect

Returns inspection string

Returns:

  • (String)

    inspection string



208
209
210
211
212
213
# File 'event.c', line 208

static VALUE Event_inspect(VALUE self)
{
    SDL_Event* ev; Data_Get_Struct(self, SDL_Event, ev);
    return rb_sprintf("<%s: type=%u timestamp=%u>",
                      rb_obj_classname(self), ev->common.type, ev->common.timestamp);
}

- (SDL2::Window) window

Return the object of Window corresponding to the window_id attribute.

Some subclasses of SDL2::Event have window_id attribute to point the window which creates the event. The type of the window_id attribute is integer, and you need to convert it with Window.find_by_id to get the Window object. This method returns the Window object.

Returns:

Raises:

  • (NoMethodError)

    raised if the window_id attribute is not present.



227
228
229
230
231
# File 'event.c', line 227

static VALUE Event_window(VALUE self)
{
    VALUE window_id = rb_funcall(self, rb_intern("window_id"), 0, 0);
    return find_window_by_id(NUM2UINT(window_id));
}