X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Forg%2Fpileus%2Fspades%2FMain.java;h=e4bac7fc96d83284d25c7aea8df8254dd6572c07;hb=7d4709a5ae7e3150d182a643cc6711568c4edbfd;hp=ebca43920a6ef56b5073d59a8e7e6804c424c1d1;hpb=088e16defac8f22d795dc208791e0031eceb1d46;p=~andy%2Fspades diff --git a/src/org/pileus/spades/Main.java b/src/org/pileus/spades/Main.java index ebca439..e4bac7f 100644 --- a/src/org/pileus/spades/Main.java +++ b/src/org/pileus/spades/Main.java @@ -2,33 +2,45 @@ 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.method.ScrollingMovementMethod; -import android.view.View; +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; +import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; -import android.widget.TextView; import android.widget.ScrollView; import android.widget.TabHost; import android.widget.TabWidget; +import android.widget.TextView; import android.widget.Toast; public class Main extends Activity { - /* Static data */ - private Handler handler; - private Messenger messenger; - /* Private data */ + private Handler handler; + private Messenger messenger; private Task task; private Toast toast; - private boolean ready; + private boolean running; + private String topic; + private String names; + private Cards cards; + private Spades game; /* Widgets */ private TabHost window; @@ -37,51 +49,186 @@ public class Main extends Activity private TextView log; private EditText input; private Button send; - private TextView spades; + private LinearLayout spades; private TextView debug; private ScrollView lscroll; 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) + { + 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 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; + } + + // Append the message + this.log.append(span); + } + /* Private handler methods */ - private void onRegister(Object obj) + private void onRegister(Task task) { Os.debug("Main: onRegister"); - this.task = (Task)obj; + this.task = task; + this.game.task = task; + this.running = this.task.isRunning(); + for (Object obj : this.task.getLog()) { + if (String.class.isInstance(obj)) + this.notice((String)obj); + if (Message.class.isInstance(obj)) + this.onMessage((Message)obj); + } } - private void onMessage(Object obj) + private void onMessage(Message msg) { - Message msg = (Message)obj; - + // Debug this.debug.append("> " + msg.line + "\n"); this.dscroll.smoothScrollTo(0, this.debug.getBottom()); - if (msg.cmd.equals("PRIVMSG")) { - this.log.append(msg.from + ": " + msg.msg + "\n"); - this.lscroll.smoothScrollTo(0, this.log.getBottom()); + // Chat + switch (msg.type) { + case PRIVMSG: + this.display(msg); + this.game.onMessage(msg); + break; + case TOPIC: + if (!msg.txt.equals(this.topic)) + this.notice("Topic for " + msg.arg + ": " + msg.txt); + this.topic = msg.txt; + break; + case NAMES: + if (!msg.txt.equals(this.names)) + this.notice("Users in " + msg.arg + ": " + msg.txt); + this.names = msg.txt; + break; + case ERROR: + this.notice("Error: " + msg.txt); + break; + case AUTHOK: + this.notice("Authentication succeeded: " + msg.txt); + break; + case AUTHFAIL: + this.notice("Authentication failed: " + msg.txt); + break; } + this.lscroll.smoothScrollTo(0, this.log.getBottom()); } private void onNotify(String text) { Os.debug("Main: onNotify - " + text); + this.notice(text); this.toast.setText(text); this.toast.show(); } /* Private service methods */ - private void startService() + private void register() { - Os.debug("Main: startService"); + Os.debug("Main: register"); startService(new Intent(this, Task.class) + .putExtra("Command", Task.REGISTER) .putExtra("Messenger", this.messenger)); } - private void stopService() + private void connect() + { + Os.debug("Main: connect"); + startService(new Intent(this, Task.class) + .putExtra("Command", Task.CONNECT)); + this.running = true; + } + + private void disconnect() { - Os.debug("Main: stopService"); + Os.debug("Main: disconnect"); + startService(new Intent(this, Task.class) + .putExtra("Command", Task.DISCONNECT)); + this.running = false; + } + + private void quit() + { + this.log.setText(""); + this.debug.setText(""); 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 */ @@ -104,6 +251,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); @@ -121,7 +271,7 @@ public class Main extends Activity this.log = (TextView) findViewById(R.id.log); this.input = (EditText) findViewById(R.id.input); this.send = (Button) findViewById(R.id.send); - this.spades = (TextView) findViewById(R.id.spades); + this.spades = (LinearLayout) findViewById(R.id.spades); this.debug = (TextView) findViewById(R.id.debug); this.lscroll = (ScrollView) findViewById(R.id.log_scroll); @@ -142,6 +292,21 @@ public class Main extends Activity .newTabSpec("debug") .setIndicator("Debug") .setContent(R.id.debug)); + + // 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(); + } catch (Exception e) { Os.debug("Error setting content view", e); return; @@ -152,6 +317,7 @@ public class Main extends Activity public void onStart() { super.onStart(); + this.register(); Os.debug("Main: onStart"); } @@ -201,8 +367,8 @@ public class Main extends Activity @Override public boolean onPrepareOptionsMenu(Menu menu) { - menu.findItem(R.id.connect).setVisible(!this.ready); - menu.findItem(R.id.disconnect).setVisible(this.ready); + menu.findItem(R.id.connect).setVisible(!this.running); + menu.findItem(R.id.disconnect).setVisible(this.running); return true; } @@ -211,14 +377,16 @@ public class Main extends Activity { switch (item.getItemId()) { case R.id.connect: - this.startService(); + this.connect(); return true; case R.id.disconnect: - this.stopService(); + this.disconnect(); return true; - case R.id.exit: - this.stopService(); - this.finish(); + case R.id.settings: + this.startActivity(new Intent(this, Prefs.class)); + return true; + case R.id.quit: + this.quit(); return true; default: return false; @@ -232,16 +400,16 @@ public class Main extends Activity { switch (msg.what) { case Task.REGISTER: - Main.this.onRegister(msg.obj); + Main.this.onRegister((Task)msg.obj); break; case Task.MESSAGE: - Main.this.onMessage(msg.obj); + Main.this.onMessage((Message)msg.obj); break; case Task.CONNECT: - Main.this.ready = true; + Main.this.running = true; break; case Task.DISCONNECT: - Main.this.ready = false; + Main.this.running = false; break; case Task.NOTIFY: Main.this.onNotify((String)msg.obj); @@ -253,4 +421,3 @@ public class Main extends Activity } } } -