summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/classes/Control.xml4
-rw-r--r--scene/gui/code_edit.cpp5
-rw-r--r--scene/gui/control.cpp20
-rw-r--r--scene/gui/control.h4
-rw-r--r--scene/gui/progress_bar.cpp7
-rw-r--r--scene/gui/rich_text_label.cpp5
-rw-r--r--scene/gui/spin_box.cpp5
-rw-r--r--scene/resources/bit_map.cpp162
-rw-r--r--scene/resources/bit_map.h2
-rw-r--r--tests/scene/test_bit_map.h31
10 files changed, 200 insertions, 45 deletions
diff --git a/doc/classes/Control.xml b/doc/classes/Control.xml
index 2159001a30..8a38deeebe 100644
--- a/doc/classes/Control.xml
+++ b/doc/classes/Control.xml
@@ -1010,6 +1010,10 @@
<member name="layout_direction" type="int" setter="set_layout_direction" getter="get_layout_direction" enum="Control.LayoutDirection" default="0">
Controls layout direction and text writing direction. Right-to-left layouts are necessary for certain languages (e.g. Arabic and Hebrew).
</member>
+ <member name="localize_numeral_system" type="bool" setter="set_localize_numeral_system" getter="is_localizing_numeral_system" default="true">
+ If [code]true[/code], automatically converts code line numbers, list indices, [SpinBox] and [ProgressBar] values from the Western Arabic (0..9) to the numeral systems used in current locale.
+ [b]Note:[/b] Numbers within the text are not automatically converted, it can be done manually, using [method TextServer.format_number].
+ </member>
<member name="mouse_default_cursor_shape" type="int" setter="set_default_cursor_shape" getter="get_default_cursor_shape" enum="Control.CursorShape" default="0">
The default cursor shape for this control. Useful for Godot plugins and applications or games that use the system's mouse cursors.
[b]Note:[/b] On Linux, shapes may vary depending on the cursor theme of the system.
diff --git a/scene/gui/code_edit.cpp b/scene/gui/code_edit.cpp
index ea310f5a12..9e0dc049e5 100644
--- a/scene/gui/code_edit.cpp
+++ b/scene/gui/code_edit.cpp
@@ -1425,7 +1425,10 @@ bool CodeEdit::is_line_numbers_zero_padded() const {
}
void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
- String fc = TS->format_number(String::num(p_line + 1).lpad(line_number_digits, line_number_padding));
+ String fc = String::num(p_line + 1).lpad(line_number_digits, line_number_padding);
+ if (is_localizing_numeral_system()) {
+ fc = TS->format_number(fc);
+ }
Ref<TextLine> tl;
tl.instantiate();
tl->add_string(fc, font, font_size);
diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp
index c5cb7157e8..4e76f72921 100644
--- a/scene/gui/control.cpp
+++ b/scene/gui/control.cpp
@@ -2773,6 +2773,20 @@ bool Control::is_layout_rtl() const {
return data.is_rtl;
}
+void Control::set_localize_numeral_system(bool p_enable) {
+ if (p_enable == data.localize_numeral_system) {
+ return;
+ }
+
+ data.localize_numeral_system = p_enable;
+
+ notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
+}
+
+bool Control::is_localizing_numeral_system() const {
+ return data.localize_numeral_system;
+}
+
void Control::set_auto_translate(bool p_enable) {
if (p_enable == data.auto_translate) {
return;
@@ -3154,6 +3168,9 @@ void Control::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_auto_translate", "enable"), &Control::set_auto_translate);
ClassDB::bind_method(D_METHOD("is_auto_translating"), &Control::is_auto_translating);
+ ClassDB::bind_method(D_METHOD("set_localize_numeral_system", "enable"), &Control::set_localize_numeral_system);
+ ClassDB::bind_method(D_METHOD("is_localizing_numeral_system"), &Control::is_localizing_numeral_system);
+
ADD_GROUP("Layout", "");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_contents"), "set_clip_contents", "is_clipping_contents");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "custom_minimum_size", PROPERTY_HINT_NONE, "suffix:px"), "set_custom_minimum_size", "get_custom_minimum_size");
@@ -3198,8 +3215,9 @@ void Control::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::INT, "size_flags_vertical", PROPERTY_HINT_FLAGS, "Fill:1,Expand:2,Shrink Center:4,Shrink End:8"), "set_v_size_flags", "get_v_size_flags");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size_flags_stretch_ratio", PROPERTY_HINT_RANGE, "0,20,0.01,or_greater"), "set_stretch_ratio", "get_stretch_ratio");
- ADD_GROUP("Auto Translate", "");
+ ADD_GROUP("Localization", "");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_translate"), "set_auto_translate", "is_auto_translating");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "localize_numeral_system"), "set_localize_numeral_system", "is_localizing_numeral_system");
ADD_GROUP("Tooltip", "tooltip_");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "tooltip_text", PROPERTY_HINT_MULTILINE_TEXT), "set_tooltip_text", "get_tooltip_text");
diff --git a/scene/gui/control.h b/scene/gui/control.h
index 72e870930d..3e9bb48a4a 100644
--- a/scene/gui/control.h
+++ b/scene/gui/control.h
@@ -249,6 +249,7 @@ private:
bool is_rtl = false;
bool auto_translate = true;
+ bool localize_numeral_system = true;
// Extra properties.
@@ -595,6 +596,9 @@ public:
LayoutDirection get_layout_direction() const;
virtual bool is_layout_rtl() const;
+ void set_localize_numeral_system(bool p_enable);
+ bool is_localizing_numeral_system() const;
+
void set_auto_translate(bool p_enable);
bool is_auto_translating() const;
_FORCE_INLINE_ String atr(const String p_string) const {
diff --git a/scene/gui/progress_bar.cpp b/scene/gui/progress_bar.cpp
index 8369eaa227..50bcfa6a0c 100644
--- a/scene/gui/progress_bar.cpp
+++ b/scene/gui/progress_bar.cpp
@@ -103,7 +103,12 @@ void ProgressBar::_notification(int p_what) {
}
if (show_percentage) {
- String txt = TS->format_number(itos(int(get_as_ratio() * 100))) + TS->percent_sign();
+ String txt = itos(int(get_as_ratio() * 100));
+ if (is_localizing_numeral_system()) {
+ txt = TS->format_number(txt) + TS->percent_sign();
+ } else {
+ txt += String("%");
+ }
TextLine tl = TextLine(txt, theme_cache.font, theme_cache.font_size);
Vector2 text_pos = (Point2(get_size().width - tl.get_size().x, get_size().height - tl.get_size().y) / 2).round();
diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp
index 87cc26187a..93ae9db8ba 100644
--- a/scene/gui/rich_text_label.cpp
+++ b/scene/gui/rich_text_label.cpp
@@ -752,7 +752,10 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
prefix = _prefix;
break;
} else if (list_items[i]->list_type == LIST_NUMBERS) {
- segment = TS->format_number(itos(list_index[i]), _find_language(l.from));
+ segment = itos(list_index[i]);
+ if (is_localizing_numeral_system()) {
+ segment = TS->format_number(segment, _find_language(l.from));
+ }
} else if (list_items[i]->list_type == LIST_LETTERS) {
segment = _letters(list_index[i], list_items[i]->capitalize);
} else if (list_items[i]->list_type == LIST_ROMAN) {
diff --git a/scene/gui/spin_box.cpp b/scene/gui/spin_box.cpp
index c4000120c8..e15b3b7bd4 100644
--- a/scene/gui/spin_box.cpp
+++ b/scene/gui/spin_box.cpp
@@ -40,7 +40,10 @@ Size2 SpinBox::get_minimum_size() const {
}
void SpinBox::_value_changed(double p_value) {
- String value = TS->format_number(String::num(get_value(), Math::range_step_decimals(get_step())));
+ String value = String::num(get_value(), Math::range_step_decimals(get_step()));
+ if (is_localizing_numeral_system()) {
+ value = TS->format_number(value);
+ }
if (!line_edit->has_focus()) {
if (!prefix.is_empty()) {
diff --git a/scene/resources/bit_map.cpp b/scene/resources/bit_map.cpp
index 1b06e09bb8..4afc82576d 100644
--- a/scene/resources/bit_map.cpp
+++ b/scene/resources/bit_map.cpp
@@ -169,7 +169,15 @@ Dictionary BitMap::_get_data() const {
return d;
}
-Vector<Vector2> BitMap::_march_square(const Rect2i &p_rect, const Point2i &p_start) const {
+struct CrossStackEntry {
+ Point2i cross;
+ Vector<int> ranges;
+
+ _FORCE_INLINE_ bool operator==(const CrossStackEntry &p_other) const { return cross == p_other.cross; }
+ _FORCE_INLINE_ bool operator!=(const CrossStackEntry &p_other) const { return cross != p_other.cross; }
+};
+
+Vector<Vector<Vector2>> BitMap::_march_square(const Rect2i &p_rect, const Point2i &p_start) const {
int stepx = 0;
int stepy = 0;
int prevx = 0;
@@ -179,9 +187,20 @@ Vector<Vector2> BitMap::_march_square(const Rect2i &p_rect, const Point2i &p_sta
int curx = startx;
int cury = starty;
unsigned int count = 0;
- HashSet<Point2i> case9s;
- HashSet<Point2i> case6s;
- Vector<Vector2> _points;
+
+ Vector<CrossStackEntry> cross_stack;
+ int cross_stack_size = 0;
+
+ // Add starting point to stack as the default entry.
+ cross_stack.push_back({ Point2i(-1, -1), Vector<int>({ 0 }) });
+ cross_stack_size++;
+
+ Vector<Point2i> _points;
+ Vector<Vector<Vector2>> ret;
+
+ // Add starting entry at start of return.
+ ret.resize(1);
+
do {
int sv = 0;
{ // Square value
@@ -202,7 +221,7 @@ Vector<Vector2> BitMap::_march_square(const Rect2i &p_rect, const Point2i &p_sta
sv += (p_rect.has_point(bl) && get_bitv(bl)) ? 4 : 0;
Point2i br = Point2i(curx, cury);
sv += (p_rect.has_point(br) && get_bitv(br)) ? 8 : 0;
- ERR_FAIL_COND_V(sv == 0 || sv == 15, Vector<Vector2>());
+ ERR_FAIL_COND_V(sv == 0 || sv == 15, Vector<Vector<Vector2>>());
}
switch (sv) {
@@ -266,70 +285,139 @@ Vector<Vector2> BitMap::_march_square(const Rect2i &p_rect, const Point2i &p_sta
stepy = 0;
break;
case 9:
- /*
+ /* Going DOWN if coming from the LEFT, otherwise go UP.
+ 9
+---+---+
| 1 | |
+---+---+
| | 8 |
+---+---+
- this should normally go UP, but if we already been here, we go down
*/
- if (case9s.has(Point2i(curx, cury))) {
- //found, so we go down, and delete from case9s;
+
+ if (prevx == 1) {
stepx = 0;
stepy = 1;
- case9s.erase(Point2i(curx, cury));
} else {
- //not found, we go up, and add to case9s;
stepx = 0;
stepy = -1;
- case9s.insert(Point2i(curx, cury));
}
break;
case 6:
- /*
+ /* Going RIGHT if coming from BELOW, otherwise go LEFT.
6
+---+---+
| | 2 |
+---+---+
| 4 | |
+---+---+
- this normally go RIGHT, but if it's coming from RIGHT, it should go LEFT
*/
- if (case6s.has(Point2i(curx, cury))) {
- //found, so we go left, and delete from case6s;
- stepx = -1;
+
+ if (prevy == -1) {
+ stepx = 1;
stepy = 0;
- case6s.erase(Point2i(curx, cury));
} else {
- //not found, we go right, and add to case6s;
- stepx = 1;
+ stepx = -1;
stepy = 0;
- case6s.insert(Point2i(curx, cury));
}
break;
default:
ERR_PRINT("this shouldn't happen.");
}
+
+ // Handle crossing points.
+ if (sv == 6 || sv == 9) {
+ const int new_index = _points.size() - 1;
+
+ // Add previous point to last stack entry.
+ cross_stack.write[cross_stack_size - 1].ranges.push_back(new_index);
+
+ // Create temporary entry to maybe insert, for searching.
+ const CrossStackEntry new_entry = { _points[new_index], Vector<int>({ new_index }) };
+
+ // Attempt to find matching entry.
+ const int found = cross_stack.rfind(new_entry, cross_stack_size - 1);
+
+ if (found != -1) {
+ Vector<Vector2> tmp;
+
+ // Iterate over entries between end of stack and found, adding ranges to result.
+ for (int i = found; i < cross_stack_size; i++) {
+ const Vector<int> &ranges = cross_stack[i].ranges;
+
+ for (int j = 0; j < ranges.size() / 2; j++) {
+ int first = ranges[2 * j];
+ const int last = ranges[2 * j + 1];
+
+ int new_pos = tmp.size();
+
+ tmp.resize(tmp.size() + (last - first));
+
+ Vector2 *tmp_ptrw = tmp.ptrw();
+
+ for (; first < last; first++, new_pos++) {
+ tmp_ptrw[new_pos].x = (float)(_points[first].x - p_rect.position.x);
+ tmp_ptrw[new_pos].y = (float)(_points[first].y - p_rect.position.y);
+ }
+ }
+ }
+
+ ret.push_back(tmp);
+
+ // Shrink stack.
+ cross_stack_size = found;
+
+ // Add previous point to last stack entry.
+ cross_stack.write[cross_stack_size - 1].ranges.push_back(new_index);
+ } else {
+ cross_stack.resize(MAX(cross_stack_size + 1, cross_stack.size()));
+ cross_stack.set(cross_stack_size, new_entry);
+ cross_stack_size++;
+ }
+ }
+
// Small optimization:
// If the previous direction is same as the current direction,
// then we should modify the last vector to current.
curx += stepx;
cury += stepy;
if (stepx == prevx && stepy == prevy) {
- _points.write[_points.size() - 1].x = (float)(curx - p_rect.position.x);
- _points.write[_points.size() - 1].y = (float)(cury + p_rect.position.y);
+ _points.write[_points.size() - 1].x = curx;
+ _points.write[_points.size() - 1].y = cury;
} else {
- _points.push_back(Vector2((float)(curx - p_rect.position.x), (float)(cury + p_rect.position.y)));
+ _points.push_back(Point2i(curx, cury));
}
count++;
prevx = stepx;
prevy = stepy;
- ERR_FAIL_COND_V((int)count > width * height, _points);
+ ERR_FAIL_COND_V((int)count > width * height, Vector<Vector<Vector2>>());
} while (curx != startx || cury != starty);
- return _points;
+
+ // Add last position to last stack entry.
+ cross_stack.write[cross_stack_size - 1].ranges.push_back(_points.size());
+
+ for (int i = 0; i < cross_stack_size; i++) {
+ const Vector<int> &ranges = cross_stack[i].ranges;
+
+ for (int j = 0; j < ranges.size() / 2; j++) {
+ int first = ranges[2 * j];
+ const int last = ranges[2 * j + 1];
+
+ int new_pos = ret[0].size();
+
+ ret.write[0].resize(ret[0].size() + (last - first));
+
+ Vector2 *tmp_ptrw = ret.write[0].ptrw();
+
+ for (; first < last; first++, new_pos++) {
+ tmp_ptrw[new_pos].x = (float)(_points[first].x - p_rect.position.x);
+ tmp_ptrw[new_pos].y = (float)(_points[first].y - p_rect.position.y);
+ }
+ }
+ }
+
+ return ret;
}
static float perpendicular_distance(const Vector2 &i, const Vector2 &start, const Vector2 &end) {
@@ -442,7 +530,7 @@ static void fill_bits(const BitMap *p_src, Ref<BitMap> &p_map, const Point2i &p_
for (int j = next_j; j <= pos.y + 1; j++) {
if (popped) {
// The next loop over j must start normally.
- next_j = pos.y;
+ next_j = pos.y - 1;
popped = false;
// Skip because an iteration was already executed with current counter values.
continue;
@@ -486,13 +574,10 @@ static void fill_bits(const BitMap *p_src, Ref<BitMap> &p_map, const Point2i &p_
}
}
} while (reenter || popped);
-
- print_verbose("BitMap: Max stack size: " + itos(stack.size()));
}
Vector<Vector<Vector2>> BitMap::clip_opaque_to_polygons(const Rect2i &p_rect, float p_epsilon) const {
Rect2i r = Rect2i(0, 0, width, height).intersection(p_rect);
- print_verbose("BitMap: Rect: " + r);
Point2i from;
Ref<BitMap> fill;
@@ -505,17 +590,16 @@ Vector<Vector<Vector2>> BitMap::clip_opaque_to_polygons(const Rect2i &p_rect, fl
if (!fill->get_bit(j, i) && get_bit(j, i)) {
fill_bits(this, fill, Point2i(j, i), r);
- Vector<Vector2> polygon = _march_square(r, Point2i(j, i));
- print_verbose("BitMap: Pre reduce: " + itos(polygon.size()));
- polygon = reduce(polygon, r, p_epsilon);
- print_verbose("BitMap: Post reduce: " + itos(polygon.size()));
+ for (Vector<Vector2> polygon : _march_square(r, Point2i(j, i))) {
+ polygon = reduce(polygon, r, p_epsilon);
- if (polygon.size() < 3) {
- print_verbose("Invalid polygon, skipped");
- continue;
- }
+ if (polygon.size() < 3) {
+ print_verbose("Invalid polygon, skipped");
+ continue;
+ }
- polygons.push_back(polygon);
+ polygons.push_back(polygon);
+ }
}
}
}
diff --git a/scene/resources/bit_map.h b/scene/resources/bit_map.h
index 291ed8c4d0..0ec5772fd1 100644
--- a/scene/resources/bit_map.h
+++ b/scene/resources/bit_map.h
@@ -46,7 +46,7 @@ class BitMap : public Resource {
int width = 0;
int height = 0;
- Vector<Vector2> _march_square(const Rect2i &p_rect, const Point2i &p_start) const;
+ Vector<Vector<Vector2>> _march_square(const Rect2i &p_rect, const Point2i &p_start) const;
TypedArray<PackedVector2Array> _opaque_to_polygons_bind(const Rect2i &p_rect, float p_epsilon) const;
diff --git a/tests/scene/test_bit_map.h b/tests/scene/test_bit_map.h
index aca7e5fe22..dc47bd7863 100644
--- a/tests/scene/test_bit_map.h
+++ b/tests/scene/test_bit_map.h
@@ -434,6 +434,37 @@ TEST_CASE("[BitMap] Clip to polygon") {
polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 128, 128));
CHECK_MESSAGE(polygons.size() == 1, "We should have exactly 1 polygon");
CHECK_MESSAGE(polygons[0].size() == 6, "The polygon should have exactly 6 points");
+
+ reset_bit_map(bit_map);
+ bit_map.set_bit_rect(Rect2i(0, 0, 64, 64), true);
+ bit_map.set_bit_rect(Rect2i(64, 64, 64, 64), true);
+ bit_map.set_bit_rect(Rect2i(192, 128, 64, 64), true);
+ bit_map.set_bit_rect(Rect2i(128, 192, 64, 64), true);
+ polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 256, 256));
+ CHECK_MESSAGE(polygons.size() == 4, "We should have exactly 4 polygons");
+ CHECK_MESSAGE(polygons[0].size() == 4, "The polygon should have exactly 4 points");
+ CHECK_MESSAGE(polygons[1].size() == 4, "The polygon should have exactly 4 points");
+ CHECK_MESSAGE(polygons[2].size() == 4, "The polygon should have exactly 4 points");
+ CHECK_MESSAGE(polygons[3].size() == 4, "The polygon should have exactly 4 points");
+
+ reset_bit_map(bit_map);
+ bit_map.set_bit(0, 0, true);
+ bit_map.set_bit(2, 0, true);
+ bit_map.set_bit_rect(Rect2i(1, 1, 1, 2), true);
+ polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 3, 3));
+ CHECK_MESSAGE(polygons.size() == 3, "We should have exactly 3 polygons");
+ CHECK_MESSAGE(polygons[0].size() == 4, "The polygon should have exactly 4 points");
+ CHECK_MESSAGE(polygons[1].size() == 4, "The polygon should have exactly 4 points");
+ CHECK_MESSAGE(polygons[2].size() == 4, "The polygon should have exactly 4 points");
+
+ reset_bit_map(bit_map);
+ bit_map.set_bit_rect(Rect2i(0, 0, 2, 1), true);
+ bit_map.set_bit_rect(Rect2i(0, 2, 3, 1), true);
+ bit_map.set_bit(0, 1, true);
+ bit_map.set_bit(2, 1, true);
+ polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 4, 4));
+ CHECK_MESSAGE(polygons.size() == 1, "We should have exactly 1 polygon");
+ CHECK_MESSAGE(polygons[0].size() == 6, "The polygon should have exactly 6 points");
}
} // namespace TestBitmap