Advertisement
Guest User

dxGridlist

a guest
Mar 18th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.29 KB | None | 0 0
  1. --[[*************************************************************************
  2. *
  3. *   PROJECT:        dxGridlists
  4. *   DEVELOPERS:     t3wz < github.com/t3wz >
  5. *   VERSION:        1.1
  6. *
  7. *   YOU AREN'T ALLOWED TO SELL THIS SCRIPT OR REMOVE THE AUTHOR'S NAME
  8. *                   EVEN IF YOU MADE SEVERAL CHANGES !
  9. *
  10. ****************************************************************************]]
  11.  
  12. dxGrid          =   { items = {} };
  13. local cursorOn;
  14.  
  15. local NATIVE_RESOLUTION     =   { 1280,1024 } -- put your screen resolution here to fit the gridlists to all resolutions (ex: { 1366, 768 } )
  16. if ( table.maxn ( NATIVE_RESOLUTION ) == 2 ) then
  17.     FIT_MODE                =   true
  18.     RES                     =   { guiGetScreenSize() };
  19.     X,Y                     =   RES[1] / NATIVE_RESOLUTION[1], RES[2] / NATIVE_RESOLUTION[2];
  20.     SCALE                   =   ( 1 / NATIVE_RESOLUTION[1] ) * RES[1];
  21. end
  22.  
  23. --=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Core - functions
  24.  
  25. function dxGrid:Create ( x, y, width, height, postGUI )
  26.     -- table dxGrid:Create ( int x, int y, int width, int height[, bool postGUI ] )
  27.  
  28.     if __checkParams ( "Create", "nnnn", x, y, width, height ) then
  29.         local data = {
  30.             x       =       FIT_MODE and ( x * X ) or x;                                -- X position
  31.             y       =       FIT_MODE and ( y * Y ) or y;                                -- Y position
  32.             w       =       FIT_MODE and ( width * X ) or width;                        -- Width
  33.             h       =       FIT_MODE and ( height * Y ) or height;                      -- Height
  34.             pg      =       postGUI or false;                                           -- PostGUI
  35.             i       =       {};                                                         -- Items
  36.             mi      =       __calcMaxItems ( FIT_MODE and ( height * Y ) or height );   -- Max items
  37.             s       =       1;                                                          -- Scroll Level
  38.             r       =       -1;                                                         -- Row count
  39.             se      =       -1;                                                         -- Selected item
  40.             mo      =       nil;                                                        -- Mouse-on item
  41.             vis     =       true                                                        -- Visible
  42.         };
  43.  
  44.         setmetatable ( data, { __index = dxGrid } );
  45.         table.insert ( dxGrid.items, data );
  46.  
  47.         return data;
  48.     end
  49. end
  50.  
  51. function dxGrid:Destroy ()
  52.     -- bool dxGrid:Destroy ()
  53.  
  54.     for k, v in pairs ( dxGrid.items ) do
  55.         if v == self then
  56.             dxGrid.items[k] = nil;
  57.             return true;
  58.         end
  59.     end
  60.     return false;
  61. end
  62.  
  63. function dxGrid:SetVisible ( visible )
  64.     -- bool Gridlist:SetVisible ( bool state )
  65.    
  66.     if __checkParams ( "SetVisible", "b", visible ) then
  67.         self.vis = visible
  68.  
  69.         return true
  70.     else
  71.         return false
  72.     end
  73. end
  74.  
  75. function dxGrid:IsVisible ( )
  76.     -- bool Gridlist:IsVisible()
  77.  
  78.     return self.vis
  79. end
  80.  
  81. function dxGrid:AddColumn ( title, width )
  82.     -- int Gridlist:AddColumn ( string title, int width )
  83.  
  84.     if __checkParams ( "AddColumn", "sn", title, width ) then
  85.         local data = {
  86.             info    =   { title = title, width = FIT_MODE and ( width * X ) or width }
  87.         };
  88.  
  89.         table.insert ( self.i, data );
  90.  
  91.         return #self.i;
  92.     end
  93. end
  94.  
  95. function dxGrid:RemoveColumn ( columnIndex )
  96.     -- bool Gridlist:RemoveColumn ( int columnIndex )
  97.  
  98.     if __checkParams ( "RemoveColumn", "n", columnIndex ) then
  99.         self.i[columnIndex] = nil;
  100.  
  101.         -- Recalculate the highest item count
  102.         local highest = -1;
  103.  
  104.         for _, v in ipairs ( self.i ) do
  105.             if #v > highest then
  106.                 highest = ( #v - 1 );
  107.             end
  108.         end
  109.  
  110.         self.r = highest;
  111.  
  112.         -- Recalculate the scroll level (if necessary)
  113.         if ( ( ( self.s + self.mi ) - 2 ) == self.r ) then
  114.             self.s = ( self.r - self.mi ) + 1;
  115.         end
  116.  
  117.         return true
  118.     end
  119.     return false
  120. end
  121.  
  122. function dxGrid:GetColumnCount ()
  123.     -- int Gridlist:GetColumnCount()
  124.  
  125.     return #self.i
  126. end
  127.  
  128. function dxGrid:AddItem ( columnIndex, text, data, r, g, b )
  129.     -- int Gridlist:AddItem ( int columnIndex, string title[, mixed data ] )
  130.  
  131.     if __checkParams ( "AddItem", "ns", columnIndex, text ) then
  132.         if self.i[columnIndex] then
  133.             local tColor = __checkRGB ( r, g, b ) and { r, g, b } or { 255, 255, 255 };
  134.            
  135.             table.insert ( self.i[columnIndex], { id = #self.i[columnIndex] + 1, text = tostring( text ), data = data, color = tColor } );
  136.  
  137.             if #self.i[columnIndex] > self.r then
  138.                 self.r = #self.i[columnIndex];
  139.             end
  140.  
  141.             return #self.i[columnIndex];
  142.         end
  143.         return false;
  144.     end
  145. end
  146.  
  147. function dxGrid:RemoveItem ( column, itemID )
  148.     -- bool Gridlist:RemoveItem ( int columnIndex, int itemIndex )
  149.  
  150.     if __checkParams ( "RemoveItem", "nn", column, itemID ) then
  151.         if self.i[column] and self.i[column][itemID] then
  152.             -- Recalculate the highest item count
  153.             if self.r == #self.i[column] then
  154.                 local highest = -1;
  155.  
  156.                 for _, v in ipairs ( self.i ) do
  157.                     if #v > highest then
  158.                         highest = ( #v - 1 );
  159.                     end
  160.                 end
  161.  
  162.                 self.r = highest;
  163.             end
  164.  
  165.             -- Recalculate the scroll level (if necessary)
  166.             if ( ( ( self.s + self.mi ) - 2 ) == self.r ) then
  167.                 self.s = ( self.r - self.mi ) + 1;
  168.             end
  169.  
  170.             -- Reset the selected item if necessary²
  171.             if itemID == self.se then
  172.                 local newItem   =   self.se - 1
  173.  
  174.                 if newItem <= self.r then
  175.                     self.se = math.max ( 0, newItem );
  176.                 else
  177.                     self.se = -1
  178.                 end
  179.             end
  180.  
  181.             table.remove ( self.i[column], itemID );
  182.  
  183.             return true;
  184.         end
  185.         return false
  186.     end
  187. end
  188.  
  189. function dxGrid:GetItemCount ( columnID )
  190.     -- int Gridlist:GetItemCount ( int columnIndex )
  191.  
  192.     if __checkParams ( "GetItemCount", "n", columnID ) then
  193.         if self.i[columnID] then
  194.             return #self.i[columnID]
  195.         end
  196.         return false
  197.     end
  198. end
  199.  
  200. function dxGrid:Clear ()
  201.     -- bool Gridlist:Clear()
  202.  
  203.     for k, v in ipairs ( self.i ) do
  204.         self.i[k] = { info = v.info }
  205.     end
  206.  
  207.     self.r = -1
  208.     self.se = nil
  209.  
  210.     -- Recalculate the scroll level
  211.     self.s = 1;
  212.  
  213.     return true
  214. end
  215.  
  216. function dxGrid:GetSelectedItem ( )
  217.     -- int Gridlist:GetSelectedItem ()
  218.  
  219.     return self.se;
  220. end
  221.  
  222. function dxGrid:SetSelectedItem ( itemID )
  223.     -- bool Gridlist:SetSelectedItem ( int itemIndex )
  224.  
  225.     if __checkParams ( "SetSelectedItem", "n", itemID ) then
  226.         if itemID <= self.r then
  227.             self.se = itemID;
  228.             return self.se == itemID;
  229.         end
  230.         return false;
  231.     end
  232. end
  233.  
  234. function dxGrid:GetItemDetails ( column, itemID )
  235.     -- string, mixed Gridlist:GetItemDetails ( int columnIndex, int itemIndex )
  236.  
  237.     if __checkParams ( "GetItemDetails", "nn", columnID, itemID ) then
  238.         if self.i[column] then
  239.             if self.i[column][itemID] then
  240.                 return self.i[column][itemID].text, self.i[column][itemID].data
  241.             end
  242.         end
  243.         return false
  244.     end
  245. end
  246.  
  247. --=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Core - render/move
  248.  
  249. addEventHandler ( "onClientRender", root,
  250.     function ( )
  251.         -- Is there any gridlist to render?
  252.         if #dxGrid.items > 0 then
  253.             -- Loop through all grid lists
  254.             for index, data in ipairs ( dxGrid.items ) do
  255.                 -- Is the gridlist visible?
  256.                 if data.vis then
  257.                     -- Draw the 'gridlist' itself
  258.                     dxDrawRectangle ( data.x, data.y, data.w, data.h, tocolor ( 18, 20, 20, 255 ), data.pg );  -- 38, 40, 40, 255
  259.  
  260.                     -- Draw the column bar
  261.                     dxDrawRectangle ( data.x, data.y, data.w, 30 % data.h, tocolor ( 217, 45, 45, 255 ), data.pg );
  262.  
  263.                     -- Set cursorOn variable to the current gridlist, if it's selected
  264.                     cursorOn = nil
  265.                     if __isMouseInPosition ( data.x, data.y, data.w, data.h ) then
  266.                         cursorOn = index;
  267.                     end
  268.  
  269.                     -- Check if there's any selected item
  270.                     local seeFrom   =   data.s;
  271.                     local seeTo     =   ( data.s + data.mi ) - 1;
  272.  
  273.                     if data.se and data.se <= data.r and data.se >= seeFrom and data.se <= seeTo then
  274.                         local index     =   data.se - ( data.s - 1 );
  275.                         local y2        =   data.y + ( ( index - 1 ) * 25 );
  276.  
  277.                         -- Draw a rectangle to make it looks like selected
  278.                         dxDrawRectangle ( data.x, ( 30 % data.h ) + y2, data.w, 20, tocolor ( 38, 40, 40, 255 ), data.pg ); --217, 45, 45, 255
  279.                     end
  280.  
  281.                     -- Is there any column?
  282.                     if #data.i > 0 then
  283.                     local cWidth = 0
  284.  
  285.                         -- Loop through all columns
  286.                         for cIndex, cData in ipairs ( data.i ) do
  287.                             -- we'll go beyond the gridlist width with this column ?
  288.                             if ( ( cWidth + cData.info.width ) <= data.w ) then
  289.                                 local x = data.x + cWidth;
  290.  
  291.                                 -- Draw the column title
  292.                                 dxDrawText ( cData.info.title, x, data.y, cData.info.width + x, ( 30 % data.h ) + data.y, tocolor ( 255, 255, 255 ), FIT_MODE and ( 1.5 * SCALE ) or 1.5, "default-bold", "center", "center", true, true, data.pg, false, true );
  293.  
  294.                                 -- Reset the selected item
  295.                                 cData.info.selected = -1;
  296.  
  297.                                 -- Is there any item ?
  298.                                 if #cData > 0 then
  299.                                     local seeFrom   =   data.s;
  300.                                     local seeTo     =   ( data.s + data.mi ) - 1;
  301.  
  302.                                     -- Loop the items
  303.                                     for iIndex = seeFrom, seeTo do
  304.                                         -- There's a row with this index in the current column?
  305.                                         if cData[iIndex] then
  306.                                             local index     =   iIndex - ( data.s - 1 );
  307.                                             local y         =   data.y + ( index * 25 );
  308.                                             local y2        =   data.y + ( ( index - 1 ) * 25 );
  309.  
  310.                                             -- Check if cursor is on item position
  311.                                             if __isMouseInPosition ( data.x, ( 30 % data.h ) + y2, data.w, 20 ) then
  312.                                                 -- Define the mouse-on variable
  313.                                                 data.mo = iIndex;
  314.                                             end
  315.            
  316.                                             -- Draw the item text
  317.                                             dxDrawText ( ""..cData[iIndex]["text"], x, y, cData.info.width + x, ( 30 % data.h ) + y, tocolor ( unpack ( cData[iIndex]["color"] ) ), FIT_MODE and ( 1.25 * SCALE ) or 1.25, "default-bold", "left", "center", true, false, data.pg, false, true );
  318.                                         end
  319.                                     end
  320.                                 end
  321.  
  322.                                 -- Increase cWidth variable (to draw the columns correctly)
  323.                                 cWidth = cWidth + cData.info.width;
  324.                             end
  325.                         end
  326.                     end
  327.                 end
  328.             end
  329.         end
  330.     end
  331. , true, "low-5")
  332.  
  333. --
  334.  
  335. addEventHandler ( "onClientKey", root,
  336.     function ( button, press )
  337.         -- Is cursor showing?
  338.         if isCursorShowing () then
  339.             -- Is there any gridlist?
  340.             if #dxGrid.items > 0 then
  341.                 -- Is there any selected gridlist?
  342.                 if cursorOn then
  343.                     -- We pressed the scroll?
  344.                     if press and #button > 6 then
  345.                         -- Does the gridlist requires scroll?
  346.                         if dxGrid.items[cursorOn].r > dxGrid.items[cursorOn].mi then
  347.                             -- Define some variables
  348.                             local index         =   cursorOn;
  349.                             local currentValue  =   dxGrid.items[index].s;
  350.                             local newValue      =   math.max ( 1, button == "mouse_wheel_down" and currentValue + 2 or currentValue -1 );
  351.  
  352.                             -- Check if we have spent the row's limit with the new value
  353.                             if ( ( newValue + dxGrid.items[index].mi ) > dxGrid.items[index].r ) then
  354.                                 newValue = ( dxGrid.items[index].r - dxGrid.items[index].mi ) + 1;
  355.                             end
  356.  
  357.                             -- Set the new scroll level
  358.                             dxGrid.items[index].s = newValue;
  359.                         end
  360.                     elseif press and button == "mouse1" and dxGrid.items[cursorOn].mo then
  361.                         dxGrid.items[cursorOn].se = dxGrid.items[cursorOn].mo;
  362.                     end
  363.                 end
  364.             end
  365.         end
  366.     end
  367. )
  368.  
  369. --=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Useful
  370.  
  371. function __calcMaxItems ( height )
  372.     for i = 0, 9999 do
  373.         if ( ( ( i + 1 ) * 25 ) >= math.floor ( height ) ) then
  374.             return ( ( ( i + 1 ) * 25 ) > math.floor ( height ) and ( i - 1 ) or i );
  375.         end
  376.     end
  377.     return false;
  378. end
  379.  
  380. function __checkParams ( methodName, pattern, ... )
  381.     local cTable = {
  382.         ["string"] = "s";
  383.         ["number"] = "n";
  384.         ["boolean"] = "b";
  385.  
  386.         ["s"] = "string";
  387.         ["n"] = "number";
  388.         ["b"] = "boolean"
  389.     };
  390.  
  391.     if #pattern > table.maxn ( { ... } ) then
  392.         local index = table.maxn ( { ... } ) == 0 and 1 or table.maxn ( { ... } ) + 1
  393.         return false, error ( "Bad Argument @ '"..methodName.."' [Expected "..cTable[ pattern:sub ( index, index ) ].." at argument "..index..", got none]" )
  394.     end
  395.  
  396.     for k, v in pairs ( { ... } ) do
  397.         if cTable[ type ( v ) ] ~= pattern:sub ( k, k ) then
  398.             return false, error ( "Bad Argument @ '"..methodName.."' [Expected "..cTable[ pattern:sub ( k, k ) ].." at argument "..k..", got "..( type ( v ) or "none" ).."]" )
  399.         end
  400.     end
  401.     return true;
  402. end
  403.  
  404. function __checkRGB ( r, g, b )
  405.     -- Check if all parameters were passed
  406.     if ( not r ) or ( not g ) or ( not b ) then
  407.         return false;
  408.     end
  409.    
  410.     for _, v in ipairs ( { r, g, b } ) do
  411.         if ( type ( v ) ~= "number" ) or ( v < 0 ) or ( v > 255 ) then
  412.             return false;
  413.         end
  414.     end
  415.    
  416.     return true;
  417. end
  418.  
  419. function __isMouseInPosition ( x, y, w, h )
  420.     if not isCursorShowing() then return false end
  421.  
  422.     local res   =   { guiGetScreenSize() };
  423.     local cpos  =   { getCursorPosition() };
  424.     local fpos  =   { res[1] * cpos[1], res[2] * cpos[2] };
  425.     return ( fpos[1] >= x and fpos[1] <= x + w ) and ( fpos[2] >= y and fpos[2] <= y + h )
  426. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement