2017-04-29 02:04:09 +08: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 22:27:10 +08:00
|
|
|
|
2017-04-07 10:36:37 +08:00
|
|
|
#ifndef CURVE_EDITOR_PLUGIN_H
|
|
|
|
#define CURVE_EDITOR_PLUGIN_H
|
|
|
|
|
2022-11-12 03:12:48 +08:00
|
|
|
#include "editor/editor_inspector.h"
|
2017-04-07 10:36:37 +08:00
|
|
|
#include "editor/editor_plugin.h"
|
2019-12-24 15:17:23 +08:00
|
|
|
#include "editor/editor_resource_preview.h"
|
2017-04-30 22:27:10 +08:00
|
|
|
#include "scene/resources/curve.h"
|
2017-04-07 10:36:37 +08:00
|
|
|
|
2023-03-14 06:03:36 +08:00
|
|
|
class EditorSpinSlider;
|
|
|
|
class MenuButton;
|
2023-04-13 03:02:28 +08:00
|
|
|
class PopupMenu;
|
|
|
|
|
2023-03-14 06:03:36 +08:00
|
|
|
class CurveEdit : public Control {
|
|
|
|
GDCLASS(CurveEdit, Control);
|
2019-03-20 02:35:57 +08:00
|
|
|
|
2017-04-30 22:27:10 +08:00
|
|
|
public:
|
2023-03-14 06:03:36 +08:00
|
|
|
CurveEdit();
|
2017-04-07 10:36:37 +08:00
|
|
|
|
2023-03-14 06:03:36 +08: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 10:36:37 +08:00
|
|
|
|
2023-03-14 06:03:36 +08:00
|
|
|
Size2 get_minimum_size() const override;
|
2017-04-07 10:36:37 +08:00
|
|
|
|
2017-04-30 22:27:10 +08:00
|
|
|
enum PresetID {
|
2023-03-14 06:03:36 +08:00
|
|
|
PRESET_CONSTANT = 0,
|
2017-04-30 22:27:10 +08:00
|
|
|
PRESET_LINEAR,
|
|
|
|
PRESET_EASE_IN,
|
|
|
|
PRESET_EASE_OUT,
|
|
|
|
PRESET_SMOOTHSTEP,
|
|
|
|
PRESET_COUNT
|
2017-04-07 10:36:37 +08:00
|
|
|
};
|
|
|
|
|
2017-04-30 22:27:10 +08:00
|
|
|
enum TangentIndex {
|
|
|
|
TANGENT_NONE = -1,
|
|
|
|
TANGENT_LEFT = 0,
|
|
|
|
TANGENT_RIGHT = 1
|
|
|
|
};
|
2017-04-07 10:36:37 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void _notification(int p_what);
|
2023-03-14 06:03:36 +08:00
|
|
|
static void _bind_methods();
|
2017-04-30 22:27:10 +08:00
|
|
|
|
|
|
|
private:
|
2021-08-22 23:37:22 +08:00
|
|
|
virtual void gui_input(const Ref<InputEvent> &p_event) override;
|
2017-04-30 22:27:10 +08:00
|
|
|
void _curve_changed();
|
2023-03-14 06:03:36 +08: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 22:27:10 +08:00
|
|
|
void update_view_transform();
|
|
|
|
|
2023-03-14 06:03:36 +08: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 22:27:10 +08:00
|
|
|
|
2023-03-14 06:03:36 +08:00
|
|
|
void _redraw();
|
2017-04-30 22:27:10 +08:00
|
|
|
|
|
|
|
private:
|
2023-05-30 03:09:44 +08:00
|
|
|
const float ASPECT_RATIO = 6.f / 13.f;
|
|
|
|
|
2017-04-30 22:27:10 +08:00
|
|
|
Transform2D _world_to_view;
|
|
|
|
|
2023-03-14 06:03:36 +08:00
|
|
|
Ref<Curve> curve;
|
2022-04-04 21:06:57 +08:00
|
|
|
PopupMenu *_presets_menu = nullptr;
|
2017-04-30 22:27:10 +08:00
|
|
|
|
2023-03-14 06:03:36 +08: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 22:27:10 +08:00
|
|
|
|
2023-03-14 06:03:36 +08: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 22:27:10 +08:00
|
|
|
|
2023-03-14 06:03:36 +08: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 10:36:37 +08:00
|
|
|
};
|
|
|
|
|
2018-05-18 05:02:16 +08:00
|
|
|
class EditorInspectorPluginCurve : public EditorInspectorPlugin {
|
2019-03-20 02:35:57 +08:00
|
|
|
GDCLASS(EditorInspectorPluginCurve, EditorInspectorPlugin);
|
|
|
|
|
2018-05-18 05:02:16 +08:00
|
|
|
public:
|
2020-07-10 18:34:39 +08:00
|
|
|
virtual bool can_handle(Object *p_object) override;
|
|
|
|
virtual void parse_begin(Object *p_object) override;
|
2018-05-18 05:02:16 +08:00
|
|
|
};
|
|
|
|
|
2017-04-30 22:27:10 +08:00
|
|
|
class CurveEditorPlugin : public EditorPlugin {
|
2019-03-20 02:35:57 +08:00
|
|
|
GDCLASS(CurveEditorPlugin, EditorPlugin);
|
|
|
|
|
2017-04-30 22:27:10 +08:00
|
|
|
public:
|
2022-01-27 17:36:51 +08:00
|
|
|
CurveEditorPlugin();
|
2017-04-07 10:36:37 +08:00
|
|
|
|
2020-07-10 18:34:39 +08:00
|
|
|
virtual String get_name() const override { return "Curve"; }
|
2017-04-07 10:36:37 +08:00
|
|
|
};
|
|
|
|
|
2017-07-02 07:46:44 +08:00
|
|
|
class CurvePreviewGenerator : public EditorResourcePreviewGenerator {
|
2019-03-20 02:35:57 +08:00
|
|
|
GDCLASS(CurvePreviewGenerator, EditorResourcePreviewGenerator);
|
|
|
|
|
2017-07-02 07:46:44 +08:00
|
|
|
public:
|
2020-07-10 18:34:39 +08:00
|
|
|
virtual bool handles(const String &p_type) const override;
|
2022-08-20 00:14:57 +08:00
|
|
|
virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const override;
|
2017-07-02 07:46:44 +08:00
|
|
|
};
|
|
|
|
|
2017-04-07 10:36:37 +08:00
|
|
|
#endif // CURVE_EDITOR_PLUGIN_H
|