Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added table.flatten and pick documentation #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/source/api/lua.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
lua
===

**Table Extensions**

.. lua:class:: table

The following are extensions to the Lua table class.

.. lua:function:: flatten(t)

Flattens a table into a single array.

:param t: The table to flatten
:type t: table
:return: A flattened array
:rtype: table

.. code-block:: lua

t = {1, 2, {3, 4, {5, 6}}}
print(table.flatten(t)) -- {1, 2, 3, 4, 5, 6}
136 changes: 136 additions & 0 deletions docs/source/api/pick.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
pick
====

.. lua:function:: pick()

Opens the document picker to pick a single asset and convert it to its corresponding Codea asset type.

:returns: The picked asset
:rtype: any

.. code-block:: lua

local pickedAsset = pick()
if pickedAsset then
print(pickedAsset)
end

.. lua:class:: pick

.. lua:staticmethod:: pick.image()

Opens the document picker to pick an image or PDF asset.

:returns: The picked image or PDF asset
:rtype: image

.. code-block:: lua

myImage = pick.image()
...
sprite(myImage, WIDTH/2, HEIGHT/2)

.. lua:staticmethod:: pick.table()

Opens the document picker to pick a JSON asset and convert it to a table.

:returns: The picked asset converted to a table
:rtype: table

.. code-block:: lua

myTable = pick.table()
...
print(myTable["key"])

.. lua:staticmethod:: pick.text()

Opens the document picker to pick a text asset.

:returns: The text content of the picked asset
:rtype: string

.. code-block:: lua

myText = pick.text()
...
text(myText, WIDTH/2, HEIGHT/2)

.. lua:staticmethod:: pick.asset()

Opens the document picker to pick an asset and return its asset key.

:returns: The picked asset key
:rtype: asset.key

.. code-block:: lua

myAssetKey = pick.asset()
...
print(myAssetKey.type)

.. lua:staticmethod:: pick.photo()

Opens the photo picker to pick a single photo from the photo library.

This is a different picker than the document picker, and only allows picking a single photo at a time.

:returns: The picked photo as an image asset
:rtype: image

.. code-block:: lua

myPhoto = pick.photo()
...
sprite(myPhoto, WIDTH/2, HEIGHT/2)

.. lua:staticmethod:: pick.sound()

Opens the document picker to pick an audio asset (sound or music).

:returns: The picked audio asset
:rtype: sound.source

.. code-block:: lua

sound.play(pick.sound())

.. lua:staticmethod:: pick.image(...)
pick.table(...)
pick.text(...)
pick.asset(...)
pick.photo(...)
pick.sound(...)

Pick assets with the specified UTType strings, options and callback function.

See ``pick(...)`` below for more information.

.. lua:attribute:: option

A table containing the following options:

- ``text`` - Text asset
- ``json`` - JSON asset
- ``sound`` - Audio asset (sound or music)
- ``pdf`` - PDF asset
- ``image`` - Image or PDF asset, defined as { "public.image", "com.adobe.pdf" }
- ``table`` - JSON asset converted to a table, defined as { pick.option.json, pick.option.decodeTable }
- ``multiple`` - Enable multiple asset selection
- ``assetKey`` - Return the asset key instead of the asset content
- ``decodeTable`` - Decode the picked asset as a table (only for json assets)

.. lua:function:: pick(...)

Pick assets with the specified UTType strings, options and callback function.

The order of types, options and callback is not important, though we recommend passing the callback last for readability.

When a callback function is provided, the function becomes asynchronous and the picked asset is passed to the callback function.

.. code-block:: lua

-- Pick multiple assets of type yaml or image
pick("public.yaml", pick.option.image, pick.option.multiple, function(multipleAssets)
print("Picked " .. #multipleAssets .. " assets")
})
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Codea 4
api/pasteboard
api/physics2d
api/physics3d
api/pick
api/viewer
api/device

Expand Down
Loading