/******************************************************************************* * Copyright (c) 2007- VTT Technical Research Centre of Finland. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.proconf.g3d.views; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.SWT; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.ColorDialog; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Slider; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.IWorkbenchPart; import org.simantics.db.Graph; import org.simantics.db.GraphRequestAdapter; import org.simantics.db.GraphRequestStatus; import org.simantics.db.Resource; import org.simantics.layer0.utils.IEntity; import org.simantics.layer0.utils.StubLinkedList; import org.simantics.layer0.utils.internal.Entity; import org.simantics.proconf.g3d.Resources; import org.simantics.proconf.g3d.base.AppearanceTools; import org.simantics.proconf.g3d.stubs.Appearance; import org.simantics.proconf.g3d.stubs.ImageTexture; import org.simantics.proconf.g3d.stubs.Material; import org.simantics.proconf.g3d.stubs.MultiTexture; import org.simantics.proconf.g3d.stubs.Shader; import org.simantics.proconf.g3d.stubs.Texture; import org.simantics.proconf.g3d.stubs.TextureCoordinateGenerator; import org.simantics.proconf.image.ImageUtils; import org.simantics.proconf.image.interfaces.IImage; import org.simantics.proconf.image.interfaces.IImageFactory; import org.simantics.proconf.image.ui.ImageComposite; import org.simantics.proconf.ui.utils.ResourceAdaptionUtils; import org.simantics.utils.ui.ErrorLogger; public class AppearanceEditor extends SinglePageResourceView{ //Appearance appearance; Resource shapeResource; Resource appearanceResource; private enum EditorState{NONE,NO_SHAPE,NO_APPEARANCE, APPEARANCE}; EditorState state = EditorState.NONE; Button materialButton; Composite ambientComposite; Composite diffuseComposite; Composite specularComposite; Composite emissiveComposite; Slider specularSlider; Slider transparencySlider; Composite textureParent; Button textureButton; Button addTextureButton; //Image image = null; boolean updating = false; private ArrayList textureComposites = new ArrayList(); Button shaderButton; Text fragmentShaderText; Text vertexShaderText; Button apply3Button; Button apply2Button; Button applyButton; public AppearanceEditor() { super(); //super(Activator.PLUGIN_ID); } @Override public void createPartControl(Composite parent) { super.createPartControl(parent); createWidgets(); } @Override protected String getFormText() { return "Appearance Editor"; } // @Override // protected void beforeCreateWidgets() { // if (!(getInputResource().isInstanceOf(GlobalIdMap.get(ThreeDimensionalModelingOntologyMapping.APPEARANCE)))) // throw new RuntimeException("Trying to open resource that is not appearance"); // appearance = AppearanceFactory.create(getInputResource()); // } @Override protected void createWidgets() { if (shapeResource == null) { if (state != EditorState.NO_SHAPE) { clearForm(); state = EditorState.NO_SHAPE; toolkit.createLabel(getBody(), "No shape selected"); getActiveForm().layout(true, true); } } else if (appearanceResource == null){ if (state != EditorState.NO_APPEARANCE) { clearForm(); state = EditorState.NO_APPEARANCE; toolkit.createLabel(getBody(), "Selected shape does not have material definition."); Button b = toolkit.createButton(getBody(), "Create Appearance", SWT.PUSH); b.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { getSession().asyncWrite(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { Appearance appearance = Appearance.createDefault(g); appearanceResource = appearance.getResource(); g.addStatement(shapeResource, Resources.g3dResource.HasAppearance, appearanceResource); return GraphRequestStatus.transactionComplete(); } @Override public void requestCompleted( GraphRequestStatus status) { reloadInUIThread(); } }); } }); getActiveForm().layout(true, true); } } else { if (state != EditorState.APPEARANCE) { clearForm(); state = EditorState.APPEARANCE; createMaterialGroup(newGridSection(2, 2, false, false, "Material", "Material properties")); createTextureGroup(newGridSection(1, 1, false, false, "Texture", "Texture properties")); createShaderGroup(newGridSection(2, 2, false, false, "Shader", "Shader properties")); getActiveForm().layout(true, true); } } } @Override public void clearForm() { super.clearForm(); textureComposites.clear(); } @Override protected void pageSelectionChanged(IWorkbenchPart part, ISelection selection) { Resource res[] = ResourceAdaptionUtils.toResources(selection); if (res.length == 0) { shapeResource = null; appearanceResource = null; reload(); return; } final Resource sel = res[0]; if (sel.equals(shapeResource)) return; //System.out.println("AppearanceEditor.pageSelectionChanged"); getSession().asyncRead(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { shapeResource = null; appearanceResource = null; // selected object must be a shape if (!g.isInstanceOf(sel, Resources.g3dResource.Shape)) { return GraphRequestStatus.transactionCancel(); } // the shape must not be ah geometry definition if (g.getObjects(sel, Resources.g3dResource.GeometryDefinitionOf).size() > 0) return GraphRequestStatus.transactionCancel(); shapeResource = sel; Collection res = g.getObjects(shapeResource, Resources.g3dResource.HasAppearance); if (res.size() == 1) appearanceResource = res.iterator().next(); return GraphRequestStatus.transactionComplete(); } @Override public void requestCompleted(GraphRequestStatus status) { reloadInUIThread(); } }); } public void reloadInUIThread() { parent.getDisplay().asyncExec(new Runnable() { @Override public void run() { reload(); } }); } public void reload() { if (updating) return; createWidgets(); if (state == EditorState.APPEARANCE) { getSession().asyncRead(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { reload(g); return GraphRequestStatus.transactionComplete(); } }); } } @Override public void reload(Graph g) { if (updating) return; if (state != EditorState.APPEARANCE) { return; } Appearance appearance = new Appearance(g,appearanceResource); Material m = appearance.getMaterial(); Texture t = appearance.getTexture(); Shader s = appearance.getShader(); loadMaterial(m); loadTexture(t); loadShader(s); } private void loadMaterial(Material m) { final boolean hasMaterial; final Color ambient; final Color diffuse; final Color specular; final Color emissive; final int shininess; final int transparency; if (m == null) { hasMaterial = false; ambient = null; diffuse = null; specular = null; emissive = null; shininess = 0; transparency = 0; } else { hasMaterial = true; ambient = AppearanceTools.getColor(m.getAmbientColor(), this.getBody().getDisplay()); diffuse = AppearanceTools.getColor(m.getDiffuseColor(), this.getBody().getDisplay()); specular = AppearanceTools.getColor(m.getSpecularColor(), this.getBody().getDisplay()); emissive = AppearanceTools.getColor(m.getEmissiveColor(), this.getBody().getDisplay()); shininess = (int)m.getShininess()[0]; transparency = (int)(m.getTransparency()[0]*100.0); } parent.getDisplay().asyncExec(new Runnable() { @Override public void run() { boolean t = hasMaterial; materialButton.setSelection(t); ambientComposite.setEnabled(t); diffuseComposite.setEnabled(t); specularComposite.setEnabled(t); emissiveComposite.setEnabled(t); specularSlider.setEnabled(t); transparencySlider.setEnabled(t); if (hasMaterial) { ambientComposite.setBackground(ambient); diffuseComposite.setBackground(diffuse); specularComposite.setBackground(specular); emissiveComposite.setBackground(emissive); specularSlider.setSelection(shininess); transparencySlider.setSelection(transparency); } else { Color c = parent.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY); ambientComposite.setBackground(c); diffuseComposite.setBackground(c); specularComposite.setBackground(c); emissiveComposite.setBackground(c); } }; }); } private void loadTexture(Texture t) { //System.out.println("AppearanceEditor.loadTexture"); if (t == null) { parent.getDisplay().asyncExec(new Runnable() { @Override public void run() { textureButton.setSelection(false); } }); return; } parent.getDisplay().asyncExec(new Runnable() { @Override public void run() { textureButton.setSelection(true); } }); if (t.isInstanceOf(Resources.g3dResource.ImageTexture)) { //final ImageTexture t2 = new ImageTexture(t); final Resource t2 = t.getResource(); parent.getDisplay().asyncExec(new Runnable() { @Override public void run() { while (textureComposites.size() > 1) { textureComposites.remove(textureComposites.size()-1).dispose(); } if (textureComposites.size() == 0) { textureComposites.add(new TextureComposite(textureParent,SWT.NONE)); } getSession().asyncWrite(new GraphRequestAdapter() { public GraphRequestStatus perform(Graph g) throws Exception { try { textureComposites.get(0).update(new ImageTexture(g,t2)); } catch (Exception e) { ErrorLogger.defaultLogError(e); } return GraphRequestStatus.transactionComplete(); }; }); } }); } else if (t.isInstanceOf(Resources.g3dResource.MultiTexture)) { MultiTexture mt = new MultiTexture(t); StubLinkedList elements = new StubLinkedList(mt.getMultiTextureElementList()); final Resource listResource = mt.getMultiTextureElementList().getResource(); final int count = elements.size(); parent.getDisplay().asyncExec(new Runnable() { @Override public void run() { while (textureComposites.size() > count) { textureComposites.get(textureComposites.size()-1).dispose(); textureComposites.remove(textureComposites.size()-1); } while (textureComposites.size() < count) { textureComposites.add(new TextureComposite(textureParent,SWT.NONE)); } getSession().asyncWrite(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { StubLinkedList elements = new StubLinkedList(new Entity(g,listResource)); Iterator i = elements.iterator(); int index = 0; while (i.hasNext()) { //MultiTextureElement e = new MultiTextureElement(i.next()); //int index = e.getMultiTextureIndexValue(); Texture tex = new Texture(i.next());//e.getTexture(); if (tex.isInstanceOf(Resources.g3dResource.ImageTexture)) { ImageTexture t2 = new ImageTexture(tex); try { textureComposites.get(index).update(t2); } catch (Exception err) { ErrorLogger.defaultLogError(err); } } index++; } return null; } }); } }); } } private void loadShader(Shader s) { final boolean hasShader = (s != null); final String vertexShader; final String fragmentShader; if (hasShader) { vertexShader = s.getVertexShader()[0]; fragmentShader = s.getFragmentShader()[0]; } else { vertexShader = null; fragmentShader = null; } parent.getDisplay().asyncExec(new Runnable() { @Override public void run() { if (!hasShader) { shaderButton.setSelection(false); } else { shaderButton.setSelection(true); vertexShaderText.setText(vertexShader); fragmentShaderText.setText(fragmentShader); } } }); } private void createMaterialGroup(Composite parent) { toolkit.paintBordersFor(parent); toolkit.setBorderStyle(SWT.BORDER); materialButton = toolkit.createButton(parent, "Has Material", SWT.CHECK); materialButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { boolean t = materialButton.getSelection(); ambientComposite.setEnabled(t); diffuseComposite.setEnabled(t); specularComposite.setEnabled(t); emissiveComposite.setEnabled(t); specularSlider.setEnabled(t); transparencySlider.setEnabled(t); } }); GridData data = new GridData(GridData.FILL, GridData.FILL, false, false,2,1); materialButton.setLayoutData(data); data = new GridData(GridData.FILL, GridData.FILL, false, false,1,1); data.widthHint = 100; data.heightHint = 20; toolkit.createLabel(parent, "Ambient"); ambientComposite = toolkit.createComposite(parent, SWT.BORDER); ambientComposite.setLayoutData(data); ambientComposite.addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { ColorDialog dialog = new ColorDialog(AppearanceEditor.this.getBody().getShell()); RGB rgb = dialog.open(); if (rgb != null) { ambientComposite.setBackground(new Color(AppearanceEditor.this.getBody().getDisplay(),rgb)); } } }); toolkit.createLabel(parent, "Diffuse"); diffuseComposite = toolkit.createComposite(parent, SWT.BORDER); diffuseComposite.setLayoutData(data); diffuseComposite.addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { ColorDialog dialog = new ColorDialog(AppearanceEditor.this.getBody().getShell()); RGB rgb = dialog.open(); if (rgb != null) { diffuseComposite.setBackground(new Color(AppearanceEditor.this.getBody().getDisplay(),rgb)); } } }); toolkit.createLabel(parent, "Specular"); specularComposite = toolkit.createComposite(parent, SWT.BORDER); specularComposite.setLayoutData(data); specularComposite.addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { ColorDialog dialog = new ColorDialog(AppearanceEditor.this.getBody().getShell()); RGB rgb = dialog.open(); if (rgb != null) { specularComposite.setBackground(new Color(AppearanceEditor.this.getBody().getDisplay(),rgb)); } } }); toolkit.createLabel(parent, "Emissive"); emissiveComposite = toolkit.createComposite(parent, SWT.BORDER); emissiveComposite.setLayoutData(data); emissiveComposite.addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { ColorDialog dialog = new ColorDialog(AppearanceEditor.this.getBody().getShell()); RGB rgb = dialog.open(); if (rgb != null) { emissiveComposite.setBackground(new Color(AppearanceEditor.this.getBody().getDisplay(),rgb)); } } }); toolkit.createLabel(parent, "Shininess"); specularSlider = new Slider(parent,SWT.NONE); specularSlider.setValues(20, 0, 255, 1, 1, 10); toolkit.adapt(specularSlider, true, true); toolkit.createLabel(parent, "Transparency"); transparencySlider = new Slider(parent,SWT.NONE); transparencySlider.setValues(0, 0, 100, 1, 1, 10); toolkit.adapt(transparencySlider, true, true); apply2Button = toolkit.createButton(parent, "Apply", SWT.PUSH); apply2Button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { convertMaterial(); } }); } private void createTextureGroup(Composite parent) { textureParent = parent; toolkit.paintBordersFor(parent); toolkit.setBorderStyle(SWT.BORDER); textureButton = toolkit.createButton(parent, "Has Texture", SWT.CHECK); textureButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { //loadImageButton.setEnabled(textureButton.getSelection()); //imageComposite.setEnabled(textureButton.getSelection()); //textureButton.setEnabled(textureButton.getSelection()); } }); textureButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { //loadImageButton.setEnabled(textureButton.getSelection()); //imageComposite.setEnabled(textureButton.getSelection()); //textureButton.setEnabled(textureButton.getSelection()); } }); addTextureButton = toolkit.createButton(parent, "Add Texture", SWT.PUSH); addTextureButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { textureComposites.add(new TextureComposite(textureParent,SWT.NONE)); } }); applyButton = toolkit.createButton(parent, "Apply", SWT.PUSH); applyButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { convertImage(); } }); } private void createShaderGroup(Composite parent) { toolkit.paintBordersFor(parent); toolkit.setBorderStyle(SWT.BORDER); GridData data = new GridData(SWT.FILL,SWT.FILL,true,true,2,1); shaderButton = toolkit.createButton(parent, "Has Shader", SWT.CHECK); shaderButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { } }); shaderButton.setLayoutData(data); data = new GridData(SWT.FILL,SWT.FILL,true,true,1,1); data.widthHint = 400; data.heightHint = 200; toolkit.createLabel(parent, "Vertex Shader"); vertexShaderText = toolkit.createText(parent,"", SWT.MULTI); vertexShaderText.setLayoutData(data); toolkit.createLabel(parent, "Fragment Shader"); fragmentShaderText = toolkit.createText(parent,"", SWT.MULTI); fragmentShaderText.setLayoutData(data); apply3Button = toolkit.createButton(parent, "Apply", SWT.PUSH); apply3Button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { convertShader(); } }); } private void convertShader() { updating = true; final boolean hasShader = shaderButton.getSelection(); final String vertexShader = vertexShaderText.getText(); final String fragmentShader = fragmentShaderText.getText(); getSession().asyncWrite(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { Appearance appearance = new Appearance(g,appearanceResource); if (hasShader) { Shader s = appearance.getShader(); if (s == null) { s = Shader.createDefault(g); appearance.setShader(s); } s.setVertexShader(vertexShader); s.setFragmentShader(fragmentShader); } else { appearance.setShader(null); } updating = false; return GraphRequestStatus.transactionComplete(); } }); } private void convertMaterial() { updating = true; final boolean hasMaterial = materialButton.getSelection(); final Color ambientColor = ambientComposite.getBackground(); final Color diffuseColor = diffuseComposite.getBackground(); final Color specularColor = specularComposite.getBackground(); final Color emissiveColor = emissiveComposite.getBackground(); final double shininess = (double)specularSlider.getSelection(); final double transparency = (double)transparencySlider.getSelection()/ 100.0; getSession().asyncWrite(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { Appearance appearance = new Appearance(g,appearanceResource); if (hasMaterial) { Material m = appearance.getMaterial(); if (m == null) { m = Material.createDefault(g); } org.simantics.proconf.g3d.stubs.Color aColor = m.getAmbientColor(); org.simantics.proconf.g3d.stubs.Color dColor = m.getDiffuseColor(); org.simantics.proconf.g3d.stubs.Color sColor = m.getSpecularColor(); org.simantics.proconf.g3d.stubs.Color eColor = m.getEmissiveColor(); AppearanceTools.setColor(aColor,ambientColor); AppearanceTools.setColor(dColor,diffuseColor); AppearanceTools.setColor(sColor, specularColor); AppearanceTools.setColor(eColor, emissiveColor); m.setShininess(shininess); m.setTransparency(transparency); appearance.setMaterial(m); } else { appearance.removeRelatedStatements(Resources.g3dResource.HasMaterial); } updating = false; return GraphRequestStatus.transactionComplete(); } }); } private void convertImage() { updating = true; getSession().syncWrite(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { Appearance appearance = new Appearance(g,appearanceResource); Texture t = appearance.getTexture(); if (textureComposites.size() == 1) { ImageTexture t2 = null; if (t != null && t.isInstanceOf(Resources.g3dResource.ImageTexture)) { t2 = new ImageTexture(t); } else { t2 = ImageTexture.createDefault(g); appearance.setTexture(t2); } final Resource mode = textureComposites.get(0).getModeType(); if (mode != null) t2.setTextureMode(mode); textureComposites.get(0).getTexture(t2); } else if (textureComposites.size() > 1) { MultiTexture mt = null; if (t != null && t.isInstanceOf(Resources.g3dResource.MultiTexture)) { mt = new MultiTexture(t); } else { mt = MultiTexture.createDefault(g); appearance.setTexture(mt); } StubLinkedList elements = new StubLinkedList(mt.getMultiTextureElementList()); //PropertyTypeSet elements = mt.getMultiTextureElementSet(); Iterator it = elements.iterator(); for (int i = 0; i < textureComposites.size(); i++) { TextureComposite tc = textureComposites.get(i); ImageTexture tex = null; if (it.hasNext()) tex = new ImageTexture(it.next()); else { tex = ImageTexture.createDefault(g); elements.add(i,tex); } Resource mode = tc.getModeType(); if (mode == null) tex.setTextureMode(Resources.g3dResource.CombineMode_modulate); else tex.setTextureMode(mode); textureComposites.get(i).getTexture(tex); } } else { appearance.setTexture(null); } updating = false; return GraphRequestStatus.transactionComplete(); } }); } private class TextureComposite extends Composite { private Button textureGeneratorButton; private Button sphereMapButton; private Button eyeLinearButton; private Button objectLinearButton; private Button normalMapButton; private Button reflectionMapButton; private Button modulateButton; private Button replaceButton; private Button decalButton; private Button blendButton; private Button loadImageButton; private ImageComposite imageComposite; private Button removeButton; public TextureComposite(Composite parent, int style) { super(parent,style); GridLayout layout = new GridLayout(2,false); this.setLayout(layout); loadImageButton = toolkit.createButton(this, "Load Texture", SWT.PUSH); loadImageButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { IImage[] images = ImageUtils.loadImagesDialog(getSite().getShell(), false, ImageUtils.getImageFactories()); if (images == null) return; IImage image = images[0]; setImage(image); } }); GridData data = new GridData(SWT.LEFT,SWT.FILL,false,false,2,1); loadImageButton.setLayoutData(data); data = new GridData(SWT.FILL,SWT.FILL,true,true,2,1); imageComposite = new ImageComposite(this,SWT.BORDER); imageComposite.setLayoutData(data); Composite texGenComposite = toolkit.createComposite(this, SWT.BORDER); layout = new GridLayout(1,false); texGenComposite.setLayout(layout); data = new GridData(SWT.FILL,SWT.FILL,true,true,1,1); data.heightHint = 140; texGenComposite.setLayoutData(data); textureGeneratorButton = toolkit.createButton(texGenComposite, "Has Texture Coordinate Generator", SWT.CHECK); textureGeneratorButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { sphereMapButton.setEnabled(textureGeneratorButton.getSelection()); eyeLinearButton.setEnabled(textureGeneratorButton.getSelection()); objectLinearButton.setEnabled(textureGeneratorButton.getSelection()); normalMapButton.setEnabled(textureGeneratorButton.getSelection()); reflectionMapButton.setEnabled(textureGeneratorButton.getSelection()); } }); sphereMapButton = toolkit.createButton(texGenComposite, "Shpere Map", SWT.RADIO); sphereMapButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (!sphereMapButton.getSelection()) { sphereMapButton.setSelection(true); } else { eyeLinearButton.setSelection(false); objectLinearButton.setSelection(false); normalMapButton.setSelection(false); reflectionMapButton.setSelection(false); } } }); eyeLinearButton = toolkit.createButton(texGenComposite, "Eye Linear", SWT.RADIO); eyeLinearButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (!eyeLinearButton.getSelection()) { eyeLinearButton.setSelection(true); } else { sphereMapButton.setSelection(false); objectLinearButton.setSelection(false); normalMapButton.setSelection(false); reflectionMapButton.setSelection(false); } } }); objectLinearButton = toolkit.createButton(texGenComposite, "Object Linear", SWT.RADIO); objectLinearButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (!objectLinearButton.getSelection()) { objectLinearButton.setSelection(true); } else { eyeLinearButton.setSelection(false); sphereMapButton.setSelection(false); normalMapButton.setSelection(false); reflectionMapButton.setSelection(false); } } }); normalMapButton = toolkit.createButton(texGenComposite, "Normal Map", SWT.RADIO); normalMapButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (!normalMapButton.getSelection()) { normalMapButton.setSelection(true); } else { eyeLinearButton.setSelection(false); objectLinearButton.setSelection(false); sphereMapButton.setSelection(false); reflectionMapButton.setSelection(false); } } }); reflectionMapButton = toolkit.createButton(texGenComposite, "Reflection Map", SWT.RADIO); reflectionMapButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (!reflectionMapButton.getSelection()) { reflectionMapButton.setSelection(true); } else { eyeLinearButton.setSelection(false); objectLinearButton.setSelection(false); normalMapButton.setSelection(false); sphereMapButton.setSelection(false); } } }); Composite texModeComposite = toolkit.createComposite(this, SWT.BORDER); texModeComposite.setLayout(layout); texModeComposite.setLayoutData(data); modulateButton = toolkit.createButton(texModeComposite, "Modulate", SWT.RADIO); modulateButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { modulateButton.setSelection(true); replaceButton.setSelection(false); blendButton.setSelection(false); decalButton.setSelection(false); } }); blendButton = toolkit.createButton(texModeComposite, "Blend", SWT.RADIO); blendButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { modulateButton.setSelection(false); replaceButton.setSelection(false); blendButton.setSelection(true); decalButton.setSelection(false); } }); replaceButton = toolkit.createButton(texModeComposite, "Replace", SWT.RADIO); replaceButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { modulateButton.setSelection(false); replaceButton.setSelection(true); blendButton.setSelection(false); decalButton.setSelection(false); } }); decalButton = toolkit.createButton(texModeComposite, "Decal", SWT.RADIO); decalButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { modulateButton.setSelection(false); replaceButton.setSelection(false); blendButton.setSelection(false); decalButton.setSelection(true); } }); removeButton = toolkit.createButton(this, "Remove texture", SWT.PUSH); parent.getParent().getParent().layout(true, true); } public void setImage(IImage image) { imageComposite.setImage(image); } public IImage getImage() { return imageComposite.getImage(); } public void update(ImageTexture t2) throws Exception { Graph graph = t2.getGraph(); org.simantics.image.stubs.Image i = t2.getImage(); IImageFactory f = ImageUtils.getImageFactoryForResource(graph,i.getResource()); final IImage p = f.createImageForResource(graph,i.getResource()); // PixelDimension pd = p.getDimensions().getPixelDimension(); // if (pd==null) pd = AppearanceTools.DEFAULT_SIZE; // final ImageData id = p.rasterize(pd.getWidth(), pd.getHeight()); parent.getDisplay().asyncExec(new Runnable() { @Override public void run() { //Image im = new Image(AppearanceEditor.this.getBody().getDisplay(),id); //imageComposite.setImage(im); //showImage(); setImage(p); } }); TextureCoordinateGenerator gen = t2.getTextureCoordinateGenerator(); final boolean tg; final boolean sm; final boolean el; final boolean ol; final boolean nm; final boolean rm; if (gen == null) { tg = false; sm = false; el = false; ol = false; nm = false; rm = false; } else { //type = gen.getTextureCoordinateGeneratorTypeValue(); //textureGeneratorButton.setSelection(true); tg = true; if (gen.equals(Resources.g3dResource.TextureCoordinateGenerator_sphere)) { sm = true; el = false; ol = false; nm = false; rm = false; } else if (gen.equals(Resources.g3dResource.TextureCoordinateGenerator_eyelinear)) { sm = false; el = true; ol = false; nm = false; rm = false; } else if (gen.equals(Resources.g3dResource.TextureCoordinateGenerator_objectlinear)) { sm = false; el = false; ol = true; nm = false; rm = false; } else if (gen.equals(Resources.g3dResource.TextureCoordinateGenerator_normal)) { sm = false; el = false; ol = false; nm = true; rm = false; } else if (gen.equals(Resources.g3dResource.TextureCoordinateGenerator_reflection)) { sm = false; el = false; ol = false; nm = false; rm = true; } else { sm = false; el = false; ol = false; nm = false; rm = false; } } parent.getDisplay().asyncExec(new Runnable() { @Override public void run() { textureGeneratorButton.setSelection(tg); sphereMapButton.setSelection(sm); eyeLinearButton.setSelection(el); objectLinearButton.setSelection(ol); normalMapButton.setSelection(nm); reflectionMapButton.setSelection(rm); sphereMapButton.setEnabled(textureGeneratorButton.getSelection()); eyeLinearButton.setEnabled(textureGeneratorButton.getSelection()); objectLinearButton.setEnabled(textureGeneratorButton.getSelection()); normalMapButton.setEnabled(textureGeneratorButton.getSelection()); reflectionMapButton.setEnabled(textureGeneratorButton.getSelection()); } }); } public Resource getGenType() { if (sphereMapButton.getSelection()) return Resources.g3dResource.TextureCoordinateGenerator_sphere; else if (eyeLinearButton.getSelection()) return Resources.g3dResource.TextureCoordinateGenerator_eyelinear; else if (objectLinearButton.getSelection()) return Resources.g3dResource.TextureCoordinateGenerator_objectlinear; else if (normalMapButton.getSelection()) return Resources.g3dResource.TextureCoordinateGenerator_normal; else if (reflectionMapButton.getSelection()) return Resources.g3dResource.TextureCoordinateGenerator_reflection; return null; } public Resource getModeType() { if (modulateButton.getSelection()) return Resources.g3dResource.MultiTextureMode_modulate; else if (replaceButton.getSelection()) return Resources.g3dResource.MultiTextureMode_replace; else if (blendButton.getSelection()) return Resources.g3dResource.MultiTextureMode_blend; else if (decalButton.getSelection()) return Resources.g3dResource.MultiTextureMode_decal; return null; } public void getTexture(ImageTexture t2) throws Exception{ Graph g = t2.getGraph(); IImage image = getImage(); if (image != null) { Resource res = image.instantiateAsResource(g); t2.setImage(res); } final Resource t2res = t2.getResource(); if (textureGeneratorButton.getSelection()) { final Resource type; if (sphereMapButton.getSelection()) type = Resources.g3dResource.TextureCoordinateGenerator_sphere; else if (eyeLinearButton.getSelection()) type = Resources.g3dResource.TextureCoordinateGenerator_eyelinear; else if (objectLinearButton.getSelection()) type = Resources.g3dResource.TextureCoordinateGenerator_objectlinear; else if (normalMapButton.getSelection()) type = Resources.g3dResource.TextureCoordinateGenerator_normal; else if (reflectionMapButton.getSelection()) type = Resources.g3dResource.TextureCoordinateGenerator_reflection; else type = null; if (type != null) { getSession().asyncWrite(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { ImageTexture t2 = new ImageTexture(g, t2res); t2.setTextureCoordinateGenerator(type); return GraphRequestStatus.transactionComplete(); } }); } } } /* * public void getTexture(ImageTexture t2) { if * (imageComposite.getImage() != null) { ImageData data = * imageComposite.getImage().getImageData(); if (!data.palette.isDirect) { * throw new RuntimeException("Not direct"); } int bytes = 0; String * type = ""; if (data.depth != 32 && data.depth != 24) { throw new * UnsupportedOperationException("Cannot handle bitdepth " + * data.depth); } if (data.depth == 32) { bytes = 4; type = "RGBA"; } * else if (data.depth == 24) { bytes = 3; type = "RGB"; } String size = * data.width + " " + data.height; byte texturedata[] = new * byte[data.width * data.height * bytes]; for (int y = 0; y < * data.height; y++) { for (int x = 0; x < data.width; x++) { int index = * (y * data.width + x) * bytes; * * texturedata[index] = data.data[index]; texturedata[index + 1] = * data.data[index + 1]; texturedata[index + 2] = data.data[index + 2]; * if (bytes == 4) texturedata[index + 3] = data.data[index + 3]; * } } String base64 = new * sun.misc.BASE64Encoder().encode(texturedata); // if * (coreTC.getCurrentTransaction() == null) * * * * fi.vtt.proconf.threedimensionalmodeling.stub.v1_0.Image i = * t2.getImage(); if (i == null) { i = ImageFactory.instantiate(graph); * graph.commitChanges(this); t2.setImage(i); } * i.setImageDataValue(base64); i.setImageTypeValue(type); * i.setImageSizeValue(size); graph.commitChanges(this); if * (textureGeneratorButton.getSelection()) { type = null; if * (sphereMapButton.getSelection()) type = * AppearanceTools.TEXTURE_COORD_GEN_SPHERE; else if * (eyeLinearButton.getSelection()) type = * AppearanceTools.TEXTURE_COORD_GEN_EYE_LINEAR; else if * (objectLinearButton.getSelection()) type = * AppearanceTools.TEXTURE_COORD_GEN_OBJECT_LINEAR; else if * (normalMapButton.getSelection()) type = * AppearanceTools.TEXTURE_COORD_GEN_NORMAL; else if * (reflectionMapButton.getSelection()) type = * AppearanceTools.TEXTURE_COORD_GEN_REFLECTION; else type = null; if * (type != null) { TextureCoordinateGenerator gen = * t2.getTextureCoordinateGenerator(); if (gen == null) { gen = * TextureCoordinateGeneratorFactory.instantiate(graph); * graph.commitChanges(this); t2.setTextureCoordinateGenerator(gen); } * gen.setTextureCoordinateGeneratorTypeValue(type); * graph.commitChanges(this); } * } else { //t2.setTextureCoordinateGenerator(null); * graph.commitChanges(this); } * } } */ } }