Skip to content

Commit d692b7b

Browse files
committedNov 14, 2024
Improve set_radial_initial_angle by removing loops
Replace two while loops with fposmodp. Document radial_initial_angle wrapping. Add testcases for set_radial_initial_angle()
1 parent 0f5f3bc commit d692b7b

File tree

4 files changed

+98
-5
lines changed

4 files changed

+98
-5
lines changed
 

‎doc/classes/TextureProgressBar.xml

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
</member>
4343
<member name="radial_initial_angle" type="float" setter="set_radial_initial_angle" getter="get_radial_initial_angle" default="0.0">
4444
Starting angle for the fill of [member texture_progress] if [member fill_mode] is [constant FILL_CLOCKWISE], [constant FILL_COUNTER_CLOCKWISE], or [constant FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE]. When the node's [code]value[/code] is equal to its [code]min_value[/code], the texture doesn't show up at all. When the [code]value[/code] increases, the texture fills and tends towards [member radial_fill_degrees].
45+
[b]Note:[/b] [member radial_initial_angle] is wrapped between [code]0[/code] and [code]360[/code] degrees (inclusive).
4546
</member>
4647
<member name="size_flags_vertical" type="int" setter="set_v_size_flags" getter="get_v_size_flags" overrides="Control" enum="Control.SizeFlags" is_bitfield="true" default="1" />
4748
<member name="step" type="float" setter="set_step" getter="get_step" overrides="Range" default="1.0" />

‎scene/gui/texture_progress_bar.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -608,11 +608,10 @@ int TextureProgressBar::get_fill_mode() {
608608
}
609609

610610
void TextureProgressBar::set_radial_initial_angle(float p_angle) {
611-
while (p_angle > 360) {
612-
p_angle -= 360;
613-
}
614-
while (p_angle < 0) {
615-
p_angle += 360;
611+
ERR_FAIL_COND_MSG(!Math::is_finite(p_angle), "Angle is non-finite.");
612+
613+
if (p_angle < 0.0 || p_angle > 360.0) {
614+
p_angle = Math::fposmodp(p_angle, 360.0f);
616615
}
617616

618617
if (rad_init_angle == p_angle) {
+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**************************************************************************/
2+
/* test_texture_progress_bar.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#ifndef TEST_TEXTURE_PROGRESS_BAR_H
32+
#define TEST_TEXTURE_PROGRESS_BAR_H
33+
34+
#include "scene/gui/texture_progress_bar.h"
35+
36+
#include "tests/test_macros.h"
37+
38+
namespace TestTextureProgressBar {
39+
40+
TEST_CASE("[SceneTree][TextureProgressBar]") {
41+
TextureProgressBar *texture_progress_bar = memnew(TextureProgressBar);
42+
43+
SUBCASE("[TextureProgressBar] set_radial_initial_angle() should wrap angle between 0 and 360 degrees (inclusive).") {
44+
texture_progress_bar->set_radial_initial_angle(0.0);
45+
CHECK(Math::is_equal_approx(texture_progress_bar->get_radial_initial_angle(), (float)0.0));
46+
47+
texture_progress_bar->set_radial_initial_angle(360.0);
48+
CHECK(Math::is_equal_approx(texture_progress_bar->get_radial_initial_angle(), (float)360.0));
49+
50+
texture_progress_bar->set_radial_initial_angle(30.5);
51+
CHECK(Math::is_equal_approx(texture_progress_bar->get_radial_initial_angle(), (float)30.5));
52+
53+
texture_progress_bar->set_radial_initial_angle(-30.5);
54+
CHECK(Math::is_equal_approx(texture_progress_bar->get_radial_initial_angle(), (float)(360 - 30.5)));
55+
56+
texture_progress_bar->set_radial_initial_angle(36000 + 30.5);
57+
CHECK(Math::is_equal_approx(texture_progress_bar->get_radial_initial_angle(), (float)30.5));
58+
59+
texture_progress_bar->set_radial_initial_angle(-(36000 + 30.5));
60+
CHECK(Math::is_equal_approx(texture_progress_bar->get_radial_initial_angle(), (float)(360 - 30.5)));
61+
}
62+
63+
SUBCASE("[TextureProgressBar] set_radial_initial_angle() should not set non-finite values.") {
64+
texture_progress_bar->set_radial_initial_angle(30.5);
65+
66+
ERR_PRINT_OFF;
67+
texture_progress_bar->set_radial_initial_angle(INFINITY);
68+
ERR_PRINT_ON;
69+
CHECK(Math::is_equal_approx(texture_progress_bar->get_radial_initial_angle(), (float)30.5));
70+
71+
ERR_PRINT_OFF;
72+
texture_progress_bar->set_radial_initial_angle(-INFINITY);
73+
ERR_PRINT_ON;
74+
CHECK(Math::is_equal_approx(texture_progress_bar->get_radial_initial_angle(), (float)30.5));
75+
76+
ERR_PRINT_OFF;
77+
texture_progress_bar->set_radial_initial_angle(NAN);
78+
ERR_PRINT_ON;
79+
CHECK(Math::is_equal_approx(texture_progress_bar->get_radial_initial_angle(), (float)30.5));
80+
81+
ERR_PRINT_OFF;
82+
texture_progress_bar->set_radial_initial_angle(-NAN);
83+
ERR_PRINT_ON;
84+
CHECK(Math::is_equal_approx(texture_progress_bar->get_radial_initial_angle(), (float)30.5));
85+
}
86+
87+
memdelete(texture_progress_bar);
88+
}
89+
90+
} // namespace TestTextureProgressBar
91+
92+
#endif // TEST_TEXTURE_PROGRESS_BAR_H

‎tests/test_main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
#include "tests/scene/test_physics_material.h"
131131
#include "tests/scene/test_sprite_frames.h"
132132
#include "tests/scene/test_style_box_texture.h"
133+
#include "tests/scene/test_texture_progress_bar.h"
133134
#include "tests/scene/test_theme.h"
134135
#include "tests/scene/test_timer.h"
135136
#include "tests/scene/test_viewport.h"

0 commit comments

Comments
 (0)