| 1 |
module user.region_shell;// image attached: region.gif |
|---|
| 2 |
import dwt.DWT; |
|---|
| 3 |
|
|---|
| 4 |
import dwt.widgets.Display; |
|---|
| 5 |
import dwt.widgets.Shell; |
|---|
| 6 |
import dwt.widgets.Button; |
|---|
| 7 |
import dwt.widgets.Listener; |
|---|
| 8 |
import dwt.widgets.Event; |
|---|
| 9 |
|
|---|
| 10 |
import dwt.layout.GridLayout; |
|---|
| 11 |
|
|---|
| 12 |
import dwt.dwthelper.ByteArrayInputStream; |
|---|
| 13 |
|
|---|
| 14 |
import dwt.graphics.Image; |
|---|
| 15 |
import dwt.graphics.ImageData; |
|---|
| 16 |
import dwt.graphics.Region; |
|---|
| 17 |
import dwt.graphics.Point; |
|---|
| 18 |
import dwt.graphics.Rectangle; |
|---|
| 19 |
|
|---|
| 20 |
import tango.io.Stdout; |
|---|
| 21 |
|
|---|
| 22 |
class RegionShell: Shell |
|---|
| 23 |
{ |
|---|
| 24 |
this(Display display, int style, Image image, bool moveable = true) |
|---|
| 25 |
{ |
|---|
| 26 |
super(display, style); |
|---|
| 27 |
|
|---|
| 28 |
setShellRegion(image, moveable); |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
this(Display display, Image image, bool moveable = true) |
|---|
| 32 |
{ |
|---|
| 33 |
this(display, DWT.NO_TRIM, image, moveable); |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
private void setShellRegion(Image image, bool moveable) |
|---|
| 37 |
{ |
|---|
| 38 |
Region region = new Region(); |
|---|
| 39 |
ImageData imageData = image.getImageData(); |
|---|
| 40 |
|
|---|
| 41 |
if (imageData.alphaData != null) |
|---|
| 42 |
{ |
|---|
| 43 |
Rectangle pixel = new Rectangle(0, 0, 1, 1); |
|---|
| 44 |
|
|---|
| 45 |
for (int y = 0; y < imageData.height; y++) |
|---|
| 46 |
{ |
|---|
| 47 |
for (int x = 0; x < imageData.width; x++) |
|---|
| 48 |
{ |
|---|
| 49 |
if (imageData.getAlpha(x, y) == 255) |
|---|
| 50 |
{ |
|---|
| 51 |
pixel.x = imageData.x + x; |
|---|
| 52 |
pixel.y = imageData.y + y; |
|---|
| 53 |
|
|---|
| 54 |
region.add(pixel); |
|---|
| 55 |
} |
|---|
| 56 |
} |
|---|
| 57 |
} |
|---|
| 58 |
} |
|---|
| 59 |
else |
|---|
| 60 |
{ |
|---|
| 61 |
ImageData mask = imageData.getTransparencyMask(); |
|---|
| 62 |
Rectangle pixel = new Rectangle(0, 0, 1, 1); |
|---|
| 63 |
for (int y = 0; y < mask.height; y++) |
|---|
| 64 |
{ |
|---|
| 65 |
for (int x = 0; x < mask.width; x++) |
|---|
| 66 |
{ |
|---|
| 67 |
if (mask.getPixel(x, y) != 0) |
|---|
| 68 |
{ |
|---|
| 69 |
pixel.x = imageData.x + x; |
|---|
| 70 |
pixel.y = imageData.y + y; |
|---|
| 71 |
|
|---|
| 72 |
region.add(pixel); |
|---|
| 73 |
} |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
this.setRegion(region); |
|---|
| 79 |
this.setSize(imageData.x + imageData.width, imageData.y + imageData.height); |
|---|
| 80 |
|
|---|
| 81 |
if (moveable) |
|---|
| 82 |
{ |
|---|
| 83 |
setMoveable(); |
|---|
| 84 |
} |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
private Shell getCurrent() |
|---|
| 88 |
{ |
|---|
| 89 |
return this; |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
private void setMoveable() |
|---|
| 93 |
{ |
|---|
| 94 |
Listener l = new class Listener { |
|---|
| 95 |
Point origin; |
|---|
| 96 |
|
|---|
| 97 |
public void handleEvent(Event e) { |
|---|
| 98 |
switch (e.type) { |
|---|
| 99 |
case DWT.MouseDown: |
|---|
| 100 |
origin = new Point(e.x, e.y); |
|---|
| 101 |
break; |
|---|
| 102 |
case DWT.MouseUp: |
|---|
| 103 |
origin = null; |
|---|
| 104 |
break; |
|---|
| 105 |
case DWT.MouseMove: |
|---|
| 106 |
if (origin !is null) { |
|---|
| 107 |
Point p = display.map(getCurrent(), null, e.x, e.y); |
|---|
| 108 |
getCurrent.setLocation(p.x - origin.x, p.y - origin.y); |
|---|
| 109 |
} |
|---|
| 110 |
break; |
|---|
| 111 |
} |
|---|
| 112 |
} |
|---|
| 113 |
}; |
|---|
| 114 |
|
|---|
| 115 |
this.addListener(DWT.MouseDown, l); |
|---|
| 116 |
this.addListener(DWT.MouseUp, l); |
|---|
| 117 |
this.addListener(DWT.MouseMove, l); |
|---|
| 118 |
} |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
void main() |
|---|
| 122 |
{ |
|---|
| 123 |
Display display = new Display(); |
|---|
| 124 |
Image image = new Image(display, new ImageData(new ByteArrayInputStream(cast(byte[]) import("region_shell.gif")))); |
|---|
| 125 |
RegionShell shell = new RegionShell(display, image); |
|---|
| 126 |
|
|---|
| 127 |
Button closeBtn = new Button(shell, DWT.PUSH); |
|---|
| 128 |
closeBtn.setText("Close"); |
|---|
| 129 |
closeBtn.addListener(DWT.Selection, new class Listener { |
|---|
| 130 |
public void handleEvent(Event e) { |
|---|
| 131 |
shell.close(); |
|---|
| 132 |
} |
|---|
| 133 |
}); |
|---|
| 134 |
|
|---|
| 135 |
shell.setLayout(new GridLayout()); |
|---|
| 136 |
shell.setBackgroundImage(image); |
|---|
| 137 |
shell.open(); |
|---|
| 138 |
|
|---|
| 139 |
while (!shell.isDisposed()) |
|---|
| 140 |
{ |
|---|
| 141 |
if (!display.readAndDispatch()) |
|---|
| 142 |
{ |
|---|
| 143 |
display.sleep(); |
|---|
| 144 |
} |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
image.dispose(); |
|---|
| 148 |
display.dispose(); |
|---|
| 149 |
} |
|---|