Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/gradient_editor_plugin.h
9896 views
1
/**************************************************************************/
2
/* gradient_editor_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 "editor/inspector/editor_inspector.h"
34
#include "editor/plugins/editor_plugin.h"
35
36
class EditorSpinSlider;
37
class ColorPicker;
38
class PopupPanel;
39
class GradientTexture1D;
40
41
class GradientEdit : public Control {
42
GDCLASS(GradientEdit, Control);
43
44
Ref<Gradient> gradient;
45
Ref<GradientTexture1D> preview_texture;
46
47
PopupPanel *popup = nullptr;
48
ColorPicker *picker = nullptr;
49
50
bool snap_enabled = false;
51
int snap_count = 10;
52
53
enum GrabMode {
54
GRAB_NONE,
55
GRAB_ADD,
56
GRAB_MOVE
57
};
58
59
GrabMode grabbing = GRAB_NONE;
60
float pre_grab_offset = 0.5;
61
int pre_grab_index = -1;
62
int selected_index = -1;
63
int hovered_index = -1;
64
65
// Make sure to use the scaled values below.
66
const int BASE_SPACING = 4;
67
const int BASE_HANDLE_WIDTH = 8;
68
69
int draw_spacing = BASE_SPACING;
70
int handle_width = BASE_HANDLE_WIDTH;
71
72
int _get_gradient_rect_width() const;
73
74
void _color_changed(const Color &p_color);
75
void _redraw();
76
77
int _get_point_at(int p_xpos) const;
78
int _predict_insertion_index(float p_offset);
79
void _show_color_picker();
80
81
protected:
82
virtual void gui_input(const Ref<InputEvent> &p_event) override;
83
void _notification(int p_what);
84
static void _bind_methods();
85
86
public:
87
void set_gradient(const Ref<Gradient> &p_gradient);
88
const Ref<Gradient> &get_gradient() const;
89
90
ColorPicker *get_picker() const;
91
PopupPanel *get_popup() const;
92
93
void set_selected_index(int p_index);
94
95
void add_point(float p_offset, const Color &p_color);
96
void remove_point(int p_index);
97
void set_offset(int p_index, float p_offset);
98
void set_color(int p_index, const Color &p_color);
99
void reverse_gradient();
100
101
void set_snap_enabled(bool p_enabled);
102
void set_snap_count(int p_count);
103
104
GradientEdit();
105
};
106
107
class GradientEditor : public VBoxContainer {
108
GDCLASS(GradientEditor, VBoxContainer);
109
110
Button *reverse_button = nullptr;
111
Button *snap_button = nullptr;
112
EditorSpinSlider *snap_count_edit = nullptr;
113
GradientEdit *gradient_editor_rect = nullptr;
114
115
void _set_snap_enabled(bool p_enabled);
116
void _set_snap_count(int p_count);
117
118
protected:
119
void _notification(int p_what);
120
121
public:
122
static const int DEFAULT_SNAP;
123
void set_gradient(const Ref<Gradient> &p_gradient);
124
125
GradientEditor();
126
};
127
128
class EditorInspectorPluginGradient : public EditorInspectorPlugin {
129
GDCLASS(EditorInspectorPluginGradient, EditorInspectorPlugin);
130
131
public:
132
virtual bool can_handle(Object *p_object) override;
133
virtual void parse_begin(Object *p_object) override;
134
};
135
136
class GradientEditorPlugin : public EditorPlugin {
137
GDCLASS(GradientEditorPlugin, EditorPlugin);
138
139
public:
140
virtual String get_plugin_name() const override { return "Gradient"; }
141
142
GradientEditorPlugin();
143
};
144
145