2017-04-28 20:04:09 +02:00
|
|
|
/**************************************************************************/
|
|
|
|
/* curve_editor_plugin.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2017-04-07 04:36:37 +02:00
|
|
|
#ifndef CURVE_EDITOR_PLUGIN_H
|
|
|
|
#define CURVE_EDITOR_PLUGIN_H
|
|
|
|
|
2022-11-11 20:12:48 +01:00
|
|
|
#include "editor/editor_inspector.h"
|
2019-12-24 08:17:23 +01:00
|
|
|
#include "editor/editor_resource_preview.h"
|
2024-04-21 10:11:35 +02:00
|
|
|
#include "editor/plugins/editor_plugin.h"
|
2017-04-30 16:27:10 +02:00
|
|
|
#include "scene/resources/curve.h"
|
2017-04-07 04:36:37 +02:00
|
|
|
|
2023-03-13 23:03:36 +01:00
|
|
|
class EditorSpinSlider;
|
|
|
|
class MenuButton;
|
2023-04-12 21:02:28 +02:00
|
|
|
class PopupMenu;
|
|
|
|
|
2023-03-13 23:03:36 +01:00
|
|
|
class CurveEdit : public Control {
|
|
|
|
GDCLASS(CurveEdit, Control);
|
2019-03-19 19:35:57 +01:00
|
|
|
|
2017-04-30 16:27:10 +02:00
|
|
|
public:
|
2023-03-13 23:03:36 +01:00
|
|
|
CurveEdit();
|
2017-04-07 04:36:37 +02:00
|
|
|
|
2023-03-13 23:03:36 +01:00
|
|
|
void set_snap_enabled(bool p_enabled);
|
|
|
|
void set_snap_count(int p_snap_count);
|
|
|
|
void use_preset(int p_preset_id);
|
|
|
|
|
|
|
|
void set_curve(Ref<Curve> p_curve);
|
|
|
|
Ref<Curve> get_curve();
|
2017-04-07 04:36:37 +02:00
|
|
|
|
2023-03-13 23:03:36 +01:00
|
|
|
Size2 get_minimum_size() const override;
|
2017-04-07 04:36:37 +02:00
|
|
|
|
2017-04-30 16:27:10 +02:00
|
|
|
enum PresetID {
|
2023-03-13 23:03:36 +01:00
|
|
|
PRESET_CONSTANT = 0,
|
2017-04-30 16:27:10 +02:00
|
|
|
PRESET_LINEAR,
|
|
|
|
PRESET_EASE_IN,
|
|
|
|
PRESET_EASE_OUT,
|
|
|
|
PRESET_SMOOTHSTEP,
|
|
|
|
PRESET_COUNT
|
2017-04-07 04:36:37 +02:00
|
|
|
};
|
|
|
|
|
2017-04-30 16:27:10 +02:00
|
|
|
enum TangentIndex {
|
|
|
|
TANGENT_NONE = -1,
|
|
|
|
TANGENT_LEFT = 0,
|
|
|
|
TANGENT_RIGHT = 1
|
|
|
|
};
|
2017-04-07 04:36:37 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void _notification(int p_what);
|
2023-03-13 23:03:36 +01:00
|
|
|
static void _bind_methods();
|
2017-04-30 16:27:10 +02:00
|
|
|
|
|
|
|
private:
|
2021-08-22 17:37:22 +02:00
|
|
|
virtual void gui_input(const Ref<InputEvent> &p_event) override;
|
2017-04-30 16:27:10 +02:00
|
|
|
void _curve_changed();
|
2023-03-13 23:03:36 +01:00
|
|
|
|
|
|
|
int get_point_at(Vector2 p_pos) const;
|
|
|
|
TangentIndex get_tangent_at(Vector2 p_pos) const;
|
|
|
|
|
|
|
|
float get_offset_without_collision(int p_current_index, float p_offset, bool p_prioritize_right = true);
|
|
|
|
|
|
|
|
void add_point(Vector2 p_pos);
|
|
|
|
void remove_point(int p_index);
|
|
|
|
void set_point_position(int p_index, Vector2 p_pos);
|
|
|
|
|
|
|
|
void set_point_tangents(int p_index, float p_left, float p_right);
|
|
|
|
void set_point_left_tangent(int p_index, float p_tangent);
|
|
|
|
void set_point_right_tangent(int p_index, float p_tangent);
|
|
|
|
void toggle_linear(int p_index, TangentIndex p_tangent = TANGENT_NONE);
|
|
|
|
|
2017-04-30 16:27:10 +02:00
|
|
|
void update_view_transform();
|
|
|
|
|
2023-03-13 23:03:36 +01:00
|
|
|
void set_selected_index(int p_index);
|
|
|
|
void set_selected_tangent_index(TangentIndex p_tangent);
|
|
|
|
|
|
|
|
Vector2 get_tangent_view_pos(int p_index, TangentIndex p_tangent) const;
|
|
|
|
Vector2 get_view_pos(Vector2 p_world_pos) const;
|
|
|
|
Vector2 get_world_pos(Vector2 p_view_pos) const;
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2023-03-13 23:03:36 +01:00
|
|
|
void _redraw();
|
2017-04-30 16:27:10 +02:00
|
|
|
|
|
|
|
private:
|
2023-05-29 21:09:44 +02:00
|
|
|
const float ASPECT_RATIO = 6.f / 13.f;
|
|
|
|
|
2017-04-30 16:27:10 +02:00
|
|
|
Transform2D _world_to_view;
|
|
|
|
|
2023-03-13 23:03:36 +01:00
|
|
|
Ref<Curve> curve;
|
2022-04-04 15:06:57 +02:00
|
|
|
PopupMenu *_presets_menu = nullptr;
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2023-03-13 23:03:36 +01:00
|
|
|
int selected_index = -1;
|
|
|
|
int hovered_index = -1;
|
|
|
|
TangentIndex selected_tangent_index = TANGENT_NONE;
|
|
|
|
TangentIndex hovered_tangent_index = TANGENT_NONE;
|
|
|
|
|
|
|
|
// Make sure to use the scaled values below.
|
|
|
|
const int BASE_POINT_RADIUS = 4;
|
|
|
|
const int BASE_HOVER_RADIUS = 10;
|
|
|
|
const int BASE_TANGENT_RADIUS = 3;
|
|
|
|
const int BASE_TANGENT_HOVER_RADIUS = 8;
|
|
|
|
const int BASE_TANGENT_LENGTH = 36;
|
|
|
|
|
|
|
|
int point_radius = BASE_POINT_RADIUS;
|
|
|
|
int hover_radius = BASE_HOVER_RADIUS;
|
|
|
|
int tangent_radius = BASE_TANGENT_RADIUS;
|
|
|
|
int tangent_hover_radius = BASE_TANGENT_HOVER_RADIUS;
|
|
|
|
int tangent_length = BASE_TANGENT_LENGTH;
|
|
|
|
|
|
|
|
enum GrabMode {
|
|
|
|
GRAB_NONE,
|
|
|
|
GRAB_ADD,
|
|
|
|
GRAB_MOVE
|
|
|
|
};
|
|
|
|
GrabMode grabbing = GRAB_NONE;
|
|
|
|
Vector2 initial_grab_pos;
|
|
|
|
int initial_grab_index;
|
|
|
|
float initial_grab_left_tangent;
|
|
|
|
float initial_grab_right_tangent;
|
|
|
|
|
|
|
|
bool snap_enabled = false;
|
|
|
|
int snap_count = 10;
|
|
|
|
};
|
|
|
|
|
|
|
|
// CurveEdit + toolbar
|
|
|
|
class CurveEditor : public VBoxContainer {
|
|
|
|
GDCLASS(CurveEditor, VBoxContainer);
|
|
|
|
|
|
|
|
// Make sure to use the scaled values below.
|
|
|
|
const int BASE_SPACING = 4;
|
|
|
|
int spacing = BASE_SPACING;
|
|
|
|
|
|
|
|
Button *snap_button = nullptr;
|
|
|
|
EditorSpinSlider *snap_count_edit = nullptr;
|
|
|
|
MenuButton *presets_button = nullptr;
|
|
|
|
CurveEdit *curve_editor_rect = nullptr;
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2023-03-13 23:03:36 +01:00
|
|
|
void _set_snap_enabled(bool p_enabled);
|
|
|
|
void _set_snap_count(int p_snap_count);
|
|
|
|
void _on_preset_item_selected(int p_preset_id);
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2023-03-13 23:03:36 +01:00
|
|
|
protected:
|
|
|
|
void _notification(int p_what);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static const int DEFAULT_SNAP;
|
|
|
|
void set_curve(const Ref<Curve> &p_curve);
|
|
|
|
|
|
|
|
CurveEditor();
|
2017-04-07 04:36:37 +02:00
|
|
|
};
|
|
|
|
|
2018-05-17 23:02:16 +02:00
|
|
|
class EditorInspectorPluginCurve : public EditorInspectorPlugin {
|
2019-03-19 19:35:57 +01:00
|
|
|
GDCLASS(EditorInspectorPluginCurve, EditorInspectorPlugin);
|
|
|
|
|
2018-05-17 23:02:16 +02:00
|
|
|
public:
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual bool can_handle(Object *p_object) override;
|
|
|
|
virtual void parse_begin(Object *p_object) override;
|
2018-05-17 23:02:16 +02:00
|
|
|
};
|
|
|
|
|
2017-04-30 16:27:10 +02:00
|
|
|
class CurveEditorPlugin : public EditorPlugin {
|
2019-03-19 19:35:57 +01:00
|
|
|
GDCLASS(CurveEditorPlugin, EditorPlugin);
|
|
|
|
|
2017-04-30 16:27:10 +02:00
|
|
|
public:
|
2022-01-27 10:36:51 +01:00
|
|
|
CurveEditorPlugin();
|
2017-04-07 04:36:37 +02:00
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual String get_name() const override { return "Curve"; }
|
2017-04-07 04:36:37 +02:00
|
|
|
};
|
|
|
|
|
2017-07-02 01:46:44 +02:00
|
|
|
class CurvePreviewGenerator : public EditorResourcePreviewGenerator {
|
2019-03-19 19:35:57 +01:00
|
|
|
GDCLASS(CurvePreviewGenerator, EditorResourcePreviewGenerator);
|
|
|
|
|
2017-07-02 01:46:44 +02:00
|
|
|
public:
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual bool handles(const String &p_type) const override;
|
2022-08-19 18:14:57 +02:00
|
|
|
virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const override;
|
2017-07-02 01:46:44 +02:00
|
|
|
};
|
|
|
|
|
2017-04-07 04:36:37 +02:00
|
|
|
#endif // CURVE_EDITOR_PLUGIN_H
|