package projacker;

public class Pattern implements Element {
	private Module parent;
	private Row child = new Row( this );
	private micromod.Pattern pattern;
	private int patternIdx;

	public Pattern( Module parent ) {
		this.parent = parent;
	}
	
	public String getToken() {
		return "Pattern";
	}
	
	public Element getParent() {
		return parent;
	}
	
	public Element getSibling() {
		return null;
	}
	
	public Element getChild() {
		return child;
	}
	
	public void begin( String value ) {
		System.out.println( getToken() + ": " + value );
		patternIdx = Parser.parseInteger( value );
		pattern = parent.getPattern( patternIdx );
		child.setRowIdx( 0 );
	}
	
	public void end() {
		/* Expand macros.*/
		micromod.Note note = new micromod.Note();
		int numChannels = pattern.getNumChannels();
		for( int channelIdx = 0; channelIdx < numChannels; channelIdx++ ) {
			micromod.Pattern macro = null;
			int rowIdx = 0;
			int macroRowIdx = 0, transpose = 0, volume = 64;
			while( rowIdx < micromod.Pattern.NUM_ROWS ) {
				pattern.getNote( rowIdx, channelIdx, note );
				if( note.instrument > 0 ) {
					macro = parent.getMacro( note.instrument );
					macroRowIdx = 0;
					transpose = ( note.key > 0 ) ? note.key - 25 : 0;
					volume = ( note.effect == 0xC ) ? note.parameter : 64;
				}
				if( macro != null ) {
					macro.getNote( macroRowIdx++, 0, note );
					note.transpose( transpose, volume, parent.getModule() );
				}
				pattern.setNote( rowIdx++, channelIdx, note );
			}
		}
	}
	
	public int getPatternIdx() {
		return patternIdx;
	}
	
	public void setNote( int rowIdx, int channelIdx, micromod.Note note ) {
		pattern.setNote( rowIdx, channelIdx, note );
	}
}

@@ -1165,46 +1165,116 @@
 x = 
-0;%0A%09%09%09%09%09transpose = ( note.key %3E 0 ) ?
+transpose = 0;%0A%09%09%09%09%09volume = 64;%0A%09%09%09%09%7D%0A%09%09%09%09if( macro != null ) %7B%0A%09%09%09%09%09if( note.key %3E 0 ) %7B%0A%09%09%09%09%09%09transpose =
  not
@@ -1287,52 +1287,64 @@
 - 25
- : 0
 ;%0A%09%09%09%09%09
-volume = ( note.effect == 0xC ) ?
+%7D%0A%09%09%09%09%09int effect = note.effect;%0A%09%09%09%09%09int param =
  not
@@ -1358,51 +1358,14 @@
 eter
- : 64
 ;%0A%09%09%09%09
-%7D%0A%09%09%09%09if( macro != null ) %7B%0A%09%09%09%09
 %09mac
@@ -1397,24 +1397,196 @@
 , 0, note );
+%0A%09%09%09%09%09if( effect == 0xC ) %7B%0A%09%09%09%09%09%09volume = param;%0A%09%09%09%09%09%7D else if( ( note.effect %7C note.parameter ) == 0 ) %7B%0A%09%09%09%09%09%09note.effect = effect;%0A%09%09%09%09%09%09note.parameter = param;%0A%09%09%09%09%09%7D
 %0A%09%09%09%09%09note.t

Make macros more flexible.