]> Pileus Git - ~andy/spades/blobdiff - src/org/pileus/spades/Main.java
Add connect button
[~andy/spades] / src / org / pileus / spades / Main.java
index 201bdd29eca67af0db75fac8543f70b654210c85..231fee0e16994447bbf9a21258c3e6bdf2850ac5 100644 (file)
@@ -48,6 +48,7 @@ public class Main extends Activity
        private LinearLayout chat;
        private TextView     log;
        private EditText     input;
+       private Button       connect;
        private Button       send;
        private LinearLayout spades;
        private TextView     debug;
@@ -56,6 +57,29 @@ 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)
        {
                String    msg  = "*** " + text + "\n";
@@ -75,9 +99,13 @@ public class Main extends Activity
                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(0xffff88ff), de+1, ne, 0);
+               span.setSpan(new ForegroundColorSpan(color),      de+1, ne, 0);
 
                // Format IRC Colors
                for (Message.Format fmt : msg.parts) {
@@ -110,13 +138,22 @@ public class Main extends Activity
                this.log.append(span);
        }
 
+       private void update(boolean running)
+       {
+               this.running = running;
+               this.connect.setVisibility(running ? View.GONE : View.VISIBLE);
+               this.send.setVisibility(running ? View.VISIBLE : View.GONE);
+       }
+
        /* Private handler methods */
        private void onRegister(Task task)
        {
                Os.debug("Main: onRegister");
                this.task      = task;
                this.game.task = task;
-               this.running = this.task.isRunning();
+               this.update(this.task.isRunning());
+               this.log.setText("");
+               this.debug.setText("");
                for (Object obj : this.task.getLog()) {
                        if (String.class.isInstance(obj))
                                this.notice((String)obj);
@@ -182,7 +219,7 @@ public class Main extends Activity
                Os.debug("Main: connect");
                startService(new Intent(this, Task.class)
                                .putExtra("Command", Task.CONNECT));
-               this.running = true;
+               this.update(true);
        }
 
        private void disconnect()
@@ -190,11 +227,13 @@ public class Main extends Activity
                Os.debug("Main: disconnect");
                startService(new Intent(this, Task.class)
                                .putExtra("Command", Task.DISCONNECT));
-               this.running = false;
+               this.update(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);
@@ -203,6 +242,11 @@ public class Main extends Activity
        }
 
        /* Widget callback functions */
+       public void onConnect(View btn)
+       {
+               this.connect();
+       }
+
        public void onSend(View btn)
        {
                if (this.task == null)
@@ -241,6 +285,7 @@ public class Main extends Activity
                        this.chat      = (LinearLayout) findViewById(R.id.chat);
                        this.log       = (TextView)     findViewById(R.id.log);
                        this.input     = (EditText)     findViewById(R.id.input);
+                       this.connect   = (Button)       findViewById(R.id.connect);
                        this.send      = (Button)       findViewById(R.id.send);
                        this.spades    = (LinearLayout) findViewById(R.id.spades);
                        this.debug     = (TextView)     findViewById(R.id.debug);
@@ -288,6 +333,7 @@ public class Main extends Activity
        public void onStart()
        {
                super.onStart();
+               this.register();
                Os.debug("Main: onStart");
        }
 
@@ -376,10 +422,10 @@ public class Main extends Activity
                                        Main.this.onMessage((Message)msg.obj);
                                        break;
                                case Task.CONNECT:
-                                       Main.this.running = true;
+                                       Main.this.update(true);
                                        break;
                                case Task.DISCONNECT:
-                                       Main.this.running = false;
+                                       Main.this.update(false);
                                        break;
                                case Task.NOTIFY:
                                        Main.this.onNotify((String)msg.obj);