diff options
| author | jussi | 2023-11-10 00:18:34 +0200 |
|---|---|---|
| committer | jussi | 2023-11-10 00:18:34 +0200 |
| commit | e06d98e0ed820c64df99c451e2c24a481d7349e8 (patch) | |
| tree | dbc82c4c16495123eb0ac0d4bfb0d08b0ea96de4 /examples/resources | |
| parent | 8effd050ffb736df2124da00227d8b2843d298f6 (diff) | |
| download | reilua-enhanced-e06d98e0ed820c64df99c451e2c24a481d7349e8.tar.gz reilua-enhanced-e06d98e0ed820c64df99c451e2c24a481d7349e8.tar.bz2 reilua-enhanced-e06d98e0ed820c64df99c451e2c24a481d7349e8.zip | |
More color and rectangle lib functionality.
Diffstat (limited to 'examples/resources')
| -rw-r--r-- | examples/resources/lib/color.lua | 109 | ||||
| -rw-r--r-- | examples/resources/lib/rectangle.lua | 112 | ||||
| -rw-r--r-- | examples/resources/lib/vector2.lua | 10 | ||||
| -rw-r--r-- | examples/resources/lib/vector3.lua | 10 |
4 files changed, 160 insertions, 81 deletions
diff --git a/examples/resources/lib/color.lua b/examples/resources/lib/color.lua index a7bec55..5f868b6 100644 --- a/examples/resources/lib/color.lua +++ b/examples/resources/lib/color.lua @@ -1,35 +1,40 @@ +-- For luaJit compatibility. +if table.unpack == nil then + table.unpack = unpack +end + Color = {} Color.meta = { __index = Color, - __tostring = function( r ) - return "{"..tostring( r.r )..", "..tostring( r.g )..", "..tostring( r.b )..", "..tostring( r.a ).."}" + __tostring = function( c ) + return "{"..math.floor( c.r )..", "..math.floor( c.g )..", "..math.floor( c.b )..", "..math.floor( c.a ).."}" + end, + __add = function( c1, c2 ) + return Color:new( c1.r + c2.r, c1.g + c2.g, c1.b + c2.b, c1.a + c2.a ) + end, + __sub = function( c1, c2 ) + return Color:new( c1.r - c2.r, c1.g - c2.g, c1.b - c2.b, c1.a - c2.a ) + end, + __mul = function( c1, c2 ) + return Color:new( c1.r * c2.r, c1.g * c2.g, c1.b * c2.b, c1.a * c2.a ) + end, + __div = function( c1, c2 ) + return Color:new( c1.r / c2.r, c1.g / c2.g, c1.b / c2.b, c1.a / c2.a ) + end, + __mod = function( c, v ) + return Color:new( c.r % v, c.g % v, c.b % v, c.a % v ) + end, + __pow = function( c, v ) + return Color:new( c.r ^ v, c.g ^ v, c.b ^ v, c.a ^ v ) + end, + __idiv = function( c, v ) + return Color:new( c.r // v, c.g // v, c.b // v, c.a // v ) + end, + __len = function( _ ) + return 4 end, - -- __add = function( v1, v2 ) - -- return Vector2:new( v1.x + v2.x, v1.y + v2.y ) - -- end, - -- __sub = function( v1, v2 ) - -- return Vector2:new( v1.x - v2.x, v1.y - v2.y ) - -- end, - -- __mul = function( v1, v2 ) - -- return Vector2:new( v1.x * v2.x, v1.y * v2.y ) - -- end, - -- __div = function( v1, v2 ) - -- return Vector2:new( v1.x / v2.x, v1.y / v2.y ) - -- end, - -- __mod = function( v, value ) - -- return Vector2:new( math.fmod( v.x, value ), math.fmod( v.y, value ) ) - -- end, - -- __pow = function( v, value ) - -- return Vector2:new( v.x ^ value, v.y ^ value ) - -- end, - -- __unm = function( v ) - -- return Vector2:new( -v.x, -v.y ) - -- end, - -- __idiv = function( v, value ) - -- return Vector2:new( v.x // value, v.y // value ) - -- end, __eq = function( c1, c2 ) - return c1.r == c2.r and c1.g == c2.g and c1.b == c2.b + return c1.r == c2.r and c1.g == c2.g and c1.b == c2.b and c1.a == c2.a end, } @@ -80,7 +85,55 @@ function Color:unpack() end function Color:clone() - return Color:new( self.r, self.g, self.b, self.a ) + return Color:new( self ) +end + +function Color:scale( scalar ) + return Color:new( math.floor( self.r * scalar ), math.floor( self.g * scalar ), math.floor( self.b * scalar ), math.floor( self.a * scalar ) ) +end + +function Color:fade( alpha ) + return Color:new( RL.Fade( self, alpha ) ) +end + +function Color:toHex() + return RL.ColorToInt( self ) +end + +function Color:fromHex( hexValue ) + return Color:new( RL.GetColor( hexValue ) ) +end + +function Color:getNormalized() + return RL.ColorNormalize( self ) +end + +function Color:fromNormalized( normalized ) + return Color:new( RL.ColorFromNormalized( normalized ) ) +end + +function Color:toHSV() + return Vector3:new( RL.ColorToHSV( self ) ) +end + +function Color:fromHSV( hue, saturation, value ) + return Color:new( RL.ColorFromHSV( hue, saturation, value ) ) +end + +function Color:tint( tint ) + return Color:new( RL.ColorTint( self, tint ) ) +end + +function Color:brightness( factor ) + return Color:new( RL.ColorBrightness( self, factor ) ) +end + +function Color:contrast( contrast ) + return Color:new( RL.ColorContrast( self, contrast ) ) +end + +function Color:alphaBlend( dst, src, tint ) + return Color:new( RL.ColorAlphaBlend( dst, src, tint ) ) end return Color diff --git a/examples/resources/lib/rectangle.lua b/examples/resources/lib/rectangle.lua index 3c0adf5..0b84b54 100644 --- a/examples/resources/lib/rectangle.lua +++ b/examples/resources/lib/rectangle.lua @@ -1,47 +1,45 @@ +-- For luaJit compatibility. +if table.unpack == nil then + table.unpack = unpack +end + Rectangle = {} --- Rectangle.TYPE = "Rectangle" Rectangle.meta = { __index = Rectangle, __tostring = function( r ) return "{"..tostring( r.x )..", "..tostring( r.y )..", "..tostring( r.width )..", "..tostring( r.height ).."}" end, - -- __add = function( v1, v2 ) - -- return Vector2:new( v1.x + v2.x, v1.y + v2.y ) - -- end, - -- __sub = function( v1, v2 ) - -- return Vector2:new( v1.x - v2.x, v1.y - v2.y ) - -- end, - -- __mul = function( v1, v2 ) - -- return Vector2:new( v1.x * v2.x, v1.y * v2.y ) - -- end, - -- __div = function( v1, v2 ) - -- return Vector2:new( v1.x / v2.x, v1.y / v2.y ) - -- end, - -- __mod = function( v, value ) - -- return Vector2:new( math.fmod( v.x, value ), math.fmod( v.y, value ) ) - -- end, - -- __pow = function( v, value ) - -- return Vector2:new( v.x ^ value, v.y ^ value ) - -- end, - -- __unm = function( v ) - -- return Vector2:new( -v.x, -v.y ) - -- end, - -- __idiv = function( v, value ) - -- return Vector2:new( v.x // value, v.y // value ) - -- end, - -- __len = function( v ) - -- local len = 0 - - -- for _, _ in pairs( v ) do - -- len = len + 1 - -- end - - -- return len - -- end, - -- __eq = function( v1, v2 ) - -- return v1.x == v2.x and v1.y == v2.y - -- end, + __add = function( r1, r2 ) + return Rectangle:new( r1.x + r2.x, r1.y + r2.y, r1.width + r2.width, r1.height + r2.height ) + end, + __sub = function( r1, r2 ) + return Rectangle:new( r1.x - r2.x, r1.y - r2.y, r1.width - r2.width, r1.height - r2.height ) + end, + __mul = function( r1, r2 ) + return Rectangle:new( r1.x * r2.x, r1.y * r2.y, r1.width * r2.width, r1.height * r2.height ) + end, + __div = function( r1, r2 ) + return Rectangle:new( r1.x / r2.x, r1.y / r2.y, r1.width / r2.width, r1.height / r2.height ) + end, + __mod = function( r, v ) + return Rectangle:new( math.fmod( r.x, v ), math.fmod( r.y, v ), math.fmod( r.width, v ), math.fmod( r.height, v ) ) + end, + __pow = function( r, v ) + return Rectangle:new( r.x ^ v, r.y ^ v, r.width ^ v, r.height ^ v ) + end, + __unm = function( r ) + return Rectangle:new( -r.x, -r.y, -r.width, -r.height ) + end, + __idiv = function( r, v ) + return Rectangle:new( r.x // v, r.y // v, r.width // v, r.height // v ) + end, + __len = function( _ ) + return 4 + end, + __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, } function Rectangle:new( x, y, width, height ) @@ -86,4 +84,44 @@ function Rectangle:clone() return Rectangle:new( self.x, self.y, self.width, self.height ) end +function Rectangle:scale( scalar ) + return Rectangle:new( self.x, self.y, self.width * scalar, self.height * scalar ) +end + +function Rectangle:min( rec ) + return Rectangle:new( self.x, self.y, math.min( self.width, rec.width ), math.min( self.height, rec.height ) ) +end + +function Rectangle:max( rec ) + return Rectangle:new( self.x, self.y, math.max( self.width, rec.width ), math.max( self.height, rec.height ) ) +end + +function Rectangle:floor() + return Rectangle:new( math.floor( self.x ), math.floor( self.y ), math.floor( self.width ), math.floor( self.height ) ) +end + +function Rectangle:ceil() + return Rectangle:new( math.ceil( self.x ), math.ceil( self.y ), math.ceil( self.width ), math.ceil( self.height ) ) +end + +function Rectangle:area() + return self.width * self.height +end + +function Rectangle:checkCollisionRec( rec ) + return RL.CheckCollisionRecs( self, rec ) +end + +function Rectangle:checkCollisionCircle( center, radius ) + return RL.CheckCollisionCircleRec( center, radius, self ) +end + +function Rectangle:checkCollisionPoint( point ) + return RL.CheckCollisionPointRec( point, self ) +end + +function Rectangle:getCollisionRec( rec ) + return Rectangle:new( RL.GetCollisionRec( self, rec ) ) +end + return Rectangle diff --git a/examples/resources/lib/vector2.lua b/examples/resources/lib/vector2.lua index 8f9ad1d..b50ac1b 100644 --- a/examples/resources/lib/vector2.lua +++ b/examples/resources/lib/vector2.lua @@ -30,14 +30,8 @@ Vector2.meta = { __unm = function( v ) return Vector2:new( -v.x, -v.y ) end, - __len = function( v ) - local len = 0 - - for _, _ in pairs( v ) do - len = len + 1 - end - - return len + __len = function( _ ) + return 2 end, __eq = function( v1, v2 ) return RL.Vector2Equals( v1, v2 ) == 1 diff --git a/examples/resources/lib/vector3.lua b/examples/resources/lib/vector3.lua index 9e3ceb1..725a38d 100644 --- a/examples/resources/lib/vector3.lua +++ b/examples/resources/lib/vector3.lua @@ -30,14 +30,8 @@ Vector3.meta = { __unm = function( v ) return Vector3:new( -v.x, -v.y, -v.z ) end, - __len = function( v ) - local len = 0 - - for _, _ in pairs( v ) do - len = len + 1 - end - - return len + __len = function( _ ) + return 3 end, __eq = function( v1, v2 ) return RL.Vector3Equals( v1, v2 ) == 1 |
