GameScript: Move initialization out of constructor.
[gemrb.git] / gemrb / core / Progressbar.cpp
blob12a7724be39b8e374ca6f09548b86005e87dadda
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 <cstring>
22 #include "win32def.h"
23 #include "Progressbar.h"
24 #include "Interface.h"
25 #include "Video.h"
27 Progressbar::Progressbar( unsigned short KnobStepsCount, bool Clear)
29 BackGround = NULL;
30 BackGround2 = NULL;
31 this->Clear = Clear;
32 this->KnobStepsCount = KnobStepsCount;
33 PBarAnim = NULL;
34 PBarCap = NULL;
35 KnobXPos = KnobYPos = 0;
36 CapXPos = CapYPos = 0;
37 ResetEventHandler( EndReached );
40 Progressbar::~Progressbar()
42 if (!Clear) {
43 return;
45 core->GetVideoDriver()->FreeSprite( BackGround );
46 core->GetVideoDriver()->FreeSprite( BackGround2 );
47 delete PBarAnim;
48 core->GetVideoDriver()->FreeSprite( PBarCap );
51 /** Draws the Control on the Output Display */
52 void Progressbar::Draw(unsigned short x, unsigned short y)
54 //it is unlikely that a floating window is above us, but...
55 if (!Changed && !(Owner->Flags&WF_FLOAT) ) {
56 return;
58 Changed = false;
59 if (XPos == 65535) {
60 return;
62 Sprite2D *bcksprite;
64 if((Value >= 100) && KnobStepsCount && BackGround2) {
65 bcksprite=BackGround2; //animated progbar end stage
67 else {
68 bcksprite=BackGround;
70 if (bcksprite) {
71 Region r( x + XPos, y + YPos, Width, Height );
72 core->GetVideoDriver()->BlitSprite( bcksprite,
73 x + XPos, y + YPos, true, &r );
74 if( bcksprite==BackGround2) {
75 return; //done for animated progbar
79 unsigned int Count;
81 if(!KnobStepsCount) {
82 //linear progressbar (pst, iwd)
83 int w = BackGround2->Width;
84 int h = BackGround2->Height;
85 //this is the PST/IWD specific part
86 Count = Value*w/100;
87 Region r( x + XPos + KnobXPos, y + YPos + KnobYPos, Count, h );
88 core->GetVideoDriver()->BlitSprite( BackGround2,
89 r.x, r.y, true, &r );
91 core->GetVideoDriver()->BlitSprite( PBarCap,
92 x+XPos+CapXPos+Count-PBarCap->Width, y+YPos+CapYPos, true );
93 return;
96 //animated progressbar (bg2)
97 Count=Value*KnobStepsCount/100;
98 for(unsigned int i=0; i<Count ;i++ ) {
99 Sprite2D *Knob = PBarAnim->GetFrame(i);
100 core->GetVideoDriver()->BlitSprite( Knob, x , y , true );
104 /** Returns the actual Progressbar Position */
105 unsigned int Progressbar::GetPosition()
107 return Value;
110 /** Sets the actual Progressbar Position trimming to the Max and Min Values */
111 void Progressbar::SetPosition(unsigned int pos)
113 if(pos>100) pos=100;
114 if (Value == pos)
115 return;
116 Value = pos;
117 Changed = true;
120 void Progressbar::RedrawProgressbar(const char* VariableName, int Sum)
122 if (strnicmp( VarName, VariableName, MAX_VARIABLE_LENGTH )) {
123 return;
125 SetPosition((unsigned int) Sum);
126 if((Value==100) && Changed)
127 RunEventHandler( EndReached );
130 /** Sets the selected image */
131 void Progressbar::SetImage(Sprite2D* img, Sprite2D* img2)
133 if (BackGround && Clear)
134 core->GetVideoDriver()->FreeSprite( BackGround );
135 BackGround = img;
136 if (BackGround2 && Clear)
137 core->GetVideoDriver()->FreeSprite( BackGround2 );
138 BackGround2 = img2;
139 Changed = true;
142 void Progressbar::SetBarCap(Sprite2D* img3)
144 core->GetVideoDriver()->FreeSprite( PBarCap );
145 PBarCap = img3;
148 void Progressbar::SetAnimation(Animation *arg)
150 delete PBarAnim;
151 PBarAnim = arg;
154 void Progressbar::SetSliderPos(int x, int y, int x2, int y2)
156 KnobXPos=x;
157 KnobYPos=y;
158 CapXPos=x2;
159 CapYPos=y2;
162 /* dummy virtual function */
163 int Progressbar::SetText(const char* /*string*/, int /*pos*/)
165 return 0;
168 bool Progressbar::SetEvent(int eventType, const char *handler)
170 Changed = true;
172 switch (eventType) {
173 case IE_GUI_PROGRESS_END_REACHED:
174 SetEventHandler( EndReached, handler );
175 break;
176 default:
177 return false;
180 return true;