MeshLib C++ Docs
Loading...
Searching...
No Matches
MRShortcutManager.h
Go to the documentation of this file.
1#pragma once
2#include "MRViewerFwd.h"
3#include "MRMesh/MRphmap.h"
5#include <string>
6#include <functional>
7#include <optional>
8
9namespace MR
10{
13
14
16{
17 int key{ 0 };
18 int mod{ 0 };
19
20 bool operator<( const ShortcutKey& other ) const
21 {
22 if ( key < other.key )
23 return true;
24 if ( key == other.key )
25 return mod < other.mod;
26 return false;
27 }
28};
29
30enum class ShortcutCategory : char
31{
32 Info,
33 Edit,
34 View,
35 Scene,
36 Objects,
38 Count
39};
40
46class MRVIEWER_CLASS ShortcutManager : public MultiListener<KeyDownListener, KeyRepeatListener>
47{
48public:
49 virtual ~ShortcutManager() = default;
50
53
55 {
57 std::string name;
58 std::function<void()> action;
59 bool repeatable = true;
60 };
61
62 inline static const std::string categoryNames[6] = { "Info", "Edit", "View", "Scene", "Objects", "Selection " };
63
67 MRVIEWER_API virtual void setShortcut( const ShortcutKey& key, const ShortcutCommand& command );
68
69 using ShortcutList = std::vector<std::tuple<ShortcutKey, Category, std::string>>;
70
73 MRVIEWER_API const ShortcutList& getShortcutList() const;
74
75 enum class Reason
76 {
77 KeyDown,
78 KeyRepeat
79 };
80
82 bool isEnabled() const { return enabled_; }
83 void enable( bool on ) { enabled_ = on; }
84
86 MRVIEWER_API virtual bool processShortcut( const ShortcutKey& key, Reason = Reason::KeyDown ) const;
87
88 MRVIEWER_API bool onKeyDown_( int key, int modifier ) override;
89 MRVIEWER_API bool onKeyRepeat_( int key, int modifier ) override;
90
92 MRVIEWER_API static const char* getModifierString( int mod );
94 MRVIEWER_API static std::string getKeyString( int key );
96 MRVIEWER_API static std::string getKeyFullString( const ShortcutKey& key, bool respectKey = true );
97
99 MRVIEWER_API std::optional<ShortcutKey> findShortcutByName( const std::string& name ) const;
100
102 MRVIEWER_API void clear();
103protected:
106 MRVIEWER_API static int mapKeyFromKeyAndMod( const ShortcutKey& key, bool respectKeyboard );
108 static ShortcutKey kayAndModFromMapKey( int mapKey ) { return { mapKey >> 6, mapKey % ( 1 << 6 ) }; }
109
110 using ShourtcutsMap = HashMap<int, ShortcutCommand>;
111 using ShourtcutsBackMap = HashMap<std::string, int>;
112
113 bool enabled_{ true };
114
117
118 mutable std::optional<ShortcutList> listCache_;
119};
120
121}
Definition MRShortcutManager.h:47
static ShortcutKey kayAndModFromMapKey(int mapKey)
returns key with modifier (alt, ctrl, shift, etc.) from simple map key
Definition MRShortcutManager.h:108
MRVIEWER_API const ShortcutList & getShortcutList() const
HashMap< std::string, int > ShourtcutsBackMap
Definition MRShortcutManager.h:111
Reason
Definition MRShortcutManager.h:76
static MRVIEWER_API const char * getModifierString(int mod)
make string from strictly one modifier
static MRVIEWER_API std::string getKeyString(int key)
make string from a key without modifiers, for arrow characters it uses icons font
MRVIEWER_API bool onKeyDown_(int key, int modifier) override
int key
Definition MRShortcutManager.h:17
virtual MRVIEWER_API bool processShortcut(const ShortcutKey &key, Reason=Reason::KeyDown) const
if given key has action in shortcut map - process it and returns true, otherwise returns false;
void enable(bool on)
Definition MRShortcutManager.h:83
MRVIEWER_API void clear()
clear all saved shortcuts
virtual MRVIEWER_API void setShortcut(const ShortcutKey &key, const ShortcutCommand &command)
std::function< void()> action
name of action
Definition MRShortcutManager.h:58
std::string name
Definition MRShortcutManager.h:57
std::vector< std::tuple< ShortcutKey, Category, std::string > > ShortcutList
Definition MRShortcutManager.h:69
HashMap< int, ShortcutCommand > ShourtcutsMap
Definition MRShortcutManager.h:110
ShourtcutsBackMap backMap_
Definition MRShortcutManager.h:116
ShourtcutsMap map_
Definition MRShortcutManager.h:115
bool isEnabled() const
processShortcut does nothing if not enabled
Definition MRShortcutManager.h:82
ShortcutCategory
Definition MRShortcutManager.h:31
bool operator<(const ShortcutKey &other) const
Definition MRShortcutManager.h:20
virtual ~ShortcutManager()=default
Category category
Definition MRShortcutManager.h:56
static MRVIEWER_API int mapKeyFromKeyAndMod(const ShortcutKey &key, bool respectKeyboard)
static MRVIEWER_API std::string getKeyFullString(const ShortcutKey &key, bool respectKey=true)
make string from all modifiers and with/without key and returns it
MRVIEWER_API std::optional< ShortcutKey > findShortcutByName(const std::string &name) const
if action with given name is present in shortcut list - returns it
std::optional< ShortcutList > listCache_
Definition MRShortcutManager.h:118
int mod
Definition MRShortcutManager.h:18
MRVIEWER_API bool onKeyRepeat_(int key, int modifier) override
@ other
Angle, normally float. Measure in radians.
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRViewerEventsListener.h:32
Definition MRShortcutManager.h:16
Definition MRShortcutManager.h:55