Repository navigation
Expand file tree
/
Copy pathprogressbar.lua
More file actions
282 lines (208 loc) · 9.94 KB
/
Copy pathprogressbar.lua
File metadata and controls
282 lines (208 loc) · 9.94 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
require('common');
require('libs/bitmap');
require('libs/color');
local imgui = require('imgui');
local ffi = require('ffi');
local d3d = require('d3d8');
local d3d8dev = d3d.get_device();
local progressbar = {
-- Bookend
bookendFilename = 'bookend',
-- Background
backgroundGradientStartColor = '#0a0f1a',
backgroundGradientEndColor = '#101825',
backgroundRounding = 3,
-- Foreground
foregroundRounding = 3,
foregroundPadding = 3,
gradientTextures = {}
};
function MakeGradientBitmap(startColor, endColor)
local height = 100;
local image = bitmap:new(1, height);
startColor = table.pack(hex2rgb(startColor));
endColor = table.pack(hex2rgb(endColor));
for pixel = 1, height do
local red = startColor[1] + (endColor[1] - startColor[1]) * (pixel / height);
local green = startColor[2] + (endColor[2] - startColor[2]) * (pixel / height);
local blue = startColor[3] + (endColor[3] - startColor[3]) * (pixel / height);
image:setPixelColor(1, (height - pixel) + 1, {red, green, blue, 255});
end
return image;
end
function GetGradient(startColor, endColor)
local texture;
for i, existingTexture in ipairs(progressbar.gradientTextures) do
if (existingTexture.startColor == startColor and existingTexture.endColor == endColor) then
texture = existingTexture;
break;
end
end
if not texture then
local image = MakeGradientBitmap(startColor, endColor);
local texture_ptr = ffi.new('IDirect3DTexture8*[1]');
local res = ffi.C.D3DXCreateTextureFromFileInMemory(d3d8dev, image:binary(), #image:binary(), texture_ptr);
if (res ~= ffi.C.S_OK) then
error(('%08X (%s)'):fmt(res, d3d.get_error(res)));
end
texture = {
startColor = startColor,
endColor = endColor,
texture = ffi.new('IDirect3DTexture8*', texture_ptr[0])
}
d3d.gc_safe_release(texture.texture);
table.insert(progressbar.gradientTextures, texture);
end
return tonumber(ffi.cast("uint32_t", texture.texture));
end
function GetBookendTexture()
if not progressbar.bookendTexture then
progressbar.bookendTexture = LoadTexture(progressbar.bookendFilename).image;
end
return tonumber(ffi.cast("uint32_t", progressbar.bookendTexture));
end
progressbar.DrawBar = function(startPosition, endPosition, gradientStart, gradientEnd, rounding, cornerFlags)
if not rounding then
rounding = 0;
end
local gradient = GetGradient(gradientStart, gradientEnd);
imgui.GetWindowDrawList():AddImageRounded(gradient, startPosition, endPosition, {0, 0}, {1, 1}, IM_COL32_WHITE, rounding, cornerFlags);
end
progressbar.DrawColoredBar = function(startPosition, endPosition, color, rounding, cornerFlags)
if not rounding then
rounding = 0;
end
imgui.GetWindowDrawList():AddRectFilled(startPosition, endPosition, color, rounding, cornerFlags);
end
progressbar.DrawBookends = function(positionStartX, positionStartY, width, height)
local bookendTexture = GetBookendTexture();
local bookendWidth = height / 2;
-- Draw the left bookend
imgui.GetWindowDrawList():AddImage(bookendTexture, {positionStartX, positionStartY}, {positionStartX + bookendWidth, positionStartY + height}, {0, 0}, {1, 1}, IM_COL32_WHITE);
-- Draw the right bookend
imgui.GetWindowDrawList():AddImage(bookendTexture, {positionStartX + width - bookendWidth, positionStartY}, {positionStartX + width, positionStartY + height}, {1, 1}, {0, 0}, IM_COL32_WHITE);
end
progressbar.ProgressBar = function(percentList, dimensions, options)
if options == nil then
options = {};
end
-- Decorate by default
if options.decorate == nil then
options.decorate = true;
end
local positionStartX, positionStartY = imgui.GetCursorScreenPos();
local width = dimensions[1];
local height = dimensions[2];
-- If our width is 0 or less, we instead get the content region's available space
-- which allows us to stretch the progress bar to fit the content region.
if width <= 0 then
width = imgui.GetContentRegionAvail();
end
local contentWidth = width;
local contentPositionStartX = positionStartX;
local contentPositionStartY = positionStartY;
local rounding;
-- Draw the bookends!
if options.decorate then
local bookendWidth = height / 2;
contentWidth = width - (bookendWidth * 2);
contentPositionStartX = contentPositionStartX + bookendWidth;
progressbar.DrawBookends(positionStartX, positionStartY, width, height);
end
-- Draw the background
local bgGradientStart = progressbar.backgroundGradientStartColor;
local bgGradientEnd = progressbar.backgroundGradientEndColor;
if options.backgroundGradientOverride then
bgGradientStart = options.backgroundGradientOverride[1];
bgGradientEnd = options.backgroundGradientOverride[2];
end
rounding = options.decorate and progressbar.backgroundRounding or gConfig.noBookendRounding;
progressbar.DrawBar({contentPositionStartX, contentPositionStartY}, {contentPositionStartX + contentWidth, contentPositionStartY + height}, bgGradientStart, bgGradientEnd, rounding);
-- Compute the actual progress bar's width and height
local paddingHalf = progressbar.foregroundPadding / 2;
local progressPositionStartX = contentPositionStartX + paddingHalf;
local progressPositionStartY = contentPositionStartY + paddingHalf;
local progressTotalWidth = contentWidth - progressbar.foregroundPadding;
local progressHeight = height - progressbar.foregroundPadding;
-- Draw the progress bar(s)
local progressOffset = 0;
for i, percentData in ipairs(percentList) do
local percent = math.clamp(percentData[1], 0, 1);
local cornerFlags = ImDrawCornerFlags_All;
if #percentList > 1 then
if i == 1 then
cornerFlags = ImDrawCornerFlags_Left;
elseif i == #percentList then
cornerFlags = ImDrawCornerFlags_Right;
else
cornerFlags = ImDrawCornerFlags_None;
end
end
if percent > 0 then
local startColor = percentData[2][1];
local endColor = percentData[2][2];
local overlayConfiguration = percentData[3];
local progressWidth = progressTotalWidth * percent;
rounding = options.decorate and progressbar.foregroundRounding or gConfig.noBookendRounding;
progressbar.DrawBar({progressPositionStartX + progressOffset, progressPositionStartY}, {progressPositionStartX + progressOffset + progressWidth, progressPositionStartY + progressHeight}, startColor, endColor, rounding, cornerFlags);
if overlayConfiguration then
local overlayColor = overlayConfiguration[1];
local overlayAlpha = overlayConfiguration[2];
-- local overlayPercent = overlayConfiguration[3];
local overlayWidth = progressWidth;
local overlayCornerFlags = cornerFlags;
--[[
if overlayPercent then
overlayWidth = progressTotalWidth * overlayPercent;
overlayCornerFlags = ImDrawCornerFlags_None;
end
]]--
local red, green, blue = hex2rgb(overlayColor);
local overlayBarColor = imgui.GetColorU32({red / 255, green / 255, blue / 255, overlayAlpha});
rounding = options.decorate and progressbar.foregroundRounding or gConfig.noBookendRounding;
progressbar.DrawColoredBar({progressPositionStartX + progressOffset, progressPositionStartY}, {progressPositionStartX + progressOffset + overlayWidth, progressPositionStartY + progressHeight}, overlayBarColor, rounding, cornerFlags);
end
progressOffset = progressOffset + progressWidth;
end
end
-- Draw the optional overlay bar (used for TP)
if options.overlayBar then
local overlayPercent = options.overlayBar[1][1];
local overlayGradientStart = options.overlayBar[1][2][1];
local overlayGradientEnd = options.overlayBar[1][2][2];
local overlayHeight = options.overlayBar[2];
local overlayTopPadding = options.overlayBar[3];
local overlayWidth = progressTotalWidth;
-- Draw the overlay background
rounding = options.decorate and progressbar.backgroundRounding or gConfig.noBookendRounding;
progressbar.DrawBar({progressPositionStartX, progressPositionStartY + progressHeight - overlayHeight}, {progressPositionStartX + overlayWidth, progressPositionStartY + progressHeight}, progressbar.backgroundGradientStartColor, progressbar.backgroundGradientEndColor, rounding);
-- Draw the overlay progress bar
local overlayProgressWidth = overlayWidth * overlayPercent;
rounding = options.decorate and progressbar.foregroundRounding or gConfig.noBookendRounding;
progressbar.DrawBar({progressPositionStartX, progressPositionStartY + progressHeight - overlayHeight + overlayTopPadding}, {progressPositionStartX + overlayProgressWidth, progressPositionStartY + progressHeight}, overlayGradientStart, overlayGradientEnd, rounding);
-- Allow optional pulsing of overlay bars
local pulseConfiguration = options.overlayBar[4];
if pulseConfiguration then
local currentTime = os.clock();
local timePerPulse = pulseConfiguration[2];
local phase = currentTime % timePerPulse;
local pulseAlpha = (2 / timePerPulse) * phase;
if pulseAlpha > 1 then
pulseAlpha = 2 - pulseAlpha;
end
local pulseColor = pulseConfiguration[1];
local red, green, blue = hex2rgb(pulseColor);
local pulseBarColor = imgui.GetColorU32({red / 255, green / 255, blue / 255, pulseAlpha});
rounding = options.decorate and progressbar.foregroundRounding or gConfig.noBookendRounding;
progressbar.DrawColoredBar({progressPositionStartX, progressPositionStartY + progressHeight - overlayHeight + overlayTopPadding}, {progressPositionStartX + overlayProgressWidth, progressPositionStartY + progressHeight}, pulseBarColor, rounding);
end
end
if options.borderConfig then
local borderWidth = options.borderConfig[1];
local borderColorRed, borderColorGreen, borderColorBlue = hex2rgb(options.borderConfig[2]);
rounding = options.decorate and height/2 or gConfig.noBookendRounding;
imgui.GetWindowDrawList():AddRect({positionStartX - (borderWidth / 2), positionStartY - (borderWidth / 2)}, {positionStartX + width + (borderWidth / 2), positionStartY + height + (borderWidth / 2)}, imgui.GetColorU32({borderColorRed / 255, borderColorGreen / 255, borderColorBlue / 255, 1}), rounding, ImDrawCornerFlags_All, borderWidth);
end
imgui.Dummy({width, height});
end
return progressbar;