Search in sources :

Example 1 with LengthValidator

use of gaiasky.util.validator.LengthValidator in project gaiasky by langurmonkey.

the class KeyframesWindow method build.

@Override
protected void build() {
    /*
         * Right and left tables
         */
    Table left = new Table(skin);
    left.align(Align.top | Align.left);
    Table right = new Table(skin);
    right.align(Align.top | Align.left);
    /* LEFT - CONTROLS */
    // ADD
    OwnTextIconButton addKeyframe = new OwnTextIconButton(I18n.txt("gui.keyframes.add.end"), skin, "add");
    addKeyframe.addListener(new OwnTextTooltip(I18n.txt("gui.tooltip.kf.add.end"), skin));
    addKeyframe.pad(pad5);
    left.add(addKeyframe).left().colspan(2).padBottom(pad5).row();
    addKeyframe.addListener(event -> {
        if (event instanceof ChangeEvent) {
            // Add at end
            return addKeyframe(-1);
        }
        return false;
    });
    // SECONDS
    FloatValidator secondsValidator = new FloatValidator(0.0001f, 9999.0f);
    secondsValidator.setIsValidCallback(() -> {
        // Enable add button
        addKeyframe.setDisabled(false);
    });
    secondsValidator.setIsInvalidCallback(() -> {
        // Disable add button
        addKeyframe.setDisabled(true);
    });
    secondsInput = new OwnTextField("1.0", skin, secondsValidator);
    secondsInput.setWidth(96f);
    OwnLabel secondsLabel = new OwnLabel(I18n.txt("gui.keyframes.secsafter") + ":", skin);
    left.add(secondsLabel).center().left().padRight(pad10).padBottom(pad10);
    left.add(secondsInput).center().left().padBottom(pad10).row();
    // NAME
    LengthValidator lengthValidator = new LengthValidator(0, 15);
    RegexpValidator nameValidator = new RegexpValidator(lengthValidator, "^[^*&%\\s\\+\\=\\\\\\/@#\\$&\\*()~]*$");
    nameInput = new OwnTextField("", skin, nameValidator);
    nameInput.setWidth(96f);
    OwnLabel nameLabel = new OwnLabel(I18n.txt("gui.keyframes.name") + ":", skin);
    left.add(nameLabel).center().left().padRight(pad10).padBottom(pad10);
    left.add(nameInput).center().left().padBottom(pad10).row();
    left.pack();
    /* RIGHT - KEYFRAMES */
    OwnLabel keyframesTitle = new OwnLabel(I18n.txt("gui.keyframes.list"), skin, "hud-header");
    // KEYFRAMES TABLE
    keyframesTable = buildKeyframesTable();
    // ADD SCROLL
    rightScroll = new OwnScrollPane(keyframesTable, skin, "minimalist-nobg");
    rightScroll.setExpand(true);
    rightScroll.setScrollingDisabled(true, false);
    rightScroll.setHeight(160f);
    rightScroll.setWidth(540f);
    rightScroll.setFadeScrollBars(true);
    right.add(keyframesTitle).top().left().padBottom(pad10).row();
    right.add(rightScroll).center().left();
    right.pack();
    /* RE-NORMALIZE TIME */
    OwnTextButton normalizeTime = new OwnTextButton(I18n.txt("gui.keyframes.normalize"), skin);
    normalizeTime.addListener(new OwnTextTooltip(I18n.txt("gui.tooltip.kf.normalize"), skin));
    normalizeTime.pad(pad10);
    normalizeTime.addListener((event) -> {
        if (event instanceof ChangeEvent) {
            if (keyframes != null && keyframes.size > 2) {
                Vector3d aux = new Vector3d();
                int n = keyframes.size;
                double totalTime = 0;
                double totalDist = 0;
                for (int i = 1; i < n; i++) {
                    Keyframe kf0 = keyframes.get(i - 1);
                    Keyframe kf1 = keyframes.get(i);
                    totalTime += kf1.seconds;
                    totalDist += aux.set(kf1.pos).sub(kf0.pos).len();
                }
                // Loop over keyframes and assign new times
                for (int i = 1; i < n; i++) {
                    Keyframe kf0 = keyframes.get(i - 1);
                    Keyframe kf1 = keyframes.get(i);
                    double dist = aux.set(kf1.pos).sub(kf0.pos).len();
                    kf1.seconds = totalTime * dist / totalDist;
                }
                // Reload window contents
                reinitialiseKeyframes(keyframes, null);
                keyframesPathObject.unselect();
                logger.info(I18n.txt("gui.keyframes.normalize.done"));
            }
            return true;
        }
        return false;
    });
    /* ACTION BUTTONS */
    HorizontalGroup buttons = new HorizontalGroup();
    buttons.space(pad10);
    // Open keyframes
    OwnTextIconButton open = new OwnTextIconButton(I18n.txt("gui.keyframes.load"), skin, "open");
    open.addListener(new OwnTextTooltip(I18n.txt("gui.tooltip.kf.load"), skin));
    open.pad(pad5);
    open.addListener((event) -> {
        if (event instanceof ChangeEvent) {
            FileChooser fc = new FileChooser(I18n.txt("gui.download.pickloc"), skin, stage, SysUtils.getDefaultCameraDir(), FileChooser.FileChooserTarget.FILES);
            fc.setShowHidden(Settings.settings.program.fileChooser.showHidden);
            fc.setShowHiddenConsumer((showHidden) -> Settings.settings.program.fileChooser.showHidden = showHidden);
            fc.setFileFilter(pathname -> pathname.getFileName().toString().endsWith(".gkf"));
            fc.setAcceptedFiles("*.gkf");
            fc.setResultListener((success, result) -> {
                if (success) {
                    if (Files.exists(result) && Files.isRegularFile(result)) {
                        // Load selected file
                        try {
                            Array<Keyframe> kfs = CameraKeyframeManager.instance.loadKeyframesFile(result);
                            // Update current instance
                            reinitialiseKeyframes(kfs, null);
                            keyframesPathObject.unselect();
                            lastKeyframeFileName = result.getFileName().toString();
                            logger.info(I18n.txt("gui.keyframes.load.success", keyframes.size, result.getFileName()));
                        } catch (RuntimeException e) {
                            logger.error(I18n.txt("gui.keyframes.load.error", result.getFileName()), e);
                            Label warn = new OwnLabel(I18n.txt("error.loading.format", result.getFileName()), skin);
                            warn.setColor(1f, .4f, .4f, 1f);
                            notice.setActor(warn);
                            return false;
                        }
                    } else {
                        logger.error(I18n.txt("error.loading.notexistent", result.getFileName()));
                        Label warn = new OwnLabel(I18n.txt("error.loading.notexistent", result.getFileName()), skin);
                        warn.setColor(1f, .4f, .4f, 1f);
                        notice.setActor(warn);
                        return false;
                    }
                }
                notice.clearActor();
                return true;
            });
            fc.show(stage);
            return true;
        }
        return false;
    });
    // Save keyframes
    OwnTextIconButton save = new OwnTextIconButton(I18n.txt("gui.keyframes.save"), skin, "save");
    save.addListener(new OwnTextTooltip(I18n.txt("gui.tooltip.kf.save"), skin));
    save.pad(pad5);
    save.addListener((event) -> {
        if (event instanceof ChangeEvent) {
            String suggestedName = lastKeyframeFileName == null ? df.format(new Date()) + "_keyframes.gkf" : lastKeyframeFileName;
            FileNameWindow fnw = new FileNameWindow(suggestedName, stage, skin);
            OwnTextField textField = fnw.getFileNameField();
            fnw.setAcceptRunnable(() -> {
                if (textField.isValid()) {
                    EventManager.publish(Event.KEYFRAMES_FILE_SAVE, fnw, keyframes, textField.getText());
                    lastKeyframeFileName = textField.getText();
                    notice.clearActor();
                } else {
                    Label warn = new OwnLabel(I18n.txt("error.file.name.notvalid", textField.getText()), skin);
                    warn.setColor(1f, .4f, .4f, 1f);
                    notice.setActor(warn);
                }
            });
            fnw.show(stage);
            return true;
        }
        return false;
    });
    // Export to camera path
    OwnTextIconButton export = new OwnTextIconButton(I18n.txt("gui.keyframes.export"), skin, "export");
    export.addListener(new OwnTextTooltip(I18n.txt("gui.tooltip.kf.export"), skin));
    export.pad(pad5);
    export.addListener((event) -> {
        if (event instanceof ChangeEvent) {
            String suggestedName = df.format(new Date()) + ".gsc";
            FileNameWindow fnw = new FileNameWindow(suggestedName, stage, skin);
            OwnTextField textField = fnw.getFileNameField();
            fnw.setAcceptRunnable(() -> {
                if (textField.isValid()) {
                    EventManager.publish(Event.KEYFRAMES_EXPORT, fnw, keyframes, textField.getText());
                    notice.clearActor();
                } else {
                    Label warn = new OwnLabel(I18n.txt("error.file.name.notvalid", textField.getText()), skin);
                    warn.setColor(1f, .4f, .4f, 1f);
                    notice.setActor(warn);
                }
            });
            fnw.show(stage);
            return true;
        }
        return false;
    });
    // Keyframe preferences
    Button preferences = new OwnTextIconButton(I18n.txt("gui.preferences"), skin, "preferences");
    preferences.setName("keyframe preferences");
    preferences.pad(pad5);
    preferences.addListener(new OwnTextTooltip(I18n.txt("gui.tooltip.kf.editprefs"), skin));
    preferences.addListener((event) -> {
        if (event instanceof ChangeEvent) {
            KeyframePreferencesWindow kpw = new KeyframePreferencesWindow(stage, skin);
            kpw.setAcceptRunnable(() -> {
                // Resample
                GaiaSky.postRunnable(() -> {
                    keyframesPathObject.resamplePath();
                });
            });
            kpw.setCancelRunnable(() -> {
            });
            kpw.show(stage, me.getWidth(), 0);
            return true;
        }
        return false;
    });
    buttons.addActor(open);
    buttons.addActor(save);
    buttons.addActor(export);
    buttons.addActor(preferences);
    /* FINAL LAYOUT */
    content.add(left).top().left().padRight(pad10 * 2f).padBottom(pad10 * 3f);
    content.add(right).width(592f).top().left().padBottom(pad10).row();
    notice = content.add();
    notice.padBottom(pad10 * 2f).expandY().center().colspan(2).row();
    content.add(normalizeTime).colspan(2).bottom().center().padBottom(pad10).row();
    content.add(buttons).colspan(2).bottom().right().row();
    // CLEAR
    OwnTextButton clear = new OwnTextButton(I18n.txt("gui.clear"), skin);
    clear.setName("clear");
    clear.addListener((event) -> {
        if (event instanceof ChangeEvent) {
            GaiaSky.postRunnable(this::clean);
            return true;
        }
        return false;
    });
    buttonGroup.addActorAt(0, clear);
    // HIDE
    OwnTextButton hide = new OwnTextButton(I18n.txt("gui.hide"), skin);
    hide.setName("hide");
    hide.addListener((event) -> {
        if (event instanceof ChangeEvent) {
            hide();
            return true;
        }
        return false;
    });
    buttonGroup.addActorAt(1, hide);
    recalculateButtonSize();
}
Also used : LengthValidator(gaiasky.util.validator.LengthValidator) Keyframe(gaiasky.desktop.util.camera.Keyframe) FloatValidator(gaiasky.util.validator.FloatValidator) RegexpValidator(gaiasky.util.validator.RegexpValidator) Date(java.util.Date) ChangeEvent(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent) Vector3d(gaiasky.util.math.Vector3d)

Example 2 with LengthValidator

use of gaiasky.util.validator.LengthValidator in project gaiasky by langurmonkey.

the class FileNameWindow method build.

@Override
protected void build() {
    OwnLabel label = new OwnLabel(I18n.txt("gui.filename.filename") + ": ", skin);
    LengthValidator lengthValidator = new LengthValidator(3, 40);
    RegexpValidator nameValidator = new RegexpValidator(lengthValidator, "^[^*&%\\s\\+\\=\\\\\\/@#\\$&\\*()~]+$");
    fileName = new OwnTextField(defaultName, skin, nameValidator);
    fileName.setWidth(400f);
    content.add(label).padRight(pad10).padBottom(pad10);
    content.add(fileName).padBottom(pad10);
}
Also used : LengthValidator(gaiasky.util.validator.LengthValidator) OwnTextField(gaiasky.util.scene2d.OwnTextField) OwnLabel(gaiasky.util.scene2d.OwnLabel) RegexpValidator(gaiasky.util.validator.RegexpValidator)

Example 3 with LengthValidator

use of gaiasky.util.validator.LengthValidator in project gaiasky by langurmonkey.

the class ControllerConfigWindow method build.

@Override
protected void build() {
    float lw = 224f;
    float iw = 176f;
    // Main tips
    OwnLabel tip = new OwnLabel("Press the button/axis indicated on the controller image. Click any input on the right\nto edit its value. Click the first input to restart the full sequence.", skin);
    content.add(tip).colspan(2).padBottom(pad10 * 2f).row();
    // Controller
    Cell<?> controllerCell = content.add().padRight(pad10 * 2);
    Table controllerTable = new Table(skin);
    controllerTable.setBackground(new SpriteDrawable(new Sprite(controller)));
    controllerTable.setSize(controller.getWidth(), controller.getHeight());
    controllerCell.setActor(controllerTable);
    elementCell = controllerTable.add((Image) null);
    // Last input
    OwnLabel currentInputLabel = new OwnLabel("Last input:", skin, "header");
    currentInput = new OwnLabel(none, skin, "default-blue");
    HorizontalGroup lastInputGroup = new HorizontalGroup();
    lastInputGroup.space(pad10);
    lastInputGroup.addActor(currentInputLabel);
    lastInputGroup.addActor(currentInput);
    // File name
    OwnLabel fileLabel = new OwnLabel("Filename:", skin, "header");
    LengthValidator lv = new LengthValidator(3, 100);
    filename = new OwnTextField(this.controllerName.replaceAll("\\s+", "_"), skin, lv);
    filename.setWidth(384f);
    OwnImageButton filenameTooltip = new OwnImageButton(skin, "tooltip");
    filenameTooltip.addListener(new OwnTextTooltip("Filename without extension.\nThe controller file will be saved in " + SysUtils.getDefaultMappingsDir().toAbsolutePath(), skin));
    HorizontalGroup filenameGroup = new HorizontalGroup();
    filenameGroup.space(pad20);
    filenameGroup.addActor(fileLabel);
    filenameGroup.addActor(filename);
    filenameGroup.addActor(filenameTooltip);
    // Table with inputs and mappings
    Table inputTable = new Table(skin);
    Gamepad[] gpds = Gamepad.values();
    for (Gamepad gpd : gpds) {
        Trio<Texture, float[], String> t = inputInfo.get(gpd);
        inputTable.add(new OwnLabel(t.getThird() + ": ", skin, lw)).left().padBottom(pad5).padRight(pad10);
        OwnTextField inputField = new OwnTextField(getMappingsValue(gpd, mappings), skin);
        inputField.setWidth(iw);
        Color origCol = inputField.getColor().cpy();
        inputFields.put(gpd, inputField);
        inputField.addListener(event -> {
            if (event instanceof FocusListener.FocusEvent) {
                FocusListener.FocusEvent fe = (FocusListener.FocusEvent) event;
                if (fe.isFocused()) {
                    inputField.setColor(0.4f, 0.4f, 1f, 1f);
                    displayElement(gpd);
                    makeCurrent(gpd, inputField);
                } else {
                    inputField.setColor(origCol);
                    displayElement(null);
                    makeCurrent(null, null);
                }
                return true;
            }
            return false;
        });
        inputTable.add(inputField).left().padBottom(pad5).row();
    }
    // Sensitivity
    lsx = new OwnSlider(0.1f, 10f, 0.1f, skin);
    lsx.setValue((float) this.mappings.AXIS_LSTICK_H_SENS);
    lsx.setWidth(iw);
    lsy = new OwnSlider(0.1f, 10f, 0.1f, skin);
    lsy.setValue((float) this.mappings.AXIS_LSTICK_V_SENS);
    lsy.setWidth(iw);
    rsx = new OwnSlider(0.1f, 10f, 0.1f, skin);
    rsx.setValue((float) this.mappings.AXIS_RSTICK_H_SENS);
    rsx.setWidth(iw);
    rsy = new OwnSlider(0.1f, 10f, 0.1f, skin);
    rsy.setValue((float) this.mappings.AXIS_RSTICK_V_SENS);
    rsy.setWidth(iw);
    lts = new OwnSlider(0.1f, 10f, 0.1f, skin);
    lts.setValue((float) this.mappings.AXIS_LT_SENS);
    lts.setWidth(iw);
    rts = new OwnSlider(0.1f, 10f, 0.1f, skin);
    rts.setValue((float) this.mappings.AXIS_RT_SENS);
    rts.setWidth(iw);
    axisPower = new OwnSlider(0.1f, 10f, 0.1f, skin);
    axisPower.setColor(1f, 0.5f, 0.5f, 1f);
    axisPower.setValue((float) this.mappings.AXIS_VALUE_POW);
    axisPower.setWidth(iw);
    Table sensitivityTable01 = new Table(skin);
    Table sensitivityTable02 = new Table(skin);
    OwnLabel titleSensitivity = new OwnLabel(I18n.txt("gui.controller.sensitivity"), skin, "header-s");
    sensitivityTable01.add(new OwnLabel(I18n.txt("gui.controller.lstick") + " X:", skin, lw)).left().padRight(pad10).padBottom(pad5);
    sensitivityTable01.add(lsx).left().padBottom(pad5).row();
    sensitivityTable01.add(new OwnLabel(I18n.txt("gui.controller.lstick") + " Y:", skin, lw)).left().padRight(pad10).padBottom(pad5);
    sensitivityTable01.add(lsy).left().padBottom(pad5).row();
    sensitivityTable01.add(new OwnLabel(I18n.txt("gui.controller.rstick") + " X:", skin, lw)).left().padRight(pad10).padBottom(pad5);
    sensitivityTable01.add(rsx).left().padBottom(pad5).row();
    sensitivityTable01.add(new OwnLabel(I18n.txt("gui.controller.rstick") + " Y:", skin, lw)).left().padRight(pad10).padBottom(pad5);
    sensitivityTable01.add(rsy).left().padBottom(pad5).row();
    sensitivityTable02.add(new OwnLabel(I18n.txt("gui.controller.lt") + ":", skin, lw)).left().padRight(pad10).padBottom(pad5);
    sensitivityTable02.add(lts).left().padBottom(pad5).row();
    sensitivityTable02.add(new OwnLabel(I18n.txt("gui.controller.rt") + ":", skin, lw)).left().padRight(pad10).padBottom(pad20);
    sensitivityTable02.add(rts).left().padBottom(pad20).row();
    sensitivityTable02.add(new OwnLabel(I18n.txt("gui.controller.axis.pow") + ":", skin, lw)).left().padRight(pad10).padBottom(pad5);
    sensitivityTable02.add(axisPower).left();
    // Add inputs and the rest
    content.add(inputTable).left();
    content.row();
    content.add(lastInputGroup).padBottom(pad10);
    content.row();
    content.add(titleSensitivity).left().colspan(2).padBottom(pad10);
    content.add();
    content.row();
    content.add(sensitivityTable01).left().top();
    content.add(sensitivityTable02).left().top();
    content.row();
    content.add(filenameGroup).colspan(2).padTop(pad20);
    // Select first
    GaiaSky.postRunnable(() -> stage.setKeyboardFocus(inputFields.get(gpds[0])));
    content.pack();
}
Also used : SpriteDrawable(com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable) LengthValidator(gaiasky.util.validator.LengthValidator) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) Color(com.badlogic.gdx.graphics.Color) Texture(com.badlogic.gdx.graphics.Texture) FocusListener(com.badlogic.gdx.scenes.scene2d.utils.FocusListener)

Example 4 with LengthValidator

use of gaiasky.util.validator.LengthValidator in project gaiasky by langurmonkey.

the class KeyframesWindow method addFrameName.

private void addFrameName(Keyframe kf, int index, Table table) {
    // Seconds
    OwnLabel nameL = new OwnLabel((index + 1) + ": " + kf.name, skin);
    nameL.setWidth(160f);
    Cell<?> nameCell;
    if (namesCells.containsKey(kf))
        nameCell = namesCells.get(kf);
    else {
        nameCell = table.add();
        namesCells.put(kf, nameCell);
    }
    nameCell.clearActor();
    nameCell.setActor(nameL).left().padRight(pad10 / 2f).padBottom(pad5);
    keyframeNames.put(kf, nameL);
    nameL.addListener(new OwnTextTooltip(I18n.txt("gui.tooltip.kf.name"), skin));
    nameL.addListener((event) -> {
        if (event instanceof InputEvent) {
            InputEvent ie = (InputEvent) event;
            if (ie.getType() == InputEvent.Type.touchDown) {
                if (editing.notEmpty()) {
                    // Remove current
                    editing.revert();
                    editing.unset();
                }
                String valText = nameL.getText().toString();
                valText = valText.substring(valText.indexOf(":") + 2);
                nameL.clear();
                keyframeNames.remove(kf);
                namesCells.get(kf).clearActor();
                LengthValidator lengthValidator = new LengthValidator(0, 15);
                RegexpValidator nameValidator = new RegexpValidator(lengthValidator, "^[^*&%\\+\\=\\\\\\/@#\\$&\\*()~]*$");
                OwnTextField nameInput = new OwnTextField(valText, skin, nameValidator);
                nameInput.setWidth(160f);
                nameInput.selectAll();
                stage.setKeyboardFocus(nameInput);
                editing.setName(kf, index, nameInput);
                nameInput.addListener((evt) -> {
                    if (nameInput.isValid() && evt instanceof InputEvent && System.currentTimeMillis() - lastMs > 1500) {
                        InputEvent ievt = (InputEvent) evt;
                        if (ievt.getType() == InputEvent.Type.keyDown && (ievt.getKeyCode() == Input.Keys.ENTER || ievt.getKeyCode() == Input.Keys.ESCAPE)) {
                            kf.name = nameInput.getText();
                            addFrameName(kf, index, table);
                            editing.unset();
                        }
                    }
                    evt.setBubbles(false);
                    return true;
                });
                namesCells.get(kf).setActor(nameInput);
                lastMs = System.currentTimeMillis();
            }
        }
        return true;
    });
    addHighlightListener(nameL, kf);
}
Also used : LengthValidator(gaiasky.util.validator.LengthValidator) InputEvent(com.badlogic.gdx.scenes.scene2d.InputEvent) RegexpValidator(gaiasky.util.validator.RegexpValidator)

Aggregations

LengthValidator (gaiasky.util.validator.LengthValidator)4 RegexpValidator (gaiasky.util.validator.RegexpValidator)3 Color (com.badlogic.gdx.graphics.Color)1 Texture (com.badlogic.gdx.graphics.Texture)1 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)1 InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)1 ChangeEvent (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent)1 FocusListener (com.badlogic.gdx.scenes.scene2d.utils.FocusListener)1 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)1 Keyframe (gaiasky.desktop.util.camera.Keyframe)1 Vector3d (gaiasky.util.math.Vector3d)1 OwnLabel (gaiasky.util.scene2d.OwnLabel)1 OwnTextField (gaiasky.util.scene2d.OwnTextField)1 FloatValidator (gaiasky.util.validator.FloatValidator)1 Date (java.util.Date)1