File: WaveUnit.cs

package info (click to toggle)
cadencii 3.3.9%2Bsvn20110818.r1732-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 35,764 kB
  • ctags: 26,929
  • sloc: cs: 160,836; java: 42,449; cpp: 7,605; ansic: 1,728; perl: 1,087; makefile: 236; php: 142; xml: 117; sh: 21
file content (176 lines) | stat: -rw-r--r-- 6,349 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * WaveUnit.cs
 * Copyright © 2010-2011 kbinani
 *
 * This file is part of org.kbinani.cadencii.
 *
 * org.kbinani.cadencii is free software; you can redistribute it and/or
 * modify it under the terms of the GPLv3 License.
 *
 * org.kbinani.cadencii is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
#if JAVA
package org.kbinani.cadencii;

import java.awt.*;
import org.kbinani.*;
#else
using System;
using org.kbinani.java.awt;

namespace org.kbinani.cadencii
{
#endif

    /// <summary>
    /// インターフェースWaveReceiver, WaveSender, WaveGeneratorを持つクラスの基底クラス.
    /// </summary>
    public abstract class WaveUnit
    {
        /// <summary>
        /// このユニットを画面に描くときの、基本となる描画幅。単位はピクセル
        /// この値を使うかどうかは任意
        /// </summary>
        public const int BASE_WIDTH = 150;

        /// <summary>
        /// このユニットを画面に描くときの、入出力ポート1個分の描画高さの標準値。単位はピクセル
        /// この値を使うかどうかは任意
        /// </summary>
        public const int BASE_HEIGHT_PER_PORTS = 20;

        /// <summary>
        /// エディターの設定値
        /// </summary>
        protected EditorConfig mConfig;

        /// <summary>
        /// メインウィンドウへの参照
        /// </summary>
        protected FormMain mMainWindow;

        /// <summary>
        /// 描画用のストローク
        /// </summary>
        private BasicStroke mStroke;

        /// <summary>
        /// 描画用のフォント
        /// </summary>
        private Font mFont;

        /// <summary>
        /// この波形処理ユニットが属している回路のルートにある波形合成器への参照を保持する
        /// </summary>
        protected WaveGenerator mRoot = null;

        /// <summary>
        /// バージョンを表す整数を返す.
        /// 実装上,setConfigに渡す文字列の書式が変わったとき,バージョンを増やすようにする.
        /// </summary>
        /// <returns></returns>
        public abstract int getVersion();

        /// <summary>
        /// 設定を行う.parameterの1字目は,parameterを分割するのに利用する文字を付ける.
        /// 例えば,3個の整数を受け取る実装の場合,次の2つは同じ意味になる(そのように実装する).
        ///     1)    parameter = "\n1\n2\n3"
        ///     2)    parameter = "\t1\t2\t3"
        /// </summary>
        /// <param name="parameter"></param>
        public abstract void setConfig( String parameter );

        /// <summary>
        /// この波形処理ユニットが属している回路のルートにある波形合成器を設定します
        /// </summary>
        /// <param name="root"></param>
        public virtual void setRoot( WaveGenerator root )
        {
            mRoot = root;
        }

        /// <summary>
        /// この波形処理ユニットが属している回路のルートにある波形合成器を取得します
        /// </summary>
        /// <returns></returns>
        public virtual WaveGenerator getRoot()
        {
            return mRoot;
        }

        /// <summary>
        /// スコアエディタ全体の設定値を設定する.
        /// </summary>
        /// <param name="config"></param>
        public virtual void setGlobalConfig( EditorConfig config )
        {
            mConfig = config;
        }

        /// <summary>
        /// メインウィンドウへの参照を設定します
        /// </summary>
        public virtual void setMainWindow( FormMain main_window )
        {
            mMainWindow = main_window;
        }

        /// <summary>
        /// このユニットを指定した位置に描画します。
        /// </summary>
        /// <param name="graphics">描画に使用するグラフィクス</param>
        /// <param name="x">描画する位置のx座標</param>
        /// <param name="y">描画する位置のy座標</param>
        /// <returns>描画された装置図に外接する四角形のサイズ</returns>
        public virtual void paintTo( Graphics2D graphics, int x, int y, int width, int height )
        {
            // 現在の描画時のストローク、色を保存しておく
            Stroke old_stroke = graphics.getStroke();
            Color old_color = graphics.getColor();

            // 描画用のストロークが初期化してなかったら初期化
            if ( mStroke == null ) {
                mStroke = new BasicStroke();
            }

            if ( mFont == null ) {
                mFont = new Font( "Arial", Font.PLAIN, 10 );
            }

            // 枠と背景を描画
            paintBackground( graphics, mStroke, x, y, width, height, Color.black, PortUtil.Pink );

            // デバイス名を書く
#if JAVA
            String typename = this.getClass().getSimpleName();
#else
            String typename = this.GetType().Name;
#endif
            PortUtil.drawStringEx(
                (Graphics)graphics, typename, mFont,
                new Rectangle( x, y, width, height ),
                PortUtil.STRING_ALIGN_CENTER, PortUtil.STRING_ALIGN_CENTER );

            // 描画時のストローク、色を元に戻す
            graphics.setStroke( old_stroke );
            graphics.setColor( old_color );
        }

        protected void paintBackground( Graphics2D graphics, Stroke stroke, int x, int y, int width, int height, Color border, Color background )
        {
            // 背景を塗りつぶし
            graphics.setColor( background );
            graphics.fillRect( x, y, width, height );

            // 枠線を描く
            graphics.setStroke( stroke );
            graphics.setColor( border );
            graphics.drawRect( x, y, width, height );
        }
    }

#if !JAVA
}
#endif