X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Forg%2Fpileus%2Fspades%2FMain.java;h=c7ef308e69f24dd72d9f7f0c33977a6c4b337de1;hb=3a5e53423f08666e94d02f1c427d3cbce0f661c0;hp=502d5ae9b735e8da076d26461fe84be0c66cc0fd;hpb=65de4b5e7e794a58727dac5eff40e484a5380285;p=~andy%2Fspades diff --git a/src/org/pileus/spades/Main.java b/src/org/pileus/spades/Main.java index 502d5ae..c7ef308 100644 --- a/src/org/pileus/spades/Main.java +++ b/src/org/pileus/spades/Main.java @@ -2,12 +2,20 @@ package org.pileus.spades; import android.app.Activity; import android.content.Intent; +import android.graphics.Color; +import android.graphics.Typeface; import android.os.Bundle; import android.os.Handler; import android.os.Messenger; -import android.text.Html; -import android.text.method.ScrollingMovementMethod; +import android.preference.PreferenceManager; +import android.text.Spannable; +import android.text.SpannableString; import android.text.format.DateFormat; +import android.text.style.BackgroundColorSpan; +import android.text.style.ForegroundColorSpan; +import android.text.style.StrikethroughSpan; +import android.text.style.StyleSpan; +import android.text.style.UnderlineSpan; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -32,6 +40,7 @@ public class Main extends Activity private String topic; private String names; private Cards cards; + private Spades game; /* Widgets */ private TabHost window; @@ -47,73 +56,93 @@ public class Main extends Activity private ScrollView dscroll; /* Private helper methods */ + private int hsv2rgb(int hsv) + { + int h = (hsv & 0xff0000) >> 16; + int s = (hsv & 0x00ff00) >> 8; + int v = (hsv & 0x0000ff) >> 0; + + int c = (v * s) / 256; + int h1 = (h * 6) / 256; + int x = c * (1 - Math.abs((h1%2)-1)); + int m = v - c; + + int rgb = 0; + + if (0 <= h1 && h1 <= 1) rgb = (c << 16) | (x << 8) | 0; + if (1 <= h1 && h1 <= 2) rgb = (x << 16) | (c << 8) | 0; + if (2 <= h1 && h1 <= 3) rgb = (0 << 16) | (c << 8) | x; + if (3 <= h1 && h1 <= 4) rgb = (0 << 16) | (x << 8) | c; + if (4 <= h1 && h1 <= 5) rgb = (x << 16) | (0 << 8) | c; + if (5 <= h1 && h1 <= 6) rgb = (c << 16) | (0 << 8) | x; + + return rgb + (m << 16) + (m << 8) + m; + } + private void notice(String text) { - this.log.append(Html.fromHtml("*** " + text + "
")); + String msg = "*** " + text + "\n"; + Spannable span = new SpannableString(msg); + span.setSpan(new StyleSpan(Typeface.BOLD), 0, msg.length(), 0); + this.log.append(span); } private void display(Message msg) { - String when = DateFormat.format("hh:mm:ss", msg.time).toString(); - String from = String.format("%s", msg.from); - String text = msg.msg; - String fmt = null; - - // Do IRC Colors - only partly works - String fg = "\">"; - text = text - .replaceAll("&", "&") - .replaceAll("<", "<") - .replaceAll(">", ">"); - text = text - .replaceAll("\\002", "") // bold - .replaceAll("\\011", "") // italic - .replaceAll("\\025", ""); // underline - text = text - .replaceAll("\\003(\\d+)(,\\d+)?", fg) // color - .replaceAll("\\013(\\d+)(,\\d+)?", fg); // color - text = text - .replaceAll("<0?0>", "#000000") // White - .replaceAll("<0?1>", "#000000") // Black - .replaceAll("<0?2>", "#000080") // Navy Blue - .replaceAll("<0?3>", "#008000") // Green - .replaceAll("<0?4>", "#FF0000") // Red - .replaceAll("<0?5>", "#804040") // Brown - .replaceAll("<0?6>", "#8000FF") // Purple - .replaceAll("<0?7>", "#808000") // Olive - .replaceAll("<0?8>", "#FFFF00") // Yellow - .replaceAll("<0?9>", "#00FF00") // Lime Green - .replaceAll("<10>", "#008080") // Teal - .replaceAll("<11>", "#00FFFF") // Aqua Light - .replaceAll("<12>", "#0000FF") // Royal Blue - .replaceAll("<13>", "#FF00FF") // Hot Pink - .replaceAll("<14>", "#808080") // Dark Gray - .replaceAll("<15>", "#C0C0C0"); // Light Gray - - // Message formatting - switch (msg.how) { - case DIRECT: - case MENTION: - case PRIVMSG: - fmt = "(%s) %s: %s"; - break; - case SENT: - fmt = "(%s) %s: %s"; - break; - default: - fmt = "(%s) %s: %s"; - break; + String date = DateFormat.format("hh:mm:ss", msg.time).toString(); + String text = String.format("(%s) %s: %s\n", date, msg.from, msg.msg); + Spannable span = new SpannableString(text); + + // Determin positions + int de = 1 + date.length() + 1; + int ne = de + 1 + msg.from.length() + 1; + int pos = ne + 1; + + // Get user color + int hash = msg.from.hashCode(); + int color = this.hsv2rgb(hash | 0x8080) | 0xff000000; + + // Format date and name + span.setSpan(new ForegroundColorSpan(0xffffff88), 0, de, 0); + span.setSpan(new ForegroundColorSpan(color), de+1, ne, 0); + + // Format IRC Colors + for (Message.Format fmt : msg.parts) { + int len = fmt.txt.length(); + + // Bold/italics + if (fmt.bold && fmt.italic) + span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), pos, pos+len, 0); + else if (fmt.bold) + span.setSpan(new StyleSpan(Typeface.BOLD), pos, pos+len, 0); + else if (fmt.italic) + span.setSpan(new StyleSpan(Typeface.ITALIC), pos, pos+len, 0); + + // Striketrough / underline + if (fmt.strike) + span.setSpan(new StrikethroughSpan(), pos, pos+len, 0); + if (fmt.underline) + span.setSpan(new UnderlineSpan(), pos, pos+len, 0); + + // Colors (reverse not supported) + if (fmt.fg!=null) + span.setSpan(new ForegroundColorSpan(fmt.fg.color), pos, pos+len, 0); + if (fmt.bg!=null) + span.setSpan(new BackgroundColorSpan(fmt.bg.color), pos, pos+len, 0); + + pos += len; } - String html = String.format(fmt, when, from, text); - this.log.append(Html.fromHtml(html + "
")); + // Append the message + this.log.append(span); } /* Private handler methods */ private void onRegister(Task task) { Os.debug("Main: onRegister"); - this.task = task; + this.task = task; + this.game.task = task; this.running = this.task.isRunning(); for (Object obj : this.task.getLog()) { if (String.class.isInstance(obj)) @@ -133,6 +162,7 @@ public class Main extends Activity switch (msg.type) { case PRIVMSG: this.display(msg); + this.game.onMessage(msg); break; case TOPIC: if (!msg.txt.equals(this.topic)) @@ -187,11 +217,16 @@ public class Main extends Activity Os.debug("Main: disconnect"); startService(new Intent(this, Task.class) .putExtra("Command", Task.DISCONNECT)); + this.running = false; } - private void exit() + private void quit() { stopService(new Intent(this, Task.class)); + Intent intent = new Intent(Intent.ACTION_MAIN); + intent.addCategory(Intent.CATEGORY_HOME); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(intent); } /* Widget callback functions */ @@ -214,6 +249,9 @@ public class Main extends Activity super.onCreate(savedInstanceState); Os.debug("Main: onCreate"); + // Setup preferences + PreferenceManager.setDefaultValues(this, R.xml.prefs, false); + // Setup main layout this.setContentView(R.layout.main); @@ -253,10 +291,17 @@ public class Main extends Activity .setIndicator("Debug") .setContent(R.id.debug)); - // Setup OpenGL view + // Setup Spades game and cards view + this.game = new Spades(PreferenceManager + .getDefaultSharedPreferences(this) + .getString("pref_referee", "rhawk")); this.cards = new Cards(this); + + this.game.cards = this.cards; + this.cards.game = this.game; + this.spades.addView(cards); - + // Attach to background service this.register(); @@ -337,9 +382,8 @@ public class Main extends Activity case R.id.settings: this.startActivity(new Intent(this, Prefs.class)); return true; - case R.id.exit: - this.exit(); - this.finish(); + case R.id.quit: + this.quit(); return true; default: return false;