-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogressBar.cs
170 lines (145 loc) · 5.86 KB
/
progressBar.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
namespace ActionGame
{
class progressBar:GraphicalGameObject
{
Color[] frameC,backC,barC,bar2C,splitC;
Texture2D frame, back, bar, bar2,split;
public float MaxValue=100;
public float Value=0;
float newvalue;
bool isAnimating;
int Aframe = 0;
public float edge=2;
public int mode = 0;
public float animationSpeed=0.8f;
public bool showSplit;
public progressBar(Game1 game, Rectangle r,Color frameC, Color backC, Color barC, Color bar2C) : base(game, null)
{
mode = 0;
setLocation(r.X, r.Y);
setSize(r.Width, r.Height);
this.frameC = new Color[1*1];
this.backC = new Color[1 * 1];
this.barC = new Color[1 * 1];
this.bar2C = new Color[1 * 1];
this.frameC[0] = frameC;
this.backC[0] = backC;
this.barC[0] = barC;
this.bar2C[0] = bar2C;
this.splitC[0] = Color.White;
frame = new Texture2D(game.GraphicsDevice, 1, 1);
frame.SetData<Color>(this.frameC);
back = new Texture2D(game.GraphicsDevice, 1, 1);
back.SetData<Color>(this.backC);
bar = new Texture2D(game.GraphicsDevice, 1, 1);
bar.SetData<Color>(this.barC);
bar2 = new Texture2D(game.GraphicsDevice, 1, 1);
bar2.SetData<Color>(this.bar2C);
split = new Texture2D(game.GraphicsDevice, 1, 1);
split.SetData<Color>(this.splitC);
}
public progressBar(Game1 game, Rectangle r, Texture2D frame, Texture2D back, Texture2D bar) : base(game, null)
{
mode = 1;
setLocation(r.X, r.Y);
setSize(r.Width, r.Height);
this.frame = frame;
this.back = back;
this.bar = bar;
this.splitC = new Color[1 * 1];
this.splitC[0] = Color.White;
split = new Texture2D(game.GraphicsDevice, 1, 1);
split.SetData<Color>(this.splitC);
}
public override void update(float delta)
{
if(getWidth()/Width > 0.6f)
{
bar = game.assets.bar;
}else if (getWidth() / Width < 0.6f && 0.3f < getWidth() / Width)
{
bar = game.assets.bar2;
}
else if (getWidth() / Width < 0.3f)
{
bar = game.assets.bar3;
}
base.update(delta);
}
public override void Draw(SpriteBatch batch, float screenAlpha)
{
batch.Begin(transformMatrix: game.GetScaleMatrix());
if (mode == 0)
{
batch.Draw(frame, new Rectangle((int)X, (int)Y, (int)Width, (int)Height), Color.White * alpha);
batch.Draw(back, new Rectangle((int)(X + edge), (int)(Y + edge), (int)(Width - edge * 2), (int)(Height - edge * 2)), Color.White*alpha);
batch.Draw(bar, new Rectangle((int)(X + edge), (int)(Y + edge), getWidth(), (int)(Height - edge * 2) / 2), Color.White * alpha);
batch.Draw(bar2, new Rectangle((int)(X + edge), (int)(Y + edge + (Height - edge * 2) / 2), getWidth(), (int)(Height - edge * 2) / 2), Color.White * alpha);
}
else if (mode == 1)
{
batch.Draw(frame, new Rectangle((int)X, (int)Y, (int)Width, (int)Height), Color.White * alpha);
batch.Draw(back, new Rectangle((int)(X + edge), (int)(Y + edge), (int)(Width - edge * 2), (int)(Height - edge * 2)), Color.White * alpha);
batch.Draw(texture :bar, destinationRectangle: new Rectangle((int)(X + edge), (int)(Y + edge), getWidth(), (int)(Height - edge * 2)),sourceRectangle :new Rectangle(0,0,getWidth()*3,200),color:Color.White * alpha);
}
if (showSplit)
{
int i = 0;
for (i =1; i <=(int) (getWidth() / ((1 / MaxValue) * Width)) ; i++)
{
batch.Draw(split, new Rectangle((int)(X + i * (Width-edge*2) / MaxValue), (int) (Y + edge), 2, (int)(Height - edge * 2)),Color.White*0.5f);
}
}
batch.End();
}
public override void setSize(double w, double h)
{
this.Width = w; this.Height = h;
}
public int getWidth()
{
float res = (float)((Value / MaxValue) * Width - edge * 2);
if (isAnimating)
{
if (Value > newvalue)
{
res -= animationSpeed * Aframe;
if ((newvalue / MaxValue) * Width - edge * 2 >= res)
{
isAnimating = false;
Value = newvalue;
res = (float)((Value / MaxValue) * Width - edge * 2);
Aframe = 0;
}
}
else
{
res += animationSpeed * Aframe;
if ((newvalue / MaxValue) * Width - edge * 2 <= res)
{
isAnimating = false;
Value = newvalue;
res = (float)((Value / MaxValue) * Width - edge * 2);
Aframe = 0;
}
}
Aframe++;
}
return (int)res;
}
public void setValue(int v)
{
newvalue = v;
isAnimating = true;
}
}
}