Quantcast
Channel: かずきのBlog@hatena
Viewing all 1388 articles
Browse latest View live

WinJSのデータバインドでコンバーターを使う(TypeScript使用)

$
0
0

TypeScriptのmoduleにコンバーターを定義する方法です。

WinJS.Binding.converter関数を使ってコンバーターを作成します。

module Converters {exportvar sampleConverter = WinJS.Binding.converter((value: string) => {return value + value;
    });
}

あとは、こんな感じでバインドするようにして

WinJS.UI.Pages.define("/pages/home/home.html", {
    ready: () => {
        WinJS.Binding.processAll(null, { name: "tanaka"});
    }});

HTMLでdata-win-bindで、バインド先の属性名: バインドするプロパティ名 コンバーターの並びで書けばOKです。

<spandata-win-bind="textContent: name Converters.sampleConverter"></span>

こうすると画面に

tanakatanaka

と表示されるようになります。


RoboBinding使うまで

$
0
0

RoboBinding使うまでにはまったのでメモ。

appの中のbuild.gradleに対して以下の記述を追記します。

apply plugin: 'com.android.application'

// 追記ここから
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
         classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+'
    }
}

apply plugin: 'com.neenbedankt.android-apt'
// 追記ここまで

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.kazuki.myapplication"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
// 追記ここから
    compile"org.robobinding:robobinding:0.8.9"

    apt "org.robobinding:codegen:0.8.9"
// 追記ここまで
}

あとは、getting startでもみて適当にViewModelこしらえて

package com.example.kazuki.myapplication;

import org.robobinding.annotation.PresentationModel;
import org.robobinding.presentationmodel.HasPresentationModelChangeSupport;
import org.robobinding.presentationmodel.PresentationModelChangeSupport;

/** * Created by Kazuki on 2015/01/07. */@PresentationModelpublicclass MainViewModel implements HasPresentationModelChangeSupport {

    private PresentationModelChangeSupport support = new PresentationModelChangeSupport(this);

    private String message = "Hello robobinding.";

    publicvoid sayHello() {
        this.setMessage("こんにちは世界");
    }

    public String getMessage() {
        return message;
    }

    publicvoid setMessage(String message) {
        this.message = message;
        this.support.firePropertyChange("message");
    }

    @Overridepublic PresentationModelChangeSupport getPresentationModelChangeSupport() {
        returnthis.support;
    }
}

Viewとバインドして

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"xmlns:bind="http://robobinding.org/android"android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context=".MainActivity"><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"bind:text="{message}"android:id="@+id/textView" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Button"android:id="@+id/button"android:layout_below="@+id/textView"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"bind:onClick="sayHello"/></RelativeLayout>

Activityで紐づける。

package com.example.kazuki.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;

import org.robobinding.binder.Binders;


publicclass MainActivity extends ActionBarActivity {

    @Overrideprotectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainViewModel vm = new MainViewModel();
        View view = Binders.inflateAndBind(this, R.layout.activity_main, vm);
        setContentView(view);
    }


    @Overridepublicboolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        returntrue;
    }

    @Overridepublicboolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();

        //noinspection SimplifiableIfStatementif (id == R.id.action_settings) {
            returntrue;
        }

        returnsuper.onOptionsItemSelected(item);
    }
}

gradle弱者なので、gradleの設定に凄い手間取った。

CharacterControllerを使ってクリックした位置まで物体を移動させる

$
0
0

Cube(地面用)とCube(動かす用)の2個を配置して動かすほうのCubeにCharacterControllerを追加します。

んで、コードも追加して以下のように書くと動いた。

using UnityEngine;
using System.Collections;

publicclass Move : MonoBehaviour
{
    /// <summary>/// 目的地/// </summary>private Vector3 goal;
    /// <summary>/// キャッシュ用/// </summary>private CharacterController controller;

    // Use this for initializationvoid Start()
    {
        // とりあえずのゴールは現在地this.goal = transform.position;
        // CharacterControllerをキャッシュしとくthis.controller = this.GetComponent<CharacterController>();
    }

    // Update is called once per framevoid Update()
    {
        // 移動速度
        Vector3 velocity = Vector3.zero;
        if (this.controller.isGrounded)
        {
            // 地面についてたらif (Input.GetButtonDown("Fire1"))
            {
                // 現在のマウスの位置から地面にRayを飛ばしてゴールの場所を特定する
                var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit info;
                if (Physics.Raycast(ray, out info))
                {
                    this.goal = info.point;
                }
                else
                {
                    // ぶつからなかったら何もしないreturn;
                }
            }

            if (Vector3.Distance(goal, transform.position) < 0.5f)
            {
                // ゴールと今の場所が近かったら何もしないreturn;
            }

            // ゴールと現在地の差分を正規化して
            var direction = (this.goal - transform.position).normalized;
            // 速度をかけて
            velocity = direction * 5.0f;

            // 一応ジャンプも考慮してif (Input.GetButton("Jump"))
            {
                velocity.y = 60.0f;
            }
            else
            {
                velocity.y = 0;
            }

        }

        // 重力を加味して
        velocity.y -= 20.0f;

        // 移動this.controller.Move(velocity * Time.deltaTime);

        // 物体を目的地のほうに向ける
        transform.LookAt(this.goal);
    }
}

くっそ苦労したなぁ…。Unity弱者は辛い。

Unityで物体についていくカメラを実現する。ついでに右クリックドラッグで回転も

$
0
0

MMO RPGにありがちなカメラっぽい動きをさせてみた。

using UnityEngine;
using System.Collections;

publicclass CameraControl : MonoBehaviour
{
    // 追いかける対象物public Transform target;

    private Vector3 move = Vector3.zero;

    private Vector3 prevPosition = Vector3.zero;

    // Use this for initializationvoid Start()
    {

    }

    // Update is called once per framevoid Update()
    {
        if (Input.GetButtonDown("Fire2"))
        {
            // 右ボタン押したときに初期位置リセットthis.prevPosition = Input.mousePosition;
        }

        if (Input.GetButton("Fire2"))
        {
            // 右ボタンが押されてたら現在地と前の位置の差分をとって動いた距離に加算しておく
            move += Input.mousePosition - this.prevPosition;
        }
        
        // 前のマウスの位置を更新this.prevPosition = Input.mousePosition;

        // 画面の高さだけマウス動かしたときに何度回転させるか
        var anglePerPixelX = 180.0f / Screen.width;
        var anglePerPixelY = 60.0f / Screen.height;

        // 移動距離と上で求めた値をかけて角度を求める// X方向は360度回転させる
        var angleX = Mathf.Repeat(anglePerPixelX * move.x, 360.0f);
        // Y方向は10度~60度で
        var angleY = Mathf.Clamp(anglePerPixelY * -move.y, 10.0f, 60.0f);

        // 対象の位置に、回転とオフセットを加味
        var cameraPosition = this.target.position + Quaternion.Euler(angleY, angleX, 0) * new Vector3(0, 0, -5);

        // カメラの位置を更新してターゲットの方向を向かせるthis.transform.position = cameraPosition;
        this.transform.LookAt(this.target);
    }
}

こんなビヘイビアをカメラにくっつけて、カメラを取り付けたいオブジェクトをtargetに設定したらおしまい。

UnityでTODO管理アプリのサンプル

$
0
0

コードレシピに挙げてみました。UnityだってUI作れるもん!

Unity 4.6でTodoアプリのサンプル in C# for Visual Studio 2012

Unityでドローンみたいにふわふわした感じを出したい

$
0
0

そんなことがありました。

using UnityEngine;
using System.Collections;

publicclass FlyingObject : MonoBehaviour
{
    privateconstfloat G = 9.9f;

    // Use this for initializationvoid Start()
    {

    }

    // Update is called once per framevoid Update()
    {
        var velocity = new Vector3(0, 0, 0);
        velocity.x += Input.GetAxis("Horizontal");
        velocity.y += Input.GetAxis("Vertical");
        this.rigidbody.velocity = velocity * 500 * Time.deltaTime + new Vector3(0, G + Random.Range(3.0f, 5.0f), 0) * Time.deltaTime;
    }
}

とりあえず、重力加速度っぽい数字に3.0~5.0の間の数字を足しこんだ値をvelocityに突っ込んでやれば少しそれっぽく動くようになりました。まだ本物っぽくするには足りないですが…。

Unityで空中浮遊するものを操作したい

$
0
0

上下と前後左右に移動できて、ちょっとふわふわして、移動方向に少し傾くビヘイビア

using UnityEngine;
using System.Collections;

publicclass FlyingObject : MonoBehaviour
{
    privateconstfloat G = 9.9f;

    // Use this for initializationvoid Start()
    {

    }

    // Update is called once per framevoid Update()
    {
        var velocity = new Vector3(0, 0, 0);
        velocity.x += Input.GetAxis("Horizontal");
        velocity.y += Input.GetAxis("Vertical");
        if (Input.GetButton("Fire1"))
        {
            velocity.z += 1.0f;
        }
        if (Input.GetButton("Fire2"))
        {
            velocity.z += -1.0f;
        }

        this.rigidbody.velocity = velocity * 500 * Time.deltaTime + new Vector3(0, G + Random.Range(0.0f, 10.0f), 0) * Time.deltaTime;
        var originalRotation = this.rigidbody.rotation;
        var zEular = 0.0f;
        if (this.rigidbody.velocity.x > 0)
        {
            zEular = -5.0f;
        }
        elseif (this.rigidbody.velocity.x < 0)
        {
            zEular = 5.0f;
        }

        var xEular = 0.0f;
        if (this.rigidbody.velocity.z > 0)
        {
            xEular = 5.0f;
        }
        elseif (this.rigidbody.velocity.z < 0)
        {
            xEular = -5.0f;
        }
        var eular = new Vector3(xEular, 0, zEular);
        this.rigidbody.rotation = Quaternion.Euler(eular);
    }
}

rotationのQuaternion型を一回Vector3に戻して角度を再設定してるところとかポイント?

Unityで飛行体を操作したい

$
0
0

Unityで飛行体を操作したい in C# for Visual Studio 2012

コードレシピにサンプルをアップしました。とりあえず、


UniRxを使ったシンプルなコードを書いて入門してみた

$
0
0

id:neueccさんの最新作と思われるUniRxをようやく触りました。

Reactive ExtensionsのUnity版。かっこいい。ということでマウスのドラッグを扱う簡単な例を書いて入門してみました。こんな感じ。

using UnityEngine;
using System.Collections;
using UniRx;

publicclass UniRxCubeBehavior : ObservableMonoBehaviour
{
    publicoverridevoid Awake()
    {
        // ドラッグ対象のオブジェクト上でマウスが押されたときに値を発行するIObservable
        var mouseDownOnUniRxCube = this.UpdateAsObservable()
            .Where(_ => Input.GetMouseButtonDown(0))
            .Select(_ => Camera.main.ScreenPointToRay(Input.mousePosition))
            .Select(x =>
                {
                    RaycastHit rh;
                    var hit = Physics.Raycast(x, out rh);
                    return Tuple.Create(hit, rh);
                })
            .Where(x => x.Item1 && x.Item2.collider.gameObject == this.gameObject)
        // マウスが離されたときに値を発行するIObservable
        var mouseUp = this.UpdateAsObservable()
            .Where(_ => Input.GetMouseButtonUp(0));

        this.UpdateAsObservable()
            // マウスが押されてるとき
            .Where(_ => Input.GetMouseButton(0))
            // 対象オブジェクトの上でマウスが押されるまでスキップして
            .SkipUntil(mouseDownOnUniRxCube)
            // マウスが離されるまで値を拾う
            .TakeUntil(mouseUp)
            // それを繰り返す
            .Repeat()
            // 現在のマウスの位置から移動位置を算出して
            .Select(_ => Input.mousePosition)
            .Select(x => 
            {
                RaycastHit rh;
                Physics.Raycast(Camera.main.ScreenPointToRay(x), out rh, 1<< LayerMask.NameToLayer("Background"));
                return rh.point;
            })
            // z座標は移動させないように変換して
            .Select(x => new Vector3(x.x, x.y, this.transform.position.z))
            // 位置を設定する
            .Subscribe(x => this.transform.position = x);

        base.Awake();
    }
}

気合で1ステートメントに収めることもできるけど、個人的には適度にわけるのがお好み。これをCubeあたりに割り当てて、Backgroundというレイヤを割り当てた透明な背景を置いておくと、マウスでドラッグが動くようになります。

因みにRxを使わない場合はこんな感じになりました。

using UnityEngine;
using System.Collections;

publicclass NormalCubeBehavior : MonoBehaviour
{
    privatebool drag;

    void Update()
    {
        // ドラッグ中じゃなくてマウスが押されたらif (!drag && Input.GetMouseButtonDown(0))
        {
            // 対象オブジェクトの上でクリックされたかチェックして
            RaycastHit rh;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rh))
            {
                if (rh.collider.gameObject == this.gameObject)
                {
                    // 対象オブジェクトの場合は、ドラッグ中の状態にする
                    drag = true;
                }
            }
        }

        // ドラッグ中にマウスが離されたらドラッグ中を解除して処理を抜けるif (drag && Input.GetMouseButtonUp(0))
        {
            drag = false;
            return;
        }

        // ドラッグ中はif (drag)
        {
            // 現在のマウスの位置を算出して
            RaycastHit rh;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rh, 1<< LayerMask.NameToLayer("Background")))
            {
                // マウスの位置を新しい場所に設定する
                var newPos = new Vector3(rh.point.x, rh.point.y, this.transform.position.z);
                this.transform.position = newPos;
            }
        }
    }
}

まだシンプルな例だから、そんなに変わりないかな。でも、ネストが嫌な感じを醸し出してる。

UniRxを使ってオブジェクトをマウスドラッグで回転させる

$
0
0

またまたありがちな例ですがUniRxとUnityに慣れるために書いてみました。

UniRx版

using UnityEngine;
using System.Collections;
using UniRx;

publicclass UniRxRollingBehaviour : ObservableMonoBehaviour
{
    publicoverridevoid Awake()
    {
        var drag = this.UpdateAsObservable()
            .Where(_ => Input.GetMouseButton(0))
            .SkipUntil(this.UpdateAsObservable()
                .Where(_ => Input.GetMouseButtonDown(0))
                .Select(_ =>
                {
                    RaycastHit rh;
                    return Tuple.Create(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rh), rh);
                })
                .Where(x => x.Item1 && x.Item2.collider.gameObject == this.gameObject))
            .TakeUntil(this.UpdateAsObservable().Where(_ => Input.GetMouseButtonUp(0)))
            .Repeat()
            .Select(_ => Input.mousePosition);

        drag.Zip(drag.Skip(1), (x, y) => Tuple.Create(x, y))
            .Select(x => x.Item2 - x.Item1)
            .Select(x => new Vector3(x.y, -x.x, x.z))
            .Subscribe(x => this.transform.Rotate(x));

        base.Awake();
    }
}

今回はがっつり1ステートメントに詰め込みました。そのぶん短くなったかな。コメントがあれば個人的には許容範囲かも。

続いて比較のためにUniRx無い版を書いてみた。状態管理のためのフラグとかが増えてきてちょっといやな感じを醸し出しはじめてるけど、まだ許容範囲。

using UnityEngine;
using System.Collections;

publicclass NormalRollingBehaviour : MonoBehaviour
{
    privatebool drag;

    privatebool first = true;

    private Vector3 prevPosition;

    // Update is called once per framevoid Update()
    {
        print("update");
       // ドラッグ中じゃなくてマウスが押されたらif (!drag && Input.GetMouseButtonDown(0))
        {
            // 対象オブジェクトの上でクリックされたかチェックして
            RaycastHit rh;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rh))
            {
                if (rh.collider.gameObject == this.gameObject)
                {
                    // 対象オブジェクトの場合は、ドラッグ中の状態にする
                    drag = true;
                }
            }
        }

        // ドラッグ中にマウスが離されたらドラッグ中を解除して処理を抜けるif (drag && Input.GetMouseButtonUp(0))
        {
            drag = false;
            return;
        }

        // ドラッグ中はif (drag)
        {
            print("drag");
            if (!first)
            {
                var diff = Input.mousePosition - prevPosition;
                print(diff);
                this.transform.Rotate(new Vector3(diff.y, -diff.x, diff.z));
            }
            first = false;
            prevPosition = Input.mousePosition;
        }
    }
}

UniRxを使って慣性っぽいのが働いてるような動きをさせる

$
0
0

これは別にUniRxじゃなくてもいいかな…。でも細かくUpdateを分割して書けるのは個人的に好きかも。

using UnityEngine;
using System.Collections;
using UniRx;

publicclass MoveBehaviour : ObservableMonoBehaviour
{
    publicoverridevoid Awake()
    {
        // 加速度
        var a = 5.0f;
        // 減速するスピード
        var downSpeed = 0.7f;
        // 最高速度
        var maxSpeed = 10.0f;
        // 物体にかかってる力
        var velocity = new Vector3();
        // 速度を押されている水平キーに応じて加算していくthis.UpdateAsObservable()
            .Select(_ => Input.GetAxis("Horizontal"))
            .Subscribe(x =>
            {
                velocity.x += x * a * Time.deltaTime;
                velocity.x = Mathf.Min(velocity.x, maxSpeed);
            });
        // 何も押されてないけど動いてるときは適当に減速する(右に動いてるとき)this.UpdateAsObservable()
            .Where(_ => velocity.x > 0)
            .Where(_ => !Input.GetButton("Horizontal"))
            .Subscribe(_ =>
            {
                velocity.x -= downSpeed * a * Time.deltaTime;
                if (velocity.x < 0) { velocity.x = 0; }
            });
        // 何も押されてないけど動いてるときは適当に減速する(左に動いてるとき)this.UpdateAsObservable()
            .Where(_ => velocity.x < 0)
            .Where(_ => !Input.GetButton("Horizontal"))
            .Subscribe(_ =>
            {
                velocity.x += downSpeed * a * Time.deltaTime;
                if (velocity.x > 0) { velocity.x = 0; }
            });

        // 力を物体にくわえるthis.UpdateAsObservable()
            .Subscribe(_ =>
            {
                this.rigidbody.velocity = velocity;
            });

        base.Awake();
    }
}

これで水平方向になるから、あとはお好きな方向を同じ要領で追加していくだけですね。

Unityで動いてる物体を完全に止める方法

$
0
0

メモ。

targetGameObjectの動きを止める場合。

targetGameObject.rigidbody.velocity = Vector3.zero;
targetGameObject.rigidbody.angularVelocity = Vector3.zero;

Unityで物体の色を変える

$
0
0

色を変える方法のメモ。

targetGameObject.renderer.material.color = Color.red;

ASP.NET MVCでリストの先頭に要素を追加すると表示がうまくいかない

$
0
0

空データを先頭に追加したのに、Viewでforループ使ってTextBoxForなんかで表示すると、うまいこと表示されない(既にリストに入ってたデータが表示されてしまう)という問題にぶちあたった。

http://1drv.ms/1xUJv5p

上のリンクのプロジェクトがミニマムな実装のつもり。誰かわかる人いないかなぁ。

ASP.NET MVCでPOSTのデータを画面に出力するとうまくいかないケースがある

$
0
0

こんな記事を書いたら、いろんな人から教えてもらえました。

ということで、ModelState.Clear()を呼び出して解決。これはバグい。


WPF 4.5 入門のSlideShareの使われ方想定

$
0
0

過去の連載をPDFにまとめてSlideShareにアップしてます。

WPF4.5入門

こいつですが、SlideShareで閲覧してもらうというよりはSlideShareへ飛んで行ってもらってDLしてもらうと、そのままPDFが落ちてきます。 連載読むのだるいとかいう人は、DLしてみるのもいいかも?と思ってアップしてます。

ユニバーサルWindowsアプリ入門をSlideShareにアップしました

C#のSeleniumで非同期処理を待つ方法

$
0
0

Javaだけど使い方はほぼおなじSelenium。とても参考になるサイトがあります。

Selenium 2で非同期処理を待機する5つの方法 - CODESCRIBBLE

ここで使われてるWait系クラスを使うにはC#では、Selenium本体以外に、Selenium WebDriver Support Classesを入れる必要があります。しばらく気づかなくて、無い無いって探してしまったのでメモ。

OpenCVで輪郭抽出と、最大の領域を求める方法

$
0
0

超、自分用メモ。

cv::Mat src = cv::imread("streat2.jpg", CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat input(src.rows, src.cols, src.type());

// 2値化
cv::threshold(src, input, 200, 255, CV_THRESH_BINARY);

// 輪郭を抽出
std::vector<std::vector<cv::Point>> contours;
cv::findContours(input, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);

// 最大面積の領域をピックアップauto maxContour = std::max_element(contours.begin(), contours.end(), [](std::vector<cv::Point> x, std::vector<cv::Point> y) {return cv::contourArea(x) < cv::contourArea(y);
});

// cv::RotatedRectに変換auto area = [maxContour](){
    cv::Mat pointf;
    cv::Mat(*maxContour).convertTo(pointf, CV_32F);
    return cv::fitEllipse(pointf);
}();

// 描画
cv::ellipse(src, area, cv::Scalar(255, 0, 0), 2);

C++のラムダ式はじめて使ったけど便利ですね。STLと相性いいです。

OpenCVでカラー画像をグレースケールに変換する

$
0
0

カラーで読んで、グレーに変換

cv::Mat src = cv::imread("lena.jpg", CV_LOAD_IMAGE_COLOR);
cv::Mat gray;
cv::cvtColor(src, gray, CV_RGB2GRAY);
Viewing all 1388 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>