Path: blob/master/editor/scene/curve_editor_plugin.h
10277 views
/**************************************************************************/1/* curve_editor_plugin.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "editor/inspector/editor_inspector.h"33#include "editor/inspector/editor_resource_preview.h"34#include "editor/plugins/editor_plugin.h"35#include "scene/resources/curve.h"3637class EditorSpinSlider;38class MenuButton;39class PopupMenu;4041class CurveEdit : public Control {42GDCLASS(CurveEdit, Control);4344public:45CurveEdit();4647void set_snap_enabled(bool p_enabled);48void set_snap_count(int p_snap_count);49void use_preset(int p_preset_id);5051void set_curve(Ref<Curve> p_curve);52Ref<Curve> get_curve();5354Size2 get_minimum_size() const override;5556enum PresetID {57PRESET_CONSTANT = 0,58PRESET_LINEAR,59PRESET_EASE_IN,60PRESET_EASE_OUT,61PRESET_SMOOTHSTEP,62PRESET_COUNT63};6465enum TangentIndex {66TANGENT_NONE = -1,67TANGENT_LEFT = 0,68TANGENT_RIGHT = 169};7071protected:72void _notification(int p_what);73static void _bind_methods();7475private:76virtual void gui_input(const Ref<InputEvent> &p_event) override;77void _curve_changed();7879int get_point_at(const Vector2 &p_pos) const;80TangentIndex get_tangent_at(const Vector2 &p_pos) const;8182float get_offset_without_collision(int p_current_index, float p_offset, bool p_prioritize_right = true);8384void add_point(const Vector2 &p_pos);85void remove_point(int p_index);86void set_point_position(int p_index, const Vector2 &p_pos);8788void set_point_tangents(int p_index, float p_left, float p_right);89void set_point_left_tangent(int p_index, float p_tangent);90void set_point_right_tangent(int p_index, float p_tangent);91void toggle_linear(int p_index, TangentIndex p_tangent = TANGENT_NONE);9293void update_view_transform();9495void plot_curve_accurate(float p_step, const Color &p_line_color, const Color &p_edge_line_color);9697void set_selected_index(int p_index);9899Vector2 get_tangent_view_pos(int p_index, TangentIndex p_tangent) const;100Vector2 get_view_pos(const Vector2 &p_world_pos) const;101Vector2 get_world_pos(const Vector2 &p_view_pos) const;102103void _redraw();104105private:106const float ASPECT_RATIO = 6.f / 13.f;107const float LINE_WIDTH = 0.5f;108const int STEP_SIZE = 2; // Number of pixels between plot points.109110Transform2D _world_to_view;111112Ref<Curve> curve;113PopupMenu *_presets_menu = nullptr;114115int selected_index = -1;116int hovered_index = -1;117TangentIndex selected_tangent_index = TANGENT_NONE;118TangentIndex hovered_tangent_index = TANGENT_NONE;119120// Make sure to use the scaled values below.121const int BASE_POINT_RADIUS = 4;122const int BASE_HOVER_RADIUS = 10;123const int BASE_TANGENT_RADIUS = 3;124const int BASE_TANGENT_HOVER_RADIUS = 8;125const int BASE_TANGENT_LENGTH = 36;126127int point_radius = BASE_POINT_RADIUS;128int hover_radius = BASE_HOVER_RADIUS;129int tangent_radius = BASE_TANGENT_RADIUS;130int tangent_hover_radius = BASE_TANGENT_HOVER_RADIUS;131int tangent_length = BASE_TANGENT_LENGTH;132133enum GrabMode {134GRAB_NONE,135GRAB_ADD,136GRAB_MOVE137};138GrabMode grabbing = GRAB_NONE;139Vector2 initial_grab_pos;140int initial_grab_index = -1;141float initial_grab_left_tangent = 0;142float initial_grab_right_tangent = 0;143144bool snap_enabled = false;145int snap_count = 10;146};147148// CurveEdit + toolbar149class CurveEditor : public VBoxContainer {150GDCLASS(CurveEditor, VBoxContainer);151152// Make sure to use the scaled values below.153const int BASE_SPACING = 4;154int spacing = BASE_SPACING;155156Button *snap_button = nullptr;157EditorSpinSlider *snap_count_edit = nullptr;158MenuButton *presets_button = nullptr;159CurveEdit *curve_editor_rect = nullptr;160161void _set_snap_enabled(bool p_enabled);162void _set_snap_count(int p_snap_count);163void _on_preset_item_selected(int p_preset_id);164165protected:166void _notification(int p_what);167168public:169static const int DEFAULT_SNAP;170void set_curve(const Ref<Curve> &p_curve);171172CurveEditor();173};174175class EditorInspectorPluginCurve : public EditorInspectorPlugin {176GDCLASS(EditorInspectorPluginCurve, EditorInspectorPlugin);177178public:179virtual bool can_handle(Object *p_object) override;180virtual void parse_begin(Object *p_object) override;181};182183class CurveEditorPlugin : public EditorPlugin {184GDCLASS(CurveEditorPlugin, EditorPlugin);185186public:187CurveEditorPlugin();188189virtual String get_plugin_name() const override { return "Curve"; }190};191192class CurvePreviewGenerator : public EditorResourcePreviewGenerator {193GDCLASS(CurvePreviewGenerator, EditorResourcePreviewGenerator);194195public:196virtual bool handles(const String &p_type) const override;197virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const override;198};199200201