init.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. local Editor = {}
  2. Editor.Current = {}
  3. Editor.curid = 3
  4. Editor.editors = {"console","code","sprite","tile","sprite","sprite"}
  5. local ModeGrid = {192-8*#Editor.editors,1,8*#Editor.editors,8,#Editor.editors,1}
  6. local ModeMFlag = false
  7. function Editor:_init()
  8. self:switchEditor(self.curid)
  9. for _,e in pairs(Editor.editors) do
  10. local m = require("editor."..e)
  11. if m._init then m:_init(Editor) end
  12. if not m.keymap then m.keymap = {} end
  13. if not m.parent then m.parent = Editor end
  14. end
  15. local init_path = (os.getenv("HOME") or "") .. "/.liko12/init.lua"
  16. local init_file = io.open(init_path)
  17. if(init_file) then
  18. init_file:close()
  19. local ok, err = pcall(dofile, init_path)
  20. if(not ok) then print(err) end
  21. end
  22. end
  23. function Editor:_redraw()
  24. self:redrawUI()
  25. if self.Current._redraw then self.Current:_redraw() end
  26. end
  27. function Editor:redrawUI()
  28. api.clear(6)
  29. api.rect(1,1,192,8,10)
  30. api.rect(1,128-7,192,8,10)
  31. api.SpriteGroup(24-#Editor.editors+1,192-8*#Editor.editors,1,#Editor.editors,1,1,1,api.EditorSheet)
  32. api.EditorSheet:draw((48-#Editor.editors)+self.curid,(192-8*#Editor.editors)+self.curid*8-8,1)
  33. api.SpriteGroup(55,1,1,4,1,1,1,api.EditorSheet)
  34. end
  35. function Editor:switchEditor(id)
  36. self.Current, self.curid = require("editor."..self.editors[id]), id
  37. if self.Current._switch then self.Current:_switch() end
  38. self:_redraw()
  39. end
  40. function Editor:_update(dt)
  41. local mx, my = api.getMPos()
  42. if api.isInRect(mx,my,ModeGrid) then
  43. if api.isMDown(1) then
  44. api.setCursor("handpress")
  45. else
  46. api.setCursor("handrelease")
  47. end
  48. else
  49. api.setCursor("normal")
  50. end
  51. if self.Current._update then self.Current:_update(dt) end
  52. end
  53. function Editor:_mpress(x,y,b,it)
  54. if self.Current._mpress then self.Current:_mpress(x,y,b,it) end
  55. local cx = api.whereInGrid(x,y,ModeGrid)
  56. if cx then
  57. self:switchEditor(cx)
  58. self:_redraw()
  59. ModeMFlag = true
  60. end
  61. end
  62. function Editor:_mmove(x,y,dx,dy,it)
  63. if self.Current._mmove then self.Current:_mmove(x,y,dx,dy,it) end
  64. local cx = api.whereInGrid(x,y,ModeGrid)
  65. if cx and ModeMFlag then
  66. self:switchEditor(cx)
  67. self:_redraw()
  68. end
  69. end
  70. function Editor:_mrelease(x,y,b,it)
  71. if self.Current._mrelease then self.Current:_mrelease(x,y,b,it) end
  72. local cx = api.whereInGrid(x,y,ModeGrid)
  73. if cx and ModeMFlag then
  74. self:switchEditor(cx)
  75. self:_redraw()
  76. end
  77. ModeMFlag = false
  78. end
  79. function Editor:_tpress(id,x,y,p)
  80. if self.Current._tpress then self.Current:_tpress(id,x,y,p) end
  81. end
  82. function Editor:_tmove(id,x,y,p)
  83. if self.Current._tmove then self.Current:_tmove(id,x,y,p) end
  84. end
  85. function Editor:_trelease(id,x,y,p)
  86. if self.Current._trelease then self.Current:_trelease(id,x,y,p) end
  87. end
  88. local key_for = function(k)
  89. if(love.keyboard.isDown("lalt", "ralt")) then
  90. k = "alt-" .. k
  91. end
  92. if(love.keyboard.isDown("lctrl", "rctrl", "capslock")) then
  93. k = "ctrl-" .. k
  94. end
  95. if(love.keyboard.isDown("lshift", "rshift")) then
  96. k = "shift-" .. k
  97. end
  98. return k
  99. end
  100. function Editor.find_binding(key, mode)
  101. if mode.keymap[key] then return mode.keymap[key], mode end
  102. if mode.parent then return Editor.find_binding(key, mode.parent) end
  103. end
  104. function Editor:_kpress(k,sc,ir)
  105. local command, mode = Editor.find_binding(key_for(k), self.Current)
  106. if command then
  107. command(mode)
  108. mode:_redraw()
  109. elseif self.Current._kpress then
  110. self.Current:_kpress(k,sc,ir)
  111. end
  112. end
  113. Editor.keymap = {
  114. ["ctrl-pageup"] = function(self)
  115. Editor:switchEditor(1 + ((Editor.curid - 2) % #Editor.editors))
  116. end,
  117. ["ctrl-pagedown"] = function(self)
  118. Editor:switchEditor(1 + (Editor.curid % #Editor.editors))
  119. end,
  120. }
  121. function Editor:_krelease(k,sc)
  122. if self.Current._krelease then self.Current:_krelease(k,sc) end
  123. end
  124. function Editor:_tinput(t)
  125. if self.Current._tinput then self.Current:_tinput(t) end
  126. end
  127. return Editor