By Daniel · December 30, 2008
Draws some text progressively, with a given delay between each character. The user can hold down a key (shift by default) to speed up the progression, or another key (space by default) to skip to the end.
// draw_text_progressive(x, y, string, sep, w, delay)
// Draws some text progressively, with _delay_ milliseconds between each
// character. The other arguments are the same as those in draw_text_ext.
var in, out, w, n_floor, n_ceil, n_mid, n, substr;
in = argument2;
out = '';
w = argument4;
// Settings
vk_fast = vk_shift;
vk_skip = vk_space;
while (string_length(in)) {
// Determine how many characters may fit on the next line via binary search
// n_floor is the (eventually largest) number that works; n_ceil is the
// (eventually smallest) number that does not.
n_floor = 1;
n_ceil = string_length(in) + 1;
while (n_ceil - n_floor > 1) {
n_mid = (n_ceil + n_floor) div 2;
if (string_width(string_copy(in, 1, n_mid)) > w) // doesn't fit; too large
n_ceil = n_mid;
else // does fit
n_floor = n_mid;
}
// Search backward until whitespace is encountered
n = n_floor;
if (n < string_length(in)) {
while (string_char_at(in, n) != ' ' && string_char_at(in, n + 1) != ' ') {
n -= 1;
if (n == 0) {
// Force line break within a word (no other good option)
n = n_floor;
break;
}
}
}
// Move this line from the input string to the output string
out += string_copy(in, 1, n) + '#';
in = string_delete(in, 1, n);
}
// Draw the result progressively
for (c = 1; c <= string_length(out); c += 1) {
io_handle();
if (keyboard_check_pressed(vk_skip))
c = string_length(out);
draw_text_ext(argument0, argument1, string_copy(out, 1, c), argument3, w);
screen_refresh();
if (!keyboard_check(vk_fast))
sleep(argument5);
}
do io_handle(); until keyboard_check_pressed(vk_skip);
Categories: 2D graphics, String handling
There are no comments to display.
You must be signed in to post comments.