From 90f2f81a14d2647bd1df035727c5cdfbd5523dc4 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 10 Apr 2020 22:47:38 -0500 Subject: [PATCH] refactoring and improvements --- app.lua | 147 ++++++++++++++++++++---------------------- docs/todo | 21 ++---- lualib/oop.lua | 45 ------------- main.lua | 7 +- src/globals.lua | 9 +++ src/ui.lua | 4 ++ src/ui/components.lua | 16 ++--- src/wasm/exprs.lua | 9 +++ 8 files changed, 109 insertions(+), 149 deletions(-) create mode 100644 src/globals.lua create mode 100644 src/wasm/exprs.lua diff --git a/app.lua b/app.lua index f1ea2bc..c735a30 100644 --- 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; } diff --git a/docs/todo b/docs/todo index 5ca193e..f1e4378 100644 --- 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 diff --git a/lualib/oop.lua b/lualib/oop.lua index bf4a9e8..43412e3 100755 --- a/lualib/oop.lua +++ b/lualib/oop.lua @@ -246,51 +246,6 @@ function enum(obj) return obj end -function interface(fns) - if type(fns) == "string" then - local sub, super = fns:match "(%w+) +extends +(%w+)" - if sub then fns = sub end - - if super then - return helper(fns, super, interface) - else - local int = {} - int = setmetatable(int, { - __call = helper(fns, nil, interface); - - __index = function(_, super) - return helper(fns, super, interface) - end; - }) - return int - end - end - - local int = {} - for k, v in pairs(fns) do - if type(v) == "string" then - int[v] = function(self) end - end - end - - if fns.extends then - for k, v in pairs(fns.extends) do - if type(v) == "function" then - int[k] = function(self) end - end - end - end - - local mt = { - __call = function(_, ...) - error "Interfaces cannot be instatiated with a constructor" - end; - } - int = setmetatable(int, mt) - - return int -end - function match(var) return function(tab) local ran = false diff --git a/main.lua b/main.lua index 7215510..d2b043a 100644 --- 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 index 0000000..424eb62 --- /dev/null +++ b/src/globals.lua @@ -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 +} + diff --git a/src/ui.lua b/src/ui.lua index 992de4c..d3f48a6 100644 --- a/src/ui.lua +++ b/src/ui.lua @@ -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 diff --git a/src/ui/components.lua b/src/ui/components.lua index 7b862ad..4de52b6 100644 --- a/src/ui/components.lua +++ b/src/ui/components.lua @@ -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 index 0000000..e0dd1af --- /dev/null +++ b/src/wasm/exprs.lua @@ -0,0 +1,9 @@ +import { + +} + + + +return module { + +} -- 2.25.1