]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Clean up and improve HUD_Get_Num_Color so that it can be used by crosshair_color_spec...
authorterencehill <piuntn@gmail.com>
Fri, 5 Jun 2020 12:11:14 +0000 (14:11 +0200)
committerterencehill <piuntn@gmail.com>
Fri, 5 Jun 2020 12:11:14 +0000 (14:11 +0200)
qcsrc/client/hud/hud.qc
qcsrc/client/hud/hud.qh
qcsrc/client/hud/panel/engineinfo.qc
qcsrc/client/hud/panel/healtharmor.qc
qcsrc/client/hud/panel/scoreboard.qc
qcsrc/client/view.qc

index 91770fd724ecc9b3b477ce5d279a88077e0448d0..bbb49199f9d727f342b5775c1456e1532b897842 100644 (file)
@@ -28,48 +28,45 @@ Misc HUD functions
 ==================
 */
 
-vector HUD_Get_Num_Color (float hp, float maxvalue)
+vector HUD_Get_Num_Color(float hp, float maxvalue, bool blink)
 {
-       float blinkingamt;
+       const vector COLOR100 = '0 1 0'; // green
+       const vector COLOR75 = '0.4 0.9 0'; // lightgreen
+       const vector COLOR50 = '1 1 1'; // white
+       const vector COLOR25 = '1 1 0.2'; // lightyellow
+       const vector COLOR10 = '1 0 0'; // red
        vector color;
-       if(hp >= maxvalue) {
-               color.x = sin(2*M_PI*time);
-               color.y = 1;
-               color.z = sin(2*M_PI*time);
-       }
-       else if(hp > maxvalue * 0.75) {
-               color.x = 0.4 - (hp-150)*0.02 * 0.4; //red value between 0.4 -> 0
-               color.y = 0.9 + (hp-150)*0.02 * 0.1; // green value between 0.9 -> 1
-               color.z = 0;
-       }
-       else if(hp > maxvalue * 0.5) {
-               color.x = 1 - (hp-100)*0.02 * 0.6; //red value between 1 -> 0.4
-               color.y = 1 - (hp-100)*0.02 * 0.1; // green value between 1 -> 0.9
-               color.z = 1 - (hp-100)*0.02; // blue value between 1 -> 0
-       }
-       else if(hp > maxvalue * 0.25) {
-               color.x = 1;
-               color.y = 1;
-               color.z = 0.2 + (hp-50)*0.02 * 0.8; // blue value between 0.2 -> 1
-       }
-       else if(hp > maxvalue * 0.1) {
-               color.x = 1;
-               color.y = (hp-20)*90/27/100; // green value between 0 -> 1
-               color.z = (hp-20)*90/27/100 * 0.2; // blue value between 0 -> 0.2
-       }
-       else {
-               color.x = 1;
-               color.y = 0;
-               color.z = 0;
-       }
 
-       blinkingamt = (1 - hp/maxvalue/0.25);
-       if(blinkingamt > 0)
+       float hp_percent = hp / maxvalue * 100;
+       #define CASE_COLOR_BETWEEN(min, max) \
+               if(hp_percent > min) \
+                       color = COLOR##min + (COLOR##max - COLOR##min) * ((hp_percent - min) / (max - min))
+
+       if(hp_percent > 100) color = COLOR100;
+       else CASE_COLOR_BETWEEN(75, 100);
+       else CASE_COLOR_BETWEEN(50, 75);
+       else CASE_COLOR_BETWEEN(25, 50);
+       else CASE_COLOR_BETWEEN(10, 25);
+       else color = COLOR10;
+
+       #undef CASE_COLOR_BETWEEN
+
+       if (blink)
        {
-               color.x = color.x - color.x * blinkingamt * sin(2*M_PI*time);
-               color.y = color.y - color.y * blinkingamt * sin(2*M_PI*time);
-               color.z = color.z - color.z * blinkingamt * sin(2*M_PI*time);
+               if(hp_percent >= 100)
+               {
+                       float f = sin(2*M_PI*time);
+                       if (color.x == 0) color.x = f;
+                       if (color.y == 0) color.y = f;
+                       if (color.z == 0) color.z = f;
+               }
+               else if(hp_percent < 25)
+               {
+                       float f = (1 - hp_percent / 25) * sin(2*M_PI*time);
+                       color -= color * f;
+               }
        }
+
        return color;
 }
 
index 38ca8a0187c9845f43bbfcc40c37ee66b7c30961..f93033f07e5fa0804d594f2e532ec9cc9bf35748 100644 (file)
@@ -59,7 +59,7 @@ float HUD_Radar_InputEvent(float bInputType, float nPrimary, float nSecondary);
 void HUD_Radar_Hide_Maximized();
 
 float HUD_GetRowCount(int item_count, vector size, float item_aspect);
-vector HUD_Get_Num_Color (float hp, float maxvalue);
+vector HUD_Get_Num_Color(float hp, float maxvalue, bool blink);
 void DrawNumIcon(vector myPos, vector mySize, float theTime, string icon, bool vertical, bool icon_right_align, vector color, float theAlpha);
 void DrawNumIcon_expanding(vector myPos, vector mySize, float theTime, string icon, bool vertical, int icon_right_align, vector color, float theAlpha, float fadelerp);
 void HUD_Panel_DrawHighlight(vector pos, vector mySize, vector color, float theAlpha, int drawflag);
index 1c59e6c789027ebeb910afe39fefe6effa1394bc..85d4259778d1598d1c0204fe0322596bd004c20f 100644 (file)
@@ -72,7 +72,6 @@ void HUD_EngineInfo()
                }
        }
 
-       vector color;
-       color = HUD_Get_Num_Color (prevfps, 100);
+       vector color = HUD_Get_Num_Color(prevfps, 100, true);
        drawstring_aspect(pos, sprintf(_("FPS: %.*f"), autocvar_hud_panel_engineinfo_framecounter_decimals, prevfps), mySize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
 }
index b1a9311939e33ea9f368f15baef17a441045331f..d6bf99fd60494d31670feee9dff583e02920c0cb 100644 (file)
@@ -132,7 +132,7 @@ void HUD_HealthArmor()
                                drawpic_aspect_skin(pos + eX * mySize.x - eX * 0.5 * mySize.y, "health", '0.5 0.5 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
                }
                if(autocvar_hud_panel_healtharmor_text)
-                       DrawNumIcon(pos, mySize, hp, biggercount, 0, iconalign, HUD_Get_Num_Color(hp, maxtotal), 1);
+                       DrawNumIcon(pos, mySize, hp, biggercount, 0, iconalign, HUD_Get_Num_Color(hp, maxtotal, true), 1);
 
                if(fuel)
                        HUD_Panel_DrawProgressBar(pos, vec2(mySize.x, 0.2 * mySize.y), "progressbar", fuel/100, 0, (baralign == 1 || baralign == 3), autocvar_hud_progressbar_fuel_color, panel_fg_alpha * 0.8, DRAWFLAG_NORMAL);
@@ -229,7 +229,7 @@ void HUD_HealthArmor()
                                HUD_Panel_DrawProgressBar(pos + health_offset, mySize, autocvar_hud_panel_healtharmor_progressbar_health, p_health/maxhealth, is_vertical, health_baralign, autocvar_hud_progressbar_health_color, autocvar_hud_progressbar_alpha * panel_fg_alpha * pain_health_alpha, DRAWFLAG_NORMAL);
                        }
                        if(autocvar_hud_panel_healtharmor_text)
-                               DrawNumIcon(pos + health_offset, mySize, health, "health", is_vertical, health_iconalign, HUD_Get_Num_Color(health, maxhealth), 1);
+                               DrawNumIcon(pos + health_offset, mySize, health, "health", is_vertical, health_iconalign, HUD_Get_Num_Color(health, maxhealth, true), 1);
                }
 
                //if(armor)
@@ -276,7 +276,7 @@ void HUD_HealthArmor()
                        }
                        if(!autocvar_hud_panel_healtharmor_progressbar || p_armor)
                        if(autocvar_hud_panel_healtharmor_text)
-                               DrawNumIcon(pos + armor_offset, mySize, armor, "armor", is_vertical, armor_iconalign, HUD_Get_Num_Color(armor, maxarmor), 1);
+                               DrawNumIcon(pos + armor_offset, mySize, armor, "armor", is_vertical, armor_iconalign, HUD_Get_Num_Color(armor, maxarmor, true), 1);
                }
 
                vector cell_size = mySize;
index a7efc181a0b1680a94ccdb01d9ea18afda3f549c..2b638dbbbb98b39f240cf435d7c99f0b2d5549d9 100644 (file)
@@ -690,7 +690,7 @@ string Scoreboard_GetField(entity pl, PlayerScoreField field)
                                sbt_field_rgb = '1 1 1';
                                return ((pl.ping == 0) ? _("N/A") : "..."); // if 0 ping, either connecting or bot (either case can't show proper score)
                        }
-                       //sbt_field_rgb = HUD_Get_Num_Color(fps, 200);
+                       //sbt_field_rgb = HUD_Get_Num_Color(fps, 200, true);
                        sbt_field_rgb = '1 0 0' + '0 1 1' * (bound(0, fps, 60) / 60);
                        return ftos(fps);
                }
index cb6b623ec208d8d084c071a03a39774486845ee5..5b07cdb210e10387aea491e5f713f7cdb3de07b5 100644 (file)
@@ -1016,47 +1016,8 @@ vector crosshair_getcolor(entity this, float health_stat)
                case 2: // color based on health and armor
                {
                        vector v = healtharmor_maxdamage(health_stat, STAT(ARMOR), armorblockpercent, DEATH_WEAPON.m_id);
-                       float hp = floor(v.x + 1);
-
-                       //x = red
-                       //y = green
-                       //z = blue
-
-                       wcross_color.z = 0;
-
-                       if(hp > 200)
-                       {
-                               wcross_color.x = 0;
-                               wcross_color.y = 1;
-                       }
-                       else if(hp > 150)
-                       {
-                               wcross_color.x = 0.4 - (hp-150)*0.02 * 0.4;
-                               wcross_color.y = 0.9 + (hp-150)*0.02 * 0.1;
-                       }
-                       else if(hp > 100)
-                       {
-                               wcross_color.x = 1 - (hp-100)*0.02 * 0.6;
-                               wcross_color.y = 1 - (hp-100)*0.02 * 0.1;
-                               wcross_color.z = 1 - (hp-100)*0.02;
-                       }
-                       else if(hp > 50)
-                       {
-                               wcross_color.x = 1;
-                               wcross_color.y = 1;
-                               wcross_color.z = 0.2 + (hp-50)*0.02 * 0.8;
-                       }
-                       else if(hp > 20)
-                       {
-                               wcross_color.x = 1;
-                               wcross_color.y = (hp-20)*90/27/100;
-                               wcross_color.z = (hp-20)*90/27/100 * 0.2;
-                       }
-                       else
-                       {
-                               wcross_color.x = 1;
-                               wcross_color.y = 0;
-                       }
+                       float health_and_armor = floor(v.x + 1);
+                       wcross_color = HUD_Get_Num_Color(health_and_armor, 200, false);
                        break;
                }