Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TINV_GuiSlotGrid {
- var SubInventory;
- var Slots = { }; // Gui slots
- var Items = { }; // Gui items
- /*
- Init
- */
- function Init() {
- self:__new();
- this:SetSize(64, 64);
- }
- /*
- Setup
- */
- function Setup(subInventory) {
- this.SubInventory = subInventory;
- if(!this.SubInventory)
- error("TINV Error: Gui slot not initialized properly! SubInventory is null.");
- // Create slots
- for(var x = 1; x <= this.SubInventory.Width; x++) {
- this.Slots[x] = { };
- for(var y = 1; y <= this.SubInventory.Height; y++) {
- this.Slots[x][y] = this:Add("TINVGuiSlot");
- this.Slots[x][y]:Setup(this.SubInventory, x, y);
- this.Slots[x][y].DrawnLast = x == this.SubInventory.Width || y == this.SubInventory.Height;
- }
- }
- this:Populate();
- this:PerformLayout();
- }
- /*
- Paint
- */
- function PaintOver(w, h) {
- if(IsValid(TINV.HoverItem)) {
- var screenX, screenY = this:LocalToScreen(0, 0);
- var hoverX, hoverY = TINV.HoverItem:GetPos();
- var startPosX = math.floor((hoverX - screenX) / this.Slots[1][1]:GetWide() + 0.5);
- var startPosY = math.floor((hoverY - screenY) / this.Slots[1][1]:GetTall() + 0.5);
- draw.RoundedBox(0, startPosX * this.Slots[1][1]:GetWide(), startPosY * this.Slots[1][1]:GetTall(),
- this.Slots[1][1]:GetWide() * TINV.HoverItem.Item.Width, this.Slots[1][1]:GetTall() * TINV.HoverItem.Item.Height, Color(200, 255, 255, 10));
- }
- }
- function Paint() { }
- /*
- Slot clicking
- */
- function SlotClick(slot, x, y) {
- LocalPlayer():ChatPrint("Slot clicked! " .. x .. " " .. y);
- 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));
- // Handle hovering item place or change
- if(IsValid(TINV.HoverItem)) {
- if(item) {
- LocalPlayer():ChatPrint("You're trying to swap items " .. item.Item.Model);
- }
- else {
- /*if(this.SubInventory:HasPlaceOn(x, y, TINV.HoverItem.Item.Width, TINV.HoverItem.Item.Height)) {
- LocalPlayer():ChatPrint("Place free");
- }
- else
- LocalPlayer():ChatPrint("No place for item");*/
- // Real size and position calculation
- var screenX, screenY = this:LocalToScreen(0, 0);
- var hoverX, hoverY = TINV.HoverItem:GetPos();
- var startPosX = math.floor((hoverX - screenX) / this.Slots[1][1]:GetWide() + 0.5) + 1;
- var startPosY = math.floor((hoverY - screenY) / this.Slots[1][1]:GetTall() + 0.5) + 1;
- LocalPlayer():ChatPrint("" .. startPosX .. " " .. startPosY);
- // Higlights the occupied area
- //for(var slotX = startPosX; slotX <= (startPosX + TINV.HoverItem.Item.Width - 1) && (slotX > 0) && (slotX <= this.SubInventory.Width); slotX++)
- // for(var slotY = startPosY; slotY <= (startPosY + TINV.HoverItem.Item.Height - 1) && (slotY > 0) && (slotY <= this.SubInventory.Height); slotY++)
- // if(this.Slots[slotX][slotY])
- // this.Slots[slotX][slotY]:HighlightRed(2);
- if(this.SubInventory:HasPlaceOn(startPosX, startPosY, TINV.HoverItem.Item.Width, TINV.HoverItem.Item.Height)) {
- LocalPlayer():ChatPrint("There's place");
- TINV.HoverItem.Item.SlotX, TINV.HoverItem.Item.SlotY = startPosX, startPosY;
- this.SubInventory.Items[#this.SubInventory.Items + 1] = TINV.HoverItem.Item;
- this.SubInventory:UpdateSlots();
- TINV.HoverItem:SetParent(this);
- this.Items[#this.Items + 1] = TINV.HoverItem;
- if(TINV.MaterialSounds[TINV.HoverItem.Item.MaterialType]) // Material sound playing
- surface.PlaySound(table.Random(TINV.MaterialSounds[TINV.HoverItem.Item.MaterialType]));
- TINV.HoverItem:SetHover(false);
- TINV.Place(this.SubInventory, startPosX, startPosY);
- }
- else {
- LocalPlayer():ChatPrint("There's no place :(");
- }
- }
- return;
- }
- // Item grabbing
- if(item) {
- LocalPlayer():ChatPrint("You clicked on item: " .. item.Item.Model);
- table.RemoveByValue(this.SubInventory.Items, item.Item);
- this.SubInventory:UpdateSlots();
- table.RemoveByValue(this.Items, item);
- item:SetHover(true);
- TINV.Grab(this.SubInventory, x, y);
- if(TINV.MaterialSounds[TINV.HoverItem.Item.MaterialType]) // Material sound playing
- surface.PlaySound(table.Random(TINV.MaterialSounds[TINV.HoverItem.Item.MaterialType]));
- }
- }
- /*
- Populate - creates gui items
- */
- function Populate() {
- foreach(var k, v in this.Items) {
- v:Remove();
- this.Items[k] = null;
- }
- foreach(var item in this.SubInventory.Items) {
- var i = this:Add("TINVGuiItem");
- i:Setup(item);
- this.Items[#this.Items+1] = i;
- }
- }
- /*
- Layout
- */
- function PerformLayout() {
- // Calculate size for single slot
- var sizeX, sizeY = this:GetWide() / this.SubInventory.Width, this:GetTall() / this.SubInventory.Height;
- // Size a position each slot
- for(var x = 0; x < this.SubInventory.Width; x++)
- for(var y = 0; y < this.SubInventory.Height; y++) {
- this.Slots[x+1][y+1]:SetSize(math.floor(sizeX), math.floor(sizeY));
- this.Slots[x+1][y+1]:SetPos(math.floor(x*sizeX), math.floor(y*sizeY));
- }
- // Gui items
- foreach(var item in this.Items) {
- item:SetPos(sizeX * (item.Item.SlotX-1), sizeY * (item.Item.SlotY-1));
- item:SetSize(sizeX * item.Item.Width, sizeY * item.Item.Height);
- }
- }
- }
- CTINV_GuiSlotGrid.__index = null; // This is required for derma inheritance ;(
- derma.DefineControl("TINVGuiSlotGrid", "Tetris Inventory Slot Grid", CTINV_GuiSlotGrid, "DPanel");
- if(TINV.Test)
- TINV.Test();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement