
Animation is integral to many parts of GUIs, including both the fancy effects when things change used in window managers, and of course games.   The core of any animation system is a scheme for periodically changing the display while still remaining responsive to the user.   This task demonstrates this.



Task

Create a window containing the string "Hello World! " (the trailing space is significant).

Make the text appear to be rotating right by periodically removing one letter from the end of the string and attaching it to the front.

When the user clicks on the (windowed) text, it should reverse its direction.




import gui
$include "guih.icn"
 
class WindowApp : Dialog (label, direction)
 
  method rotate_left (msg)
    return msg[2:0] || msg[1]
  end
 
  method rotate_right (msg)
    return msg[-1:0] || msg[1:-1]
  end
 
  method reverse_direction ()
    direction := 1-direction
  end
 
  # this method gets called by the ticker, and updates the label
  method tick ()
    static msg := "Hello World! "
    if direction = 0 
      then msg := rotate_left (msg)
      else msg := rotate_right (msg)
    label.set_label(msg)
  end
 
  method component_setup ()
    direction := 1 # start off rotating to the right
    label := Label("label=Hello World! ", "pos=0,0")
    # respond to a mouse click on the label
    label.connect (self, "reverse_direction", MOUSE_RELEASE_EVENT)
    add (label)
 
    connect (self, "dispose", CLOSE_BUTTON_EVENT)
    # tick every 100ms
    self.set_ticker (100)
  end
end
 
# create and show the window
procedure main ()
  w := WindowApp ()
  w.show_modal ()
end
 