Console border handler with percentages

Share your scripts and packages with other Mudlet users.
Post Reply
chrio
Posts: 73
Joined: Mon Aug 22, 2016 11:34 am

Console border handler with percentages

Post by chrio »

I made a script to handle the borders sizes when changing window size, I hope someone will find it useful.

It handles the border sizes given both in percent, like "25%" or in pixels, either as number 123 or "123". It can not deal with fractions of percentages yet.
A negative number or percentage sets the border from the opposite side, so ui.border_right = -800 will set the right edge of the console 800 px from the left side of the mudlet window. (More useful with pixels since -70% could just be written as 30%)
Code: [show] | [select all] lua
-- consoleborder - sysWindowResizeEvent handler
-- 
-- Main console border handler that can handle percentages
-------------------------------------------------
ui = ui or {}
ui.window = ui.window or {}

ui.border_left   = "10%"
ui.border_top    = "20%"
ui.border_right  = "40%"
ui.border_bottom = "7%"
ui.bordercolor = {27,25,22}  -- border color


-----------------------------------------------
setBorderColor(unpack(ui.bordercolor))



-- ui.borderTranslation( border, value )
-- border is either "left", "right", "top" or "bottom"
-- value is borderwidth in pixels: 123, "123", -700 or "-700"
--       or a percentage of main window: "15%" "-20%"
-- negative value implies distance to the opposite side of the screen
function ui.borderTranslation( border, value )
	local w,h = getMainWindowSize()
	local str = tostring(value)
	local _,_,v = string.find(str, "^(-?%d+)%%$")
	
	
	value = tonumber(value)
	if( type(value) == "number" ) then
		-- simple case, no percentages.
		-- Treat minus as pixels from opposite side
		if( value >= 0) then return value
		else
			if( border == "top" or  border == "bottom" ) then
				return h+value
			elseif( border == "left" or border == "right") then
				return w+value
			end
		end
	elseif( v ) then
		v = tonumber(v)
		-- percentage, calculate from window size
		if(v >= 0) then
			if( border == "top" or  border == "bottom") then
				return h*(v/100)
			elseif( border == "left" or border == "right" ) then
				return w*(v/100)
			end
		elseif(v < 0) then
			if( border == "top" or  border == "bottom") then
				return h+h*(v/100)
			elseif( border == "left" or border == "right" ) then
				return w+w*(v/100)
			end
		end
	end
end


-- Update the borders, but only if widow size have changed (to prevent event loop)
function ui.updateBorderSizes()
	local w,h = getMainWindowSize()
	if( w ~= ui.window.width or h ~= ui.window.height ) then
		ui.window.width = w
		ui.window.height = h
		setBorderLeft(ui.borderTranslation("left",ui.border_left))
		setBorderRight(ui.borderTranslation("right",ui.border_right))
		setBorderBottom(ui.borderTranslation("bottom",ui.border_bottom))
		setBorderTop(ui.borderTranslation("top",ui.border_top))
	end
end


-- Handler for sysWindowResizeEvent
function consoleborder(event,x,y)
	if( ui.eventtimer ) then killTimer( ui.eventtimer ) end
	ui.eventtimer = tempTimer(0.1, [[killTimer( "ui.eventtimer" ) ui.updateBorderSizes()]])
end
To avoid the function being spammed when you resize the window, a timer is used that will call for border resize once you stop changing window size for 0.1s (ui.eventtimer = tempTimer(0.1, ....)) If you want the changes to happen instantly, just change 0.1 to 0

The script should be named consoleborder and be registred as an eventhandler to sysWindowResizeEvent. If you want to name it differently you will have to rename the function consoleborder() to the same name as the script.

chrio
Posts: 73
Joined: Mon Aug 22, 2016 11:34 am

Re: Console border handler with percentages

Post by chrio »

Ok, so shortly after submitting this I came up with an alternate solution that merges better with Geyser, which was the reason I wanted to do this in the first place: Resizing main console using Geyser

Post Reply