| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "imousetool.h" |
| 4 |
#include "imodule.h" |
| 5 |
#include <list> |
| 6 |
#include <functional> |
| 7 |
#include <memory> |
| 8 |
|
| 9 |
namespace ui |
| 10 |
{ |
| 11 |
|
| 12 |
// A list of mousetools |
| 13 |
class MouseToolStack : |
| 14 |
public std::list<MouseToolPtr> |
| 15 |
{ |
| 16 |
public: |
| 17 |
// Tries to handle the given event, returning the first tool that responded positively |
| 18 |
MouseToolPtr handleMouseDownEvent(MouseTool::Event& mouseEvent) |
| 19 |
{ |
| 20 |
for (const_iterator i = begin(); i != end(); ++i) |
| 21 |
{ |
| 22 |
// Ask each tool to handle the event |
| 23 |
MouseTool::Result result = (*i)->onMouseDown(mouseEvent); |
| 24 |
|
| 25 |
if (result != MouseTool::Result::Ignored && result != MouseTool::Result::Finished) |
| 26 |
{ |
| 27 |
// This tool is now activated |
| 28 |
return *i; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
return MouseToolPtr(); |
| 33 |
} |
| 34 |
}; |
| 35 |
|
| 36 |
// A set of MouseTools, use the GlobalMouseToolManager() to get access |
| 37 |
class IMouseToolGroup |
| 38 |
{ |
| 39 |
public: |
| 40 |
virtual ~IMouseToolGroup() {} |
| 41 |
|
| 42 |
// MouseTools categories |
| 43 |
enum class Type |
| 44 |
{ |
| 45 |
OrthoView = 0, |
| 46 |
CameraView = 1, |
| 47 |
TextureTool = 2, |
| 48 |
}; |
| 49 |
|
| 50 |
// Returns the type of this group |
| 51 |
virtual Type getType() = 0; |
| 52 |
|
| 53 |
// Returns the display name of this group |
| 54 |
virtual std::string getDisplayName() = 0; |
| 55 |
|
| 56 |
// Add a MouseTool to the given group |
| 57 |
virtual void registerMouseTool(const MouseToolPtr& tool) = 0; |
| 58 |
|
| 59 |
// Removes the given mousetool from all categories. |
| 60 |
virtual void unregisterMouseTool(const MouseToolPtr& tool) = 0; |
| 61 |
|
| 62 |
// Returns the named mouse tool (case sensitive) or NULL if not found. |
| 63 |
virtual MouseToolPtr getMouseToolByName(const std::string& name) = 0; |
| 64 |
|
| 65 |
// Visit each mouse tool with the given function object |
| 66 |
virtual void foreachMouseTool(const std::function<void(const MouseToolPtr&)>& func) = 0; |
| 67 |
|
| 68 |
// Returns the set of mapped tools for this mouse event. The unsigned int is a bitmap |
| 69 |
// using the same flags as used in wxutil::MouseButton |
| 70 |
virtual MouseToolStack getMappedTools(unsigned int state) = 0; |
| 71 |
|
| 72 |
// Returns the mapping for the given mousetool |
| 73 |
virtual unsigned int getMappingForTool(const MouseToolPtr& tool) = 0; |
| 74 |
|
| 75 |
// Map a tool to a defined mouse state (as used in wxutil::MouseButton) |
| 76 |
// It's possible to map the same tool to multiple mouse states |
| 77 |
virtual void addToolMapping(unsigned int state, const MouseToolPtr& tool) = 0; |
| 78 |
|
| 79 |
// Iterate over each tool mapping |
| 80 |
virtual void foreachMapping(const std::function<void(unsigned int, const MouseToolPtr&)>& func) = 0; |
| 81 |
|
| 82 |
// Removes all mappings for all tools |
| 83 |
virtual void clearToolMappings() = 0; |
| 84 |
|
| 85 |
// Removes all mappings for the given tool |
| 86 |
virtual void clearToolMapping(MouseToolPtr& tool) = 0; |
| 87 |
}; |
| 88 |
typedef std::shared_ptr<IMouseToolGroup> IMouseToolGroupPtr; |
| 89 |
|
| 90 |
/** |
| 91 |
* An interface to an object which is able to hold one or more mousetool |
| 92 |
* groups. |
| 93 |
* |
| 94 |
* Clients of IMouseToolManager are GlobalXYWnd and GlobalCamera, for instance. |
| 95 |
*/ |
| 96 |
class IMouseToolManager : |
| 97 |
public RegisterableModule |
| 98 |
{ |
| 99 |
public: |
| 100 |
virtual ~IMouseToolManager() {} |
| 101 |
|
| 102 |
// Get the group defined by the given enum. This always succeeds, if the group |
| 103 |
// is not existing yet, a new one will be created internally. |
| 104 |
virtual IMouseToolGroup& getGroup(IMouseToolGroup::Type group) = 0; |
| 105 |
|
| 106 |
// Iterate over each group using the given visitor function |
| 107 |
virtual void foreachGroup(const std::function<void(IMouseToolGroup&)>& functor) = 0; |
| 108 |
|
| 109 |
// Returns matching MouseTools for the given event and group type. To convert a wxMouseEvent to |
| 110 |
// the unsigned int mouseState, use wxutil::MouseButton |
| 111 |
virtual MouseToolStack getMouseToolsForEvent(IMouseToolGroup::Type group, unsigned int mouseState) = 0; |
| 112 |
|
| 113 |
// Prints the list of possible mouse actions to the statusbar |
| 114 |
virtual void updateStatusbar(unsigned int mouseState) = 0; |
| 115 |
|
| 116 |
// Reverts all mappings to the defaults as defined in the stock input.xml |
| 117 |
virtual void resetBindingsToDefault() = 0; |
| 118 |
}; |
| 119 |
|
| 120 |
} // namespace |
| 121 |
|
| 122 |
const char* const MODULE_MOUSETOOLMANAGER = "MouseToolManager"; |
| 123 |
|
| 124 |
inline ui::IMouseToolManager& GlobalMouseToolManager() |
| 125 |
{ |
| 126 |
static module::InstanceReference<ui::IMouseToolManager> _reference(MODULE_MOUSETOOLMANAGER); |
| 127 |
return _reference; |
| 128 |
} |