Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// LOVE / libs
"utf8",
"lfs",
"compy",
// "pending",
// busted
"describe",
Expand All @@ -19,10 +20,16 @@
"stop",
"pause",
"continue",
"readfile",
"readlines",
"writefile",
// luautils
"noop",
"identity",
"prequire",
"codeload",
"error_test",
"parse_int",
// end of array
""
],
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ contents.

- `readfile(file)`

Open _file_ and display it's contents.
Open _file_ and return it's contents as a single string.

- `readlines(file)`

Open _file_ and return a string table of it's lines.

- `writefile(file, content)`

Expand Down
161 changes: 115 additions & 46 deletions src/controller/consoleController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ end

--- @private
--- @param name string
--- @return string[]?
--- @return string?
function ConsoleController:_readfile(name)
local PS = self.model.projects
local p = PS.current
Expand All @@ -147,6 +147,16 @@ function ConsoleController:_readfile(name)
end
end

--- @private
--- @param name string
--- @return string[]?
function ConsoleController:_readlines(name)
local s = self:_readfile(name)
if s then
return string.lines(s)
end
end

--- @private
--- @param name string
--- @param content string[]
Expand All @@ -159,6 +169,54 @@ function ConsoleController:_writefile(name, content)
return p:writefile(name, text)
end

function ConsoleController:writefile(name, content)
local P = self.model.projects
local p = P.current
local fpath = p:get_path(name)
local ex = FS.exists(fpath)
if ex then
-- TODO: confirm overwrite
end
local ok, err = self:_writefile(name, content)
if ok then
print(name .. ' written')
else
print(err)
end
end

--- Wrap `f` with errhand if passed, and set target canvas
--- @param f function
--- @param errhand function?
--- @return function wrapped_handler
function ConsoleController:wrap_handler(f, errhand)
local eh = errhand or identity
return function(...)
local args = { ... }
self:use_canvas(
function()
return eh(f, self, unpack(args))
end
)
end
end

--- @param name string
--- @return function? handler
function ConsoleController:get_compy_handler(name)
local env = self:get_project_env()
if not env then return end
local active_compy = env['compy']
if not active_compy then return end
local handler = active_compy[name]
if not handler then
return
else
return handler
end
end

--- @param name string
function ConsoleController:run_project(name)
if love.state.app_state == 'inspect' or
love.state.app_state == 'running'
Expand Down Expand Up @@ -237,6 +295,25 @@ end

-- Set up audio table
local compy_audio = require("util.audio")
local get_compy_terminal = function(terminal)
return {
--- @param x number
--- @param y number
gotoxy = function(x, y)
return terminal:move_to(x, y)
end,
show_cursor = function()
return terminal:show_cursor()
end,
hide_cursor = function()
return terminal:hide_cursor()
end,
clear = function()
terminal:move_to(1, 1)
return terminal:clear()
end
}
end

function ConsoleController.prepare_env(cc)
local prepared = cc.main_env
Expand Down Expand Up @@ -325,28 +402,21 @@ function ConsoleController.prepare_env(cc)
end

--- @param name string
--- @return string[]?
--- @return string?
prepared.readfile = function(name)
return check_open_pr(cc._readfile, cc, name)
end

--- @param name string
--- @return string[]?
prepared.readlines = function(name)
return check_open_pr(cc._readlines, cc, name)
end

--- @param name string
--- @param content string[]
prepared.writefile = function(name, content)
return check_open_pr(function()
local p = P.current
local fpath = p:get_path(name)
local ex = FS.exists(fpath)
if ex then
-- TODO: confirm overwrite
end
local ok, err = cc:_writefile(name, content)
if ok then
print(name .. ' written')
else
print(err)
end
end)
return check_open_pr(cc.writefile, cc, name, content)
end

--- @param name string
Expand All @@ -360,22 +430,7 @@ function ConsoleController.prepare_env(cc)

local terminal = cc.model.output.terminal
local compy_namespace = {
terminal = {
--- @param x number
--- @param y number
gotoxy = function(x, y)
return terminal:move_to(x, y)
end,
show_cursor = function()
return terminal:show_cursor()
end,
hide_cursor = function()
return terminal:hide_cursor()
end,
clear = function()
return terminal:clear()
end
},
terminal = get_compy_terminal(terminal),
audio = compy_audio,
}
prepared.compy = compy_namespace
Expand Down Expand Up @@ -405,11 +460,6 @@ function ConsoleController.prepare_project_env(cc)
local cfg = cc.model.cfg
---@type table
local project_env = cc:get_pre_env_c()
project_env.gfx = love.graphics

project_env.compy = {
audio = compy_audio
}

project_env.require = function(name)
return project_require(name)
Expand All @@ -421,6 +471,26 @@ function ConsoleController.prepare_project_env(cc)
-- return project_require(name, 'run')
-- end

--- @param name string
--- @return string?
project_env.readfile = function(name)
--- @diagnostic disable-next-line: invisible
return cc:_readfile(name)
end

--- @param name string
--- @return string[]?
project_env.readlines = function(name)
--- @diagnostic disable-next-line: invisible
return cc:_readlines(name)
end

--- @param name string
--- @param content string[]
project_env.writefile = function(name, content)
return cc:writefile(name, content)
end

--- @param msg string?
project_env.pause = function(msg)
cc:suspend_run(msg)
Expand Down Expand Up @@ -519,6 +589,14 @@ function ConsoleController.prepare_project_env(cc)
return cc:edit(name)
end

project_env.gfx = love.graphics

project_env.compy = {
audio = compy_audio,
terminal = get_compy_terminal(cc.model.output.terminal),
text_input = input_text
}

project_env.eval = LANG.eval
project_env.print_eval = LANG.print_eval

Expand Down Expand Up @@ -811,15 +889,6 @@ function ConsoleController:finish_edit()
self.editor:close()
local ok = true
local errs = {}
-- local bfs = self.editor:close()
-- for _, bc in ipairs(bfs) do
-- local name, newcontent = bc.name, bc.content
-- local bok, err = self:_writefile(name, newcontent)
-- if not bok then
-- ok = false
-- table.insert(errs, err)
-- end
-- end
if ok then
love.state.app_state = love.state.prev_state
love.state.prev_state = nil
Expand Down
30 changes: 15 additions & 15 deletions src/controller/controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ local _supported = {
'mousepressed',
'mousereleased',
'wheelmoved',
--- custom handlers
'singleclick',
'doubleclick',

'touchmoved',
'touchpressed',
Expand Down Expand Up @@ -123,7 +120,7 @@ local set_handlers = function(userlove, C)
end
end

local click_delay = 0.2
local click_delay = 0.4
local drift_tolerance = 2.5

local click_count = 0
Expand Down Expand Up @@ -351,8 +348,8 @@ Controller = {
-- update --
--------------
--- @private
--- @param C ConsoleController
set_love_update = function(C)
--- @param CC ConsoleController
set_love_update = function(CC)
local function update(dt)
if love.PROFILE then
Prof.update()
Expand All @@ -363,22 +360,25 @@ Controller = {
if click_timer <= 0 then
if click_count == 1 then
-- single click confirmed after delay
local handler = love.singleclick
local handler = CC:get_compy_handler('singleclick')
if handler then
local h = CC:wrap_handler(handler, wrap)
local x, y = love.mouse.getPosition()
local cur = { x = x, y = y }
if no_drift(click_pos, cur) then
handler(x, y)
h(x, y)
end
end
elseif click_count >= 2 then
-- double click detected
local dbl_handler = love.doubleclick
local dbl_handler =
CC:get_compy_handler('doubleclick')
if dbl_handler then
local h = CC:wrap_handler(dbl_handler, wrap)
local x, y = love.mouse.getPosition()
local cur = { x = x, y = y }
if no_drift(click_pos, cur) then
dbl_handler(x, y)
h(x, y)
end
end
end
Expand All @@ -391,7 +391,7 @@ Controller = {
local draw = function()
if ldr then
gfx.push('all')
wrap(ldr, C)
wrap(ldr, CC)
gfx.pop()
end
local ui = get_user_input()
Expand All @@ -403,20 +403,20 @@ Controller = {
View.prev_draw = draw
love.draw = draw
end
C:pass_time(dt)
CC:pass_time(dt)

local uup = Controller._userhandlers.update
if user_update and uup
then
C:use_canvas(function()
wrap(uup, C, dt)
CC:use_canvas(function()
wrap(uup, CC, dt)
end)
end
if love.state.app_state == 'snapshot' then
gfx.captureScreenshot(function(img)
local snap = gfx.newImage(img)
View.snapshot = snap
C:suspend()
CC:suspend()
end)
end

Expand Down
4 changes: 2 additions & 2 deletions src/examples/paint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ Double clicks/taps are a workable solution, but there is a problem: detecting th
To solve for this, we created custom handlers for single and double clicks:

```lua
function love.singleclick(x, y)
function compy.singleclick(x, y)
point(x, y, 1)
end

function love.doubleclick(x, y)
function compy.doubleclick(x, y)
point(x, y, 2)
end
```
Expand Down
4 changes: 2 additions & 2 deletions src/examples/paint/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,11 @@ function point(x, y, btn)
end
end

function love.singleclick(x, y)
function compy.singleclick(x, y)
point(x, y, 1)
end

function love.doubleclick(x, y)
function compy.doubleclick(x, y)
point(x, y, 2)
end

Expand Down
Loading
Loading