Skip to content

Commit 280e1fe

Browse files
vogellaCopilot
andcommitted
Migrate 4 effects to Graphics Example: Dancing, BumpMapping, FlatText, Lens
Migrate the next 4 old-school effects from SWT-OldSchoolEffect to the GraphicsExample framework: - DancingTab: Isometric dancing shape with sine-wave grid deformations - BumpMappingTab: Bump mapping with moving light source (uses bump.png) - FlatTextTab: Flat text perspective distortion (uses TEXFLAT2.png) - LensTab: Spherical lens distortion bouncing across image (uses tuxblackbg.png) Each effect extends AnimatedGraphicsTab with proper resource disposal, localized strings in examples_graphics.properties, and registration in GraphicsExample.createTabs(). See #3189 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 188aed6 commit 280e1fe

9 files changed

Lines changed: 616 additions & 0 deletions

File tree

examples/org.eclipse.swt.examples/src/examples_graphics.properties

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,15 @@ TwisterDescription=This is a miscellaneous demonstration of an animated twister
226226

227227
Wave=Wave
228228
WaveDescription=This is a miscellaneous demonstration of an animated wave effect with multiple motion patterns cycling through four different wave algorithms.
229+
230+
Dancing=Dancing
231+
DancingDescription=This is a miscellaneous demonstration of an animated isometric dancing shape with sine-wave deformations across a grid of polygons.
232+
233+
BumpMapping=Bump Mapping
234+
BumpMappingDescription=This is a miscellaneous demonstration of an animated bump mapping effect with a moving light source illuminating a textured surface.
235+
236+
FlatText=Flat Text
237+
FlatTextDescription=This is a miscellaneous demonstration of an animated flat text perspective distortion effect using sine/cosine rotation on a tiled texture.
238+
239+
Lens=Lens
240+
LensDescription=This is a miscellaneous demonstration of an animated spherical lens distortion effect that bounces across an image, magnifying the area beneath the lens.
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Laurent Caron and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Laurent Lepinay - Original Version
13+
* Laurent Caron (laurent.caron at gmail dot com) - Conversion to SWT
14+
* IBM Corporation - adaptation to GraphicsExample
15+
*******************************************************************************/
16+
17+
package org.eclipse.swt.examples.graphics;
18+
19+
import org.eclipse.swt.graphics.GC;
20+
import org.eclipse.swt.graphics.Image;
21+
import org.eclipse.swt.graphics.ImageData;
22+
import org.eclipse.swt.graphics.PaletteData;
23+
import org.eclipse.swt.graphics.RGB;
24+
25+
/**
26+
* This tab displays an animated bump mapping effect with a moving light source
27+
* illuminating a textured surface.
28+
*/
29+
public class BumpMappingTab extends AnimatedGraphicsTab {
30+
31+
private ImageData bumpImage;
32+
private int[] envMap;
33+
private float fTime;
34+
private int lightX, lightY;
35+
private ImageData imageData;
36+
private Image outputImage;
37+
private int imgWidth, imgHeight;
38+
39+
public BumpMappingTab(GraphicsExample example) {
40+
super(example);
41+
}
42+
43+
@Override
44+
public String getCategory() {
45+
return GraphicsExample.getResourceString("Misc"); //$NON-NLS-1$
46+
}
47+
48+
@Override
49+
public String getText() {
50+
return GraphicsExample.getResourceString("BumpMapping"); //$NON-NLS-1$
51+
}
52+
53+
@Override
54+
public String getDescription() {
55+
return GraphicsExample.getResourceString("BumpMappingDescription"); //$NON-NLS-1$
56+
}
57+
58+
@Override
59+
public int getInitialAnimationTime() {
60+
return 10;
61+
}
62+
63+
@Override
64+
public void dispose() {
65+
if (outputImage != null) {
66+
outputImage.dispose();
67+
outputImage = null;
68+
}
69+
}
70+
71+
@Override
72+
public void next(int width, int height) {
73+
if (bumpImage == null) {
74+
return;
75+
}
76+
77+
for (int y = 1; y < imgHeight - 1; y++) {
78+
for (int x = 1; x < imgWidth - 1; x++) {
79+
int dX = (bumpImage.getPixel(x + 1, y) >> 16) - (bumpImage.getPixel(x - 1, y) >> 16);
80+
int dY = (bumpImage.getPixel(x, y + 1) >> 16) - (bumpImage.getPixel(x, y - 1) >> 16);
81+
82+
dX = dX - (lightX - x);
83+
dY = dY - (lightY - y);
84+
if (dX <= -128 || dX >= 128) {
85+
dX = dY = -128;
86+
}
87+
if (dY <= -128 || dY >= 128) {
88+
dX = dY = -128;
89+
}
90+
dX += 128;
91+
dY += 128;
92+
93+
imageData.setPixel(x, y, envMap[dX + 256 * dY]);
94+
}
95+
}
96+
97+
lightX = (int) (imgWidth / 2 + 80 * Math.cos(fTime += .1));
98+
lightY = (int) (imgHeight / 2 + 80 * Math.sin(fTime));
99+
}
100+
101+
@Override
102+
public void paint(GC gc, int width, int height) {
103+
if (!example.checkAdvancedGraphics()) return;
104+
105+
if (bumpImage == null) {
106+
Image sourceImage = example.loadImage(gc.getDevice(), "bump.png"); //$NON-NLS-1$
107+
if (sourceImage == null) return;
108+
bumpImage = sourceImage.getImageData();
109+
imgWidth = bumpImage.width;
110+
imgHeight = bumpImage.height;
111+
112+
envMap = new int[256 * 256];
113+
for (int y = 0; y < 256; y++) {
114+
for (int x = 0; x < 256; x++) {
115+
envMap[x + 256 * y] = (int) (255 - 255 * Math.sqrt((x - 128) * (x - 128) + (y - 128) * (y - 128)) / Math.sqrt(128 * 128 + 128 * 128));
116+
}
117+
}
118+
119+
RGB[] colors = new RGB[256];
120+
for (int i = 0; i < 64; i++) {
121+
colors[i] = new RGB(0, 0, 0);
122+
}
123+
for (int i = 64; i < 128; i++) {
124+
colors[i] = new RGB(0, 0, (i - 64) * 4);
125+
}
126+
for (int i = 128; i < 256; i++) {
127+
colors[i] = new RGB((i - 128) * 2, (i - 128) * 2, 255);
128+
}
129+
130+
fTime = 0.0f;
131+
lightX = (int) (imgWidth / 2 + 80 * Math.cos(fTime));
132+
lightY = (int) (imgHeight / 2 + 80 * Math.sin(fTime));
133+
134+
imageData = new ImageData(imgWidth, imgHeight, 8, new PaletteData(colors));
135+
}
136+
137+
if (imageData == null) {
138+
return;
139+
}
140+
141+
if (outputImage != null) {
142+
outputImage.dispose();
143+
}
144+
outputImage = new Image(gc.getDevice(), imageData);
145+
146+
int x = (width - imgWidth) / 2;
147+
int y = (height - imgHeight) / 2;
148+
gc.drawImage(outputImage, x, y);
149+
}
150+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Laurent Caron and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Guillaume Bouchon (bouchon_guillaume@yahoo.fr) - Original Version
13+
* Laurent Caron (laurent.caron at gmail dot com) - Conversion to SWT
14+
* IBM Corporation - adaptation to GraphicsExample
15+
*******************************************************************************/
16+
17+
package org.eclipse.swt.examples.graphics;
18+
19+
import org.eclipse.swt.SWT;
20+
import org.eclipse.swt.graphics.Color;
21+
import org.eclipse.swt.graphics.GC;
22+
import org.eclipse.swt.graphics.Image;
23+
24+
/**
25+
* This tab displays an animated isometric dancing shape with sine-wave
26+
* deformations across a grid of polygons.
27+
*/
28+
public class DancingTab extends AnimatedGraphicsTab {
29+
30+
private int pw, ph;
31+
private float[][] pointsX;
32+
private float[][] pointsY;
33+
private float[][] pointsZ;
34+
private double dec;
35+
private int cw, ch, dx;
36+
private int waveCx, waveCy;
37+
private Image offscreenImage;
38+
private int lastWidth, lastHeight;
39+
40+
public DancingTab(GraphicsExample example) {
41+
super(example);
42+
}
43+
44+
@Override
45+
public String getCategory() {
46+
return GraphicsExample.getResourceString("Misc"); //$NON-NLS-1$
47+
}
48+
49+
@Override
50+
public String getText() {
51+
return GraphicsExample.getResourceString("Dancing"); //$NON-NLS-1$
52+
}
53+
54+
@Override
55+
public String getDescription() {
56+
return GraphicsExample.getResourceString("DancingDescription"); //$NON-NLS-1$
57+
}
58+
59+
@Override
60+
public int getInitialAnimationTime() {
61+
return 10;
62+
}
63+
64+
@Override
65+
public void dispose() {
66+
if (offscreenImage != null) {
67+
offscreenImage.dispose();
68+
offscreenImage = null;
69+
}
70+
}
71+
72+
@Override
73+
public void next(int width, int height) {
74+
if (pointsX == null || width != lastWidth || height != lastHeight) {
75+
init(width, height);
76+
}
77+
dec += 0.25;
78+
for (int x = 0; x < pw; x++) {
79+
for (int y = 0; y < ph; y++) {
80+
pointsX[x][y] = x * cw + y * dx;
81+
pointsZ[x][y] = (float) (-Math.sin(dec + 1.5 * Math.sqrt((x - waveCx) * (x - waveCx) + (y - waveCy) * (y - waveCy)) + ch) * dx);
82+
pointsY[x][y] = y * ch - pointsZ[x][y];
83+
}
84+
}
85+
}
86+
87+
@Override
88+
public void paint(GC gc, int width, int height) {
89+
if (pointsX == null) {
90+
init(width, height);
91+
}
92+
93+
gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
94+
gc.fillRectangle(0, 0, width, height);
95+
96+
if (offscreenImage != null) {
97+
offscreenImage.dispose();
98+
}
99+
offscreenImage = new Image(gc.getDevice(), width, height);
100+
GC imageGC = new GC(offscreenImage);
101+
imageGC.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
102+
imageGC.fillRectangle(0, 0, width, height);
103+
imageGC.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_BLACK));
104+
105+
int xc = ph / 2 * dx;
106+
int[] p = new int[8];
107+
108+
for (int x = 0; x < pw - 1; x++) {
109+
for (int y = 0; y < ph - 1; y++) {
110+
int gray = 255 / ph * y;
111+
Color color = new Color(gray, gray, gray);
112+
imageGC.setBackground(color);
113+
114+
p[0] = (int) (pointsX[x][y] - xc);
115+
p[2] = (int) (pointsX[x + 1][y] - xc);
116+
p[4] = (int) (pointsX[x + 1][y + 1] - xc);
117+
p[6] = (int) (pointsX[x][y + 1] - xc);
118+
119+
p[1] = (int) pointsY[x][y];
120+
p[3] = (int) pointsY[x + 1][y];
121+
p[5] = (int) pointsY[x + 1][y + 1];
122+
p[7] = (int) pointsY[x][y + 1];
123+
124+
imageGC.fillPolygon(p);
125+
}
126+
}
127+
imageGC.dispose();
128+
129+
gc.drawImage(offscreenImage, 0, 0);
130+
}
131+
132+
private void init(int width, int height) {
133+
lastWidth = width;
134+
lastHeight = height;
135+
cw = 15;
136+
ch = 15;
137+
dx = 10;
138+
139+
pw = width / cw;
140+
ph = height / ch;
141+
if (pw < 2) pw = 2;
142+
if (ph < 2) ph = 2;
143+
144+
waveCx = pw / 2;
145+
waveCy = ph / 2;
146+
147+
pointsX = new float[pw][ph];
148+
pointsY = new float[pw][ph];
149+
pointsZ = new float[pw][ph];
150+
dec = 0;
151+
}
152+
}

0 commit comments

Comments
 (0)