Skip to content

Commit

Permalink
form: Fixed Slider Bugs, various little improvements (#100)
Browse files Browse the repository at this point in the history
* Changed F2 key behavior in input boxes, it now uses key_repeating instead of key_pressed.

* Fixed Slider Bugs, particularly:
1. get_slider returning 0 when step size was a number with persision.
2. Replaced all slider floats with doubles because of data corruption.

* Added get_custom_type and has_custom_type
1. `bool audio_form::has_custom_type();`
2. `string audio_form::get_custom_type();`

* Added 2 more functions for link control
1. `string audio_form::get_link_url();`
2. "bool audio_form::set_url(int control_index, string new_url);"

* Added 2 more slider functions
1. `bool audio_form::set_slider_step_size(int control_index, double new_size);`
2. `double audio_form::get_step_size(control_index);`

* Slider text value functions now accept doubles
1. `string audio_form::get_slider_text_value(int control_index, double value);`
2. `	bool audio_form::set_slider_text_value(int control_index, double value, const string& in text);`.

* Improved docs
1. There was a markdown bug in create input box that put 2 arguments in 1 list item. Removed ")" sign to make it another list item.
2. Modified the create slider docs according to the new signature.
  • Loading branch information
literary-programmer authored Sep 28, 2024
1 parent 7e743bc commit ae37036
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Creates an input box control on the audio form.
## Arguments:
* string caption: the label of the input box (e.g. what will be read when you tab over it?).
* string default_text = "": the text to populate the input box with by default (if any).
*( string password_mask = "": a string to mask typed characters with, (e.g. "star"). Mainly useful if you want your field to be password protected. Leave blank for no password protection.
* string password_mask = "": a string to mask typed characters with, (e.g. "star"). Mainly useful if you want your field to be password protected. Leave blank for no password protection.
* int maximum_length = 0: the maximum number of characters that can be typed in this field, 0 for unlimited.
* bool read_only = false: should this text field be read-only?
* bool multiline = false: should this text field have multiple lines?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# create_slider
Creates a new slider control and adds it to the audio form.

`int audio_form::create_slider(string caption, int default_value = 50, int minimum_value = 0, int maximum_value = 100, string text = "");`
`int audio_form::create_slider(string caption, double default_value = 50, double minimum_value = 0, double maximum_value = 100, string text = "", double step_size = 1);`

## Arguments:
* string caption: the text to be spoken when this slider is tabbed over.
* int default_value = 50: the default value to set the slider to.
* int minimum_value = 0: the minimum value of the slider.
* int maximum_value = 100: the maximum value of the slider.
* double default_value = 50: the default value to set the slider to.
* double minimum_value = 0: the minimum value of the slider.
* double maximum_value = 100: the maximum value of the slider.
* string text = "": extra text to be associated with the slider.
* double step_size = 1: the value that will increment/decrement the slider value.

## Returns:
int: the control index of the new slider, or -1 if there was an error. To get error information, see `audio_form::get_last_error();`.
122 changes: 108 additions & 14 deletions release/include/form.nvgt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,30 @@ class audio_form {
c_form[control_index].custom_type = custom_type;
return true;
}
string get_custom_type(int control_index) {
form_error = 0;
if (!active) {
form_error = form_error_no_window;
return "";
}
if ((control_index < 0) || (control_index > c_form.length() - 1)) {
form_error = form_error_invalid_index;
return "";
}
return c_form[control_index].custom_type;
}
bool has_custom_type(int control_index) {
form_error = 0;
if (!active) {
form_error = form_error_no_window;
return false;
}
if ((control_index < 0) || (control_index > c_form.length() - 1)) {
form_error = form_error_invalid_index;
return false;
}
return !c_form[control_index].custom_type.empty();
}
bool set_subform(int control_index, audio_form@ f) {
form_error = 0;
if (!active) {
Expand Down Expand Up @@ -354,7 +378,7 @@ class audio_form {
@c_form[control_counter].ui_speech = ui_speech;
return control_counter;
}
int create_slider(string caption, float default_value = 50, float minimum_value = 0, float maximum_value = 100, string text = "", float step_size = 1) {
int create_slider(string caption, double default_value = 50, double minimum_value = 0, double maximum_value = 100, string text = "", double step_size = 1) {
form_error = 0;
if (active_controls >= 50) {
form_error = form_error_window_full;
Expand Down Expand Up @@ -1815,6 +1839,39 @@ class audio_form {
int get_last_error() {
return form_error;
}
bool set_link_url(int control_index, string new_url) {
form_error = 0;
if (!active) {
form_error = form_error_no_window;
return false;
}
if ((control_index < 0) || (control_index > c_form.length() - 1)) {
form_error = form_error_invalid_index;
return false;
}
if (c_form[control_index].type != ct_link) {
form_error = form_error_invalid_control;
return false;
}
c_form[control_index].link_url = new_url;
return true;
}
string get_link_url(int control_index) {
form_error = 0;
if (!active) {
form_error = form_error_no_window;
return "";
}
if ((control_index < 0) || (control_index > c_form.length() - 1)) {
form_error = form_error_invalid_index;
return "";
}
if (c_form[control_index].type != ct_link) {
form_error = form_error_invalid_control;
return "";
}
return c_form[control_index].link_url;
}
bool set_progress(int control_index, int value) {
form_error = 0;
if (!active) {
Expand Down Expand Up @@ -1848,7 +1905,7 @@ class audio_form {
}
return true;
}
bool set_slider(int control_index, int value, int min = -1, int max = -1) {
bool set_slider(int control_index, double value, double min = -1, double max = -1) {
form_error = 0;
if (!active) {
form_error = form_error_no_window;
Expand Down Expand Up @@ -1881,7 +1938,7 @@ class audio_form {
c_form[control_index].slider_value = value;
return true;
}
int get_slider(int control_index) {
double get_slider(int control_index) {
form_error = 0;
if (!active) {
form_error = form_error_no_window;
Expand All @@ -1897,7 +1954,7 @@ class audio_form {
}
return c_form[control_index].slider_value;
}
string get_slider_text_value(int control_index, int value) {
string get_slider_text_value(int control_index, double value) {
form_error = 0;
if (!active) {
form_error = form_error_no_window;
Expand All @@ -1915,7 +1972,44 @@ class audio_form {
if (c_form[control_index].slider_text_values.get(value, text)) return "";
return text;
}
bool set_slider_text_value(int control_index, int value, const string& in text) {
bool set_slider_step_size(int control_index, double new_size) {
form_error = 0;
if (!active) {
form_error = form_error_no_window;
return false;
}
if ((control_index < 0) || (control_index > c_form.length() - 1)) {
form_error = form_error_invalid_index;
return false;
}
if (c_form[control_index].type != ct_slider) {
form_error = form_error_invalid_control;
return false;
}
if (new_size <= 0) {
form_error = form_error_invalid_value;
return false;
}
c_form[control_index].slider_step_size = new_size;
return true;
}
double get_slider_step_size(int control_index) {
form_error = 0;
if (!active) {
form_error = form_error_no_window;
return -1;
}
if ((control_index < 0) || (control_index > c_form.length() - 1)) {
form_error = form_error_invalid_index;
return -1;
}
if (c_form[control_index].type != ct_slider) {
form_error = form_error_invalid_control;
return -1;
}
return c_form[control_index].slider_step_size;
}
bool set_slider_text_value(int control_index, double value, const string& in text) {
form_error = 0;
if (!active) {
form_error = form_error_no_window;
Expand Down Expand Up @@ -2106,7 +2200,7 @@ class audio_form {
}
}
if (c_form[tab_index].type == ct_input) {
if (key_pressed(KEY_F2)) {
if (key_repeating(KEY_F2)) {
int dir = 1;
if (keyboard_modifiers & KEYMOD_SHIFT != 0) dir = -1;
audioform_keyboard_echo = audioform_change_keyboard_echo(audioform_keyboard_echo, dir);
Expand Down Expand Up @@ -2319,10 +2413,10 @@ class control {
bool list_nav_translate;
int type;
int progress;
float slider_value;
float slider_minimum_value;
float slider_maximum_value;
float slider_step_size;
double slider_value;
double slider_minimum_value;
double slider_maximum_value;
double slider_step_size;
dictionary slider_text_values;
int echo_flag;
int speak_interval;
Expand Down Expand Up @@ -2626,7 +2720,7 @@ class control {
}
}
if (type == ct_slider) {
float increment = 0;
double increment = 0;
// we don't want to add to the slider_value if it touches the edges.
bool slider_edge = false;
if (key_repeating(KEY_LEFT) || key_repeating(KEY_DOWN))
Expand All @@ -2648,12 +2742,12 @@ class control {
int width = floor((slider_maximum_value - slider_minimum_value) / slider_step_size);
if (keyboard_modifiers & KEYMOD_CTRL == 0 && width > 100 && !slider_edge) {
increment *= (width * 0.025);
// Because we now support floating points, increment multiplication can return numbers with decimal points.
// Because we now support doubles, increment multiplication can return numbers with decimal points.
// It can set unexpected values if step size is without decimal points.
if (slider_step_size % 1 == 0)
increment -= increment % 1;
}
float new_value;
double new_value;
if (slider_edge)
new_value = increment;
else
Expand All @@ -2663,7 +2757,7 @@ class control {
if (new_value > slider_maximum_value)
new_value = slider_maximum_value;
if (new_value != slider_value) {
float old = slider_value;
double old = slider_value;
slider_value = new_value;
if (@event_callback != null and event_callback(f, tab_index, event_slider, {{"increment", increment}, {"value", slider_value}}) == 1) {
slider_value = old;
Expand Down
2 changes: 1 addition & 1 deletion release/include/virtual_dialogs.nvgt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* virtual_dialogs.nvgt - high level virtual alert box, question, input box, and other dialogs using audio form
*
* Include copyright (c) 2024 literary-programmer, under the same license as:
* Include copyright (c) 2024 Hamza Ahmad (literary-programmer) <https://github.com/literary-programmer>, under the same license as:
* NVGT - NonVisual Gaming Toolkit
* Copyright (c) 2022-2024 Sam Tupy
* https://nvgt.gg
Expand Down

0 comments on commit ae37036

Please sign in to comment.