Skip to content

Commit

Permalink
[MIRROR] New Processing Unit UI (#608)
Browse files Browse the repository at this point in the history
* New Processing Unit UI (#81029)

Made a new TGUI for Ore Processing Unit, removed all `updateUsrDialog`
calls.

* New Processing Unit UI

---------

Co-authored-by: Interception&? <[email protected]>
  • Loading branch information
2 people authored and FFMirrorBot committed Jan 25, 2024
1 parent c18c4c3 commit f437915
Show file tree
Hide file tree
Showing 2 changed files with 243 additions and 64 deletions.
138 changes: 74 additions & 64 deletions code/modules/mining/machine_processing.dm
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
icon = 'icons/obj/machines/mining_machines.dmi'
icon_state = "console"
density = TRUE
interaction_flags_machine = INTERACT_MACHINE_WIRES_IF_OPEN|INTERACT_MACHINE_ALLOW_SILICON|INTERACT_MACHINE_OPEN_SILICON|INTERACT_MACHINE_SET_MACHINE
interaction_flags_machine = INTERACT_MACHINE_WIRES_IF_OPEN|INTERACT_MACHINE_ALLOW_SILICON|INTERACT_MACHINE_OPEN_SILICON
/// Connected ore processing machine.
var/obj/machinery/mineral/processing_unit/processing_machine

Expand All @@ -82,39 +82,43 @@
else
return INITIALIZE_HINT_QDEL

/obj/machinery/mineral/processing_unit_console/ui_interact(mob/user)
. = ..()
if(!processing_machine)
return
/obj/machinery/mineral/processing_unit_console/ui_interact(mob/user, datum/tgui/ui)
ui = SStgui.try_update_ui(user, src, ui)
if(!ui)
ui = new(user, src, "ProcessingConsole")
ui.open()

var/dat = processing_machine.get_machine_data()
/obj/machinery/mineral/processing_unit_console/ui_static_data(mob/user)
return processing_machine.ui_static_data()

var/datum/browser/popup = new(user, "processing", "Smelting Console", 300, 500)
popup.set_content(dat)
popup.open()
/obj/machinery/mineral/processing_unit_console/ui_data(mob/user)
return processing_machine.ui_data()

/obj/machinery/mineral/processing_unit_console/Topic(href, href_list)
if(..())
/obj/machinery/mineral/processing_unit_console/ui_act(action, list/params)
. = ..()
if(.)
return
usr.set_machine(src)
add_fingerprint(usr)

if(href_list["material"])
var/datum/material/new_material = locate(href_list["material"])
if(istype(new_material))
switch(action)
if("setMaterial")
var/datum/material/new_material = locate(params["value"])
if(!istype(new_material))
return

processing_machine.selected_material = new_material
processing_machine.selected_alloy = null
return TRUE

if(href_list["alloy"])
processing_machine.selected_material = null
processing_machine.selected_alloy = href_list["alloy"]
if("setAlloy")
processing_machine.selected_material = null
processing_machine.selected_alloy = params["value"]
return TRUE

if(href_list["set_on"])
processing_machine.on = (href_list["set_on"] == "on")
processing_machine.begin_processing()

updateUsrDialog()
return
if("toggle")
processing_machine.on = !processing_machine.on
if(processing_machine.on)
processing_machine.begin_processing()
return TRUE

/obj/machinery/mineral/processing_unit_console/Destroy()
processing_machine = null
Expand Down Expand Up @@ -181,41 +185,53 @@
unload_mineral(O)
else
materials.insert_item(O)
if(mineral_machine)
mineral_machine.updateUsrDialog()

/obj/machinery/mineral/processing_unit/proc/get_machine_data()
var/dat = "<b>Smelter control console</b><br><br>"
for(var/datum/material/all_materials as anything in materials.materials)
var/amount = materials.materials[all_materials]
dat += "<span class=\"res_name\">[all_materials.name]: </span>[amount] cm&sup3;"
if (selected_material == all_materials)
dat += " <i>Smelting</i>"
else
dat += " <A href='?src=[REF(mineral_machine)];material=[REF(all_materials)]'><b>Not Smelting</b></A> "
dat += "<br>"

dat += "<br><br>"
dat += "<b>Smelt Alloys</b><br>"

/obj/machinery/mineral/processing_unit/ui_static_data()
var/list/data = list()

for(var/datum/material/material as anything in materials.materials)
var/obj/display = initial(material.sheet_type)
data["materialIcons"] += list(
list(
"id" = REF(material),
"icon" = icon2base64(icon(initial(display.icon), icon_state = initial(display.icon_state), frame = 1)),
)
)

for(var/research in stored_research.researched_designs)
var/datum/design/designs = SSresearch.techweb_design_by_id(research)
dat += "<span class=\"res_name\">[designs.name] "
if (selected_alloy == designs.id)
dat += " <i>Smelting</i>"
else
dat += " <A href='?src=[REF(mineral_machine)];alloy=[designs.id]'><b>Not Smelting</b></A> "
dat += "<br>"

dat += "<br><br>"
//On or off
dat += "Machine is currently "
if (on)
dat += "<A href='?src=[REF(mineral_machine)];set_on=off'>On</A> "
else
dat += "<A href='?src=[REF(mineral_machine)];set_on=on'>Off</A> "
var/datum/design/design = SSresearch.techweb_design_by_id(research)
var/obj/display = initial(design.build_path)
data["alloyIcons"] += list(
list(
"id" = design.id,
"icon" = icon2base64(icon(initial(display.icon), icon_state = initial(display.icon_state), frame = 1)),
)
)

return dat
data += materials.ui_static_data()

return data

/obj/machinery/mineral/processing_unit/ui_data()
var/list/data = list()

data["materials"] = materials.ui_data()
data["selectedMaterial"] = selected_material?.name

data["alloys"] = list()
for(var/research in stored_research.researched_designs)
var/datum/design/design = SSresearch.techweb_design_by_id(research)
data["alloys"] += list(
list(
"name" = design.name,
"id" = design.id,
)
)
data["selectedAlloy"] = selected_alloy

data["state"] = on

return data

/obj/machinery/mineral/processing_unit/pickup_item(datum/source, atom/movable/target, direction)
if(QDELETED(target))
Expand All @@ -225,19 +241,13 @@

/obj/machinery/mineral/processing_unit/process(seconds_per_tick)
if(!on)
end_processing()
if(mineral_machine)
mineral_machine.updateUsrDialog()
return
return PROCESS_KILL

if(selected_material)
smelt_ore(seconds_per_tick)
else if(selected_alloy)
smelt_alloy(seconds_per_tick)

if(mineral_machine)
mineral_machine.updateUsrDialog()

/obj/machinery/mineral/processing_unit/proc/smelt_ore(seconds_per_tick = 2)
var/datum/material/mat = selected_material
if(!mat)
Expand Down
169 changes: 169 additions & 0 deletions tgui/packages/tgui/interfaces/ProcessingConsole.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import { toTitleCase } from 'common/string';

import { useBackend } from '../backend';
import {
Box,
Button,
Icon,
Image,
NoticeBox,
Section,
Stack,
Table,
} from '../components';
import { formatSiUnit } from '../format';
import { Window } from '../layouts';
import { Material } from './Fabrication/Types';

type IconData = {
id: string;
icon: string;
};

type Alloy = {
name: string;
id: string;
};

type Data = {
materials: Material[];
materialIcons: IconData[];
selectedMaterial: string | null;
alloys: Alloy[];
alloyIcons: IconData[];
selectedAlloy: string | null;
state: boolean;
SHEET_MATERIAL_AMOUNT: number;
};

export const ProcessingConsole = (props: any) => {
const { act, data } = useBackend<Data>();
const { state } = data;

return (
<Window title="Processing Unit Console" width={580} height={500}>
<Window.Content>
<Stack fill vertical>
<Stack.Item grow basis={0}>
<Stack fill>
<Stack.Item grow={1.2} basis={0}>
<Section fill textAlign="center" title="Materials">
<MaterialSelection />
</Section>
</Stack.Item>
<Stack.Item grow basis={0}>
<Section fill title="Alloys" textAlign="center">
<AlloySelection />
</Section>
</Stack.Item>
</Stack>
</Stack.Item>
<Stack.Item>
<Button
icon="arrows-spin"
iconSpin={state}
color={state ? 'bad' : 'good'}
textAlign="center"
fontSize={1.25}
py={1}
fluid
bold
onClick={() => act('toggle')}
>
{state ? 'Deactivate' : 'Activate'}
</Button>
</Stack.Item>
</Stack>
</Window.Content>
</Window>
);
};

const MaterialSelection = (props: any) => {
const { act, data } = useBackend<Data>();
const { materials, materialIcons, selectedMaterial, SHEET_MATERIAL_AMOUNT } =
data;

return materials.length > 0 ? (
<Table>
{materials.map((material) => (
<DisplayRow
key={material.name}
name={material.name}
icon={materialIcons.find((icon) => icon.id === material.ref)?.icon}
amount={Math.floor(material.amount / SHEET_MATERIAL_AMOUNT)}
selected={selectedMaterial === material.name}
onSelect={() => act('setMaterial', { value: material.ref })}
/>
))}
</Table>
) : (
<NoticeBox danger>No material recipes found!</NoticeBox>
);
};

const AlloySelection = (props: any) => {
const { act, data } = useBackend<Data>();
const { alloys, alloyIcons, selectedAlloy } = data;

return alloys.length > 0 ? (
<Table>
{alloys.map((alloy) => (
<DisplayRow
key={alloy.id}
name={alloy.name}
icon={alloyIcons.find((icon) => icon.id === alloy.id)?.icon}
selected={selectedAlloy === alloy.id}
onSelect={() => act('setAlloy', { value: alloy.id })}
/>
))}
</Table>
) : (
<NoticeBox danger>No alloy recipes found!</NoticeBox>
);
};

type DisplayRowProps = {
name: string;
icon?: string;
amount?: number;
selected: boolean;
onSelect: () => void;
};

const DisplayRow = (props: DisplayRowProps) => {
const { name, icon, amount, selected, onSelect } = props;

return (
<Table.Row collapsing className="candystripe">
<Table.Cell collapsing pl={1}>
{icon ? (
<Image
m={1}
width="24px"
height="24px"
verticalAlign="middle"
src={`data:image/jpeg;base64,${icon}`}
/>
) : (
<Icon name="circle-question" verticalAlign="middle" />
)}
</Table.Cell>
<Table.Cell collapsing textAlign="left">
{toTitleCase(name)}
</Table.Cell>
{amount !== undefined ? (
<Box color="label">
{`${formatSiUnit(amount, 0)} ${amount === 1 ? 'sheet' : 'sheets'}`}
</Box>
) : null}
<Table.Cell collapsing pr={1} textAlign="right">
<Button.Checkbox
color="transparent"
checked={selected}
onClick={() => onSelect()}
/>
</Table.Cell>
</Table.Row>
);
};

0 comments on commit f437915

Please sign in to comment.