Skip to content

Commit e4041e6

Browse files
committed
Improved mapper.lua to use new utils.inputbox techniques
1 parent f6756e0 commit e4041e6

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

lua/mapper.lua

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Author: Nick Gammon
66
Date: 11th March 2010
77
Amended: 15th August 2010
8+
Amended: 2nd October 2010
89
910
Generic MUD mapper.
1011
@@ -61,7 +62,7 @@ Room info should include:
6162

6263
module (..., package.seeall)
6364

64-
VERSION = 2.0 -- for querying by plugins
65+
VERSION = 2.1 -- for querying by plugins
6566

6667
require "movewindow"
6768
require "copytable"
@@ -230,25 +231,54 @@ local function check_connected ()
230231
return true
231232
end -- check_connected
232233

233-
local function get_number_from_user (msg, title, current, min, max)
234-
local n = utils.inputbox (msg, title, current)
234+
local function make_number_checker (title, min, max, decimals)
235+
return function (s)
236+
local n = tonumber (s)
237+
if not n then
238+
utils.msgbox (title .. " must be a number", "Incorrect input", "ok", "!", 1)
239+
return false -- bad input
240+
end -- if
241+
242+
if n < min or n > max then
243+
utils.msgbox (title .. " must be in range " .. min .. " to " .. max, "Incorrect input", "ok", "!", 1)
244+
return false -- bad input
245+
end -- if
235246

236-
if not n then
237-
return nil
238-
end -- if dismissed
247+
if not decimals then
248+
if string.match (s, "%.") then
249+
utils.msgbox (title .. " cannot have decimal places", "Incorrect input", "ok", "!", 1)
250+
return false -- bad input
251+
end -- if
252+
end -- no decimals
253+
254+
return true -- good input
255+
end -- generated function
239256

240-
n = tonumber (n)
241-
if not n then
242-
utils.msgbox ("You must enter a number", "Incorrect input", "ok", "!", 1)
243-
return nil
244-
end -- if
257+
end -- make_number_checker
258+
259+
260+
local function get_number_from_user (msg, title, current, min, max, decimals)
261+
local max_length = math.ceil (math.log10 (max) + 1)
245262

246-
if n < min or n > max then
247-
utils.msgbox (title .. " must be in range " .. min .. " to " .. max, "Incorrect input", "ok", "!", 1)
248-
return nil
263+
-- if decimals allowed, allow room for them
264+
if decimals then
265+
max_length = max_length + 2 -- allow for 0.x
249266
end -- if
250-
251-
return n
267+
268+
-- if can be negative, allow for minus sign
269+
if min < 0 then
270+
max_length = max_length + 1
271+
end -- if can be negative
272+
273+
return tonumber (utils.inputbox (msg, title, current, nil, nil,
274+
{ validate = make_number_checker (title, min, max, decimals),
275+
prompt_height = 14,
276+
box_height = 130,
277+
box_width = 300,
278+
reply_width = 150,
279+
max_length = max_length,
280+
} -- end extra stuff
281+
))
252282
end -- get_number_from_user
253283

254284
local function draw_configuration ()
@@ -1452,7 +1482,7 @@ end -- mouseup_change_depth
14521482

14531483
function mouseup_change_delay (flags, hotspot_id)
14541484

1455-
local delay = get_number_from_user ("Choose speedwalk delay time (0 to 10 seconds)", "Delay in seconds", config.DELAY.time, 0, 10)
1485+
local delay = get_number_from_user ("Choose speedwalk delay time (0 to 10 seconds)", "Delay in seconds", config.DELAY.time, 0, 10, true)
14561486

14571487
if not delay then
14581488
return

0 commit comments

Comments
 (0)