Advertisement
szymski

Untitled

Mar 13th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.47 KB | None | 0 0
  1. class TINV_GuiSlotGrid {
  2.  
  3.     var SubInventory;
  4.     var Slots = { }; // Gui slots
  5.     var Items = { }; // Gui items
  6.  
  7.     /*
  8.         Init
  9.     */
  10.     function Init() {
  11.         self:__new();
  12.  
  13.         this:SetSize(64, 64);
  14.     }
  15.  
  16.     /*
  17.         Setup
  18.     */
  19.     function Setup(subInventory) {
  20.         this.SubInventory = subInventory;
  21.  
  22.         if(!this.SubInventory)
  23.             error("TINV Error: Gui slot not initialized properly! SubInventory is null.");
  24.  
  25.         // Create slots
  26.         for(var x = 1; x <= this.SubInventory.Width; x++) {
  27.             this.Slots[x] = { };
  28.             for(var y = 1; y <= this.SubInventory.Height; y++) {
  29.                 this.Slots[x][y] = this:Add("TINVGuiSlot");
  30.                 this.Slots[x][y]:Setup(this.SubInventory, x, y);
  31.                 this.Slots[x][y].DrawnLast = x == this.SubInventory.Width || y == this.SubInventory.Height;
  32.             }
  33.         }
  34.  
  35.         this:Populate();
  36.         this:PerformLayout();
  37.     }
  38.  
  39.     /*
  40.         Paint
  41.     */
  42.     function PaintOver(w, h) {
  43.         if(IsValid(TINV.HoverItem)) {
  44.             var screenX, screenY = this:LocalToScreen(0, 0);
  45.             var hoverX, hoverY = TINV.HoverItem:GetPos();
  46.             var startPosX = math.floor((hoverX - screenX) / this.Slots[1][1]:GetWide() + 0.5);
  47.             var startPosY = math.floor((hoverY - screenY) / this.Slots[1][1]:GetTall() + 0.5);
  48.  
  49.             draw.RoundedBox(0, startPosX * this.Slots[1][1]:GetWide(), startPosY * this.Slots[1][1]:GetTall(),
  50.                 this.Slots[1][1]:GetWide() * TINV.HoverItem.Item.Width, this.Slots[1][1]:GetTall()  * TINV.HoverItem.Item.Height, Color(200, 255, 255, 10));
  51.         }
  52.     }
  53.  
  54.     function Paint() { }
  55.  
  56.     /*
  57.         Slot clicking
  58.     */
  59.     function SlotClick(slot, x, y) {
  60.         LocalPlayer():ChatPrint("Slot clicked! " .. x .. " " .. y);
  61.  
  62.         var item = Enumerable(this.Items):First(i => (x >= i.Item.SlotX) && (y >= i.Item.SlotY) && (x <= i.Item.SlotX + i.Item.Width - 1) && (y <= i.Item.SlotY + i.Item.Height - 1));
  63.  
  64.         // Handle hovering item place or change
  65.         if(IsValid(TINV.HoverItem)) {
  66.             if(item) {
  67.                 LocalPlayer():ChatPrint("You're trying to swap items " .. item.Item.Model);
  68.             }
  69.             else {
  70.                 /*if(this.SubInventory:HasPlaceOn(x, y, TINV.HoverItem.Item.Width,  TINV.HoverItem.Item.Height)) {
  71.                     LocalPlayer():ChatPrint("Place free");
  72.                 }
  73.                 else
  74.                     LocalPlayer():ChatPrint("No place for item");*/
  75.  
  76.  
  77.                 // Real size and position calculation
  78.  
  79.                 var screenX, screenY = this:LocalToScreen(0, 0);
  80.                 var hoverX, hoverY = TINV.HoverItem:GetPos();
  81.                 var startPosX = math.floor((hoverX - screenX) / this.Slots[1][1]:GetWide() + 0.5) + 1;
  82.                 var startPosY = math.floor((hoverY - screenY) / this.Slots[1][1]:GetTall() + 0.5) + 1;
  83.  
  84.                 LocalPlayer():ChatPrint("" .. startPosX .. " " .. startPosY);
  85.  
  86.                 // Higlights the occupied area
  87.                 //for(var slotX = startPosX; slotX <= (startPosX + TINV.HoverItem.Item.Width - 1) && (slotX > 0) && (slotX <= this.SubInventory.Width); slotX++)
  88.                 //  for(var slotY = startPosY; slotY <= (startPosY + TINV.HoverItem.Item.Height - 1) && (slotY > 0) && (slotY <= this.SubInventory.Height); slotY++)
  89.                 //      if(this.Slots[slotX][slotY])
  90.                 //          this.Slots[slotX][slotY]:HighlightRed(2);
  91.  
  92.                 if(this.SubInventory:HasPlaceOn(startPosX, startPosY, TINV.HoverItem.Item.Width, TINV.HoverItem.Item.Height)) {
  93.                     LocalPlayer():ChatPrint("There's place");
  94.                     TINV.HoverItem.Item.SlotX, TINV.HoverItem.Item.SlotY = startPosX, startPosY;
  95.                     this.SubInventory.Items[#this.SubInventory.Items + 1] = TINV.HoverItem.Item;
  96.                     this.SubInventory:UpdateSlots();
  97.                     TINV.HoverItem:SetParent(this);
  98.                     this.Items[#this.Items + 1] = TINV.HoverItem;
  99.                     if(TINV.MaterialSounds[TINV.HoverItem.Item.MaterialType]) // Material sound playing
  100.                         surface.PlaySound(table.Random(TINV.MaterialSounds[TINV.HoverItem.Item.MaterialType]));
  101.                     TINV.HoverItem:SetHover(false);
  102.                     TINV.Place(this.SubInventory, startPosX, startPosY);
  103.                 }
  104.                 else {
  105.                     LocalPlayer():ChatPrint("There's no place :(");
  106.                 }
  107.             }
  108.  
  109.             return;
  110.         }
  111.  
  112.         // Item grabbing
  113.         if(item) {
  114.             LocalPlayer():ChatPrint("You clicked on item: " .. item.Item.Model);
  115.             table.RemoveByValue(this.SubInventory.Items, item.Item);
  116.             this.SubInventory:UpdateSlots();
  117.             table.RemoveByValue(this.Items, item);
  118.             item:SetHover(true);
  119.             TINV.Grab(this.SubInventory, x, y);
  120.             if(TINV.MaterialSounds[TINV.HoverItem.Item.MaterialType]) // Material sound playing
  121.                 surface.PlaySound(table.Random(TINV.MaterialSounds[TINV.HoverItem.Item.MaterialType]));
  122.         }
  123.     }
  124.  
  125.     /*
  126.         Populate - creates gui items
  127.     */
  128.     function Populate() {
  129.         foreach(var k, v in this.Items) {
  130.             v:Remove();
  131.             this.Items[k] = null;
  132.         }
  133.  
  134.         foreach(var item in this.SubInventory.Items) {
  135.             var i = this:Add("TINVGuiItem");
  136.             i:Setup(item);
  137.             this.Items[#this.Items+1] = i; 
  138.         }
  139.     }
  140.  
  141.     /*
  142.         Layout
  143.     */
  144.     function PerformLayout() {
  145.         // Calculate size for single slot
  146.         var sizeX, sizeY = this:GetWide() / this.SubInventory.Width, this:GetTall() / this.SubInventory.Height;
  147.  
  148.         // Size a position each slot
  149.         for(var x = 0; x < this.SubInventory.Width; x++)
  150.             for(var y = 0; y < this.SubInventory.Height; y++) {
  151.                 this.Slots[x+1][y+1]:SetSize(math.floor(sizeX), math.floor(sizeY));
  152.                 this.Slots[x+1][y+1]:SetPos(math.floor(x*sizeX), math.floor(y*sizeY));
  153.             }
  154.  
  155.         // Gui items
  156.         foreach(var item in this.Items) {
  157.             item:SetPos(sizeX * (item.Item.SlotX-1), sizeY * (item.Item.SlotY-1));
  158.             item:SetSize(sizeX * item.Item.Width, sizeY * item.Item.Height);
  159.         }
  160.     }
  161. }
  162.  
  163. CTINV_GuiSlotGrid.__index = null; // This is required for derma inheritance ;(
  164. derma.DefineControl("TINVGuiSlotGrid", "Tetris Inventory Slot Grid", CTINV_GuiSlotGrid, "DPanel");
  165.  
  166. if(TINV.Test)
  167.     TINV.Test();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement