summaryrefslogtreecommitdiff
path: root/plugins/MirandaG15/src/LCDFramework/CLCDBar.cpp
blob: f5701edd351314fae6f42b6192394d49200bd00f (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
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
#include "stdafx.h"
#include "CLCDGfx.h"
#include "CLCDObject.h"
#include "CLCDBar.h"

//************************************************************************
// constructor
//************************************************************************
CLCDBar::CLCDBar()
{
	m_iSliderSize = 0;
	m_iPosition = 0;
	m_iMin = 0;
	m_iMax = 0;
	m_iMode = MODE_SCROLLBAR;
	m_iOrientation = DIRECTION_VERTICAL;
	m_iAlignment = TOP;
}

//************************************************************************
// destructor
//************************************************************************
CLCDBar::~CLCDBar()
{
}

//************************************************************************
// initializes the bar
//************************************************************************
bool CLCDBar::Initialize()
{
	return true;
}

//************************************************************************
// deinitializes the bar
//************************************************************************
bool CLCDBar::Shutdown()
{
	return true;
}

//************************************************************************
// specifies the bar's mode ( scrollbar / progressbar )
//************************************************************************
void CLCDBar::SetMode(int iMode)
{
	m_iMode = iMode;
}

//************************************************************************
// specifies the orientation of the bar
//************************************************************************
void CLCDBar::SetOrientation(int iOrientation)
{
	m_iOrientation = iOrientation;
}

//************************************************************************
// scrolls down/right
//************************************************************************
bool CLCDBar::ScrollDown()
{
	if(m_iPosition < m_iMax)
	{
		m_iPosition++;
		return true;
	}
	return false;
}

//************************************************************************
// scrolls up/left
//************************************************************************
bool CLCDBar::ScrollUp()
{
	if(m_iPosition > m_iMin)
	{
		m_iPosition--;
		return true;
	}
	return false;
}

//************************************************************************
// scrolls to the specified position
//************************************************************************
bool CLCDBar::ScrollTo(int iPosition)
{
	if(iPosition >= m_iMin && iPosition <= m_iMax)
	{
		m_iPosition = iPosition;
		return true;
	}
	return false;
}

//************************************************************************
// sets the size of the slider
//************************************************************************
void CLCDBar::SetSliderSize(int iSize)
{
	m_iSliderSize = iSize;
}

//************************************************************************
// sets the alignment of the scrollbar position
//************************************************************************
void CLCDBar::SetAlignment(int iAlignment)
{
	m_iAlignment = iAlignment;
}

//************************************************************************
// updates the bar
//************************************************************************
bool CLCDBar::Update()
{
	return true;
}

//************************************************************************
// specifies the bar's range
//************************************************************************
void CLCDBar::SetRange(int iMin, int iMax)
{
	m_iMin = iMin;
	m_iMax = iMax;
	if(m_iPosition < m_iMin)
		m_iPosition = m_iMin;
	else if(m_iPosition > m_iMax )
		m_iPosition = m_iMax;
}

//************************************************************************
// draws the bar
//************************************************************************
bool CLCDBar::Draw(CLCDGfx *pGfx)
{
	if((m_iMode != MODE_SCROLLBAR || m_iSliderSize > 0) && m_iMax >= m_iMin)
	{
		// draw border
		pGfx->DrawRect(0,0,GetWidth(),GetHeight());
		
		// initialize variables
		int iSize = (m_iMax - m_iMin)+1;
		int iPosition = m_iPosition - m_iMin;
		int iPixels = m_iOrientation == DIRECTION_VERTICAL?GetHeight():GetWidth();
		int iFirst=0,iLast=0;

		// generate scrollbar offsets
		if(m_iMode == MODE_SCROLLBAR)
		{
			int iOffset = iPosition;
			if(m_iSliderSize >= 2)
			{
				switch(m_iAlignment)
				{
				case CENTER:
					iOffset -= (m_iSliderSize-1)/2;
					break;
				case BOTTOM:
					iOffset -= (m_iSliderSize-1);
					break;
				case TOP:
					break;
				}
				if(iOffset < 0)
					iOffset = 0;
			}
			int iEnd = iOffset + m_iSliderSize;
			if(iEnd > iSize)
				iEnd = iSize;

			iFirst = iPixels*((float)iOffset/(float)iSize);
			iLast = iPixels*((float)iEnd/(float)iSize);
		}
		// generate progressbar offsets
		else if(m_iMode == MODE_PROGRESSBAR)
		{
			iFirst = 1;
			iLast = iPixels*((float)iPosition/(float)iSize);
		}

		// draw the bar
		if(m_iOrientation == DIRECTION_VERTICAL)
			pGfx->DrawFilledRect(1,iFirst,GetWidth()-1,iLast-iFirst);
		else
			pGfx->DrawFilledRect(iFirst,1,iLast-iFirst,GetHeight()-1);
	}
	return true;
}