浏览代码

Merge pull request #27805 from Kanabenki/line2d-add-point-idx

Add optional position argument for add_point in Line2D
Rémi Verschelde 6 年之前
父节点
当前提交
6e1b8b07b9
共有 3 个文件被更改,包括 11 次插入4 次删除
  1. 3 0
      doc/classes/Line2D.xml
  2. 7 3
      scene/2d/line_2d.cpp
  3. 1 1
      scene/2d/line_2d.h

+ 3 - 0
doc/classes/Line2D.xml

@@ -14,8 +14,11 @@
 			</return>
 			<argument index="0" name="position" type="Vector2">
 			</argument>
+			<argument index="1" name="at_position" type="int" default="-1">
+			</argument>
 			<description>
 				Add a point at the [code]position[/code]. Appends the point at the end of the line.
+				If [code]at_position[/code] is given, the point is inserted before the point number [code]at_position[/code], moving that point (and every point after) after the inserted point. If [code]at_position[/code] is not given, or is an illegal value ([code]at_position &lt; 0[/code] or [code]at_position &gt;= [method get_point_count][/code]), the point will be appended at the end of the point list.
 			</description>
 		</method>
 		<method name="clear_points">

+ 7 - 3
scene/2d/line_2d.cpp

@@ -120,8 +120,12 @@ void Line2D::clear_points() {
 	}
 }
 
-void Line2D::add_point(Vector2 pos) {
-	_points.append(pos);
+void Line2D::add_point(Vector2 pos, int atpos) {
+	if (atpos < 0 || _points.size() < atpos) {
+		_points.append(pos);
+	} else {
+		_points.insert(atpos, pos);
+	}
 	update();
 }
 
@@ -318,7 +322,7 @@ void Line2D::_bind_methods() {
 
 	ClassDB::bind_method(D_METHOD("get_point_count"), &Line2D::get_point_count);
 
-	ClassDB::bind_method(D_METHOD("add_point", "position"), &Line2D::add_point);
+	ClassDB::bind_method(D_METHOD("add_point", "position", "at_position"), &Line2D::add_point, DEFVAL(-1));
 	ClassDB::bind_method(D_METHOD("remove_point", "i"), &Line2D::remove_point);
 
 	ClassDB::bind_method(D_METHOD("clear_points"), &Line2D::clear_points);

+ 1 - 1
scene/2d/line_2d.h

@@ -72,7 +72,7 @@ public:
 
 	void clear_points();
 
-	void add_point(Vector2 pos);
+	void add_point(Vector2 pos, int atpos = -1);
 	void remove_point(int i);
 
 	void set_width(float width);