Skip to content

Commit f1f9571

Browse files
authored
Support Custom Gauges in HUD Config UI (#6670)
* Support coloring custom gauges * remove NULL
1 parent 42dd1eb commit f1f9571

10 files changed

Lines changed: 450 additions & 173 deletions

File tree

code/hud/hud.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,18 @@ render_for_cockpit_toggle(0), custom_gauge(true), textoffset_x(txtoffset_x), tex
385385

386386
const HC_gauge_mappings& gauge_map = HC_gauge_mappings::get_instance();
387387

388-
gauge_config_id = ""; // gauge_map.get_string_id_from_numeric_id(_gauge_config);
388+
gauge_config_id = create_custom_gauge_id(_custom_name);
389389
can_popup = hud_config_can_popup(gauge_map.get_numeric_id_from_string_id(gauge_config_id));
390390
use_iff_color = hud_config_use_iff_color(gauge_map.get_numeric_id_from_string_id(gauge_config_id));
391391
use_tag_color = hud_config_use_tag_color(gauge_map.get_numeric_id_from_string_id(gauge_config_id));
392392

393+
//Set the gauge type for color backup lookup
394+
HUD_config.set_gauge_type(gauge_config_id, gauge_type);
395+
396+
// Custom gauges are always default visible and popup false for now
397+
HUD_config.set_gauge_visibility(gauge_config_id, true);
398+
HUD_config.set_gauge_popup(gauge_config_id, false);
399+
393400
popup_timer = timestamp(1);
394401

395402
texture_target_fname[0] = '\0';
@@ -583,15 +590,9 @@ void HudGauge::setGaugeColor(int bright_index, bool config)
583590

584591
if (config) {
585592
color use_color;
586-
if (custom_gauge) {
587-
// Custom gauges currently use the color based on their gauge type
588-
const HC_gauge_mappings& gauge_map = HC_gauge_mappings::get_instance();
589-
use_color = HUD_config.get_gauge_color(gauge_map.get_string_id_from_numeric_id(gauge_type));
590-
} else {
591-
use_color = HUD_config.get_gauge_color(gauge_config_id);
592-
}
593+
use_color = HUD_config.get_gauge_color(gauge_config_id);
593594

594-
if (!custom_gauge && HUD_config.is_gauge_visible(gauge_config_id)) {
595+
if (HUD_config.is_gauge_visible(gauge_config_id)) {
595596
if (use_iff_color) {
596597
// Ditto for target tagging color
597598
if (use_tag_color) {
@@ -610,11 +611,6 @@ void HudGauge::setGaugeColor(int bright_index, bool config)
610611
}
611612
gr_init_alphacolor(&use_color, use_color.red, use_color.green, use_color.blue, alpha);
612613
gr_set_color_fast(&use_color);
613-
} else if (custom_gauge) {
614-
// Custom gauges currently use the current color but alpha dimmed since they are not selectable
615-
alpha = MAX(use_color.alpha - 50, 25);
616-
gr_init_alphacolor(&use_color, use_color.red, use_color.green, use_color.blue, alpha);
617-
gr_set_color_fast(&use_color);
618614
} else {
619615
// if its off, make it dark gray
620616
gr_init_alphacolor(&use_color, 127, 127, 127, 64);
@@ -881,10 +877,10 @@ void HudGauge::render(float /*frametime*/, bool config)
881877

882878
if (config) {
883879
std::tie(x, y, scale) = hud_config_convert_coord_sys(position[0], position[1], base_w, base_h);
884-
// The following code will be used to allow custom coloring of custom HUD gauges. This will be implemented in the future.
885-
/*int bmw, bmh;
880+
// Commenting out this code below will disable custom gauges from being selectable and individually colorable
881+
int bmw, bmh;
886882
bm_get_info(custom_frame.first_frame, &bmw, &bmh);
887-
hud_config_set_mouse_coords(gauge_config_id, x, x + static_cast<int>(bmw * scale), y, y + static_cast<int>(bmh * scale));*/
883+
hud_config_set_mouse_coords(gauge_config_id, x, x + static_cast<int>(bmw * scale), y, y + static_cast<int>(bmh * scale));
888884
}
889885

890886
setGaugeColor(HUD_C_NONE, config);

code/hud/hudconfig.cpp

Lines changed: 78 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -937,31 +937,37 @@ void hud_config_record_color(int in_color)
937937
HUD_color_blue = HC_colors[in_color].b;
938938
}
939939

940-
// Set the HUD color
940+
// Set the HUD color to one of the color presets
941941
void hud_config_set_color(int in_color)
942942
{
943943
hud_config_record_color(in_color);
944944

945945
HUD_init_hud_color_array();
946946

947-
// apply the color to all gauges
948-
for(int idx=0; idx<NUM_HUD_GAUGES; idx++){
949-
color clr;
950-
gr_init_alphacolor(&clr, HC_colors[in_color].r, HC_colors[in_color].g, HC_colors[in_color].b, (HUD_color_alpha+1)*16);
951-
SCP_string gauge = gauge_map.get_string_id_from_numeric_id(idx);
952-
HUD_config.set_gauge_color(gauge, clr);
953-
}
947+
color clr;
948+
gr_init_alphacolor(&clr, HC_colors[in_color].r, HC_colors[in_color].g, HC_colors[in_color].b, (HUD_color_alpha+1)*16);
949+
950+
// apply the color to all built-in gauges gauges
951+
for(const auto& gauge_pair : HC_gauge_map){
952+
const SCP_string& gauge_id = gauge_pair.first;
953+
if (!gauge_id.empty()) {
954+
HUD_config.set_gauge_color(gauge_id, clr);
955+
}
956+
}
954957
}
955958

959+
// Set the HUD color when ALL GAUGES is selected in the HUD Config UI
956960
void hud_config_stuff_colors(int r, int g, int b)
957961
{
958962
color clr;
959963
gr_init_alphacolor(&clr, r, g, b, 255);
960964

961965
// apply the color to all gauges
962-
for(int idx=0; idx<NUM_HUD_GAUGES; idx++){
963-
SCP_string gauge = gauge_map.get_string_id_from_numeric_id(idx);
964-
HUD_config.set_gauge_color(gauge, clr);
966+
for (const auto& gauge_pair : HC_gauge_map) {
967+
const SCP_string& gauge_id = gauge_pair.first;
968+
if (!gauge_id.empty()) {
969+
HUD_config.set_gauge_color(gauge_id, clr);
970+
}
965971
}
966972
}
967973

@@ -1338,7 +1344,10 @@ void hud_config_button_enable(int index)
13381344
// determine if on/off/popup buttons should be shown
13391345
void hud_config_set_button_state()
13401346
{
1341-
if (HC_gauge_selected.empty()) {
1347+
const auto gauge = hud_config_get_gauge_pointer(HC_gauge_selected);
1348+
1349+
// Custom gauges cannot currently be set to popup or toggle visibility
1350+
if (HC_gauge_selected.empty() || gauge->isCustom()) {
13421351
hud_config_button_disable(HCB_ON);
13431352
hud_config_button_disable(HCB_OFF);
13441353
hud_config_button_disable(HCB_POPUP);
@@ -1349,8 +1358,6 @@ void hud_config_set_button_state()
13491358
hud_config_button_enable(HCB_ON);
13501359
hud_config_button_enable(HCB_OFF);
13511360

1352-
const auto gauge = hud_config_get_gauge_pointer(HC_gauge_selected);
1353-
13541361
// popup is maybe available
13551362
if (gauge != nullptr && gauge->getConfigCanPopup()) {
13561363
hud_config_button_enable(HCB_POPUP);
@@ -1553,8 +1560,6 @@ void hud_config_close(bool API_Access)
15531560
// hud_set_default_hud_config() will set the hud configuration to default values
15541561
void hud_set_default_hud_config(player * /*p*/, const SCP_string& filename)
15551562
{
1556-
int idx;
1557-
15581563
HUD_color_alpha = HUD_COLOR_ALPHA_DEFAULT;
15591564
HUD_config.main_color = HC_default_color;
15601565
HUD_color_red = HC_colors[HUD_config.main_color].r;
@@ -1564,20 +1569,31 @@ void hud_set_default_hud_config(player * /*p*/, const SCP_string& filename)
15641569
color clr;
15651570
gr_init_alphacolor(&clr, HUD_color_red, HUD_color_green, HUD_color_blue, (HUD_color_alpha + 1) * 16);
15661571

1567-
for(idx=0; idx<NUM_HUD_GAUGES; idx++){
1568-
SCP_string gauge = gauge_map.get_string_id_from_numeric_id(idx);
1569-
HUD_config.set_gauge_color(gauge, clr);
1572+
for (const auto& gauge_pair : HC_gauge_map) {
1573+
const SCP_string& gauge_id = gauge_pair.first;
1574+
if (!gauge_id.empty()) {
1575+
HUD_config.set_gauge_color(gauge_id, clr);
1576+
}
15701577
}
15711578

15721579
HUD_config.show_flags_map.clear();
15731580
HUD_config.popup_flags_map.clear();
15741581

1582+
// Built-in have specific settings
15751583
for (const auto& gauge_id : default_visible_gauges) {
15761584
HUD_config.show_flags_map[gauge_id] = true;
15771585
}
15781586

1587+
// Custom gauges are always visible by default
1588+
for (const auto& gauge_pair : HC_gauge_map) {
1589+
if (gauge_pair.second->isCustom()) {
1590+
const SCP_string& gauge_id = gauge_pair.first;
1591+
HUD_config.show_flags_map[gauge_id] = true;
1592+
}
1593+
}
1594+
15791595
HUD_config.rp_dist = RR_INFINITY;
1580-
HUD_config.is_observer = 0;
1596+
HUD_config.is_observer = false;
15811597

15821598
// load up the default colors
15831599
hud_config_color_load(filename.c_str());
@@ -1801,23 +1817,22 @@ void hud_config_recalc_alpha_slider()
18011817

18021818
void hud_config_red_slider()
18031819
{
1804-
int idx;
18051820
int pos = HCS_CONV(HC_color_sliders[HCS_RED].get_currentItem()) ;
18061821

1807-
// select all ?
18081822
if(HC_select_all){
1809-
for(idx=0; idx<NUM_HUD_GAUGES; idx++){
1810-
SCP_string gauge = gauge_map.get_string_id_from_numeric_id(idx);
1811-
gr_init_alphacolor(&HUD_config.gauge_colors[gauge], pos, HUD_config.gauge_colors[gauge].green, HUD_config.gauge_colors[gauge].blue, HUD_config.gauge_colors[gauge].alpha);
1812-
}
1823+
for(const auto& gauge_pair : HC_gauge_map){
1824+
const SCP_string& gauge_id = gauge_pair.first;
1825+
if (!gauge_id.empty()) {
1826+
gr_init_alphacolor(&HUD_config.gauge_colors[gauge_id], pos, HUD_config.gauge_colors[gauge_id].green, HUD_config.gauge_colors[gauge_id].blue, HUD_config.gauge_colors[gauge_id].alpha);
1827+
}
1828+
}
18131829
}
18141830
// individual gauge
18151831
else {
18161832
if(HC_gauge_selected.empty()){
18171833
return;
18181834
}
18191835

1820-
// get slider position
18211836
gr_init_alphacolor(&HUD_config.gauge_colors[HC_gauge_selected], pos, HUD_config.gauge_colors[HC_gauge_selected].green, HUD_config.gauge_colors[HC_gauge_selected].blue, HUD_config.gauge_colors[HC_gauge_selected].alpha);
18221837
}
18231838

@@ -1826,23 +1841,22 @@ void hud_config_red_slider()
18261841

18271842
void hud_config_green_slider()
18281843
{
1829-
int idx;
18301844
int pos = HCS_CONV(HC_color_sliders[HCS_GREEN].get_currentItem()) ;
18311845

1832-
// select all ?
18331846
if(HC_select_all){
1834-
for(idx=0; idx<NUM_HUD_GAUGES; idx++){
1835-
SCP_string gauge = gauge_map.get_string_id_from_numeric_id(idx);
1836-
gr_init_alphacolor(&HUD_config.gauge_colors[gauge], HUD_config.gauge_colors[gauge].red, pos, HUD_config.gauge_colors[gauge].blue, HUD_config.gauge_colors[gauge].alpha);
1837-
}
1847+
for(const auto& gauge_pair : HC_gauge_map){
1848+
const SCP_string& gauge_id = gauge_pair.first;
1849+
if (!gauge_id.empty()) {
1850+
gr_init_alphacolor(&HUD_config.gauge_colors[gauge_id], pos, HUD_config.gauge_colors[gauge_id].green, HUD_config.gauge_colors[gauge_id].blue, HUD_config.gauge_colors[gauge_id].alpha);
1851+
}
1852+
}
18381853
}
18391854
// individual gauge
18401855
else {
18411856
if(HC_gauge_selected.empty()){
18421857
return;
18431858
}
18441859

1845-
// get slider position
18461860
gr_init_alphacolor(&HUD_config.gauge_colors[HC_gauge_selected], HUD_config.gauge_colors[HC_gauge_selected].red, pos, HUD_config.gauge_colors[HC_gauge_selected].blue, HUD_config.gauge_colors[HC_gauge_selected].alpha);
18471861
}
18481862

@@ -1851,23 +1865,22 @@ void hud_config_green_slider()
18511865

18521866
void hud_config_blue_slider()
18531867
{
1854-
int idx;
18551868
int pos = HCS_CONV(HC_color_sliders[HCS_BLUE].get_currentItem());
18561869

1857-
// select all ?
18581870
if(HC_select_all){
1859-
for(idx=0; idx<NUM_HUD_GAUGES; idx++){
1860-
SCP_string gauge = gauge_map.get_string_id_from_numeric_id(idx);
1861-
gr_init_alphacolor(&HUD_config.gauge_colors[gauge], HUD_config.gauge_colors[gauge].red, HUD_config.gauge_colors[gauge].green, pos, HUD_config.gauge_colors[gauge].alpha);
1862-
}
1871+
for(const auto& gauge_pair : HC_gauge_map){
1872+
const SCP_string& gauge_id = gauge_pair.first;
1873+
if (!gauge_id.empty()) {
1874+
gr_init_alphacolor(&HUD_config.gauge_colors[gauge_id], pos, HUD_config.gauge_colors[gauge_id].green, HUD_config.gauge_colors[gauge_id].blue, HUD_config.gauge_colors[gauge_id].alpha);
1875+
}
1876+
}
18631877
}
18641878
// individual gauge
18651879
else {
18661880
if(HC_gauge_selected.empty()){
18671881
return;
18681882
}
18691883

1870-
// get slider position
18711884
gr_init_alphacolor(&HUD_config.gauge_colors[HC_gauge_selected], HUD_config.gauge_colors[HC_gauge_selected].red, HUD_config.gauge_colors[HC_gauge_selected].green, pos, HUD_config.gauge_colors[HC_gauge_selected].alpha);
18721885
}
18731886

@@ -1991,3 +2004,27 @@ void hud_config_select_all_toggle(bool toggle, bool API_Access)
19912004
HC_select_all = true;
19922005
}
19932006
}
2007+
2008+
SCP_string create_custom_gauge_id(const SCP_string& gauge_name) {
2009+
Assertion(!gauge_name.empty(), "Custom gauge has no name!");
2010+
2011+
SCP_string id;
2012+
if (Mod_title.empty()) {
2013+
id = Cmdline_mod;
2014+
2015+
// Basic cleanup attempt
2016+
id = id.substr(0, id.find_first_of(DIR_SEPARATOR_CHAR));
2017+
} else {
2018+
id = Mod_title;
2019+
}
2020+
2021+
if (!id.empty()) {
2022+
std::replace(id.begin(), id.end(), ' ', '_');
2023+
} else {
2024+
id = "Custom";
2025+
}
2026+
2027+
id += "::" + gauge_name;
2028+
2029+
return id;
2030+
}

0 commit comments

Comments
 (0)