|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2019 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 | + * Calogiuri Enzo Antonio (Insolit Dust) - 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.GC; |
| 21 | +import org.eclipse.swt.graphics.Image; |
| 22 | +import org.eclipse.swt.graphics.ImageData; |
| 23 | +import org.eclipse.swt.graphics.PaletteData; |
| 24 | + |
| 25 | +/** |
| 26 | + * This tab displays an animated plasma effect using precomputed sine tables |
| 27 | + * and a cycling color palette. |
| 28 | + */ |
| 29 | +public class PlasmaTab extends AnimatedGraphicsTab { |
| 30 | + |
| 31 | + private static final int SIN_TABLE_SIZE = 1800; |
| 32 | + |
| 33 | + private ImageData imageData; |
| 34 | + private Image outputImage; |
| 35 | + private int[] palette; |
| 36 | + private final int[] sin = new int[SIN_TABLE_SIZE]; |
| 37 | + private int plasmaIndex = 1; |
| 38 | + private int lastWidth; |
| 39 | + private int lastHeight; |
| 40 | + |
| 41 | + public PlasmaTab(GraphicsExample example) { |
| 42 | + super(example); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String getCategory() { |
| 47 | + return GraphicsExample.getResourceString("Misc"); //$NON-NLS-1$ |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getText() { |
| 52 | + return GraphicsExample.getResourceString("Plasma"); //$NON-NLS-1$ |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String getDescription() { |
| 57 | + return GraphicsExample.getResourceString("PlasmaDescription"); //$NON-NLS-1$ |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public int getInitialAnimationTime() { |
| 62 | + return 10; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void dispose() { |
| 67 | + if (outputImage != null) { |
| 68 | + outputImage.dispose(); |
| 69 | + outputImage = null; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void next(int width, int height) { |
| 75 | + if (palette == null) { |
| 76 | + return; |
| 77 | + } |
| 78 | + if (width != lastWidth || height != lastHeight) { |
| 79 | + init(width, height); |
| 80 | + } |
| 81 | + drawPlasma(width, height); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void paint(GC gc, int width, int height) { |
| 86 | + if (palette == null) { |
| 87 | + init(width, height); |
| 88 | + } |
| 89 | + |
| 90 | + gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_BLACK)); |
| 91 | + gc.fillRectangle(0, 0, width, height); |
| 92 | + |
| 93 | + if (imageData == null) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + if (outputImage != null) { |
| 98 | + outputImage.dispose(); |
| 99 | + } |
| 100 | + outputImage = new Image(gc.getDevice(), imageData); |
| 101 | + gc.drawImage(outputImage, 0, 0); |
| 102 | + } |
| 103 | + |
| 104 | + private void init(int width, int height) { |
| 105 | + lastWidth = width; |
| 106 | + lastHeight = height; |
| 107 | + createColors(); |
| 108 | + precomputeSin(); |
| 109 | + imageData = new ImageData(width, height, 24, new PaletteData(0xFF0000, 0xFF00, 0xFF)); |
| 110 | + } |
| 111 | + |
| 112 | + private void createColors() { |
| 113 | + palette = new int[256]; |
| 114 | + int r = 0, g = 0, b = 0; |
| 115 | + for (int i = 0; i < 256; i++) { |
| 116 | + palette[i] = 0; |
| 117 | + } |
| 118 | + |
| 119 | + for (int i = 0; i < 42; i++) { |
| 120 | + palette[i] = r << 18 | g << 10 | b << 2; |
| 121 | + r++; |
| 122 | + } |
| 123 | + for (int i = 42; i < 84; i++) { |
| 124 | + palette[i] = r << 18 | g << 10 | b << 2; |
| 125 | + g++; |
| 126 | + } |
| 127 | + for (int i = 84; i < 126; i++) { |
| 128 | + palette[i] = r << 18 | g << 10 | b << 2; |
| 129 | + b++; |
| 130 | + } |
| 131 | + for (int i = 126; i < 168; i++) { |
| 132 | + palette[i] = r << 18 | g << 10 | b << 2; |
| 133 | + r--; |
| 134 | + } |
| 135 | + for (int i = 168; i < 210; i++) { |
| 136 | + palette[i] = r << 18 | g << 10 | b << 2; |
| 137 | + g--; |
| 138 | + } |
| 139 | + for (int i = 210; i < 252; i++) { |
| 140 | + palette[i] = r << 18 | g << 10 | b << 2; |
| 141 | + b--; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private void precomputeSin() { |
| 146 | + for (int i = 0; i < SIN_TABLE_SIZE; i++) { |
| 147 | + sin[i] = (int) (Math.cos(Math.PI * i / 180) * 1024); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private void drawPlasma(int width, int height) { |
| 152 | + plasmaIndex += 2; |
| 153 | + if (plasmaIndex > 360) { |
| 154 | + plasmaIndex = 0; |
| 155 | + } |
| 156 | + |
| 157 | + for (int x = 0; x < width; x++) { |
| 158 | + int ix1 = Math.abs(((x << 1) + (plasmaIndex >> 1)) % SIN_TABLE_SIZE); |
| 159 | + int ix2 = Math.abs((x + (plasmaIndex << 1)) % SIN_TABLE_SIZE); |
| 160 | + int ix3 = Math.abs(((x >> 1) + plasmaIndex) % SIN_TABLE_SIZE); |
| 161 | + final int indexX = 75 + (sin[ix1] + sin[ix2] + (sin[ix3] << 1) >> 6); |
| 162 | + |
| 163 | + for (int y = 0; y < height; y++) { |
| 164 | + int iy1 = Math.abs((y + (plasmaIndex << 1)) % SIN_TABLE_SIZE); |
| 165 | + int iy2 = Math.abs(((y << 1) + (plasmaIndex >> 1)) % SIN_TABLE_SIZE); |
| 166 | + int iy3 = Math.abs((y + plasmaIndex) % SIN_TABLE_SIZE); |
| 167 | + final int indexY = 75 + ((sin[iy1] << 1) + sin[iy2] + (sin[iy3] << 1) >> 5); |
| 168 | + final int colorIndex = Math.abs((indexX * indexY >> 5) % 256); |
| 169 | + imageData.setPixel(x, y, palette[colorIndex]); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments