Control: Change controls to use new callback infrastrucure.
[gemrb.git] / gemrb / core / Progressbar.cpp
blob3c055ce767ff9f6d6349cb7f4f822d8313c58167
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "Progressbar.h"
23 #include "win32def.h"
25 #include "Interface.h"
26 #include "Video.h"
28 #include <cstring>
30 Progressbar::Progressbar( unsigned short KnobStepsCount, bool Clear)
32 BackGround = NULL;
33 BackGround2 = NULL;
34 this->Clear = Clear;
35 this->KnobStepsCount = KnobStepsCount;
36 PBarAnim = NULL;
37 PBarCap = NULL;
38 KnobXPos = KnobYPos = 0;
39 CapXPos = CapYPos = 0;
40 ResetEventHandler( EndReached );
43 Progressbar::~Progressbar()
45 if (!Clear) {
46 return;
48 core->GetVideoDriver()->FreeSprite( BackGround );
49 core->GetVideoDriver()->FreeSprite( BackGround2 );
50 delete PBarAnim;
51 core->GetVideoDriver()->FreeSprite( PBarCap );
54 /** Draws the Control on the Output Display */
55 void Progressbar::Draw(unsigned short x, unsigned short y)
57 //it is unlikely that a floating window is above us, but...
58 if (!Changed && !(Owner->Flags&WF_FLOAT) ) {
59 return;
61 Changed = false;
62 if (XPos == 65535) {
63 return;
65 Sprite2D *bcksprite;
67 if((Value >= 100) && KnobStepsCount && BackGround2) {
68 bcksprite=BackGround2; //animated progbar end stage
70 else {
71 bcksprite=BackGround;
73 if (bcksprite) {
74 Region r( x + XPos, y + YPos, Width, Height );
75 core->GetVideoDriver()->BlitSprite( bcksprite,
76 x + XPos, y + YPos, true, &r );
77 if( bcksprite==BackGround2) {
78 return; //done for animated progbar
82 unsigned int Count;
84 if(!KnobStepsCount) {
85 //linear progressbar (pst, iwd)
86 int w = BackGround2->Width;
87 int h = BackGround2->Height;
88 //this is the PST/IWD specific part
89 Count = Value*w/100;
90 Region r( x + XPos + KnobXPos, y + YPos + KnobYPos, Count, h );
91 core->GetVideoDriver()->BlitSprite( BackGround2,
92 r.x, r.y, true, &r );
94 core->GetVideoDriver()->BlitSprite( PBarCap,
95 x+XPos+CapXPos+Count-PBarCap->Width, y+YPos+CapYPos, true );
96 return;
99 //animated progressbar (bg2)
100 Count=Value*KnobStepsCount/100;
101 for(unsigned int i=0; i<Count ;i++ ) {
102 Sprite2D *Knob = PBarAnim->GetFrame(i);
103 core->GetVideoDriver()->BlitSprite( Knob, x , y , true );
107 /** Returns the actual Progressbar Position */
108 unsigned int Progressbar::GetPosition()
110 return Value;
113 /** Sets the actual Progressbar Position trimming to the Max and Min Values */
114 void Progressbar::SetPosition(unsigned int pos)
116 if(pos>100) pos=100;
117 if (Value == pos)
118 return;
119 Value = pos;
120 Changed = true;
123 void Progressbar::RedrawProgressbar(const char* VariableName, int Sum)
125 if (strnicmp( VarName, VariableName, MAX_VARIABLE_LENGTH )) {
126 return;
128 SetPosition((unsigned int) Sum);
129 if((Value==100) && Changed)
130 RunEventHandler( EndReached );
133 /** Sets the selected image */
134 void Progressbar::SetImage(Sprite2D* img, Sprite2D* img2)
136 if (BackGround && Clear)
137 core->GetVideoDriver()->FreeSprite( BackGround );
138 BackGround = img;
139 if (BackGround2 && Clear)
140 core->GetVideoDriver()->FreeSprite( BackGround2 );
141 BackGround2 = img2;
142 Changed = true;
145 void Progressbar::SetBarCap(Sprite2D* img3)
147 core->GetVideoDriver()->FreeSprite( PBarCap );
148 PBarCap = img3;
151 void Progressbar::SetAnimation(Animation *arg)
153 delete PBarAnim;
154 PBarAnim = arg;
157 void Progressbar::SetSliderPos(int x, int y, int x2, int y2)
159 KnobXPos=x;
160 KnobYPos=y;
161 CapXPos=x2;
162 CapYPos=y2;
165 /* dummy virtual function */
166 int Progressbar::SetText(const char* /*string*/, int /*pos*/)
168 return 0;
171 bool Progressbar::SetEvent(int eventType, EventHandler handler)
173 Changed = true;
175 switch (eventType) {
176 case IE_GUI_PROGRESS_END_REACHED:
177 EndReached = handler;
178 break;
179 default:
180 return false;
183 return true;