paint_control.gd 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. extends Control
  2. # A constant for whether or not we're needing to undo a shape.
  3. const UNDO_MODE_SHAPE = -2
  4. # A constant for whether or not we can undo.
  5. const UNDO_NONE = -1
  6. # Enums for the various modes and brush shapes that can be applied.
  7. enum BrushModes {
  8. PENCIL,
  9. ERASER,
  10. CIRCLE_SHAPE,
  11. RECTANGLE_SHAPE,
  12. }
  13. enum BrushShapes {
  14. RECTANGLE,
  15. CIRCLE,
  16. }
  17. # A list to hold all of the dictionaries that make up each brush.
  18. var brush_data_list = []
  19. # A boolean to hold whether or not the mouse is inside the drawing area, the mouse position last _process call
  20. # and the position of the mouse when the left mouse button was pressed.
  21. var is_mouse_in_drawing_area = false
  22. var last_mouse_pos = Vector2()
  23. var mouse_click_start_pos = null
  24. # A boolean to tell whether we've set undo_elements_list_num, which holds the size of draw_elements_list
  25. # before a new stroke is added (unless the current brush mode is 'rectangle shape' or 'circle shape', in
  26. # which case we do things a litte differently. See the undo_stroke function for more details).
  27. var undo_set = false
  28. var undo_element_list_num = -1
  29. # The current brush settings: The mode, size, color, and shape we have currently selected.
  30. var brush_mode = BrushModes.PENCIL
  31. var brush_size = 32
  32. var brush_color = Color.BLACK
  33. var brush_shape = BrushShapes.CIRCLE;
  34. # The color of the background. We need this for the eraser (see the how we handle the eraser
  35. # in the _draw function for more details).
  36. var bg_color = Color.WHITE
  37. @onready var drawing_area = $"../DrawingAreaBG"
  38. func _process(_delta):
  39. var mouse_pos = get_viewport().get_mouse_position()
  40. # Check if the mouse is currently inside the canvas/drawing-area.
  41. var drawing_area_rect := Rect2(drawing_area.position, drawing_area.size)
  42. is_mouse_in_drawing_area = drawing_area_rect.has_point(mouse_pos)
  43. if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
  44. # If we do not have a position for when the mouse was first clicked, then this must
  45. # be the first time is_mouse_button_pressed has been called since the mouse button was
  46. # released, so we need to store the position.
  47. if mouse_click_start_pos == null:
  48. mouse_click_start_pos = mouse_pos
  49. # If the mouse is inside the canvas and the mouse is 1px away from the position of the mouse last _process call.
  50. if check_if_mouse_is_inside_canvas():
  51. if mouse_pos.distance_to(last_mouse_pos) >= 1:
  52. # If we are in pencil or eraser mode, then we need to draw.
  53. if brush_mode == BrushModes.PENCIL or brush_mode == BrushModes.ERASER:
  54. # If undo has not been set, meaning we've started a new stroke, then store the size of the
  55. # draw_elements_list so we can undo from this point in time.
  56. if undo_set == false:
  57. undo_set = true
  58. undo_element_list_num = brush_data_list.size()
  59. # Add the brush object to draw_elements_array.
  60. add_brush(mouse_pos, brush_mode)
  61. else:
  62. # We've finished our stroke, so we can set a new undo (if a new storke is made).
  63. undo_set = false
  64. # If the mouse is inside the canvas.
  65. if check_if_mouse_is_inside_canvas():
  66. # If we're using either the circle shape mode, or the rectangle shape mode, then
  67. # add the brush object to draw_elements_array.
  68. if brush_mode == BrushModes.CIRCLE_SHAPE or brush_mode == BrushModes.RECTANGLE_SHAPE:
  69. add_brush(mouse_pos, brush_mode)
  70. # We handle undo's differently than either pencil or eraser mode, so we need to set undo
  71. # element_list_num to -2 so we can tell if we need to undo a shape. See undo_stroke for details.
  72. undo_element_list_num = UNDO_MODE_SHAPE
  73. # Since we've released the left mouse, we need to get a new mouse_click_start_pos next time
  74. #is_mouse_button_pressed is true.
  75. mouse_click_start_pos = null
  76. # Store mouse_pos as last_mouse_pos now that we're done with _process.
  77. last_mouse_pos = mouse_pos
  78. func check_if_mouse_is_inside_canvas():
  79. # Make sure we have a mouse click starting position.
  80. if mouse_click_start_pos != null:
  81. # Make sure the mouse click starting position is inside the canvas.
  82. # This is so if we start out click outside the canvas (say chosing a color from the color picker)
  83. # and then move our mouse back into the canvas, it won't start painting.
  84. if Rect2(drawing_area.position, drawing_area.size).has_point(mouse_click_start_pos):
  85. # Make sure the current mouse position is inside the canvas.
  86. if is_mouse_in_drawing_area:
  87. return true
  88. return false
  89. func undo_stroke():
  90. # Only undo a stroke if we have one.
  91. if undo_element_list_num == UNDO_NONE:
  92. return
  93. # If we are undoing a shape, then we can just remove the latest brush.
  94. if undo_element_list_num == UNDO_MODE_SHAPE:
  95. if brush_data_list.size() > 0:
  96. brush_data_list.remove_at(brush_data_list.size() - 1)
  97. # Now that we've undone a shape, we cannot undo again until another stoke is added.
  98. undo_element_list_num = UNDO_NONE
  99. # NOTE: if we only had shape brushes, then we could remove the above line and could let the user
  100. # undo until we have a empty element list.
  101. # Otherwise we're removing a either a pencil stroke or a eraser stroke.
  102. else:
  103. # Figure out how many elements/brushes we've added in the last stroke.
  104. var elements_to_remove = brush_data_list.size() - undo_element_list_num
  105. # Remove all of the elements we've added this in the last stroke.
  106. #warning-ignore:unused_variable
  107. for elment_num in range(0, elements_to_remove):
  108. brush_data_list.pop_back()
  109. # Now that we've undone a stoke, we cannot undo again until another stoke is added.
  110. undo_element_list_num = UNDO_NONE
  111. # Redraw the brushes
  112. queue_redraw()
  113. func add_brush(mouse_pos, type):
  114. # Make new brush dictionary that will hold all of the data we need for the brush.
  115. var new_brush = {}
  116. # Populate the dictionary with values based on the global brush variables.
  117. # We will override these as needed if the brush is a rectange or circle.
  118. new_brush.brush_type = type
  119. new_brush.brush_pos = mouse_pos
  120. new_brush.brush_shape = brush_shape
  121. new_brush.brush_size = brush_size
  122. new_brush.brush_color = brush_color
  123. # If the new bursh is a rectangle shape, we need to calculate the top left corner of the rectangle and the
  124. # bottom right corner of the rectangle.
  125. if type == BrushModes.RECTANGLE_SHAPE:
  126. var TL_pos = Vector2()
  127. var BR_pos = Vector2()
  128. # Figure out the left and right positions of the corners and assign them to the proper variable.
  129. if mouse_pos.x < mouse_click_start_pos.x:
  130. TL_pos.x = mouse_pos.x
  131. BR_pos.x = mouse_click_start_pos.x
  132. else:
  133. TL_pos.x = mouse_click_start_pos.x
  134. BR_pos.x = mouse_pos.x
  135. # Figure out the top and bottom positions of the corners and assign them to the proper variable.
  136. if mouse_pos.y < mouse_click_start_pos.y:
  137. TL_pos.y = mouse_pos.y
  138. BR_pos.y = mouse_click_start_pos.y
  139. else:
  140. TL_pos.y = mouse_click_start_pos.y
  141. BR_pos.y = mouse_pos.y
  142. # Assign the positions to the brush.
  143. new_brush.brush_pos = TL_pos
  144. new_brush.brush_shape_rect_pos_BR = BR_pos
  145. # If the brush isa circle shape, then we need to calculate the radius of the circle.
  146. if type == BrushModes.CIRCLE_SHAPE:
  147. # Get the center point inbetween the mouse position and the position of the mouse when we clicked.
  148. var center_pos = Vector2((mouse_pos.x + mouse_click_start_pos.x) / 2, (mouse_pos.y + mouse_click_start_pos.y) / 2)
  149. # Assign the brush position to the center point, and calculate the radius of the circle using the distance from
  150. # the center to the top/bottom positon of the mouse.
  151. new_brush.brush_pos = center_pos
  152. new_brush.brush_shape_circle_radius = center_pos.distance_to(Vector2(center_pos.x, mouse_pos.y))
  153. # Add the brush and update/draw all of the brushes.
  154. brush_data_list.append(new_brush)
  155. queue_redraw()
  156. func _draw():
  157. # Go through all of the brushes in brush_data_list.
  158. for brush in brush_data_list:
  159. match brush.brush_type:
  160. BrushModes.PENCIL:
  161. # If the brush shape is a rectangle, then we need to make a Rect2 so we can use draw_rect.
  162. # Draw_rect draws a rectagle at the top left corner, using the scale for the size.
  163. # So we offset the position by half of the brush size so the rectangle's center is at mouse position.
  164. if brush.brush_shape == BrushShapes.RECTANGLE:
  165. var rect = Rect2(brush.brush_pos - Vector2(brush.brush_size / 2, brush.brush_size / 2), Vector2(brush.brush_size, brush.brush_size))
  166. draw_rect(rect, brush.brush_color)
  167. # If the brush shape is a circle, then we draw a circle at the mouse position,
  168. # making the radius half of brush size (so the circle is brush size pixels in diameter).
  169. elif brush.brush_shape == BrushShapes.CIRCLE:
  170. draw_circle(brush.brush_pos, brush.brush_size / 2, brush.brush_color)
  171. BrushModes.ERASER:
  172. # NOTE: this is a really cheap way of erasing that isn't really erasing!
  173. # However, this gives similar results in a fairy simple way!
  174. # Erasing works exactly the same was as pencil does for both the rectangle shape and the circle shape,
  175. # but instead of using brush.brush_color, we instead use bg_color instead.
  176. if brush.brush_shape == BrushShapes.RECTANGLE:
  177. var rect = Rect2(brush.brush_pos - Vector2(brush.brush_size / 2, brush.brush_size / 2), Vector2(brush.brush_size, brush.brush_size))
  178. draw_rect(rect, bg_color)
  179. elif brush.brush_shape == BrushShapes.CIRCLE:
  180. draw_circle(brush.brush_pos, brush.brush_size / 2, bg_color)
  181. BrushModes.RECTANGLE_SHAPE:
  182. # We make a Rect2 with the postion at the top left. To get the size we take the bottom right position
  183. # and subtract the top left corner's position.
  184. var rect = Rect2(brush.brush_pos, brush.brush_shape_rect_pos_BR - brush.brush_pos)
  185. draw_rect(rect, brush.brush_color)
  186. BrushModes.CIRCLE_SHAPE:
  187. # We simply draw a circle using stored in brush.
  188. draw_circle(brush.brush_pos, brush.brush_shape_circle_radius, brush.brush_color)
  189. func save_picture(path):
  190. # Wait until the frame has finished before getting the texture.
  191. await RenderingServer.frame_post_draw
  192. # Get the viewport image.
  193. var img = get_viewport().get_texture().get_image()
  194. # Crop the image so we only have canvas area.
  195. var cropped_image = img.get_region(Rect2(drawing_area.position, drawing_area.size))
  196. # Save the image with the passed in path we got from the save dialog.
  197. cropped_image.save_png(path)