]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Add a macro to avoid code repetition
authorterencehill <piuntn@gmail.com>
Tue, 19 Apr 2016 17:29:34 +0000 (19:29 +0200)
committerterencehill <piuntn@gmail.com>
Tue, 19 Apr 2016 17:29:34 +0000 (19:29 +0200)
qcsrc/client/hud/panel/weapons.qc

index ed896e52ac730c2853e56e5888e50cbca7f3a44c..7e7c8b49a16f85f6be8616056fb1bbad466bc37a 100644 (file)
@@ -19,6 +19,20 @@ int weaponorder_cmp(int i, int j, entity pass)
        return aj - ai; // the string is in REVERSE order (higher prio at the right is what we want, but higher prio first is the string)
 }
 
+#define HUD_WEAPONS_GET_FULL_LAYOUT() MACRO_BEGIN { \
+       int nHidden = 0; \
+       WepSet weapons_stat = WepSet_GetFromStat(); \
+       FOREACH(Weapons, it != WEP_Null, { \
+               if (weapons_stat & it.m_wepset) continue; \
+               if (it.spawnflags & WEP_FLAG_MUTATORBLOCKED) nHidden += 1; \
+       }); \
+       vector table_size = HUD_GetTableSize_BestItemAR((Weapons_COUNT - 1) - nHidden, panel_size, aspect); \
+       columns = table_size.x; \
+       rows = table_size.y; \
+       weapon_size.x = panel_size.x / columns; \
+       weapon_size.y = panel_size.y / rows; \
+} MACRO_END
+
 void HUD_Weapons()
 {
        // declarations
@@ -128,40 +142,29 @@ void HUD_Weapons()
                        return;
 
                vector old_panel_size = panel_size;
-               vector padded_panel_size = panel_size - '2 2 0' * panel_bg_padding;
-
-               // get the all-weapons layout
-               int nHidden = 0;
-               WepSet weapons_stat = WepSet_GetFromStat();
-               FOREACH(Weapons, it != WEP_Null, {
-                       if (weapons_stat & it.m_wepset) continue;
-                       if (it.spawnflags & WEP_FLAG_MUTATORBLOCKED) nHidden += 1;
-               });
-               vector table_size = HUD_GetTableSize_BestItemAR((Weapons_COUNT - 1) - nHidden, padded_panel_size, aspect);
-               columns = table_size.x;
-               rows = table_size.y;
-               weapon_size.x = padded_panel_size.x / columns;
-               weapon_size.y = padded_panel_size.y / rows;
+               panel_size -= '2 2 0' * panel_bg_padding;
+
+               HUD_WEAPONS_GET_FULL_LAYOUT();
 
                // NOTE: although weapons should aways look the same even if onlyowned is enabled,
                // we enlarge them a bit when possible to better match the desired aspect ratio
-               if(padded_panel_size.x / padded_panel_size.y < aspect)
+               if(panel_size.x / panel_size.y < aspect)
                {
                        // maximum number of rows that allows to display items with the desired aspect ratio
-                       int max_rows = floor(padded_panel_size.y / (weapon_size.x / aspect));
+                       int max_rows = floor(panel_size.y / (weapon_size.x / aspect));
                        columns = min(columns, ceil(weapon_count / max_rows));
                        rows = ceil(weapon_count / columns);
-                       weapon_size.y = min(padded_panel_size.y / rows, weapon_size.x / aspect);
-                       weapon_size.x = min(padded_panel_size.x / columns, aspect * weapon_size.y);
+                       weapon_size.y = min(panel_size.y / rows, weapon_size.x / aspect);
+                       weapon_size.x = min(panel_size.x / columns, aspect * weapon_size.y);
                        vertical_order = false;
                }
                else
                {
-                       int max_columns = floor(padded_panel_size.x / (weapon_size.y * aspect));
+                       int max_columns = floor(panel_size.x / (weapon_size.y * aspect));
                        rows = min(rows, ceil(weapon_count / max_columns));
                        columns = ceil(weapon_count / rows);
-                       weapon_size.x = min(padded_panel_size.x / columns, aspect * weapon_size.y);
-                       weapon_size.y = min(padded_panel_size.y / rows, weapon_size.x / aspect);
+                       weapon_size.x = min(panel_size.x / columns, aspect * weapon_size.y);
+                       weapon_size.y = min(panel_size.y / rows, weapon_size.x / aspect);
                        vertical_order = true;
                }
 
@@ -285,7 +288,7 @@ void HUD_Weapons()
        HUD_Panel_DrawBg(1);
 
        if(center.x == -1)
-               return;
+               return; // panel has gone off screen
 
        if(panel_bg_padding)
        {
@@ -297,17 +300,7 @@ void HUD_Weapons()
 
        if(!rows) // if rows is > 0 onlyowned code has already updated these vars
        {
-               int nHidden = 0;
-               WepSet weapons_stat = WepSet_GetFromStat();
-               FOREACH(Weapons, it != WEP_Null, {
-                       if (weapons_stat & it.m_wepset) continue;
-                       if (it.spawnflags & WEP_FLAG_MUTATORBLOCKED) nHidden += 1;
-               });
-               vector table_size = HUD_GetTableSize_BestItemAR((Weapons_COUNT - 1) - nHidden, panel_size, aspect);
-               columns = table_size.x;
-               rows = table_size.y;
-               weapon_size.x = panel_size.x / columns;
-               weapon_size.y = panel_size.y / rows;
+               HUD_WEAPONS_GET_FULL_LAYOUT();
                vertical_order = (panel_size.x / panel_size.y >= aspect);
        }