Search in sources :

Example 1 with VectorAsset

use of com.android.tools.idea.npw.assetstudio.assets.VectorAsset in project android by JetBrains.

the class VectorIconButton method updateIcon.

private void updateIcon(@NotNull VdIcon selectedIcon) {
    myIcon = null;
    setIcon(null);
    try {
        // The VectorAsset class works with files, but IconPicker returns resources from a jar. We
        // adapt by wrapping the resource into a temporary file.
        File iconFile = new File(FileUtil.getTempDirectory(), selectedIcon.getName());
        InputStream iconStream = selectedIcon.getURL().openStream();
        FileOutputStream outputStream = new FileOutputStream(iconFile);
        FileUtil.copy(iconStream, outputStream);
        myXmlAsset.path().set(iconFile);
        // Our icons are always square, so although parse() expects width, we can pass in height
        int h = getHeight() - getInsets().top - getInsets().bottom;
        VectorAsset.ParseResult result = myXmlAsset.parse(h, false);
        BufferedImage image = result.getImage();
        // Switch foreground to white instead?
        image = VdIcon.adjustIconColor(this, image);
        setIcon(new ImageIcon(image));
        myIcon = selectedIcon;
    } catch (IOException ignored) {
    }
}
Also used : InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) VectorAsset(com.android.tools.idea.npw.assetstudio.assets.VectorAsset) BufferedImage(java.awt.image.BufferedImage)

Example 2 with VectorAsset

use of com.android.tools.idea.npw.assetstudio.assets.VectorAsset in project android by JetBrains.

the class NewVectorAssetStep method onWizardStarting.

@Override
protected void onWizardStarting(@NotNull ModelWizard.Facade wizard) {
    final Runnable onAssetModified = myPreviewUpdater::enqueueUpdate;
    loadAssetPath();
    SelectedProperty iconSelected = new SelectedProperty(myMaterialIconRadioButton);
    myListeners.receiveAndFire(iconSelected, isIconActive -> {
        myIconPickerPanel.setVisible(isIconActive);
        myBrowserPanel.setVisible(!isIconActive);
        myActiveAsset.set(isIconActive ? myIconButton.getAsset() : myBrowser.getAsset());
    });
    ActionListener assetListener = actionEvent -> {
        onAssetModified.run();
        saveAssetPath();
    };
    myIconButton.addAssetListener(assetListener);
    myBrowser.addAssetListener(assetListener);
    Disposer.register(this, myIconButton);
    Disposer.register(this, myBrowser);
    final BoolProperty overrideSize = new SelectedProperty(myOverrideSizeCheckBox);
    final IntProperty width = new IntValueProperty();
    final IntProperty height = new IntValueProperty();
    myGeneralBindings.bindTwoWay(new StringToIntAdapterProperty(new TextProperty(myWidthTextField)), width);
    myGeneralBindings.bindTwoWay(new StringToIntAdapterProperty(new TextProperty(myHeightTextField)), height);
    myGeneralBindings.bind(new EnabledProperty(myWidthTextField), overrideSize);
    myGeneralBindings.bind(new EnabledProperty(myHeightTextField), overrideSize);
    myListeners.listenAll(overrideSize, myOriginalSize).withAndFire(() -> {
        if (!overrideSize.get() || !myOriginalSize.get().isPresent()) {
            width.set(DEFAULT_MATERIAL_ICON_SIZE);
            height.set(DEFAULT_MATERIAL_ICON_SIZE);
        } else {
            width.set(myOriginalSize.getValue().width);
            height.set(myOriginalSize.getValue().height);
        }
    });
    final IntProperty opacityValue = new SliderValueProperty(myOpacitySlider);
    myGeneralBindings.bind(new TextProperty(myOpacityValueLabel), new FormatExpression("%d %%", opacityValue));
    final BoolProperty autoMirrored = new SelectedProperty(myEnableAutoMirroredCheckBox);
    myListeners.listenAll(myActiveAsset, overrideSize, width, height, opacityValue, autoMirrored).with(onAssetModified);
    final StringProperty name = new TextProperty(myOutputNameField);
    myListeners.listenAndFire(myActiveAsset, sender -> {
        myActiveAssetBindings.releaseAll();
        myActiveAssetBindings.bind(name, new Expression<String>(myActiveAsset.get().path()) {

            @NotNull
            @Override
            public String get() {
                File path = myActiveAsset.get().path().get();
                if (path.exists() && !path.isDirectory()) {
                    String name1 = FileUtil.getNameWithoutExtension(path).toLowerCase(Locale.getDefault());
                    if (!name1.startsWith(ICON_PREFIX)) {
                        name1 = ICON_PREFIX + AndroidResourceUtil.getValidResourceFileName(name1);
                    }
                    return AndroidResourceUtil.getValidResourceFileName(name1);
                } else {
                    return "ic_vector_name";
                }
            }
        });
        myActiveAssetBindings.bind(myActiveAsset.get().opacity(), opacityValue);
        myActiveAssetBindings.bind(myActiveAsset.get().autoMirrored(), autoMirrored);
        myActiveAssetBindings.bind(myActiveAsset.get().outputWidth(), width);
        myActiveAssetBindings.bind(myActiveAsset.get().outputHeight(), height);
    });
    // Refresh the asset preview, but fire using invokeLater, as this lets the UI lay itself out,
    // which should happen before the "generate preview" logic runs.
    ApplicationManager.getApplication().invokeLater(onAssetModified, ModalityState.any());
    // Cast VectorAsset -> BaseAsset
    myGeneralBindings.bind(myIconGenerator.sourceAsset(), new AsOptionalExpression<>(myActiveAsset));
    myGeneralBindings.bind(myIconGenerator.name(), name);
}
Also used : com.android.tools.idea.ui.properties.core(com.android.tools.idea.ui.properties.core) ActionListener(java.awt.event.ActionListener) AndroidSourceSet(com.android.tools.idea.npw.project.AndroidSourceSet) ModalityState(com.intellij.openapi.application.ModalityState) ModelWizard(com.android.tools.idea.wizard.model.ModelWizard) VectorImageComponent(com.android.tools.idea.ui.VectorImageComponent) VectorIconButton(com.android.tools.idea.npw.assetstudio.ui.VectorIconButton) JBLabel(com.intellij.ui.components.JBLabel) FormatExpression(com.android.tools.idea.ui.properties.expressions.string.FormatExpression) AndroidVectorIconGenerator(com.android.tools.idea.npw.assetstudio.icon.AndroidVectorIconGenerator) TextProperty(com.android.tools.idea.ui.properties.swing.TextProperty) VectorAssetBrowser(com.android.tools.idea.npw.assetstudio.ui.VectorAssetBrowser) Locale(java.util.Locale) Disposer(com.intellij.openapi.util.Disposer) AndroidResourceUtil(org.jetbrains.android.util.AndroidResourceUtil) BindingsManager(com.android.tools.idea.ui.properties.BindingsManager) Project(com.intellij.openapi.project.Project) StringToIntAdapterProperty(com.android.tools.idea.ui.properties.adapters.StringToIntAdapterProperty) SliderValueProperty(com.android.tools.idea.ui.properties.swing.SliderValueProperty) FileUtil(com.intellij.openapi.util.io.FileUtil) PropertiesComponent(com.intellij.ide.util.PropertiesComponent) Collection(java.util.Collection) AsOptionalExpression(com.android.tools.idea.ui.properties.expressions.optional.AsOptionalExpression) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) ModelWizardStep(com.android.tools.idea.wizard.model.ModelWizardStep) SelectedProperty(com.android.tools.idea.ui.properties.swing.SelectedProperty) File(java.io.File) JBScrollPane(com.intellij.ui.components.JBScrollPane) EnabledProperty(com.android.tools.idea.ui.properties.swing.EnabledProperty) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) VectorAsset(com.android.tools.idea.npw.assetstudio.assets.VectorAsset) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) Expression(com.android.tools.idea.ui.properties.expressions.Expression) Collections(java.util.Collections) ListenerManager(com.android.tools.idea.ui.properties.ListenerManager) javax.swing(javax.swing) StringToIntAdapterProperty(com.android.tools.idea.ui.properties.adapters.StringToIntAdapterProperty) SliderValueProperty(com.android.tools.idea.ui.properties.swing.SliderValueProperty) NotNull(org.jetbrains.annotations.NotNull) ActionListener(java.awt.event.ActionListener) EnabledProperty(com.android.tools.idea.ui.properties.swing.EnabledProperty) File(java.io.File) TextProperty(com.android.tools.idea.ui.properties.swing.TextProperty) FormatExpression(com.android.tools.idea.ui.properties.expressions.string.FormatExpression) SelectedProperty(com.android.tools.idea.ui.properties.swing.SelectedProperty)

Example 3 with VectorAsset

use of com.android.tools.idea.npw.assetstudio.assets.VectorAsset in project android by JetBrains.

the class GenerateVectorIconModel method generateIntoPath.

@Override
protected void generateIntoPath(@NotNull AndroidProjectPaths paths, @NotNull AndroidIconGenerator iconGenerator) {
    // We always know that this model is used on a step that requires VectorAssets
    VectorAsset vectorAsset = (VectorAsset) iconGenerator.sourceAsset().getValue();
    VectorAsset.ParseResult result = vectorAsset.parse();
    Map<File, BufferedImage> fileMap = iconGenerator.generateIntoFileMap(paths);
    ArrayList<File> outputFiles = Lists.newArrayList(fileMap.keySet());
    // Vector asset generator ONLY generates a single XML file
    assert outputFiles.size() == 1;
    File file = outputFiles.get(0);
    VirtualFile directory = null;
    try {
        directory = VfsUtil.createDirectories(file.getParentFile().getAbsolutePath());
        VirtualFile xmlFile = directory.findChild(file.getName());
        if (xmlFile == null || !xmlFile.exists()) {
            xmlFile = directory.createChildData(this, file.getName());
        }
        VfsUtil.saveText(xmlFile, result.getXmlContent());
    } catch (IOException e) {
        getLog().error(e);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) VectorAsset(com.android.tools.idea.npw.assetstudio.assets.VectorAsset) BufferedImage(java.awt.image.BufferedImage)

Aggregations

VectorAsset (com.android.tools.idea.npw.assetstudio.assets.VectorAsset)3 File (java.io.File)3 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 AndroidVectorIconGenerator (com.android.tools.idea.npw.assetstudio.icon.AndroidVectorIconGenerator)1 VectorAssetBrowser (com.android.tools.idea.npw.assetstudio.ui.VectorAssetBrowser)1 VectorIconButton (com.android.tools.idea.npw.assetstudio.ui.VectorIconButton)1 AndroidSourceSet (com.android.tools.idea.npw.project.AndroidSourceSet)1 VectorImageComponent (com.android.tools.idea.ui.VectorImageComponent)1 BindingsManager (com.android.tools.idea.ui.properties.BindingsManager)1 ListenerManager (com.android.tools.idea.ui.properties.ListenerManager)1 StringToIntAdapterProperty (com.android.tools.idea.ui.properties.adapters.StringToIntAdapterProperty)1 com.android.tools.idea.ui.properties.core (com.android.tools.idea.ui.properties.core)1 Expression (com.android.tools.idea.ui.properties.expressions.Expression)1 AsOptionalExpression (com.android.tools.idea.ui.properties.expressions.optional.AsOptionalExpression)1 FormatExpression (com.android.tools.idea.ui.properties.expressions.string.FormatExpression)1 EnabledProperty (com.android.tools.idea.ui.properties.swing.EnabledProperty)1 SelectedProperty (com.android.tools.idea.ui.properties.swing.SelectedProperty)1 SliderValueProperty (com.android.tools.idea.ui.properties.swing.SliderValueProperty)1 TextProperty (com.android.tools.idea.ui.properties.swing.TextProperty)1