File: fader.ss

package info (click to toggle)
opensurge 0.6.1.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 47,404 kB
  • sloc: ansic: 62,066; sh: 739; makefile: 193; java: 110; xml: 66; javascript: 53
file content (114 lines) | stat: -rw-r--r-- 2,382 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// -----------------------------------------------------------------------------
// File: fader.ss
// Description: simple fader object (fade-in & fade-out)
// Author: Alexandre Martins <http://opensurge2d.org>
// License: MIT
// -----------------------------------------------------------------------------
using SurgeEngine.Transform;
using SurgeEngine.Vector2;
using SurgeEngine.Actor;
using SurgeEngine.Video.Screen;

//
// Fader is a transition effect used to fade the screen
//
// Functions:
// - fadeIn(): fade from black
// - fadeOut(): fade to black
//
// Properties:
// - fadeTime: number. Transition time, in seconds
//
// Example - how to use:
//
// object "FadeTest" is "entity" {
//     fader = spawn("Fader");
//
//     state "main" {
//         fader.fadeIn();
//         state = "wait";
//     }
//
//     state "wait" {
//         if(timeout(fader.fadeTime + 2.0)) {
//             fader.fadeOut();
//             state = "done";
//         }
//     }
//
//     state "done" { }
// }
//
object "Fader" is "entity", "detached", "awake", "private"
{
    actor = Actor("Fade Effect");
    transform = Transform();
    fadeTime = 0.5; // seconds

    state "main"
    {
        // setup
        transform.position = Vector2.zero;
        transform.localScale = Vector2(Screen.width / actor.width, Screen.height / actor.height);
        state = "idle";
    }

    state "idle"
    {
    }

    state "fade out"
    {
        actor.alpha += Time.delta / fadeTime;
        if(actor.alpha >= 1.0) {
            actor.alpha = 1.0;
            state = "idle";
        }
    }

    state "fade in"
    {
        actor.alpha -= Time.delta / fadeTime;
        if(actor.alpha <= 0.0) {
            actor.alpha = 0.0;
            actor.visible = false;
            state = "idle";
        }
    }

    fun fadeOut()
    {
        actor.alpha = 0.0;
        actor.visible = true;
        state = "fade out";
    }

    fun fadeIn()
    {
        actor.alpha = 1.0;
        actor.visible = true;
        state = "fade in";
    }

    fun get_fadeTime()
    {
        return fadeTime;
    }

    fun set_fadeTime(seconds)
    {
        fadeTime = Math.max(seconds, 0.001);
    }

    fun setFadeTime(seconds)
    {
        this.set_fadeTime(seconds);
        return this;
    }

    fun constructor()
    {
        actor.zindex = Math.infinity;
        actor.visible = false;
    }
}