1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
#include "MouseToolHandler.h"
#include "itextstream.h"
#include "MouseButton.h"
#include "ui/imainframe.h"
namespace wxutil
{
MouseToolHandler::MouseToolHandler(ui::IMouseToolGroup::Type type) :
_type(type)
{}
void MouseToolHandler::onGLMouseButtonPress(wxMouseEvent& ev)
{
// Filter out the button that was triggering this event
unsigned int state = wxutil::MouseButton::GetButtonStateChangeForMouseEvent(ev);
// For the active tool mapping, we need just the mouse button without modifiers
unsigned int button = wxutil::MouseButton::GetButtonStateChangeForMouseEvent(ev) & wxutil::MouseButton::ALL_BUTTON_MASK;
ui::MouseToolPtr activeToolToBeCleared;
if (_activeMouseTools.find(button) != _activeMouseTools.end())
{
// Send the click event to the currently active tool. Some tools stay active after
// mouse up and might choose to end their action along with the mouse down event
// The FreeMoveTool with toggle mode activated is such an example.
ui::MouseToolPtr tool = _activeMouseTools[button];
switch (processMouseDownEvent(tool, Vector2(ev.GetX(), ev.GetY())))
{
case ui::MouseTool::Result::Finished:
// Tool is done
activeToolToBeCleared = tool;
break;
case ui::MouseTool::Result::Activated:
case ui::MouseTool::Result::Continued:
handleViewRefresh(tool->getRefreshMode());
break;
case ui::MouseTool::Result::Ignored:
break;
};
}
// Now consider all the available tools and send the event
// Currently active tools are handled above, so don't send the event again
// Get all mouse tools mapped to this button/modifier combination
ui::MouseToolStack toolStack = GlobalMouseToolManager().getMouseToolsForEvent(_type, state);
// Remove all active mouse tools from this stack
toolStack.remove_if(std::bind(&MouseToolHandler::toolIsActive, this, std::placeholders::_1));
// The candidates have been trimmed, so let's clear out any pending tools
if (activeToolToBeCleared)
{
clearActiveMouseTool(activeToolToBeCleared);
activeToolToBeCleared.reset();
}
// Check which one of the candidates responds to the mousedown event
ui::MouseToolPtr activeTool;
for (ui::MouseToolPtr tool : toolStack)
{
// Ask each tool to handle the event
ui::MouseTool::Result result = processMouseDownEvent(tool, Vector2(ev.GetX(), ev.GetY()));
if (result != ui::MouseTool::Result::Ignored && result != ui::MouseTool::Result::Finished)
{
// This tool is now activated
activeTool = tool;
break;
}
}
if (!activeTool)
{
return;
}
// Store this tool in our map
_activeMouseTools[button] = activeTool;
unsigned int pointerMode = activeTool->getPointerMode();
// Check if the mousetool requires pointer freeze
if ((pointerMode & ui::MouseTool::PointerMode::Capture) != 0)
{
startCapture(activeTool);
}
if (!_escapeListener)
{
// Register a hook to capture the ESC key during the active phase
_escapeListener.reset(new KeyEventFilter(WXK_ESCAPE,
std::bind(&MouseToolHandler::handleEscapeKeyPress, this)));
}
}
void MouseToolHandler::onGLMouseMove(wxMouseEvent& ev)
{
// Skip this event if any of the active mouse tools is in capture mode
// the call here still arrives on OSX even during capture
for (const ActiveMouseTools::value_type& pair : _activeMouseTools)
{
if (pair.second->getPointerMode() & ui::MouseTool::PointerMode::Capture)
{
return; // skip
}
}
Vector2 position(ev.GetX(), ev.GetY());
sendMoveEventToInactiveTools(ev.GetX(), ev.GetY());
// Pass the move event to all active tools and clear the ones that are done
for (ActiveMouseTools::const_iterator i = _activeMouseTools.begin();
i != _activeMouseTools.end();)
{
ui::MouseToolPtr tool = (i++)->second;
// Ask the active mousetool to handle this event
switch (processMouseMoveEvent(tool, ev.GetX(), ev.GetY()))
{
case ui::MouseTool::Result::Finished:
// Tool is done
clearActiveMouseTool(tool);
handleViewRefresh(tool->getRefreshMode());
break;
case ui::MouseTool::Result::Activated:
case ui::MouseTool::Result::Continued:
handleViewRefresh(tool->getRefreshMode());
break;
case ui::MouseTool::Result::Ignored:
break;
};
}
}
void MouseToolHandler::onGLCapturedMouseMove(int x, int y, unsigned int mouseState)
{
sendMoveEventToInactiveTools(x, y);
for (ActiveMouseTools::const_iterator i = _activeMouseTools.begin(); i != _activeMouseTools.end();)
{
ui::MouseToolPtr tool = (i++)->second;
switch (processMouseMoveEvent(tool, x, y))
{
case ui::MouseTool::Result::Finished:
// Tool is done
clearActiveMouseTool(tool);
handleViewRefresh(tool->getRefreshMode());
break;
case ui::MouseTool::Result::Activated:
case ui::MouseTool::Result::Continued:
handleViewRefresh(tool->getRefreshMode());
break;
case ui::MouseTool::Result::Ignored:
break;
};
}
}
bool MouseToolHandler::toolIsActive(const ui::MouseToolPtr& tool)
{
// The active tools don't count
for (const ActiveMouseTools::value_type& i : _activeMouseTools)
{
if (i.second == tool) return true;
}
return false;
}
void MouseToolHandler::sendMoveEventToInactiveTools(int x, int y)
{
// Send mouse move events to all tools that want them
GlobalMouseToolManager().getGroup(_type).foreachMouseTool([&] (const ui::MouseToolPtr& tool)
{
if (!tool->alwaysReceivesMoveEvents()) return;
// The active tools don't receive this event a second time
if (toolIsActive(tool)) return;
processMouseMoveEvent(tool, x, y);
});
}
void MouseToolHandler::onGLMouseButtonRelease(wxMouseEvent& ev)
{
if (_activeMouseTools.empty()) return;
// Determine the button that has been released
unsigned int state = wxutil::MouseButton::GetButtonStateChangeForMouseEvent(ev) & wxutil::MouseButton::ALL_BUTTON_MASK;
ActiveMouseTools::const_iterator i = _activeMouseTools.find(state);
if (i != _activeMouseTools.end())
{
// Ask the active mousetool to handle this event
ui::MouseTool::Result result = processMouseUpEvent(i->second, Vector2(ev.GetX(), ev.GetY()));
if (result == ui::MouseTool::Result::Finished)
{
handleViewRefresh(i->second->getRefreshMode());
clearActiveMouseTool(i->second);
}
}
}
void MouseToolHandler::handleCaptureLost(const ui::MouseToolPtr& tool)
{
if (!tool) return;
if (tool->getPointerMode() & ui::MouseTool::PointerMode::Capture)
{
// Send the capture lost event, which should make the tool cancel the operation
tool->onMouseCaptureLost(getInteractiveView());
handleViewRefresh(tool->getRefreshMode());
// Clear the tool when the capture is lost
clearActiveMouseTool(tool);
}
}
void MouseToolHandler::clearActiveMouseTool(const ui::MouseToolPtr& tool)
{
unsigned int previousPointerMode = tool->getPointerMode();
for (ActiveMouseTools::const_iterator i = _activeMouseTools.begin(); i != _activeMouseTools.end(); ++i)
{
if (i->second == tool)
{
_activeMouseTools.erase(i);
break;
}
}
// Check if any mouse tools still require capture mode
unsigned int remainingPointerMode = ui::MouseTool::PointerMode::Normal;
for (const ActiveMouseTools::value_type& pair : _activeMouseTools)
{
remainingPointerMode |= pair.second->getPointerMode();
}
// If no more freezing mouse tools: release the mouse cursor again
if (previousPointerMode & ui::MouseTool::PointerMode::Capture &&
!(remainingPointerMode & ui::MouseTool::PointerMode::Capture))
{
endCapture();
}
// Reset the escape listener if this was the last active tool
if (_activeMouseTools.empty())
{
_escapeListener.reset();
}
}
void MouseToolHandler::clearActiveMouseTool(unsigned int button)
{
if (_activeMouseTools.find(button) != _activeMouseTools.end())
{
clearActiveMouseTool(_activeMouseTools[button]);
}
}
void MouseToolHandler::clearActiveMouseTools()
{
// Reset the escape listener
_escapeListener.reset();
if (_activeMouseTools.empty())
{
return;
}
// Check the capture mode
unsigned int pointerMode = ui::MouseTool::PointerMode::Normal;
// Freezing mouse tools: release the mouse cursor again
for (ActiveMouseTools::const_iterator i = _activeMouseTools.begin(); i != _activeMouseTools.end();)
{
pointerMode |= i->second->getPointerMode();
// Tool is done
_activeMouseTools.erase(i++);
}
// If any of the active tools was capturing, end this now
if (pointerMode & ui::MouseTool::PointerMode::Capture)
{
endCapture();
}
}
KeyEventFilter::Result MouseToolHandler::handleEscapeKeyPress()
{
// Key will slip through unless one tool reports having it processed
KeyEventFilter::Result result = KeyEventFilter::Result::KeyIgnored;
for (ActiveMouseTools::const_iterator i = _activeMouseTools.begin(); i != _activeMouseTools.end();)
{
ui::MouseToolPtr tool = (i++)->second;
switch (tool->onCancel(getInteractiveView()))
{
// Any MouseTool returning the Finished signal will be removed
case ui::MouseTool::Result::Finished:
// Tool is done
clearActiveMouseTool(tool);
handleViewRefresh(tool->getRefreshMode());
result = KeyEventFilter::Result::KeyProcessed;
break;
case ui::MouseTool::Result::Activated:
case ui::MouseTool::Result::Continued:
case ui::MouseTool::Result::Ignored:
break;
};
}
return result;
}
void MouseToolHandler::handleViewRefresh(unsigned int flags)
{
if (flags & ui::MouseTool::RefreshMode::AllViews)
{
// Pass the signal to the mainframe
GlobalMainFrame().updateAllWindows((flags & ui::MouseTool::RefreshMode::Force) != 0);
}
else if (flags & ui::MouseTool::RefreshMode::ActiveView)
{
if (flags & ui::MouseTool::RefreshMode::Force)
{
getInteractiveView().forceRedraw();
}
else
{
getInteractiveView().queueDraw();
}
}
}
}
|