/*//////////   SCRIPT:   EM_Modifiers Control v1.0   //////////*/
/*//////////   WRITTEN BY:   Branko Zivkovic   //////////*/
/*//////////   EMAIL:   barigazy@hotmail.com   //////////*/
(
if ::_netForm != undefined do try(_netForm.close())catch()
	local settingsINI = getdir(#userScripts)+"\\em_modifier_ctrls_settings.ini"
	local MRG1111 = (dotNetObject "padding" 1 1 1 1)	, cursorPos
	local ttText = " Label Commands:\n   • LMB - move form\n   • MMB - select base object\n   • RMB - open context menu\n ContextMenu Commands:\n   • LMB - select modifier\n   • MMB - toggle enable/disable\n   • RMB - delete modifier"
	fn _netRect x y w h = (dotNetObject "System.Drawing.Rectangle" x y w h)
	fn _netSize x y = (dotNetObject "System.Drawing.Size" x y)
	fn _netColor r g b = ((dotNetClass "System.Drawing.Color").FromArgb r g b)
	fn _netBrush clr = (dotNetObject "System.Drawing.SolidBrush" clr)
	fn _netPoint x y = (dotNetObject "System.Drawing.Point" x y)
	fn _netFontB fName fSize = (dotNetObject "System.Drawing.Font" fName fSize ((dotNetClass "System.Drawing.FontStyle").bold))
	fn _netFont fName fSize = (dotNetObject "System.Drawing.Font" fName fSize)
	fn maxHW = (dotNetObject "maxCustomControls.win32HandleWrapper" (dotNetObject "System.IntPtr" (windows.getMaxHWND())))
	
	struct _netColorStruct 
	(
		maxBC = (colorMan.getColor #background) * 255.0,
		maxFC = (colorMan.getColor #Text) * 255.0,
		C1a = _netColor maxBC.x maxBC.y maxBC.z, 
		C1b = _netColor (maxBC.x-10) (maxBC.y-10) (maxBC.z-10),
		C1c = _netColor (maxBC.x-20) (maxBC.y-20) (maxBC.z-20),
		C2 = _netColor maxFC.x maxFC.y maxFC.z, 
		C3 = _netColor 0 0 0, 
		C4 = _netColor 150 20 20
	)
	global _netColors = _netColorStruct()
	
	fn _netToolTip dnTT = 
	(
		dnTT.AutoPopDelay = 10000 ; dnTT.InitialDelay = 2000 ; dnTT.ReshowDelay = 2000
		dnTT.BackColor = _netColors.C1c ; dnTT.OwnerDraw = on ; dnTT
	)	
	
	_netForm = dotnetobject "Form"
	_netForm.ShowInTaskbar = off
	_netForm.StartPosition = _netForm.StartPosition.Manual
	_netForm.ClientSize = _netSize 200 20 
	_netForm.FormBorderStyle = _netForm.FormBorderStyle.None
	_netForm.AutoSizeMode = _netForm.AutoSizeMode.GrowAndShrink
	_netForm.AutoSize = on ; _netForm.BackColor = _netColors.C3
	_netForm.DesktopLocation = if not (doesFileExist settingsINI) then _netPoint 30 110 else
	(
		local X = (GetINISetting settingsINI "FormLocation" "PosX") as integer
		local Y = (GetINISetting settingsINI "FormLocation" "PosY") as integer
		_netPoint X Y
	)
	
	local _netLabel = dotnetobject "Label"
	_netLabel.Bounds = _netRect 1 1 191 18 ; _netLabel.Margin = MRG1111
	_netLabel.BackColor = _netColors.C1b ; _netLabel.ForeColor = _netColors.C3
	_netLabel.TextAlign = _netLabel.TextAlign.MiddleCenter
	_netLabel.Font = _netFontB "Verdana" 6.5
	_netLabel.Text = "Select Object"

   local _netButton = dotnetobject "Button"	
   	_netButton.Bounds = _netRect 193 1 6 18
	_netButton.FlatStyle = _netButton.FlatStyle.Flat
	_netButton.FlatAppearance.BorderSize = 0 ; _netButton.Margin = MRG1111
	_netButton.BackColor = _netButton.FlatAppearance.BorderColor = _netColors.C4

	local _netTip = dotnetobject "ToolTip" ;	(_netToolTip _netTip).SetToolTip _netButton ttText
	local _netTimer = dotNetObject "Timer" ; _netTimer.Interval = 100 ; _netTimer.Enabled = false
	
	local _netCM = dotnetobject "ContextMenuStrip"
	_netCM.ShowImageMargin = off
	_netCM.Autosize = _netCM.ShowCheckMargin = on 
	_netCM.RenderMode = _netCM.RenderMode.Professional
	_netCM.BackColor = _netColors.C1b
	fn add_MenuItem mody =
	(
		mi = dotnetobject "ToolStripMenuItem" mody.name
		mi.tag = dotnetmxsvalue mody
		mi.checked = mody.enabled
		dotnet.addEventHandler mi "MouseUp" \
		(
			fn mi_Click s arg =
			(
				case arg.Button of
				(
					(arg.Button.Left):
					(
						if modPanel.getCurrentObject() == undefined do max modify mode
						modPanel.setCurrentObject s.tag.value
					)
					(arg.Button.Middle): (s.Checked = (s.tag.value).enabled = not s.Checked ; max create mode ; max modify mode)
					(arg.Button.Right): (undo "Del_Modifier" on deletemodifier ((refs.dependentnodes s.tag.value)[1]) s.tag.value)
				)
			)
		) ; mi
	)

	fn _netLabel_MouseDown s arg = (s.BackColor = _netColors.C1c ; cursorPos = [(mouse.screenpos.x - _netForm.DesktopLocation.x), (mouse.screenpos.y - _netForm.DesktopLocation.y)])
	fn _netLabel_MouseMove s arg =
	(
		if arg.Button == arg.Button.Left and cursorPos != undefined do
		(
			_netForm.DesktopLocation = _netPoint (mouse.screenpos.x - cursorPos.x) (mouse.screenpos.y - cursorPos.y)
		)
	)
	fn _netLabel_MouseUp s arg = 
	(
		s.BackColor = _netColors.C1a
		if isValidNode (obj = getNodeByName (s.text)) do
		(
			case arg.Button of
			(
				(arg.Button.Left): cursorPos = undefined
				(arg.Button.Middle): 
				(
					if modPanel.getCurrentObject() == undefined do max modify mode
					modPanel.setCurrentObject obj.baseobject	
				)			
				(arg.Button.Right): 
				(
					s.ContextMenuStrip.Items.Clear()
					if (cnt = obj.modifiers.count) == 0 then (s.ContextMenuStrip.items.add (dotnetobject "ToolStripMenuItem" "No Modifiers")) else
					(
						for i in 1 to cnt do s.ContextMenuStrip.items.add (add_MenuItem obj.modifiers[i])
					)
					s.ContextMenuStrip.Show s (_netPoint arg.x arg.y)
				)
			)	
		)
		format "obj = %\n" (isValidNode (obj = getNodeByName (s.text)))
	)
	fn _netTip_draw s arg = 
	(
		local _netTTB = _netBrush _netColors.C2, _netST = dotnetobject "System.Drawing.StringFormat", _netTTF = _netFont "Tahoma" 8.5
		local _netRectF = dotnetobject "System.Drawing.RectangleF" arg.Bounds.x arg.Bounds.y (arg.Bounds.width) (arg.Bounds.height)
		arg.DrawBackground() ; arg.DrawBorder() ; _netST.LineAlignment = _netST.LineAlignment.Center
		arg.Graphics.DrawString arg.ToolTipText _netTTF _netTTB _netRectF _netST
	)
	fn _netButton_close = 
	(
		SetIniSetting settingsINI "FormLocation" "PosX" (_netForm.DesktopLocation.x as string)
		SetIniSetting settingsINI "FormLocation" "PosY" (_netForm.DesktopLocation.y as string)		
		_netTimer.Stop() ; dotnet.removeAllEventHandlers _netTimer ; _netTimer.Dispose()
		try(_netForm.close())catch() ; _netForm = undefined ; clearundobuffer() ; clearListener()
	)	
	fn checkSelection =
	(
		if isKindOf _netForm dotNetObject and isCreatingObject() == false do
		(
			if selection.count == 1 then
			(
				_netForm.Controls.item[0].ForeColor = _netColors.C2
				_netForm.Controls.item[0].Text = selection[1].name
			)
			else
			(
				_netForm.Controls.item[0].ForeColor = _netColors.C3
				_netForm.Controls.item[0].Text = "Select Object"				
			)
		)
	)
	dotnet.addeventhandler _netTip "Draw" _netTip_draw
	dotNet.addEventHandler _netLabel "MouseDown" _netLabel_MouseDown
	dotNet.addEventHandler _netLabel "MouseMove" _netLabel_MouseMove
	dotNet.addEventHandler _netLabel "MouseUp" _netLabel_MouseUp
	dotNet.addEventHandler _netLabel "MouseHover" (fn _netLabel_MouseHover s arg = (s.BackColor = _netColors.C1a))
	dotNet.addEventHandler _netLabel "MouseLeave" (fn _netLabel_MouseLeave s arg = (s.BackColor = _netColors.C1b))
	dotnet.addEventHandler _netCM "Closed" (fn _netCM_close s e = s.items.Clear())
	dotnet.addEventHandler _netButton "MouseUp" _netButton_close
	dotNet.addEventHandler _netTimer "Tick" (fn _netTimerTick = checkSelection())
	_netLabel.ContextMenuStrip = _netCM	
		
	_netForm.Controls.addrange #(_netLabel,_netButton)
	dotNet.setLifetimeControl _netForm #dotnet
	_netForm.Show (maxHW()) ; _netTimer.Start()
ok
)