Class: UIC::Application

Inherits:
Object
  • Object
show all
Includes:
FileBacked
Defined in:
lib/ruic/application.rb

Overview

The Application represents the root of your UIC application, corresponding to a .uia file.

Defined Under Namespace

Classes: Behavior, Presentation, StateMachine

Instance Attribute Summary collapse

Attributes included from FileBacked

#doc, #file

Instance Method Summary collapse

Methods included from FileBacked

#file_found?, #filename, #path_to, #save!, #save_as, #to_xml

Constructor Details

#initialize(metadata, uia_path = nil) ⇒ Application

Returns a new instance of Application.

Parameters:

  • metadata (UIC::MetaData)

    the MetaData to use for this application.

  • uia_path (String) (defaults to: nil)

    path to a .uia file to load. If omitted you will need to later set the .file = for the instance and then call #load_from_file.



16
17
18
19
20
21
# File 'lib/ruic/application.rb', line 16

def initialize(,uia_path=nil)
	@metadata = 
	self.file = uia_path
	@assets = {}
	load_from_file if file_found?
end

Instance Attribute Details

#metadataUIC::MetaData (readonly)

Returns the metadata loaded for this application.

Returns:



10
11
12
# File 'lib/ruic/application.rb', line 10

def 
  @metadata
end

Instance Method Details

#[](asset_id_or_path) ⇒ UIC::Application::Behavior, ...

Find an asset by .uia identifier or path to the asset.

Examples:

main1 = app['#main']
main2 = app['MyMain.uip']
main3 = app.main_presentation
assert main1==main2 && main2==main3

Parameters:

  • asset_id_or_path (String)

    an idref like "#status" or a relative path to the asset like "VehicleStatus.uip" or "scripts/Main.lua".

Returns:



63
64
65
66
67
68
69
70
71
72
# File 'lib/ruic/application.rb', line 63

def [](asset_id_or_path)
	all = assets
	if asset_id_or_path.start_with?('#')
		id = asset_id_or_path[1..-1]
		all.find{ |asset| asset.id==id }
	else
		full_path = File.expand_path(asset_id_or_path,File.dirname(file))
		all.find{ |asset| asset.file==full_path }
	end
end

#assetsArray

Returns all assets referenced by the application. Ordered by the order they appear in the .uia.

Returns:

  • (Array)

    all assets referenced by the application. Ordered by the order they appear in the .uia.



88
89
90
# File 'lib/ruic/application.rb', line 88

def assets
	@assets.values.inject(:+)
end

#at(path) ⇒ MetaData::AssetBase Also known as: /

Find an element or asset in a presentation by scripting path.

Examples:

# Four ways to find the same layer
layer1 = app.at "main:Scene.Layer"
layer2 = app/"main:Scene.Layer"
layer3 = app.main.at "Scene.Layer"
layer4 = app.main/"Scene.Layer"

assert layer1==layer2 && layer2==layer3 && layer3==layer4

Returns:

See Also:



172
173
174
175
176
177
# File 'lib/ruic/application.rb', line 172

def at(path)
	parts = path.split(':')
	preso = parts.length==2 ? self["##{parts.first}"] : main_presentation
	raise "Cannot find presentation for #{id}" unless preso
	preso.at(parts.last)
end

#behaviorsArray<UIC::Application::Behavior>

Returns all behaviors referenced by the application.

Returns:



137
138
139
# File 'lib/ruic/application.rb', line 137

def behaviors
	@assets['behavior'] ||= []
end

#errorsArray<String>

Returns an array (possibly empty) of all errors in this application (including referenced assets).

Examples:

show app.errors if app.errors?

Returns:

  • (Array<String>)

    an array (possibly empty) of all errors in this application (including referenced assets).



48
49
50
# File 'lib/ruic/application.rb', line 48

def errors
	file_found? ? assets.flat_map(&:errors) : ["File not found: '#{file}'"]
end

#errors?Boolean

Returns true if there are any errors with the application.

Returns:

  • (Boolean)

    true if there are any errors with the application.



41
42
43
# File 'lib/ruic/application.rb', line 41

def errors?
	!errors.empty?
end

#image_pathsArray<String>

Returns array of all image paths used by the application (not just in subfolders).

Returns:

  • (Array<String>)

    array of all image paths used by the application (not just in subfolders).



127
128
129
# File 'lib/ruic/application.rb', line 127

def image_paths
	image_usage.keys
end

#image_usageHash

Returns a mapping of image paths to arrays of elements/assets that reference that image.

Returns:

  • (Hash)

    a mapping of image paths to arrays of elements/assets that reference that image.



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ruic/application.rb', line 113

def image_usage
	# TODO: this returns the same asset multiple times, with no indication of which property is using it; should switch to Properties.
	Hash[
		(presentations + statemachines)
			.map(&:image_usage)
			.inject{ |h1,h2| h1.merge(h2){ |path,els1,els2| [*els1,*els2] } }
			.sort_by do |path,assets|
				parts = path.downcase.split '/'
				[ parts.length, parts ]
			end
	].tap{ |h| h.extend(UIC::PresentableHash) }
end

#inspectObject



5
6
7
# File 'lib/ruic/application.rb', line 5

def inspect
	"<UIC::Application '#{File.basename(file)}'#{:' FILENOTFOUND' unless file_found?}>"
end

#load_from_filenil

Loads the application from the file. If you pass the path to your .uia to #initialize then this method is called automatically.

Returns:

  • (nil)


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruic/application.rb', line 27

def load_from_file
	self.doc = Nokogiri.XML(File.read(file,encoding:'utf-8'))
	@assets  = @doc.search('assets *').map do |el|
		case el.name
			when 'behavior'     then UIC::Application::Behavior
			when 'statemachine' then UIC::Application::StateMachine
			when 'presentation' then UIC::Application::Presentation
			when 'renderplugin' then UIC::Application::RenderPlugin
		end.new(self,el)
	end.group_by{ |asset| asset.el.name }
	nil
end

#main_presentationUIC::Application::Presentation Also known as: main

Returns the main presentation rendered by the application.

Examples:

main = app.main
main = app.main_presentation # more-explicit alternative

Returns:



96
97
98
99
100
# File 'lib/ruic/application.rb', line 96

def main_presentation
	initial_id = @doc.at('assets')['initial']
	presos = presentations
	presos.find{ |pres| pres.id==initial_id } || presos.first
end

#main_presentation=(presentation) ⇒ UIC::Application::Presentation

Change which presentation is rendered for the application.

Parameters:

Returns:



106
107
108
109
110
# File 'lib/ruic/application.rb', line 106

def main_presentation=(presentation)
	# TODO: set to Presentation or Application::Presentation
	# TODO: create a unique ID if none exists
	@doc.at('assets')['initial'] = presentation.id
end

#presentationsArray<UIC::Application::Presentation>

Returns all presentations referenced by the application.

Returns:



132
133
134
# File 'lib/ruic/application.rb', line 132

def presentations
	@assets['presentation'] ||= []
end

#renderpluginsArray<UIC::Application::RenderPlugin>

Returns all render plug-ins referenced by the application.

Returns:

  • (Array<UIC::Application::RenderPlugin>)

    all render plug-ins referenced by the application.



147
148
149
# File 'lib/ruic/application.rb', line 147

def renderplugins
	@assets['renderplugin'] ||= []
end

#save_all!Object

Save changes to this application and every asset to disk.



152
153
154
155
156
# File 'lib/ruic/application.rb', line 152

def save_all!
	save!
	presentations.each(&:save!)
	# TODO: enumerate other assets and save them
end

#statemachinesArray<UIC::Application::StateMachine>

Returns all state machines referenced by the application.

Returns:



142
143
144
# File 'lib/ruic/application.rb', line 142

def statemachines
	@assets['statemachine'] ||= []
end