utils.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. --This file is reponsible for providing a base to create standalone mode editors.
  2. --Like the paint and edit program.
  3. local eapi = require("Editors")
  4. local utils = {}
  5. local swidth, sheight = screenSize()
  6. function utils:newTool()
  7. local tool = {} --The tool editor api.
  8. tool.editorsheet = eapi.editorsheet
  9. tool.flavor = eapi.flavor
  10. tool.flavorBack = eapi.flavorBack
  11. tool.background = eapi.background
  12. local sid --Selected option id
  13. local controlID = 11 --The sprite id of the first control button icon.
  14. local controlNum = 3 --The number of the control buttons at the top right corner of the editor.
  15. local controlGrid = {swidth-8*controlNum,0, 8*controlNum,8, controlNum,1}
  16. function tool:drawTopBar()
  17. rect(0,0,swidth,8,false,self.flavor)
  18. SpriteGroup(55, 0,0, 4,1, 1,1, false, self.editorsheet) --The LIKO12 Logo
  19. SpriteGroup(controlID, controlGrid[1],controlGrid[2], controlGrid[5],controlGrid[6], 1,1, false, self.editorsheet)
  20. if sid then
  21. SpriteGroup(controlID+24+sid, controlGrid[1]+sid*8,controlGrid[2], 1,1, 1,1, false, self.editorsheet)
  22. end
  23. end
  24. function tool:drawBottomBar()
  25. rect(0,sheight-8,swidth,8,false,self.flavor)
  26. end
  27. function tool:drawUI()
  28. clear(self.background) --Clear the screen
  29. self:drawTopBar() --Draw the top bar
  30. self:drawBottomBar() --Draw the bottom bar
  31. end
  32. function tool:start(editor,reload,save,data,hotkey)
  33. self.editor = editor
  34. local screen = screenshot() --Backup the screen
  35. local px,py,pc = printCursor() --Backup the current printing cursor.
  36. cursor("normal") --Set the current cursor.
  37. if data then --Import some data.
  38. self.editor:import(data)
  39. end
  40. self.editor:entered() --Enter the editor.
  41. local eflag = false --Controls selection mode flag.
  42. local hflag = false --Mouse hover flag.
  43. --The control buttons actions
  44. local controls = {
  45. function() --Reload
  46. self.editor:leaved()
  47. if reload then reload(self) end
  48. self.editor:entered()
  49. end,
  50. function() --Save
  51. if save then save(self) end
  52. end,
  53. function() --Exit
  54. self.editor:leaved()
  55. return true
  56. end
  57. }
  58. for event, a,b,c,d,e,f in pullEvent do
  59. if event == "keypressed" then
  60. if a == "escape" then
  61. eflag = not eflag
  62. if eflag then
  63. cursor("none")
  64. sid = 1
  65. else
  66. cursor("normal")
  67. sid = false
  68. end
  69. pushMatrix() cam() self:drawTopBar() popMatrix()
  70. elseif eflag then
  71. if a == "left" then
  72. sid = sid - 1
  73. if sid < 0 then sid = 2 end
  74. pushMatrix() cam() self:drawTopBar() popMatrix()
  75. elseif a == "right" then
  76. sid = sid + 1
  77. if sid > 2 then sid = 0 end
  78. pushMatrix() cam() self:drawTopBar() popMatrix()
  79. elseif a == "return" then
  80. if controls[sid+1]() then break end
  81. sid, eflag = false, false
  82. pushMatrix() cam() self:drawTopBar() popMatrix()
  83. end
  84. else
  85. local key, sc = a, b
  86. if(isKDown("lalt", "ralt")) then
  87. key = "alt-" .. key
  88. sc = "alt-" .. sc
  89. end
  90. if(isKDown("lctrl", "rctrl", "capslock")) then
  91. key = "ctrl-" .. key
  92. sc = "ctrl-" .. sc
  93. end
  94. if(isKDown("lshift", "rshift")) then
  95. key = "shift-" .. key
  96. sc = "shift-" .. sc
  97. end
  98. local ishotkey --Was it an hotkey ?
  99. if key == "ctrl-s" then
  100. if controls[2]() then break end
  101. pushMatrix() cam() self:drawTopBar() popMatrix()
  102. elseif key == "ctrl-l" then
  103. if controls[1]() then break end
  104. pushMatrix() cam() self:drawTopBar() popMatrix()
  105. elseif key == "ctrl-q" then
  106. if controls[3]() then break end
  107. pushMatrix() cam() self:drawTopBar() popMatrix()
  108. elseif hotkey then
  109. if hotkey(self,key,sc) then
  110. ishotkey = true
  111. end
  112. end
  113. sc = "sc_"..sc
  114. if self.editor.keymap and not ishotkey then
  115. local usedKey
  116. if self.editor.keymap[key] then usedKey = key
  117. elseif self.editor.keymap[sc] then usedKey = sc
  118. end
  119. if usedKey then
  120. self.editor.keymap[usedKey](self.editor,c)
  121. self.editor.lastKey = usedKey
  122. end
  123. end
  124. if self.editor[event] and not ishotkey then self.editor[event](self.editor,a,b,c,d,e,f) end
  125. end
  126. elseif event == "mousepressed" and not eflag then
  127. local cx, cy = whereInGrid(a,b,controlGrid)
  128. if cx then
  129. cursor("handpress")
  130. hflag = "d"
  131. sid = cx-1
  132. pushMatrix() cam() self:drawTopBar() popMatrix()
  133. else
  134. if self.editor[event] then self.editor[event](self.editor,a,b,c,d,e,f) end
  135. end
  136. elseif event == "mousemoved" and not eflag then
  137. local cx, cy = whereInGrid(a,b,controlGrid)
  138. if cx then
  139. if hflag and hflag == "d" then
  140. sid = cx-1
  141. pushMatrix() cam() self:drawTopBar() popMatrix()
  142. elseif not hflag then
  143. cursor("handrelease")
  144. hflag = "h"
  145. end
  146. else
  147. if hflag and hflag == "h" then
  148. hflag = false
  149. cursor("normal")
  150. end
  151. if self.editor[event] then self.editor[event](self.editor,a,b,c,d,e,f) end
  152. end
  153. elseif event == "mousereleased" and not eflag then
  154. local cx, cy = whereInGrid(a,b,controlGrid)
  155. if cx then
  156. if hflag and hflag == "d" then
  157. cursor("handrelease")
  158. if controls[sid+1]() then break end
  159. sid, hflag = false, false
  160. pushMatrix() cam() self:drawTopBar() popMatrix()
  161. elseif not hflag then
  162. hflag = "h"
  163. cursor("handrelease")
  164. end
  165. else
  166. if hflag then
  167. if hflag == "d" then
  168. sid = false
  169. pushMatrix() cam() self:drawTopBar() popMatrix()
  170. end
  171. cursor("normal")
  172. hflag = nil
  173. end
  174. if self.editor[event] then self.editor[event](self.editor,a,b,c,d,e,f) end
  175. end
  176. elseif eflag then
  177. if event == "touchpressed" then textinput(true) end
  178. else
  179. if self.editor[event] then self.editor[event](self.editor,a,b,c,d,e,f) end
  180. end
  181. end
  182. clear() --Clear the screen
  183. screen:image():draw(0,0) --Restore the old frame
  184. printCursor(px,py,pc) --Restore the old print cursor
  185. end
  186. return tool
  187. end
  188. return utils