Skip to content

Commit ae57eb2

Browse files
authored
Merge pull request #742 from aaronfranke/2d-to-control
Convert some 2D nodes to Control nodes in GDPaint and Pseudolocalization demos
2 parents 4af3876 + 5f1776b commit ae57eb2

File tree

13 files changed

+237
-180
lines changed

13 files changed

+237
-180
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

2d/gd_paint/paint_control.gd renamed to gui/gd_paint/paint_control.gd

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ extends Control
44
const UNDO_MODE_SHAPE = -2
55
# A constant for whether or not we can undo.
66
const UNDO_NONE = -1
7-
# How large is the image (it's actually the size of DrawingAreaBG, because that's our background canvas).
8-
const IMAGE_SIZE = Vector2(674, 600)
97

108
# Enums for the various modes and brush shapes that can be applied.
119
enum BrushModes {
@@ -20,9 +18,6 @@ enum BrushShapes {
2018
CIRCLE,
2119
}
2220

23-
# The top-left position of the canvas.
24-
var TL_node
25-
2621
# A list to hold all of the dictionaries that make up each brush.
2722
var brush_data_list = []
2823

@@ -48,20 +43,15 @@ var brush_shape = BrushShapes.CIRCLE;
4843
# in the _draw function for more details).
4944
var bg_color = Color.WHITE
5045

51-
func _ready():
52-
# Get the top left position node. We need this to find out whether or not the mouse is inside the canvas.
53-
TL_node = get_node(^"TLPos")
54-
set_process(true)
46+
@onready var drawing_area = $"../DrawingAreaBG"
5547

5648

5749
func _process(_delta):
5850
var mouse_pos = get_viewport().get_mouse_position()
5951

6052
# Check if the mouse is currently inside the canvas/drawing-area.
61-
is_mouse_in_drawing_area = false
62-
if mouse_pos.x > TL_node.global_position.x:
63-
if mouse_pos.y > TL_node.global_position.y:
64-
is_mouse_in_drawing_area = true
53+
var drawing_area_rect := Rect2(drawing_area.position, drawing_area.size)
54+
is_mouse_in_drawing_area = drawing_area_rect.has_point(mouse_pos)
6555

6656
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
6757
# If we do not have a position for when the mouse was first clicked, then this must
@@ -110,11 +100,10 @@ func check_if_mouse_is_inside_canvas():
110100
# Make sure the mouse click starting position is inside the canvas.
111101
# This is so if we start out click outside the canvas (say chosing a color from the color picker)
112102
# and then move our mouse back into the canvas, it won't start painting.
113-
if mouse_click_start_pos.x > TL_node.global_position.x:
114-
if mouse_click_start_pos.y > TL_node.global_position.y:
115-
# Make sure the current mouse position is inside the canvas.
116-
if is_mouse_in_drawing_area:
117-
return true
103+
if Rect2(drawing_area.position, drawing_area.size).has_point(mouse_click_start_pos):
104+
# Make sure the current mouse position is inside the canvas.
105+
if is_mouse_in_drawing_area:
106+
return true
118107
return false
119108

120109

@@ -244,7 +233,7 @@ func save_picture(path):
244233
# Get the viewport image.
245234
var img = get_viewport().get_texture().get_image()
246235
# Crop the image so we only have canvas area.
247-
var cropped_image = img.get_rect(Rect2(TL_node.global_position, IMAGE_SIZE))
236+
var cropped_image = img.get_rect(Rect2(drawing_area.position, drawing_area.size))
248237

249238
# Save the image with the passed in path we got from the save dialog.
250239
cropped_image.save_png(path)

2d/gd_paint/paint_root.tscn renamed to gui/gd_paint/paint_root.tscn

Lines changed: 91 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=6 format=3 uid="uid://bhjmyer4wlwy2"]
1+
[gd_scene load_steps=10 format=3 uid="uid://bhjmyer4wlwy2"]
22

33
[ext_resource type="Script" path="res://paint_control.gd" id="1"]
44
[ext_resource type="Script" path="res://tools_panel.gd" id="2"]
@@ -10,6 +10,22 @@ bg_color = Color(1, 1, 1, 1)
1010
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_npkcn"]
1111
bg_color = Color(0.2, 0.2, 0.2, 1)
1212

13+
[sub_resource type="AtlasTexture" id="AtlasTexture_v0gbg"]
14+
atlas = ExtResource( "3" )
15+
region = Rect2(0, 0, 16, 16)
16+
17+
[sub_resource type="AtlasTexture" id="AtlasTexture_j7akv"]
18+
atlas = ExtResource( "3" )
19+
region = Rect2(16, 0, 16, 16)
20+
21+
[sub_resource type="AtlasTexture" id="AtlasTexture_63q87"]
22+
atlas = ExtResource( "3" )
23+
region = Rect2(0, 16, 16, 16)
24+
25+
[sub_resource type="AtlasTexture" id="AtlasTexture_t52fe"]
26+
atlas = ExtResource( "3" )
27+
region = Rect2(16, 16, 16, 16)
28+
1329
[node name="PaintRoot" type="Control"]
1430
anchor_right = 1.0
1531
anchor_bottom = 1.0
@@ -33,9 +49,6 @@ script = ExtResource( "1" )
3349
metadata/_edit_layout_mode = 1
3450
metadata/_edit_use_custom_anchors = false
3551

36-
[node name="TLPos" type="Position2D" parent="PaintControl"]
37-
position = Vector2(350, 0)
38-
3952
[node name="ToolsPanel" type="Panel" parent="."]
4053
offset_right = 350.0
4154
offset_bottom = 600.0
@@ -61,12 +74,18 @@ offset_bottom = 85.0
6174
metadata/_edit_layout_mode = 0
6275
metadata/_edit_use_custom_anchors = false
6376

64-
[node name="Sprite2D" type="Sprite2D" parent="ToolsPanel/ButtonToolPencil"]
65-
position = Vector2(27, 26)
66-
scale = Vector2(2.5, 2.5)
67-
texture = ExtResource( "3" )
68-
region_enabled = true
69-
region_rect = Rect2(0, 0, 16, 16)
77+
[node name="TextureRect" type="TextureRect" parent="ToolsPanel/ButtonToolPencil"]
78+
anchor_right = 1.0
79+
anchor_bottom = 1.0
80+
offset_left = 5.0
81+
offset_top = 5.0
82+
offset_right = -5.0
83+
offset_bottom = -5.0
84+
grow_horizontal = 2
85+
grow_vertical = 2
86+
texture = SubResource( "AtlasTexture_v0gbg" )
87+
metadata/_edit_layout_mode = 1
88+
metadata/_edit_use_custom_anchors = false
7089

7190
[node name="ButtonToolEraser" type="Button" parent="ToolsPanel"]
7291
offset_left = 110.0
@@ -76,12 +95,18 @@ offset_bottom = 85.0
7695
metadata/_edit_layout_mode = 0
7796
metadata/_edit_use_custom_anchors = false
7897

79-
[node name="Sprite2D" type="Sprite2D" parent="ToolsPanel/ButtonToolEraser"]
80-
position = Vector2(29, 26)
81-
scale = Vector2(2.5, 2.5)
82-
texture = ExtResource( "3" )
83-
region_enabled = true
84-
region_rect = Rect2(16, 0, 16, 16)
98+
[node name="TextureRect2" type="TextureRect" parent="ToolsPanel/ButtonToolEraser"]
99+
anchor_right = 1.0
100+
anchor_bottom = 1.0
101+
offset_left = 5.0
102+
offset_top = 5.0
103+
offset_right = -5.0
104+
offset_bottom = -5.0
105+
grow_horizontal = 2
106+
grow_vertical = 2
107+
texture = SubResource( "AtlasTexture_j7akv" )
108+
metadata/_edit_layout_mode = 1
109+
metadata/_edit_use_custom_anchors = false
85110

86111
[node name="ButtonToolRectangle" type="Button" parent="ToolsPanel"]
87112
offset_left = 180.0
@@ -91,12 +116,18 @@ offset_bottom = 85.0
91116
metadata/_edit_layout_mode = 0
92117
metadata/_edit_use_custom_anchors = false
93118

94-
[node name="Sprite2D" type="Sprite2D" parent="ToolsPanel/ButtonToolRectangle"]
95-
position = Vector2(28, 26)
96-
scale = Vector2(2.5, 2.5)
97-
texture = ExtResource( "3" )
98-
region_enabled = true
99-
region_rect = Rect2(0, 16, 16, 16)
119+
[node name="TextureRect" type="TextureRect" parent="ToolsPanel/ButtonToolRectangle"]
120+
anchor_right = 1.0
121+
anchor_bottom = 1.0
122+
offset_left = 5.0
123+
offset_top = 5.0
124+
offset_right = -5.0
125+
offset_bottom = -5.0
126+
grow_horizontal = 2
127+
grow_vertical = 2
128+
texture = SubResource( "AtlasTexture_63q87" )
129+
metadata/_edit_layout_mode = 1
130+
metadata/_edit_use_custom_anchors = false
100131

101132
[node name="ButtonToolCircle" type="Button" parent="ToolsPanel"]
102133
offset_left = 250.0
@@ -106,12 +137,18 @@ offset_bottom = 85.0
106137
metadata/_edit_layout_mode = 0
107138
metadata/_edit_use_custom_anchors = false
108139

109-
[node name="Sprite2D" type="Sprite2D" parent="ToolsPanel/ButtonToolCircle"]
110-
position = Vector2(28, 26)
111-
scale = Vector2(2.5, 2.5)
112-
texture = ExtResource( "3" )
113-
region_enabled = true
114-
region_rect = Rect2(16, 16, 16, 16)
140+
[node name="TextureRect" type="TextureRect" parent="ToolsPanel/ButtonToolCircle"]
141+
anchor_right = 1.0
142+
anchor_bottom = 1.0
143+
offset_left = 5.0
144+
offset_top = 5.0
145+
offset_right = -5.0
146+
offset_bottom = -5.0
147+
grow_horizontal = 2
148+
grow_vertical = 2
149+
texture = SubResource( "AtlasTexture_t52fe" )
150+
metadata/_edit_layout_mode = 1
151+
metadata/_edit_use_custom_anchors = false
115152

116153
[node name="LabelBrushColor" type="Label" parent="ToolsPanel"]
117154
offset_left = 20.0
@@ -172,12 +209,18 @@ offset_bottom = 114.0
172209
metadata/_edit_layout_mode = 0
173210
metadata/_edit_use_custom_anchors = false
174211

175-
[node name="Sprite2D" type="Sprite2D" parent="ToolsPanel/BrushSettings/ButtonShapeBox"]
176-
position = Vector2(27, 27)
177-
scale = Vector2(2.5, 2.5)
178-
texture = ExtResource( "3" )
179-
region_enabled = true
180-
region_rect = Rect2(0, 16, 16, 16)
212+
[node name="TextureRect" type="TextureRect" parent="ToolsPanel/BrushSettings/ButtonShapeBox"]
213+
anchor_right = 1.0
214+
anchor_bottom = 1.0
215+
offset_left = 5.0
216+
offset_top = 5.0
217+
offset_right = -5.0
218+
offset_bottom = -5.0
219+
grow_horizontal = 2
220+
grow_vertical = 2
221+
texture = SubResource( "AtlasTexture_63q87" )
222+
metadata/_edit_layout_mode = 1
223+
metadata/_edit_use_custom_anchors = false
181224

182225
[node name="ButtonShapeCircle" type="Button" parent="ToolsPanel/BrushSettings"]
183226
offset_left = 190.0
@@ -187,12 +230,18 @@ offset_bottom = 114.0
187230
metadata/_edit_layout_mode = 0
188231
metadata/_edit_use_custom_anchors = false
189232

190-
[node name="Sprite2D" type="Sprite2D" parent="ToolsPanel/BrushSettings/ButtonShapeCircle"]
191-
position = Vector2(28, 27)
192-
scale = Vector2(2.5, 2.5)
193-
texture = ExtResource( "3" )
194-
region_enabled = true
195-
region_rect = Rect2(16, 16, 16, 16)
233+
[node name="TextureRect" type="TextureRect" parent="ToolsPanel/BrushSettings/ButtonShapeCircle"]
234+
anchor_right = 1.0
235+
anchor_bottom = 1.0
236+
offset_left = 5.0
237+
offset_top = 5.0
238+
offset_right = -5.0
239+
offset_bottom = -5.0
240+
grow_horizontal = 2
241+
grow_vertical = 2
242+
texture = SubResource( "AtlasTexture_t52fe" )
243+
metadata/_edit_layout_mode = 1
244+
metadata/_edit_use_custom_anchors = false
196245

197246
[node name="LabelBackgroundColor" type="Label" parent="ToolsPanel"]
198247
offset_left = 20.0
@@ -251,6 +300,8 @@ metadata/_edit_layout_mode = 0
251300
metadata/_edit_use_custom_anchors = false
252301

253302
[node name="SaveFileDialog" type="FileDialog" parent="."]
303+
size = Vector2i(800, 300)
304+
min_size = Vector2i(800, 300)
254305
access = 2
255306
filters = PackedStringArray("*.png")
256307
show_hidden_files = true
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)