Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release-beta-0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiashuo Li committed Apr 28, 2015
2 parents f166e37 + bf7d826 commit 782fb21
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 109 deletions.
3 changes: 2 additions & 1 deletion UI/Controls/InstructionPanel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
<Style TargetType="Image">
<Setter Property="Stretch" Value="Fill" />
<Setter Property="ToolTipService.ShowDuration" Value="100000" />
<EventSetter Event="MouseDown" Handler="MouseMove_General" />
<EventSetter Event="MouseMove" Handler="Image_MouseMove" />
<EventSetter Event="MouseDown" Handler="Image_MouseDown" />
</Style>
</Grid.Resources>

Expand Down
55 changes: 37 additions & 18 deletions UI/Controls/InstructionPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,42 @@

namespace iRobotGUI.Controls
{
/// <summary>
/// Interaction logic for InstructionPanel.xaml
/// </summary>
public partial class InstructionPanel : UserControl
{
public InstructionPanel()
{
InitializeComponent();
}
/// <summary>
/// Interaction logic for InstructionPanel.xaml
/// </summary>
public partial class InstructionPanel : UserControl
{

private void MouseMove_General(object sender, MouseEventArgs e)
{
Image dragSource = sender as Image;
if (e.LeftButton == MouseButtonState.Pressed)
{
DragDrop.DoDragDrop(sender as Image, dragSource.Tag, DragDropEffects.Copy);
}
}
}
public delegate void AddNewInstructionEventHandler(string opcode);

/// <summary>
/// A instruction image is double clicked and should be added to the program list.
/// </summary>
public event AddNewInstructionEventHandler AddNewInstruction;

public InstructionPanel()
{
InitializeComponent();
}

private void Image_MouseMove(object sender, MouseEventArgs e)
{
Image dragSource = sender as Image;
if (e.LeftButton == MouseButtonState.Pressed)
{
DragDrop.DoDragDrop(dragSource, dragSource.Tag, DragDropEffects.Copy);
}

}

private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
Image dragSource = sender as Image;
if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
{
if (AddNewInstruction != null)
AddNewInstruction(dragSource.Tag.ToString());
}
}
}
}
8 changes: 4 additions & 4 deletions UI/Controls/ProgramList.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
RenderOptions.BitmapScalingMode="HighQuality">

<UserControl.CommandBindings>
<CommandBinding Command="ApplicationCommands.Cut" CanExecute="ListCutCopyCanExecute" Executed="ListCutExecuted" />
<CommandBinding Command="ApplicationCommands.Copy" CanExecute="ListCutCopyCanExecute" Executed="ListCopyExecuted" />
<CommandBinding Command="ApplicationCommands.Paste" CanExecute="ListPasteCanExecute" Executed="ListPasteExecuted" />
<CommandBinding Command="ApplicationCommands.Cut" CanExecute="CutCopyCanExecute" Executed="CutExecuted" />
<CommandBinding Command="ApplicationCommands.Copy" CanExecute="CutCopyCanExecute" Executed="CopyExecuted" />
<CommandBinding Command="ApplicationCommands.Paste" CanExecute="PasteCanExecute" Executed="PasteExecuted" />
<CommandBinding Command="ApplicationCommands.Delete" CanExecute="DeleteCanExecute" Executed="DeleteExecuted" />
</UserControl.CommandBindings>

Expand All @@ -26,7 +26,7 @@
</UserControl.Resources>

<Grid>
<ListView x:Name="listViewProgram" SelectionMode="Single" Background="{x:Null}">
<ListView x:Name="listViewProgram" SelectionMode="Single" Background="{x:Null}" SelectionChanged="listViewProgram_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
Expand Down
138 changes: 91 additions & 47 deletions UI/Controls/ProgramList.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public string Description
/// </summary>
public partial class ProgramList : UserControl
{

This comment has been minimized.

Copy link
@jiasli

jiasli Apr 28, 2015

Owner

@cherishchen8 Use event to define callback methods.

public delegate void ProgramListEventHandler();
public delegate void SelectedInstructionChangedEventHandler(string insString);

public event ProgramListEventHandler ProgramChanged;
public event ProgramListEventHandler ClipboardChanged;
public event SelectedInstructionChangedEventHandler SelectedInstructionChanged;

#region data

private ListViewDragDropManager<DisplayItem> dragMgr;
Expand Down Expand Up @@ -181,6 +189,7 @@ void listView_Drop(object sender, DragEventArgs e)
int item = pvm[oldIndex];
pvm.Remove(item);
pvm.Insert(newIndex, item);
listViewProgram.SelectedIndex = newIndex;

UpdateContent();
e.Effects = DragDropEffects.Move;
Expand All @@ -193,37 +202,60 @@ void listView_Drop(object sender, DragEventArgs e)

string op = (string)e.Data.GetData(DataFormats.StringFormat);

Instruction newIns = Instruction.CreatDefaultFromOpcode(op);
InsertNewInstruction(newIndex, op);
}
}

/// <summary>
/// Insert a new instruction at specified index and update the content.
/// </summary>
/// <param name="index">The zero-base index at which the new instruction should be inserted.</param>
/// <param name="opCode">The opcode of the instruction.</param>
public void InsertNewInstruction(int index, string opCode)
{
Instruction newIns = Instruction.CreatDefaultFromOpcode(opCode);

if (newIns != null)
if (newIns != null)
{
if (opCode == Instruction.IF || opCode == Instruction.LOOP)
{
if (op == Instruction.IF || op == Instruction.LOOP)
{
// Add HLProgram for IF and LOOP.
pvm.InsertSubProgram(newIndex, HLProgram.GetDefaultIfLoopBlock(newIns));
}
else
{
// Add single Instruction.
pvm.InsertInstruction(newIndex, newIns);
}
// Add HLProgram for IF and LOOP.
pvm.InsertSubProgram(index, HLProgram.GetDefaultIfLoopBlock(newIns));
}
else
{
// Add single Instruction.
pvm.InsertInstruction(index, newIns);
}

UpdateContent();
listViewProgram.SelectedIndex = newIndex;
listViewProgram.SelectedIndex = index;

// To ensure that the selected item is visible.
listViewProgram.ScrollIntoView(listViewProgram.SelectedItem);

if (Properties.Settings.Default.PopupWindowForNewIns)
ShowParamWindow(newIndex);
ShowParamWindow(index);
}
}

/// <summary>
/// Add a new instruction to the end of the list.
/// </summary>
/// <param name="opCode">The opcode of the instruction.</param>
public void AddNewInstruction(string opCode)
{
int index = pvm.Count;
InsertNewInstruction(index, opCode);
}

#endregion // NewlistView_Drop

#endregion // event handler

#region private operations

/// <summary>
/// Update content in ProgramList accordint to pvm
/// Update content in ProgramList according to pvm
/// </summary>
private void UpdateContent()
{
Expand All @@ -234,7 +266,7 @@ private void UpdateContent()

for (int i = 0; i < pvm.Count; i++)
{
Instruction ins = pvm.GetInstruction(pvm[i]);
Instruction ins = pvm.GetInstruction(i);
//Image icon = GetImageFromInstruction(ins);
//DisplayItem itemToDisplay = new DisplayItem(icon, TextDescriber.GetTextDescription(ins));
Uri path = GetPathFromInstruction(ins);
Expand All @@ -245,6 +277,12 @@ private void UpdateContent()
}
}
listViewProgram.SelectedIndex = selectedIndex;

// Fire ProgramChanged event.
if (ProgramChanged != null)
{
ProgramChanged();
}
}

/// <summary>
Expand Down Expand Up @@ -287,11 +325,11 @@ private Uri GetPathFromInstruction(Instruction ins)
private void ShowParamWindow(int index)
{
// The Ins under modification
Instruction selectedIns = pvm.GetInstruction(pvm[index]);
Instruction selectedIns = pvm.GetInstruction(index);

if (selectedIns.opcode == Instruction.IF || selectedIns.opcode == Instruction.LOOP)
{
HLProgram subProgram = pvm.GetSubProgram(pvm[index]);
HLProgram subProgram = pvm.GetSubProgram(index);

// invoke the dialog
HLProgram result = DialogInvoker.ShowDialog(subProgram, Window.GetWindow(this));
Expand All @@ -309,48 +347,51 @@ private void ShowParamWindow(int index)

#endregion // private operations

#region file operations
#region Edit

private void ListCopyExecuted(object sender, ExecutedRoutedEventArgs e)
private void DeleteCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (listViewProgram.SelectedIndex != -1)
{
e.CanExecute = true;
}
}

private void DeleteExecuted(object sender, ExecutedRoutedEventArgs e)
{
RemoveSelection();
}

private void CopyExecuted(object sender, ExecutedRoutedEventArgs e)
{
CopySelection();
}

private void ListCutExecuted(object sender, ExecutedRoutedEventArgs e)
private void CutExecuted(object sender, ExecutedRoutedEventArgs e)
{
CopySelection();
RemoveSelection();
}

private void ListPasteExecuted(object sender, ExecutedRoutedEventArgs e)
private void PasteExecuted(object sender, ExecutedRoutedEventArgs e)
{
int newIndex = listViewProgram.SelectedIndex;
if (newIndex < 0)
newIndex = pvm.Count;

int pvmvalue = Int32.Parse(Clipboard.GetText());
Instruction ins = pvm.GetInstruction(pvmvalue);

if (ins.opcode == Instruction.IF || ins.opcode == Instruction.LOOP)
{
pvm.InsertSubProgram(newIndex, pvm.GetSubProgram(pvmvalue));
}
else
{
String insstring = ins.ToString();
Instruction newins = new Instruction(insstring);
pvm.InsertInstruction(newIndex, newins);
}
string subProgramString = Clipboard.GetText();
HLProgram subProgram = new HLProgram(subProgramString);
pvm.InsertSubProgram(newIndex, subProgram);

UpdateContent();
}

private void ListCutCopyCanExecute(object sender, CanExecuteRoutedEventArgs e)
private void CutCopyCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = listViewProgram.SelectedIndex >= 0;
}

private void ListPasteCanExecute(object sender, CanExecuteRoutedEventArgs e)
private void PasteCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = Clipboard.ContainsText();
}
Expand All @@ -362,7 +403,12 @@ private void CopySelection()
return;

Clipboard.Clear();
Clipboard.SetText(pvm[index].ToString());
Clipboard.SetText(pvm.GetSubProgram(index).ToString());

if (ClipboardChanged != null)
{
ClipboardChanged();
}
}

private void RemoveSelection()
Expand All @@ -372,25 +418,23 @@ private void RemoveSelection()
return;

// Just remove the pointer. We don't care the source in HLProgram.
pvm.Remove(pvm[index]);
pvm.Remove(index);

UpdateContent();
}

#endregion // file operations

private void DeleteCanExecute(object sender, CanExecuteRoutedEventArgs e)
private void listViewProgram_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (listViewProgram.SelectedIndex != -1)
if (SelectedInstructionChanged != null)
{
e.CanExecute = true;
if (listViewProgram.SelectedIndex != -1)
SelectedInstructionChanged(pvm.GetSubProgram(listViewProgram.SelectedIndex).ToString());
}
}

private void DeleteExecuted(object sender, ExecutedRoutedEventArgs e)
{
RemoveSelection();
}


}
}
8 changes: 5 additions & 3 deletions UI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@
<ColumnDefinition x:Name="columnDefinitionDebug" Width="250"/>
</Grid.ColumnDefinitions>

<Controls:InstructionPanel Grid.Column="0" />
<Controls:InstructionPanel Grid.Column="0" AddNewInstruction="InstructionPanel_AddNewInstruction" />

<Image Grid.Column="1" Source="/iRobotGUI;component/pic/iRobotBG.jpg" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="150" Height="150" />
<Controls:ProgramList x:Name="programList" Grid.Column="1" Margin="5" />
<Controls:ProgramList x:Name="programList" Grid.Column="1" Margin="5" ProgramChanged="programList_ProgramChanged" ClipboardChanged="programList_ClipboardChanged" SelectedInstructionChanged="programList_SelectedInstructionChanged" />

<Grid Grid.Column="2">
<Grid.RowDefinitions>
Expand All @@ -89,7 +89,9 @@
<Button Content="Load Example Code" HorizontalAlignment="Left" VerticalAlignment="Top" Click="buttonLoadExampleCode_Click" Grid.Row="1" Margin="10,0,0,0" />
<Button x:Name="buttonRefreshSource" Content="-> Refresh Source" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1" Margin="10,27,0,0" Click="buttonRefreshSource_Click" />
<Button x:Name="buttonLoadIntoGraph" Content="&lt;- Load into Graph" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1" Margin="116,27,0,0" Click="buttonLoadIntoGraph_Click" />
<TextBox x:Name="textBoxSource" TextWrapping="Wrap" Text="Source Code" Grid.Row="2" AcceptsReturn="True" Margin="5,0,5,5" Background="{x:Null}"/>
<TextBox x:Name="textBoxSource" TextWrapping="Wrap" Text="Source Code" Grid.Row="2" AcceptsReturn="True" Margin="5,0,5,155" />
<TextBox x:Name="textBoxSelectedInstruction" TextWrapping="Wrap" Text="Selected Instruction" Grid.Row="2" AcceptsReturn="True" Margin="5,212.04,134,5.08" />
<TextBox x:Name="textBoxClipboard" TextWrapping="Wrap" Text="Clipboard" Grid.Row="2" AcceptsReturn="True" Margin="121,212.04,5,5.08" />
</Grid>
</Grid>

Expand Down
Loading

0 comments on commit 782fb21

Please sign in to comment.