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