tools_panel.gd 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. extends Panel
  2. @onready var brush_settings: Control = $BrushSettings
  3. @onready var label_brush_size: Label = brush_settings.get_node(^"LabelBrushSize")
  4. @onready var label_brush_shape: Label = brush_settings.get_node(^"LabelBrushShape")
  5. @onready var label_stats: Label = $LabelStats
  6. @onready var label_tools: Label = $LabelTools
  7. @onready var _parent: Control = get_parent()
  8. @onready var save_dialog: FileDialog = _parent.get_node(^"SaveFileDialog")
  9. @onready var paint_control: Control = _parent.get_node(^"PaintControl")
  10. func _ready() -> void:
  11. # Assign all of the needed signals for the option buttons.
  12. $ButtonUndo.pressed.connect(button_pressed.bind("undo_stroke"))
  13. $ButtonSave.pressed.connect(button_pressed.bind("save_picture"))
  14. $ButtonClear.pressed.connect(button_pressed.bind("clear_picture"))
  15. # Assign all of the needed signals for the brush buttons.
  16. $ButtonToolPencil.pressed.connect(button_pressed.bind("mode_pencil"))
  17. $ButtonToolEraser.pressed.connect(button_pressed.bind("mode_eraser"))
  18. $ButtonToolRectangle.pressed.connect(button_pressed.bind("mode_rectangle"))
  19. $ButtonToolCircle.pressed.connect(button_pressed.bind("mode_circle"))
  20. $BrushSettings/ButtonShapeBox.pressed.connect(button_pressed.bind("shape_rectangle"))
  21. $BrushSettings/ButtonShapeCircle.pressed.connect(button_pressed.bind("shape_circle"))
  22. # Assign all of the needed signals for the other brush settings (and ColorPickerBackground).
  23. $ColorPickerBrush.color_changed.connect(brush_color_changed)
  24. $ColorPickerBackground.color_changed.connect(background_color_changed)
  25. $BrushSettings/HScrollBarBrushSize.value_changed.connect(brush_size_changed)
  26. # Assign the "file_selected" signal in SaveFileDialog.
  27. save_dialog.file_selected.connect(save_file_selected)
  28. func _physics_process(_delta: float) -> void:
  29. # Update the status label with the newest brush element count.
  30. label_stats.text = "Brush objects: %d" % paint_control.brush_data_list.size()
  31. func button_pressed(button_name: String) -> void:
  32. # If a brush mode button is pressed.
  33. var tool_name := ""
  34. var shape_name := ""
  35. if button_name == "mode_pencil":
  36. paint_control.brush_mode = paint_control.BrushMode.PENCIL
  37. brush_settings.modulate = Color(1, 1, 1)
  38. tool_name = "Pencil"
  39. elif button_name == "mode_eraser":
  40. paint_control.brush_mode = paint_control.BrushMode.ERASER
  41. brush_settings.modulate = Color(1, 1, 1)
  42. tool_name = "Eraser"
  43. elif button_name == "mode_rectangle":
  44. paint_control.brush_mode = paint_control.BrushMode.RECTANGLE_SHAPE
  45. brush_settings.modulate = Color(1, 1, 1, 0.5)
  46. tool_name = "Rectangle shape"
  47. elif button_name == "mode_circle":
  48. paint_control.brush_mode = paint_control.BrushMode.CIRCLE_SHAPE
  49. brush_settings.modulate = Color(1, 1, 1, 0.5)
  50. tool_name = "Circle shape"
  51. # If a brush shape button is pressed
  52. elif button_name == "shape_rectangle":
  53. paint_control.brush_shape = paint_control.BrushShape.RECTANGLE
  54. shape_name = "Rectangle"
  55. elif button_name == "shape_circle":
  56. paint_control.brush_shape = paint_control.BrushShape.CIRCLE
  57. shape_name = "Circle"
  58. # If a opperation button is pressed
  59. elif button_name == "clear_picture":
  60. paint_control.brush_data_list.clear()
  61. paint_control.queue_redraw()
  62. elif button_name == "save_picture":
  63. save_dialog.popup_centered()
  64. elif button_name == "undo_stroke":
  65. paint_control.undo_stroke()
  66. # Update the labels (in case the brush mode or brush shape has changed).
  67. if not tool_name.is_empty():
  68. label_tools.text = "Selected tool: %s" % tool_name
  69. if not shape_name.is_empty():
  70. label_brush_shape.text = "Brush shape: %s" % shape_name
  71. func brush_color_changed(color: Color) -> void:
  72. # Change the brush color to whatever color the color picker is.
  73. paint_control.brush_color = color
  74. func background_color_changed(color: Color) -> void:
  75. # Change the background color to whatever colorthe background color picker is.
  76. get_parent().get_node(^"DrawingAreaBG").modulate = color
  77. paint_control.bg_color = color
  78. # Because of how the eraser works we also need to redraw the paint control.
  79. paint_control.queue_redraw()
  80. func brush_size_changed(value: float) -> void:
  81. # Change the size of the brush, and update the label to reflect the new value.
  82. paint_control.brush_size = ceilf(value)
  83. label_brush_size.text = "Brush size: " + str(ceil(value)) + "px"
  84. func save_file_selected(path: String) -> void:
  85. # Call save_picture in paint_control, passing in the path we recieved from SaveFileDialog.
  86. paint_control.save_picture(path)