/// <summary>
        /// CreateObjectの結果に基づいてプレビューを表示します.
        /// </summary>
        protected virtual void SetPreview()
        {
            if (_operation == null)
            {
                return;
            }
            // 処理の種類を判別したい
            IMotionOperationCreateObject   createOpe  = _operation as IMotionOperationCreateObject;
            IMotionOperationEditObject     editOpe    = _operation as IMotionOperationEditObject;
            IMotionOperationGeneral        generalOpe = _operation as IMotionOperationGeneral;
            IMotionOperationOutputSequence outputOpe  = _operation as IMotionOperationOutputSequence;

            // 前回のプレビュー用オブジェクトを消しておく
            motionDataViewer.ClearPreviewObjects();

            // 作成や編集でなければプレビューしない
            if (createOpe == null && editOpe == null)
            {
                panelPreview.Visible = false;
                return;
            }

            panelPreview.Visible = true;
            // 引数チェック.失敗したら表示しない
            string errorMessage = "";

            if (!_operation.ValidateArguments(_exec.Parameters, ref errorMessage))
            {
                return;
            }
            // フレーム取得.失敗したら表示しない
            MotionFrame frame = _console.MotionDataSet.GetFrameAt(TimeController.Singleton.CurrentTime);

            if (frame == null)
            {
                return;
            }
            // 作成/編集結果のオブジェクトと,プレビュー時のみに表示されるオブジェクトを分類する
            IList <MotionObject> previewObjs    = null;
            IList <MotionObject> previewSubObjs = null;

            if (createOpe != null)
            {
                previewSubObjs = createOpe.CreateObjects(_targetInfoList, _exec.Parameters, new ReadOnlyMotionFrame(frame), true);
                previewObjs    = createOpe.CreateObjects(_targetInfoList, _exec.Parameters, new ReadOnlyMotionFrame(frame), false);
            }
            else if (editOpe != null)
            {
                previewSubObjs = editOpe.EditObject(_targetInfoList, _exec.Parameters, new ReadOnlyMotionFrame(frame), true);
                previewObjs    = editOpe.EditObject(_targetInfoList, _exec.Parameters, new ReadOnlyMotionFrame(frame), false);
            }
            if (previewObjs == null)
            {
                return;
            }
            if (previewSubObjs == null)
            {
                previewSubObjs = previewObjs;
            }
            if (previewSubObjs.Count >= previewObjs.Count)
            {
                previewSubObjs = previewSubObjs.Skip(previewObjs.Count).ToList();
            }
            // プレビュー対象をビューに登録しつつ,重心を求める
            Vector3 sum   = Vector3.Empty;
            int     count = 0;

            if (previewObjs != null)
            {
                foreach (var @object in previewObjs)
                {
                    if (@object != null)
                    {
                        sum += @object.GravityPoint;
                        motionDataViewer.AddPreviewObject(@object, false);
                        count++;
                    }
                }
                foreach (var @object in previewSubObjs)
                {
                    if (@object != null)
                    {
                        sum += @object.GravityPoint;
                        motionDataViewer.AddPreviewObject(@object, true);
                        count++;
                    }
                }
                if (editOpe != null)
                {
                    foreach (MotionObjectInfo info in _targetInfoList)
                    {
                        motionDataViewer.AddPreviewRemoveObject(info);
                    }
                }
            }
            // ビューのカメラを重心の方に向ける
            if (count > 0)
            {
                sum *= 1f / count;
                motionDataViewer.ViewCamera.SetTarget(sum, false);
            }
        }
Exemplo n.º 2
0
        public ScriptVariable Call(IList <ScriptVariable> args, ScriptConsole console)
        {
            _progress.Initialize(0, "Initializing...");
            if (args == null)
            {
                throw new ArgumentNullException("args", "args cannot be null");
            }
            if (args.Count < 1)
            {
                args = new List <ScriptVariable> {
                    null
                }
            }
            ;
            if (args[0] == null)
            {
                args[0] = new ListVariable();
            }
            IList <ScriptVariable> objectNamesVar = args[0].ToList();

            if (objectNamesVar.Any(n => n.IsNull()))
            {
                throw new ArgumentException(global::MotionDataHandler.Properties.Settings.Default.Msg_ObjectNameCannotBeNull, "args");
            }
            List <string>           objectNames = objectNamesVar.Select(n => n.ToString()).ToList();
            MotionDataSet           dataSet     = console.MotionDataSet;
            MotionProcEnv           env2        = new MotionProcEnv(console);
            List <MotionObjectInfo> infoList    = new List <MotionObjectInfo>();

            foreach (string objectName in objectNames)
            {
                MotionObjectInfo info = dataSet.GetObjectInfoByName(objectName);
                if (info == null)
                {
                    throw new ArgumentException(global::MotionDataHandler.Properties.Settings.Default.Msg_ObjectNotFound + ": " + objectName, "args");
                }
                infoList.Add(info);
            }
            foreach (MotionObjectInfo info in infoList)
            {
                if (!_operation.FilterSelection(info))
                {
                    throw new ArgumentException(global::MotionDataHandler.Properties.Settings.Default.Msg_InvalidTargetObjectSpecified + ": " + info.Name, "args");
                }
            }
            string errorMessage = "";

            if (!_operation.ValidateSelection(infoList, ref errorMessage))
            {
                if (errorMessage == null)
                {
                    errorMessage = "";
                }
                throw new ArgumentException(global::MotionDataHandler.Properties.Settings.Default.Msg_ImproperObjectSelection + ": " + errorMessage, "args");
            }

            IList <ProcParam <MotionProcEnv> > parameters = _operation.GetParameters() ?? new ProcParam <MotionProcEnv> [0];

            if (args.Count != parameters.Count + 1)
            {
                throw new ArgumentException(string.Format(global::MotionDataHandler.Properties.Settings.Default.Msg_NumberOfArgumentsRequired, parameters.Count + 1));
            }
            for (int i = 0; i < parameters.Count; i++)
            {
                if (!parameters[i].FromScriptVariable(env2, args[i + 1], ref errorMessage))
                {
                    throw new ArgumentException(string.Format(global::MotionDataHandler.Properties.Settings.Default.Msg_InvalidNthArgument + ": {1}", i + 1, errorMessage ?? ""), "args");
                }
            }
            if (!_operation.ValidateArguments(parameters, ref errorMessage))
            {
                throw new ArgumentException(string.Format(global::MotionDataHandler.Properties.Settings.Default.Msg_InvalidArgument + ": {0}", errorMessage ?? ""), "args");
            }
            IMotionOperationGeneral general = _operation as IMotionOperationGeneral;

            if (general != null)
            {
                _progress.Initialize(0, "Operation");
                general.Operate(infoList, parameters, dataSet, _progress);
                return(new ListVariable(infoList.Select(info => new StringVariable(info.Name))));
            }
            IMotionOperationEditObject edit = _operation as IMotionOperationEditObject;

            if (edit != null)
            {
                _progress.Initialize(dataSet.FrameLength, "Edit Object");
                foreach (MotionFrame frame in dataSet.EnumerateFrame())
                {
                    IList <MotionObject> results = edit.EditObject(infoList, parameters, new ReadOnlyMotionFrame(frame), false);
                    int count = Math.Min(results.Count, infoList.Count);
                    for (int i = 0; i < count; i++)
                    {
                        frame[infoList[i]] = results[i];
                    }
                    _progress.CurrentValue++;
                }
                dataSet.DoFrameListChanged();
                return(new ListVariable(infoList.Select(info => new StringVariable(info.Name))));
            }
            IMotionOperationOutputSequence output = _operation as IMotionOperationOutputSequence;

            if (output != null)
            {
                _progress.Initialize(0, "Output");
                IList <Sequence.SequenceData> sequences = output.OutputSequence(infoList, parameters, dataSet.EnumerateFrame().Select(frame => new ReadOnlyMotionFrame(frame)), _progress);
                foreach (Sequence.SequenceData sequence in sequences)
                {
                    console.SequenceController.AddSequence(sequence);
                }
                return(new ListVariable(sequences.Select(s => new StringVariable(s.Title))));
            }
            IMotionOperationCreateObject create = _operation as IMotionOperationCreateObject;

            if (create != null)
            {
                _progress.Initialize(dataSet.FrameLength, "Create Object");
                IList <MotionObjectInfo> newInfoList = create.GetNewObjectInfoList(infoList, parameters);
                MotionFrame firstFrame = dataSet.GetFrameByIndex(0);
                if (firstFrame != null)
                {
                    IList <MotionObject> newObjects = create.CreateObjects(infoList, parameters, new ReadOnlyMotionFrame(firstFrame), false) ?? new MotionObject[0];
                    if (newObjects.Count != newInfoList.Count)
                    {
                        throw new InvalidOperationException(global::MotionDataHandler.Properties.Settings.Default.Msg_CreateObjectLengthMismatch);
                    }
                }
                foreach (MotionObjectInfo newInfo in newInfoList)
                {
                    dataSet.AddObject(newInfo);
                }
                foreach (MotionFrame frame in dataSet.EnumerateFrame())
                {
                    IList <MotionObject> newObjects = create.CreateObjects(infoList, parameters, new ReadOnlyMotionFrame(frame), false) ?? new MotionObject[0];
                    int count = Math.Min(newObjects.Count, newInfoList.Count);
                    for (int i = 0; i < count; i++)
                    {
                        frame[newInfoList[i]] = newObjects[i];
                    }
                    _progress.CurrentValue++;
                }
                dataSet.DoObjectInfoSetChanged();
                dataSet.DoFrameListChanged();
                return(new ListVariable(newInfoList.Select(info => new StringVariable(info.Name))));
            }
            return(null);
        }
        /// <summary>
        /// 各メニューを構築します
        /// </summary>
        public void InitializeMenus()
        {
            _menuAllItems = new List <OperationMenuItem>();

            // 作成メニュー
            _createMenuItem = new ToolStripMenuItem("Create");
            _createMenuItem.DropDownOpening += onMenuOpening;
            // 作成されるオブジェクトごとにメニューを分けるので,作成されるオブジェクトを列挙
            HashSet <Type> motionObjectTypes = new HashSet <Type>();
            bool           otherTypeExists   = false;

            foreach (IMotionOperationCreateObject ope in _dataSet.GetOperationCreateObject())
            {
                Type t = ope.CreatedType;
                if (t != null && t.IsSubclassOf(typeof(MotionObject)))
                {
                    motionObjectTypes.Add(t);
                }
                else
                {
                    otherTypeExists = true;
                }
            }
            // 作成されるオブジェクトごとのメニューを作成
            Dictionary <Type, ToolStripMenuItem> typeMenus = new Dictionary <Type, ToolStripMenuItem>();

            foreach (Type type in motionObjectTypes)
            {
                MotionObjectInfo  testInfo = new MotionObjectInfo(type);
                MotionObject      testObj  = testInfo.GetEmptyObject();
                ToolStripMenuItem item     = new ToolStripMenuItem(type.Name, testObj.GetIcon() ?? global::MotionDataHandler.Properties.Resources.question);
                typeMenus[type] = item;
            }
            ToolStripMenuItem otherTypeMenu = new ToolStripMenuItem("General");

            // 処理ごとにメニューを作成
            foreach (bool isEditWrapper in new bool[] { false, true })
            {
                if (isEditWrapper)
                {
                    foreach (Type t in motionObjectTypes)
                    {
                        typeMenus[t].DropDownItems.Add(new ToolStripSeparator());
                    }
                    if (otherTypeExists)
                    {
                        otherTypeMenu.DropDownItems.Add(new ToolStripSeparator());
                    }
                }
                foreach (IMotionOperationCreateObject ope in _dataSet.GetOperationCreateObject())
                {
                    if ((ope is MotionOperationEditToCreateWrapper) != isEditWrapper)
                    {
                        continue;
                    }
                    Type t = ope.CreatedType;
                    ToolStripMenuItem item = new ToolStripMenuItem(ope.GetTitle());
                    try {
                        Bitmap icon = ope.IconBitmap;
                        if (icon != null)
                        {
                            item.Image = icon;
                        }
                    } catch (NotImplementedException) { }
                    IMotionOperationCreateObject opeForLabmda = ope;
                    item.Click += new EventHandler((sender, e) => {
                        DialogMotionOperation dialog = new DialogMotionOperation(ScriptConsole.Singleton, opeForLabmda);
                        dialog.ShowDialog();
                    });
                    _menuAllItems.Add(new OperationMenuItem(ope, item));
                    if (motionObjectTypes.Contains(t))
                    {
                        typeMenus[t].DropDownItems.Add(item);
                    }
                    else
                    {
                        otherTypeMenu.DropDownItems.Add(item);
                    }
                }
            }
            foreach (Type t in motionObjectTypes.OrderBy(t => t.Name))
            {
                _createMenuItem.DropDownItems.Add(typeMenus[t]);
            }
            if (otherTypeExists)
            {
                _createMenuItem.DropDownItems.Add(otherTypeMenu);
            }
            if (_createMenuItem.DropDownItems.Count == 0)
            {
                _createMenuItem = null;
            }

            // その他
            _editMenuItem    = new ToolStripMenuItem();
            _outputMenuItem  = new ToolStripMenuItem();
            _generalMenuItem = new ToolStripMenuItem();
            List <MenuAutoGenerator> genList = new List <MenuAutoGenerator>();

            genList.Add(new MenuAutoGenerator("Edit", _editMenuItem, () => _dataSet.GetOperationEditObject().Select(ope => (IMotionOperationBase)ope).ToList()));
            genList.Add(new MenuAutoGenerator("Output", _outputMenuItem, () => _dataSet.GetOperationOutputSequence().Select(ope => (IMotionOperationBase)ope).ToList()));
            genList.Add(new MenuAutoGenerator("General", _generalMenuItem, () => _dataSet.GetOperationGeneral().Select(ope => (IMotionOperationBase)ope).ToList()));
            foreach (MenuAutoGenerator gen in genList)
            {
                gen.OutputMenu.Text             = gen.DefaultName;
                gen.OutputMenu.DropDownOpening += onMenuOpening;
                foreach (IMotionOperationBase ope in gen.OperationGenerator())
                {
                    ToolStripMenuItem item = new ToolStripMenuItem(ope.GetTitle());
                    try {
                        Bitmap icon = ope.IconBitmap;
                        if (icon != null)
                        {
                            item.Image = icon;
                        }
                    } catch (NotImplementedException) { }
                    IMotionOperationBase opeForLabmda = ope;
                    item.Click += new EventHandler((sender, e) => {
                        DialogMotionOperation dialog = new DialogMotionOperation(ScriptConsole.Singleton, opeForLabmda);
                        dialog.ShowDialog();
                    });
                    _menuAllItems.Add(new OperationMenuItem(ope, item));
                    gen.OutputMenu.DropDownItems.Add(item);
                }
            }
            if (_editMenuItem.DropDownItems.Count == 0)
            {
                _editMenuItem = null;
            }
            if (_outputMenuItem.DropDownItems.Count == 0)
            {
                _outputMenuItem = null;
            }
            if (_generalMenuItem.DropDownItems.Count == 0)
            {
                _generalMenuItem = null;
            }
        }