I've recently tried to make a custom Object API, which includes a customized Read()
Code:
Spoiler
Object = {}
Object.InputBox = function( _, Tbl )
Tbl = Tbl or {
Position = { X = 2, Width = 10, Y = 2, },
Color = { Background = colors.black, Foreground = colors.yellow, Foreground_After = colors.white, Background_After = colors.black },
Replace = nil,
}
Tbl.Position.Width = Tbl.Position.Width + 1
Tbl.Current = Tbl.Current or {}
Tbl.Current.Text = Tbl.Current.Text or ''
Tbl.Current.TextPos = Tbl.Current.TextPos or 0
local Width, Height = term.getSize()
term.setCursorPos( Tbl.Position.X, Tbl.Position.Y )
term.setCursorBlink( true )
term.setTextColor( Tbl.Color.Foreground )
term.setBackgroundColor( Tbl.Color.Background )
-- Draw to the screen
local Draw = function( ReplaceChar )
local Scroll = 0
if Tbl.Position.X + Tbl.Current.TextPos >= Tbl.Position.X + Tbl.Position.Width then
Scroll = ( Tbl.Position.X + Tbl.Current.TextPos ) - Tbl.Position.Width
elseif Tbl.Position.X + Tbl.Current.TextPos >= Width then
Scroll = ( Tbl.Position.X + Tbl.Current.TextPos ) - Width
end
local _, CursorY = term.getCursorPos()
term.setCursorPos( Tbl.Position.X, CursorY )
local ReplaceChar = ReplaceChar or Tbl.Replace
if ReplaceChar then
term.write( string.rep( ReplaceChar, math.max( string.len( Tbl.Current.Text ) - Scroll, 0 ) ) )
else
term.write( string.sub( Tbl.Current.Text, Scroll ) )
end
local ExtraScroll = 0
if Scroll > 0 then
ExtraScroll = 1
end
term.setCursorPos( Tbl.Position.X + Tbl.Current.TextPos - Scroll + ExtraScroll, CursorY )
end
-- Event handling
while true do
local Event, Param = coroutine.yield( )
if Event == 'Object:Pause' then
-- Paused
term.setCursorBlink( false )
coroutine.yield( 'Object:Resume' )
term.setCursorBlink( true )
elseif Event == 'char' then
-- Typed key
Tbl.Current.Text = string.sub( Tbl.Current.Text, 1, Tbl.Current.TextPos ) .. Param .. string.sub( Tbl.Current.Text, Tbl.Current.TextPos + 1 )
Tbl.Current.TextPos = Tbl.Current.TextPos + 1
Draw()
elseif Event == 'key' then
if Param == keys[ 'enter' ] then
-- Enter
break
elseif Param == keys[ 'left' ] then
-- Left
if Tbl.Current.TextPos > 0 then
Tbl.Current.TextPos = Tbl.Current.TextPos - 1
Draw()
end
elseif Param == keys[ 'right' ] then
-- Right
if Tbl.Current.TextPos < string.len( Tbl.Current.Text ) then
Draw( ' ' )
Tbl.Current.TextPos = Tbl.Current.TextPos + 1
Draw()
end
elseif Param == keys[ 'backspace' ] then
-- Backspace
if Tbl.Current.TextPos > 0 then
Draw( ' ' )
Tbl.Current.Text = string.sub( Tbl.Current.Text, 1, Tbl.Current.TextPos - 1 ) .. string.sub( Tbl.Current.Text, Tbl.Current.TextPos + 1 )
Tbl.Current.TextPos = Tbl.Current.TextPos - 1
Draw()
end
elseif Param == keys[ 'home' ] then
-- Home
Draw( ' ' )
Tbl.Current.TextPos = 0
Draw()
elseif Param == keys[ 'delete' ] then
-- Delete
if Tbl.Current.TextPos < string.len( Tbl.Current.Text ) then
Draw( ' ' )
Tbl.Current.Text = string.sub( Tbl.Current.Text, 1, Tbl.Current.TextPos ) .. string.sub( Tbl.Current.Text, Tbl.Current.TextPos + 2 )
Draw()
end
elseif Param == keys[ 'end' ] then
-- End
Draw( ' ' )
Tbl.Current.TextPos = string.len(Tbl.Current.Text)
Draw()
end
elseif Event == 'term_resize' then
-- Terminal resized
Width = term.getSize()
Draw()
end
end
term.setTextColor( Tbl.Color.Foreground_After )
term.setBackgroundColor( Tbl.Color.Background_After )
term.setCursorBlink( false )
return Tbl.Current.Text
end
However, when I call it with a position of > 2 it kinda does something weird, when the position is at 10.
Code calling:
Spoiler
Objects[ 1 ]:InputBox(
{
Position = {
X = 3,
Width = 10,
Y = 2,
},
Color = {
Background_After = colors.black,
Background = colors.lightGray,
Foreground = colors.black,
Foreground_After = colors.white,
},
}
)
Screenies
http://imgur.com/a/Vew6X/embed#0
Pastebin'ed codes
API: 2eHAejyD
CallCode: i4HvSXDW
If you need more explanation, please tell me
Thanks in advance