bool has_focus(const handler* hand, const SDL_Event* event) { if(event_contexts.empty()) { return true; } if(hand->requires_event_focus(event) == false) { return true; } const int foc_i = event_contexts.back().focused_handler; //if no-one has focus at the moment, this handler obviously wants //focus, so give it to it. if(foc_i == -1) { focus_handler(hand); return true; } handler *const foc_hand = event_contexts.back().handlers[foc_i]; if(foc_hand == hand){ return true; } else if(!foc_hand->requires_event_focus(event)) { //if the currently focused handler doesn't need focus for this event //allow the most recent interested handler to take care of it int back_i = event_contexts.back().handlers.size() - 1; for(int i=back_i; i>=0; --i) { handler *const thief_hand = event_contexts.back().handlers[i]; if(i != foc_i && thief_hand->requires_event_focus(event)) { //steal focus focus_handler(thief_hand); if(foc_i < back_i) { //position the previously focused handler to allow stealing back event_contexts.back().delete_handler_index(foc_i); event_contexts.back().add_handler(foc_hand); } return thief_hand == hand; } } } return false; }
bool has_focus(const sdl_handler* hand, const SDL_Event* event) { if(event_contexts.empty()) { return true; } if(hand->requires_event_focus(event) == false) { return true; } const handler_list::iterator foc = event_contexts.back().focused_handler; auto& handlers = event_contexts.back().handlers; // If no-one has focus at the moment, this handler obviously wants // focus, so give it to it. if(foc == handlers.end()) { focus_handler(hand); return true; } sdl_handler* const foc_hand = *foc; if(foc_hand == hand){ return true; } else if(!foc_hand->requires_event_focus(event)) { // If the currently focused handler doesn't need focus for this event // allow the most recent interested handler to take care of it for(auto i = handlers.rbegin(); i != handlers.rend(); ++i) { sdl_handler* const thief_hand = *i; if(thief_hand != foc_hand && thief_hand->requires_event_focus(event)) { // Steal focus focus_handler(thief_hand); // Position the previously focused handler to allow stealing back handlers.splice(handlers.end(), handlers, foc); return thief_hand == hand; } } } return false; }