gdx-studio
git clone https://git.pyrossh.dev/gdx-studio
An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop
src_libs/com/badlogic/gdx/tools/hiero/unicodefont/effects/GradientEffect.java
| a62d533 | 1 | /******************************************************************************* |
| a62d533 | 2 | * Copyright 2011 See AUTHORS file. |
| a62d533 | 3 | * |
| a62d533 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| a62d533 | 5 | * you may not use this file except in compliance with the License. |
| a62d533 | 6 | * You may obtain a copy of the License at |
| a62d533 | 7 | * |
| a62d533 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| a62d533 | 9 | * |
| a62d533 | 10 | * Unless required by applicable law or agreed to in writing, software |
| a62d533 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| a62d533 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| a62d533 | 13 | * See the License for the specific language governing permissions and |
| a62d533 | 14 | * limitations under the License. |
| a62d533 | 15 | ******************************************************************************/ |
| a62d533 | 16 | |
| a62d533 | 17 | package com.badlogic.gdx.tools.hiero.unicodefont.effects; |
| a62d533 | 18 | |
| a62d533 | 19 | import java.awt.Color; |
| a62d533 | 20 | import java.awt.GradientPaint; |
| a62d533 | 21 | import java.awt.Graphics2D; |
| a62d533 | 22 | import java.awt.image.BufferedImage; |
| a62d533 | 23 | import java.util.ArrayList; |
| a62d533 | 24 | import java.util.Iterator; |
| a62d533 | 25 | import java.util.List; |
| a62d533 | 26 | |
| a62d533 | 27 | import com.badlogic.gdx.tools.hiero.unicodefont.Glyph; |
| a62d533 | 28 | import com.badlogic.gdx.tools.hiero.unicodefont.UnicodeFont; |
| a62d533 | 29 | |
| a62d533 | 30 | /** Paints glyphs with a gradient fill. |
| a62d533 | 31 | * @author Nathan Sweet */ |
| a62d533 | 32 | public class GradientEffect implements ConfigurableEffect {
|
| a62d533 | 33 | private Color topColor = Color.cyan, bottomColor = Color.blue; |
| a62d533 | 34 | private int offset = 0; |
| a62d533 | 35 | private float scale = 1; |
| a62d533 | 36 | private boolean cyclic; |
| a62d533 | 37 | |
| a62d533 | 38 | public GradientEffect () {
|
| a62d533 | 39 | } |
| a62d533 | 40 | |
| a62d533 | 41 | public GradientEffect (Color topColor, Color bottomColor, float scale) {
|
| a62d533 | 42 | this.topColor = topColor; |
| a62d533 | 43 | this.bottomColor = bottomColor; |
| a62d533 | 44 | this.scale = scale; |
| a62d533 | 45 | } |
| a62d533 | 46 | |
| a62d533 | 47 | public void draw (BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph) {
|
| a62d533 | 48 | int ascent = unicodeFont.getAscent(); |
| a62d533 | 49 | float height = (ascent) * scale; |
| a62d533 | 50 | float top = -glyph.getYOffset() + unicodeFont.getDescent() + offset + ascent / 2 - height / 2; |
| a62d533 | 51 | g.setPaint(new GradientPaint(0, top, topColor, 0, top + height, bottomColor, cyclic)); |
| a62d533 | 52 | g.fill(glyph.getShape()); |
| a62d533 | 53 | } |
| a62d533 | 54 | |
| a62d533 | 55 | public Color getTopColor () {
|
| a62d533 | 56 | return topColor; |
| a62d533 | 57 | } |
| a62d533 | 58 | |
| a62d533 | 59 | public void setTopColor (Color topColor) {
|
| a62d533 | 60 | this.topColor = topColor; |
| a62d533 | 61 | } |
| a62d533 | 62 | |
| a62d533 | 63 | public Color getBottomColor () {
|
| a62d533 | 64 | return bottomColor; |
| a62d533 | 65 | } |
| a62d533 | 66 | |
| a62d533 | 67 | public void setBottomColor (Color bottomColor) {
|
| a62d533 | 68 | this.bottomColor = bottomColor; |
| a62d533 | 69 | } |
| a62d533 | 70 | |
| a62d533 | 71 | public int getOffset () {
|
| a62d533 | 72 | return offset; |
| a62d533 | 73 | } |
| a62d533 | 74 | |
| a62d533 | 75 | /** Sets the pixel offset to move the gradient up or down. The gradient is normally centered on the glyph. */ |
| a62d533 | 76 | public void setOffset (int offset) {
|
| a62d533 | 77 | this.offset = offset; |
| a62d533 | 78 | } |
| a62d533 | 79 | |
| a62d533 | 80 | public float getScale () {
|
| a62d533 | 81 | return scale; |
| a62d533 | 82 | } |
| a62d533 | 83 | |
| a62d533 | 84 | /** Changes the height of the gradient by a percentage. The gradient is normally the height of most glyphs in the font. */ |
| a62d533 | 85 | public void setScale (float scale) {
|
| a62d533 | 86 | this.scale = scale; |
| a62d533 | 87 | } |
| a62d533 | 88 | |
| a62d533 | 89 | public boolean isCyclic () {
|
| a62d533 | 90 | return cyclic; |
| a62d533 | 91 | } |
| a62d533 | 92 | |
| a62d533 | 93 | /** If set to true, the gradient will repeat. */ |
| a62d533 | 94 | public void setCyclic (boolean cyclic) {
|
| a62d533 | 95 | this.cyclic = cyclic; |
| a62d533 | 96 | } |
| a62d533 | 97 | |
| a62d533 | 98 | public String toString () {
|
| a62d533 | 99 | return "Gradient"; |
| a62d533 | 100 | } |
| a62d533 | 101 | |
| a62d533 | 102 | public List getValues () {
|
| a62d533 | 103 | List values = new ArrayList(); |
| a62d533 | 104 | values.add(EffectUtil.colorValue("Top color", topColor));
|
| a62d533 | 105 | values.add(EffectUtil.colorValue("Bottom color", bottomColor));
|
| a62d533 | 106 | values.add(EffectUtil.intValue("Offset", offset,
|
| a62d533 | 107 | "This setting allows you to move the gradient up or down. The gradient is normally centered on the glyph.")); |
| a62d533 | 108 | values.add(EffectUtil.floatValue("Scale", scale, 0, 10, "This setting allows you to change the height of the gradient by a"
|
| a62d533 | 109 | + "percentage. The gradient is normally the height of most glyphs in the font.")); |
| a62d533 | 110 | values.add(EffectUtil.booleanValue("Cyclic", cyclic, "If this setting is checked, the gradient will repeat."));
|
| a62d533 | 111 | return values; |
| a62d533 | 112 | } |
| a62d533 | 113 | |
| a62d533 | 114 | public void setValues (List values) {
|
| a62d533 | 115 | for (Iterator iter = values.iterator(); iter.hasNext();) {
|
| a62d533 | 116 | Value value = (Value)iter.next(); |
| a62d533 | 117 | if (value.getName().equals("Top color")) {
|
| a62d533 | 118 | topColor = (Color)value.getObject(); |
| a62d533 | 119 | } else if (value.getName().equals("Bottom color")) {
|
| a62d533 | 120 | bottomColor = (Color)value.getObject(); |
| a62d533 | 121 | } else if (value.getName().equals("Offset")) {
|
| a62d533 | 122 | offset = ((Integer)value.getObject()).intValue(); |
| a62d533 | 123 | } else if (value.getName().equals("Scale")) {
|
| a62d533 | 124 | scale = ((Float)value.getObject()).floatValue(); |
| a62d533 | 125 | } else if (value.getName().equals("Cyclic")) {
|
| a62d533 | 126 | cyclic = ((Boolean)value.getObject()).booleanValue(); |
| a62d533 | 127 | } |
| a62d533 | 128 | } |
| a62d533 | 129 | } |
| a62d533 | 130 | } |