90 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			90 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
| local utf8 = require("utf8")
 | |
| 
 | |
| local function backspace(text)
 | |
| 	local byteoffset = utf8.offset(text, -1)
 | |
| 
 | |
| 	if byteoffset then
 | |
| 			-- remove the last UTF-8 character.
 | |
| 			-- string.sub operates on bytes rather than UTF-8 characters,
 | |
| 			-- so we couldn't do string.sub(text, 1, -2).
 | |
| 			return string.sub(text, 1, byteoffset - 1)
 | |
| 	end
 | |
| 
 | |
| 	return ""
 | |
| end
 | |
| 
 | |
| Prompt = {
 | |
| 	-- defaults for instance variables
 | |
| 	pos = { x = 10, y = 10 },
 | |
| 	input = "",
 | |
| 	name = "input",
 | |
| 	canceled = false,
 | |
| 	closing = false,
 | |
| 	color = {1,1,1,1},
 | |
| 	background_color = {0,0,0,1},
 | |
| 	active_prompt = nil,
 | |
| }
 | |
| 
 | |
| function Prompt:new(o)
 | |
| 	o = o or {}
 | |
| 	setmetatable(o, self)
 | |
| 	self.__index = self
 | |
| 	return o
 | |
| end
 | |
| 
 | |
| function Prompt:keypressed(key, scancode, isrepeat)
 | |
| 	if key == "backspace" then
 | |
| 		self.input = backspace(self.input)
 | |
| 	elseif key == "return" or key == "kpenter" or key == "escape" then
 | |
| 		if key == "escape" then
 | |
| 			self.canceled = true
 | |
| 		end
 | |
| 		self.closing = true
 | |
| 		self:func()
 | |
| 	end
 | |
| end
 | |
| 
 | |
| function Prompt:update()
 | |
| 
 | |
| end
 | |
| 
 | |
| function Prompt:textinput(text)
 | |
| 	self.input = self.input .. text
 | |
| end
 | |
| 
 | |
| function Prompt:draw()
 | |
| 	local c1, c2, c3, a = love.graphics.getColor()
 | |
| 	local text = self.name .. ": " .. self.input
 | |
| 	local width = locale_font:getWidth(text)
 | |
| 	local height = locale_font:getHeight(text)
 | |
| 	local margin = 10
 | |
| 	love.graphics.setColor(unpack(self.color))
 | |
| 	love.graphics.rectangle("fill",
 | |
| 		self.pos.x-margin-1,
 | |
| 		self.pos.y-1,
 | |
| 		width+margin*2+2,
 | |
| 		height+margin+2
 | |
| 	)
 | |
| 	love.graphics.setColor(unpack(self.background_color))
 | |
| 	love.graphics.rectangle("fill",
 | |
| 		self.pos.x-margin,
 | |
| 		self.pos.y,
 | |
| 		width+margin*2,
 | |
| 		height+margin
 | |
| 	)
 | |
| 	love.graphics.setColor(unpack(self.color))
 | |
| 	love.graphics.print(text, self.pos.x, self.pos.y)
 | |
| 	love.graphics.setColor(c1,c2,c3,a)
 | |
| end
 | |
| 
 | |
| function Prompt:activate()
 | |
| 	Prompt.active_prompt = self
 | |
| 	love.keyboard.setTextInput(true)
 | |
| end
 | |
| 
 | |
| -- demonstration of how default values work
 | |
| local test_prompt = Prompt:new()
 | |
| assert(test_prompt.name == "input")
 | |
| test_prompt.name = "foobar"
 | |
| assert(Prompt.name == "input")
 |