User Tools

Site Tools


Simple Composite Image Dialog

Summary

Below is an example demonstrating how to construct a simple dialog with a composite image, name it so its position is stored uniquely from other script constructed dialogs, and size it according to the image.

API Areas of Interest

Example

Simple_Composite_Image_Dialog.dsa
// DAZ Studio version 4.10.0.120 filetype DAZ Script
 
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function( sFileName1, sFileName2 ){
 
	/*********************************************************************/
	// String : A function for retrieving a translation if one exists
	function text( sText )
	{
		// If the version of the application supports qsTr()
		if( typeof( qsTr ) != "undefined" ){
			// Return the translated (if any) text
			return qsTr( sText );
		}
 
		// Return the original text
		return sText;
	};
 
	/*********************************************************************/
	// Define the template for What's This text
	var sWhatsThis = "<b>%1</b><br/><br/>%2";
 
	// Create a basic dialog
	var wDlg = new DzBasicDialog();
 
	// Get the wrapped widget for the dialog
	var oDlgWgt = wDlg.getWidget();
 
	// Set the title of the dialog
	wDlg.caption = "My Image";
 
	// Strip the space for a settings key
	var sKey = wDlg.caption.replace( / /g, "" ) + "Dlg";
 
	// Set an [unique] object name on the wrapped dialog widget;
	// this is used for recording position and size separately
	// from all other [uniquely named] DzBasicDialog instances
	oDlgWgt.objectName = sKey;
 
	// Create images to be used for compositing
	var oImage1 = new Image( sFileName1 );
	var oImage2 = new Image( sFileName2 );
	var oImage3 = new Image( sFileName1 );
 
	// --- #1 - Image1 over Image2 ---
 
	// Create a pixmap for the image
	var pixImage1 = new Pixmap();
 
	// If the application version is 4.10.0.120 or newer
	if( App.version64 >= 0x0004000a00000078 ){
		// Convert the image to a pixmap, compositing image 1 over image 2
		pixImage1.fromImage( oImage1.compositeOver( oImage2 ) );
	// If the application version is older
	} else {
		// Convert the image to a pixmap
		pixImage1.fromImage( oImage1 );
	}
 
	// Create a label and assign a pixmap
	var wLabel = new DzLabel( wDlg );
	wLabel.pixmap = pixImage1;
 
	// Add the label to the dialog
	wDlg.addWidget( wLabel );
 
	// --- #2 - Image2 over Image1 ---
 
	// Create a pixmap for the image
	var pixImage2 = new Pixmap();
 
	// If the application version is 4.10.0.120 or newer
	if( App.version64 >= 0x0004000a00000078 ){
		// Convert the image to a pixmap, compositing image 2 over image 1
		pixImage2.fromImage( oImage2.compositeOver( oImage1 ) );
	// If the application version is older
	} else {
		// Convert the image to a pixmap
		pixImage2.fromImage( oImage2 );
	}
 
	// Create a label and assign a pixmap
	wLabel = new DzLabel( wDlg );
	wLabel.pixmap = pixImage2;
 
	// Add the label to the dialog
	wDlg.addWidget( wLabel );
 
	// --- #3 - Text over Image1 ---
 
	// If the application version is 4.10.0.121 or newer
	if( App.version64 >= 0x0004000a00000079 ){
		// Define the text we want to draw over the image
		var sText = String("Copyright (c) %1 %2. All rights reserved.")
			.arg( (new Date()).getFullYear() )
			.arg( App.getCurrentAuthor().name );
 
		// Define which flags to use
		var nFlags = DzWidget.AlignHCenter|DzWidget.TextWordWrap;
 
		// Define the font to use
		var oFont = new Font();
		oFont.family = "Helvetica";
		oFont.pixelSize = 10;
		var sizeText = oFont.size( sText, nFlags );
		var oColor = new Color( 255, 255, 255, 90 );
		var rectText = oImage3.drawText(
			(oImage3.width/2) - (sizeText.width/2),
			(oImage3.height/2) - (sizeText.height/2),
			sText, oFont, oColor, nFlags );
 
		// Create a pixmap for the image
		var pixImage3 = new Pixmap();
 
		// Convert the image to a pixmap
		pixImage3.fromImage( oImage3 );
 
		// Create a label and assign a pixmap
		wLabel = new DzLabel( wDlg );
		wLabel.pixmap = pixImage3;
 
		// Add the label to the dialog
		wDlg.addWidget( wLabel );
	}
 
	// Get the minimum size of the dialog
	var sizeHint = oDlgWgt.minimumSizeHint;
 
	// Set the fixed size of the dialog
	wDlg.setFixedSize( sizeHint.width, sizeHint.height );
 
	// Set the text on the accept button
	wDlg.setAcceptButtonText( text( "&Close" ) );
	// Hide the cancel button
	wDlg.showCancelButton( false );
 
	// Display the dialog
	wDlg.exec();
 
// Finalize the function and invoke
})( "D:/temp/1.png", "D:/temp/2.png" );