class SetProfile: UIFrame112
{
//This class uses the concept of a redrawn frame
//Note that the UIFrame object is NOT destroyed during redrawing

number ProfileSlices 	//how many input lines are presently in UIFrame
number ProfileID	//ID of current line profile

//DLG tags
TagGroup DLGtags(object self)
	{
	//This DLG tags are of fixed length	
	string ProfileLabel, KnobTitle
	image Profile
	
	if(ProfileID !=0) 
		{
		KnobTitle ="de-Assign"
		Profile =ProfileID.GetImageFromID()
		ProfileLabel ="         "//"--> "+ Profile.ImageGetLabel()
		}						//not return the correct label in GMS3
	else
		{
		KnobTitle =" Assign  "
		ProfileLabel ="        "
		}
		
	taggroup FixedGroup = DLGCreateBox("").DLGTableLayout(5,1,0).DLGAnchor("West");	
	FixedGroup.DLGAddElement(DLGCreatePushButton(KnobTitle, "Assign").DLGIdentifier("Assign"))
	FixedGroup.DLGAddElement(DLGCreateLabel(ProfileLabel).DLGIdentifier("ProfileLabel"))
	FixedGroup.DLGAddElement(DLGCreatePushButton("Read", "Read"))
	FixedGroup.DLGAddElement(DLGCreateLabel("       "))
	FixedGroup.DLGAddElement(DLGCreatePushButton("Apply", "Apply"))
	
	//This DLG tags consist of variable number of lines
	TagGroup VariableGroup = DLGCreateDialog("").DLGTableLayout(8,ProfileSlices+1,0)
	
	//Titles
	VariableGroup.DLGAddElement(DLGCreateLabel("                name                "))
	VariableGroup.DLGAddElement(DLGCreateLabel("  fill  "))
	VariableGroup.DLGAddElement(DLGCreateLabel("         color        "))
	VariableGroup.DLGAddElement(DLGCreateLabel("    R    "))
	VariableGroup.DLGAddElement(DLGCreateLabel("    G    "))
	VariableGroup.DLGAddElement(DLGCreateLabel("    B    "))
	VariableGroup.DLGAddElement(DLGCreateLabel("thick"))
	VariableGroup.DLGAddElement(DLGCreateLabel("transp"))

	if(ProfileID !=0) 
		{	
		for(number i=0;i<ProfileSlices;i++)
			{				
			string Name ="Slice "+i+"                    " 
					
			VariableGroup.DLGAddElement(DLGCreateStringField(Name,24).DLGIdentifier("label"+i))
				number defaultFill =0
			VariableGroup.DLGAddElement(DLGCreateCheckBox("fill",defaultFill).DLGIdentifier("fill"+i))
				TagGroup PopUpItem
				number defaultColor =1
				TagGroup PopUp = DLGCreatePopup(PopUpItem,defaultColor,"ChangeColor")//.DLGIdentifier("popup"+i)
				PopUpItem.DLGAddPopupItemEntry("custom")	//1
				PopUpItem.DLGAddPopupItemEntry("black")		//2		
				PopUpItem.DLGAddPopupItemEntry("red")		//3
				PopUpItem.DLGAddPopupItemEntry("green")		//4
				PopUpItem.DLGAddPopupItemEntry("blue")		//5
				PopUpItem.DLGAddPopupItemEntry("magenda")	//6	
				PopUpItem.DLGAddPopupItemEntry("cyan")		//7
				PopUpItem.DLGAddPopupItemEntry("yellow")	//8			
			VariableGroup.DLGAddElement(PopUp.DLGIdentifier("color"+i))
			VariableGroup.DLGAddElement(DLGCreateRealField(0,6,3).DLGIdentifier("R"+i))
			VariableGroup.DLGAddElement(DLGCreateRealField(0,6,3).DLGIdentifier("G"+i))
			VariableGroup.DLGAddElement(DLGCreateRealField(0,6,3).DLGIdentifier("B"+i))
				number defaultThick =2
			VariableGroup.DLGAddElement(DLGCreateIntegerField(defaultThick, 2).DLGIdentifier("thick"+i))
				number defaultTransparancy =0
			VariableGroup.DLGAddElement(DLGCreateRealField(defaultTransparancy, 5,2).DLGIdentifier("Transp"+i))
				}
			}

	//compose them both
	taggroup Dialog = DLGCreateBox("")	
	Dialog.DLGAddElement(FixedGroup)
	Dialog.DLGAddElement(VariableGroup)


	return Dialog 
	}


//The essential part of the redrawing UIFrames
//this methodName is STANDARD and interfaced (not change the name)
//This method can be call either from "init" or from "Redrawer112" object	
void RedrawnUIFrame112(object self) 
	{
	//self.initUIFChild112(self.GetMotherID(),self.DLGtags(),self.GetPackageName(),self.GetFrameName())
	if(!self.initUIFrame112(self.DLGtags(),self.GetPackageName(),self.GetFrameName())) exit(0)
	
	}

//Frame initialization	
void init(object self,string PackageP,string NameP)
	{
	//set standard names
	self.SetPackageName(PackageP)
	self.SetFrameName(NameP)
	
	ProfileSlices = 0	//initially no setting lines
	
	self.SetFrameName(NameP) //needed because UIFrame112 displayed through redrawer
	
	//to draw first variant of frame 
	self.RedrawnUIFrame112()	
	}

number AboutToCloseUIFrame112(object self, number verify) 
	{
	//add here some actions you like to proceed when the UIFrame is about to be closed
	return 1
	}
	
void CloseUIFrame112(object self)
	{	
	//add here some actions you like to proceed when this object is closed
	}	


//This changes the number of input lines 
void RedrawLines(object self) 
	{
	//lanch the redrawer object
	alloc(DLGredrawer112).init(self.ScriptObjectgetID()).StartThread("Go")
	}


void GetSliceProperties(object self,imagedisplay disp, number &index, string &Name,number &style,number &red, number &green, number &blue, number &thick, number &DoTransp, number &transparancy)
	{
	object slice = disp.ImageDisplayGetSliceIDByIndex(index)		
	Name =disp.ImageDisplayGetSliceLabelByID( slice)
 
	//fill
	style =disp.LinePlotImageDisplayGetSliceDrawingStyle( index)
	
	//color
    disp.LinePlotImageDisplayGetSliceComponentColor( index, style-1, red, green, blue)
    
    //line thickness
    //Default DM thickness of 1 is not a useful seed
	//thick =disp.LinePlotImageDisplayGetSliceLineThickness( index)
	
	//filling transparancy
    disp.LinePlotImageDisplayGetSliceTransparency( index,doTransp,transparancy)
	}

void SetSliceProperties(object self, imagedisplay disp, number index, string Name,number style,number red, number green, number blue, number thick, number DoTransp, number transparancy)
	{
	object slice = disp.ImageDisplayGetSliceIDByIndex(index)		
	disp.ImageDisplaySetSliceLabelByID( slice, Name)
	
	//fill
	disp.LinePlotImageDisplaySetSliceDrawingStyle( index, style)
	
	//color
    disp.LinePlotImageDisplaySetSliceComponentColor( index, style-1, red, green, blue)
    
    //line thickness
	disp.LinePlotImageDisplaySetSliceLineThickness( index, thick)
	
	//filling transparancy
    disp.LinePlotImageDisplaySetSliceTransparency( index,doTransp,transparancy)
	}

void SetLineContent(object self,number index, string Name,number style,number red, number green, number blue, number thick, number DoTransp, number transparancy)
	{
	//title
	self.LookUpElement("label"+index).DLGValue(name)
	
	//fill
	self.LookUpElement("fill"+index).DLGValue(style-1)	
	
	//colors
	self.LookUpElement("R"+index).DLGValue(red)
	self.LookUpElement("G"+index).DLGValue(green)
	self.LookUpElement("B"+index).DLGValue(blue)
	
	//line thickness
	//Default DM thickness of 1 is not a useful seed
	//self.LookUpElement("thick"+index).DLGValue(thick)	
	
	//filling transparancy
	if(!DoTransp) transparancy =0
	self.LookUpElement("Transp"+index).DLGValue(transparancy)
	}

void GetLineContent(object self,number index, string &Name,number &style,number &red, number &green, number &blue, number &thick, number &DoTransp, number &transparancy)
	{
	//title
	name =self.LookUpElement("label"+index).DLGgetStringValue()
	
	//fill
	style =self.LookUpElement("fill"+index).DLGgetValue()
	style ++
	
	//colors
	red =self.LookUpElement("R"+index).DLGgetValue()
	green =self.LookUpElement("G"+index).DLGgetValue()
	blue =self.LookUpElement("B"+index).DLGgetValue()
	
	//line thickness
	thick =self.LookUpElement("thick"+index).DLGgetValue()
	
	//filling transparancy
	transparancy =self.LookUpElement("Transp"+index).DLGgetValue()
	if(transparancy !=0) DoTransp =1
	}
	
void Assign(object self)
	{
	if(ProfileID ==0)	//no profile yet assigned
		{
		image Profile :=getFrontImage()		
		ProfileID =Profile.ImageGetID()

		imageDisplay disp = Profile.ImageGetImageDisplay(0)
		disp.LinePlotImageDisplaySetLegendShown(1)
		ProfileSlices = disp.LinePlotImageDisplayCountSlices()
result("\nProfileSlices "+ProfileSlices)
		
		self.RedrawLines()
		}
	else	//deassign currently assigned profile
		{
		try{
		imageDisplay disp = ProfileID.GetImageFromID().ImageGetImageDisplay(0)
		disp.LinePlotImageDisplaySetLegendShown(0)
			}
		catch 
			{
			break
			}
	
		ProfileID =0
		
		ProfileSlices =0
		self.RedrawLines()
		}		
	}

void Read(object self)
	{
	image Profile :=getFrontImage()		
	ProfileID =Profile.ImageGetID()

	imageDisplay disp = Profile.ImageGetImageDisplay(0)
		
	for(number index =0;index<ProfileSlices;index++)
		{				
		string name
		number style, red, green, blue, thick, doTrans, transparancy			
		self.GetSliceProperties(disp,index,name, style, red, green, blue, thick, doTrans, transparancy)
result("\n"+name+" "+style+" "+red+ " "+green +" "+blue+ " "+thick+" "+doTrans+" "+transparancy)
		self.SetLineContent(index, name, style, red, green, blue, thick, doTrans, transparancy)
		}			
	}

void Apply(object self)
	{
	image Profile :=getFrontImage()		
	ProfileID =Profile.ImageGetID()

	imageDisplay disp = Profile.ImageGetImageDisplay(0)
		
	for(number index =0;index<ProfileSlices;index++)
		{			
		string name
		number style, red, green, blue, thick, doTrans, transparancy
		self.GetLineContent(index,name, style, red, green, blue, thick, doTrans, transparancy)			
		
//result("\n"+name+" "+style+" "+red+ " "+green +" "+blue+ " "+thick+" "+doTrans+" "+transparancy)
		self.SetSliceProperties(disp,index,name, style, red, green, blue, thick, doTrans, transparancy)
		}
	}

void ChangeColor(object self,TagGroup tags)	//set RGB of basic colors
	{
	string Identificator
	tags.DLGGetIdentifier(Identificator)
	number index =right(Identificator,1).val()

	number red,green,blue
	number colorCode =self.LookUpElement("color"+index).DLGgetValue()
result("\n"+Identificator +" "+index+" "+colorCode)		

	if(colorCode ==2) {red =0; green =0; blue =0;}	//black	
	if(colorCode ==3) {red =1; green =0; blue =0;}	//red
	if(colorCode ==4) {red =0; green =1; blue =0;}	//green
	if(colorCode ==5) {red =0; green =0; blue =1;}	//blue
	if(colorCode ==6) {red =1; green =0; blue =1;}	//magenda
	if(colorCode ==7) {red =0; green =1; blue =1;}	//cyan
	if(colorCode ==8) {red =1; green =1; blue =0;}	//yellow
		
	if(colorCode >1) 
		{
		// set RGB values
		self.LookUpElement("R"+index).DLGValue(red)
		self.LookUpElement("G"+index).DLGValue(green)
		self.LookUpElement("B"+index).DLGValue(blue)
		}
	}
}

{
object ProfileFrame = alloc(SetProfile)
ProfileFrame.init("_temDM-Scripts:SetProfile","SetProfile")
}