00001 /* $Id: events.hpp 52820 2012-01-30 18:35:33Z mordante $ */ 00002 /* 00003 Copyright (C) 2003 - 2012 by David White <dave@whitevine.net> 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 EVENTS_HPP_INCLUDED 00017 #define EVENTS_HPP_INCLUDED 00018 00019 #include "SDL.h" 00020 #include <vector> 00021 00022 //our user-defined double-click event type 00023 #define DOUBLE_CLICK_EVENT SDL_USEREVENT 00024 #define TIMER_EVENT (SDL_USEREVENT + 1) 00025 #define HOVER_REMOVE_POPUP_EVENT (SDL_USEREVENT + 2) 00026 #define DRAW_EVENT (SDL_USEREVENT + 3) 00027 #define CLOSE_WINDOW_EVENT (SDL_USEREVENT + 4) 00028 #define SHOW_HELPTIP_EVENT (SDL_USEREVENT + 5) 00029 00030 namespace events 00031 { 00032 00033 //any classes that derive from this class will automatically 00034 //receive sdl events through the handle function for their lifetime, 00035 //while the event context they were created in is active. 00036 // 00037 //NOTE: an event_context object must be initialized before a handler object 00038 //can be initialized, and the event_context must be destroyed after 00039 //the handler is destroyed. 00040 class handler 00041 { 00042 public: 00043 virtual void handle_event(const SDL_Event& event) = 0; 00044 virtual void process_event() {} 00045 virtual void draw() {} 00046 00047 virtual void volatile_draw() {} 00048 virtual void volatile_undraw() {} 00049 00050 virtual bool requires_event_focus(const SDL_Event * = NULL) const { return false; } 00051 00052 virtual void process_help_string(int /*mousex*/, int /*mousey*/) {} 00053 00054 virtual void join(); /*joins the current event context*/ 00055 virtual void leave(); /*leave the event context*/ 00056 00057 protected: 00058 handler(const bool auto_join=true); 00059 virtual ~handler(); 00060 virtual std::vector<handler*> handler_members() 00061 { 00062 return std::vector<handler*>(); 00063 } 00064 00065 private: 00066 int unicode_; 00067 bool has_joined_; 00068 }; 00069 00070 void focus_handler(const handler* ptr); 00071 void cycle_focus(); 00072 00073 bool has_focus(const handler* ptr, const SDL_Event* event); 00074 00075 //event_context objects control the handler objects that SDL events are sent 00076 //to. When an event_context is created, it will become the current event context. 00077 //event_context objects MUST be created in LIFO ordering in relation to each other, 00078 //and in relation to handler objects. That is, all event_context objects should be 00079 //created as automatic/stack variables. 00080 // 00081 //handler objects need not be created as automatic variables (e.g. you could put 00082 //them in a vector) however you must guarantee that handler objects are destroyed 00083 //before their context is destroyed 00084 struct event_context 00085 { 00086 event_context(); 00087 ~event_context(); 00088 }; 00089 00090 //causes events to be dispatched to all handler objects. 00091 void pump(); 00092 00093 struct pump_info { 00094 pump_info() : resize_dimensions(), ticks_(0) {} 00095 std::pair<int,int> resize_dimensions; 00096 int ticks(unsigned *refresh_counter=NULL, unsigned refresh_rate=1); 00097 private: 00098 int ticks_; //0 if not calculated 00099 }; 00100 00101 class pump_monitor { 00102 //pump_monitors receive notifcation after an events::pump() occurs 00103 public: 00104 pump_monitor(); 00105 virtual ~pump_monitor(); 00106 virtual void process(pump_info& info) = 0; 00107 }; 00108 00109 int discard(Uint32 event_mask); 00110 00111 void raise_process_event(); 00112 void raise_draw_event(); 00113 void raise_volatile_draw_event(); 00114 void raise_volatile_undraw_event(); 00115 void raise_help_string_event(int mousex, int mousey); 00116 } 00117 00118 typedef std::vector<events::handler*> handler_vector; 00119 00120 #define INPUT_MASK (SDL_EVENTMASK(SDL_KEYDOWN)|\ 00121 SDL_EVENTMASK(SDL_KEYUP)|\ 00122 SDL_EVENTMASK(SDL_MOUSEBUTTONDOWN)|\ 00123 SDL_EVENTMASK(SDL_MOUSEBUTTONUP)|\ 00124 SDL_EVENTMASK(SDL_JOYBUTTONDOWN)|\ 00125 SDL_EVENTMASK(SDL_JOYBUTTONUP)) 00126 00127 #endif
| Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:36 for The Battle for Wesnoth | Gna! | Forum | Wiki | CIA | devdocs |