Skip to content

Commit

Permalink
FEATURE: print styled (bold, fixed, italic) text if the game requests…
Browse files Browse the repository at this point in the history
… it.
  • Loading branch information
onyxbits committed Jan 3, 2014
1 parent 231a07e commit 0f660a0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 10 deletions.
38 changes: 35 additions & 3 deletions src/de/onyxbits/textfiction/GameActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
import de.onyxbits.textfiction.input.InputFragment;
import de.onyxbits.textfiction.input.WordExtractor;
import de.onyxbits.textfiction.zengine.GrueException;
import de.onyxbits.textfiction.zengine.StyleRegion;
import de.onyxbits.textfiction.zengine.ZMachine;
import de.onyxbits.textfiction.zengine.ZState;
import de.onyxbits.textfiction.zengine.ZStatus;
import de.onyxbits.textfiction.zengine.ZWindow;

import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
Expand All @@ -35,6 +40,7 @@
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;

/**
Expand Down Expand Up @@ -302,7 +308,7 @@ public void executeCommand(byte[] inputBuffer) {
if (retainerFragment.engine.getRunState() != ZMachine.STATE_WAIT_CHAR) {
String tmp = new String(inputBuffer).replaceAll("\n", "").trim();
retainerFragment.messageBuffer.add(new StoryItem(
new String(tmp).trim(), StoryItem.MYSELF));
new SpannableString(tmp), StoryItem.MYSELF));
}
try {
retainerFragment.engine.run();
Expand All @@ -323,7 +329,7 @@ public void publishResult() {
ZWindow upper = retainerFragment.engine.window[1];
ZWindow lower = retainerFragment.engine.window[0];
ZStatus status = retainerFragment.engine.status_line;
String tmp;
String tmp="";
boolean showLower = false;

if (status != null) {
Expand All @@ -347,8 +353,34 @@ public void publishResult() {

if (lower.cursor > 0) {
tmp = new String(lower.frameBuffer, 0, lower.noPrompt());
SpannableString stmp = new SpannableString(tmp);
StyleRegion reg = lower.regions;
if (reg!=null) {
while(reg!=null) {
if (reg.next==null) {
// The printer does not "close" the last style since it doesn't know
// when the last character is printed.
reg.end=tmp.length()-1;
}
switch (reg.style) {
case ZWindow.BOLD: {
stmp.setSpan(new StyleSpan(Typeface.BOLD),reg.start,reg.end,0);
break;
}
case ZWindow.ITALIC: {
stmp.setSpan(new StyleSpan(Typeface.ITALIC),reg.start,reg.end,0);
break;
}
case ZWindow.FIXED: {
stmp.setSpan(new TypefaceSpan("monospace"),reg.start,reg.end,0);
break;
}
}
reg=reg.next;
}
}
retainerFragment.messageBuffer
.add(new StoryItem(tmp, StoryItem.NARRATOR));
.add(new StoryItem(stmp, StoryItem.NARRATOR));
showLower = true;
}
lower.retrieved();
Expand Down
6 changes: 4 additions & 2 deletions src/de/onyxbits/textfiction/StoryItem.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.onyxbits.textfiction;

import android.text.SpannableString;

/**
* A story element to be shown in the story fragment.
*
Expand All @@ -26,14 +28,14 @@ class StoryItem {
/**
* What to show to the user
*/
protected String message;
protected SpannableString message;

/**
* Is this a message from the player or the narrator?
*/
protected int type;

public StoryItem(String message, int type) {
public StoryItem(SpannableString message, int type) {
this.message = message;
this.type = type;
}
Expand Down
21 changes: 21 additions & 0 deletions src/de/onyxbits/textfiction/zengine/StyleRegion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.onyxbits.textfiction.zengine;

/**
* A simple datacontainer for applying textstyles (bold, italic, etc) to
* text printed in a window
* @author patrick
*
*/
public class StyleRegion {

public int style= ZWindow.ROMAN;
public int start=0;
public int end=0;

public StyleRegion next;

public StyleRegion() {
// TODO Auto-generated constructor stub
}

}
27 changes: 22 additions & 5 deletions src/de/onyxbits/textfiction/zengine/ZWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import java.util.Arrays;

public class ZWindow {
final static int ROMAN = 0;
final static int REVERSE = 1;
final static int BOLD = 2;
final static int ITALIC = 4;
final static int FIXED = 8;
public final static int ROMAN = 0;
public final static int REVERSE = 1;
public final static int BOLD = 2;
public final static int ITALIC = 4;
public final static int FIXED = 8;

final static char FIRST_STYLE = '\u8000';
final static char BUF_ROMAN = (char) (FIRST_STYLE + ROMAN);
Expand Down Expand Up @@ -46,6 +46,7 @@ public class ZWindow {

public boolean upper;
public char[] frameBuffer;
public StyleRegion regions;
public int cursor;
public int maxCursor;
public int endWindow;
Expand Down Expand Up @@ -186,6 +187,21 @@ public void set_color(int foreground, int background) {
}

public void set_text_style(int style) {
StyleRegion region = new StyleRegion();
region.style=style;
region.start=cursor;
region.end=cursor;
if (regions==null) {
regions=region;
}
else {
StyleRegion tmp = regions;
while(tmp.next!=null) {
tmp=tmp.next;
}
tmp.end=cursor;
tmp.next=region;
}
}

public int getHeight() {
Expand Down Expand Up @@ -214,6 +230,7 @@ public void retrieved() {
cursory = 0;
cursor = 0;
}
regions=null;
}

/**
Expand Down

0 comments on commit 0f660a0

Please sign in to comment.