void ProcDefBlock_FormalParamAdded(object sender, IProcDefBit newBit) { ProcDefBlock b = (ProcDefBlock)sender; ProcDefView v = (ProcDefView)ViewFromBlock(b); DataType type = DataType.Invalid; if (newBit is VarDefBlock) { VarDefBlock vb = (VarDefBlock)newBit; type = (vb).Type; int height = ArgBitTextHeight(newBit); EditableVarDefView vv = new EditableVarDefView(vb, MakeTextBitBitmap(vb.Name, height), varb_parts, textMetrics, textFont); blockViews[vb] = vv; vb.TextChanged += new VarDefBlockTextChangedEvent(ProcDef_FormalParamTextChanged); v.AddFormalBit(vv, type); } else { ProcDefTextBit pb = (ProcDefTextBit)newBit; pb.TextChanged += new ProcDefTextBitTextChangedEvent(ProcDefTextBit_TextChanged); EditableLabelView lv = new EditableLabelView(MakeTextBitBitmap(pb.Text, ArgBitTextHeight(pb)), (ProcDefTextBit)newBit); blockViews[pb] = lv; v.AddFormalBit(lv, DataType.Invalid); } }
private IBlock ToBlock(JToken json) { if (json.Type == JTokenType.Array) { if (json[0].ToString() == "do") { return(ToBlockStack(json)); } else if (json[0].ToString() == "var") { return(ToVarAccess(json)); } else if (json[0].ToString() == "define") { ProcDefBlock b = ToProcDef(json); handleDefineNewProc(b); return(b); } else // assume invokation block { return(ToInvokationBlock(json)); } } else { return(new TextBlock(json.ToString())); } }
private static ProcDefBlock ToProcDef(JToken json) { ProcDefBlock b = new ProcDefBlock(); string procName = json[1][0].ToString(); List<string> argNames = new List<string>(); List<DataType> argTypes = new List<DataType>(); for (int i = 1; i < json[1].Count(); i += 2) { argNames.Add(json[1][i].ToString()); argTypes.Add(DataTypeNames.TypeOf(json[1][i + 1].ToString())); } string[] bits = procName.SplitFuncArgs(); int argCount = 0; for (int i = 0; i < bits.Length; ++i) { string bit = bits[i]; if (bit == "%") { b.AddBit(new VarDefBlock(argNames[argCount], argTypes[argCount])); argCount++; } else { b.AddBit(new ProcDefTextBit(bit)); } } return b; }
private static ProcDefBlock ToProcDef(JToken json) { ProcDefBlock b = new ProcDefBlock(); string procName = json[1][0].ToString(); List <string> argNames = new List <string>(); List <DataType> argTypes = new List <DataType>(); for (int i = 1; i < json[1].Count(); i += 2) { argNames.Add(json[1][i].ToString()); argTypes.Add(DataTypeNames.TypeOf(json[1][i + 1].ToString())); } string[] bits = procName.SplitFuncArgs(); int argCount = 0; for (int i = 0; i < bits.Length; ++i) { string bit = bits[i]; if (bit == "%") { b.AddBit(new VarDefBlock(argNames[argCount], argTypes[argCount])); argCount++; } else { b.AddBit(new ProcDefTextBit(bit)); } } return(b); }
public IBlock DeepClone() { ProcDefBlock ret = new ProcDefBlock(); Bits.ForEach(b => ret.AddBit(b.DeepClone())); return(ret); }
private void Run() { // Assume only one block with 'when flag clicked' for now IBlock mainBlock = null; foreach (IBlock b in controller.GetTopLevelBlocks()) { if (b is BlockStack) { BlockStack stack = ((BlockStack)b); IBlock s = stack[0]; if (s is InvokationBlock) { InvokationBlock ib = (InvokationBlock)s; if (ib.Text == "when _flag_ clicked") { mainBlock = b; } } else if (s is ProcDefBlock) { ProcDefBlock pdb = (ProcDefBlock)s; Method m = compiler.DefineMethod(pdb, stack); vm.DefineMethod(pdb.GetMethodString(), m); } } } if (mainBlock != null) { Run(mainBlock); } }
void ProcDefBlock_FormalParamRemoved(object sender, IProcDefBit bit) { ProcDefBlock b = (ProcDefBlock)sender; ProcDefView v = (ProcDefView)ViewFromBlock(b); IBlockView bitView = ViewFromBlock((IBlock)bit); v.RemoveFormalBit(bitView); }
private IBlockView ViewFromProcDefBlock(ProcDefBlock b) { List <IBlockView> subContent = new List <IBlockView>(); int i = 0; int n = b.Bits.Count; BitArray trueArgs = new BitArray(n); DataType[] argTypes = new DataType[n]; // slow: Repeats instanceof test twice for all bits; once for height and once to add to subContent int height = b.Bits.Count == 0?1: b.Bits.Max(bb => ArgBitTextHeight(bb)); foreach (IProcDefBit bit in b.Bits) { if (bit is VarDefBlock) { VarDefBlock vb = (VarDefBlock)bit; EditableVarDefView vv = new EditableVarDefView(vb, MakeTextBitBitmap(vb.Name, height), varb_parts, textMetrics, textFont); blockViews[vb] = vv; subContent.Add(vv); trueArgs[i] = true; argTypes[i] = vb.Type; vb.TextChanged += new VarDefBlockTextChangedEvent(ProcDef_FormalParamTextChanged); } else { ProcDefTextBit ptb = (ProcDefTextBit)bit; EditableLabelView ev = new EditableLabelView( MakeTextBitBitmap(ptb.Text, height), ptb); blockViews[ptb] = ev; subContent.Add(ev); argTypes[i] = DataType.Invalid; trueArgs[i] = false; ptb.TextChanged += new ProcDefTextBitTextChangedEvent(ProcDefTextBit_TextChanged); } ++i; } Bitmap[] defineTextView = RenderTextBits(new string[] { "define" }); ContentView innerContent = new ContentView(subContent.ToArray(), argTypes, trueArgs, sb_parts); ContentView outerContent = new ContentView(new IBlockView[] { new LabelView(defineTextView[0]), innerContent }, new DataType[] { DataType.Invalid }, new BitArray(new bool[] { false, true }), pdb_parts); ProcDefView pdb = new ProcDefView(b, outerContent, innerContent); b.FormalParamAdded += new ProcDefBitAddedEvent(ProcDefBlock_FormalParamAdded); b.FormalParamRemoved += new ProcDefBitRemovedEvent(ProcDefBlock_FormalParamRemoved); b.FormalParamChanged += new ProcDefBitChangedEvent(ProcDefBlock_FormalParamChanged); return(pdb); }
public EditProcDefController NewProcDef(Func <TextBox> textBoxMaker, Button eraseButton) { ProcDefBlock block = new ProcDefBlock(); ProcDefView view = (ProcDefView)viewFactory.ViewFromBlock(block); EditProcDefController controller = new EditProcDefController(view, block, viewFactory, textBoxMaker, eraseButton); return(controller); }
public void DefineNewProcFromFile(ProcDefBlock proc, string category) { string methodName = proc.GetMethodString(); DataType[] argTypes = proc.GetArgTypes(); blockSpace.RegisterMethod(methodName, BlockAttributes.Stack, DataType.Script, argTypes, false); palette.AddTool(blockSpace, tool("nobmp|" + methodName, category, argTypes.Select(t => blockSpace.Default(t)).ToArray())); }
public ProcDefView(ProcDefBlock model, ContentView surroundingContent, ContentView invokationContent) { this._model = model; this.surroundingContent = surroundingContent; this.invokationContent = invokationContent; Changed += delegate(object sender) { }; surroundingContent.Changed += new ViewChangedEvent(content_Changed); surroundingContent.Parent = this; invokationContent.RelativePos = new Point(0, 0); Reassemble(); }
private IBlock ToBlockStack(JToken json) { BlockStack b = new BlockStack(); for (int i = 1; i < json.Count(); ++i) { IBlock subBlock = ToBlock(json[i]); if (i == 1 && subBlock is ProcDefBlock) { currentProcDef = subBlock as ProcDefBlock; } b.Add(subBlock); } currentProcDef = null; return(b); }
public void DefineNewProc(ProcDefBlock proc, string category) { string methodName = proc.GetMethodString(); DataType[] argTypes = proc.GetArgTypes(); blockSpace.RegisterMethod(methodName, BlockAttributes.Stack, DataType.Script, argTypes, false); palette.AddTool(blockSpace, tool("nobmp|" + methodName, category, argTypes.Select(t => blockSpace.Default(t)).ToArray())); Random r = new Random(); int thirdX = canvasSize.Width / 3; int thirdY = canvasSize.Height / 3; int x = thirdX + r.Next(thirdX); int y = thirdY + r.Next(thirdY); AddTopLevel(proc, new Point(x, y)); }
public IBlockView ViewFromBlock(IBlock block) { if (blockViews.ContainsKey(block)) { return(blockViews[block]); } if (block is BlockStack) { IBlockView ret = ViewFromBlockStack((BlockStack)block);; blockViews[block] = ret; return(ret); } if (block is VarAccessBlock || block is VarDefBlock) { IBlockView ret = new VariableView((IVarBlock)block, varb_parts, textMetrics, textFont); blockViews[block] = ret; return(ret); } if (block is TextBlock) { IBlockView ret = new TextView((TextBlock)block, ib_parts, textMetrics, textFont); blockViews[block] = ret; return(ret); } if (block is InvokationBlock) { InvokationBlock b = (InvokationBlock)block; BlockAttributes attr = blockSpace.AttributeOf(b); IBlockView r = ViewFromInvokationBlock(b, attr); blockViews[block] = r; return(r); } if (block is ProcDefBlock) { ProcDefBlock b = (ProcDefBlock)block; BlockAttributes attr = blockSpace.AttributeOf(b); IBlockView r = ViewFromProcDefBlock(b); blockViews[block] = r; return(r); } throw new ArgumentException(); }
public EditProcDefController(ProcDefView view, ProcDefBlock model, BlockViewFactory factory, Func<TextBox> textBoxMaker, Button eraseButton) { this.view = view; this.view.Changed += new ViewChangedEvent(view_Changed); this.model = model; this.factory = factory; this.textBoxMaker = textBoxMaker; this.eraseButton = eraseButton; this.eraseButton.Hide(); this.eraseButton.Click += new EventHandler(eraseButton_Click); this.Changed += delegate(object sender) { }; State = EditState.Ready; // I wish the view was dynamically centered, but there's some mismatch // between a label's textbox and the place where the text is drawn // for now we hide it by not constantly moving the view // strangely, the mistmatch disappears (text is drawn in the right place) // when view.RelativePos is not moved anymore. hmm... // Point origin = size.Center(b.Size); Point origin = new Point(15, 15); view.RelativePos = origin; }
public EditProcDefController(ProcDefView view, ProcDefBlock model, BlockViewFactory factory, Func <TextBox> textBoxMaker, Button eraseButton) { this.view = view; this.view.Changed += new ViewChangedEvent(view_Changed); this.model = model; this.factory = factory; this.textBoxMaker = textBoxMaker; this.eraseButton = eraseButton; this.eraseButton.Hide(); this.eraseButton.Click += new EventHandler(eraseButton_Click); this.Changed += delegate(object sender) { }; State = EditState.Ready; // I wish the view was dynamically centered, but there's some mismatch // between a label's textbox and the place where the text is drawn // for now we hide it by not constantly moving the view // strangely, the mistmatch disappears (text is drawn in the right place) // when view.RelativePos is not moved anymore. hmm... // Point origin = size.Center(b.Size); Point origin = new Point(15, 15); view.RelativePos = origin; }
public IBlock DeepClone() { ProcDefBlock ret = new ProcDefBlock(); Bits.ForEach(b=>ret.AddBit(b.DeepClone())); return ret; }
private IBlockView ViewFromProcDefBlock(ProcDefBlock b) { List<IBlockView> subContent = new List<IBlockView>(); int i = 0; int n = b.Bits.Count; BitArray trueArgs = new BitArray(n); DataType[] argTypes = new DataType[n]; // slow: Repeats instanceof test twice for all bits; once for height and once to add to subContent int height = b.Bits.Count==0?1: b.Bits.Max(bb => ArgBitTextHeight(bb)); foreach (IProcDefBit bit in b.Bits) { if (bit is VarDefBlock) { VarDefBlock vb = (VarDefBlock)bit; EditableVarDefView vv = new EditableVarDefView(vb, MakeTextBitBitmap(vb.Name, height), varb_parts, textMetrics, textFont); blockViews[vb] = vv; subContent.Add(vv); trueArgs[i] = true; argTypes[i] = vb.Type; vb.TextChanged += new VarDefBlockTextChangedEvent(ProcDef_FormalParamTextChanged); } else { ProcDefTextBit ptb = (ProcDefTextBit) bit; EditableLabelView ev = new EditableLabelView( MakeTextBitBitmap(ptb.Text, height), ptb); blockViews[ptb] = ev; subContent.Add(ev); argTypes[i] = DataType.Invalid; trueArgs[i] = false; ptb.TextChanged += new ProcDefTextBitTextChangedEvent(ProcDefTextBit_TextChanged); } ++i; } Bitmap[] defineTextView = RenderTextBits(new string[] { "define" }); ContentView innerContent = new ContentView(subContent.ToArray(), argTypes, trueArgs, sb_parts); ContentView outerContent = new ContentView(new IBlockView[] { new LabelView(defineTextView[0]), innerContent }, new DataType[] { DataType.Invalid }, new BitArray(new bool[] { false, true }), pdb_parts); ProcDefView pdb = new ProcDefView(b, outerContent, innerContent); b.FormalParamAdded += new ProcDefBitAddedEvent(ProcDefBlock_FormalParamAdded); b.FormalParamRemoved += new ProcDefBitRemovedEvent(ProcDefBlock_FormalParamRemoved); b.FormalParamChanged += new ProcDefBitChangedEvent(ProcDefBlock_FormalParamChanged); return pdb; }
private IBlock ToBlockStack(JToken json) { BlockStack b = new BlockStack(); for (int i = 1; i < json.Count(); ++i) { IBlock subBlock = ToBlock(json[i]); if (i == 1 && subBlock is ProcDefBlock) currentProcDef = subBlock as ProcDefBlock; b.Add(subBlock); } currentProcDef = null; return b; }
public void DefineNewProc(ProcDefBlock proc, string category) { string methodName = proc.GetMethodString(); DataType[] argTypes = proc.GetArgTypes(); blockSpace.RegisterMethod(methodName, BlockAttributes.Stack, DataType.Script, argTypes, false); palette.AddTool(blockSpace, tool("nobmp|"+methodName, category, argTypes.Select(t=>blockSpace.Default(t)).ToArray())); Random r = new Random(); int thirdX = canvasSize.Width / 3; int thirdY = canvasSize.Height / 3; int x = thirdX + r.Next(thirdX); int y = thirdY + r.Next(thirdY); AddTopLevel(proc, new Point(x, y)); }
public EditProcDefController NewProcDef(Func<TextBox> textBoxMaker, Button eraseButton) { ProcDefBlock block = new ProcDefBlock(); ProcDefView view = (ProcDefView) viewFactory.ViewFromBlock(block); EditProcDefController controller = new EditProcDefController(view, block, viewFactory, textBoxMaker, eraseButton); return controller; }
internal void MouseDown(Point p) { if (state == CanvasState.TextEditing) { // Since the mousedown registered, we've clicked outside the textbox ResetTextEditState(); } else if (state == CanvasState.Ready) { if (canvasView.PaletteRect.Contains(p)) { int x = canvasView.PaletteRect.Left; int y = canvasView.PaletteRect.Top; IBlock[] defaultArgs; string funcName = palette.HitTest(p.Offseted(-x, -y), out defaultArgs); if (funcName != "") { IBlock b = blockSpace.makeNewBlock(funcName, defaultArgs); TopLevelScript s = AddTopLevel(b, p.Offseted(-5, -5)); dragged = blockViews[b]; draggingOrigin = p; draggedModel = s; state = CanvasState.Dragging; PrepareDropRegions(b); Update(ViewBounds(dragged)); return; } } IBlockView hit = HitTest(p); if (hit == null) { return; } if (!allViews.ContainsKey(hit)) { if (hit.Model.ParentRelationship.Type == ParentRelationshipType.Stack) { int i = hit.Model.ParentRelationship.Index; Point np = hit.AbsolutePos(); Rectangle bounds = ViewBounds(hit.AbsoluteAncestor()); BlockStack parent = (BlockStack)hit.Model.ParentRelationship.Parent; TopLevelScript splitted = SplitBlockStack(parent, i, np); Update(bounds); draggedModel = splitted; hit = blockViews[splitted.Block]; } else if (hit.Model.ParentRelationship.Type == ParentRelationshipType.Arg) { if (hit is ITextualView) { // We shouldn't detach e.g a number argument from its block // but we should enable the user to edit it SetEditState((ITextualView)hit); return; } int i = hit.Model.ParentRelationship.Index; Point np = hit.AbsolutePos(); Rectangle bounds = ViewBounds(hit.AbsoluteAncestor()); InvokationBlock parent = (InvokationBlock)hit.Model.ParentRelationship.Parent; TopLevelScript splitted = TakeoutBlockArgument(parent, i, np); Update(bounds); draggedModel = splitted; hit = blockViews[splitted.Block]; } else if (hit.Model.ParentRelationship.Type == ParentRelationshipType.FormalParameter) { ProcDefBlock pd = (ProcDefBlock )hit.Model.ParentRelationship.Parent; VarAccessBlock va = new VarAccessBlock((VarDefBlock)pd.Bits[hit.Model.ParentRelationship.Index]); TopLevelScript tls = AddTopLevel(va, p); hit = ViewFromBlock(va); draggedModel = tls; } else if (hit.Model.ParentRelationship.Type == ParentRelationshipType.None) { hit = null; draggedModel = null; } } else { draggedModel = blockSpace.FindScript(hit.Model); } if (hit != null) { dragged = hit; draggingOrigin = p; state = CanvasState.Dragging; PrepareDropRegions(hit.Model); } Update(); } }