Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/inspector/editor_context_menu_plugin.h
9896 views
1
/**************************************************************************/
2
/* editor_context_menu_plugin.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/object/gdvirtual.gen.inc"
34
#include "core/object/ref_counted.h"
35
36
class InputEvent;
37
class PopupMenu;
38
class Shortcut;
39
class Texture2D;
40
41
class EditorContextMenuPlugin : public RefCounted {
42
GDCLASS(EditorContextMenuPlugin, RefCounted);
43
44
friend class EditorContextMenuPluginManager;
45
46
static constexpr int MAX_ITEMS = 100;
47
48
public:
49
enum ContextMenuSlot {
50
CONTEXT_SLOT_SCENE_TREE,
51
CONTEXT_SLOT_FILESYSTEM,
52
CONTEXT_SLOT_SCRIPT_EDITOR,
53
CONTEXT_SLOT_FILESYSTEM_CREATE,
54
CONTEXT_SLOT_SCRIPT_EDITOR_CODE,
55
CONTEXT_SLOT_SCENE_TABS,
56
CONTEXT_SLOT_2D_EDITOR,
57
};
58
static constexpr int BASE_ID = 2000;
59
60
private:
61
int slot = -1;
62
63
public:
64
struct ContextMenuItem {
65
int id = 0;
66
String item_name;
67
Callable callable;
68
Ref<Texture2D> icon;
69
Ref<Shortcut> shortcut;
70
PopupMenu *submenu = nullptr;
71
};
72
HashMap<String, ContextMenuItem> context_menu_items;
73
HashMap<Ref<Shortcut>, Callable> context_menu_shortcuts;
74
75
protected:
76
static void _bind_methods();
77
78
GDVIRTUAL1(_popup_menu, Vector<String>);
79
80
public:
81
virtual void get_options(const Vector<String> &p_paths);
82
83
void add_menu_shortcut(const Ref<Shortcut> &p_shortcut, const Callable &p_callable);
84
void add_context_menu_item(const String &p_name, const Callable &p_callable, const Ref<Texture2D> &p_texture);
85
void add_context_menu_item_from_shortcut(const String &p_name, const Ref<Shortcut> &p_shortcut, const Ref<Texture2D> &p_texture);
86
void add_context_submenu_item(const String &p_name, PopupMenu *p_menu, const Ref<Texture2D> &p_texture);
87
};
88
89
VARIANT_ENUM_CAST(EditorContextMenuPlugin::ContextMenuSlot);
90
91
class EditorContextMenuPluginManager : public Object {
92
GDCLASS(EditorContextMenuPluginManager, Object);
93
94
using ContextMenuSlot = EditorContextMenuPlugin::ContextMenuSlot;
95
static inline EditorContextMenuPluginManager *singleton = nullptr;
96
97
LocalVector<Ref<EditorContextMenuPlugin>> plugin_list;
98
99
public:
100
static EditorContextMenuPluginManager *get_singleton() { return singleton; }
101
102
void add_plugin(ContextMenuSlot p_slot, const Ref<EditorContextMenuPlugin> &p_plugin);
103
void remove_plugin(const Ref<EditorContextMenuPlugin> &p_plugin);
104
105
bool has_plugins_for_slot(ContextMenuSlot p_slot);
106
void add_options_from_plugins(PopupMenu *p_popup, ContextMenuSlot p_slot, const Vector<String> &p_paths);
107
Callable match_custom_shortcut(ContextMenuSlot p_slot, const Ref<InputEvent> &p_event);
108
bool activate_custom_option(ContextMenuSlot p_slot, int p_option, const Variant &p_arg);
109
110
void invoke_callback(const Callable &p_callback, const Variant &p_arg);
111
112
static void create();
113
static void cleanup();
114
};
115
116