Ui = "src.ui.ui:Ui";
UiRegion = "src.ui.ui:UiRegion";
+ UiButton = "src.ui.ui:UiButton";
ScrollingUiRegion = "src.ui.ui:ScrollingUiRegion";
DraggableRect = "src.ui.ui:DraggableRect";
render_text = "src.ui.ui:render_text";
scissor_points = "src.utils:scissor_points";
+ Rectangle = "src.utils:Rectangle";
COLORS = "conf:COLOR_SCHEME";
}
WasmCodeGenerator:setup(func, mod, COLORS)
self.header_text = WasmCodeGenerator:build_header()
+
if func.body then
self.body_text = WasmCodeGenerator:build_body()
else
self.old_height = 400
end;
- onclick = function(self, button, x, y)
- DraggableRect.onclick(self, button, x, y)
+ toggle_body_visible = function(self)
+ self.body_visible = not self.body_visible
+ self.rect.h = self.body_visible and self.old_height or 22
+ end;
- if y <= 24 and button == 2 then
- self.body_visible = not self.body_visible
- self.rect.h = self.body_visible and self.old_height or 22
+ onclick = function(self, button, x, y, ox, oy)
+ DraggableRect.onclick(self, button, x, y, ox, oy)
+
+ if button == 2 then
+ local cmr
+ cmr = FunctionBlockContextMenu(self.ui, ox, oy, {
+ { text = "Toggle open", action = function()
+ cmr.should_delete = true
+ self:toggle_body_visible()
+ end
+ };
+ { text = "Resize", action = function() cmr.should_delete = true end };
+ { text = "Hide", action = function() end; };
+ })
+ self.ui:add_region(cmr)
end
end;
ondrag = function(self, button, x, y, dx, dy)
DraggableRect.ondrag(self, button, x, y, dx, dy)
- if y >= 24 and button == 2 then
+ if y >= 24 and button == 3 then
self.rect.w = math.max(100, x)
if self.body_visible then
self.rect.h = math.max(22, y)
end;
}
+class "FunctionBlockContextMenu" [UiRegion] {
+ init = function(self, ui, cx, cy, options)
+ UiRegion.init(self, ui)
+
+ self.rect.w = 200
+ self.rect.h = 25 * #options
+ self.rect.x = cx - self.rect.w / 2
+ self.rect.y = cy - self.rect.h / 2
+
+ self.background = COLORS.secondary_dark
+
+ for i, opt in ipairs(options) do
+ self:add_object(with(UiButton()) {
+ text = opt.text;
+ background = COLORS.secondary_dark;
+ hover_background = COLORS.secondary;
+ click_background = COLORS.secondary_light;
+ foreground = COLORS.secondary_text;
+ rect = Rectangle(0, (i - 1) * 25, 200, 25);
+ click = opt.action
+ })
+ end
+
+ self.layer = 100
+ end;
+
+ onmouseleave = function(self)
+ self.should_delete = true
+ end;
+}
+
mod = nil
ui = nil
function love.load()
mod = decompile(arg[2])
- ui_region = ScrollingUiRegion()
+ ui_region = ScrollingUiRegion(ui)
ui_region.rect.x = 0
ui_region.rect.y = 0
ui_region.rect.w = 1200
import {
+ pprint = "lualib.pprint";
Rectangle = "src.utils:Rectangle";
uuid = "src.utils:uuidv4";
revipairs = "src.utils:revipairs";
end
if region ~= nil then
- region:onmousedown(button, x - region.rect.x, y - region.rect.y)
+ region:onmousedown(button, x - region.rect.x, y - region.rect.y, x, y)
end
end;
end
if region ~= nil then
- region:onmouseup(button, x - region.rect.x, y - region.rect.y)
+ region:onmouseup(button, x - region.rect.x, y - region.rect.y, x, y)
end
end;
end;
update = function(self, dt)
- for _, region in ipairs(self.regions) do
- region:update(dt);
+ for i, region in ipairs(self.regions) do
+ region:update(dt)
+
+ if region.should_delete then
+ table.remove(self.regions, i)
+ end
end
end;
}
class "UiRegion" {
- init = function(self)
+ init = function(self, ui)
self.active = false
+ self.ui = ui
self.selected_object = nil
self.objects = {}
-- Internals
self.is_mouse_down = false
self.fire_onclick = false
+ self.should_delete = false
+ self.hovered_object = nil
end;
add_object = function(self, obj)
+ obj.ui = self.ui
table.insert(self.objects, obj)
end;
return obj.rect
end;
- onmousedown = function(self, button, x, y)
+ onmousedown = function(self, button, x, y, ox, oy)
self.is_mouse_down = button
self.fire_onclick = true
local new_selected = nil
self.selected_object:onselect()
end
end
+
+ if self.selected_object then
+ self.selected_object:onmousedown(button, x, y, ox, oy)
+ end
end;
- onmouseup = function(self, button, x, y)
+ onmouseup = function(self, button, x, y, ox, oy)
self.is_mouse_down = false
if self.selected_object ~= nil and self.fire_onclick then
local obj_rect = self:_obj_rect(self.selected_object)
- self.selected_object:onclick(button, x - obj_rect.x, y - obj_rect.y)
+ self.selected_object:onclick(button, x - obj_rect.x, y - obj_rect.y, ox, oy)
end
end;
onmousemove = function(self, x, y, dx, dy)
+ for _, obj in revipairs(self.objects) do
+ if self:_obj_rect(obj):contains(x, y) then
+ if self.hovered_object and obj ~= self.hovered_object then
+ self.hovered_object:onunhover()
+ end
+
+ obj:onhover(x, y)
+ self.hovered_object = obj
+ break
+ end
+ end
+
if self.is_mouse_down and self.selected_object ~= nil then
local obj_rect = self:_obj_rect(self.selected_object)
self.selected_object:ondrag(self.is_mouse_down, x - obj_rect.x, y - obj_rect.y, dx, dy)
}
class "ScrollingUiRegion" [UiRegion] {
- init = function(self)
- UiRegion.init(self)
+ init = function(self, ui)
+ UiRegion.init(self, ui)
self.offset = { x = 0; y = 0; }
self.line_color = { 0, 0, 0 }
((y + self.rect.y) - self.rect.h / 2) / self.zoom + self.rect.h / 2 - self.offset.y - self.rect.y
end;
- onmousedown = function(self, button, x, y)
+ onmousedown = function(self, button, x, y, ox, oy)
local mx, my = self:_transform_mouse(x, y)
- UiRegion.onmousedown(self, button, mx, my)
+ UiRegion.onmousedown(self, button, mx, my, ox, oy)
end;
- onmouseup = function(self, button, x, y)
+ onmouseup = function(self, button, x, y, ox, oy)
local mx, my = self:_transform_mouse(x, y)
- UiRegion.onmouseup(self, button, mx, my)
+ UiRegion.onmouseup(self, button, mx, my, ox, oy)
end;
onmousemove = function(self, x, y, dx, dy)
if self.selected_object == nil then
self.zoom = self.zoom * (dy > 0 and 1.05 or (1 / 1.05))
- if self.zoom >= 1 then self.zoom = 1 end
- if self.zoom <= (1 / 1.05) ^ 30 then self.zoom = (1 / 1.05) ^ 30 end
+ -- if self.zoom >= 1 then self.zoom = 1 end
+ if self.zoom <= 0.2 then self.zoom = 0.2 end
end
end;
self.uuid = uuid()
self.selected = false
self.order = math.random(1, 1024)
+ self.ui = {}
end;
- onclick = function(self, button, x, y) end;
+ onclick = function(self, button, x, y, ox, oy) end;
ondrag = function(self, button, x, y, dx, dy) end;
+ onhover = function(self, x, y) end;
+ onunhover = function(self) end;
onwheel = function(self, dx, dy) end;
+ onmousedown = function(self, button, x, y, ox, oy)
+ self.selected = true
+ end;
onselect = function(self)
self.selected = true
onkey = function(self, button) end;
- update = function(dt) end;
- draw = function() end;
+ update = function(self, dt) end;
+ draw = function(self) end;
}
-class "DraggableRect" [UiObject] {
- init = function(self, x, y, w, h)
+class "UiButton" [UiObject] {
+ init = function(self)
UiObject.init(self)
- self.rect = Rectangle(x, y, w, h)
+ self.text = "Placeholder"
+ self.hovered = false
+ self.background = { 0, 0, 0 }
+ self.hover_background = { 0.2, 0.2, 0.2 }
+ self.click_background = { 0.4, 0.4, 0.4 }
+ self.foreground = { 1, 1, 1 }
end;
- ondrag = function(self, button, x, y, dx, dy)
- if button == 1 then
- self.rect.x = self.rect.x + dx
- self.rect.y = self.rect.y + dy
+ onclick = function(self, button, x, y, ox, oy)
+ if button == 1 and self.click then
+ self.click(self, x, y)
end
+ self.selected = false
+ end;
+
+ onhover = function(self, x, y)
+ self.hovered = true
+ end;
+
+ onunhover = function(self)
+ self.hovered = false
end;
draw = function(self)
if self.selected then
- love.graphics.setColor(0, 0, 1)
- love.graphics.rectangle("fill", -2, -2, self.rect.w + 4, self.rect.h + 4)
+ love.graphics.setColor(self.click_background)
+ elseif self.hovered then
+ love.graphics.setColor(self.hover_background)
+ else
+ love.graphics.setColor(self.background)
end
- love.graphics.setColor(1, 1, 1)
love.graphics.rectangle("fill", 0, 0, self.rect.w, self.rect.h)
- render_text(0, 0, {
- { text = "1 Hello ", color = { 1, 0, 0 } };
- { text = "world", color = { 0, 0, 1 } };
- { newline = true };
- { text = "2 Nother line", color = { 0, 0.5, 0 } };
- { newline = true };
- { newline = true };
- { text = "3 nother Nother line", color = { 0, 0.5, 0 } };
- })
+ love.graphics.setColor(self.foreground)
+ love.graphics.printf(self.text, 2, 2, self.rect.w, "center")
+ end;
+}
+
+class "DraggableRect" [UiObject] {
+ init = function(self, x, y, w, h)
+ UiObject.init(self)
+
+ self.rect = Rectangle(x, y, w, h)
+ end;
+
+ ondrag = function(self, button, x, y, dx, dy)
+ if button == 1 then
+ self.rect.x = self.rect.x + dx
+ self.rect.y = self.rect.y + dy
+ end
end;
}
UiRegion = UiRegion;
ScrollingUiRegion = ScrollingUiRegion;
UiObject = UiObject;
+ UiButton = UiButton;
DraggableRect = DraggableRect;
render_text = render_text;