Last active
January 11, 2024 18:42
-
-
Save AltimorTASDK/34556af72d65b6fe9cb7a41d9ecae7c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Hud::Init() | |
{ | |
exec("hudLayout"); | |
$Hud::count = 0; | |
$Hud::baseExtent[0] = 1920; | |
$Hud::baseExtent[1] = 1080; | |
} | |
function Hud::Reload() | |
{ | |
exec("hudLayout"); | |
for (%i = 0; %i < $Hud::count; %i++) | |
Hud::Restore(%i); | |
Hud::RestorePlayGui(); | |
} | |
function Hud::New(%name, %x, %y, %w, %h, %wake, %sleep, %shaded) | |
{ | |
// don't recreate the hud | |
if ($Hud::huds[%name] != 0) | |
return false; | |
if ($Hud::positions[%name] == "") | |
return false; | |
// shaded = background, non shaded = just the hud text | |
%type = %shaded ? FearGui::ShadedHudCtrl : FearGui::HudCtrl; | |
%obj = newObject(%name, %type, %x, %y, %w, %h); | |
if (%obj == 0) | |
return false; | |
// set the hud variables | |
$Hud::huds[%name] = %obj; | |
$Hud::huds[$Hud::count] = %obj; | |
$Hud::huds[$Hud::count, name] = %name; | |
$Hud::huds[$Hud::count, wake] = %wake; | |
$Hud::huds[$Hud::count, sleep] = %sleep; | |
Control::SetVisible(%name, true); | |
$Hud::count++; | |
return true; | |
} | |
function Hud::New::Shaded(%name, %x, %y, %w, %h, %wake, %sleep) | |
{ | |
Hud::New(%name, %x, %y, %w, %h, %wake, %sleep, true); | |
} | |
// add a component to a hud | |
function Hud::Add(%name, %addname) | |
{ | |
if ($Hud::huds[%name]) | |
addToSet(%name, %addname); | |
} | |
function Hud::Eval(%expr, %axis) | |
{ | |
%expr = String::trim(%expr); | |
%opIndex = max(String::findSubStr(%expr, '+'), | |
String::findSubStr(%expr, '-')); | |
if (%opIndex > 0) { | |
%a = String::getSubStr(%expr, 0, %opIndex); | |
%b = String::getSubStr(%expr, %opIndex + 1, 255); | |
switch (String::char(%expr, %opIndex)) { | |
case '+': return Hud::Eval(%a, %axis) + Hud::Eval(%b, %axis); | |
case '-': return Hud::Eval(%a, %axis) - Hud::Eval(%b, %axis); | |
} | |
} | |
if (String::ends(%expr, '%')) | |
return %expr * $Hud::extent[%axis] / 100; | |
if (String::ends(%expr, 'vw')) | |
return %expr * $Hud::extent[0] / 100; | |
if (String::ends(%expr, 'vh')) | |
return %expr * $Hud::extent[1] / 100; | |
if (String::ends(%expr, 'px')) | |
return %expr + 0; | |
return %expr * $Hud::extent[%axis] / $Hud::baseExtent[%axis]; | |
} | |
function Hud::RestoreControl(%control, %position) | |
{ | |
if (String::explode(%position, ",", "fields") != 4) { | |
%name = Object::getName(%control); | |
echoc($CON_RED, %name~": Expected: x, y, <left|center|right>, <top|center|bottom>"); | |
return; | |
} | |
%x = $fields[0]; | |
%y = $fields[1]; | |
%alignX = String::trim($fields[2]); | |
%alignY = String::trim($fields[3]); | |
%x = Hud::Eval(%x, 0); | |
%y = Hud::Eval(%y, 1); | |
%extentX = getWord(%control.extent, 0); | |
%extentY = getWord(%control.extent, 1); | |
switch (%alignX) { | |
case 'center': %x -= %extentX / 2; break; | |
case 'right': %x -= %extentX; break; | |
} | |
switch (%alignY) { | |
case 'center': %y -= %extentY / 2; break; | |
case 'bottom': %y -= %extentY; break; | |
} | |
%fracX = %x / ($Hud::extent[0] - %extentX); | |
%fracY = %y / ($Hud::extent[1] - %extentY); | |
%control.position = round(%x)~" "~round(%y); | |
%control.fracPos = %fracX~" "~%fracY; | |
} | |
function Hud::RestorePlayGui() | |
{ | |
%control = getNextObject(playGui, 0); | |
while (%control != 0) { | |
%name = "playGui/"~object::getName(%control); | |
%position = $Hud::positions[%name]; | |
if (%position != "") | |
Hud::RestoreControl(%control, %position); | |
%control = getNextObject(playGui, %control); | |
} | |
} | |
function Hud::Restore(%i) | |
{ | |
%control = $Hud::huds[%i]; | |
%name = $Hud::huds[%i, name]; | |
%position = $Hud::positions[%name]; | |
Hud::RestoreControl(%control, %position); | |
} | |
function Hud::CheckResize() | |
before Game::EndFrame | |
{ | |
if ($Hud::lastExtent && playGui.extent != $Hud::lastExtent) { | |
String::explode(playGui.extent, " ", "Hud::extent"); | |
for (%i = 0; %i < $Hud::count; %i++) | |
Hud::Restore(%i); | |
Hud::RestorePlayGui(); | |
} | |
$Hud::lastExtent = playGui.extent; | |
} | |
function Hud::OnGuiOpen(%gui) | |
{ | |
if (%gui != playGui) | |
return; | |
String::explode(playGui.extent, " ", "Hud::extent"); | |
for (%i = 0; %i < $Hud::count; %i++) { | |
%handle = $Hud::huds[%i]; | |
Hud::Restore(%i); | |
addToSet(%gui, %handle); | |
*$Hud::huds[%i, wake](); | |
} | |
Hud::RestorePlayGui(); | |
} | |
function Hud::OnGuiClose(%gui) | |
{ | |
if (%gui != playGui) | |
return; | |
for (%i = 0; %i < $Hud::count; %i++) { | |
%handle = $Hud::huds[%i]; | |
removeFromSet(%gui, %handle); | |
*$Hud::huds[%i, sleep](); | |
} | |
} | |
// clean up so we don't leave our garbage in play.gui | |
function Hud::OnExit() | |
{ | |
for (%i = 0; %i < $Hud::count; %i++) { | |
%handle = $Hud::huds[%i]; | |
removeFromSet(playGui, %handle); | |
deleteObject(%handle); | |
} | |
} | |
Hud::Init(); | |
Event::Attach(eventGuiClose, Hud::OnGuiClose); | |
Event::Attach(eventGuiOpen, Hud::OnGuiOpen); | |
Event::Attach(eventExit, Hud::OnExit); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment