123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- #include "line_2d.h"
- #include "line_builder.h"
- #include "core_string_names.h"
- VARIANT_ENUM_CAST(Line2D::LineJointMode)
- VARIANT_ENUM_CAST(Line2D::LineCapMode)
- VARIANT_ENUM_CAST(Line2D::LineTextureMode)
- Line2D::Line2D()
- : Node2D() {
- _joint_mode = LINE_JOINT_SHARP;
- _begin_cap_mode = LINE_CAP_NONE;
- _end_cap_mode = LINE_CAP_NONE;
- _width = 10;
- _default_color = Color(0.4, 0.5, 1);
- _sharp_limit = 2.f;
- _round_precision = 8;
- }
- void Line2D::set_points(const PoolVector<Vector2> &p_points) {
- _points = p_points;
- update();
- }
- void Line2D::set_width(float width) {
- if (width < 0.0)
- width = 0.0;
- _width = width;
- update();
- }
- float Line2D::get_width() const {
- return _width;
- }
- PoolVector<Vector2> Line2D::get_points() const {
- return _points;
- }
- void Line2D::set_point_position(int i, Vector2 pos) {
- _points.set(i, pos);
- update();
- }
- Vector2 Line2D::get_point_position(int i) const {
- return _points.get(i);
- }
- int Line2D::get_point_count() const {
- return _points.size();
- }
- void Line2D::add_point(Vector2 pos) {
- _points.append(pos);
- update();
- }
- void Line2D::remove_point(int i) {
- _points.remove(i);
- update();
- }
- void Line2D::set_default_color(Color color) {
- _default_color = color;
- update();
- }
- Color Line2D::get_default_color() const {
- return _default_color;
- }
- void Line2D::set_gradient(const Ref<Gradient> &gradient) {
-
- if (_gradient.is_valid()) {
- (**_gradient).disconnect(CoreStringNames::get_singleton()->changed, this, "_gradient_changed");
- }
- _gradient = gradient;
-
- if (_gradient.is_valid()) {
- (**_gradient).connect(CoreStringNames::get_singleton()->changed, this, "_gradient_changed");
- }
- update();
- }
- Ref<Gradient> Line2D::get_gradient() const {
- return _gradient;
- }
- void Line2D::set_texture(const Ref<Texture> &texture) {
- _texture = texture;
- update();
- }
- Ref<Texture> Line2D::get_texture() const {
- return _texture;
- }
- void Line2D::set_texture_mode(const LineTextureMode mode) {
- _texture_mode = mode;
- update();
- }
- Line2D::LineTextureMode Line2D::get_texture_mode() const {
- return _texture_mode;
- }
- void Line2D::set_joint_mode(LineJointMode mode) {
- _joint_mode = mode;
- update();
- }
- Line2D::LineJointMode Line2D::get_joint_mode() const {
- return _joint_mode;
- }
- void Line2D::set_begin_cap_mode(LineCapMode mode) {
- _begin_cap_mode = mode;
- update();
- }
- Line2D::LineCapMode Line2D::get_begin_cap_mode() const {
- return _begin_cap_mode;
- }
- void Line2D::set_end_cap_mode(LineCapMode mode) {
- _end_cap_mode = mode;
- update();
- }
- Line2D::LineCapMode Line2D::get_end_cap_mode() const {
- return _end_cap_mode;
- }
- void Line2D::_notification(int p_what) {
- switch (p_what) {
- case NOTIFICATION_DRAW:
- _draw();
- break;
- }
- }
- void Line2D::set_sharp_limit(float limit) {
- if (limit < 0.f)
- limit = 0.f;
- _sharp_limit = limit;
- update();
- }
- float Line2D::get_sharp_limit() const {
- return _sharp_limit;
- }
- void Line2D::set_round_precision(int precision) {
- if (precision < 1)
- precision = 1;
- _round_precision = precision;
- update();
- }
- int Line2D::get_round_precision() const {
- return _round_precision;
- }
- void Line2D::_draw() {
- if (_points.size() <= 1 || _width == 0.f)
- return;
-
-
- Vector<Vector2> points;
- points.resize(_points.size());
- int len = points.size();
- {
- PoolVector<Vector2>::Read points_read = _points.read();
- for (int i = 0; i < len; ++i) {
- points[i] = points_read[i];
- }
- }
-
- LineBuilder lb;
- lb.points = points;
- lb.default_color = _default_color;
- lb.gradient = *_gradient;
- lb.texture_mode = _texture_mode;
- lb.joint_mode = _joint_mode;
- lb.begin_cap_mode = _begin_cap_mode;
- lb.end_cap_mode = _end_cap_mode;
- lb.round_precision = _round_precision;
- lb.sharp_limit = _sharp_limit;
- lb.width = _width;
- lb.build();
- RID texture_rid;
- if (_texture.is_valid())
- texture_rid = (**_texture).get_rid();
- VS::get_singleton()->canvas_item_add_triangle_array(
- get_canvas_item(),
- lb.indices,
- lb.vertices,
- lb.colors,
- lb.uvs,
- texture_rid);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- void Line2D::_gradient_changed() {
- update();
- }
- void Line2D::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_points", "points"), &Line2D::set_points);
- ClassDB::bind_method(D_METHOD("get_points"), &Line2D::get_points);
- ClassDB::bind_method(D_METHOD("set_point_position", "i", "position"), &Line2D::set_point_position);
- ClassDB::bind_method(D_METHOD("get_point_position", "i"), &Line2D::get_point_position);
- 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("remove_point", "i"), &Line2D::remove_point);
- ClassDB::bind_method(D_METHOD("set_width", "width"), &Line2D::set_width);
- ClassDB::bind_method(D_METHOD("get_width"), &Line2D::get_width);
- ClassDB::bind_method(D_METHOD("set_default_color", "color"), &Line2D::set_default_color);
- ClassDB::bind_method(D_METHOD("get_default_color"), &Line2D::get_default_color);
- ClassDB::bind_method(D_METHOD("set_gradient", "color"), &Line2D::set_gradient);
- ClassDB::bind_method(D_METHOD("get_gradient"), &Line2D::get_gradient);
- ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Line2D::set_texture);
- ClassDB::bind_method(D_METHOD("get_texture"), &Line2D::get_texture);
- ClassDB::bind_method(D_METHOD("set_texture_mode", "mode"), &Line2D::set_texture_mode);
- ClassDB::bind_method(D_METHOD("get_texture_mode"), &Line2D::get_texture_mode);
- ClassDB::bind_method(D_METHOD("set_joint_mode", "mode"), &Line2D::set_joint_mode);
- ClassDB::bind_method(D_METHOD("get_joint_mode"), &Line2D::get_joint_mode);
- ClassDB::bind_method(D_METHOD("set_begin_cap_mode", "mode"), &Line2D::set_begin_cap_mode);
- ClassDB::bind_method(D_METHOD("get_begin_cap_mode"), &Line2D::get_begin_cap_mode);
- ClassDB::bind_method(D_METHOD("set_end_cap_mode", "mode"), &Line2D::set_end_cap_mode);
- ClassDB::bind_method(D_METHOD("get_end_cap_mode"), &Line2D::get_end_cap_mode);
- ClassDB::bind_method(D_METHOD("set_sharp_limit", "limit"), &Line2D::set_sharp_limit);
- ClassDB::bind_method(D_METHOD("get_sharp_limit"), &Line2D::get_sharp_limit);
- ClassDB::bind_method(D_METHOD("set_round_precision", "precision"), &Line2D::set_round_precision);
- ClassDB::bind_method(D_METHOD("get_round_precision"), &Line2D::get_round_precision);
- ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "points"), "set_points", "get_points");
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "width"), "set_width", "get_width");
- ADD_PROPERTY(PropertyInfo(Variant::COLOR, "default_color"), "set_default_color", "get_default_color");
- ADD_GROUP("Fill", "");
- ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_gradient", "get_gradient");
- ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
- ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "texture_mode", PROPERTY_HINT_ENUM, "None,Tile"), "set_texture_mode", "get_texture_mode");
- ADD_GROUP("Capping", "");
- ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "joint_mode", PROPERTY_HINT_ENUM, "Sharp,Bevel,Round"), "set_joint_mode", "get_joint_mode");
- ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "begin_cap_mode", PROPERTY_HINT_ENUM, "None,Box,Round"), "set_begin_cap_mode", "get_begin_cap_mode");
- ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "end_cap_mode", PROPERTY_HINT_ENUM, "None,Box,Round"), "set_end_cap_mode", "get_end_cap_mode");
- ADD_GROUP("Border", "");
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "sharp_limit"), "set_sharp_limit", "get_sharp_limit");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "round_precision"), "set_round_precision", "get_round_precision");
- BIND_ENUM_CONSTANT(LINE_JOINT_SHARP);
- BIND_ENUM_CONSTANT(LINE_JOINT_BEVEL);
- BIND_ENUM_CONSTANT(LINE_JOINT_ROUND);
- BIND_ENUM_CONSTANT(LINE_CAP_NONE);
- BIND_ENUM_CONSTANT(LINE_CAP_BOX);
- BIND_ENUM_CONSTANT(LINE_CAP_ROUND);
- BIND_ENUM_CONSTANT(LINE_TEXTURE_NONE);
- BIND_ENUM_CONSTANT(LINE_TEXTURE_TILE);
- ClassDB::bind_method(D_METHOD("_gradient_changed"), &Line2D::_gradient_changed);
- }
|