001package squidpony.squidgrid.gui.gdx; 002 003import java.util.List; 004 005import com.badlogic.gdx.Gdx; 006import com.badlogic.gdx.graphics.Color; 007 008import squidpony.SquidTags; 009import squidpony.panel.IColoredString; 010 011/** 012 * A less abstract version of {@link AbstractTextScreen} tuned for libgdx 013 * {@link Color}. It uses a {@link GroupCombinedPanel} for display. 014 * 015 * <p> 016 * Here's an example of this class in action: 017 * 018 * <img src="http://i.imgur.com/g34bHvo.png"></img> 019 * </p> 020 * 021 * @author smelC 022 * 023 * @see AbstractTextScreen 024 */ 025public abstract class TextScreen extends AbstractTextScreen<Color> { 026 027 protected /* @Nullable */ GroupCombinedPanel<Color> gcp; 028 029 /** 030 * Doc: see 031 * {@link AbstractTextScreen#AbstractTextScreen(squidpony.squidgrid.gui.gdx.AbstractSquidScreen.SquidScreenInput, List, int[])} 032 * . 033 */ 034 public TextScreen(SquidScreenInput<Color> si, /* @Nullable */List<IColoredString<Color>> text, 035 /* @Nullable */ int[] alignment) { 036 super(si, text, alignment); 037 } 038 039 @Override 040 public void render(float delta) { 041 if (gcp != null) 042 /* Job done already */ 043 return; 044 045 gcp = new GroupCombinedPanel<>(buildScreenWideSquidPanel(), buildScreenWideSquidPanel()); 046 047 final int width = gcp.getGridWidth(); 048 final int height = gcp.getGridHeight(); 049 050 if (text == null) { 051 Gdx.app.log(SquidTags.SCREEN, 052 "Cannot display null list of text in " + getClass().getSimpleName()); 053 return; 054 } 055 056 int y = 0; 057 for (IColoredString<Color> ics : text) { 058 if (height <= y) 059 /* Outside the screen */ 060 break; 061 062 if (ics == null) { 063 /* An empty line */ 064 y++; 065 continue; 066 } 067 068 /* the alignment for 'ics' */ 069 final int a; 070 if (alignment == null) 071 /* default is left */ 072 a = -1; 073 else { 074 if (y < alignment.length) { 075 final int b = alignment[y]; 076 if (-1 <= b && b <= 1) 077 a = b; 078 else { 079 Gdx.app.log(SquidTags.SCREEN, 080 "Unrecognized alignment in " + getClass().getSimpleName() + ":" + b 081 + ". Expected -1, 0, or 1. Defaulting to -1 (left)"); 082 /* default is left */ 083 a = -1; 084 } 085 } else { 086 /* default is left */ 087 a = -1; 088 } 089 } 090 final int x; 091 if (a == -1) 092 /* left */ 093 x = 0; 094 else if (a == 0) { 095 /* center */ 096 final int len = ics.length(); 097 if (width <= len) 098 /* String too large, it'll get cut */ 099 x = 0; 100 else 101 x = width - (ics.length() / 2); 102 } else { 103 /* right */ 104 assert a == 1; 105 final int len = ics.length(); 106 x = len < width ? width - len : 0; 107 } 108 109 gcp.putFG(x, y, ics); 110 y++; 111 } 112 113 if (stage == null) 114 /* Looks like we need to build it */ 115 stage = buildStage(); 116 117 stage.addActor(gcp); 118 stage.draw(); 119 } 120 121}