-
Notifications
You must be signed in to change notification settings - Fork 1
/
progressbar.dm
202 lines (160 loc) · 5.9 KB
/
progressbar.dm
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
#define PROGRESSBAR_HEIGHT 6
#define PROGRESSBAR_ANIMATION_TIME 5
/datum/progressbar
///The progress bar visual element.
var/image/bar
///The target where this progress bar is applied and where it is shown.
var/atom/bar_loc
///The mob whose client sees the progress bar.
var/mob/user
///The client seeing the progress bar.
var/client/user_client
///Effectively the number of steps the progress bar will need to do before reaching completion.
var/goal = 1
///Control check to see if the progress was interrupted before reaching its goal.
var/last_progress = 0
///Variable to ensure smooth visual stacking on multiple progress bars.
var/listindex = 0
/datum/progressbar/New(mob/User, goal_number, atom/target)
. = ..()
if (!istype(target))
EXCEPTION("Invalid target given")
if(QDELETED(User) || !istype(User))
stack_trace("/datum/progressbar created with [isnull(User) ? "null" : "invalid"] user")
qdel(src)
return
if(!isnum(goal_number))
stack_trace("/datum/progressbar created with [isnull(User) ? "null" : "invalid"] goal_number")
qdel(src)
return
goal = goal_number
bar_loc = target
bar = image('icons/effects/progessbar.dmi', bar_loc, "prog_bar_0")
bar.plane = ABOVE_HUD_PLANE
bar.appearance_flags = APPEARANCE_UI_IGNORE_ALPHA
user = User
LAZYADDASSOCLIST(user.progressbars, bar_loc, src)
var/list/bars = user.progressbars[bar_loc]
listindex = bars.len
if(user.client)
user_client = user.client
add_prog_bar_image_to_client()
RegisterSignal(user, COMSIG_PARENT_QDELETING, .proc/on_user_delete)
RegisterSignal(user, COMSIG_MOB_LOGOUT, .proc/clean_user_client)
RegisterSignal(user, COMSIG_MOB_LOGIN, .proc/on_user_login)
/datum/progressbar/Destroy()
if(user)
for(var/pb in user.progressbars[bar_loc])
var/datum/progressbar/progress_bar = pb
if(progress_bar == src || progress_bar.listindex <= listindex)
continue
progress_bar.listindex--
progress_bar.bar.pixel_y = 32 + (PROGRESSBAR_HEIGHT * (progress_bar.listindex - 1))
var/dist_to_travel = 32 + (PROGRESSBAR_HEIGHT * (progress_bar.listindex - 1)) - PROGRESSBAR_HEIGHT
animate(progress_bar.bar, pixel_y = dist_to_travel, time = PROGRESSBAR_ANIMATION_TIME, easing = SINE_EASING)
LAZYREMOVEASSOC(user.progressbars, bar_loc, src)
user = null
if(user_client)
clean_user_client()
bar_loc = null
if(bar)
QDEL_NULL(bar)
return ..()
///Called right before the user's Destroy()
/datum/progressbar/proc/on_user_delete(datum/source)
SIGNAL_HANDLER
user.progressbars = null //We can simply nuke the list and stop worrying about updating other prog bars if the user itself is gone.
user = null
qdel(src)
///Removes the progress bar image from the user_client and nulls the variable, if it exists.
/datum/progressbar/proc/clean_user_client(datum/source)
SIGNAL_HANDLER
if(!user_client) //Disconnected, already gone.
return
user_client.images -= bar
user_client = null
///Called by user's Login(), it transfers the progress bar image to the new client.
/datum/progressbar/proc/on_user_login(datum/source)
SIGNAL_HANDLER
if(user_client)
if(user_client == user.client) //If this was not client handling I'd condemn this sanity check. But clients are fickle things.
return
clean_user_client()
if(!user.client) //Clients can vanish at any time, the bastards.
return
user_client = user.client
add_prog_bar_image_to_client()
///Adds a smoothly-appearing progress bar image to the player's screen.
/datum/progressbar/proc/add_prog_bar_image_to_client()
bar.pixel_y = 0
bar.alpha = 0
user_client.images += bar
animate(bar, pixel_y = 32 + (PROGRESSBAR_HEIGHT * (listindex - 1)), alpha = 255, time = PROGRESSBAR_ANIMATION_TIME, easing = SINE_EASING)
///Updates the progress bar image visually.
/datum/progressbar/proc/update(progress)
progress = clamp(progress, 0, goal)
if(progress == last_progress)
return
last_progress = progress
bar.icon_state = "prog_bar_[round(((progress / goal) * 100), 5)]"
///Called on progress end, be it successful or a failure. Wraps up things to delete the datum and bar.
/datum/progressbar/proc/end_progress()
if(last_progress != goal)
bar.icon_state = "[bar.icon_state]_fail"
animate(bar, alpha = 0, time = PROGRESSBAR_ANIMATION_TIME)
QDEL_IN(src, PROGRESSBAR_ANIMATION_TIME)
#undef PROGRESSBAR_ANIMATION_TIME
#undef PROGRESSBAR_HEIGHT
/**
* Creates a timed progressbar which invokes a callback when finished
* Arguments:
* * user - The user to show the progress bar to
* * delay - The amount of time until the progress bar finishes
* * finish_callback - The callback to invoke when the progress bar is finished
**/
#define TIMED_PROGRESS_BAR(user, delay, target, finish_callback) new /datum/timed_progressbar(user, delay, target, finish_callback)
/datum/timed_progressbar
/// The attached user.
var/mob/user
/// The target to parent the progress bar to
var/atom/target
/// The time for the progress bar to complete.
var/delay
/// The time at which the timer began.
var/start_time
/// The time at which the timer will finish.
var/end_time
/// The callback to invoke when the progressbar is finished .
var/datum/callback/finish_callback
/// The progress bar object itself
var/datum/progressbar/progress
/datum/timed_progressbar/New(user, delay, target, finish_callback)
. = ..()
src.user = user
src.delay = delay
src.finish_callback = finish_callback
src.target = target
end_time = world.time + delay
start_time = world.time
progress = new(user, delay, target)
START_PROCESSING(SSprogressbar, src)
/datum/timed_progressbar/process(delta_time)
if(QDELETED(progress))
finish_timer()
return PROCESS_KILL
progress.update(world.time - start_time)
if(end_time < world.time)
progress.end_progress()
finish_timer()
return PROCESS_KILL
/datum/timed_progressbar/proc/finish_timer()
finish_callback.Invoke()
qdel(src)
/datum/timed_progressbar/Destroy(force, ...)
if(!QDELETED(progress))
qdel(progress)
progress = null
user = null
target = null
qdel(finish_callback)
return ..()