X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fxwt%2FDoubleBuffer.java;h=9e9d92318acf3448a6a223a31ad0186021c2342a;hp=f420875dfce0def8a76759d1a716a721fa95e74e;hb=ad683cf8054380f70abd63ff6018db29c3137bde;hpb=3ca9b272597fb7e8e536d176e313145ae155d1a7 diff --git a/src/org/xwt/DoubleBuffer.java b/src/org/xwt/DoubleBuffer.java index f420875..9e9d923 100644 --- a/src/org/xwt/DoubleBuffer.java +++ b/src/org/xwt/DoubleBuffer.java @@ -9,10 +9,10 @@ package org.xwt; *

* *

- * Implementations of the Platform class should return objects - * supporting this interface from the _createDoubleBuffer() - * method. These implementations may choose to use off-screen video - * ram for this purpose (for example, a Pixmap on X11). + * Implementations of the Platform class should return subclasses of + * DoubleBuffer from the _createDoubleBuffer() method. These + * implementations may choose to use off-screen video ram for this + * purpose (for example, a Pixmap on X11). *

* *

@@ -21,23 +21,206 @@ package org.xwt; * represents regions internally as x,y,w,h. *

*/ + public abstract class DoubleBuffer { - /** Draw the region of source within (sx1, sy1, sx2, sy2) onto the region of this DoubleBuffer within (dx1, dy1, dx2, dy2), scaling as needed. */ - public abstract void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2); + // Methods to Be Overridden////////////////////////////////////////////////////////////////////////////// - /** Draw source onto this DoubleBuffer at (x,y) */ - public abstract void drawPicture(Picture source, int x, int y); + /** Stretch the source image to (dx1, dy1, dx2, dy2), but clip it to (cx1, cy1, cx2, cy2) */ + protected abstract void _drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int cx1, int cy1, int cx2, int cy2); /** Draw text in font and color on this DoubleBuffer, with the upper left corner of the text at (x, y) */ - public abstract void drawString(String font, String text, int x, int y, int color); + protected abstract void _drawString(String font, String text, int x, int y, int color); + + /** Draw a point at (x, y) */ + protected void _drawPoint(int x, int y, int color) { fillRect(x, y, x + 1, y + 1, color); } + + /** Draw a line from (x1, y1) to (x2, y2) that is width pixels wide */ + protected void _drawLine(int x1, int y1, int x2, int y2, int width, int color) { + if (Math.abs(x1 - x2) < 1) { + for(int y=Math.min(y1, y2); y x2) { + int _x1 = x1; x1 = x2; x2 = _x1; + int _y1 = y1; y1 = y2; y2 = _y1; + } + double slope = (double)(y2 - y1) / (double)(x2 - x1); + int last_y = y1; + for(int x=x1; x<=x2; x++) { + int new_y = (int)(slope * (double)(x - x1)) + y1; + if (slope >= 0) { + for(int y=last_y + 1; y < new_y; y++) drawPoint(x, y, color); + } else { + for(int y=last_y - 1; y > new_y; y--) drawPoint(x, y, color); + } + if (x != x2) drawPoint(x, new_y, color); + last_y = new_y; + } + } /** Fill the region (x1, y1, x2, y2) with color (AARRGGBB format); the alpha channel component is ignored */ - public abstract void fillRect(int x1, int y1, int x2, int y2, int color); + protected abstract void _fillRect(int x1, int y1, int x2, int y2, int color); + + /** Fill in the trapezoid defined by (x1, y1), (x2, y1), (x3, y2), (x4, y2); leftSlope and rightSlope are provided for convenience */ + protected void _fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, double leftSlope, double rightSlope, int color) { + for(int y=y1; y _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; } + fillRect(_x1, _y1, _x2, _y2, color); + } + } + + + // Internal Stuff ////////////////////////////////////////////////////////////////////////////// + + protected int width, height; + private int clipx = 0, clipy = 0, clipw, cliph; + private DoubleBuffer() { } + protected DoubleBuffer(int width, int height) { this.width = width; this.height = height; clipw = width; cliph = height; } + protected SVG.Affine ctm = null; + + + // Final methods ////////////////////////////////////////////////////////////////////////////// + + public final int getHeight() { return width; } + public final int getWidth() { return height; } + public final void setTransform(SVG.Affine ctm) { this.ctm = ctm; } + public final void setClip(int x, int y, int x2, int y2) { clipx = x; clipy = y; clipw = x2 - x; cliph = y2 - y; } + public final void drawPicture(Picture p, int x, int y) { drawPicture(p, x, y, x + p.getWidth(), y + p.getHeight(), 0, 0, p.getWidth(), p.getHeight()); } + + public final void drawPoint(int x, int y, int color) { + if (x > clipx && x < clipx + clipw && y > clipy && y < clipy + cliph) + _drawPoint(x, y, color); + } + + public final void drawLine(int x1, int y1, int x2, int y2, int width, int color) { + + if (x1 == x2 || y1 == y2) return; + + // FIXME: don't compute slope if not needed + double slope = (y2 - y1) / (x2 - x1); + if (x1 < clipx) { y1 = (int)((clipx - x1) * slope + y1); x1 = clipx; } + if (y1 < clipy) { x1 = (int)((clipy - y1) / slope + x1); y1 = clipy; } + if (x2 > clipx + clipw) { x2 = clipx + clipw; y2 = (int)((x2 - x1) / slope + y1); } + if (y2 > clipy + cliph) { y2 = clipy + cliph; x2 = (int)((y2 - y1) * slope + y1); } + + _drawLine(x1, y1, x2, y2, width, color); + } + + /** Stretch the picture to fill (dx1, dy1, dx2, dy2) and blit it, clipping to (cx1, cy1, cx2, cy2) */ + public final void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int cx1, int cy1, int cx2, int cy2) { + + if (org.xwt.util.Log.assertionsEnabled) { + if (dx1 >= dx2) org.xwt.util.Log.log(DoubleBuffer.class, "drawPicture(): dx1 >= dx2: dx1=" + dx1 + " dx2=" + dx2, true); + if (dy1 >= dy2) org.xwt.util.Log.log(DoubleBuffer.class, "drawPicture(): dy1 >= dy2: dy1=" + dy1 + " dy2=" + dy2, true); + if (cx1 >= cx2) org.xwt.util.Log.log(DoubleBuffer.class, "drawPicture(): cx1 >= cx2: cx1=" + cx1 + " cx2=" + cx2, true); + if (cy1 >= cy2) org.xwt.util.Log.log(DoubleBuffer.class, "drawPicture(): cy1 >= cy2: cy1=" + cy1 + " cy2=" + cy2, true); + } + + // shrink clipping rectangle to be no bigger than drawing rectangle + if (cx1 < dx1) cx1 = dx1; + if (cy1 < dy1) cy1 = dy1; + if (cx2 > dx2) cx2 = dx2; + if (cy2 > dy2) cy2 = dy2; + + // intersect the two clipping regions + if (cx2 < clipx) return; + if (cy2 < clipy) return; + if (cx1 > clipx + clipw) return; + if (cy1 > clipy + cliph) return; + if (cx1 < clipx) cx1 = clipx; + if (cy1 < clipy) cy1 = clipy; + if (cx2 > clipx + clipw) cx2 = clipx + clipw; + if (cy2 > clipy + cliph) cy2 = clipy + cliph; + + if (ctm == null) { + _drawPicture(source, dx1, dy1, dx2, dy2, cx1, cy1, cx2, cy2); + } else { + _drawPicture(source, + (int)ctm.multiply_px(dx1, dy1), (int)ctm.multiply_py(dx1, dy1), + (int)ctm.multiply_px(dx2, dy2), (int)ctm.multiply_py(dx2, dy2), + (int)ctm.multiply_px(cx1, cy1), (int)ctm.multiply_py(cx1, cy1), + (int)ctm.multiply_px(cx2, cy2), (int)ctm.multiply_py(cx2, cy2)); + } + } + + public final void drawString(String font, String text, int x, int y, int color) { + // FIXME: clipping + if (ctm == null) { + _drawString(font, text, x, y, color); + } else { + _drawString(font, text, (int)ctm.multiply_px(x, y), (int)ctm.multiply_py(x, y), color); + } + } + + public final void fillRect(int x1, int y1, int x2, int y2, int color) { + + x1 = Math.max(Math.min(clipx + clipw, x1), clipx); + x2 = Math.max(Math.min(clipx + clipw, x2), clipx); + y1 = Math.max(Math.min(clipy + cliph, y1), clipy); + y2 = Math.max(Math.min(clipy + cliph, y2), clipy); + + if (ctm == null) { + _fillRect(x1, y1, x2, y2, color); + } else { + _fillRect((int)ctm.multiply_px(x1, y1), (int)ctm.multiply_py(x1, y1), + (int)ctm.multiply_px(x2, y2), (int)ctm.multiply_py(x2, y2), + color); + } + } + + public final void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color) { + fillTrapezoid(x1, x2, y1, x3, x4, y2, (double)(x3 - x1) / (double)(y2 - y1), (double)(x4 - x2) / (double)(y2 - y1), color); + } + + private final void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, double slope1, double slope2, int color) { + + // invariant: x1 <= x2, x3 <= x4, y1 <= y2 + if (y1 == y2) return; + if (org.xwt.util.Log.assertionsEnabled) { + if (x1 > x2) org.xwt.util.Log.log(DoubleBuffer.class, "fillTrapezoid(): x1 > x2: x1=" + x1 + " x2=" + x2, true); + if (x3 > x4) org.xwt.util.Log.log(DoubleBuffer.class, "fillTrapezoid(): x3 > x4: x3=" + x3 + " x4=" + x4, true); + if (y1 >= y2) org.xwt.util.Log.log(DoubleBuffer.class, "fillTrapezoid(): y1 >= y2: y1=" + y1 + " y2=" + y2, true); + } + + // clip top and bottom edges + if (y1 > clipy + cliph) return; + if (y2 < clipy) return; + if (y1 < clipy) { x1 = (int)(y1 + slope1 * clipy); x2 = (int)(y1 + slope2 * clipy); y1 = clipy; } + if (y2 > clipy + cliph) { y2 = clipy + cliph; x3 = (int)(y1 + slope1 * y2); x4 = (int)(slope2 * y2); } + + if (x1 < clipx && x3 < clipx) { + x1 = x3 = clipx; + } else if (!(x1 >= clipx && x3 >= clipx)) { + int y = (int)((clipx - x1) / slope1 + y1); + int x = (int)(slope2 * (y - y2) + x2); + // we have to recurse here because we don't know which subtrapezoid might get split on the other side + fillTrapezoid(x1, x2, y1, clipx, x, y, slope1, slope2, color); + fillTrapezoid(clipx, x, y, x3, x4, y2, slope1, slope2, color); + return; + } + + if (x2 >= clipx + clipw && x4 >= clipx + clipw) { + x2 = x4 = clipx + clipw; + } else if (!(x2 < clipx + clipw && x4 < clipx + clipw)) { + int y = (int)((clipx + clipw - x3) / slope2 + y1); + int x = (int)(slope1 * (y - y1) + x1); + _fillTrapezoid(x1, x2, y1, x, clipx + clipw, y, slope1, slope2, color); + _fillTrapezoid(x, clipx + clipw, y, x3, x4, y2, slope1, slope2, color); + return; + } - /** Sets the clip region for this DoubleBuffer to (x,y,x2,y2) */ - public abstract void setClip(int x, int y, int x2, int y2); + _fillTrapezoid(x1, x2, y1, x3, x4, y2, slope1, slope2, color); + } - public abstract int getHeight(); - public abstract int getWidth(); }