I originally made this for a WIP gamemode I am working on, but I figured others may want to use this for their projects as well, rather than creating the same functions from scratch.
The source code, along with (pretty much the copy of what's written here) some documentation is available at library's GitHub repo.
[B]GITHUB: [/B][URL]https://github.com/MrMe0w/fl-util[/URL]
More documentation can be found in the code itself.
[code]
-- A function to get lowercase type of an object.
function typeof(obj)
-- A nicer wrapper for pcall.
function Try(id, func, ...)
-- An even nicer wrapper for pcall.
function try(tab)
function catch(handler)
-- A function to check whether character is vowel or not.
function util.IsVowel(char)
-- A function to remove a substring from the end of the string.
function string.RemoveTextFromEnd(str, strNeedle, bAllOccurences)
-- A function to remove a substring from the beginning of the string.
function string.RemoveTextFromStart(str, strNeedle, bAllOccurences)
-- A function to check whether all of the arguments in vararg are valid (via IsValid).
function util.Validate(...)
-- A function to include a file based on it's prefix.
function util.Include(strFile)
-- A function to include all files in a directory.
function util.IncludeDirectory(strDirectory, strBase, bIsRecursive)
-- A function to get a material. It caches the material automatically.
function util.GetMaterial(mat)
-- A function to convert a single hexadecimal digit to decimal.
function util.HexToDec(hex)
-- A function to convert hexadecimal number to decimal.
function util.HexToDecimal(hex)
-- A function to convert hexadecimal color to a color structure.
function util.HexToColor(hex)
-- A function to do C-style formatted prints.
function printf(str, ...)
-- A function to select a random player.
function player.Random()
-- A function to find player based on their name or steamID.
function player.Find(name, bCaseSensitive)
-- A function to check whether the string is full uppercase or not.
function string.IsUppercase(str)
-- A function to check whether the string is full lowercase or not.
function string.IsLowercase(str)
-- A function to find all occurences of a substring in a string.
function string.FindAll(str, pattern)
-- A function to check if string is command or not.
function string.IsCommand(str)
-- Strips the string of ID-unfriendly characters and converts it to lowercase.
function string.MakeID(str)
-- Nice wrapper for surface.GetTextSize.
function util.GetTextSize(text, font)
function util.GetTextWidth(text, font)
function util.GetTextHeight(text, font)
function util.GetFontSize(font)
function util.GetFontHeight(font)
function util.GetPanelClass(panel)
-- Adjusts x, y to fit inside x2, y2 while keeping original aspect ratio.
function util.FitToAspect(x, y, x2, y2)
function util.ToBool(value)
function util.CubicEaseIn(curStep, steps, from, to)
function util.CubicEaseOut(curStep, steps, from, to)
function util.CubicEaseInTable(steps, from, to)
function util.CubicEaseOutTable(steps, from, to)
function util.CubicEaseInOut(curStep, steps, from, to)
function util.CubicEaseInOutTable(steps, from, to)
function util.WaitForEntity(entIndex, callback, delay, waitTime)
-- A function to determine whether vector from A to B intersects with a
-- vector from C to D.
function util.VectorsIntersect(vFrom, vTo, vFrom2, vTo2)
-- A function to determine whether a 2D point is inside of a 2D polygon.
function util.VectorIsInPoly(point, polyVertices)
-- A safer way to merge two tables.
function table.SafeMerge(to, from)
-- A function to convers vararg to a string list.
function util.ListToString(callback, separator, ...)
-- A function to check whether a string is a number.
function string.IsNumber(char)
-- A function to count character in a string.
function string.CountCharacter(str, char)
-- INTERNAL: used to remove all newlines from table that is passed to BuildTableFromString.
function util.SmartRemoveNewlines(str)
-- A function to build a table from string.
-- It has /almost/ the same syntax as Lua tables,
-- except key-values ONLY work like this {key = "value"},
-- so, NO {["key"] = "value"} or {[1] = value}, THOSE WON'T WORK.
-- This supports tables-inside-tables structure.
function util.BuildTableFromString(str)
-- A function to 'flatten' a numeric-indexed table (eliminate all tables-inside-tables).
function table.Flatten(tTable, tResult)
[/code]
It also adds the following functionality to Color meta table:
[code]
-- If the first argument is a hexadecimal number (as a string) - it will convert it to color.
-- If the first argument is color's name (E.G. "red") - it will convert it to color.
-- Otherwise works exactly like old Color()
function Color(r, g, b, a)
-- Darkens the original color and returns a copy of it.
function Color:Darken(nAmount)
-- Lightens the original color and returns a copy of it.
function Color:Lighten(nAmount)
[/code]
Good that you shared it for others, but a few things I want to say:
- For the player.Find function I wouldn't recommend combining the search for SteamID and Name, could be exploited (A console command wants to send credits to SteamID or name that was in the input, I input a SteamID, but some dude put that SteamID as the name and luckily is in the player list before the real one and he gets the credits)
- Again for the player.Find function, it doesn't care about multiple people of same name, if I want to send my money in chat to "Bob Somename", I type in gimemoonies 10 Bob but if there is another "Bob Greenname" and he is before the other one in the player list, he gets the money, without me getting a warning about there being 2 players with that name.
- Sadly player.Find function again, Player:SteamName() is not a built-in function in Gmod(It's DarkRP right?) and will error if it's not DarkRP.
- util.GetTextSize Why do another call to the function you are in to return the variables you just set, you already know you set them, why call the function again to check if they are set and then return them? Also the cache should be a boolean argument, caching all of them might not be a good idea, as they will never be removed, just pile up with every new string.
[QUOTE=Newjorciks;52211168]Good that you shared it for others, but a few things I want to say:
- For the player.Find function I wouldn't recommend combining the search for SteamID and Name, could be exploited (A console command wants to send credits to SteamID or name that was in the input, I input a SteamID, but some dude put that SteamID as the name and luckily is in the player list before the real one and he gets the credits)
- Again for the player.Find function, it doesn't care about multiple people of same name, if I want to send my money in chat to "Bob Somename", I type in gimemoonies 10 Bob but if there is another "Bob Greenname" and he is before the other one in the player list, he gets the money, without me getting a warning about there being 2 players with that name.
- Sadly player.Find function again, Player:SteamName() is not a built-in function in Gmod(It's DarkRP right?) and will error if it's not DarkRP.
- util.GetTextSize Why do another call to the function you are in to return the variables you just set, you already know you set them, why call the function again to check if they are set and then return them? Also the cache should be a boolean argument, caching all of them might not be a good idea, as they will never be removed, just pile up with every new string.[/QUOTE]
Thanks for the heads up! I just pushed a fix that addresses the issues you outlined.
As for player:SteamName(), it's actually a thing that got carried over from my gamemode, here's how the detour looks: [url]http://c2n.me/3KhIfIJ.png[/url]
Thanks for the release but maybe separate the code into more files with names specific to their purpose. Its just not very nice having an 800 line file.
[QUOTE=F14;52214647]Thanks for the release but maybe separate the code into more files with names specific to their purpose. Its just not very nice having an 800 line file.[/QUOTE]
Thanks for the suggestion! I just pushed an update that separates everything into multiple files based on the general category the functions belong to.
Include flutil/sh_all.lua to load all of the libraries at once.
These look like some pretty useful additions. I like.
You should keep adding to it.
I found this to be pretty useful recently. (format is fucked)
[code]
function table.flatten( tbl )
local result = {}
local function flatten( tbl )
for _, v in ipairs( tbl ) do
if type( v ) == 'table' then
flatten( v )
else
table.insert( result, v )
end
end
end
flatten( tbl )
return result
end
[/code]
How it works pretty much
[code]
table.flatten( { 'hi', 'im', { 'in', 'a' }, 'table', { 1, 2, { 4, 5, 6 } } } )
// => { 'hi', 'im', 'in', 'a', 'table', 1, 2, 4, 5, 6 }
[/code]
[QUOTE=Ilyaaa;52215777]These look like some pretty useful additions. I like.
You should keep adding to it.
I found this to be pretty useful recently. (format is fucked)
[code]
function table.flatten( tbl )
local result = {}
local function flatten( tbl )
for _, v in ipairs( tbl ) do
if type( v ) == 'table' then
flatten( v )
else
table.insert( result, v )
end
end
end
flatten( tbl )
return result
end
[/code]
How it works pretty much
[code]
table.flatten( { 'hi', 'im', { 'in', 'a' }, 'table', { 1, 2, { 4, 5, 6 } } } )
// => { 'hi', 'im', 'in', 'a', 'table', 1, 2, 4, 5, 6 }
[/code][/QUOTE]
Thanks for your suggestion! I added a function that does pretty much what you described, along with a couple of other functions that I wrote for my gamemode today.
As for updates, this library will probably keep on being updated as I work on my gamemode, however no promises can be made (as in, I'm not sure what else to add to this library). If anyone has any suggestions, feel free to post them in this thread and I'll see if I can add this to the library.
[QUOTE=TheGarry;52214874]Thanks for the suggestion! I just pushed an update that separates everything into multiple files based on the general category the functions belong to.
Include flutil/sh_all.lua to load all of the libraries at once.[/QUOTE]
Very nice! I will definitely be using this now.
If you want, [URL="https://github.com/EmmanuelOga/easing/blob/master/lib/easing.lua"]there's a ton more easings here[/URL] you could steal and shove in your easings file
[editline]12th May 2017[/editline]
[url=http://wiki.garrysmod.com/page/Global/tobool]Also, tobool is already a thing[/url], so you probably don't need that in the util file
[QUOTE=F14;52218080]Very nice! I will definitely be using this now.[/QUOTE]
I'm really glad to hear that, it makes me feel really good knowing that somebody found what I made useful.
[QUOTE=MPan1;52218109]If you want, [URL="https://github.com/EmmanuelOga/easing/blob/master/lib/easing.lua"]there's a ton more easings here[/URL] you could steal and shove in your easings file
[editline]12th May 2017[/editline]
[URL="http://wiki.garrysmod.com/page/Global/tobool"]Also, tobool is already a thing[/URL], so you probably don't need that in the util file[/QUOTE]
That's one hell of a lot of easings, haha. I will see if I can add them to the library. I will also make them all have usage similar to Lerp() function (with a single number from 0 to 1 defining the result value).
As for util.ToBool, I believe there was a reason behind making it, but I am not sure. I will see if it really is useless and remove it if so.
I was going to mention this before - I'm not quite sure if your util.GetFontSize function is useful for anything. It seems a bit odd just checking the length of "abg" - different fonts don't necessarily have those letters as the widest or tallest. Also, it seems pretty easy to mix up that function with util.GetTextSize.
Speaking of which, I can't imagine caching the text making much of a significant performance impact. It just seems odd to me - especially if you are drawing different text each frame - to be adding all that data to a table.
The rest of the code seems fine, it's just those ones that seem strange and out of place
[QUOTE=MPan1;52218283]I was going to mention this before - I'm not quite sure if your util.GetFontSize function is useful for anything. It seems a bit odd just checking the length of "abg" - different fonts don't necessarily have those letters as the widest or tallest. Also, it seems pretty easy to mix up that function with util.GetTextSize.
Speaking of which, I can't imagine caching the text making much of a significant performance impact. It just seems odd to me - especially if you are drawing different text each frame - to be adding all that data to a table.
The rest of the code seems fine, it's just those ones that seem strange and out of place[/QUOTE]
GetFontSize was mainly used to get the height of the font, sorta ghetto, but it worked for what we were using it.
GetTextSize caches the text because in my gamemode there is translation code going on in it (so that it returns the size of the translated text, rather than the phrase) and the performance benefit is quite significant in my case. (My languages system works much like Valve's phrases: draw.SimpleText("#phraseName", "font", 0, 0, color) rather than putting the phrase inside of a function like L("#phraseName"))
I'll check if caching makes much of a difference and remove it if it doesn't.
Sorry, you need to Log In to post a reply to this thread.