Fixed drop target to work correctly with multiple groups

Added initial implementation for fake mouse events for tests
This commit is contained in:
Pauli Nieminen 2008-08-03 22:04:44 +00:00
parent 3978253e28
commit 723d8c3cbc
5 changed files with 93 additions and 4 deletions

View file

@ -92,5 +92,62 @@ BOOST_AUTO_TEST_CASE( check_memory_leaks )
BOOST_CHECK(gui::drop_target::empty());
}
BOOST_AUTO_TEST_CASE( test_multiple_drop_groups )
{
gui::drop_group_manager_ptr group(new gui::drop_group_manager());
gui::drop_group_manager_ptr group2(new gui::drop_group_manager());
BOOST_CHECK(group->get_group_id() > 0);
BOOST_CHECK(group2->get_group_id() > 0);
typedef std::vector<SDL_Rect> location_store;
location_store locations;
location_store locations2;
// Create rectangles for drop targets
locations.push_back(create_rect(50,50,20,20));
locations.push_back(create_rect(50,100,20,20));
locations.push_back(create_rect(50,150,20,20));
locations.push_back(create_rect(50,200,20,20));
locations.push_back(create_rect(50,250,20,20));
locations.push_back(create_rect(50,300,20,20));
locations2.push_back(create_rect(50,50,20,20));
locations2.push_back(create_rect(100,50,20,20));
locations2.push_back(create_rect(150,50,20,20));
locations2.push_back(create_rect(200,50,20,20));
locations2.push_back(create_rect(250,50,20,20));
locations2.push_back(create_rect(300,50,20,20));
target_store targets;
target_store targets2;
int id_counter = 0;
std::for_each(locations.begin(), locations.end(),
boost::bind(create_drop_targets,_1, group, boost::ref(targets), boost::ref(id_counter)));
id_counter = 0;
std::for_each(locations2.begin(), locations2.end(),
boost::bind(create_drop_targets,_1, group2, boost::ref(targets2), boost::ref(id_counter)));
BOOST_CHECK_EQUAL(targets.size(), locations.size());
BOOST_CHECK_EQUAL(targets2.size(), locations2.size());
// Modify 3rd rectangle to overlap with 4th
locations[2].y = 190;
// Check for correct drop results
BOOST_CHECK_EQUAL(targets[2]->handle_drop(), 3);
BOOST_CHECK_EQUAL(targets[3]->handle_drop(), 2);
BOOST_CHECK_EQUAL(targets[1]->handle_drop(), -1);
BOOST_CHECK_EQUAL(targets[4]->handle_drop(), -1);
locations2[2].y = 180;
locations2[2].x = 50;
BOOST_CHECK_EQUAL(targets2[2]->handle_drop(), -1);
}
/* vim: set ts=4 sw=4: */
BOOST_AUTO_TEST_SUITE_END()

View file

@ -65,6 +65,13 @@ namespace test_utils {
key_list[event_.key.keysym.sym] = 0;
}
event_node_mouse_motion::event_node_mouse_motion(size_t time, SDL_Event& event) : event_node(time,event)
{}
void event_node_mouse_motion::fire_event()
{
SDL_WarpMouse(event_.motion.x,event_.motion.y);
}
fake_event_source::fake_event_source() : frame_count_(0)
{
}
@ -113,6 +120,17 @@ namespace test_utils {
return event;
}
event_node_ptr fake_event_source::move_mouse(const size_t time, const int x, const int y)
{
SDL_Event event;
event.type = SDL_MOUSEMOTION;
event.motion.x = x;
event.motion.y = y;
event_node_ptr new_move(new event_node_mouse_motion(time, event));
add_event(new_move);
return new_move;
}
event_node_ptr fake_event_source::press_key(const size_t time, const SDLKey key, const SDLMod mod)
{
SDL_Event event = make_key_event(SDL_KEYDOWN, key, mod);

View file

@ -65,7 +65,13 @@ namespace test_utils {
class event_node_keyboard : public event_node {
public:
event_node_keyboard(size_t time, SDL_Event& event);
void fire_event();
virtual void fire_event();
};
class event_node_mouse_motion : public event_node {
public:
event_node_mouse_motion(size_t time, SDL_Event& event);
virtual void fire_event();
};
typedef boost::shared_ptr<event_node> event_node_ptr;
@ -97,6 +103,10 @@ namespace test_utils {
event_node_ptr press_key(const size_t time, const SDLKey key, const SDLMod mod = KMOD_NONE);
event_node_ptr release_key(const size_t time, const SDLKey key, const SDLMod mod =KMOD_NONE);
event_node_ptr move_mouse(const size_t time, const int x, const int y);
event_node_ptr mouse_press(const size_t time, const Uint8 button);
event_node_ptr mouse_release(const size_t time, const Uint8 button);
void process(events::pump_info& /*info*/);
};
}

View file

@ -64,7 +64,7 @@ namespace gui {
DROP_DOWN
};
drag_state drag_;
static const int MIN_DRAG_DISTANCE;
static const float MIN_DRAG_DISTANCE;
static const float RETURN_SPEED;
}; //end class combo

View file

@ -16,6 +16,8 @@
#include <boost/bind.hpp>
#include <algorithm>
#include <iostream>
namespace gui {
drop_target::drop_groups drop_target::groups_;
@ -57,15 +59,17 @@ namespace gui {
int drop_target::handle_drop()
{
drop_groups::iterator end = groups_.upper_bound(group_->get_group_id());
drop_target::drop_groups::iterator itor
= std::find_if(groups_.lower_bound(group_->get_group_id()),
groups_.upper_bound(group_->get_group_id()),
end,
boost::bind(&drop_target::hit_rect,
boost::bind(&drop_target::drop_groups::value_type::second,_1)
,loc_, id_));
if (itor == groups_.end())
if (itor == end)
return -1;
return itor->second->get_id();
}