tests/utils/fake_event_source.hpp

Go to the documentation of this file.
00001 /* $Id: fake_event_source.hpp 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 #ifndef TESTS_UTILS_FAKE_EVENT_SOURCE_HPP_INCLUDED
00017 #define TESTS_UTILS_FAKE_EVENT_SOURCE_HPP_INCLUDED
00018 
00019 #include <boost/shared_ptr.hpp>
00020 #include <boost/noncopyable.hpp>
00021 #include <queue>
00022 #include "SDL.h"
00023 
00024 #include "events.hpp"
00025 
00026 namespace test_utils {
00027 
00028 
00029     template<class T>
00030         struct less_ptr {
00031             bool operator()(const T& a, const T& b) const
00032             {
00033                 return (*a) < (*b);
00034             }
00035         };
00036 
00037     /**
00038      * Base class for all event nodes to be used to fire fake input events
00039      **/
00040     class event_node :
00041         public boost::noncopyable
00042     {
00043         size_t time_;
00044         bool fired_;
00045         protected:
00046         SDL_Event event_;
00047 
00048         public:
00049         event_node(const size_t time, const SDL_Event& event);
00050         virtual ~event_node();
00051 
00052         /**
00053          * Used to fire sdl event stored in this object.
00054          * Child class may extend or override functionality
00055          **/
00056         virtual void fire_event();
00057         /**
00058          * Test if this event should fire now
00059          **/
00060         bool test_if_should_fire(const size_t frame_count ) const;
00061 
00062         /**
00063          * @return true if this event has fired
00064          **/
00065         bool is_fired() const;
00066 
00067         /**
00068          * We want the smallestat the top
00069          **/
00070         bool operator<(const event_node& o) const;
00071     };
00072 
00073     /**
00074      * modifies SDL_GetKeyState table to have
00075      * correct state.
00076      **/
00077     class event_node_keyboard : public event_node {
00078         public:
00079             event_node_keyboard(size_t time, SDL_Event& event);
00080             virtual void fire_event();
00081     };
00082 
00083     /**
00084      * Uses special SDL_WarpMouse function to
00085      * generate mouse move events
00086      **/
00087     class event_node_mouse_motion : public event_node {
00088         public:
00089             event_node_mouse_motion(size_t time, SDL_Event& event);
00090             virtual void fire_event();
00091     };
00092 
00093     /**
00094      * Used to create SDL_MOUSEBUTTONDOWN/UP events
00095      * with correct x,y values
00096      **/
00097     class event_node_mouse_click : public event_node {
00098         public:
00099             event_node_mouse_click(size_t time, SDL_Event& event);
00100             virtual void fire_event();
00101     };
00102 
00103     typedef boost::shared_ptr<event_node> event_node_ptr;
00104 
00105     /**
00106      * fake_event_source is used to generate new events in
00107      * events::pump()
00108      * Timing used in function is loop counter incremented
00109      * everytime events::pump() is called.
00110      **/
00111 
00112     class fake_event_source
00113         : public events::pump_monitor,
00114         public boost::noncopyable
00115     {
00116             size_t frame_count_;
00117             typedef std::priority_queue<event_node_ptr,std::vector<event_node_ptr>,less_ptr<event_node_ptr> > event_queue;
00118 
00119             event_queue queue_;
00120 
00121             SDL_Event make_key_event(Uint8 type, const SDLKey key, const SDLMod mod);
00122             SDL_Event make_mouse_click_event(const Uint8 type, const Uint8 button);
00123         public:
00124             fake_event_source();
00125             ~fake_event_source();
00126 
00127             /**
00128              * adds a generic event to queue
00129              **/
00130             void add_event(const size_t time, const SDL_Event& event);
00131             /**
00132              * adds any type of event to queue
00133              **/
00134             void add_event(event_node_ptr new_node);
00135 
00136             /**
00137              * Sets event time source back to zero
00138              **/
00139             void start();
00140 
00141             /**
00142              * adds keyboard press event to queue
00143              **/
00144             event_node_ptr press_key(const size_t time, const SDLKey key, const SDLMod mod = KMOD_NONE);
00145             /**
00146              * adds keyboard release event to queue
00147              **/
00148             event_node_ptr release_key(const size_t time, const SDLKey key, const SDLMod mod =KMOD_NONE);
00149             /**
00150              * Just push and release a key
00151              * release is done in time+1
00152              * @return release event only
00153              **/
00154             event_node_ptr type_key(const size_t time, const SDLKey key, const SDLMod mod =KMOD_NONE);
00155 
00156             /**
00157              * Adds mouse motion event to queue
00158              **/
00159             event_node_ptr move_mouse(const size_t time, const int x, const int y);
00160             /**
00161              * adds mouse button click event to queue
00162              **/
00163             event_node_ptr mouse_press(const size_t time, const Uint8 button);
00164             /**
00165              * adds mouse button realease event to queue
00166              **/
00167             event_node_ptr mouse_release(const size_t time, const Uint8 button);
00168             /**
00169              * Make mouse click that equals to press and release
00170              * relase is done in time+1
00171              * @return release event only
00172              **/
00173             event_node_ptr mouse_click(const size_t time, const Uint8 button);
00174 
00175             /**
00176              * Called by events::pump() to fire events
00177              **/
00178             void process(events::pump_info& /*info*/);
00179     };
00180 }
00181 #endif
 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