From 174a3aa450eb3cc2ab8e10650aef1728b4873a5c Mon Sep 17 00:00:00 2001 From: Flairieve Date: Thu, 8 Aug 2019 03:49:17 -0500 Subject: [PATCH] Fixed ColorPicker and added time debug class --- addons/graphics_editor/ColorPicker.gd | 32 ++++++++++++++++++++++ addons/graphics_editor/Editor.tscn | 21 ++++++++------ addons/graphics_editor/PaintCanvas.gd | 23 +++++++++++----- addons/graphics_editor/PaintCanvasChunk.gd | 3 ++ addons/graphics_editor/Settings.tscn | 1 - addons/graphics_editor/Util.gd | 15 ++++++++++ 6 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 addons/graphics_editor/ColorPicker.gd diff --git a/addons/graphics_editor/ColorPicker.gd b/addons/graphics_editor/ColorPicker.gd new file mode 100644 index 0000000..ce1f509 --- /dev/null +++ b/addons/graphics_editor/ColorPicker.gd @@ -0,0 +1,32 @@ +tool +extends ColorPickerButton + +var color_picking = false +var mouse_on_top = false + +func _ready(): + var color_picker = get_picker() + var color_picker_button = color_picker.get_children()[0].get_children()[1] + color_picker_button.disconnect("pressed", color_picker, "_screen_pick_pressed") + color_picker_button.connect("pressed", self, "color_picker_button_pressed") + +func _process(delta): + if color_picking and not mouse_on_top: + var editor = get_node("/root/Editor") + var paint_canvas = get_node("/root/Editor/PaintCanvasContainer/PaintCanvas") + var highlighted_color = paint_canvas.get_pixel_cell_color_v(editor.cell_mouse_position) + if not highlighted_color == null: + color = highlighted_color + +func color_picker_button_pressed(): + if not color_picking: + color_picking = true + +func _on_ColorPicker_focus_exited(): + color_picking = false + +func _on_ColorPicker_mouse_entered(): + mouse_on_top = true + +func _on_ColorPicker_mouse_exited(): + mouse_on_top = false diff --git a/addons/graphics_editor/Editor.tscn b/addons/graphics_editor/Editor.tscn index d589069..aa29fea 100644 --- a/addons/graphics_editor/Editor.tscn +++ b/addons/graphics_editor/Editor.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=11 format=2] +[gd_scene load_steps=12 format=2] [ext_resource path="res://addons/graphics_editor/Editor.gd" type="Script" id=1] [ext_resource path="res://addons/graphics_editor/PaintCanvasContainer.gd" type="Script" id=2] @@ -7,9 +7,10 @@ [ext_resource path="res://addons/graphics_editor/CanvasOutline.gd" type="Script" id=5] [ext_resource path="res://addons/graphics_editor/Navbar.gd" type="Script" id=6] [ext_resource path="res://addons/graphics_editor/MenuButtonExtended.gd" type="Script" id=7] -[ext_resource path="res://addons/graphics_editor/TextInfo.gd" type="Script" id=8] -[ext_resource path="res://addons/graphics_editor/SaveFileDialog.gd" type="Script" id=9] -[ext_resource path="res://addons/graphics_editor/Settings.tscn" type="PackedScene" id=10] +[ext_resource path="res://addons/graphics_editor/ColorPicker.gd" type="Script" id=8] +[ext_resource path="res://addons/graphics_editor/TextInfo.gd" type="Script" id=9] +[ext_resource path="res://addons/graphics_editor/SaveFileDialog.gd" type="Script" id=10] +[ext_resource path="res://addons/graphics_editor/Settings.tscn" type="PackedScene" id=11] [node name="Editor" type="Control"] anchor_right = 1.0 @@ -51,7 +52,7 @@ color = Color( 0.639216, 0.639216, 0.639216, 1 ) [node name="VisualGrid2" parent="PaintCanvasContainer/PaintCanvas/Grids" instance=ExtResource( 4 )] mouse_filter = 2 color = Color( 1, 1, 1, 1 ) -size = 160.0 +size = 160 [node name="CanvasOutline" type="Control" parent="PaintCanvasContainer/PaintCanvas"] anchor_right = 1.0 @@ -126,6 +127,7 @@ text = "Rainbow Tool" anchor_right = 1.0 margin_top = 210.0 margin_bottom = 240.0 +script = ExtResource( 8 ) [node name="Layers" type="Control" parent="ToolMenu"] anchor_right = 1.0 @@ -146,7 +148,7 @@ anchor_bottom = 1.0 margin_top = -40.0 [node name="TextInfo" type="Control" parent="BottomPanel"] -script = ExtResource( 8 ) +script = ExtResource( 9 ) [node name="SaveFileDialog" type="FileDialog" parent="."] anchor_left = 0.5 @@ -158,9 +160,9 @@ margin_top = -165.0 margin_right = 340.0 margin_bottom = 165.0 filters = PoolStringArray( "*.png ; PNG Images" ) -script = ExtResource( 9 ) +script = ExtResource( 10 ) -[node name="Settings" parent="." instance=ExtResource( 10 )] +[node name="Settings" parent="." instance=ExtResource( 11 )] anchor_left = 0.5 anchor_top = 0.5 anchor_right = 0.5 @@ -175,6 +177,9 @@ margin_bottom = 150.0 [connection signal="pressed" from="ToolMenu/Buttons/BucketTool" to="." method="_on_BucketTool_pressed"] [connection signal="pressed" from="ToolMenu/Buttons/RainbowTool" to="." method="_on_RainbowTool_pressed"] [connection signal="color_changed" from="ToolMenu/Buttons/ColorPicker" to="." method="_on_ColorPicker_color_changed"] +[connection signal="focus_exited" from="ToolMenu/Buttons/ColorPicker" to="ToolMenu/Buttons/ColorPicker" method="_on_ColorPicker_focus_exited"] +[connection signal="mouse_entered" from="ToolMenu/Buttons/ColorPicker" to="ToolMenu/Buttons/ColorPicker" method="_on_ColorPicker_mouse_entered"] +[connection signal="mouse_exited" from="ToolMenu/Buttons/ColorPicker" to="ToolMenu/Buttons/ColorPicker" method="_on_ColorPicker_mouse_exited"] [connection signal="about_to_show" from="SaveFileDialog" to="SaveFileDialog" method="_on_SaveFileDialog_about_to_show"] [connection signal="confirmed" from="SaveFileDialog" to="SaveFileDialog" method="_on_SaveFileDialog_confirmed"] [connection signal="file_selected" from="SaveFileDialog" to="SaveFileDialog" method="_on_SaveFileDialog_file_selected"] diff --git a/addons/graphics_editor/PaintCanvas.gd b/addons/graphics_editor/PaintCanvas.gd index a283b4b..09f3722 100644 --- a/addons/graphics_editor/PaintCanvas.gd +++ b/addons/graphics_editor/PaintCanvas.gd @@ -283,8 +283,18 @@ func get_pixels_from_line(vec2_1, vec2_2): #even though the function checks for it, we can't afford adding more functions to the call stack #because godot has a limit until it crashes -var flood_fill_queue = 0 + func flood_fill(x, y, target_color, replacement_color): + var time_debug = util.time_debug.new() + time_debug.start() + flood_fill_main(x, y, target_color, replacement_color) + time_debug.end() + print("Bucket fill time passed: msec: %s | sec: %s" % [time_debug.get_time_passed(), time_debug.get_time_passed() / float(1000)]) + return + +var flood_fill_queue = 0 +func flood_fill_main(x, y, target_color, replacement_color): + #yield(get_tree().create_timer(1), "timeout") flood_fill_queue += 1 if not cell_in_canvas_region(x, y): @@ -298,21 +308,20 @@ func flood_fill(x, y, target_color, replacement_color): return else: set_pixel_cell(x, y, replacement_color) - if flood_fill_queue >= 500: - print(flood_fill_queue) + if flood_fill_queue >= 200: yield(get_tree().create_timer(0.01), "timeout") #up if get_pixel_cell_color(x, y - 1) == target_color: - flood_fill(x, y - 1, target_color, replacement_color) + flood_fill_main(x, y - 1, target_color, replacement_color) #down if get_pixel_cell_color(x, y + 1) == target_color: - flood_fill(x, y + 1, target_color, replacement_color) + flood_fill_main(x, y + 1, target_color, replacement_color) #left if get_pixel_cell_color(x - 1, y) == target_color: - flood_fill(x - 1, y, target_color, replacement_color) + flood_fill_main(x - 1, y, target_color, replacement_color) #right if get_pixel_cell_color(x + 1, y) == target_color: - flood_fill(x + 1, y, target_color, replacement_color) + flood_fill_main(x + 1, y, target_color, replacement_color) flood_fill_queue -= 1 return diff --git a/addons/graphics_editor/PaintCanvasChunk.gd b/addons/graphics_editor/PaintCanvasChunk.gd index f2ec93b..70bcc4e 100644 --- a/addons/graphics_editor/PaintCanvasChunk.gd +++ b/addons/graphics_editor/PaintCanvasChunk.gd @@ -20,6 +20,9 @@ func set_cell(x, y, color): image.set_pixel(x, y, color) update_chunk() +func get_cell(x, y): + return image.get_pixel(x, y) + func _on_VisibilityNotifier2D_screen_entered(): visible = true diff --git a/addons/graphics_editor/Settings.tscn b/addons/graphics_editor/Settings.tscn index 5102904..c20349f 100644 --- a/addons/graphics_editor/Settings.tscn +++ b/addons/graphics_editor/Settings.tscn @@ -3,7 +3,6 @@ [ext_resource path="res://addons/graphics_editor/Settings.gd" type="Script" id=1] [node name="Settings" type="WindowDialog"] -visible = true margin_top = 20.0 margin_right = 310.0 margin_bottom = 320.0 diff --git a/addons/graphics_editor/Util.gd b/addons/graphics_editor/Util.gd index 63adee1..0d59167 100644 --- a/addons/graphics_editor/Util.gd +++ b/addons/graphics_editor/Util.gd @@ -1,6 +1,21 @@ tool extends Node +class time_debug: + var start_time = 0.0 + var end_time = 0.0 + + func start(): + start_time = OS.get_ticks_msec() + return start_time + + func end(): + end_time = OS.get_ticks_msec() + return end_time + + func get_time_passed(): + return end_time - start_time + static func color_from_array(color_array): var r = color_array[0] var g = color_array[1]