<?xml version="1.0" encoding="UTF-8"?>
<rmml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="L:\collaborative-nir\program\Cscl3DWS\rmml\RMML.xsd">
	<!-- Виртуальная доска для модели молекулы -->
	<include src="spliceComp.xml"/>

	<composition name="CMCBoard" _class="CDraggableComposition">
		<include src="styles.xml"/>
		<composition name="splicedTreeView" class="CSplicedView" visible="false"/>
		<composition   _name="subst" _class="CResizablePaper" _depth="1" visible="false">
			<image _name="lt" src="eval(KM2DRESPATH+'substrate.png')">
				<script>this.crop(0,0,2,2);</script>
			</image>
			<image _name="ct" src="eval(KM2DRESPATH+'substrate.png')">
				<script>this.crop(1,0,2,2);</script>
			</image>
			<image _name="rt" src="eval(KM2DRESPATH+'substrate.png')">
				<script>this.crop(2,0,2,2);</script>
			</image>
			<image _name="lc" src="eval(KM2DRESPATH+'substrate.png')">
				<script>this.crop(0,1,2,2);</script>
			</image>
			<image _name="cc" src="eval(KM2DRESPATH+'substrate.png')">
				<script>this.crop(1,1,2,2);</script>
			</image>
			<image _name="rc" src="eval(KM2DRESPATH+'substrate.png')">
				<script>this.crop(2,1,2,2);</script>
			</image>
			<image _name="lb" src="eval(KM2DRESPATH+'substrate.png')">
				<script>this.crop(0,2,2,2);</script>
			</image>
			<image _name="cb" src="eval(KM2DRESPATH+'substrate.png')">
				<script>this.crop(1,2,2,2);</script>
			</image>
			<image _name="rb" src="eval(KM2DRESPATH+'substrate.png')">
				<script>this.crop(2,2,2,2);</script>
			</image>
		</composition>
		<!--
		<image _name="im3d" src="eval(KM2DRESPATH+'btn3D.png')" depth="-1" visible="false">
			<script event="onPress">
				if( typeof(scene.show3DView) == "function")
				{
					scene.show3DView( this.parent.molecule.getXML(), this.parent.molecule.toString());
				}
			</script>
		</image>
		-->
		<composition name="im3d" class="CKMButton" depth="-1" visible="false">
			<image _name="img" src="eval(KM2DRESPATH+'btnView3D.png')"/>
			<script>
				this.checkButton = false;
			</script>
			<script event="onClick">
				if( typeof(scene.show3DView) == "function")
				{
					scene.show3DBoardView( this.parent);
					//scene.show3DView( this.parent.molecule.getXML(), this.parent.molecule.toString());
				}
			</script>
		</composition>
		<composition name="imExport" class="CKMButton" depth="-1" visible="false">
			<image _name="img" src="eval(KM2DRESPATH+'btnExport.png')"/>
			<script>
				this.checkButton = false;
			</script>
			<script event="onClick">
				this.parent.workspace.exportBoard( this.parent);
			</script>
		</composition>

		<composition name="compSliced" _class="CDraggableComposition" visible="false">
			<image _name="imSliced" enabled="false"/>
		</composition>

		<script src="board.mjs"/>
		<script><![CDATA[
			this.workspace = this.parent;
			this.molecule = null;
			this.autoHighlight = true;
			this.cellSizeX = gvCellSize;
			this.cellSizeY = gvCellSize;
			this.freezed = false;
			this.showBtn3D = true;

			// Notifications functions
			this.externalOnMoleculeChanged = null;
			this.externalOnBeforeDestroy = null;

			this.addHotspot(this.subst);

			function setMolecule(amolecule)
			{
				this.molecule = amolecule;
				this.molecule.board = this;
				this.molecule.rmmlParent = this;
				this.molecule.x = this.widthCellsCount/2;
				this.molecule.y = this.heightCellsCount/2;
				this.molecule.optimize();
				this.molecule.visualize();
			}

			function getImagePath(path)
			{
				return getFullPath("rmgr:molconst2d/"+path);
			}

			function highlightMolecule(val)
			{
				if( val)
				{
//trace("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
//trace("Highlight");
					var border = 0;
					var bounds = this.molecule.getScreenBounds();
					var w = bounds.right-bounds.left+2*border;
					var h = bounds.bottom-bounds.top+2*border;
					subst.resize(w, h);
					subst.x = bounds.left-border;
					subst.y = bounds.top-border;
					if( this.showBtn3D)
					{
						this.im3d.x = w - this.im3d.width/2 + subst.x;
						this.im3d.y = h - this.im3d.height + subst.y;
						this.im3d.visible=false;
						this.imExport.x = this.im3d.x + this.im3d.width;
						this.imExport.y = this.im3d.y;
						this.imExport.visible=false;
					}
					subst.visible = true;
					this.seq3d.rewindAndPlay();
				}
				else
				{
					//trace("UnHighlight");
					subst.visible = false;
					if( this.showBtn3D)
					{
						this.im3d.visible = false;
						this.imExport.visible = false;
					}
				}
			}

			function isMouseOn()
			{
				if( this.molecule == null)
				{
					trace("mc2d isMouseOn error: this.molecule is null");
					return false;
				}
				var bounds = this.molecule.getScreenBounds();
//trace("ScreenBounds: "+bounds.left+","+bounds.top+","+bounds.right+","+bounds.bottom);
				if( this.xMouse>bounds.left && this.xMouse<bounds.right &&
					this.yMouse>bounds.top  && this.yMouse<bounds.bottom)
				{
					return true;
				}
				if( this.subst.visible && this.im3d.visible)
				{
					bounds.left = bounds.right - this.im3d.width/2;
					bounds.top =  bounds.bottom - this.im3d.height;
					bounds.right = bounds.left + this.im3d.width + this.imExport.width;
					bounds.bottom = bounds.top + this.im3d.height;
					if( this.xMouse>bounds.left && this.xMouse<bounds.right &&
						this.yMouse>bounds.top  && this.yMouse<bounds.bottom)
					{
						return true;
					}
				}
				//trace("Bounds are:"+bounds.left+","+bounds.top+":"+bounds.right+","+bounds.bottom);
				//trace("Mouse are:"+this.xMouse+","+this.yMouse);
				return false;
			}

			function onMoleculeChanged()
			{
				highlightMolecule(subst.visible);
				if( this.molecule.root == null)
				{
					this.workspace.removeBoard( this);
				}
				else
				{
					var obj = acceptMoveBy(0, 0);
					if( obj.x!=0 || obj.y!=0)
					{
						this.x += obj.x;
						this.y += obj.y;
					}
					if( this.externalOnMoleculeChanged != null)
						this.externalOnMoleculeChanged();
				}
			}

			function onWarning(id)
			{
				scene.showWarning( id);
			}

			function delayOptimization()
			{
				this.seqOptimization.rewindAndPlay();
			}

			function beginDrag()
			{
				this.im3d.visible=false;
				this.imExport.visible=false;
				this.seq3d.stop();
				this.workspace.mouseOperation = "moveMolecule";
				this.workspace.mouseTarget = this.molecule;
			}

			function endDrag()
			{
				this.seq3d.rewindAndPlay();
				if( typeof(this.parent.onBoardEndDrag) == "function")
				{
					this.parent.onBoardEndDrag(this);
				}
				this.workspace.mouseOperation = "";
				this.workspace.mouseTarget = null;
				if( this.workspace.trash.isActive())
				{
					this.workspace.removeBoard( this);
				}
			}

			function handleOnAtomPressed(atom)
			{
				if( this.freezed)	return;
				this.workspace.mouseOperation = "pressedOnAtom";
				this.workspace.mouseTarget = this;
			}

			function handleOnAtomReleased(atom)
			{
				if( this.freezed)	return;
				if( this.workspace.mouseOperation == "sliceMolecule")
				{
					this.endSlice();
				}
				this.workspace.mouseOperation = "";
				this.workspace.mouseTarget = null;
				this.molecule.setDefaultStyles();
			}

			function handleOnTextRollOver(atom)
			{
				this.workspace.highlightedAtom = atom;
			}

			function handleOnTextRollOut(atom)
			{
				this.workspace.highlightedAtom = null;
			}

			function acceptMoveBy(dx, dy)
			{
				var obj = new Object;
				obj.x = dx;
				obj.y = dy;
				if( this.freezed)	return obj;
				var bounds = this.molecule.getScreenBounds();
				var lt = this.localToGlobal( bounds.left, bounds.top);
				var rb = this.localToGlobal( bounds.right, bounds.bottom);
				// Ограничение по X
				if( lt.x + dx - scene.x < this.workspace.sceneBounds.left)
					obj.x = this.workspace.sceneBounds.left - lt.x + scene.x;
				else	if( rb.x + dx - scene.x > this.workspace.sceneBounds.right)
					obj.x = this.workspace.sceneBounds.right - rb.x + scene.x;
				// Ограничение по Y
				if( lt.y + dy - scene.y < this.workspace.sceneBounds.top)
					obj.y = this.workspace.sceneBounds.top - lt.y + scene.y;
				else	if( rb.y + dy - scene.y > this.workspace.sceneBounds.bottom)
					obj.y = this.workspace.sceneBounds.bottom - rb.y + scene.y;

				if( typeof(scene.molList) != "undefined")
				{
					if( this.workspace.isBoardOnMolList(this))
					{
						if( this.workspace.mouseOperation == "moveMolecule")
							scene.molList.highlight( true);
					}
					else
					{
						scene.molList.highlight( false);
					}
				}

				return obj;
			}

			function setMoleculePosition(ax, ay)
			{
				var screenBounds = this.molecule.getScreenBounds();
				screenBounds.left += this.x;
				screenBounds.top += this.y;
				this.x += (ax - screenBounds.left);
				this.y += (ay - screenBounds.top);
			}

			function destroy()
			{
				if( this.externalOnBeforeDestroy != null)
					this.externalOnBeforeDestroy();
				this.molecule.destroy();
				this.parent = null;
			}

			// Функция начинает перетаскивание части молекулы
			function beginSlice()
			{
				if( this.molecule.aMarked == null)	return;
				//var visibleArray = new Array();
				for( var i=0;i<this.molecule.aMarked.length;i++)
				{
					var atom = this.molecule.aMarked[i];
					if( atom.oText != null)
					{
						atom.setExtendedDrawStyle(atom);
						//visibleArray.push( atom.oText);
					}
					if( atom!=this.molecule.markedRoot && atom.oLink != null)
					{
						//visibleArray.push( atom.oLink);
					}
				}
				//if( this.molecule.markedRoot!=null)
					//this.molecule.markedRoot.setExtendedDrawStyle(16);
				this.molecule.setStyles();

				//this.compSliced.imSliced.makeSnapshot( visibleArray);
				//this.compSliced.visible = true;
				var screenBounds = this.molecule.getMarkedScreenBounds();
				//this.compSliced.x = screenBounds.left + 4;
				//this.compSliced.y = screenBounds.top + 4;
				//this.compSliced.stick( true);
				this.workspace.mouseOperation = "sliceMolecule";
				//
				this.splicedTreeView.init( this.molecule);
				this.splicedTreeView.x = this.xMouse + 20;
				this.splicedTreeView.y = this.yMouse + 10;
				this.splicedTreeView.stick( true);
			}

			function endSlice()
			{
trace("#FF5F00 ********************************************************************");
				if( this.workspace.mouseOperation != "sliceMolecule")	return;
				if( this.workspace.highlightedAtom != null)
				{
					var i=0;
					var destAtom = this.workspace.highlightedAtom;
					if( typeof(destAtom.myCycle) != "undefined")
					{
					trace("#FF00FF Yopt!!");
						destAtom = destAtom.myCycle;
					}

					for( ;i<this.molecule.aMarked.length;i++)
					{
						trace("#FF0000 this.molecule.aMarked[i]="+this.molecule.aMarked[i].type+",this.workspace.highlightedAtom="+destAtom.type);
						if( this.molecule.aMarked[i] == destAtom)
						{
							break;
						}
					}
					if( i == this.molecule.aMarked.length)
					{
						// попробуем добавить атом к молекуле
						trace("Try to add");
						this.molecule.spliceSmallestSubtree(this.molecule.markedRoot, this.workspace.highlightedAtom);
					}
					else
					{
						// атом на той же цепочке
						trace("It`s try to make cycle");
					}
				}
				else
				{
					// Удалим все нах!
					trace("this.molecule.markRoot="+this.molecule.markedRoot);
					this.molecule.deleteSmallestSubtree(this.molecule.markedRoot);

					if( !this.workspace.trash.isActive())
					{
						// И создадим новую молекулу
trace("this.splicedTreeView.molecule=" + this.splicedTreeView.molecule + ":" + typeof(this.splicedTreeView.molecule));
						var board = this.workspace.addMolecule( this.splicedTreeView.molecule);
						this.splicedTreeView.molecule = null;
						board.setMoleculePosition( this.workspace.xMouse, this.workspace.yMouse);
trace("#FF5000***********************************");
						this.workspace.sceneBounds.right = 803;
						var objXY = board.acceptMoveBy(0, 0);
						board.x += objXY.x;
						board.y += objXY.y;
						this.workspace.sceneBounds.right = 1010;
						/*if( typeof(board.parent.onBoardEndDrag) == "function")
						{
							board.parent.onBoardEndDrag(board);
						}*/
trace("objXY.x="+objXY.x+":"+objXY.y);
					}
				}
				this.molecule.setDefaultStyles();
				this.splicedTreeView.stick( false);
				this.splicedTreeView.visible=false;
				this.workspace.trash.activate( false);
				this.workspace.mouseOperation = "";
				this.workspace.mouseTarget = null;
			}

			function cancelSlice()
			{
				if( this.workspace.mouseOperation != "sliceMolecule")	return;
				this.molecule.setDefaultStyles();
				this.compSliced.stick( false);
				this.compSliced.visible=false;
				this.workspace.mouseOperation = "";
				this.workspace.mouseTarget = null;
			}

			function prepareToSnapshot()
			{
				this.im3d.hide();
				this.imExport.hide();
			}

			function handleOnLinkRButtonDown( atom)
			{
				if( atom.linkType == 2)
				{
					this.workspace.showSelection4Link( atom, true/*extended*/);
				}
				else	if( atom.linkType > 0)
				{
					this.workspace.showSelection4Link( atom, false);
				}
			}

			function handleOnAtomRButtonDown( atom)
			{
trace("0. #00FF00 ******************************");
				this.workspace.showSelection4Atom( atom);
				if( subst.visible)
				{
					this.highlightMolecule(true);
				}
			}

			function showH(abShow)
			{
				this.molecule.showH(abShow);
			}
		]]></script>
		<sequencer name="seq3d">
			<script>
				1000;
			</script>
			<script><![CDATA[
				if( subst.visible == true && this.showBtn3D)
				{
					this.im3d.visible = true;
					this.imExport.visible = true;
				}
			]]></script>
		</sequencer>
		<script event="onMouseMove"><![CDATA[
			if( this.freezed)	exit();
			if( this.workspace.mouseOperation == "pressedOnAtom" && this.workspace.mouseTarget==this)
			{
				if( this.molecule.xPressMouse != scene.xMouse ||
					this.molecule.yPressMouse != scene.yMouse)
				{
					this.workspace.mouseOperation = "sliceMolecule";
					this.beginSlice();
				}
			}
			if( this.autoHighlight)
			{
				if( isMouseOn())
				{
					if( !subst.visible)
						this.highlightMolecule(true);
				}
				else
				{
					if( subst.visible)
					{
						if( this.workspace.isMouseOn())
							this.highlightMolecule(false);
					}
				}
			}
		]]></script>
		<script event="onRMouseDown">
			if( this.workspace.mouseOperation == "sliceMolecule")
			{
				this.cancelSlice();
			}
		</script>
		<sequencer name="seqOptimization">
			<script>
				1000;
			</script>
			<script>
				if( this.molecule!=null)
				{
					this.molecule.visualize();
					this.onMoleculeChanged();
				}
			</script>
		</sequencer>
	</composition>
</rmml>