In this blog, I will write a small scenegraph function which will help to develop Roku channel.
Print anything in BrighScript :
Sub PrintAny(depth As Integer, prefix As String, any As Dynamic)
if depth >= 10
print "**** TOO DEEP " + itostr(5)
return
endif
prefix = string(depth*2," ") + prefix
depth = depth + 1
str = AnyToString(any)
if str <> invalid
print prefix + str
return
endif
if type(any) = "roAssociativeArray"
print prefix + "(assocarr)..."
PrintAnyAA(depth, any)
return
endif
if islist(any) = true
print prefix + "(list of " + itostr(any.Count()) + ")..."
PrintAnyList(depth, any)
return
endif
print prefix + "?" + type(any) + "?"
End Sub
Function AnyToString(any As Dynamic) As dynamic if any = invalid return "invalid" if isstr(any) return any if isint(any) return itostr(any) if isbool(any) if any = true return "true" return "false" endif if isfloat(any) return Str(any) if type(any) = "roTimespan" return itostr(any.TotalMilliseconds()) + "ms" return invalid End Function
Sub PrintAnyAA(depth As Integer, aa as Object) for each e in aa x = aa[e] PrintAny(depth, e + ": ", aa[e]) next End Sub
Sub PrintAnyList(depth As Integer, list as Object) i = 0 for each e in list PrintAny(depth, "List(" + itostr(i) + ")= ", e) i = i + 1 next End Sub
Determine if the given object is invalid or supports
the ifString interface and returns a string of non zero length
Function isnullorempty(obj) if obj = invalid return true if not isstr(obj) return true if Len(obj) = 0 return true return false End Function
Determine if the given object supports the ifString interface
Function isstr(obj as dynamic) As Boolean if obj = invalid return false if GetInterface(obj, "ifString") = invalid return false return true End Function
Determine if the given object supports the ifBoolean interface
Function isbool(obj as dynamic) As Boolean if obj = invalid return false if GetInterface(obj, "ifBoolean") = invalid return false return true End Function
Determine if the given object supports the ifFloat interface
Function isfloat(obj as dynamic) As Boolean if obj = invalid return false if GetInterface(obj, "ifFloat") = invalid return false return true End Function
Convert string to boolean.
Function strtobool(obj As dynamic) As Boolean if obj = invalid return false if type(obj) <> "roString" and type(obj) <> "String" return false o = strTrim(obj) o = Lcase(o) if o = "true" return true if o = "t" return true if o = "y" return true if o = "1" return true return false End Function
Get remaining hours from a total seconds
Function hoursLeft(seconds As Integer) As Integer hours% = seconds / 3600 return hours% End Function
Get remaining minutes from a total seconds
Function minutesLeft(seconds As Integer) As Integer hours% = seconds / 3600 mins% = seconds - (hours% * 3600) mins% = mins% / 60 return mins% End Function