animate_plot.lua


NAME
    animate_plot

FUNCTION
    animate_plot(render, width, height, plot, rotz, rotx)

NOTES
    Creates a window and renders the plot to it for animation.
    Double click the left mouse button to start/stop the animation.
    Click the right mouse button to restore the original state.
    Use arrow keys to control rotation directions.

INPUTS
    render - zeRender object
    width  - image width
    height - image height
    plot   - zePlot object
    rotz   - initial rotation angle (degree) about the z-axis.
    rotx   - initial rotation angle (degree) about the x-axis.

OUTPUTS
    None

SOURCE

require("register")

function animate_plot(render, width, height, plot, rotz, rotx)

    local deg = 2
    local interval = 30
    local hwnd = 0
    local key = 0
    local timer = true
    local rotx0 = rotx
    local rotz0 = rotz

    callback = function(message, hwnd, wparm, lwparm, hwparm, lparm, llparm, hlparm)
        if (message == "PAINT") then
            if (hwnd > 0) then
                render:towindow(hwnd, 1)  
            end
        elseif (message =="KEYUP") then
            if (wparm >= 37 and wparm <= 40) then
                if (wparm == 37) then
                    -- left arrow
                    rotz = rotz - deg
                elseif (wparm == 38) then
                    -- up arrow
                    rotx = rotx - deg
                elseif (wparm == 39) then
                    -- right arrow
                    rotz = rotz + deg
                else
                    -- down arrow
                    rotx = rotx + deg
                end
                key = wparm
                plot:rotate(rotz, rotx)
                render:towindow(hwnd, 1)
            end
        elseif (message =="LBUTTONDBLCLK") then
            if (timer) then
                zeWindow.timer(interval)
                timer = false
            else
                zeWindow.timer(0)
                timer = true
            end
        elseif (message =="RBUTTONDOWN") then
                zeWindow.timer(0)
                timer = true
                plot:rotate(rotz0, rotx0)
                rotx = rotx0
                rotz = rotz0
                render:towindow(hwnd, 1)
        elseif (message == "TIMER") then
            if (key >= 37 and key <= 40) then
                if (key == 37) then
                    rotz = rotz - deg
                elseif (key == 38) then
                    rotx = rotx - deg
                elseif (key == 39) then
                    rotz = rotz + deg
                else
                    rotx = rotx + deg
                end
                plot:rotate(rotz, rotx)
                render:towindow(hwnd, 1)  
            end
        elseif (message == "CREATE") then
            hwnd = wparm
        end
    end

    zeWindow.create("callback",  width, height)
end