tests/utils/fake_event_source.cpp

Go to the documentation of this file.
00001 /* $Id: fake_event_source.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by Pauli Nieminen <paniemin@cc.hut.fi>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-test"
00017 
00018 #include "tests/utils/fake_event_source.hpp"
00019 
00020 #include "mouse_handler_base.hpp"
00021 
00022 namespace test_utils {
00023     /**
00024      * Base class for all event nodes to be used to fire fake input events
00025      **/
00026     event_node::event_node(const size_t time, const SDL_Event& event) : time_(time), fired_(false), event_(event)
00027     {}
00028 
00029     event_node::~event_node()
00030     { }
00031 
00032     void event_node::fire_event()
00033     {
00034         const int number_of_events = 1;
00035         const Uint32 mask = 0;
00036         SDL_PeepEvents(&event_, number_of_events, SDL_ADDEVENT, mask);
00037         fired_ = true;
00038     }
00039 
00040     /**
00041      * @return true if this should stop firing events
00042      **/
00043     bool event_node::test_if_should_fire(const size_t frame_count) const
00044     {
00045         return frame_count >= time_;
00046     }
00047 
00048     bool event_node::is_fired() const
00049     {
00050         return fired_;
00051     }
00052 
00053     /**
00054      * We want the smallestat the top
00055      **/
00056     bool event_node::operator<(const event_node& o) const
00057     { return time_ > o.time_; }
00058 
00059     event_node_keyboard::event_node_keyboard(size_t time, SDL_Event& event) : event_node(time,event)
00060     {}
00061     void event_node_keyboard::fire_event()
00062     {
00063         event_node::fire_event();
00064         static int num_keys = 300;
00065         Uint8* key_list = SDL_GetKeyState( &num_keys );
00066         if (event_.type == SDL_KEYDOWN)
00067         {
00068             key_list[event_.key.keysym.sym] = 1;
00069         }
00070         else
00071             key_list[event_.key.keysym.sym] = 0;
00072     }
00073 
00074     event_node_mouse_motion::event_node_mouse_motion(size_t time, SDL_Event& event) : event_node(time,event)
00075     {}
00076     void event_node_mouse_motion::fire_event()
00077     {
00078         SDL_WarpMouse(event_.motion.x,event_.motion.y);
00079     }
00080 
00081     event_node_mouse_click::event_node_mouse_click(size_t time, SDL_Event& event) : event_node(time,event)
00082     {}
00083     void event_node_mouse_click::fire_event()
00084     {
00085         // We have to use temporaries because of difference
00086         // in types for mouse position.
00087         int x, y;
00088         SDL_GetMouseState(&x, &y);
00089         event_.button.x = static_cast<Uint16>(x);
00090         event_.button.y = static_cast<Uint16>(y);
00091         event_node::fire_event();
00092     }
00093 
00094 
00095     fake_event_source::fake_event_source()
00096         : frame_count_(0)
00097         , queue_()
00098     {
00099     }
00100 
00101     fake_event_source::~fake_event_source()
00102     {
00103         // send all still queued events
00104         // so keyboard/mouse state is restored
00105         while(!queue_.empty())
00106         {
00107             queue_.top()->fire_event();
00108             queue_.pop();
00109         }
00110         events::pump();
00111     }
00112 
00113 
00114     void fake_event_source::add_event(const size_t time, const SDL_Event& event)
00115     {
00116         event_node_ptr new_node(new event_node(time,event));
00117         queue_.push(new_node);
00118     }
00119 
00120     void fake_event_source::add_event(event_node_ptr new_node)
00121     {
00122         queue_.push(new_node);
00123     }
00124 
00125     void fake_event_source::start()
00126     {
00127         frame_count_ = 0;
00128     }
00129 
00130     SDL_Event fake_event_source::make_key_event(Uint8 type, const SDLKey key, const SDLMod mod)
00131     {
00132         SDL_Event event;
00133         event.type = type;
00134         if (type == SDL_KEYDOWN)
00135             event.key.state = SDL_PRESSED;
00136         else
00137             event.key.state = SDL_RELEASED;
00138         event.key.keysym.sym = key;
00139         event.key.keysym.scancode = static_cast<Uint8>(key); //
00140         event.key.keysym.mod = mod;
00141         event.key.keysym.unicode = static_cast<Uint16>(key); //
00142         return event;
00143     }
00144 
00145     event_node_ptr fake_event_source::move_mouse(const size_t time, const int x, const int y)
00146     {
00147         SDL_Event event;
00148         event.type = SDL_MOUSEMOTION;
00149         event.motion.x = static_cast<Uint16>(x);
00150         event.motion.y = static_cast<Uint16>(y);
00151         event_node_ptr new_move(new event_node_mouse_motion(time, event));
00152         add_event(new_move);
00153         return new_move;
00154     }
00155 
00156     SDL_Event fake_event_source::make_mouse_click_event(const Uint8 type, const Uint8 button)
00157     {
00158         SDL_Event event;
00159         event.type = type;
00160         if (type == SDL_MOUSEBUTTONDOWN)
00161             event.button.state = SDL_PRESSED;
00162         else
00163             event.button.state = SDL_RELEASED;
00164         event.button.button = button;
00165         return event;
00166     }
00167 
00168     event_node_ptr fake_event_source::mouse_press(const size_t time, const Uint8 button)
00169     {
00170         SDL_Event event = make_mouse_click_event(SDL_MOUSEBUTTONDOWN, button);
00171         event_node_ptr new_click(new event_node_mouse_click(time, event));
00172         add_event(new_click);
00173         return new_click;
00174     }
00175 
00176     event_node_ptr fake_event_source::mouse_release(const size_t time, const Uint8 button)
00177     {
00178         SDL_Event event = make_mouse_click_event(SDL_MOUSEBUTTONDOWN, button);
00179         event_node_ptr new_click(new event_node_mouse_click(time, event));
00180         add_event(new_click);
00181         return new_click;
00182     }
00183 
00184     event_node_ptr fake_event_source::mouse_click(const size_t time, const Uint8 button)
00185     {
00186         mouse_press(time, button);
00187         return mouse_release(time+1,button);
00188     }
00189 
00190     event_node_ptr fake_event_source::press_key(const size_t time, const SDLKey key, const SDLMod mod)
00191     {
00192         SDL_Event event = make_key_event(SDL_KEYDOWN, key, mod);
00193         event_node_ptr new_key(new event_node_keyboard(time, event));
00194         add_event(new_key);
00195         return new_key;
00196     }
00197 
00198     event_node_ptr fake_event_source::release_key(const size_t time, const SDLKey key, const SDLMod mod)
00199     {
00200         SDL_Event event = make_key_event(SDL_KEYUP, key, mod);
00201         event_node_ptr new_key(new event_node_keyboard(time, event));
00202         add_event(new_key);
00203         return new_key;
00204     }
00205 
00206     event_node_ptr fake_event_source::type_key(const size_t time, const SDLKey key, const SDLMod mod)
00207     {
00208         press_key(time,key,mod);
00209         return release_key(time+1,key,mod);
00210     }
00211 
00212     void fake_event_source::process(events::pump_info& /*info*/)
00213     {
00214         if (events::commands_disabled > 0)
00215             return;
00216         ++frame_count_;
00217         if (queue_.empty())
00218             return;
00219         while (!queue_.empty()
00220                 && queue_.top()->test_if_should_fire(frame_count_))
00221         {
00222             queue_.top()->fire_event();
00223             queue_.pop();
00224         }
00225     }
00226 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:54 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs