| 1 | [[PageOutline(1-5, Contents)]] |
---|
| 2 | = Contexts = |
---|
| 3 | |
---|
| 4 | == Interface == |
---|
| 5 | |
---|
| 6 | === Properties === |
---|
| 7 | |
---|
| 8 | || '''Lua property''' || '''Brief description''' || '''Equivalent C++ functions''' || |
---|
| 9 | || ''dimensions'' || Gets/sets the dimensions of the context. || ''GetDimensions()'', ''SetDimensions()'' || |
---|
| 10 | || ''documents'' || Retrieves a document within the interface. || ''GetDocument()'', ''GetNumDocuments()'' || |
---|
| 11 | || ''focus_element'' || Retrieves the context's focus element. || ''GetFocusElement()'' || |
---|
| 12 | || ''hover_element'' || Retrieves the element under the context's cursor. || ''GetHoverElement()'' || |
---|
| 13 | || ''root_element'' || Retrieves the context's root element. || ''GetRootElement()'' || |
---|
| 14 | || ''name'' || Retrieves the context's name. || ''GetName()'' || |
---|
| 15 | |
---|
| 16 | ==== Retrieving documents ==== |
---|
| 17 | |
---|
| 18 | The ''documents'' property on the context is a [wiki:documentation/LuaManual/ProxyTables proxy table] in regards to the C++ interface with Lua, accessed by both string and integer keys. |
---|
| 19 | |
---|
| 20 | {{{ |
---|
| 21 | for key, doc in ipairs(context.documents:GetTable()) do |
---|
| 22 | print doc.title |
---|
| 23 | end |
---|
| 24 | |
---|
| 25 | local index = 0 |
---|
| 26 | local length = #context.documents:GetTable() / 2 |
---|
| 27 | while index < length do |
---|
| 28 | print (index .. ": " .. context.documents[index].title) |
---|
| 29 | end |
---|
| 30 | }}} |
---|
| 31 | Because the items can be indexed by both string and integer keys, the contexts are duplicated in the table. This means that the size operator on ''context.documents:GetTable()'' returns twice as many items as there are contexts. |
---|
| 32 | |
---|
| 33 | It is also possible to access them as a dictionary, looking documents up by their ID: |
---|
| 34 | {{{ |
---|
| 35 | document = context.documents["highscores"] |
---|
| 36 | if document == nil then |
---|
| 37 | print ("No document found!") |
---|
| 38 | end |
---|
| 39 | }}} |
---|
| 40 | |
---|
| 41 | Or accessing documents as attributes on the ''documents'' property itself: |
---|
| 42 | |
---|
| 43 | {{{ |
---|
| 44 | document = context.documents.highscores |
---|
| 45 | if document == nil then |
---|
| 46 | print "No document found!" |
---|
| 47 | end |
---|
| 48 | }}} |
---|
| 49 | |
---|
| 50 | === Methods === |
---|
| 51 | |
---|
| 52 | The following methods are exported from the C++ interface. |
---|
| 53 | |
---|
| 54 | || '''Lua method''' || '''Brief description''' || |
---|
| 55 | || ''AddEventListener()'' || Attaches an inline event listener to the root of the context.|| |
---|
| 56 | || ''AddMouseCursor()'' || Adds a previously-loaded mouse cursor to the document. || |
---|
| 57 | || ''CreateDocument()'' || Creates a new document. || |
---|
| 58 | || ''LoadDocument()'' || Loads a document from an external RML file. || |
---|
| 59 | || ''LoadMouseCursor()'' || Loads a mouse cursor from an external RML file. || |
---|
| 60 | || ''Render()'' || Renders the context. || |
---|
| 61 | || ''ShowMouseCursor()'' || Shows or hides the mouse cursor. || |
---|
| 62 | || ''UnloadAllDocuments()'' || Unloads all loaded documents within the context. || |
---|
| 63 | || ''UnloadAllMouseCursors()'' || Unloads all of the context's mouse cursors. || |
---|
| 64 | || ''UnloadDocument()'' || Unloads one of the context's documents. || |
---|
| 65 | || ''UnloadMouseCursor()'' || Unloads one of the context's cursors. || |
---|
| 66 | || ''Update()'' || Updates the context. || |
---|
| 67 | |
---|
| 68 | == Creating contexts == |
---|
| 69 | |
---|
| 70 | Contexts can be created in Lua with the ''CreateContext()'' function in the ''rocket'' table. This function takes the name of the context as a string and the dimensions as a ''Vector2i'' type. |
---|
| 71 | |
---|
| 72 | {{{ |
---|
| 73 | new_context = rocket.CreateContext("hud", Vector2i.new(1024, 768)) |
---|
| 74 | }}} |
---|
| 75 | |
---|
| 76 | == Accessing contexts == |
---|
| 77 | |
---|
| 78 | Existing contexts can be accessed in Lua via the contexts member on the rocket table. They can then be accessed via name or index. |
---|
| 79 | |
---|
| 80 | {{{ |
---|
| 81 | context = rocket.contexts["hud"] |
---|
| 82 | }}} |
---|
| 83 | |
---|
| 84 | List all contexts |
---|
| 85 | {{{ |
---|
| 86 | for key,context in ipairs(rocket.contexts) do |
---|
| 87 | print context.name |
---|
| 88 | end |
---|
| 89 | }}} |
---|
| 90 | Because they are indexed by both integers and strings, the contexts exist twice in the table. If iterating over them, use ipairs() rather than pairs() to not iterate over duplicates. |