diff --git a/README.md b/README.md index 1af5dd7..68a6871 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ This repository contains the add-on and examples. +## Fork + +This fork adds a custom node named FloatingLegend that displays the legend of a Graph2D. It is used when you don't want to show the legend inside the plot area, but display it somewhere else. A boolean parameter is added to Graph2D to not display the legend. + ## Features - Display several plots on the same graph. diff --git a/addons/graph_2d/custom_nodes/legend.gd b/addons/graph_2d/custom_nodes/legend.gd index 6a1ee0c..e6adeaa 100644 --- a/addons/graph_2d/custom_nodes/legend.gd +++ b/addons/graph_2d/custom_nodes/legend.gd @@ -1,5 +1,6 @@ @tool extends Control +class_name Legend var layout := VBoxContainer.new() diff --git a/addons/graph_2d/custom_refcounted/plot_item.gd b/addons/graph_2d/custom_refcounted/plot_item.gd index faf4050..9c833e7 100644 --- a/addons/graph_2d/custom_refcounted/plot_item.gd +++ b/addons/graph_2d/custom_refcounted/plot_item.gd @@ -39,6 +39,7 @@ func _init(obj, l, c, w): label = l _curve.name = l _curve.color = c + color = c _curve.width = w _graph.get_node("PlotArea").add_child(_curve) diff --git a/addons/graph_2d/floating_legend.gd b/addons/graph_2d/floating_legend.gd new file mode 100644 index 0000000..6856af4 --- /dev/null +++ b/addons/graph_2d/floating_legend.gd @@ -0,0 +1,45 @@ +@tool +extends Control +class_name FloatingLegend +## Display the legend of a [Graph2D] +## +## This node will display the list of [PoltItem] names that a [Graph2D] +## has. It is used as alternative to the legend displayed inside the +## plot area. You can hide the legend of the plot area by setting the +## show_legend parameter of the [Graph2D]. +## +## tutorial: https://www.reddit.com/r/godot/comments/1jkc0al/graph2d_plugin_floating_legend_branch/ + +## The [Graph2D] of which's legend will be displayed. +@export var graph : Graph2D + +const _Graph2DLegend = preload("res://addons/graph_2d/custom_nodes/legend.gd") +var legend: Legend +var label_list : VBoxContainer +var backgrnound: ColorRect + + +func _ready() -> void: + legend = _Graph2DLegend.new() + add_child(legend) + label_list = legend.get_child(0) as VBoxContainer + label_list.resized.connect(_on_resized) + if graph : + graph.legend_updated.connect(_on_graph_legend_updated) + backgrnound = ColorRect.new() + backgrnound.layout_mode = 1 + backgrnound.anchors_preset = Control.PRESET_FULL_RECT + backgrnound.color = graph.background_color + add_child(backgrnound) + move_child(backgrnound,0) + else : + printerr("No graph declared for the floating legend node") + + +func _on_graph_legend_updated(labels:Array): + legend.update(labels) + + +func _on_resized() -> void: + size = label_list.size + Vector2(20,40) + diff --git a/addons/graph_2d/graph_2d.gd b/addons/graph_2d/graph_2d.gd index b19b3fc..c549d54 100644 --- a/addons/graph_2d/graph_2d.gd +++ b/addons/graph_2d/graph_2d.gd @@ -90,8 +90,12 @@ extends Control _update_graph() @export_group("Background") +## Show legend on graph +@export var show_legend := true: + set(value): + show_legend = value ## Background color of graph -@export var background_color = Color.BLACK: +@export var background_color := Color.BLACK: set(value): background_color = value if get_node_or_null("Background"): @@ -136,6 +140,8 @@ var _plots: Array var _x_step: float var _y_step: float +signal legend_updated + #endregion func _ready(): @@ -333,7 +339,11 @@ func _update_legend() -> void: name = p.label, color = p.color, }) - get_node("PlotArea/Legend").update(labels) + if show_legend: + get_node("PlotArea/Legend").update(labels) + else : + get_node("PlotArea/Legend").update([]) + legend_updated.emit(labels) func _on_Graph_resized() -> void: _update_graph() diff --git a/addons/graph_2d/legend.svg b/addons/graph_2d/legend.svg new file mode 100644 index 0000000..0d1b18b --- /dev/null +++ b/addons/graph_2d/legend.svg @@ -0,0 +1,64 @@ + + + + + + image/svg+xml + + + + + + + + + diff --git a/addons/graph_2d/plugin.gd b/addons/graph_2d/plugin.gd index d0d4f06..de5ddb4 100644 --- a/addons/graph_2d/plugin.gd +++ b/addons/graph_2d/plugin.gd @@ -6,9 +6,14 @@ func _enter_tree() -> void: # Initialization of the plugin goes here. add_custom_type("Graph2D", "Control", preload("res://addons/graph_2d/graph_2d.gd"), - preload("res://addons/graph_2d/graph_2d.svg")) - + preload("res://addons/graph_2d/graph_2d.svg"), + ) + add_custom_type("FloatingLegend", "Control", + preload("res://addons/graph_2d/floating_legend.gd"), + preload("res://addons/graph_2d/legend.svg"), + ) func _exit_tree() -> void: # Clean-up of the plugin goes here. remove_custom_type("Graph2D") + remove_custom_type("FloatingLegend") diff --git a/examples/graph_with_legend.tscn b/examples/graph_with_legend.tscn new file mode 100644 index 0000000..527d9b9 --- /dev/null +++ b/examples/graph_with_legend.tscn @@ -0,0 +1,56 @@ +[gd_scene load_steps=4 format=3 uid="uid://desy7txx62pb2"] + +[ext_resource type="Script" path="res://examples/single_plot.gd" id="1_62wlx"] +[ext_resource type="Script" path="res://addons/graph_2d/graph_2d.gd" id="2_y0q6u"] +[ext_resource type="Script" path="res://addons/graph_2d/floating_legend.gd" id="3_7par3"] + +[node name="SinglePlot" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1_62wlx") + +[node name="Graph2D" type="Control" parent="."] +anchors_preset = 0 +offset_left = 282.0 +offset_right = 1141.0 +offset_bottom = 635.0 +script = ExtResource("2_y0q6u") +x_label = "X" +y_min = -53.0 +y_max = 50.0 +show_legend = false +background_color = Color(0.280876, 0.373644, 0.701865, 1) +grid_horizontal_visible = true +grid_vertical_visible = true + +[node name="AddPlot" type="Button" parent="."] +layout_mode = 0 +offset_left = 97.0 +offset_top = 37.0 +offset_right = 210.0 +offset_bottom = 68.0 +text = "Add New Plot" + +[node name="RemoveAllPlots" type="Button" parent="."] +layout_mode = 0 +offset_left = 69.0 +offset_top = 98.0 +offset_right = 206.0 +offset_bottom = 129.0 +text = "Remove All Plots" + +[node name="FloatingLegend" type="Control" parent="." node_paths=PackedStringArray("graph")] +anchors_preset = 0 +offset_left = 52.4538 +offset_top = 281.007 +offset_right = 92.4538 +offset_bottom = 321.007 +script = ExtResource("3_7par3") +graph = NodePath("../Graph2D") + +[connection signal="pressed" from="AddPlot" to="." method="_on_add_plot_pressed"] +[connection signal="pressed" from="RemoveAllPlots" to="." method="_on_remove_all_plots_pressed"] diff --git a/examples/single_plot.tscn b/examples/single_plot.tscn index 6c2e4c9..c6a7d91 100644 --- a/examples/single_plot.tscn +++ b/examples/single_plot.tscn @@ -24,6 +24,7 @@ x_label = "Time(s)" y_min = -53.0 y_max = 50.0 y_label = "Y" +show_legend = null background_color = Color(0.0941176, 0.227451, 0.4, 1) grid_horizontal_visible = true grid_vertical_visible = true