aboutsummaryrefslogtreecommitdiff
path: root/Src/Wasabi/Lib/com/fillbar.m
blob: b28d7f824666b33e19a5c81edc6a595b78944b7d (plain) (blame)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
 * fillbar.m
 *
 * Manages custom fillbars.
 *
 * @package	com.winamp.maki.lib.community.fillbar
 * @author	mpdeimos
 * @date	08/10/01
 * @version	1.0
 */

#ifndef included
#error This script can only be compiled as a #include
#endif

Class Layer FillBar;
// {
	Member Map FillBar.fillmap;
	Member int FillBar.pos;
	Member boolean Fillbar.reverse;

	// User dragging stuff
	Member boolean Fillbar.dragable;
	Member boolean Fillbar.dragging;

	/**
	 * constructor
	 * 
	 * @param layer that should be handled like a fillbar
	 * @param bitmapID that should be used as region map
	 * @ret FillBar object
	 */
	Function FillBar FillBar_construct(Layer l, String bitmapID);
	Function FillBar_setMap(FillBar fb, String bitmapID);

	/**
	 * destructor, always call on script unloading
	 * 
	 */
	Function FillBar_destruct(FillBar fb);

	/**
	 * sets the region
	 * 
	 * @param fillbar to act on
	 * @param threshold of the map to generate a region
	 */
	Function FillBar_setPosition(FillBar fb, int threshold);

	/**
	 * called each time the users drags the fillbar
	 * 
	 * @param The dragged FillBar
	 * @param The alue the FillBar was dragged to.
	 * @ret FALSE if you do not want to allow dragging.
	 */
	Function boolean FillBar_onDrag(FillBar fb, int pos);

	/*
	 * called each time the users ends dragging the fillbar
	 * 
	 * @param The dragged FillBar
	 * @param The alue the FillBar was dragged to.
	 * @ret FALSE if you do not want to allow dragging.
	 */
	Function boolean FillBar_onEndDrag(FillBar fb, int pos);


	/*
	 * IMPLEMENTATION
	 */

	FillBar FillBar_construct(Layer l, String bitmapID)
	{
		FillBar fb = l;
		fb.reverse = TRUE;
		fb.fillmap = new Map;
		fb.fillmap.loadMap(bitmapID);
		return fb;
	}

	FillBar_setMap(Fillbar fb, String bitmapID)
	{
		if (fb.fillmap != NULL)
		{
			delete fb.fillmap;
		}
		
		fb.fillmap = new Map;
		fb.fillmap.loadMap(bitmapID);
	}

	FillBar_destruct(FillBar fb)
	{
		Map tmp = fb.fillmap;
		delete tmp;
	}

	FillBar_setPosition(FillBar fb, int threshold)
	{
		fb.pos = threshold;
		fb.setRegionFromMap(fb.fillmap, threshold, fb.reverse);
	}


	// User dragging handles

	FillBar.onLeftButtonDown (int x, int y)
	{
		if (!FillBar.dragable)
		{
			return;
		}
		
		Fillbar.dragging = TRUE;
	}

	FillBar.onMouseMove (int x, int y)
	{
		if (!FillBar.dragable || !Fillbar.dragging)
		{
			return;
		}

		int mouseLeft = x - FillBar.getLeft();
		int mouseTop = y - Fillbar.getTop();

		if (!FillBar.fillMap.inRegion(mouseLeft, mouseTop))
		{
			return;
		}
		
		int position = FillBar.fillMap.getValue(mouseLeft, mouseTop);

		int update = FillBar_onDrag(FillBar, position);
		
		if (update)
		{
			FillBar_setPosition(FillBar, position);
		}
	}
	
	Fillbar.onLeftButtonUp (int x, int y)
	{
		if (!FillBar.dragable || !Fillbar.dragging)
		{
			return;
		}

		int mouseLeft = x - FillBar.getLeft();
		int mouseTop = y - Fillbar.getTop();

		int position = FillBar.fillMap.getValue(mouseLeft, mouseTop);

		if (!FillBar.fillMap.inRegion(mouseLeft, mouseTop))
		{
			position = fb.pos;
		}

		int update = FillBar_onEndDrag(FillBar, position);
		
		if (update)
		{
			FillBar_setPosition(FillBar, position);
		}

		Fillbar.dragging = FALSE;
	}
	
	// Callback Stubs
	boolean FillBar_onDrag(Fillbar fb, int pos) {  return TRUE; }
	boolean FillBar_onEndDrag(Fillbar fb, int pos) { return TRUE; }
// }