PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/eventConsole.lua

https://github.com/RSCdev/shared-lua-scripts
Lua | 227 lines | 75 code | 32 blank | 120 comment | 4 complexity | 08a5fa5c622cd5562dc97017ac538549 MD5 | raw file
  1. module(..., package.seeall)
  2. --[[
  3. Simple helper class that makes it possible to show text based output (like with the "print()" command) directly
  4. in your App and not in the console window. This is useful for using it on the device where no extra console
  5. window is available.
  6. Beside the possibility to show only white colored text, it is also possible to use special methods that give the text
  7. a specific color. This is useful to give the messages you write out to the console a weight just as with good old java
  8. loggers.
  9. The following methods for giving your text a weight are available
  10. error() -> produces red text
  11. warning() -> produces yellow text
  12. info() -> produces green text
  13. debug() -> produces light blue text
  14. If you just want to print out white text and don't care about the weight of information, then you get the job done
  15. by using the print() method just like the regular lua print command that writes to the console window.
  16. Sample usage:
  17. Initialize an instance of this class as soon as possible. A good place to do so is your main.lua file
  18. << main.lua
  19. require("eventConsole")
  20. eventConsole = eventConsole:new()
  21. ...
  22. >>>>>>>>>>>>>>>
  23. That's all. The above code creates a small touchable button in the top center of the device screen, that always is
  24. kept on the top of all drawn display objects. If you click the button the first time, a half transparent scroll view
  25. with the log messages will become visible. If you click the button again, the scroll view will become invisible and
  26. so on.
  27. Now you have access to this instance everywhere in your code and you can use it like shown here:
  28. << yourFile.lua
  29. ...
  30. function object:checkFileExistence(fileName)
  31. local retVal = false
  32. local path = system.pathForFile( fileName, self.baseDirectory )
  33. eventConsole:print("Checking file existence now...") -- writes a white colored text
  34. local file = io.open( path )
  35. if(file ~= nil) then
  36. eventConsole:info("File was found...") -- writes a green colored text of weight "info"
  37. io.close(file)
  38. retVal= true
  39. else
  40. eventConsole:error("File not found...") -- writes a red colored text of weight "error"
  41. end
  42. return retVal
  43. end
  44. >>>>>>>>>>>>>>>
  45. IMPORTANT:
  46. Once your App is ready for being published, you don't need to delete all the calls of the eventConsole instance
  47. everywhere in your code.
  48. The only thing you need to do in order to deactivate the console is adding the following line
  49. right after the creation of the instance in the main.lua file
  50. eventConsole:disable()
  51. --]]
  52. --[[
  53. author : andreas ermrich
  54. author mail : aeh@incowia.com
  55. company : incowia GmbH / Garamox
  56. websites : http://www.incowia.com
  57. http://www.garamox.de
  58. version : 1.0
  59. date : 2012.07.12
  60. author comments : use it for free and enjoy. Please give me your feedback with a short mail!!!
  61. ]]--
  62. function eventConsole:new()
  63. local CoronaWidget = require("widget")
  64. local object = {
  65. numLogEntries = 0 ,
  66. scrollView = nil ,
  67. activator = display.newGroup(),
  68. consoleFontSize = 24 ,
  69. visible = false ,
  70. disabled = false ,
  71. toFrontThread = nil
  72. }
  73. --[[
  74. constructor
  75. ]]--
  76. function object:init()
  77. --create a scroll view that acts as container for all the log entries
  78. self.scrollView = CoronaWidget.newScrollView( {bgColor = {0,0,0,210}, scrollWidth = display.contentWidth, scrollHeight = display.contentHeight} )
  79. self.scrollView.isVisible = false
  80. --the next elements are used as touchable display group for switching the visibility state of the scroll view
  81. local circle = display.newCircle(display.contentWidth/2, 20, 60) -- needed for better handling. By the help of this circle we don't need to exactly
  82. circle.alpha=0.05 -- hit the smaller visible circle which is created next to trigger the touch event
  83. local circle2 = display.newCircle(display.contentWidth/2, 20, 15)
  84. circle2:setFillColor(255,255,255)
  85. circle2.strokeWidth=5
  86. circle2:setStrokeColor(200,50,50)
  87. circle2.alpha=1
  88. local caption = display.newText("C", 0, 0, native.systemFont, 16)
  89. caption:setTextColor(0,0,250)
  90. caption:setReferencePoint(display.CenterReferencePoint)
  91. caption.x = display.contentWidth/2 + 1
  92. caption.y = 21
  93. self.activator:insert(circle)
  94. self.activator:insert(circle2)
  95. self.activator:insert(caption)
  96. self.activator:addEventListener("touch", self )
  97. --Closure that will be executed in a separate thread. Necessary to keep the console on top of the screen
  98. local cl = function()
  99. self.scrollView:toFront()
  100. self.activator:toFront()
  101. end
  102. self.toFrontThread = timer.performWithDelay(10, cl, 0) --start the thread with an calling interval of 10 milliseconds
  103. end
  104. --[[
  105. This adds a new text without a color code
  106. ]]--
  107. function object:print(text)
  108. self:write(text, {255,255,255}, "")
  109. end
  110. --[[
  111. This adds a new text marked as error
  112. ]]--
  113. function object:error(text)
  114. self:write(text, {255,0,0}, "E")
  115. end
  116. --[[
  117. This adds a new text marked as warning
  118. ]]--
  119. function object:warning(text)
  120. self:write(text, {255,235,55}, "W")
  121. end
  122. --[[
  123. This adds a new text marked as info message
  124. ]]--
  125. function object:info(text)
  126. self:write(text, {0,255,0}, "I")
  127. end
  128. --[[
  129. This adds a new text marked as debug message
  130. ]]--
  131. function object:debug(text)
  132. self:write(text, {147,180,255}, "D")
  133. end
  134. --[[
  135. This adds a new text to the output scrollview
  136. ]]--
  137. function object:write(text, colorTable, typeMarker)
  138. if(self.disabled == true) then return end
  139. local text = display.newText(os.date("%Y/%m/%d %H:%M:%S") .. " >" .. typeMarker .. "> " .. text, 0, self.numLogEntries * self.consoleFontSize, native.systemFont, self.consoleFontSize)
  140. text:setTextColor(colorTable[1], colorTable[2], colorTable[3])
  141. self.scrollView:insert(text)
  142. self.numLogEntries = self.numLogEntries + 1
  143. end
  144. --[[
  145. Touch handler that switches the scroll view visibility state
  146. ]]--
  147. function object:touch(event)
  148. if(event.phase == "began") then
  149. self.visible = not self.visible
  150. self.scrollView.isVisible = self.visible
  151. end
  152. return true
  153. end
  154. --[[
  155. deactivate this instance. Used when your code goes into productive phase
  156. ]]--
  157. function object:disable()
  158. self.disabled = true
  159. timer.cancel(self.toFrontThread) -- stop the thread that brings the scroll view and the touchable display group to the front
  160. display.remove(self.scrollView)
  161. display.remove(self.activator)
  162. end
  163. object:init()
  164. return object
  165. end