How to resolve the algorithm Animation step by step in the E programming language

Published on 12 May 2024 09:40 PM
#E

How to resolve the algorithm Animation step by step in the E programming language

Table of Contents

Problem Statement

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.

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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Animation step by step in the E programming language

Source code in the e programming language

# State
var text := "Hello World! "
var leftward := false

# Window
def w := ("RC: Basic Animation")

# Text in window
w.setContentPane(def l := (text))
l.setOpaque(true) # repaints badly if not set!
l.addMouseListener(def mouseListener {
    to mouseClicked(_) {
        leftward := !leftward
    }
    match _ {}
})

# Animation
def anim := timer.every(100, fn _ { # milliseconds
    def s := text.size()
    l.setText(text := if (leftward) {
        text(1, s) + text(0, 1)
    } else {
        text(s - 1, s) + text(0, s - 1)
    })
})

# Set up window shape and close behavior
w.pack()
w.setLocationRelativeTo(null)
w.addWindowListener(def windowListener { 
    to windowClosing(_) { anim.stop() } 
    match _ {}
})

# Start everything
w.show()
anim.start()

def [reverse, stop] := {
    var text := "Hello World! "
    var leftward := false

    def anim := timer.every(100, fn _ { # milliseconds
        def s := text.size()
        text := if (leftward) {
            text(1, s) + text(0, 1)
        } else {
            text(s - 1, s) + text(0, s - 1)
        }
        print("\b" * s, text)
    })
    print("\n", text)
    anim.start()
    [def _() { leftward := !leftward; null }, anim.stop]
}

  

You may also check:How to resolve the algorithm Stair-climbing puzzle step by step in the E programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the E programming language
You may also check:How to resolve the algorithm Ordered words step by step in the E programming language
You may also check:How to resolve the algorithm Constrained genericity step by step in the E programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the E programming language