summaryrefslogtreecommitdiff
path: root/httemplate/elements/qlib/sprite.js
blob: 72a68fb7c39e798397e5b10fda1009759d95ca1e (plain)
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
115
116
117
118
119
120
121
122
123
124
125
/**
 * QLIB 1.0 Sprite Object
 * Copyright (C) 2002 2003, Quazzle.com Serge Dolgov
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * http://qlib.quazzle.com
 */

function QSprite_load(src) {
    if (src) {
        this.face = new Image(this.cwidth, this.cheight);
        this.face.src = src;
        this.valid = false;
    }
}
 
function QSprite_show(show) {
    if (show && !this.valid && this.face.complete) {
        this._img.src = this.face.src;
        this.valid = true;
    }
    this._show(show);
}

function QSprite_moveTo(x, y) {
    this.stop();
    this._move(x, y);
}

function QSprite_slideTo(x, y) {
    this.stop();
    if (this.visible) {
        this.doSlide(++this._spro, x, y);
    } else {
        this.moveTo(x, y);
    }
}

function QSprite_shake() {
    this.stop();
    if (this.visible) {
        this.doShake(++this._spro, 0, this.x, this.y);
    }
}

function QSprite_stop() {
    this._spro++;
    if (this._sprt) {
        clearTimeout(this._sprt);
        this._sprt = false;
    }
}

function QSprite_doSlide(id, x, y) {
    if (this._spro == id) {
        this._sprt = false;
        var dx = Math.round(x - this.x);
        var dy = Math.round(y - this.y);
        if (dx || dy) {
            if (dx) dx = dx > 0 ? Math.ceil(dx/4) : Math.floor(dx/4);
            if (dy) dy = dy > 0 ? Math.ceil(dy/4) : Math.floor(dy/4);
            this._move(this.x + dx, this.y + dy);
            this._sprt = setTimeout(this.name + ".doSlide(" + id + "," + x + "," + y + ")", 30);
        } else {
            this._move(x, y);
        }
    }
}

function QSprite_doShake(id, phase, x, y) {
    if (this._spro == id) {
        this._sprt = false;
        if (phase < 20) {
            var m = 3 * Math.sin(.16 * phase);
            this._move(x + m * Math.sin(phase), y + m * Math.cos(phase));
            this._sprt = setTimeout(this.name + ".doShake(" + id + "," + (++phase) + "," + x + "," + y + ")", 20);
        } else {
            this._move(x, y);
        }
    }
}

function QSprite_doClick() {
    if (!this._sprt) {
        this.onClick(this.tag);
    }
    return false;
}

function QSprite(parent, name, x, y, width, height, src, visible, effects, opacity, zindex) {
    this.init(parent, name);
    this.x = x - 0;
    this.y = y - 0;
    this.width = (this.cwidth = width - 0) + 8;
    this.height = (this.cheight = height - 0) + 8;
    var j = QSprite.arguments.length;
    this.visible = (j > 7) ? visible : true;
    this.effects = (j > 8) ? effects : 0;
    this.opacity = (j > 9) ? opacity : 100;
    this.zindex  = (j > 10) ? zindex : null;
    this.valid = !!src;
    this.content = '<a href="#" title="" onclick="return false" onmousedown="return ' + this.name +
        '.doClick()" onmouseover="window.top.status=\'\';return true" hidefocus="true" unselectable="on"><img name="' +
        this.id + '" src="' + (src || '') + '" border="0" width="' + this.cwidth + '" height="' + this.cheight +
        '" alt="" unselectable="on"></a>';
    this.doClick = QSprite_doClick;
    this.doSlide = QSprite_doSlide;
    this.doShake = QSprite_doShake;
    this.onClick = QControl.event;
    this.create();
    this.face = this._img = this.document.images[this.id] || new Image(1, 1);
    this._spro = 0;
    this._sprt = false;
    this._show = this.show;
    this._move = this.moveTo;
    this.load = QSprite_load;
    this.show = QSprite_show;
    this.moveTo = QSprite_moveTo;
    this.slideTo = QSprite_slideTo;
    this.shake = QSprite_shake;
    this.stop = QSprite_stop;
}
QSprite.prototype = new QWndCtrl();