Advertisement
Guest User

mat_weapons.lua

a guest
Aug 20th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.06 KB | None | 0 0
  1. require "base/internal/ui/reflexcore"
  2.  
  3. mat_weapons =
  4. {
  5.     userData = {};
  6. };
  7. registerWidget("mat_weapons");
  8.  
  9. function mat_weapons:initialize()
  10.     self.userData = loadUserData();
  11.  
  12.     CheckSetDefaultValue(self, "userData", "table", {});
  13.     CheckSetDefaultValue(self.userData, "alpha1", "number", 255);
  14.     CheckSetDefaultValue(self.userData, "alpha2", "number", 255);
  15. end
  16.  
  17. function mat_weapons:drawOptions(x, y)
  18.     local u = self.userData;
  19.  
  20.     uiLabel("Transparency (white boxes)", x, y);
  21.     u.alpha1 = uiEditBox(u.alpha1, x+230, y, 80);
  22.     y = y + 40;
  23.  
  24.     uiLabel("Transparency (black boxes)", x, y);
  25.     u.alpha2 = uiEditBox(u.alpha2, x+230, y, 80);
  26.     y = y + 40;
  27.  
  28.     saveUserData(u);
  29. end
  30.  
  31. function draw_box(x, y, w, h, r, color, stroke, strokeColor)
  32.     nvgBeginPath();
  33.  
  34.     nvgRoundedRect(x, y, w, h, r);
  35.     nvgFillColor(color);
  36.     nvgFill();
  37.  
  38.     if stroke then
  39.         nvgStrokeWidth(2);
  40.         nvgStrokeColor(strokeColor);
  41.         nvgStroke();
  42.     end
  43. end
  44.  
  45. function draw_weapon(x_Icon, y_Icon, x_Ammo, y_Ammo, icon, color, size, fontColor, ammo)
  46.     -- icon
  47.     local x = 0.38;
  48.     nvgFillColor(Color(100,100,100,255));
  49.     nvgSvg(icon, x_Icon+0.5, y_Icon+0.5, size * x);
  50.     nvgFillColor(color);
  51.     nvgSvg(icon, x_Icon, y_Icon, size * x);
  52.  
  53.     -- ammo
  54.     nvgFontSize(size * 1.1);
  55.     --nvgFontFace("TitilliumWeb-Bold");
  56.     nvgTextAlign(NVG_ALIGN_CENTER, NVG_ALIGN_MIDDLE);
  57.     nvgFontBlur(0);
  58.     nvgFillColor(fontColor);
  59.     nvgText(x_Ammo, y_Ammo, ammo);
  60. end
  61.  
  62. function mat_weapons:draw()
  63.  
  64.     if not shouldShowHUD() then return end;
  65.  
  66.     -- info
  67.  
  68.     local player = getPlayer();
  69.  
  70.     local alpha1 = self.userData.alpha1;
  71.     local alpha2 = self.userData.alpha2;
  72.  
  73.  
  74.     local numWeapons = 9; -- axe, bg, sg, pg, gl, rl, ic, br, stake?
  75.     local numCarried = 0;
  76.     local selectedShown = false;
  77.  
  78.     for weaponIndex = 3, numWeapons-1 do
  79.         local weapon = player.weapons[weaponIndex];
  80.         if weapon.pickedup then
  81.             numCarried = numCarried + 1;
  82.         end
  83.         if weaponIndex == player.weaponIndexSelected then
  84.             selectedShown = true;
  85.         end
  86.     end
  87.  
  88.     -- style
  89.  
  90.     local roundness = 0;
  91.  
  92.     local color_Box = Color(0,0,0,alpha2);
  93.     local color_BoxSelected = Color(255,255,255,alpha1);
  94.  
  95.     local stroke = false;
  96.     local color_BoxStroke = Color(0,0,0,alpha2);
  97.     local color_BoxStrokeSelected = Color(255,255,255,alpha1);
  98.  
  99.     local color_Font = Color(255,255,255,255);
  100.     local color_FontSelected = Color(0,0,0,255);
  101.  
  102.     -- weapon box
  103.  
  104.     local height_Box = 30;
  105.     local width_IconBox = height_Box;
  106.     local width_AmmoBox = 40;
  107.     local width_Box = width_IconBox + width_AmmoBox;
  108.  
  109.     local space = 10;
  110.     local scale = 1.8;
  111.  
  112.     -- weapon rack
  113.  
  114.     local width_Rack = (numCarried - 1) * (width_Box + space) + scale * width_Box;
  115.     local offset_y = height_Box / 2;
  116.  
  117.     if selectedShown then
  118.         offset_y = (height_Box * scale) / 2;
  119.     end
  120.  
  121.     -- draw
  122.  
  123.     local xpos = -width_Rack / 2;
  124.  
  125.     for i = 3, numWeapons-1 do
  126.         local weapon = player.weapons[i];
  127.         local color = weapon.color;
  128.  
  129.         if weapon.pickedup then
  130.             -- box
  131.             local x = xpos;
  132.             local y = -height_Box / 2;
  133.             local w = width_Box;
  134.             local h = height_Box;
  135.             local c = color_Box;
  136.             local s = color_BoxStroke;
  137.             if i == player.weaponIndexSelected then
  138.                 y = -(height_Box * scale) / 2;
  139.                 w = w * scale;
  140.                 h = h * scale;
  141.                 c = color_BoxSelected;
  142.                 s = color_BoxStrokeSelected;
  143.             end
  144.  
  145.             -- icon/ammo
  146.             local x_Icon = x + width_IconBox / 2;
  147.             local y_Icon = y + height_Box / 2;
  148.             local x_Ammo = x + width_IconBox + width_AmmoBox / 2;
  149.             local y_Ammo = y + height_Box / 2;
  150.             local icon = "internal/ui/icons/weapon"..i;
  151.             local size = width_IconBox;
  152.             local fontColor = color_Font;
  153.             if i == player.weaponIndexSelected then
  154.                 size = size * scale;
  155.                 x_Icon = x + scale * width_IconBox / 2;
  156.                 y_Icon = y + scale * height_Box / 2;
  157.                 x_Ammo = x + scale * width_IconBox + scale * width_AmmoBox / 2;
  158.                 y_Ammo = y + scale * height_Box / 2;
  159.                 fontColor = color_FontSelected;
  160.             end
  161.  
  162.             -- ammo color
  163.             local ammoMax = weapon.maxAmmo;
  164.             local ammoLow = weapon.lowAmmoWarning;
  165.  
  166.             local colorTop = Color(fontColor.r, fontColor.g, fontColor.b);
  167.             local colorMid = Color(255,176,14);
  168.             local colorLow = Color(236,0,0);
  169.  
  170.             if i == player.weaponIndexSelected then
  171.                 colorTop = Color(c.r, c.g, c.b, 255);
  172.             end
  173.  
  174.             local ammoColor = Color(colorTop.r, colorTop.g, colorTop.b, 255);
  175.  
  176.             if weapon.ammo > ammoLow then
  177.                 local a = (weapon.ammo - ammoLow) / (ammoMax - ammoLow);
  178.                 ammoColor.r = a * colorTop.r + (1-a) * colorMid.r;
  179.                 ammoColor.g = a * colorTop.g + (1-a) * colorMid.g;
  180.                 ammoColor.b = a * colorTop.b + (1-a) * colorMid.b;
  181.             else
  182.                 local a = weapon.ammo / ammoLow;
  183.                 ammoColor.r = a * colorMid.r + (1-a) * colorLow.r;
  184.                 ammoColor.g = a * colorMid.g + (1-a) * colorLow.g;
  185.                 ammoColor.b = a * colorMid.b + (1-a) * colorLow.b;
  186.             end
  187.  
  188.             if i == player.weaponIndexSelected then
  189.                 ammoColor.a = alpha1;
  190.                 c = ammoColor;
  191.             else
  192.                 fontColor = ammoColor;
  193.             end
  194.  
  195.             -- draw
  196.             draw_box(x, y, w, h, roundness, c, stroke, s);
  197.             draw_weapon(x_Icon, y_Icon, x_Ammo, y_Ammo, icon, color, size, fontColor, weapon.ammo);
  198.  
  199.             xpos = xpos + w + space;
  200.         end
  201.     end
  202.  
  203. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement