File: ProgressBar.cpp

package info (click to toggle)
between 6%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,532 kB
  • sloc: cpp: 28,110; php: 718; ansic: 638; objc: 245; sh: 236; makefile: 97; perl: 67
file content (122 lines) | stat: -rw-r--r-- 2,562 bytes parent folder | download | duplicates (19)
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
// Jason Rohrer
// ProgressBar.cpp

/**
*
*	ProgressBar Gui element implementation
*
*
*	Created 11-6-99
*	Mods:	
*		Jason Rohrer	11-8-99		Changed to use GraphicBuffer object as screen buffer
*
*/



#include "ProgressBar.h"


ProgressBar::ProgressBar(int x, int y, int w, int h, Color bordC, Color hC, Color tipC) {

	startX = x;
	startY = y;
	wide = w;
	high = h;
	
	barWide = wide - 2*borderWide;
	barHigh = high - 2*borderWide;
	
	
	borderC = bordC;
	highC = hC;
	barTipC = tipC;
	
	
	imageMap = new unsigned long[high * wide];
	
	mapYOffset = new int[high];	
	
	// precalc y offsets into 2d image map
	for( int y=0; y<high; y++ ) {
		mapYOffset[y] = y*wide;
		}
		
	// prepare image map	
	for( int y=0; y<high; y++ ) {
		int yContrib = mapYOffset[y];
		
		for( int x=0; x<wide; x++ ) {
			
			if( y<borderWide || x<borderWide || y>high-borderWide-1 || x>wide-borderWide-1 ) {
				imageMap[ yContrib + x ] = borderC.composite;			// border
				}
			else {
				imageMap[ yContrib + x ] = 0xFF000000;			// black inside
				}
					
			}
	
		}
	
	lastProgress = 0;
	}
	
	
ProgressBar::~ProgressBar() {
	delete [] imageMap;
	delete [] mapYOffset;
	}
	
	
void ProgressBar::setProgress(float fractionFull) {
	
	if( fractionFull < 0) fractionFull = 0;
	if( fractionFull > 1) fractionFull = 1;
	
	if( fractionFull < lastProgress ) {		// decreasing proress, erase part of bar 

		
		int deleteXStart = (int)((fractionFull) * barWide + borderWide);
		
		int deleteXEnd = (int)((lastProgress) * barWide + borderWide);
		
		
		for( int y=borderWide; y<high-borderWide; y++ ) {
		
			int yContrib = mapYOffset[y];
			
			for( int x=deleteXStart; x<=deleteXEnd; x++ ) {
				imageMap[ yContrib + x ] = 0xFF000000;			// erase to black
				}
			// color bar tip
			imageMap[yContrib + deleteXStart] = barTipC.composite;
			}
		}
	else if( fractionFull > lastProgress) {		//progress has increased
	
		int addXStart = (int)((lastProgress) * barWide + borderWide);
		int addXEnd = (int)((fractionFull) * barWide + borderWide);
		
		float weight = lastProgress;
		float deltaWeight = (fractionFull - lastProgress) / (addXEnd - addXStart);

		for( int x=addXStart; x<addXEnd; x++) {
			// weighted color
			unsigned long thisColor = highC.getWeightedComposite(weight);
			weight += deltaWeight;
			for( int y=borderWide; y<high-borderWide; y++) {
				imageMap[mapYOffset[y] + x] = thisColor;
				}
			}
		// color bar tip
		for( int y=borderWide; y<high-borderWide; y++) {
			imageMap[mapYOffset[y] + addXEnd] = barTipC.composite;
			}
		}
	
	lastProgress = fractionFull;
	}