fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
a7d6bcc
gui
1local class ={ 2structs ={} , 3inherits ={} , 4} 5 6local constructor_method ="Initialize" 7 8local OBJECT ={} 9 10function OBJECT : __index ( key ) 11-- Check for any specific, class-based, methods like "super" 12local meta_index =rawget ( getmetatable ( self ), key ) -- OBJECT 13if meta_index then 14-- Only return if not nil 15return meta_index 16end 17 18local accessors =rawget ( self , "__accessors" ) 19if accessors then 20-- Only do the lookup once to prevent multiple __index calls 21local value =accessors [ key ] 22if value then 23-- Only return if not nil 24return value 25end 26end 27 28-- Try the base class next 29local base =rawget ( self , "__baseclass" ) 30if base then 31-- Only do the lookup once to prevent multiple __index calls 32local value =base [ key ] 33if value then 34-- Only return if not nil 35return value 36end 37end 38 39-- Welp, nothing to find 40return nil 41end 42 43function OBJECT : super ( method , ... ) 44local base =self 45 46-- Attempt to determine the base via the current scope of the call 47for i =1 , self . __superscope do 48base =rawget ( base , "__baseclass" ) 49end 50 51-- Error if the scope of the call has no baseclass (AKA the main class) 52if not base then return error ( "attempted to call method 'super' in class with no baseclass" ) end 53 54local call =method or constructor_method 55 56if base [ call ] == nil then 57return error ( string . format ( "attempted to call method '%s' (a nil value)" , call )) 58end 59 60-- +1 to the scope 61self . __superscope =self . __superscope + 1 62 63-- Call the method 64-- This can trigger another super call, so thats why we need __superscope 65base [ call ]( self , ... ) 66 67-- -1 from the scope 68self . __superscope =self . __superscope - 1 69end 70 71function OBJECT : MakeAccessor ( name , internal , default ) 72self . __accessors [ internal ] =default 73if type ( default ) == "boolean" then 74self [ "Is" .. name ] =function ( this ) return this . __accessors [ internal ] end 75end 76self [ "Get" .. name ] =function ( this ) return this . __accessors [ internal ] end 77self [ "Set" .. name ] =function ( this , value ) this . __accessors [ internal ] =value end 78end 79 80function OBJECT : getBaseClass () 81return self . __baseclass 82end 83 84function OBJECT : __tostring () 85local base =self . __baseclass 86return string . format ( "%s[%s]" , base and base . __classname or "Object" , self . __classname ) 87end 88 89function class . new ( name , ... ) 90if class . structs [ name ] then 91local struct =class . structs [ name ] 92 93-- Create a copy of the struct, with metatable and all 94local obj =table . copy ( struct ) 95-- Set values that are unique to this instance 96obj . __superscope =1 97obj . __accessors ={} 98 99-- Try to call the initializer 100if obj [ constructor_method ] then 101obj [ constructor_method ]( obj , ... ) 102end 103return obj 104end 105 106return error (( "failed to create unknown class '%s'" ): format ( name )) 107end 108 109function class . register ( classname , basename ) 110return function ( struct ) 111struct . __classname =classname 112class . inherits [ classname ] =basename 113class . structs [ classname ] =struct 114return struct 115end 116end 117 118function class . init () 119-- We need to do this after registering our classes rather than during, 120-- since we don't know the order in which classes will be registered. 121-- We wan't to be sure that every classes baseclass is ready. 122 123for classname , struct in pairs ( class . structs ) do 124-- Set the baseclass 125struct . __baseclass =class . structs [ class . inherits [ classname ]] 126-- Register it as a valid class 127setmetatable ( struct , OBJECT ) 128end 129end 130 131return class