File: tap_detector.ss

package info (click to toggle)
opensurge 0.6.1.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 47,404 kB
  • sloc: ansic: 62,066; sh: 739; makefile: 193; java: 110; xml: 66; javascript: 53
file content (97 lines) | stat: -rw-r--r-- 2,311 bytes parent folder | download | duplicates (2)
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
// -----------------------------------------------------------------------------
// File: tap_detector.ss
// Description: tap detector plugin (Debug Mode)
// Author: Alexandre Martins <http://opensurge2d.org>
// License: MIT
// -----------------------------------------------------------------------------

/*

This plugin is used to detect taps on the screen. Use it as in this example:

object "Debug Mode - My Tapping Test" is "debug-mode-plugin"
{
    fun onLoad(debugMode)
    {
        tapDetector = debugMode.plugin("Debug Mode - Tap Detector");
        tapDetector.subscribe(this);
    }

    fun onUnload(debugMode)
    {
        tapDetector = debugMode.plugin("Debug Mode - Tap Detector");
        tapDetector.unsubscribe(this);
    }

    fun onTapScreen(position)
    {
        // position is a Vector2 given in screen coordinates
        Console.print("Tapped at " + position);
    }
}

*/

object "Debug Mode - Tap Detector" is "debug-mode-plugin", "debug-mode-observable"
{
    observable = spawn("Debug Mode - Observable");
    touchId = -1;
    touchMoved = false;
    touchInitialTime = 0.0;
    tapTimeThreshold = 0.25; //0.2;

    fun onLoad(debugMode)
    {
        touchInput = debugMode.plugin("Debug Mode - Touch Input");
        touchInput.subscribe(this);
    }

    fun onUnload(debugMode)
    {
        touchInput = debugMode.plugin("Debug Mode - Touch Input");
        touchInput.unsubscribe(this);
    }

    fun onTouchBegin(touch)
    {
        touchId = touch.id;
        touchInitialTime = Time.time;
        touchMoved = false;
    }

    fun onTouchEnd(touch)
    {
        if(touchId == touch.id) {
            touchId = -1;

            if(!touchMoved) {
                if(Time.time <= touchInitialTime + tapTimeThreshold) {
                    observable.notify(touch.position);
                }
            }
        }
    }

    fun onTouchMove(touch)
    {
        if(touchId == touch.id) {
            touchMoved = true;
            //touchMoved = (touch.deltaPosition.length >= 0.5);
        }
    }

    fun subscribe(observer)
    {
        observable.subscribe(observer);
    }

    fun unsubscribe(observer)
    {
        observable.unsubscribe(observer);
    }

    fun onNotifyObserver(observer, position)
    {
        observer.onTapScreen(position);
    }
}