Skip to content

Commit

Permalink
Adição dos eventos de mouse.
Browse files Browse the repository at this point in the history
  • Loading branch information
edsomjr committed May 21, 2016
1 parent 92b3fd9 commit e1de17b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 58 deletions.
2 changes: 2 additions & 0 deletions include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define IJENGINE_KERNEL_H

#include "event.h"
#include "mouse_event.h"
#include "system_event.h"
#include "keyboard_event.h"

Expand All @@ -28,6 +29,7 @@ namespace ijengine {
virtual void play_audio_from_path(const string& title) = 0;
virtual void stop_audio() = 0;

virtual list<MouseEvent> pending_mouse_events(unsigned now) = 0;
virtual list<SystemEvent> pending_system_events(unsigned now) = 0;
virtual list<KeyboardEvent> pending_keyboard_events(unsigned now) = 0;

Expand Down
92 changes: 46 additions & 46 deletions kernel/sdl2/sdl2kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,57 +283,57 @@ SDL2Kernel::pending_keyboard_events(unsigned now)

return events;
}
/*
case SDL_MOUSEBUTTONDOWN:
{
auto p = MouseEvent(timestamp, MouseEvent::PRESSED,
button_state(event.button.button, SDL_BUTTON_LEFT),
button_state(event.button.button, SDL_BUTTON_MIDDLE),
button_state(event.button.button, SDL_BUTTON_RIGHT),
event.button.x, event.button.y, 0, 0);
events.push_back(event_t(timestamp, p.serialize()));
}
break;
case SDL_MOUSEBUTTONUP:
{
auto p = MouseEvent(timestamp, MouseEvent::RELEASED,
button_state(event.button.button, SDL_BUTTON_LEFT),
button_state(event.button.button, SDL_BUTTON_MIDDLE),
button_state(event.button.button, SDL_BUTTON_RIGHT),
event.button.x, event.button.y, 0, 0);
events.push_back(event_t(timestamp, p.serialize()));
}
break;
case SDL_MOUSEMOTION:
{
auto p = MouseEvent(timestamp, MouseEvent::MOTION,
button_state(event.button.button, SDL_BUTTON_LEFT),
button_state(event.button.button, SDL_BUTTON_MIDDLE),
button_state(event.button.button, SDL_BUTTON_RIGHT),
event.button.x, event.button.y, event.motion.xrel,
event.motion.yrel);
events.push_back(event_t(timestamp, p.serialize()));
}
break;
default:
break;
}

SDL_PumpEvents();
list<MouseEvent>
SDL2Kernel::pending_mouse_events(unsigned now)
{
update_pending_events(now);
auto it = m_events.begin();

list<MouseEvent> events;

while (it != m_events.end())
{
unsigned timestamp = it->quit.timestamp;

if (it->type == SDL_MOUSEBUTTONDOWN)
{
auto event = MouseEvent(timestamp, MouseEvent::PRESSED,
button_state(it->button.button, SDL_BUTTON_LEFT),
button_state(it->button.button, SDL_BUTTON_MIDDLE),
button_state(it->button.button, SDL_BUTTON_RIGHT),
it->button.x, it->button.y, 0, 0);

events.push_back(event);
it = m_events.erase(it);
} else if (it->type == SDL_MOUSEBUTTONUP)
{

auto event = MouseEvent(timestamp, MouseEvent::RELEASED,
button_state(it->button.button, SDL_BUTTON_LEFT),
button_state(it->button.button, SDL_BUTTON_MIDDLE),
button_state(it->button.button, SDL_BUTTON_RIGHT),
it->button.x, it->button.y, 0, 0);

events.push_back(event);
it = m_events.erase(it);
} else if (it->type == SDL_MOUSEMOTION)
{
auto event = MouseEvent(timestamp, MouseEvent::MOTION,
button_state(it->button.button, SDL_BUTTON_LEFT),
button_state(it->button.button, SDL_BUTTON_MIDDLE),
button_state(it->button.button, SDL_BUTTON_RIGHT),
it->button.x, it->button.y, it->motion.xrel, it->motion.yrel);

events.push_back(event);
it = m_events.erase(it);
} else
++it;
}

return events;
}

*/

list<SystemEvent>
SDL2Kernel::pending_system_events(unsigned now)
{
Expand Down
1 change: 1 addition & 0 deletions kernel/sdl2/sdl2kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SDL2Kernel : public Kernel {
void play_audio_from_path(const string& title);
void stop_audio();

list<MouseEvent> pending_mouse_events(unsigned now);
list<SystemEvent> pending_system_events(unsigned now);
list<KeyboardEvent> pending_keyboard_events(unsigned now);

Expand Down
23 changes: 12 additions & 11 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,13 @@ namespace ijengine
game_events.merge(translate<KeyboardEvent>(keyboard_events,
translators));

auto mouse_events = kernel->pending_mouse_events(now);
game_events.merge(translate<MouseEvent>(mouse_events,
translators));

if (game_events.size() > 0)
printf("%lu events found\n", game_events.size());
/*
list<game_event_t> game_events;

for (auto translator : translators)
{
auto more = translator->translate(events);
game_events.merge(more);
if (events.empty())
break;
}
*/
game_events.sort();

for (auto event : game_events)
Expand All @@ -144,6 +137,14 @@ try {
} catch (Exception e)
{
}
try {
auto x = event.get_property<double>("x");
auto y = event.get_property<double>("y");
printf("prop: [%s] = (%.2f, %2.f)\n", "position", x, y);
} catch (Exception e)
{
}

for (auto listener : listeners)
if (listener->on_event(event))
break;
Expand Down
17 changes: 16 additions & 1 deletion test/include/test_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <ijengine/game.h>
#include <ijengine/engine.h>
#include <ijengine/mouse_event.h>
#include <ijengine/system_event.h>
#include <ijengine/keyboard_event.h>
#include <ijengine/events_translator.h>
Expand All @@ -16,6 +17,8 @@ namespace ijengine
namespace game_event
{
const unsigned MOVEMENT = GameEvent::assign_id();
const unsigned MOTION = GameEvent::assign_id();
const unsigned CLICK = GameEvent::assign_id();
}
}

Expand All @@ -30,7 +33,19 @@ class TestGame {
class Translator : public EventsTranslator
{
bool
translate(GameEvent& to, const MouseEvent& from) { return false; }
translate(GameEvent& to, const MouseEvent& from)
{
to.set_timestamp(from.timestamp());
to.set_property<double>("x", from.x());
to.set_property<double>("y", from.y());

if (from.state() == MouseEvent::MOTION)
to.set_id(game_event::MOTION);
else
to.set_id(game_event::CLICK);

return true;
}

bool
translate(GameEvent& to, const SystemEvent& from)
Expand Down

0 comments on commit e1de17b

Please sign in to comment.