XKCDViewer/source/literally_everything.brs
2023-12-17 22:53:00 -06:00

289 lines
No EOL
9 KiB
Text

function init()
m.Keebpalette = createObject("roSGNode", "RSGPalette")
m.Keebpalette.colors = { DialogBackgroundColor: "0xFFFFFFFF",
DialogItemColor: "0x000000FF",
DialogTextColor: "0x000000FF",
DialogFocusColor: "0xFFFFFFFF",
DialogFocusItemColor: "0x000000FF",
DialogSecondaryItemColor: "0x000000FF",
DialogKeyboardColor: "0xFFFFFFFF",
DialogInputFieldColor: "0xFFFFFFFF",
DialogFootprintColor: "0xFFFFFFFF" }
m.isBig = false
m.canExitSearch = true
m.inKeyboard = false
m.inBigAlt = false
m.skipGet = false
m.months = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
m.top.backgroundColor = "0xFFFFFFFF"
m.top.backgroundURI = ""
m.XKCDfontcolor = "0x000000FF"
m.XKCDseccolor = "0x606060FF"
m.title = m.top.findNode("title")
m.num = m.top.findNode("num")
m.date = m.top.findNode("date")
m.comic = m.top.findNode("comic")
m.scrim = m.top.findNode("scrim")
m.comic_sel_label = m.top.findNode("comic_sel_label")
m.alt = m.top.findNode("alt")
m.alttextbig = m.top.findNode("alttextbig")
m.title.font.uri = "pkg:/images/xkcd.ttf"
m.title.font.size = 40
m.title.color = m.XKCDfontcolor
m.comic_sel_label.font.uri = "pkg:/images/xkcd.ttf"
m.comic_sel_label.font.size = 40
m.comic_sel_label.color = m.XKCDfontcolor
m.num.font.uri = "pkg:/images/xkcd.ttf"
m.num.font.size = 20
m.num.color = m.XKCDfontcolor
m.date.font.uri = "pkg:/images/xkcd.ttf"
m.date.font.size = 20
m.date.color = m.XKCDfontcolor
m.alt.font.uri = "pkg:/images/xkcd.ttf"
m.alt.font.size = 20
m.alt.color = m.XKCDseccolor
m.alttextbig.font.uri = "pkg:/images/xkcd.ttf"
m.alttextbig.font.size = 20
m.alttextbig.color = m.XKCDfontcolor
m.MainScene = m.top.findNode("MainScene")
m.SearchScene = m.top.findNode("SearchScene")
m.comic_id = m.top.findNode("comic_id")
m.doneButton = m.top.findNode("doneButton")
m.backButton = m.top.findNode("backButton")
m.comic_id.observeField("text", "searchkeyboardUpdate")
m.doneButton.observeField("buttonSelected", "searchbuttonClick")
m.backButton.observeField("buttonSelected", "backbuttonClick")
' m.TextKeeb = m.top.findNode("TextKeeb")
' m.TextKeeb.palette = m.Keebpalette
' m.TextKeeb.buttons = [
' "BACK",
' "DONE"
' ]
getNewestComicLol()
m.top.setFocus(true)
end function
function OpenSearchDialog()
m.SearchScene.visible = true
m.top.findNode("SearchScrim").visible = true
m.inKeyboard = true
m.comic_id.setFocus(true)
if m.comic_id.text.toInt() > m.num_latestComic OR m.comic_id.text.toInt() <= 0 then
m.canExitSearch = false
else
m.canExitSearch = true
end if
end function
function searchkeyboardUpdate()
?m.comic_id.text
if Len(m.comic_id.text) > 5 then
m.comic_id.text = Left(m.comic_id.text, 5)
end if
if m.comic_id.text.toInt() > m.num_latestComic OR m.comic_id.text.toInt() <= 0 then
m.canExitSearch = false
else
m.canExitSearch = true
end if
m.comic_sel_label.text = "GET COMIC #" + m.comic_id.text
end function
function searchbuttonClick()
if not m.canExitSearch or not m.inKeyboard then return 0
m.SearchScene.visible = false
m.top.findNode("SearchScrim").visible = false
m.inKeyboard = false
specificcomicget(m.comic_id.text.toInt())
m.comic_id.text = ""
m.comic_sel_label.text = "GET COMIC #..."
m.comic_id.setFocus(false)
m.doneButton.setFocus(false)
m.backButton.setFocus(false)
m.top.setFocus(true)
end function
function backbuttonClick()
m.SearchScene.visible = false
m.top.findNode("SearchScrim").visible = false
m.inKeyboard = false
m.comic_id.text = ""
m.comic_sel_label.text = "GET COMIC #..."
m.comic_id.setFocus(false)
m.doneButton.setFocus(false)
m.backButton.setFocus(false)
m.top.setFocus(true)
end function
function getNewestComicLol()
m.getLatestComicTask = createObject("roSGNode", "GetXKCDComic")
m.ct = m.getLatestComicTask
m.getLatestComicTask.comicid = ""
m.getLatestComicTask.observeField("comic", "startComicInitial")
m.getLatestComicTask.control = "RUN"
m.top.signalBeacon("AppLaunchComplete")
end function
function startComicInitial()
m.num_earliestComic = 1
m.num_latestComic = m.getLatestComicTask.comic.num
m.num_currentComic = m.getLatestComicTask.comic.num
sec = CreateObject("roRegistrySection", "Settings")
if sec.Exists("CurrentComic") and not m.skipGet then
specificcomicget(sec.Read("CurrentComic").toInt())
else
getcomic()
endif
m.skipGet = true
end function
function getcomic()
?m.ct.comic
if m.ct.comic = invalid then
m.title.text = "For some reason, that failed."
return true
end if
m.title.text = m.ct.comic.title
m.num.text = "Comic #" + m.ct.comic.num.ToStr()
m.alt.text = m.ct.comic.alt
m.alttextbig.text = m.ct.comic.alt
m.date.text = m.months[m.ct.comic.month.toInt() - 1] + " " + m.ct.comic.day + ", " + m.ct.comic.year
m.comic.SetCertificatesFile("common:/certs/ca-bundle.crt")
m.comic.InitClientCertificates()
m.comic.uri = m.ct.comic.img
if m.ct.comic.num >= m.num_latestComic then
m.top.findNode("changeIfFinal").text = "NEW?"
else
m.top.findNode("changeIfFinal").text = "NEXT"
end if
if m.ct.comic.num <= m.num_earliestComic then
m.top.findNode("changeIfFirst").text = ""
else
m.top.findNode("changeIfFirst").text = "PREV"
end if
sec = CreateObject("roRegistrySection", "Settings")
sec.Write("CurrentComic", m.ct.comic.num.toStr())
sec.Flush()
end function
function newcomicget(number as Integer)
m.num_currentComic = m.num_currentComic + number
if m.num_currentComic > m.num_latestComic then
m.num_currentComic = m.num_latestComic
getNewestComicLol()
return 0
else if m.num_currentComic < m.num_earliestComic then
m.num_currentComic = m.num_earliestComic
return 0
end if
m.getComicTask = createObject("roSGNode", "GetXKCDComic")
m.ct = m.getComicTask
m.getComicTask.comicid = "/" + m.num_currentComic.ToStr()
m.getComicTask.observeField("comic", "getcomic")
m.getComicTask.control = "RUN"
end function
function specificcomicget(number as Integer)
m.num_currentComic = number
m.getComicTask = createObject("roSGNode", "GetXKCDComic")
m.ct = m.getComicTask
m.getComicTask.comicid = "/" + m.num_currentComic.ToStr()
?m.getComicTask.comicid
m.getComicTask.observeField("comic", "getcomic")
m.getComicTask.control = "RUN"
end function
function toggleBig()
m.isBig = not m.isBig
if m.isBig then
m.comic.translation = [100, 50]
m.comic.width = 1080
m.comic.height = 620
m.comic.loadingBitmapUri="pkg:/images/reactions/loadingBig.png"
m.scrim.visible = true
else
m.comic.translation = [100, 0]
m.comic.width = 1080
m.comic.height = 400
m.comic.loadingBitmapUri="pkg:/images/reactions/loading.png"
m.scrim.visible = false
end if
end function
function toggleAlt()
m.inBigAlt = not m.inBigAlt
if m.inBigAlt then
m.top.findNode("AltScrim").visible = true
m.top.findNode("AltScene").visible = true
else
m.top.findNode("AltScrim").visible = false
m.top.findNode("AltScene").visible = false
end if
end function
function onKeyEvent(key as String, press as Boolean) as Boolean
handled = false
if press then
if not m.inKeyboard then
if (key = "back") then
handled = false
if m.inBigAlt then
toggleAlt()
handled = true
end if
if m.isBig then
toggleBig()
handled = true
end if
else
if (key = "left") then
newcomicget(-1)
else if (key = "right") then
newcomicget(1)
else if (key = "down") then
if not m.isBig and not m.inBigAlt then OpenSearchDialog()
else if (key = "up") then
if not m.isBig then
toggleAlt()
else
toggleBig()
end if
else if (key = "replay" OR key = "fastforward") then
getNewestComicLol()
else if (key = "rewind") then
specificcomicget(1)
else if (key = "OK") then
if not m.inBigAlt then
toggleBig()
else
toggleAlt()
end if
end if
handled = true
end if
else
if (key = "back") then
backbuttonClick()
else if key = "down" and not m.backButton.hasFocus() and not m.doneButton.hasFocus()
m.backButton.setFocus(true)
else if key = "up" and not m.comic_id.hasFocus()
m.comic_id.setFocus(true)
else if key = "left" and not m.doneButton.hasFocus() and not m.comic_id.hasFocus()
m.doneButton.setFocus(true)
else if key = "left" and not m.backButton.hasFocus() and not m.comic_id.hasFocus()
m.backButton.setFocus(true)
else if key = "right" and not m.doneButton.hasFocus() and not m.comic_id.hasFocus()
m.doneButton.setFocus(true)
else if key = "right" and not m.backButton.hasFocus() and not m.comic_id.hasFocus()
m.backButton.setFocus(true)
end if
handled = true
end if
end if
return handled
end function