Skip to content

Commit c33c24e

Browse files
committed
fix dialog text options to wrap to screen.
1 parent 197f60d commit c33c24e

File tree

1 file changed

+58
-14
lines changed

1 file changed

+58
-14
lines changed

blade-engine/src/com/bladecoder/engine/ui/DialogUI.java

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import com.badlogic.gdx.graphics.Color;
2121
import com.badlogic.gdx.graphics.g2d.Batch;
2222
import com.badlogic.gdx.graphics.g2d.BitmapFont;
23+
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
2324
import com.badlogic.gdx.scenes.scene2d.Actor;
2425
import com.badlogic.gdx.scenes.scene2d.InputEvent;
2526
import com.badlogic.gdx.scenes.scene2d.InputListener;
2627
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
28+
import com.badlogic.gdx.utils.Align;
2729
import com.bladecoder.engine.i18n.I18N;
2830
import com.bladecoder.engine.model.DialogOption;
2931
import com.bladecoder.engine.model.World;
@@ -37,6 +39,8 @@ public class DialogUI extends Actor {
3739
private Recorder recorder;
3840

3941
private int selected = -1;
42+
43+
private final GlyphLayout layout = new GlyphLayout();
4044

4145
public DialogUI(SceneScreen scr) {
4246
style = scr.getUI().getSkin().get(DialogUIStyle.class);
@@ -81,6 +85,11 @@ public void act(float delta) {
8185
} else if( !isVisible() && World.getInstance().getCurrentDialog() != null && !World.getInstance().inCutMode()) {
8286
setVisible(true);
8387
}
88+
89+
if(isVisible()) {
90+
setWidth(getStage().getViewport().getScreenWidth());
91+
setHeight(calcHeight());
92+
}
8493
}
8594

8695
@Override
@@ -100,45 +109,80 @@ else if (options.size() == 1) { // If only has one option,
100109
return;
101110
}
102111

103-
float lineHeight = style.font.getLineHeight();
104-
float y = lineHeight * options.size();
105-
setWidth(getStage().getViewport().getScreenWidth());
106112
float margin = DPIUtils.getMarginSize();
107-
setHeight( margin + lineHeight * options.size());
113+
float y = margin;
108114

109115
if (style.background != null) {
110116
style.background.draw(batch, getX(), getY(), getWidth(), getHeight());
111117
}
112118

113-
for (int i = 0; i < options.size(); i++) {
119+
for (int i = options.size() - 1; i >= 0; i--) {
114120
DialogOption o = options.get(i);
115121
String str = o.getText();
116122

117123
if (str.charAt(0) == '@')
118124
str = I18N.getString(str.substring(1));
119125

120126
if (i == selected) {
121-
style.font.setColor(style.overFontColor);
122-
style.font.draw(batch, str, margin, y);
127+
layout.setText(style.font, str, style.overFontColor, getWidth() - margin * 2, Align.left, true);
123128
} else {
124-
style.font.setColor(style.fontColor);
125-
style.font.draw(batch, str, margin, y);
129+
layout.setText(style.font, str, style.fontColor, getWidth() - margin * 2, Align.left, true);
126130
}
127131

128-
y -= lineHeight;
132+
y += layout.height - style.font.getDescent() + style.font.getAscent();
133+
style.font.draw(batch, layout, margin, y);
129134
}
130135
}
136+
137+
private float calcHeight() {
138+
float height = 0;
139+
float margin = DPIUtils.getMarginSize();
140+
141+
ArrayList<DialogOption> options = World.getInstance()
142+
.getCurrentDialog().getVisibleOptions();
143+
144+
for (int i = 0; i < options.size(); i++) {
145+
DialogOption o = options.get(i);
146+
String str = o.getText();
147+
148+
if (str.charAt(0) == '@')
149+
str = I18N.getString(str.substring(1));
150+
layout.setText(style.font, str, style.overFontColor, getStage().getViewport().getScreenWidth() - margin * 2, Align.left, true);
151+
height += layout.height - style.font.getDescent() + style.font.getAscent();
152+
}
153+
154+
return height + margin * 2;
155+
}
131156

132157
private int getOption(float x, float y) {
133158
if(World.getInstance().getCurrentDialog() == null)
134159
return -1;
135160

136-
float lineHeight = style.font.getLineHeight();
161+
ArrayList<DialogOption> options = World.getInstance()
162+
.getCurrentDialog().getVisibleOptions();
163+
164+
float margin = DPIUtils.getMarginSize();
165+
float oy = margin;
166+
167+
int selectedLine = 0;
168+
169+
for (int i = options.size() - 1; i >= 0; i--) {
170+
DialogOption o = options.get(i);
171+
String str = o.getText();
137172

138-
int selectedLine = (int) (y / lineHeight);
173+
if (str.charAt(0) == '@')
174+
str = I18N.getString(str.substring(1));
175+
176+
layout.setText(style.font, str, style.overFontColor, getStage().getViewport().getScreenWidth() - margin * 2, Align.left, true);
177+
oy += layout.height - style.font.getDescent() + style.font.getAscent();
178+
179+
if(oy > y) {
180+
selectedLine = i;
181+
break;
182+
}
183+
}
139184

140-
return World.getInstance().getCurrentDialog().getNumVisibleOptions()
141-
- selectedLine - 1;
185+
return selectedLine;
142186
}
143187

144188
private void select(int i) {

0 commit comments

Comments
 (0)