Skip to content

Commit

Permalink
Display turtle graphics in a view window.
Browse files Browse the repository at this point in the history
Remove old ruler version of the display.
Rename AddComposite to LiveCodingAnalyst.
  • Loading branch information
donkirkby committed Sep 11, 2012
1 parent cd4b762 commit c502993
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 451 deletions.
2 changes: 2 additions & 0 deletions license.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Turtle icon by Hylke (www.bomahy.nl) licensed as GPL v2.

Eclipse Public License - v 1.0

THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC
Expand Down
22 changes: 8 additions & 14 deletions plugin/PySrc/code_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,23 +291,17 @@ def trace_code(self, source):
type=int,
default=600,
help='height of the canvas in pixels')
parser.add_argument('-c',
'--canvas',
action='store_true',
help='Is the canvas on? If not, do a standard trace.')
parser.add_argument('-t',
'--turtle',
action='store_true',
help='Is the turtle on? If not, do a standard trace.')

args = parser.parse_args()
code = sys.stdin.read()
canvas = Canvas(args.width, args.height)
turtle = MockTurtle(canvas=canvas)
tracer = CodeTracer(turtle)
if args.turtle:
print tracer.trace_turtle(code)
elif args.canvas:
print tracer.trace_canvas(code)
else:
print tracer.trace_code(code)
code_report = tracer.trace_code(code)
turtle_report = tracer.turtle.report
if turtle_report:
print 'start_canvas'
print '\n'.join(turtle_report)
print 'end_canvas'
print '.'
print code_report
Binary file added plugin/icons/turtle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions plugin/license.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Turtle icon by Hylke (www.bomahy.nl) licensed as GPL v2.

Eclipse Public License - v 1.0

THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC
Expand Down
29 changes: 11 additions & 18 deletions plugin/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.ui.workbench.texteditor.rulerColumns">
<column
class="live_py.LiveCodingResultsRuler"
enabled="true"
global="true"
id="live-py.live-results"
includeInMenu="true"
name="Live Coding Results">
<placement
gravity="1.0">
</placement>
<targetClass
class="org.python.pydev.editor.PyEdit">
</targetClass>
</column>
</extension>
<extension
point="org.python.pydev.pydev_pyedit_listener">
<pyedit_listener_participant
class="live_py.AddComposite">
class="live_py.LiveCodingAnalyst">
</pyedit_listener_participant>
</extension>
<extension
point="org.eclipse.ui.views">
<view
class="live_py.LiveCanvasView"
icon="icons/turtle.png"
id="com.github.donkirkby.live-canvas"
name="Live Coding Canvas"
restorable="true">
</view>
</extension>

</plugin>
145 changes: 145 additions & 0 deletions plugin/src/live_py/LiveCanvasView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package live_py;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;
import org.python.pydev.editor.PyEdit;

public class LiveCanvasView extends ViewPart {
private LiveCodingAnalyst analyst;
private Canvas canvas;

public LiveCanvasView() {
super();
}

public void setFocus() {
}

public void createPartControl(Composite parent) {
canvas = new Canvas(parent, SWT.NONE);

canvas.addPaintListener(new PaintListener() {

@Override
public void paintControl(PaintEvent e) {
drawResult(e.gc);
}
});

IViewSite site = getViewSite();
site.getPage().addPartListener(new IPartListener() {

@Override
public void partOpened(IWorkbenchPart part) {
}

@Override
public void partDeactivated(IWorkbenchPart part) {
}

@Override
public void partClosed(IWorkbenchPart part) {
}

@Override
public void partBroughtToTop(IWorkbenchPart part) {
}

@Override
public void partActivated(IWorkbenchPart part) {
if (part instanceof PyEdit)
{
PyEdit editor = (PyEdit)part;
LiveCodingAnalyst newAnalyst =
LiveCodingAnalyst.getAnalyst(editor);
if (newAnalyst != analyst && analyst != null) {
analyst.setCanvasView(null);
}
analyst = newAnalyst;
analyst.setCanvasView(LiveCanvasView.this);
}
}
});
}

public void redraw() {
if (canvas != null) {
canvas.redraw();
}
}

private void drawResult(GC gc) {
// Clear the drawing
gc.fillRectangle(gc.getDevice().getBounds());

if (analyst == null) {
return;
}
ArrayList<CanvasCommand> canvasCommands = analyst.getCanvasCommands();
// Execute the drawing commands
for (CanvasCommand command : canvasCommands) {
String method = command.getName();
if (method.equals("create_line")) {
gc.drawLine(
command.getCoordinate(0),
command.getCoordinate(1),
command.getCoordinate(2),
command.getCoordinate(3));
}
else if (method.equals("create_rectangle")) {
gc.drawRectangle(
command.getCoordinate(0),
command.getCoordinate(1),
command.getCoordinate(2) - command.getCoordinate(0),
command.getCoordinate(3) - command.getCoordinate(1));
}
else if (method.equals("create_text")) {
Font oldFont = gc.getFont();
gc.setFont(command.getFontOption(gc.getDevice(), "font"));
String text = command.getOption("text");
Point size = gc.textExtent(text);
int x = command.getCoordinate(0);
int y = command.getCoordinate(1);
String anchor = command.getOption("anchor");
anchor = anchor == null ? "center" : anchor;
if (anchor.startsWith("s")) {
y -= size.y;
}
else if (anchor.startsWith("n")) {
// defaults to top
}
else {
y -= size.y/2;
}
if (anchor.endsWith("e")) {
x -= size.x;
}
else if (anchor.endsWith("w")) {
// defaults to left side
}
else {
x -= size.x/2;
}
gc.drawText(
text,
x,
y,
SWT.DRAW_TRANSPARENT);
gc.setFont(oldFont);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.ListResourceBundle;
import java.util.WeakHashMap;

import org.eclipse.compare.Splitter;
import org.eclipse.core.runtime.IProgressMonitor;
Expand Down Expand Up @@ -49,17 +50,29 @@
* @author Don Kirkby
*
*/
public class AddComposite implements IPyEditListener, IPyEditListener4 {
public class LiveCodingAnalyst implements IPyEditListener, IPyEditListener4 {
/**
* Making it true will print some debug info to stdout.
*/
private final static boolean DEBUG = true;
private final static boolean DEBUG = false;
private static WeakHashMap<PyEdit, LiveCodingAnalyst> analystMap =
new WeakHashMap<PyEdit, LiveCodingAnalyst>();

private ISourceViewer mainViewer;
private IDocument mainDocument;
private PyEdit pyEdit;
private Document displayDocument;
private SourceViewer displayViewer;
private LiveCanvasView canvasView;
private File scriptPath;
private ArrayList<CanvasCommand> canvasCommands =
new ArrayList<CanvasCommand>();

public static LiveCodingAnalyst getAnalyst(PyEdit editor)
{
// TODO: add locking
return analystMap.get(editor);
}

/**
* Wire up a new editor so that it will be displayed the way we want.
Expand Down Expand Up @@ -136,6 +149,11 @@ public void viewportChanged(int verticalOffset) {
*/
@Override
public Object call(ISourceViewer newViewer) {
if (newViewer instanceof PySourceViewer)
{
pyEdit = ((PySourceViewer)newViewer).getEdit();
analystMap.put(pyEdit, LiveCodingAnalyst.this);
}
mainViewer = newViewer;
displayViewer.getTextWidget().setFont(
mainViewer.getTextWidget().getFont());
Expand Down Expand Up @@ -243,6 +261,10 @@ private void analyseDocument(IDocument document) {
}

loadResults(reader);
LiveCanvasView view = canvasView;
if (view != null) {
view.redraw();
}
if (DEBUG) {
String line;
do
Expand Down Expand Up @@ -322,19 +344,46 @@ private void loadResults(BufferedReader reader) throws IOException {
String line;
StringWriter writer = new StringWriter();
PrintWriter printer = new PrintWriter(writer);
boolean isStarted = false;
do {
line = reader.readLine();
if(DEBUG){
System.out.println("load results line: "+line);
}

if (line != null) {
printer.println(line);
if (isStarted) {
printer.println(line);
}
else {
if (line.equals("start_canvas")) {
loadCanvasCommands(reader);
}
else {
printer.println();
}
isStarted = true;
}
}
} while (line != null);
displayDocument.set(writer.toString());
}

private void loadCanvasCommands(BufferedReader reader) {
canvasCommands.clear();
CanvasReader canvasReader = new CanvasReader(reader);
CanvasCommand command;
boolean isDone;
do {
command = canvasReader.read();

isDone = command == null || command.getName().equals("end_canvas");
if ( ! isDone) {
canvasCommands.add(command);
}
} while ( ! isDone);
}

private void checkScriptPath() {
if (scriptPath != null)
{
Expand All @@ -356,4 +405,16 @@ private void checkScriptPath() {
System.out.println("Script path: "+scriptPath);
}
}

public LiveCanvasView getCanvasView() {
return canvasView;
}

public void setCanvasView(LiveCanvasView canvasView) {
this.canvasView = canvasView;
}

public ArrayList<CanvasCommand> getCanvasCommands() {
return canvasCommands;
}
}
Loading

0 comments on commit c502993

Please sign in to comment.