forked from HDictus/hump
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegmentsinput.lua
More file actions
149 lines (131 loc) · 3.84 KB
/
segmentsinput.lua
File metadata and controls
149 lines (131 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
local pack = require("pack-utils")
local utf8 = require("utf8")
KEYPRESSED_DT = .1
local function rect_collides(rect)
return function(x, y)
return x >= rect.x and y >= rect.y and
x <= rect.x + rect.w and y <= rect.y + rect.h
end
end
return function(segments, set_dirty)
local rect = { x = 16, y = 16, w = 128, h = 256 }
local colors = {
background = { .8, .8, .8, .5 },
border = { .2, .2, .2, .5 },
text = { .2, .2, .2 }
}
local text, cursor
local active, visible, dirty
local keypressed, keypressed_timer
local function import()
text = ''
for _, xs, ys, xgs, ygs in pack.ipairs(segments, 4) do
text = text .. string.format('%d %d %d %d\n', xs, ys, xgs, ygs)
end
cursor = utf8.offset(text, -1)
end
local function open()
active = true
end
local function close()
active = false
do
local exp = {}
for value in string.gmatch(text, "%d+") do
table.insert(exp, tonumber(value))
end
if #segments % 4 == 0 then
for k, _ in pairs(segments) do
segments[k] = nil
end
for _, value in ipairs(exp) do
table.insert(segments, value)
end
end
end
import()
set_dirty()
end
local function write(t)
text = string.sub(text, 0, cursor - 1) .. t .. string.sub(text, cursor)
cursor = cursor + #t
end
local function movecursor(xmove, ymove)
cursor = cursor + xmove + ymove * 8
end
local segmentsinput = {}
function segmentsinput.init()
import()
active = false
visible = true
dirty = false
end
function segmentsinput.draw()
if visible then
love.graphics.setColor(colors.background)
love.graphics.rectangle("fill", rect.x, rect.y, rect.w, rect.h)
if active then
love.graphics.setColor(colors.border)
love.graphics.rectangle("line", rect.x, rect.y, rect.w, rect.h)
end
love.graphics.setColor(colors.text)
local draw_text = string.sub(text, 0, cursor - 1) .. "|" .. string.sub(text, cursor)
love.graphics.printf(draw_text, rect.x, rect.y, rect.w, "left")
end
end
function segmentsinput.update(dt)
if dirty then import() dirty = false end
if keypressed_timer ~= nil then
keypressed_timer = keypressed_timer + dt
if keypressed_timer >= KEYPRESSED_DT then
if keypressed == "left" then movecursor(-1, 0)
elseif keypressed == "right" then movecursor(1, 0)
elseif keypressed == "up" then movecursor(0, -1)
elseif keypressed == "down" then movecursor(0, 1)
elseif keypressed == "backspace" then
-- delete char
local byteoffset = utf8.offset(text, cursor)
if byteoffset then
text = string.sub(text, 1, byteoffset - 2) ..
string.sub(text, byteoffset)
cursor = cursor - 1
end
elseif keypressed == "escape" then close()
elseif keypressed == "kpenter" then write("\n")
end
keypressed_timer = keypressed_timer - KEYPRESSED_DT
end
end
end
function segmentsinput.keypressed(key)
if active then
if key == "v" and love.keyboard.isDown("lctrl") then
write(love.system.getClipboardText())
else
if key == "kpenter" then write("\n") end
keypressed = key
keypressed_timer = 0
end
return true
end
return false
end
function segmentsinput.keyreleased(key)
if keypressed == key then keypressed = nil end
end
function segmentsinput.mousepressed(x, y, button)
if button == 1 then
if rect_collides(rect)(x, y) then open() return true end
if active then
active = false
close()
end
return false
end
end
function segmentsinput.textinput(t)
if active then write(t) end
end
function segmentsinput.dirty() dirty = true end
return segmentsinput
end