Created
September 19, 2012 18:01
-
-
Save daranable/3751139 to your computer and use it in GitHub Desktop.
Object Oriented Starfall Screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--@name OOS Basic Tier | |
--@author Daranable | |
local OOS = OOS; | |
local screen = screen; | |
local render_order = render_order; | |
local object_ids = object_ids; | |
local repaint = OOS.data.repaint; | |
------------------------------------------------------------------------ | |
-- OOS Basic Tier Class -- | |
------------------------------------------------------------------------ | |
-- The basic meta-table for a movable colorable object. | |
local basic_tier = {}; | |
-- Store the class on the main table so other files may access it. | |
OOS.data.classes.basic_tier = basic_tier; | |
------------------------------------------------------------------------ | |
-- Basic Tier Class Methods -- | |
------------------------------------------------------------------------ | |
--- Sets the position on the screen the object should be placed | |
-- @param x number position horizontally on the screen | |
-- @param y number position vertically on the screen | |
function basic_tier:setPos( x, y ) | |
assert( type( x ) == "number", "x must be a number." ) | |
assert( type( y ) == "number", "y must be a number." ) | |
self.posx = x | |
self.posy = y | |
repaint() | |
return self | |
end | |
--- Gets the current position of the object | |
-- @returns xpos, ypos | |
function basic_tier:getPos( ) | |
return self.posx, self.posy | |
end | |
--- Sets the color of an object | |
-- @param r number value of the red color channel | |
-- @param g number value of the green color channel | |
-- @param b number value of the blue color channel | |
-- @param a number value of the alpha channel | |
function basic_tier:setColor( r, g, b, a ) | |
assert( type( r ) == "number", "r must be a number." ) | |
assert( type( g ) == "number", "g must be a number." ) | |
assert( type( b ) == "number", "b must be a number." ) | |
assert( type( a ) == "number", "a must be a number." ) | |
if a == nil then a = 255 end | |
self.r = r | |
self.g = g | |
self.b = b | |
self.a = a | |
repaint() | |
return self | |
end | |
--- Gets the current color of the object | |
-- @returns r, g, b, a the list of color values | |
function basic_tier:getColor( ) | |
return self.color.r, self.color.g, self.color.b, self.color.a | |
end | |
--- Returns the objects current id | |
function basic_tier:getID() | |
return self.id | |
end | |
--- Returns the render position of the object | |
function basic_tier:getRenderPos() | |
return self.placement | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--@name OOS Box Object | |
--@author Daranable | |
local OOS = OOS; | |
local screen = screen; | |
local render_order = render_order; | |
local object_ids = object_ids; | |
local repaint = OOS.data.repaint; | |
local d_color = OOS.data.d_color; | |
------------------------------------------------------------------------ | |
-- OOS Box Object Class -- | |
------------------------------------------------------------------------ | |
-- The meta-table for a sizable object. | |
local sizable = {}; | |
setmetatable( sizable, {__index = OOS.data.classes.basic_tier} ); | |
-- The meta-table for a box object. | |
local box = {}; | |
setmetatable( box, {__index = sizable} ); | |
-- Store the class on the main table so other files may access it. | |
OOS.data.classes.sizable = sizable; | |
------------------------------------------------------------------------ | |
-- Box Paint Method -- | |
------------------------------------------------------------------------ | |
--- This method is an internal method for objects that the library will | |
-- use to paint the object to the screen. This method will error if called | |
-- outside of a render hook. | |
function box:paint() | |
if self.r then | |
screen.setColor( self.r, self.g, self.b, self.a ); | |
end | |
if self.notfilled then | |
screen.drawRectOutline( self.posx, self.posy, self.sizex, self.sizey ); | |
else | |
screen.drawRect( self.posx, self.posy, self.sizex, self.sizey ); | |
end | |
screen.setColor( d_color.r, d_color.g, d_color.b, d_color.a ); | |
end | |
------------------------------------------------------------------------ | |
-- Sizable Class Methods -- | |
------------------------------------------------------------------------ | |
--- Sets the size the object should render as | |
-- @param x number size horizontally on the screen | |
-- @param y number size vertically on the screen | |
function sizable:setSize( x, y ) | |
assert( type( x ) == "number", "x must be a number." ); | |
assert( type( y ) == "number", "y must be a number." ); | |
self.sizex = x; | |
self.sizey = y; | |
repaint(); | |
return self; | |
end | |
--- Retrieves the current size of the object | |
-- @returns x, y the size horizontally and vertically of the object. | |
function sizable:getSize( ) | |
return self.sizex, self.sizey; | |
end | |
------------------------------------------------------------------------ | |
-- Box Class Methods -- | |
------------------------------------------------------------------------ | |
function box:outline( bool ) | |
assert( type( bool ) == "boolean", "bool must be a boolean value." ); | |
self.notfilled = bool; | |
repaint(); | |
return self; | |
end | |
function box:isOutline() | |
return self.notfilled; | |
end | |
------------------------------------------------------------------------ | |
-- Box Create Function -- | |
------------------------------------------------------------------------ | |
--- Creates and returns a box object to you | |
-- | |
function OOS.createBox( ) | |
local object = {} | |
setmetatable( object, {__index = box} ) | |
local id = #object_ids + 1 | |
object_ids[ id ] = object | |
object.id = id | |
local order = #render_order + 1 | |
render_order[ order ] = object | |
object.placement = order | |
object.notfilled = false; | |
return object | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--@name OOS Line Object | |
--@author Daranable | |
local OOS = OOS; | |
local screen = screen; | |
local render_order = render_order; | |
local object_ids = object_ids; | |
local repaint = OOS.data.repaint; | |
local d_color = OOS.data.d_color; | |
------------------------------------------------------------------------ | |
-- OOS Line Object Class -- | |
------------------------------------------------------------------------ | |
-- The meta-table for a line object. | |
local line = {}; | |
setmetatable( line, {__index = OOS.data.classes.basic_tier} ); | |
------------------------------------------------------------------------ | |
-- Line Paint Method -- | |
------------------------------------------------------------------------ | |
--- This method is an internal method for objects that the library will | |
-- use to paint the object to the screen. This method will error if called | |
-- outside of a render hook. | |
function line:paint() | |
if self.r then | |
screen.setColor( self.r, self.g, self.b, self.a ); | |
end | |
if self.size == 1 then | |
screen.drawLine( self.posx, self.posy, self.pos2x, self.pos2y ); | |
else | |
screen.drawTexturedRectRotated( | |
self.x3, | |
self.y3, | |
self.width, | |
self.size, | |
self.angle ) | |
end | |
screen.setColor( d_color.r, d_color.g, d_color.b, d_color.a ); | |
end | |
------------------------------------------------------------------------ | |
-- Text Class Methods -- | |
------------------------------------------------------------------------ | |
--- Internal function to recalculate the angle and height of the line. | |
-- Thanks to Divran for figuring out how to draw a line with thickness. | |
function line:recalc( ) | |
if not self.size or not self.posx then return end | |
if self.size == 1 then repaint(); return end | |
local x = self.posx; | |
local x2 = self.pos2x; | |
local y = self.posy; | |
local y2 = self.pos2y; | |
-- calculate position | |
self.x3 = ( x + x2 ) / 2; | |
self.y3 = ( y + y2 ) / 2; | |
-- calculate height | |
self.width = math.sqrt( ( x2 - x ) ^ 2 + ( y2 - y ) ^ 2 ); | |
-- calculate angle (Thanks to Fizyk) | |
self.angle = math.deg( math.atan2( y - y2, x2 - x ) ) | |
repaint(); | |
end | |
function line:setPoints( x1, y1, x2, y2 ) | |
assert( type( x1 ) == "number", "x1 must be a number." ); | |
assert( type( y1 ) == "number", "y1 must be a number." ); | |
assert( type( x2 ) == "number", "x2 must be a number." ); | |
assert( type( y2 ) == "number", "y2 must be a number." ); | |
self.posx = x1; | |
self.posy = y1; | |
self.pos2x = x2; | |
self.pos2y = y2; | |
self:recalc() | |
return self; | |
end | |
function line:setWidth( size ) | |
assert( type( size ) == "number", "size must be a number." ); | |
self.size = size; | |
self:recalc() | |
return self; | |
end | |
------------------------------------------------------------------------ | |
-- Line Create Function -- | |
------------------------------------------------------------------------ | |
function OOS.createLine( ) | |
local object = {}; | |
setmetatable( object, {__index = line} ); | |
local id = #object_ids + 1; | |
object_ids[ id ] = object; | |
object.id = id; | |
local order = #render_order + 1 ; | |
render_order[ order ] = object; | |
object.placement = order; | |
object.size = 1; | |
return object; | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--@name Object Oriented Screen Library | |
--@author Daranable | |
--@include libs/oos_objects/basic_tier.txt | |
--@include libs/oos_objects/box.txt | |
--@include libs/oos_objects/text.txt | |
--@include libs/oos_objects/line.txt | |
--@include libs/oos_objects/poly.txt | |
------------------------------------------------------------------------ | |
-- Object Oriented Screens Library -- | |
------------------------------------------------------------------------ | |
OOS = {} | |
OOS.data = {} | |
OOS.data.classes = {} | |
render_order = {} | |
object_ids = { } | |
------------------------------------------------------------------------ | |
-- Screen Paint -- | |
------------------------------------------------------------------------ | |
local function paint( ) | |
screen.clear() | |
for i = 1, #render_order do | |
local v = render_order[i] | |
v:paint() | |
end | |
hook( "render", "paint", nil ) | |
end | |
hook( "render", "paint", paint ) | |
function OOS.data.repaint() | |
hook( "render", "paint", paint ) | |
end | |
local d_color = {} | |
d_color.r = 255 | |
d_color.g = 255 | |
d_color.b = 255 | |
d_color.a = 255 | |
OOS.data.d_color = d_color; | |
-- Load Libraries | |
screen = loadLibrary( "render" ) or loadLibrary( "screen" ) | |
-- Run basic_tier file | |
dofile( "libs/oos_objects/basic_tier.txt" ); | |
-- Run box class file | |
dofile( "libs/oos_objects/box.txt" ); | |
-- Run text class file | |
dofile( "libs/oos_objects/text.txt" ); | |
-- Run line class file | |
dofile( "libs/oos_objects/line.txt" ); | |
-- Run poly class file | |
dofile( "libs/oos_objects/poly.txt" ); | |
------------------------------------------------------------------------ | |
-- Object Methods -- | |
------------------------------------------------------------------------ | |
------------------------------------------------------------------------ | |
-- Library Functions -- | |
------------------------------------------------------------------------ | |
function OOS.getObject( id ) | |
return object_ids[ id ] | |
end | |
function OOS.clear() | |
render_order = {} | |
object_ids = {} | |
repaint() | |
end | |
return OOS; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--@name OOS Poly Object | |
--@author Daranable | |
local OOS = OOS; | |
local screen = screen; | |
local render_order = render_order; | |
local object_ids = object_ids; | |
local repaint = OOS.data.repaint; | |
local d_color = OOS.data.d_color; | |
------------------------------------------------------------------------ | |
-- OOS Poly Object Class -- | |
------------------------------------------------------------------------ | |
local poly = {} | |
setmetatable( poly, {__index = OOS.data.classes.basic_tier} ) | |
------------------------------------------------------------------------ | |
-- Poly Paint Method -- | |
------------------------------------------------------------------------ | |
--- This method is an internal method for objects that the library will | |
-- use to paint the object to the screen. This method will error if called | |
-- outside of a render hook. | |
function poly:paint() | |
if self.r then | |
screen.setColor( self.r, self.g, self.b, self.a ) | |
end | |
if self.posx then | |
local matrix = Matrix() | |
matrix:Translate( Vector( self.posx, self.posy, 0 ) ) | |
screen.pushMatrix( matrix ) | |
end | |
screen.drawPoly( self.mesh ) | |
if self.posx then | |
screen.popMatrix( ) | |
end | |
end | |
------------------------------------------------------------------------ | |
-- Poly Class Methods -- | |
------------------------------------------------------------------------ | |
function poly:setPoints( points ) | |
assert( type( points ) == "table", "points must be a table of points" ) | |
assert( type( points[1] ) == "table" and #points[1] == 2, | |
"points must be a table, containing tables of 2 elements" ) | |
local mesh = screen.createPoly( points ) | |
self.mesh = mesh | |
repaint() | |
return self | |
end | |
function poly:setMesh( mesh ) | |
self.mesh = mesh | |
repaint() | |
return self | |
end | |
------------------------------------------------------------------------ | |
-- Poly Create Function -- | |
------------------------------------------------------------------------ | |
function OOS.createPoly( ) | |
local object = {} | |
setmetatable( object, {__index = poly} ) | |
local id = #object_ids + 1 | |
object_ids[ id ] = object | |
object.id = id | |
local order = #render_order + 1 | |
render_order[ order ] = object | |
object.placement = order | |
return object | |
end | |
function OOS.createPolyMesh( points ) | |
assert( type( points ) == "table", "points must be a table of points" ) | |
assert( type( points[1] ) == "table" and #points[1] == 2, | |
"points must be a table, containing tables of 2 elements" ) | |
local mesh = screen.createPoly( points ) | |
return mesh | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--@name OOS Text Object | |
--@author Daranable | |
local OOS = OOS; | |
local screen = screen; | |
local render_order = render_order; | |
local object_ids = object_ids; | |
local repaint = OOS.data.repaint; | |
local d_color = OOS.data.d_color; | |
------------------------------------------------------------------------ | |
-- OOS Text Object Class -- | |
------------------------------------------------------------------------ | |
-- The meta-table for a text object | |
local text = {} | |
setmetatable( text, {__index = OOS.data.classes.basic_tier} ) | |
------------------------------------------------------------------------ | |
-- Text Paint Method -- | |
------------------------------------------------------------------------ | |
--- This method is an internal method for objects that the library will | |
-- use to paint the object to the screen. This method will error if called | |
-- outside of a render hook. | |
function text:paint() | |
if self.r then | |
screen.setColor( self.r, self.g, self.b, self.a ) | |
end | |
screen.drawText( self.font, self.posx, self.posy, self.text ) | |
screen.setColor( d_color.r, d_color.g, d_color.b, d_color.a ) | |
end | |
------------------------------------------------------------------------ | |
-- Text Class Methods -- | |
------------------------------------------------------------------------ | |
function text:setText( text ) | |
assert( type( text ) == "string", "text must be a string" ); | |
self.text = text; | |
repaint(); | |
return self; | |
end | |
function text:setFont( font ) | |
self.font = font | |
return self | |
end | |
------------------------------------------------------------------------ | |
-- Text Create Function -- | |
------------------------------------------------------------------------ | |
function OOS.createText( ) | |
local object = {} | |
setmetatable( object, {__index = text} ) | |
local id = #object_ids + 1 | |
object_ids[ id ] = object | |
object.id = id | |
local order = #render_order + 1 | |
render_order[ order ] = object | |
object.placement = order | |
return object | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment