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.

Defined Under Namespace

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#timestampInteger

timestamp of the event

Returns:

  • (Integer)

#typeInteger

SDL’s internal event type enum

Returns:

  • (Integer)

Class Method Details

.enable=(bool) ⇒ Boolean

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:

  • (Boolean)

See Also:



158
159
160
161
162
163
164
165
# File 'event.c', line 158

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;
}

.enabled?Boolean

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=


133
134
135
136
137
138
139
140
141
# File 'event.c', line 133

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);
}

.pollSDL2::Event?

Poll for currently pending events.

Returns:

  • (SDL2::Event)

    next event from the queue

  • (nil)

    the queue is empty



115
116
117
118
119
120
121
122
123
# File 'event.c', line 115

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

Instance Method Details

#inspectString

Returns inspection string

Returns:

  • (String)

    inspection string



215
216
217
218
219
220
# File 'event.c', line 215

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);
}

#windowSDL2::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.



234
235
236
237
238
# File 'event.c', line 234

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