Skip to content

Commit 18e0387

Browse files
authored
Cleanup browser.dm (#8764)
# About the pull request This PR is a follow up to #8734 that changed the type of a browser user from mob to client but there were a few lingering situations where it was not getting converted (VVing bit fields and I guess viewing tickets). Past that just cleanup generally in this file... # Explain why it's good for the game Working browsers that open? # Testing Photographs and Procedure <details> <summary>Screenshots & Videos</summary> ![image](https://github.com/user-attachments/assets/be43bd03-b8ae-4a9e-afe4-13278188f7a9) </details> # Changelog :cl: Drathek fix: Fixed browsers sometimes not handling a mob passed instead of a client (VV bitfields & tickets atleast) /:cl:
1 parent b70e4c5 commit 18e0387

File tree

1 file changed

+97
-92
lines changed

1 file changed

+97
-92
lines changed

code/datums/browser.dm

+97-92
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,25 @@
2323
var/existing_browser = FALSE
2424

2525

26-
/datum/browser/New(nuser, nwindow_id, ntitle = 0, nstylesheet = "common.css", nwidth = 0, nheight = 0, atom/nref = null)
26+
/datum/browser/New(client/nuser, nwindow_id, ntitle = 0, nstylesheet = "common.css", nwidth = 0, nheight = 0, atom/nref = null)
27+
if(!nuser)
28+
return
29+
if(!istype(nuser))
30+
if(ismob(nuser))
31+
var/mob/mob_user = nuser
32+
nuser = mob_user.client
33+
else
34+
CRASH("Passed [nuser] ([nuser?.type]) as a client!")
35+
2736
user = nuser
2837
window_id = nwindow_id
29-
if (ntitle)
38+
if(ntitle)
3039
title = format_text(ntitle)
31-
if (nwidth)
40+
if(nwidth)
3241
width = nwidth
33-
if (nheight)
42+
if(nheight)
3443
height = nheight
35-
if (nref)
44+
if(nref)
3645
ref = nref
3746
stylesheet = nstylesheet
3847

@@ -49,15 +58,15 @@
4958
window_options = nwindow_options
5059

5160
/datum/browser/proc/add_stylesheet(name, file)
52-
if (istype(name, /datum/asset/spritesheet))
61+
if(istype(name, /datum/asset/spritesheet))
5362
var/datum/asset/spritesheet/sheet = name
5463
stylesheets["spritesheet_[sheet.name].css"] = "data/spritesheets/[sheet.name]"
5564
else
5665
var/asset_name = "[name].css"
5766

5867
stylesheets[asset_name] = file
5968

60-
if (!SSassets.cache[asset_name])
69+
if(!SSassets.cache[asset_name])
6170
SSassets.transport.register_asset(asset_name, file)
6271

6372
/datum/browser/proc/add_script(name, file)
@@ -83,16 +92,16 @@
8392
</style>
8493
"}
8594

86-
for (var/file in stylesheets)
95+
for(var/file in stylesheets)
8796
head_content += "<link rel='stylesheet' type='text/css' href='[SSassets.transport.get_asset_url(file)]'>"
8897

8998

90-
for (var/file in scripts)
99+
for(var/file in scripts)
91100
head_content += "<script type='text/javascript' src='[SSassets.transport.get_asset_url(file)]'></script>"
92101
head_content += "<script type='text/javascript' src='[other_asset.get_url_mappings()["search.js"]]'></script>"
93102

94103
var/title_attributes = "class='uiTitle'"
95-
if (title_image)
104+
if(title_image)
96105
title_attributes = "class='uiTitle icon' style='background-image: url([title_image]);'"
97106

98107
return {"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
@@ -126,30 +135,30 @@
126135
to_chat(user, SPAN_USERDANGER("The [title] browser you tried to open failed a sanity check! Please report this on github!"))
127136
return
128137
var/window_size = ""
129-
if (width && height)
138+
if(width && height)
130139
if(user?.window_scaling && user.prefs.window_scale)
131140
window_size = "size=[width * user.window_scaling]x[height * user.window_scaling];"
132141
else
133142
window_size = "size=[width]x[height];"
134143
common_asset.send(user)
135144
other_asset.send(user)
136-
if (length(stylesheets))
145+
if(length(stylesheets))
137146
SSassets.transport.send_assets(user, stylesheets)
138-
if (length(scripts))
147+
if(length(scripts))
139148
SSassets.transport.send_assets(user, scripts)
140149

141150
user << browse(get_content(), "window=[window_id];[window_size][window_options]")
142151

143152
if(existing_browser)
144153
winset(user, existing_browser, window_size)
145154

146-
if (use_onclose)
155+
if(use_onclose)
147156
setup_onclose()
148157

149158
/datum/browser/proc/setup_onclose()
150159
set waitfor = 0 //winexists sleeps, so we don't need to.
151-
for (var/i in 1 to 10)
152-
if (user && winexists(user, window_id))
160+
for(var/i in 1 to 10)
161+
if(user && winexists(user, window_id))
153162
onclose(user, window_id, ref)
154163
break
155164

@@ -163,7 +172,7 @@
163172
/mob/proc/browse_rsc_icon(icon, icon_state, dir = -1)
164173
/*
165174
var/icon/I
166-
if (dir >= 0)
175+
if(dir >= 0)
167176
I = new /icon(icon, icon_state, dir)
168177
else
169178
I = new /icon(icon, icon_state)
@@ -190,28 +199,28 @@
190199
// Topic() proc for special handling.
191200
// Otherwise, the user mob's machine var will be reset directly.
192201
//
193-
/proc/onclose(user, windowid, atom/ref, list/params)
194-
var/client/C = user
195-
196-
if (ismob(user))
197-
var/mob/M = user
198-
C = M.client
199-
200-
if (!istype(C))
202+
/proc/onclose(client/user, windowid, atom/ref, list/params)
203+
if(!user)
201204
return
205+
if(!istype(user))
206+
if(ismob(user))
207+
var/mob/mob_user = user
208+
user = mob_user.client
209+
else
210+
CRASH("Passed [user] ([user?.type]) as a client!")
202211

203212
var/ref_string = "null"
204-
if (ref)
213+
if(ref)
205214
ref_string = "\ref[ref]"
206215

207216
var/params_string = "null"
208-
if (params)
217+
if(params)
209218
params_string = ""
210-
for (var/param in params)
219+
for(var/param in params)
211220
params_string += "[param]=[params[param]];"
212221
params_string = copytext(params_string, 1, -1)
213222

214-
winset(C, windowid, "on-close=\".windowclose \\\"[ref_string]\\\" \\\"[params_string]\\\"\"")
223+
winset(user, windowid, "on-close=\".windowclose \\\"[ref_string]\\\" \\\"[params_string]\\\"\"")
215224

216225

217226
// the on-close client verb
@@ -231,7 +240,7 @@
231240
var/param_string = "close=1"
232241
var/list/param_list = list("close"="1")
233242

234-
if (params && params != "null")
243+
if(params && params != "null")
235244
param_string = params
236245
param_list = params2list(params)
237246

@@ -245,28 +254,28 @@
245254
mob.unset_interaction()
246255
return
247256

248-
/proc/show_browser(target, browser_content, browser_name, id = null, window_options = null, closeref, width, height, existing_container = FALSE)
249-
var/client/C = target
250-
251-
if (ismob(target))
252-
var/mob/M = target
253-
C = M.client
254-
255-
if (!istype(C))
257+
/proc/show_browser(client/target, browser_content, browser_name, id = null, window_options = null, closeref, width, height, existing_container = FALSE)
258+
if(!target)
256259
return
260+
if(!istype(target))
261+
if(ismob(target))
262+
var/mob/mob_user = target
263+
target = mob_user.client
264+
else
265+
CRASH("Passed [target] ([target?.type]) as a client!")
257266

258-
var/stylesheet = C.prefs.stylesheet
259-
if (!(stylesheet in GLOB.stylesheets))
260-
C.prefs.stylesheet = "Modern"
267+
var/stylesheet = target.prefs.stylesheet
268+
if(!(stylesheet in GLOB.stylesheets))
269+
target.prefs.stylesheet = "Modern"
261270
stylesheet = "Modern"
262271

263-
var/datum/browser/popup = new(C, id ? id : browser_name, browser_name, GLOB.stylesheets[stylesheet], nwidth = width, nheight = height, nref = closeref)
272+
var/datum/browser/popup = new(target, id ? id : browser_name, browser_name, GLOB.stylesheets[stylesheet], nwidth = width, nheight = height, nref = closeref)
264273

265274
if(existing_container)
266275
popup.existing_browser = existing_container
267276

268277
popup.set_content(browser_content)
269-
if (window_options)
278+
if(window_options)
270279
popup.set_window_options(window_options)
271280
popup.open()
272281

@@ -276,12 +285,12 @@
276285
var/selectedbutton = 0
277286
var/stealfocus
278287

279-
/datum/browser/modal/New(nuser, nwindow_id, ntitle = 0, nstylesheet = "common.css", nwidth = 0, nheight = 0, atom/nref = null, StealFocus = 1, Timeout = 6000)
288+
/datum/browser/modal/New(client/nuser, nwindow_id, ntitle = 0, nstylesheet = "common.css", nwidth = 0, nheight = 0, atom/nref = null, stealfocus = 1, timeout = 6000)
280289
..()
281-
stealfocus = StealFocus
282-
if (!StealFocus)
290+
src.stealfocus = stealfocus
291+
if(!stealfocus)
283292
window_options += "focus=false;"
284-
timeout = Timeout
293+
src.timeout = timeout
285294

286295

287296
/datum/browser/modal/close()
@@ -292,106 +301,102 @@
292301
set waitfor = FALSE
293302
opentime = world.time
294303

295-
if (stealfocus)
304+
if(stealfocus)
296305
. = ..(use_onclose = 1)
297306
else
298307
var/focusedwindow = winget(user, null, "focus")
299308
. = ..(use_onclose = 1)
300309

301310
//waits for the window to show up client side before attempting to un-focus it
302311
//winexists sleeps until it gets a reply from the client, so we don't need to bother sleeping
303-
for (var/i in 1 to 10)
304-
if (user && winexists(user, window_id))
305-
if (focusedwindow)
312+
for(var/i in 1 to 10)
313+
if(user && winexists(user, window_id))
314+
if(focusedwindow)
306315
winset(user, focusedwindow, "focus=true")
307316
else
308317
winset(user, "mapwindow", "focus=true")
309318
break
310-
if (timeout)
319+
if(timeout)
311320
addtimer(CALLBACK(src, PROC_REF(close)), timeout)
312321

313322
/datum/browser/modal/proc/wait()
314-
while (opentime && selectedbutton <= 0 && (!timeout || opentime+timeout > world.time))
323+
while(opentime && selectedbutton <= 0 && (!timeout || opentime+timeout > world.time))
315324
stoplag(1)
325+
316326
/datum/browser/modal/listpicker
317327
var/valueslist = list()
318328

319-
/datum/browser/modal/listpicker/New(User,Message,Title,Button1="Ok",Button2,Button3,StealFocus = 1, Timeout = FALSE,list/values,inputtype="checkbox", width, height)
320-
if (!User)
329+
/datum/browser/modal/listpicker/New(client/user, message, title, button1="Ok", button2, button3, stealfocus=TRUE, timeout=FALSE, list/values, inputtype="checkbox", width, height)
330+
if(!user)
321331
return
322332

323333
var/output = {"<form><input type="hidden" name="src" value="[REF(src)]"><ul class="sparse">"}
324-
if (inputtype == "checkbox" || inputtype == "radio")
334+
if(inputtype == "checkbox" || inputtype == "radio")
325335
output += {"<table border=1 cellspacing=0 cellpadding=3 style='border: 1px solid black;'>"}
326-
for (var/i in values)
336+
for(var/i in values)
327337
output += {"<tr>
328338
<td><input type="[inputtype]" value="1" name="[i["name"]]"[i["checked"] ? " checked" : ""][i["allowed_edit"] ? "" : " onclick='return false' onkeydown='return false'"]></td>
329339
<td>[i["name"]]</td>
330340
</tr>"}
331341
output += {"</table>"}
332342
else
333-
for (var/i in values)
343+
for(var/i in values)
334344
output += {"<li><input id="name="[i["name"]]"" style="width: 50px" type="[type]" name="[i["name"]]" value="[i["value"]]">
335345
<label for="[i["name"]]">[i["name"]]</label></li>"}
336346
output += {"</ul><div style="text-align:center">
337-
<button type="submit" name="button" value="[Button1]" style="font-size:large;float:[( Button2 ? "left" : "right" )]">[Button1]</button>"}
347+
<button type="submit" name="button" value="[button1]" style="font-size:large;float:[( button2 ? "left" : "right" )]">[button1]</button>"}
338348

339-
if (Button2)
340-
output += {"<button type="submit" name="button" value="[Button2]" style="font-size:large;[( Button3 ? "" : "float:right" )]">[Button2]</button>"}
349+
if(button2)
350+
output += {"<button type="submit" name="button" value="[button2]" style="font-size:large;[( button3 ? "" : "float:right" )]">[button2]</button>"}
341351

342-
if (Button3)
343-
output += {"<button type="submit" name="button" value="[Button3]" style="font-size:large;float:right">[Button3]</button>"}
352+
if(button3)
353+
output += {"<button type="submit" name="button" value="[button3]" style="font-size:large;float:right">[button3]</button>"}
344354

345355
output += {"</form></div>"}
346-
..(User, ckey("[User]-[Message]-[Title]-[world.time]-[rand(1,10000)]"), Title, "common.css", width, height, src, StealFocus, Timeout)
356+
..(user, ckey("[user]-[message]-[title]-[world.time]-[rand(1,10000)]"), title, "common.css", width, height, src, stealfocus, timeout)
347357
set_content(output)
348358

349-
/datum/browser/modal/listpicker/Topic(href,href_list)
350-
if (href_list["close"] || !user)
359+
/datum/browser/modal/listpicker/Topic(href, href_list)
360+
if(href_list["close"] || !user)
351361
opentime = 0
352362
return
353-
if (href_list["button"])
363+
if(href_list["button"])
354364
selectedbutton = href_list["button"]
355-
for (var/item in href_list)
365+
for(var/item in href_list)
356366
switch(item)
357-
if ("close", "button", "src")
367+
if("close", "button", "src")
358368
continue
359369
else
360370
valueslist[item] = href_list[item]
361371
opentime = 0
362372
close()
363373

364-
/proc/presentpicker(mob/User,Message, Title, Button1="Ok", Button2, Button3, StealFocus = 1,Timeout = 6000,list/values, inputtype = "checkbox", width, height)
365-
if (!istype(User))
366-
if (istype(User, /client/))
367-
var/client/C = User
368-
User = C.mob
369-
else
370-
return
371-
var/datum/browser/modal/listpicker/A = new(User, Message, Title, Button1, Button2, Button3, StealFocus,Timeout, values, inputtype, width, height)
372-
A.open()
373-
A.wait()
374-
if (A.selectedbutton)
375-
return list("button" = A.selectedbutton, "values" = A.valueslist)
376-
377-
/proc/input_bitfield(mob/User, title, bitfield, current_value, nwidth = 350, nheight = 350, allowed_edit_list = null)
378-
if (!User || !(bitfield in GLOB.bitfields))
374+
/proc/presentpicker(mob/user, message, title, button1="Ok", button2, button3, steal_focus=TRUE, timeout=6000, list/values, input_type="checkbox", width, height)
375+
// We actually want to pass user as a client, but it already handles mob vs client
376+
var/datum/browser/modal/listpicker/picker = new(user, message, title, button1, button2, button3, steal_focus, timeout, values, input_type, width, height)
377+
picker.open()
378+
picker.wait()
379+
if(picker.selectedbutton)
380+
return list("button" = picker.selectedbutton, "values" = picker.valueslist)
381+
382+
/proc/input_bitfield(mob/user, title, bitfield, current_value, nwidth = 350, nheight = 350, allowed_edit_list = null)
383+
if(!user || !(bitfield in GLOB.bitfields))
379384
return
380385
var/list/pickerlist = list()
381-
for (var/i in GLOB.bitfields[bitfield])
382-
var/can_edit = 1
386+
for(var/i in GLOB.bitfields[bitfield])
387+
var/can_edit = TRUE
383388
if(!isnull(allowed_edit_list) && !(allowed_edit_list & GLOB.bitfields[bitfield][i]))
384-
can_edit = 0
385-
if (current_value & GLOB.bitfields[bitfield][i])
389+
can_edit = FALSE
390+
if(current_value & GLOB.bitfields[bitfield][i])
386391
pickerlist += list(list("checked" = 1, "value" = GLOB.bitfields[bitfield][i], "name" = i, "allowed_edit" = can_edit))
387392
else
388393
pickerlist += list(list("checked" = 0, "value" = GLOB.bitfields[bitfield][i], "name" = i, "allowed_edit" = can_edit))
389-
var/list/result = presentpicker(User, "", title, Button1="Save", Button2 = "Cancel", Timeout=FALSE, values = pickerlist, width = nwidth, height = nheight)
390-
if (islist(result))
391-
if (result["button"] != "Save") // If the user pressed the cancel button
394+
var/list/result = presentpicker(user, "", title, button1="Save", button2 = "Cancel", timeout=FALSE, values=pickerlist, width=nwidth, height=nheight)
395+
if(islist(result))
396+
if(result["button"] != "Save") // If the user pressed the cancel button
392397
return
393398
. = 0
394-
for (var/flag in result["values"])
399+
for(var/flag in result["values"])
395400
. |= GLOB.bitfields[bitfield][flag]
396401
else
397402
return

0 commit comments

Comments
 (0)