It is currently September 19th, 2020, 3:13 pm

Get Current Monitor Code

Discuss the use of Lua in Script measures.
prince1142003
Posts: 56
Joined: December 27th, 2011, 12:32 pm

Get Current Monitor Code

Post by prince1142003 »

I had a need to get the current monitor the skin is being displayed on. Improvised the following script that works, but I'm sure could use some improvement from the Lua gurus.

I used the concepts from here: http://martin-thoma.com/how-to-check-if-a-point-is-inside-a-rectangle/

CurrentMonitor.lua

Code: Select all

function Update()
	currentMonitor = 1
	numMonitors = SELF:GetNumberOption('NumMonitors', 1)
	
	if (numMonitors > 0) then
		skin = {}
		skin['x'] = tonumber(SKIN:GetVariable('CURRENTCONFIGX'))
		skin['y'] = tonumber(SKIN:GetVariable('CURRENTCONFIGY'))
	
		for i = numMonitors, 1, -1 do
			monitorL = tonumber(SKIN:GetVariable('SCREENAREAX@'..i))
			monitorT = tonumber(SKIN:GetVariable('SCREENAREAY@'..i))
			monitorW = tonumber(SKIN:GetVariable('SCREENAREAWIDTH@'..i))
			monitorH = tonumber(SKIN:GetVariable('SCREENAREAHEIGHT@'..i))
			
			monitor = {}
			monitor['A'] = {['x']=monitorL; ['y']=monitorT}
			monitor['B'] = {['x']=monitorL+monitorW-1; ['y']=monitorT}
			monitor['C'] = {['x']=monitorL+monitorW-1; ['y']=monitorT+monitorH-1}
			monitor['D'] = {['x']=monitorL; ['y']=monitorT+monitorH-1}
			
			aRect = math.abs((monitor.A.y-monitor.C.y)*(monitor.D.x-monitor.B.x)+(monitor.B.y-monitor.D.y)*(monitor.A.x-monitor.C.x))/2
			
			aTri = {}
			aTri['pAB'] = math.abs(skin.x*(monitor.A.y-monitor.B.y)+monitor.A.x*(monitor.B.y-skin.y)+monitor.B.x*(skin.y-monitor.A.y))/2
			aTri['pBC'] = math.abs(skin.x*(monitor.B.y-monitor.C.y)+monitor.B.x*(monitor.C.y-skin.y)+monitor.C.x*(skin.y-monitor.B.y))/2
			aTri['pCD'] = math.abs(skin.x*(monitor.C.y-monitor.D.y)+monitor.C.x*(monitor.D.y-skin.y)+monitor.D.x*(skin.y-monitor.C.y))/2
			aTri['pAD'] = math.abs(skin.x*(monitor.A.y-monitor.D.y)+monitor.A.x*(monitor.D.y-skin.y)+monitor.D.x*(skin.y-monitor.A.y))/2
			
			aTriSum = aTri['pAB'] + aTri['pBC'] + aTri['pCD'] + aTri['pAD']
			
			if aRect == aTriSum then
				currentMonitor = i
				break
			end
		end
	end
	
	return currentMonitor
end
CurrentMonitor.ini

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1
Group=#ROOTCONFIG#
MouseActionCursor=0
DynamicWindowSize=1
DefaultUpdateDivider=1
SolidColor=0,0,0,128

[mNumMonitors]
Measure=Plugin
Plugin=SysInfo
SysInfoType=NUM_MONITORS

[mCurrentMonitor]
Measure=Script
ScriptFile=CurrentMonitor.lua
NumMonitors=[mNumMonitors]

[stDefaultFont]
X=r
Y=R
SolidColor=0,0,0,128
W=250
ClipString=2
FontColor=255,255,255
AntiAlias=1
Padding=5,5,5,5
FontFace=Segoe UI

[Something]
Meter=String
MeterStyle=stDefaultFont
Text=Config#CRLF#x=#CURRENTCONFIGX# y=#CURRENTCONFIGY##CRLF#monitor=[mCurrentMonitor]#CRLF##CRLF#Monitor 1#CRLF#x=#SCREENAREAX@1# y=#SCREENAREAY@1# w=#SCREENAREAWIDTH@1# h=#SCREENAREAHEIGHT@1##CRLF##CRLF#Monitor 2#CRLF#x=#SCREENAREAX@2# y=#SCREENAREAY@2# w=#SCREENAREAWIDTH@2# h=#SCREENAREAHEIGHT@2#
X=0
Y=0
FontSize=10
DynamicVariables=1
InlinePattern=(Config)
InlineSetting=Italic
InlinePattern2=(Monitor \d)
InlineSetting2=Italic
InlinePattern3=(Config)
InlineSetting3=Underline
InlinePattern4=(Monitor \d)
InlineSetting4=Underline