2
0
bjorn 9 жил өмнө
parent
commit
46a79504b3

+ 12 - 10
app/editor/editor.lua → app/editor/context.lua

@@ -1,21 +1,21 @@
-Editor = class()
+local Editor = class()
 
 function Editor:load(options)
   self.options = options
-  self.grid = EditorGrid()
+  self.grid = app.editor.grid()
 
-  self.view = EditorView()
+  self.event = app.core.event()
+  self.view = app.editor.view()
   self.effects = app.core.effects()
   self.effects:remove('deathDesaturate')
-  self.event = app.core.event()
   self.collision = app.core.collision()
   self.map = app.map()
-  self.dragger = EditorDragger()
-  self.scaler = EditorScaler()
-  self.selector = EditorSelector()
-  self.deletor = EditorDeletor()
-  self.saver = EditorSaver()
-  self.debug = EditorDebug()
+  self.dragger = app.editor.dragger()
+  self.scaler = app.editor.scaler()
+  self.selector = app.editor.selector()
+  self.deletor = app.editor.deletor()
+  self.saver = app.editor.saver()
+  self.debug = app.editor.debug()
 
   self.widgets = {self.grid}
   self.components = {
@@ -74,3 +74,5 @@ end
 function Editor:resize()
   self.view:resize()
 end
+
+return Editor

+ 5 - 3
app/editor/editordebug.lua → app/editor/debug.lua

@@ -1,13 +1,13 @@
-EditorDebug = class()
+local Debug = class()
 
 local g = love.graphics
 
-function EditorDebug:init()
+function Debug:init()
   self.depth = -1000000
   if ctx.view then ctx.view:register(self, 'gui') end
 end
 
-function EditorDebug:gui()
+function Debug:gui()
   local x = math.floor(ctx.view:worldMouseX() / ctx.grid.size) * ctx.grid.size
   local y = math.floor(ctx.view:worldMouseY() / ctx.grid.size) * ctx.grid.size
   local message = x .. ',' .. y
@@ -19,3 +19,5 @@ function EditorDebug:gui()
   g.setColor(255, 255, 255)
   g.print(message, w - g.getFont():getWidth(message), h - g.getFont():getHeight())
 end
+
+return Debug

+ 4 - 2
app/editor/editordeletor.lua → app/editor/deletor.lua

@@ -1,8 +1,8 @@
-EditorDeletor = class()
+local Deletor = class()
 
 local function invoke(x, k, ...) return x.editor[k](x, ...) end
 
-function EditorDeletor:keypressed(key)
+function Deletor:keypressed(key)
   if key == 'delete' then
     table.each(ctx.map.props, function(p)
       if math.inside(ctx.view:worldMouseX(), ctx.view:worldMouseY(), invoke(p, 'boundingBox')) then
@@ -14,3 +14,5 @@ function EditorDeletor:keypressed(key)
     end)
   end
 end
+
+return Deletor

+ 7 - 5
app/editor/editordragger.lua → app/editor/dragger.lua

@@ -1,8 +1,8 @@
-EditorDragger = class()
+local Dragger = class()
 
 local function invoke(x, k, ...) return x.editor[k](x, ...) end
 
-function EditorDragger:init()
+function Dragger:init()
   self.dragging = false
   self.dragX = 0
   self.dragY = 0
@@ -10,7 +10,7 @@ function EditorDragger:init()
   self.deselect = false
 end
 
-function EditorDragger:update()
+function Dragger:update()
   if self.dragging then
     ctx.selector:each(function(prop)
       local ox, oy = ctx.grid:snap(prop._dragX, prop._dragY)
@@ -21,7 +21,7 @@ function EditorDragger:update()
   end
 end
 
-function EditorDragger:mousepressed(x, y, button)
+function Dragger:mousepressed(x, y, button)
   if button == 'l' then
     self.deselect = false
     if love.keyboard.isDown('lshift') then return end
@@ -39,7 +39,9 @@ function EditorDragger:mousepressed(x, y, button)
   end
 end
 
-function EditorDragger:mousereleased(x, y, button)
+function Dragger:mousereleased(x, y, button)
   self.dragging = false
   if self.deselect then ctx.selector:deselectAll() end
 end
+
+return Dragger

+ 8 - 6
app/editor/editorgrid.lua → app/editor/grid.lua

@@ -1,16 +1,16 @@
-EditorGrid = class()
-EditorGrid.name = 'Grid'
+local Grid = class()
+Grid.name = 'Grid'
 
 local g = love.graphics
 
-function EditorGrid:init()
+function Grid:init()
   self.color = {0, 0, 0, 50}
   self.hoverColor = {0, 0, 0, 20}
   self.size = 32
   self.depth = -10000
 end
 
-function EditorGrid:draw()
+function Grid:draw()
   g.setLineWidth(1 / ctx.view.scale)
 
   g.setColor(self.hoverColor)
@@ -32,12 +32,14 @@ function EditorGrid:draw()
   g.setLineWidth(1)
 end
 
-function EditorGrid:keypressed(key)
+function Grid:keypressed(key)
   if key == '[' then self.size = math.max(self.size / 2, 8)
   elseif key == ']' then self.size = math.min(self.size * 2, 256) end
 end
 
-function EditorGrid:snap(x, y)
+function Grid:snap(x, y)
   if love.keyboard.isDown('lalt') then return x, y end
   return math.round(x / self.size) * self.size, math.round(y / self.size) * self.size
 end
+
+return Grid

+ 5 - 3
app/editor/editorsaver.lua → app/editor/saver.lua

@@ -1,12 +1,12 @@
-EditorSaver = class()
+local Saver = class()
 
-function EditorSaver:keypressed(key)
+function Saver:keypressed(key)
   if key == 's' and love.keyboard.isDown('lctrl') then
     self:save()
   end
 end
 
-function EditorSaver:save()
+function Saver:save()
   local str = 'return {'
   table.each(ctx.map.props, function(p)
     str = str .. p
@@ -15,3 +15,5 @@ function EditorSaver:save()
   love.filesystem.createDirectory('maps/' .. ctx.map.code)
   love.filesystem.write('maps/' .. ctx.map.code .. '/props.lua', str)
 end
+
+return Saver

+ 8 - 6
app/editor/editorscaler.lua → app/editor/scaler.lua

@@ -1,8 +1,8 @@
-EditorScaler = class()
+local Scaler = class()
 
 local function invoke(x, k, ...) return x.editor[k](x, ...) end
 
-function EditorScaler:init()
+function Scaler:init()
   self.scaling = nil
   self.scaleOriginX = 0
   self.scaleOriginY = 0
@@ -12,7 +12,7 @@ function EditorScaler:init()
   ctx.view:register(self)
 end
 
-function EditorScaler:update()
+function Scaler:update()
   if self.scaling then
     local prop = self.scaling
     local mx, my = ctx.view:worldPoint(love.mouse.getPosition())
@@ -36,7 +36,7 @@ function EditorScaler:update()
   end
 end
 
-function EditorScaler:draw()
+function Scaler:draw()
   if self.scaling and self.scaling.collision.shape == 'rectangle' then
     love.graphics.setColor(0, 255, 255, 200)
     local x, y, w, h = self.scaling.x, self.scaling.y, self.scaling.width, self.scaling.height
@@ -53,7 +53,7 @@ function EditorScaler:draw()
   end
 end
 
-function EditorScaler:mousepressed(x, y, button)
+function Scaler:mousepressed(x, y, button)
   if button == 'r' and not love.keyboard.isDown('lshift') then
     local p = ctx.selector:pointTest(x, y)[1]
     if p then
@@ -75,6 +75,8 @@ function EditorScaler:mousepressed(x, y, button)
   end
 end
 
-function EditorScaler:mousereleased(x, y, button)
+function Scaler:mousereleased(x, y, button)
   self.scaling = nil
 end
+
+return Scaler

+ 18 - 16
app/editor/editorselector.lua → app/editor/selector.lua

@@ -1,9 +1,9 @@
-EditorSelector = class()
-EditorSelector.doubleClickSpeed = .25
+local Selector = class()
+Selector.doubleClickSpeed = .25
 
 local function invoke(x, k, ...) return x.editor[k](x, ...) end
 
-function EditorSelector:init()
+function Selector:init()
   self.selection = {}
   self.active = false
   self.lastClick = tick
@@ -16,14 +16,14 @@ function EditorSelector:init()
   ctx.view:register(self, 'gui')
 end
 
-function EditorSelector:update()
+function Selector:update()
   self.active = love.keyboard.isDown('lshift')
   if self.active and love.mouse.isDown('l', 'r') and math.distance(self.dragStartX, self.dragStartY, love.mouse.getPosition()) > 5 then
     self.dragging = true
   end
 end
 
-function EditorSelector:draw()
+function Selector:draw()
   love.graphics.setColor(0, 255, 255, 100)
   for _, prop in ipairs(self.selection) do
     if prop.shape then prop.shape:draw('fill') end
@@ -37,7 +37,7 @@ function EditorSelector:draw()
   end
 end
 
-function EditorSelector:gui()
+function Selector:gui()
   if self.active and self.dragging then
     if love.keyboard.isDown('lctrl') then
       love.graphics.setColor(0, 255, 255, 150)
@@ -53,12 +53,12 @@ function EditorSelector:gui()
   end
 end
 
-function EditorSelector:pointTest(x, y)
+function Selector:pointTest(x, y)
   local shapes = ctx.collision.hc:shapesAt(ctx.view:worldPoint(x, y))
   return table.map(shapes, function(s) return s.owner end)
 end
 
-function EditorSelector:rectTest(x1, y1, x2, y2)
+function Selector:rectTest(x1, y1, x2, y2)
   if x1 > x2 then x1, x2 = x2, x1 end
   if y1 > y2 then y1, y2 = y2, y1 end
   x1, y1 = ctx.view:worldPoint(x1, y1)
@@ -77,7 +77,7 @@ function EditorSelector:rectTest(x1, y1, x2, y2)
   return res
 end
 
-function EditorSelector:lineTest(x1, y1, x2, y2)
+function Selector:lineTest(x1, y1, x2, y2)
   x1, y1 = ctx.view:worldPoint(x1, y1)
   x2, y2 = ctx.view:worldPoint(x2, y2)
   local dis = math.distance(x1, y1, x2, y2)
@@ -93,7 +93,7 @@ function EditorSelector:lineTest(x1, y1, x2, y2)
   return res
 end
 
-function EditorSelector:mousepressed(x, y, button)
+function Selector:mousepressed(x, y, button)
   local function doubleClick()
     return button == self.lastButton and (tick - self.lastClick) * tickRate <= self.doubleClickSpeed
   end
@@ -123,7 +123,7 @@ function EditorSelector:mousepressed(x, y, button)
   end
 end
 
-function EditorSelector:mousereleased(x, y, button)
+function Selector:mousereleased(x, y, button)
   if self.active and self.dragging then
     local selector = love.keyboard.isDown('lctrl') and self.lineTest or self.rectTest
     local targets = selector(self, self.dragStartX, self.dragStartY, x, y)
@@ -136,7 +136,7 @@ function EditorSelector:mousereleased(x, y, button)
   end
 end
 
-function EditorSelector:select(prop, ...)
+function Selector:select(prop, ...)
   if not prop then return end
 
   if not self.selection[prop] then
@@ -147,7 +147,7 @@ function EditorSelector:select(prop, ...)
   return self:select(...)
 end
 
-function EditorSelector:deselect(prop, ...)
+function Selector:deselect(prop, ...)
   if not prop then return end
 
   if self.selection[prop] then
@@ -161,16 +161,18 @@ function EditorSelector:deselect(prop, ...)
   return self:deselect(...)
 end
 
-function EditorSelector:selectAll()
+function Selector:selectAll()
   self:select(unpack(ctx.map.props))
 end
 
-function EditorSelector:deselectAll()
+function Selector:deselectAll()
   self:deselect(unpack(ctx.map.props))
 end
 
-function EditorSelector:each(fn)
+function Selector:each(fn)
   for i = 1, #self.selection do
     fn(self.selection[i])
   end
 end
+
+return Selector

+ 7 - 5
app/editor/editorview.lua → app/editor/view.lua

@@ -1,6 +1,6 @@
-EditorView = extend(app.core.view)
+local View = extend(app.core.view)
 
-function EditorView:init()
+function View:init()
   self.targetScale = 1
 
   self.xVel = 0
@@ -10,7 +10,7 @@ function EditorView:init()
   app.core.view.init(self)
 end
 
-function EditorView:update()
+function View:update()
   app.core.view.update(self)
 
   if not love.keyboard.isDown('lctrl') then
@@ -43,7 +43,7 @@ function EditorView:update()
   self.y = self.y + (prevh - self.height) * yf
 end
 
-function EditorView:mousepressed(x, y, button)
+function View:mousepressed(x, y, button)
   if button == 'wu' then
     self.targetScale = math.min(self.targetScale + .2, 4)
   elseif button == 'wd' then
@@ -51,4 +51,6 @@ function EditorView:mousepressed(x, y, button)
   end
 end
 
-EditorView.contain = f.empty
+View.contain = f.empty
+
+return View

+ 1 - 1
data/menu/menumain.lua

@@ -52,5 +52,5 @@ end
 
 function MenuMain:edit()
   app.core.context:remove(ctx)
-  app.core.context:add(Editor, ctx.options.data)
+  app.core.context:add(app.editor.context, ctx.options.data)
 end