読者です 読者をやめる 読者になる 読者になる

atelier:mitsuba

i love UI/UX, Blend, XAML, Behavior, P5, oF, Web, Tangible Bits and Physical computing. なにかあればお気軽にご連絡ください。atelier@c-mitsuba.com

Dをなげるだけのかんたんなおしごと

Processing Android

どうみてもイベントハンドラなアプリだろ...Processingなんだぜ...それ...


ボタンをタップするとDを投げて、しかも3秒後に死んでくれるアプリです。
Processingでボタンイベントみたいなんを1から自分でやるのってかなりだるいですね!

import twitter4j.*;
Twitter twitter;

PImage bg;
PImage home,home_d;
boolean homePressed;
boolean homeReleased;

String message;
String[] goHome = {"かえるよー","帰るよー","かえるでー","かえるぅ","かえるょ","かえるで!"};
String[] face = {"ヾ(>ヮ<)ノ゙","( ・ω・゛)","ヽ(´ー`)ノ","( ー`дー´)","Σ(゚∀゚ノ)ノ"};

boolean IsSended;
int sec;
int cntsec;

int buttonSize = 300;

void setup(){
  
  twitter = new TwitterFactory().getInstance(); 
  String consumerKey = "こんしゅぅまぁきぃ";
  String consumerSecret = "ひみつのこんしゅぅまぁきぃ";
  String accessToken = "あくせすとぉくん";
  String accessTokenSecret = "ひみつのあくせすとぉくん";

  twitter.setOAuthConsumer(consumerKey,consumerSecret);
  twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret)); 
  
  message = goHome[int(random(goHome.length))] + face[int(random(face.length))];
  
  size(720,1280);
  rectMode(CENTER);
  textAlign(CENTER);
  colorMode(RGB, 255, 255, 255, 100);
  background(0);
  bg = loadImage("bg.png");
  
  home = loadImage("home_d.png");
  home_d = loadImage("home.png");
  homePressed = false;
  homeReleased = false;
  IsSended = false;
  sec = second();
  cntsec = 3;
}

void draw(){
  image(bg, 0, 0); 
  homeButton(width/2,height/2);
  cover(width/2,height/2);
  exitTimer();
}

void exitTimer(){
  if(IsSended == true){
    if(sec != second()){
      cntsec--;
      sec = second();
      if(cntsec == 0){
        exit();
      }
    }
  }
}

void homeButton(int dx,int dy){
  image(home,dx-buttonSize/2,dy-buttonSize/2);
  if(mousePressed == true){
    if(mouseX>=dx-buttonSize/2 && mouseX<=dx+buttonSize/2 && mouseY>=dy-buttonSize/2 && mouseY<=dy+buttonSize/2){
      image(home_d,dx-buttonSize/2,dy-buttonSize/2);
      homePressed = true;
    }
  }
  
  if(mousePressed == false && homePressed == true){
     if(mouseX>=dx-buttonSize/2 && mouseX<=dx+buttonSize/2 && mouseY>=dy-buttonSize/2 && mouseY<=dy+buttonSize/2){
        homePressed = false;
        homeReleased = true;
      }
  }
}

void cover(int dx,int dy){
  if(homeReleased == true){
    fill(0,0,0,60);
    rect(dx,dy,700,400);
    textSize(30);
    fill(255,255,255);
    sendDirectMessage();
    text(message,width/2,height/2);
  }
}

void sendDirectMessage(){  
  if(IsSended == false){
    try{
      twitter.sendDirectMessage("mitsuba_tan",message);
      IsSended = true;
      sec = second();
    }catch (TwitterException e){
      message = e.getStatusCode() + "";
    }
  }
}

void keyPressed() {
   if(keyCode == 4){
     exit();
   }  
}

これがWPならかなり短くなるんだろうけど、そもそもまともなAndroidアプリの組み方してないから比較のやりようがなかった。


追記:Javaなら....

https://gist.github.com/6b98c32d14d711a0706b

さすが世界のつもりんヾ(>ヮ<)ノ゙