File size: 5,302 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// -----------------------------------------------------------
class ScrollBarContainer : ScriptedWidgetEventHandler
{
	reference bool Invert;
	protected Widget Content;
	protected Widget ScrollBar;
	protected Widget Scroller;
	protected Widget m_root;
	
	const int WHEEL_STEP = 20;
	protected float		m_root_height;
	protected float		m_content_height;
	protected float		m_position;
	protected bool		m_scrolling;
	protected float		m_scrolling_start_pos;
	protected int		m_scrolling_mouse_pos;
		
	void ~ScrollBarContainer()
	{
		//if(GetGame() != NULL)
		//GetGame().GetDragQueue().RemoveCalls(this);
	}
	
	void ScrollFixedAmount( bool down, float amount )
	{
		m_root.Update();
		Content.Update();
		float width;
	
		m_root.GetScreenSize(width, m_root_height);
		Content.GetScreenSize(width, m_content_height);
		
		float diff = m_root_height / m_content_height;
		float one_percent = diff / 100;
		float percents = amount / m_content_height;
		//float step = (1.0 / (m_content_height - m_root_height)) * WHEEL_STEP;
		float step = (percents/100);
		if(down)
		m_position += 1 * ( percents + 0.05 );
		else
		m_position -= 1 * ( percents + 0.05 );
		
		if (m_position < 0) m_position = 0;
		if (m_position > 1 - diff) m_position = 1 - diff;
		UpdateScroller();
	}
	
	void ScrollToPos( float pos )
	{
		m_root.Update();
		Content.Update();
		float width;
	
		m_root.GetScreenSize(width, m_root_height);
		Content.GetScreenSize(width, m_content_height);
		
		float diff = m_root_height / m_content_height;
		float percents = pos / m_content_height;

		m_position = percents;
		
		if (m_position < 0)
			m_position = 0;
		if (m_position > 1 - diff)
			m_position = 1 - diff;
		UpdateScroller();
	}
	
	void ScrollToBottom()
	{
		m_root.Update();
		Content.Update();
		float width;
	
		m_root.GetScreenSize(width, m_root_height);
		Content.GetScreenSize(width, m_content_height);
		
		float diff = m_root_height / m_content_height;
		m_position = 1 - diff;
		UpdateScroller();
	}
	
	void ScrollToTop()
	{
		if( m_position != 0 )
		{
			m_position = 0;
			UpdateScroller();
		}
	}
	
	float GetContentYPos()
	{
		float x, y;
		Content.GetPos(x, y);
		return y;
	}
	
	float GetRootHeight()
	{
		return m_root_height;
	}
	
	void UpdateScroller()
	{
		m_root.Update();
		Content.Update();
		float width;
		float height;
		float diff;
		float scroller_height;
	
		m_root.GetScreenSize(width, m_root_height);
		Content.GetScreenSize(width, m_content_height);
	
		diff = m_content_height - m_root_height;
		if (diff <= 0)
		{
			Content.SetPos(0,0);
			Scroller.Show(false);
			ScrollBar.Show(false);
			m_position = 0;
			return;
		}
		
		scroller_height = (m_root_height / m_content_height) * m_root_height;

		ScrollBar.Show(true);
		Scroller.Show(true);
		Scroller.GetSize(width, height);
		Scroller.SetSize(width, scroller_height);

		float pos = ( -m_content_height * m_position );
		
		if( pos <= -diff )
			pos = -diff;
		
		Scroller.SetPos(0, -pos);
		
		if(Invert)
			Content.SetPos(0, -(diff + (-diff * m_position)));
		else
			Content.SetPos(0, pos);
	}

	void OnWidgetScriptInit(Widget w)
	{
		m_root = w;
		m_root.SetHandler(this);
		m_root.SetFlags(WidgetFlags.VEXACTPOS);
		m_scrolling = false;
		UpdateScroller();
	}
	
	protected void StopScrolling()
	{
		if (m_scrolling)
		{
			GetGame().GetDragQueue().RemoveCalls(this);
			m_scrolling = false;
		}
	}
	
	protected void UpdateScroll(int mouse_x, int mouse_y, bool is_dragging)
	{
		m_root.Update();
		Content.Update();
		float width;
	
		m_root.GetScreenSize(width, m_root_height);
		Content.GetScreenSize(width, m_content_height);
		
		if (m_scrolling)
		{
			if (is_dragging)
			{
				float diff = (mouse_y - m_scrolling_mouse_pos);
				float scroller_height = (m_root_height / m_content_height) * m_root_height; 
				m_position = m_scrolling_start_pos + (diff / (m_root_height - scroller_height));
				if (m_position < 0) m_position = 0;
				if (m_position > 1) m_position = 1;
			}
			else
			{
				m_scrolling = false;
				StopScrolling();
			}
		}
		
		UpdateScroller();
	}

	
	
	// ScriptedWidgetEventHandler override
	//--------------------------------------------------------------------------
	override bool OnMouseButtonDown(Widget  w, int  x, int  y, int  button)
	{
		if (button == MouseState.LEFT && w == Scroller && !m_scrolling)
		{
			m_scrolling = true;
			m_scrolling_start_pos = m_position;
			int mouse_x;
			GetMousePos(mouse_x, m_scrolling_mouse_pos);
			GetGame().GetDragQueue().Call(this, "UpdateScroll");
			return true;
		}
	
		return false;
	}
	
	//--------------------------------------------------------------------------
	override bool OnMouseButtonUp(Widget  w, int  x, int  y, int  button)
	{
		StopScrolling();
		return false;
	}
	
	//--------------------------------------------------------------------------
	override bool OnMouseWheel(Widget  w, int  x, int  y, int  wheel)
	{
		if (m_scrolling || m_content_height <= m_root_height) return false;
	
		float step = (1.0 / (m_content_height - m_root_height)) * WHEEL_STEP;
		m_position -= wheel * step;
	
		if (m_position < 0) m_position = 0;
		if (m_position > 1) m_position = 1;
		UpdateScroller();
		return true;
	}
	
	override bool OnResize( Widget  w, int x, int y) {
		if (w == m_root || w == Content) 
		{
			UpdateScroller();
		}
		return false;
	}
};