Path: blob/master/editor/scene/curve_editor_plugin.h
21345 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;113114int selected_index = -1;115int hovered_index = -1;116TangentIndex selected_tangent_index = TANGENT_NONE;117TangentIndex hovered_tangent_index = TANGENT_NONE;118119// Make sure to use the scaled values below.120const int BASE_POINT_RADIUS = 4;121const int BASE_HOVER_RADIUS = 10;122const int BASE_TANGENT_RADIUS = 3;123const int BASE_TANGENT_HOVER_RADIUS = 8;124const int BASE_TANGENT_LENGTH = 36;125126int point_radius = BASE_POINT_RADIUS;127int hover_radius = BASE_HOVER_RADIUS;128int tangent_radius = BASE_TANGENT_RADIUS;129int tangent_hover_radius = BASE_TANGENT_HOVER_RADIUS;130int tangent_length = BASE_TANGENT_LENGTH;131132enum GrabMode {133GRAB_NONE,134GRAB_ADD,135GRAB_MOVE136};137GrabMode grabbing = GRAB_NONE;138Vector2 initial_grab_pos;139int initial_grab_index = -1;140float initial_grab_left_tangent = 0;141float initial_grab_right_tangent = 0;142143bool snap_enabled = false;144int snap_count = 10;145};146147// CurveEdit + toolbar148class CurveEditor : public VBoxContainer {149GDCLASS(CurveEditor, VBoxContainer);150151// Make sure to use the scaled values below.152const int BASE_SPACING = 4;153int spacing = BASE_SPACING;154155Button *snap_button = nullptr;156EditorSpinSlider *snap_count_edit = nullptr;157MenuButton *presets_button = nullptr;158CurveEdit *curve_editor_rect = nullptr;159160void _set_snap_enabled(bool p_enabled);161void _set_snap_count(int p_snap_count);162void _on_preset_item_selected(int p_preset_id);163164protected:165void _notification(int p_what);166167public:168static const int DEFAULT_SNAP;169void set_curve(const Ref<Curve> &p_curve);170171CurveEditor();172};173174class EditorInspectorPluginCurve : public EditorInspectorPlugin {175GDCLASS(EditorInspectorPluginCurve, EditorInspectorPlugin);176177public:178virtual bool can_handle(Object *p_object) override;179virtual void parse_begin(Object *p_object) override;180};181182class CurveEditorPlugin : public EditorPlugin {183GDCLASS(CurveEditorPlugin, EditorPlugin);184185public:186CurveEditorPlugin();187188virtual String get_plugin_name() const override { return "Curve"; }189};190191class CurvePreviewGenerator : public EditorResourcePreviewGenerator {192GDCLASS(CurvePreviewGenerator, EditorResourcePreviewGenerator);193194public:195virtual bool handles(const String &p_type) const override;196virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const override;197};198199200