Rune lib and __concat for variable libs.
This commit is contained in:
@@ -41,6 +41,9 @@ Color.meta = {
|
||||
and math.floor( c1.b ) == math.floor( c2.b )
|
||||
and math.floor( c1.a ) == math.floor( c2.a )
|
||||
end,
|
||||
__concat = function( a, b )
|
||||
return tostring( a )..tostring( b )
|
||||
end,
|
||||
}
|
||||
|
||||
function Color:new( r, g, b, a )
|
||||
|
||||
@@ -41,7 +41,10 @@ Matrix.meta = {
|
||||
end,
|
||||
__mul = function( m1, m2 )
|
||||
return Matrix:new( RL.MatrixMultiply( m1, m2 ) )
|
||||
end
|
||||
end,
|
||||
__concat = function( a, b )
|
||||
return tostring( a )..tostring( b )
|
||||
end,
|
||||
}
|
||||
|
||||
function Matrix:new( m )
|
||||
|
||||
@@ -33,6 +33,9 @@ Quaternion.meta = {
|
||||
__eq = function( q1, q2 )
|
||||
return RL.QuaternionEquals( q1, q2 ) == 1
|
||||
end,
|
||||
__concat = function( a, b )
|
||||
return tostring( a )..tostring( b )
|
||||
end,
|
||||
}
|
||||
|
||||
function Quaternion:new( x, y, z, w )
|
||||
|
||||
@@ -38,6 +38,9 @@ Rectangle.meta = {
|
||||
__eq = function( r1, r2 )
|
||||
return RL.Vector2Equals( { r1.x, r1.y }, { r2.x, r2.y } ) and RL.Vector2Equals( { r1.width, r1.height }, { r2.width, r2.height } )
|
||||
end,
|
||||
__concat = function( a, b )
|
||||
return tostring( a )..tostring( b )
|
||||
end,
|
||||
}
|
||||
|
||||
function Rectangle:new( x, y, width, height )
|
||||
|
||||
97
examples/resources/lib/rune.lua
Normal file
97
examples/resources/lib/rune.lua
Normal file
@@ -0,0 +1,97 @@
|
||||
-- For luaJit compatibility.
|
||||
if table.unpack == nil then
|
||||
table.unpack = unpack
|
||||
end
|
||||
|
||||
local Rune = {}
|
||||
Rune.meta = {
|
||||
__index = Rune,
|
||||
__tostring = function( r )
|
||||
return r.string
|
||||
end,
|
||||
__len = function( r )
|
||||
return RL.GetCodepointCount( r.string )
|
||||
end,
|
||||
__eq = function( r1, r2 )
|
||||
return r1.string == r2.string
|
||||
end,
|
||||
__concat = function( a, b )
|
||||
return tostring( a )..tostring( b )
|
||||
end,
|
||||
}
|
||||
|
||||
function Rune:new( string )
|
||||
if type( string ) == "table" then
|
||||
string = RL.LoadUTF8( string )
|
||||
elseif type( string ) == "nil" then
|
||||
string = ""
|
||||
end
|
||||
|
||||
local object = setmetatable( {}, Rune.meta )
|
||||
|
||||
object.string = string
|
||||
|
||||
return object
|
||||
end
|
||||
|
||||
function Rune:set( string )
|
||||
if type( string ) == "table" then
|
||||
string = RL.LoadUTF8( string )
|
||||
elseif type( string ) == "nil" then
|
||||
string = ""
|
||||
end
|
||||
|
||||
self.string = string
|
||||
end
|
||||
|
||||
function Rune:clone()
|
||||
return Rune:new( self.string )
|
||||
end
|
||||
|
||||
function Rune:len()
|
||||
return RL.GetCodepointCount( self.string )
|
||||
end
|
||||
|
||||
function Rune:getCodepoints()
|
||||
return RL.LoadCodepoints( self.string )
|
||||
end
|
||||
|
||||
function Rune:getCodepoint( index )
|
||||
local codepoint = RL.GetCodepoint( self:sub( index, index ) )
|
||||
return codepoint
|
||||
end
|
||||
|
||||
function Rune:getCodepointSize( index )
|
||||
local _, codepointSize = RL.GetCodepoint( self:sub( index, index ) )
|
||||
return codepointSize
|
||||
end
|
||||
|
||||
function Rune:insert( pos, string )
|
||||
local codepoints = self:getCodepoints()
|
||||
|
||||
for i, codepoint in ipairs( RL.LoadCodepoints( string ) ) do
|
||||
table.insert( codepoints, pos + i - 1, codepoint )
|
||||
end
|
||||
self.string = RL.LoadUTF8( codepoints )
|
||||
end
|
||||
|
||||
function Rune:sub( i, j )
|
||||
local codepoints = self:getCodepoints()
|
||||
return RL.LoadUTF8( { table.unpack( codepoints, i, j ) } )
|
||||
end
|
||||
|
||||
function Rune:gsub( pattern, repl )
|
||||
return string.gsub( self.string, pattern, repl )
|
||||
end
|
||||
|
||||
function Rune:split( delimiter )
|
||||
local splits = {}
|
||||
|
||||
for str in string.gmatch( self.string, "([^"..delimiter.."]+)" ) do
|
||||
table.insert( splits, str )
|
||||
end
|
||||
|
||||
return splits
|
||||
end
|
||||
|
||||
return Rune
|
||||
@@ -83,14 +83,14 @@ function utillib.tableLen( t )
|
||||
return count
|
||||
end
|
||||
|
||||
function utillib.split( str, sep )
|
||||
function utillib.split( string, sep )
|
||||
if sep == nil then
|
||||
sep = "%s"
|
||||
end
|
||||
|
||||
local t = {}
|
||||
|
||||
for str in string.gmatch( str, "([^"..sep.."]+)" ) do
|
||||
for str in string.gmatch( string, "([^"..sep.."]+)" ) do
|
||||
table.insert( t, str )
|
||||
end
|
||||
|
||||
|
||||
@@ -36,6 +36,9 @@ Vector2.meta = {
|
||||
__eq = function( v1, v2 )
|
||||
return RL.Vector2Equals( v1, v2 ) == 1
|
||||
end,
|
||||
__concat = function( a, b )
|
||||
return tostring( a )..tostring( b )
|
||||
end,
|
||||
}
|
||||
|
||||
function Vector2:new( x, y )
|
||||
|
||||
@@ -38,6 +38,9 @@ Vector3.meta = {
|
||||
__eq = function( v1, v2 )
|
||||
return RL.Vector3Equals( v1, v2 ) == 1
|
||||
end,
|
||||
__concat = function( a, b )
|
||||
return tostring( a )..tostring( b )
|
||||
end,
|
||||
}
|
||||
|
||||
function Vector3:new( x, y, z )
|
||||
|
||||
Reference in New Issue
Block a user