Skip to content

Instantly share code, notes, and snippets.

@electron0zero
Created August 31, 2017 19:16
Show Gist options
  • Save electron0zero/6db99a706a890e20710ed65ccc034f83 to your computer and use it in GitHub Desktop.
Save electron0zero/6db99a706a890e20710ed65ccc034f83 to your computer and use it in GitHub Desktop.
superscript text in libgdx and Handling of Computer scintific notation

parses number written in Computer Scientific Notation and returns an String Array containing Elements of Scientific Notation

    /**
     * This Function parses number written in Computer Scientific Notation and returns
     * an String Array containing Elements of Scientific Notation
     * value: value in scientific format like 1.0005E-25 or 1.0005E25
     * Return String Array with Following thing
     * String[0] = multiplier section(example [10.2 * ] )
     * String[1] = base (example [10] )
     * String[2] = power (example [-2] )
     *
     * this example represents 10.2 * 10^-2
     * */
    public static String[] makeEtoPower(float value){
        String[] return_value;

        if (Float.isInfinite(value)){
            Gdx.app.error(TAG, "Passed value is Infinite, value: " + value);
            return_value = new String[]{"Inf.", "", " "};
        } else if (Float.isNaN(value)){
            Gdx.app.error(TAG, "Passed value is Not a Number, value: " + value);
            return_value = new String[]{"NaN", "", " "};
        } else { // we have a valid value, parse it
            // match all the values in scientific format like 1.0005E-25 or 1.0005E25
            Matcher match = Pattern.compile("-?\\d+(\\.\\d+)?").matcher(String.valueOf(value));
            ArrayList<String> matches = new ArrayList<String>();
            while(match.find()) {
                matches.add(match.group());
            }
            try {
                double val = Double.valueOf(matches.get(0));
                String multiplier = String.valueOf(Math.round(val * 100.0)/ 100.0f); // round to two decimal place
                multiplier = multiplier + " * "; // append Multiply Sign
                String base = "10";
                String power = matches.get(1);
                return_value = new String[]{multiplier, base, power};

            } catch (IndexOutOfBoundsException e){
                // In this case we got passed a value in non Scientific notation
                // So we will send that back in multiplier field of array
                Gdx.app.error(TAG, "Caught IndexOutOfBoundsException - RegEx can not parse provided value, reporting value as Infinite, value: " + value);
                return_value = new String[]{String.valueOf(value), "", ""};
            }
        }
        return return_value;
}

SuperScript

    public static Group createSuperScriptText(String preText, String[] EtoPower,
                                        String postText, Label.LabelStyle labelStyle) {
        // Group is Primitive Type and does not handle it's own Height, Width
        // and  positing of it's elements
        // So Here we manage position the elements of group in relative to each other
        // and Set the height and width of group before returning it.
        // We set Height and width of group so the containing Table can position it properly

        Group group = new Group();
        // group.debugAll();

        // Text before the superScript Text
        Label preLabel = new Label(preText, labelStyle);
        group.addActor(preLabel);

        // EtoPower is String array with following things
        // String[0] = multiplier section(example [10.2 * ] )
        // String[1] = base (example [10] )
        // String[2] = power (example [-2] )

        // multiplier Text
        Label multiplierLabel = new Label(EtoPower[0], labelStyle);
        multiplierLabel.setPosition(preLabel.getX() + preLabel.getPrefWidth(), preLabel.getY());
        group.addActor(multiplierLabel);

        // SuperScript Text
        // SuperScript Base Text
        Label baseLabel = new Label(EtoPower[1], labelStyle);
        baseLabel.setPosition(multiplierLabel.getX() + multiplierLabel.getPrefWidth(), multiplierLabel.getY());
        group.addActor(baseLabel);

        // SuperScript power Text
        float superscriptOFFSET = baseLabel.getPrefHeight()/3; // how high is superscript
        float superscriptSCALE = 0.80f;
        Label powerLabel = new Label(EtoPower[2], labelStyle);
        powerLabel.setFontScale(superscriptSCALE);
        powerLabel.setPosition(baseLabel.getX() + baseLabel.getPrefWidth(), baseLabel.getY() + superscriptOFFSET);
        group.addActor(powerLabel);

        // Something we want to write after the SuperScript Text
        Label postLabel = new Label(postText, labelStyle);
        postLabel.setPosition(powerLabel.getX() + powerLabel.getPrefWidth(), baseLabel.getY());
        group.addActor(postLabel);

        group.setHeight(baseLabel.getPrefHeight() + superscriptOFFSET);
        group.setWidth(preLabel.getPrefWidth() + multiplierLabel.getPrefWidth() + baseLabel.getPrefWidth() +
                powerLabel.getPrefWidth() + postLabel.getPrefWidth());

        return group;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment