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