Changes from Version 1 of documentation/LuaManual/Contexts

Show
Ignore:
Author:
gambini (IP: 96.38.104.126)
Timestamp:
05/21/12 16:45:19 (5 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • documentation/LuaManual/Contexts

    v0 v1  
     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 
     18The ''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{{{ 
     21for key, doc in ipairs(context.documents:GetTable()) do 
     22    print doc.title 
     23end 
     24 
     25local index = 0 
     26local length = #context.documents:GetTable() / 2 
     27while index < length do 
     28    print (index .. ": " .. context.documents[index].title) 
     29end 
     30}}} 
     31Because 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 
     33It is also possible to access them as a dictionary, looking documents up by their ID: 
     34{{{ 
     35document = context.documents["highscores"] 
     36if document == nil then 
     37    print ("No document found!") 
     38end 
     39}}} 
     40 
     41Or accessing documents as attributes on the ''documents'' property itself: 
     42 
     43{{{ 
     44document = context.documents.highscores 
     45if document == nil then 
     46    print "No document found!" 
     47end 
     48}}} 
     49 
     50=== Methods === 
     51 
     52The 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 
     70Contexts 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{{{ 
     73new_context = rocket.CreateContext("hud", Vector2i.new(1024, 768)) 
     74}}} 
     75 
     76== Accessing contexts == 
     77 
     78Existing 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{{{ 
     81context = rocket.contexts["hud"] 
     82}}} 
     83 
     84List all contexts 
     85{{{ 
     86for key,context in ipairs(rocket.contexts) do 
     87  print context.name 
     88end 
     89}}} 
     90Because 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.