DMC Corona Library

Posted by dmccuskey, Posted on September 2, 2011

2 votes
GitHub URL: 
https://github.com/dmccuskey/DMC-Corona-Library

The DMC Corona Library is a collection of classes and utilities that I have created while developing apps using Corona SDK. I thought it might be useful to others so I decided to polish it to make it into a re-usable framework.

Right now there are three core files (more coming):

  • dmc_objects - for object-oriented programming
  • dmc_buttons - to create buttons and button groups
  • dmc_utils - collection of useful functions

There are many examples contained within the library and online documentation is available.

The library can be found on github: https://github.com/dmccuskey/DMC-Corona-Library

Enjoy !

====================================

Here are the details:

dmc_objects.lua

This file contains several methods and object classes which together form an object-oriented framework to use when programming in Lua with Corona SDK. Though it's not just for Corona - the top-level object classes can be used when developing software in plain Lua.

When doing OOP, these classes provide:

  • a classical model of object oriented programming

    dmc_objects is structured after the classical inheritance model, thus is easy to learn and use by people with varying levels of experience. This also helps give a solid organizational structure when using a language that is relatively unstructured.

  • a simple structure for doing OOP in Lua and Corona SDK

    The framework also abstracts the details of doing inheritance in Lua so that both experienced and inexperienced users of the language can focus on the code being written, and not the gory details of the language - ie, there is no need to learn about Lua metatables until you want to know. All of that is taken care of so you can get things done.

  • fast execution through structure and optimizations

    Because of the way Lua performs lookups for properties and methods, there can be a small performance penalty when using objects with many levels of inheritance. The solution for this is to get an object's properties and methods as close to the object as possible (ie, ON the object). dmc_objects does this for you so you can get the execution speed without having to sacrifice the benefit of code organization provided by object-oriented programming.

  • a mechanism for getters and setters

    The object model in dmc_objects was built to provide getters and setters in your object classes ! Go ahead, you can say it, "Yay, getters and setters !!!"

  • superClass() and superCall()

    Among other object-related methods, dmc_objects has superClass() to access an object's parent, and superCall() which allows you to call an overridden method on a super class !

  • an API similar to Corona display objects

    The core Corona Object API and Corona Physics Body API have been added to the pertinent base classes in dmc_objects. This allows you to treat your custom objects as if they were native Corona Display Objects †.

  • object printing support

    There is flexible, built-in support to print objects during debugging.

† You can treat them like Corona objects 99.5% of the time. Don't worry, the other 0.5% is easy to understand. :)

Documentation

Quick Guide: http://docs.davidmccuskey.com/display/docs/Quick+Guide+-+dmc_objects

API: http://docs.davidmccuskey.com/display/docs/dmc_objects.lua

Main Docs: http://docs.davidmccuskey.com/display/docs/dmc_buttons+Documentation

Examples

There are several examples in the folder examples/dmc_objects/ which show how to setup OOP structures in Lua. Among these are some original Corona examples which have been modified to use dmc_objects and fit into an OOP style of programming. These will make it easier to see how to move your projects to be object-oriented as well.

dmc_buttons.lua

This file contains classes to create different types of graphical buttons and button groups. It can create:

  • a Push button with optional text label
  • a Radio/Toggle button (on/off state) with optional text label
  • Toggle Group which allows either none or one selection of a group of buttons
  • Radio Group which allows single selection of a group of buttons

The code in this file is also great if you're looking for an example of multi-level class inheritance in Lua. If you want an easier example of multi-level inheritance, see examples/dmc_objects/DMC-Multishapes/.

Documentation

Quick Guide: http://docs.davidmccuskey.com/display/docs/Quick+Guide+-+dmc_buttons

API: http://docs.davidmccuskey.com/display/docs/dmc_buttons.lua

Main Docs: http://docs.davidmccuskey.com/display/docs/dmc_buttons+Documentation

Examples

There are examples in the folder examples/dmc_buttons/ which show how to use the dmc_buttons library. Other examples use the button class as well - check in examples/dmc_objects/.

dmc_utils.lua

This file is a small, but growing list of helpful utility functions. At the moment they are mostly concerned with tables. It provides these functions:

  • extend() - copy one table into another ( similar to jQuery extend() )
  • hasOwnProperty() - check if a property is directly on an object ( similar to JavaScript hasOwnProperty )
  • propertyIn() - check property existence in a list
  • destroy() - generic table destruction
  • createObjectCallback() - create a callback closure to call any method on your object
  • print() - multi-level object printing

Documentation

API: http://docs.davidmccuskey.com/display/docs/dmc_utils.lua

Examples

As of yet there are no specific examples for dmc_utils, however the other files in this library make use of it. Check them for examples.

License

(The MIT License)


Replies

RestlessNativeGames's picture
RestlessNativeGames
User offline. Last seen 9 hours 26 min ago. Offline
Joined: 21 Feb 2011

Looks pretty sweet, can't wait to test it out!

JayantV's picture
JayantV
User offline. Last seen 2 hours 10 min ago. Offline
Joined: 31 Oct 2009

Nice one David,

It is a very good tight implementation like a C++ class, which is good and hmmmm, not required all the time.

Nevertheless, a good port of lua to c++ ?;)

cheers

?:)

JRQ
User offline. Last seen 8 weeks 1 day ago. Offline
Joined: 28 Sep 2010

Thanks for sharing, the tab bar example rocks!

marqshaw
User offline. Last seen 25 weeks 6 days ago. Offline
Joined: 12 Mar 2011

Hi,

In the dmc_objects.lua file, you encapsulate the Corona functions, does this mean any new functions created by Corona you will need to add to your class?

thanks,

dmccuskey
User offline. Last seen 1 day 17 hours ago. Offline
Joined: 8 Jul 2011

hi marqshaw,

right now, yes - you'd have to add new functions as they come available.

however, since i've hacked into the Lua property lookup to implement getters/setters, i'm sure it would be pretty easy to include auto-lookup on the Corona object itself.

as you can imagine, it would certainly be preferable to have that in place so to lessen the maintenance for the project. i'll make sure that's on my todo list, unless you want to submit a patch. :)

cheers,
david

krzysiek
User offline. Last seen 12 weeks 3 days ago. Offline
Joined: 24 Mar 2011

I have trouble inserting objects into display groups. when i do this a get "bad argument #-2 to 'insert' (Proxy expected, got nil)". is it a bug or known limitation?

dmccuskey
User offline. Last seen 1 day 17 hours ago. Offline
Joined: 8 Jul 2011

hi krzysiek,

that error is happening because Corona display groups only accept native Corona objects.

by design, there is always a native Corona object available in a dmc object which can be added to a display group. by default this object is itself a Corona display group and is accessible via the property called 'display'.

here are some lines of code taken from one of the code examples at github. they form a simple example of how this works:

1
2
3
4
5
6
7
8
9
10
11
12
-- here we have a native Corona display group object
local shapeGroup = display.newGroup()
 
-- here we create a object which uses the dmc object library
local shape = ShapeFactory.create()
 
--[[
we want to add the dmc object to the native Corona display group,
so we pass in the native Corona object contained within the dmc object.
it is accessible via the property 'display'
--]]
shapeGroup:insert( shape.display )

cheers
David

d3mac123
User offline. Last seen 4 weeks 1 day ago. Offline
Joined: 12 Feb 2010

David,

I am looking for some add-ons into the libraries. Would you be available/interested in hear what I need (I will pay the implementations, of course)?

If so, send me a note to alex@asouza.com

Juf Jannie's picture
Juf Jannie
User offline. Last seen 6 days 5 hours ago. Offline
Joined: 29 Oct 2011

@gtatarkin I kind of hoped the automated the process. Or I mis understood their message. They asked us to redownload Cider cause some things couldn't be done through the regular update. Don't think they want to answer a lot of mails handing out new links and passwords by hand.