All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.pepsoft.worldpainter.NoiseSettingsEditor Maven / Gradle / Ivy

There is a newer version: 2.23.2
Show newest version
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.pepsoft.worldpainter;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JSpinner;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author SchmitzP
 */
public class NoiseSettingsEditor extends javax.swing.JPanel {
    /**
     * Creates new form NoiseSettingsEditor
     */
    public NoiseSettingsEditor() {
        initComponents();

        JSpinner.NumberEditor scaleEditor = new JSpinner.NumberEditor(spinnerScale, "0");
        scaleEditor.getTextField().setColumns(3);
        spinnerScale.setEditor(scaleEditor);
    }

    public NoiseSettings getNoiseSettings() {
        NoiseSettings noiseSettings = new NoiseSettings();
        noiseSettings.setRange((Integer) spinnerRange.getValue());
        noiseSettings.setRoughness((Integer) spinnerOctaves.getValue());
        noiseSettings.setScale((Integer) spinnerScale.getValue() / 100f);
        noiseSettings.setSeed(seed);
        return noiseSettings;
    }
    
    public void setNoiseSettings(NoiseSettings noiseSettings) {
        spinnerRange.setValue(noiseSettings.getRange());
        spinnerOctaves.setValue(noiseSettings.getRoughness());
        spinnerScale.setValue((int) (noiseSettings.getScale() * 100));
        seed = noiseSettings.getSeed();
        setControlStates();
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        setControlStates();
    }

    @Override
    public int getBaseline(int width, int height) {
        return spinnerRange.getBaseline(width, height);
    }

    public void addChangeListener(ChangeListener listener) {
        listeners.add(listener);
    }
    
    public void removeChangeListener(ChangeListener listener) {
        listeners.remove(listener);
    }
    
    private void setControlStates() {
        boolean enabled = isEnabled();
        boolean flat = (Integer) spinnerRange.getValue() == 0;
        spinnerRange.setEnabled(enabled);
        spinnerScale.setEnabled(enabled && (! flat));
        spinnerOctaves.setEnabled(enabled && (! flat));
    }

    private void fireChangeEvent() {
        ChangeEvent event = new ChangeEvent(this);
        for (ChangeListener listener: listeners) {
            listener.stateChanged(event);
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        spinnerRange = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        spinnerScale = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        spinnerOctaves = new javax.swing.JSpinner();

        jLabel1.setLabelFor(spinnerRange);
        jLabel1.setText("range:");
        jLabel1.setToolTipText("The vertical height difference, 0 being perfectly smooth");

        spinnerRange.setModel(new javax.swing.SpinnerNumberModel(0, 0, 256, 1));
        spinnerRange.setToolTipText("The vertical height difference, 0 being perfectly smooth");
        spinnerRange.addChangeListener(this::spinnerRangeStateChanged);

        jLabel2.setLabelFor(spinnerScale);
        jLabel2.setText(", scale:");
        jLabel2.setToolTipText("The horizontal scale, lower percentages creating thinner peaks and troughs");

        spinnerScale.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(100), Integer.valueOf(1), null, Integer.valueOf(1)));
        spinnerScale.setToolTipText("The horizontal scale, lower percentages creating thinner peaks and troughs");
        spinnerScale.setEnabled(false);
        spinnerScale.addChangeListener(this::spinnerScaleStateChanged);

        jLabel3.setLabelFor(spinnerOctaves);
        jLabel3.setText("%, roughness:");
        jLabel3.setToolTipText("How random or chaotic the variations are, 0 being the smoothest");

        spinnerOctaves.setModel(new javax.swing.SpinnerNumberModel(0, 0, 3, 1));
        spinnerOctaves.setToolTipText("How random or chaotic the variations are, 0 being the smoothest");
        spinnerOctaves.setEnabled(false);
        spinnerOctaves.addChangeListener(this::spinnerOctavesStateChanged);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spinnerRange, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spinnerScale, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spinnerOctaves, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(spinnerRange, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2)
                .addComponent(spinnerScale, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel3)
                .addComponent(spinnerOctaves, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// //GEN-END:initComponents

    private void spinnerRangeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerRangeStateChanged
        setControlStates();
        fireChangeEvent();
    }//GEN-LAST:event_spinnerRangeStateChanged

    private void spinnerScaleStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerScaleStateChanged
        fireChangeEvent();
    }//GEN-LAST:event_spinnerScaleStateChanged

    private void spinnerOctavesStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerOctavesStateChanged
        fireChangeEvent();
    }//GEN-LAST:event_spinnerOctavesStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSpinner spinnerOctaves;
    private javax.swing.JSpinner spinnerRange;
    private javax.swing.JSpinner spinnerScale;
    // End of variables declaration//GEN-END:variables

    private long seed;
    private final List listeners = new ArrayList<>();

    private static final long serialVersionUID = 1L;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy