refactoring and improvements
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 11 Apr 2020 03:47:38 +0000 (22:47 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 11 Apr 2020 03:47:38 +0000 (22:47 -0500)
app.lua
docs/todo
lualib/oop.lua
main.lua
src/globals.lua [new file with mode: 0644]
src/ui.lua
src/ui/components.lua
src/wasm/exprs.lua [new file with mode: 0644]

diff --git a/app.lua b/app.lua
index f1ea2bcee82b2c8498a0f0de410e7f79c561e84d..c735a30f4062254731c8759349dda5b59f79665e 100644 (file)
--- a/app.lua
+++ b/app.lua
@@ -8,94 +8,87 @@ import {
        ui_components = "src.ui.components"; -- Just imported to add the functionality
 
        COLORS = "conf:COLOR_SCHEME";
-}
 
--- Responsible for containing and managing all
--- global-ish objects for the application
-class "Application" {
-       init = function(self)
-               ui_text.register_font("fonts/FiraCode-Regular", 16)
-               ui_text.set_font("fonts/FiraCode-Regular", 16)
-
-               self.ui = with(ui.make_element "root") {
-                       rect = Rectangle(0, 0, 0, 0);
-               }
-
-               self.scroller = with(ui.make_element "scrolling_area") {
-                       rect = Rectangle(0, 0, 1200, 900);
-                       background_color = COLORS.background;
-                       line_color = COLORS.dark_background;
-                       layer = 10;
-               }
-
-               ui.insert_child(self.ui, self.scroller)
-
-               self.wasm = nil
-       end;
-
-       bootstrap = function(self)
-               -- Sets all Love callbacks
-               function love.filedropped(file) self:open_file(file:getFilename()) end
-               function love.update(dt) self:update(dt) end
-               function love.draw() self:draw() end
-
-               function love.mousepressed(x, y, button) ui.mousepressed(self.ui, button, x, y) end
-               function love.mousereleased(x, y, button) ui.mousereleased(self.ui, button, x, y) end
-               function love.mousemoved(x, y, dx, dy) ui.mousemoved(self.ui, x, y, dx, dy) end
-               function love.wheelmoved(dx, dy) ui.wheelmoved(self.ui, dx, dy) end
-               function love.keypressed(key) ui.keypressed(self.ui, key) end
-               function love.keyreleased(key) ui.keyreleased(self.ui, key) end
-               function love.resize(w, h) ui.resize(self.ui, w, h) end
-       end;
-
-       open_file = function(self, path)
-               local wasm_mod = wasm_decompile(path)
-               wasm_mod = wasm_analyze(wasm_mod)
-               self.wasm = wasm_mod
-
-               -- Delete all old children
-               for _, chld in ipairs(self.scroller.children) do
-                       chld.remove = true
-               end
-               ui.fire_propagating_event(self.scroller, "dummy")
+       globals = "src.globals";
+}
 
-               local i = 0
-               for _, func in ipairs(wasm_mod.funcs) do
-                       if not func.imported then
-                               ui.insert_child(self.scroller, with(ui.make_element("function_block", func, wasm_mod)) {
+function init()
+       ui_text.register_font("fonts/FiraCode-Regular", 16)
+       ui_text.set_font("fonts/FiraCode-Regular", 16)
+
+       globals.ui.root = with(ui.make_element "root") {
+               rect = Rectangle(0, 0, 0, 0);
+       }
+
+       globals.ui.scrolling_area = with(ui.make_element "scrolling_area") {
+               rect = Rectangle(0, 0, 1200, 900);
+               background_color = COLORS.background;
+               line_color = COLORS.dark_background;
+               layer = 10;
+       }
+
+       ui.insert_child(globals.ui.root, globals.ui.scrolling_area)
+end
+
+function bootstrap()
+       -- Sets all Love callbacks
+       function love.filedropped(file) open_file(file:getFilename()) end
+       love.update = update
+       love.draw = draw
+
+       function love.mousepressed(x, y, button) ui.mousepressed(globals.ui.root, button, x, y) end
+       function love.mousereleased(x, y, button) ui.mousereleased(globals.ui.root, button, x, y) end
+       function love.mousemoved(x, y, dx, dy) ui.mousemoved(globals.ui.root, x, y, dx, dy) end
+       function love.wheelmoved(dx, dy) ui.wheelmoved(globals.ui.root, dx, dy) end
+       function love.keypressed(key) ui.keypressed(globals.ui.root, key) end
+       function love.keyreleased(key) ui.keyreleased(globals.ui.root, key) end
+       function love.resize(w, h) ui.resize(globals.ui.root, w, h) end
+end
+
+function open_file(path)
+       globals.wasm_module = wasm_decompile(path)
+       globals.wasm_module = wasm_analyze(globals.wasm_module)
+
+       -- Delete all old children
+       for _, chld in ipairs(globals.ui.scrolling_area.children) do
+               chld.remove = true
+       end
+       ui.fire_propagating_event(globals.ui.scrolling_area, "dummy")
+
+       local i = 0
+       for _, func in ipairs(globals.wasm_module.funcs) do
+               if not func.imported then
+                       ui.insert_child(
+                               globals.ui.scrolling_area,
+                               with(ui.make_element("function_block", func, globals.wasm_module)) {
                                        rect = Rectangle(0, i * 22, 400, 400);
-                                       layer = 30 - i
+                                       layer = #globals.wasm_module.funcs - i
                                })
 
-                               i = i + 1
-                               if i > 25 then break end
-                       end
+                       i = i + 1
                end
-       end;
+       end
+end
 
-       update = function(self, dt)
-               if love.keyboard.isDown "escape" then
-                       love.event.quit()
-               end
-
-               ui.update(self.ui, dt)
-       end;
+function update(dt)
+       if love.keyboard.isDown "escape" then
+               love.event.quit()
+       end
 
-       draw = function(self)
-               love.graphics.setBackgroundColor(0, 0, 0)
-               love.graphics.clear()
+       ui.update(globals.ui.root, dt)
+end
 
-               ui.draw(self.ui)
+function draw()
+       love.graphics.setBackgroundColor(0, 0, 0)
+       love.graphics.clear()
 
-               love.graphics.setScissor()
-       end;
-}
+       ui.draw(globals.ui.root)
 
--- Delay the execution of the init function
--- until everything has been imported and
--- resolved
-app = Application()
+       love.graphics.setScissor()
+end
 
 return module {
-       app
+       init = init;
+       bootstrap = bootstrap;
+       open_file = open_file;
 }
index 5ca193e74b8b0c3954d4a6504008e5a286b87b18..f1e4378e1a424b2747ad9d888c939294f1b782b6 100644 (file)
--- a/docs/todo
+++ b/docs/todo
@@ -9,22 +9,11 @@
        * Big window with better details
 
 GOALS FOR TODAY:
-[ ] Get the app back to the state that it was in, but with slightly better code
-[ ] Decide the features I want to implement
+[X] Get the app back to the state that it was in, but with slightly better code
+[X] Decide the features I want to implement
 [ ] Look into how text input works so textboxes can be a thing
 
-FIRST THING:
-[ ] Refactor to not use classes in the ui
-       * UI object will of the form:
+OTHER GOALS:
+[ ] Compacted text view
+       * Parse the expressions and produce a better looking text format
 
-type UIObject a = {
-       type :: string;
-       layer :: string number;
-       data :: a;
-
-       children :: [UiObject b]; // This is sorted by the layer of the object
-}
-
-       * Traversal will be pre-order for everything
-       * Drawing will be split into two parts, pre and post, for whether on the "left" or "right" of the node
-       * Events will be depth first, and return true if they consumed the action, and false if the action should propagate
index bf4a9e8e20affa4ead79dcab0d1489c0826da5e4..43412e39c80f9cef0461d75ec9a14ea2bcba3e78 100755 (executable)
@@ -246,51 +246,6 @@ function enum(obj)
     return obj\r
 end\r
 \r
-function interface(fns)\r
-    if type(fns) == "string" then\r
-        local sub, super = fns:match "(%w+) +extends +(%w+)"\r
-        if sub then fns = sub end\r
-\r
-        if super then\r
-            return helper(fns, super, interface)\r
-        else\r
-            local int = {}\r
-            int = setmetatable(int, {\r
-                __call = helper(fns, nil, interface);\r
-\r
-                __index = function(_, super)\r
-                    return helper(fns, super, interface)\r
-                end;\r
-            })\r
-            return int\r
-        end\r
-    end\r
-\r
-    local int = {}\r
-    for k, v in pairs(fns) do\r
-        if type(v) == "string" then\r
-            int[v] = function(self) end\r
-        end\r
-    end\r
-\r
-    if fns.extends then\r
-        for k, v in pairs(fns.extends) do\r
-            if type(v) == "function" then\r
-                int[k] = function(self) end\r
-            end\r
-        end\r
-    end\r
-\r
-    local mt = {\r
-        __call = function(_, ...)\r
-            error "Interfaces cannot be instatiated with a constructor"\r
-        end;\r
-    }\r
-    int = setmetatable(int, mt)\r
-\r
-    return int\r
-end\r
-\r
 function match(var)\r
     return function(tab)\r
         local ran = false\r
index 72155101d67ab03616283bb7a614061e61c6947f..d2b043a6270facd9c0718c7cd0f20337a1640f77 100644 (file)
--- a/main.lua
+++ b/main.lua
@@ -2,13 +2,14 @@
 require "lualib.oop"
 
 import {
-       app = "app:"
+       app = "app"
 }
 
 function love.load()
-       app:bootstrap()
+       app.init()
+       app.bootstrap()
 
        if arg[2] then
-               app:open_file(arg[2])
+               app.open_file(arg[2])
        end
 end
diff --git a/src/globals.lua b/src/globals.lua
new file mode 100644 (file)
index 0000000..424eb62
--- /dev/null
@@ -0,0 +1,9 @@
+return {
+       ui = {
+               root = {};                                      -- The root ui element
+               scrolling_area = {};            -- The scrolling/panning area of the ui
+       };
+
+       wasm_module = {};                               -- The active decompiled webassembly module
+}
+
index 992de4c6c526278551f2ce9f7a9e15cc4d19096f..d3f48a6c68f4bcb07480a97d7d86e56632b6ad6d 100644 (file)
@@ -5,6 +5,10 @@ import {
 
 components = {}
 function component_lookup(type_, name)
+       if not components[type_] then
+               error("Component " .. type_ .. " not found.")
+       end
+
        if type(components[type_][name]) == "function" then
                return components[type_][name]
        end
index 7b862ad2f8be1d27a7195aea21224fe22b34dc12..4de52b6c25885d7a60a2b39e966bd6f36e5d9ad4 100644 (file)
@@ -5,6 +5,7 @@ import {
        Rectangle = "src.utils:Rectangle";
        wasm_text = "src.wasm.text";
 
+       globals = "src.globals";
        COLORS = "conf:COLOR_SCHEME";
 }
 
@@ -76,11 +77,10 @@ function drag_rect:mousemoved(x, y, dx, dy)
 end
 
 local function_block = { extends = "drag_rect" }
-function function_block:init(wasm_function, wasm_mod)
+function function_block:init(wasm_function)
        self.func = wasm_function
-       self.mod = wasm_mod
 
-       local header_text, body_text = wasm_text.generate_text_format(self.func, self.mod, COLORS)
+       local header_text, body_text = wasm_text.generate_text_format(self.func, globals.wasm_module, COLORS)
        self.header_text = header_text
        self.body_text = body_text
 
@@ -89,7 +89,7 @@ function function_block:init(wasm_function, wasm_mod)
        self.old_height = 400
        self.resize_down = false
 
-       self.canvas = love.graphics.newCanvas(800, 800)
+       self.canvas = love.graphics.newCanvas(1200, 1200)
        self.redraw = true
 end
 
@@ -118,8 +118,8 @@ function function_block:mousereleased(button, x, y)
        if ui.focused == self and button == 2 then self.resize_down = false end
 
        if ui.focused == self and button == 3 then
-               ui.insert_child(self, with(ui.make_element "rect") {
-                       rect = Rectangle(50, 40, 80, 60);
+               ui.insert_child(self, with(ui.make_element "deletable_rect") {
+                       rect = Rectangle(x, y, 80, 60);
                        color = { 1, 0, 0 };
                })
        end
@@ -142,9 +142,9 @@ function function_block:mousemoved(x, y, dx, dy)
        drag_rect.mousemoved(self, x, y, dx, dy)
 
        if self.resize_down then
-               self.rect.w = math.min(800, math.max(100, x))
+               self.rect.w = math.min(1200, math.max(100, x))
                if self.body_visible then
-                       self.rect.h = math.min(800, math.max(22, y))
+                       self.rect.h = math.min(1200, math.max(22, y))
                        self.old_height = self.rect.h
                end
                self.redraw = true
diff --git a/src/wasm/exprs.lua b/src/wasm/exprs.lua
new file mode 100644 (file)
index 0000000..e0dd1af
--- /dev/null
@@ -0,0 +1,9 @@
+import {
+
+}
+
+
+
+return module {
+
+}