Animation: Difference between revisions
Content deleted Content added
Added R code |
|||
Line 804: | Line 804: | ||
input(pygame.event.wait()) |
input(pygame.event.wait()) |
||
pygame.display.flip()</lang> |
pygame.display.flip()</lang> |
||
=={{header|R}}== |
|||
{{libheader|gWidgets}} |
|||
The basic principle is:create a window with a label in it, then add a handler to the label for rotating the string, and another for changing direction on a click. Use of the tag function allows you to store the text flow direction as an attribute of the label. |
|||
<lang r> |
|||
rotate_string <- function(x, forwards) |
|||
{ |
|||
nchx <- nchar(x) |
|||
if(forwards) |
|||
{ |
|||
paste(substr(x, nchx, nchx), substr(x, 1, nchx - 1), sep = "") |
|||
} else |
|||
{ |
|||
paste(substr(x, 2, nchx), substr(x, 1, 1), sep = "") |
|||
} |
|||
} |
|||
handle_rotate_label <- function(obj, interval = 100) |
|||
{ |
|||
addHandlerIdle(obj, |
|||
handler = function(h, ...) |
|||
{ |
|||
svalue(obj) <- rotate_string(svalue(obj), tag(obj, "forwards")) |
|||
}, |
|||
interval = interval |
|||
) |
|||
} |
|||
handle_change_direction_on_click <- function(obj) |
|||
{ |
|||
addHandlerClicked(obj, |
|||
handler = function(h, ...) |
|||
{ |
|||
tag(h$obj, "forwards") <- !tag(h$obj, "forwards") |
|||
} |
|||
) |
|||
} |
|||
library(gWidgets) |
|||
library(gWidgetstcltk) #or library(gWidgetsRGtk2) or library(gWidgetsrJava) |
|||
lab <- glabel("Hello World! ", container = gwindow()) |
|||
tag(lab, "forwards") <- TRUE |
|||
handle_rotate_label(lab) |
|||
handle_change_direction_on_click(lab) |
|||
</lang> |
|||
=={{header|REBOL}}== |
=={{header|REBOL}}== |