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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
function _view(div) { var ctx = null; this.get_ctx = function() { return ctx; } var quality = div.dataset.quality; quality = (quality == undefined) ? 2 : parseInt(quality); if (isNaN(quality) || quality < 1 || quality > 3) throw new Error(_err.BAD_QUALITY); this.get_quality = function() { return quality; } var canvas = document.createElement("canvas"); canvas.width = _val.VIEW_W * quality; canvas.height = _val.VIEW_H * quality; div.appendChild(canvas); ctx = canvas.getContext("2d"); if (ctx == null) throw new Error(_err.BROWSER_NOT_SUPPORTED); this.get_canvas = function() { return canvas; } var komadai_mode = 0; this.get_komadai_mode = function() { return komadai_mode; } this.set_komadai_mode = function(mode) { komadai_mode = mode; } var ban_left = (_val.VIEW_W - 9 * _val.MASU_W) / 2; var ban_right = (_val.VIEW_W + 9 * _val.MASU_W) / 2; var ban_top; var ban_bottom; var mochigoma_mode = 0; this.get_mochigoma_mode = function() { return mochigoma_mode; } this.set_mochigoma_mode = function(mode) { mochigoma_mode = mode; } var sente_mochigoma_h; var gote_mochigoma_h; var player_names = _util.arr_copy(_str.DEFAULT_PLAYERS); this.get_player_names = function() { return player_names; } this.set_player_names = function(names) { if (names.length != 4) return; for (var i = 0; i < 4; i++) player_names[i] = _util.max_len(names[i], i % 2 == 0 ? _val.KOMADAI_LEN : _val.NAME_LEN); } var b_kyouchou = true; this.set_kyouchou = function(b) { b_kyouchou = b; } var b_hanten = false; this.get_hanten = function() { return b_hanten; } this.set_hanten = function(b) { b_hanten = b; } this.set_transform = function(scale_x, scale_y, x, y) { if (b_hanten) { scale_x *= -1; scale_y *= -1; x = _val.VIEW_W - x; y = _val.VIEW_H - y; } scale_x *= quality; scale_y *= quality; x *= quality; y *= quality; ctx.setTransform(scale_x, 0, 0, scale_y, x, y); } var pick = _sasite.get_empty_sasite(); this.get_pick = function() { return pick; } var kyokumen = null; this.get_kyokumen = function() { return kyokumen; } this.set_kyokumen = function(k) { kyokumen = k; }; this.get_sente_mochigoma = function() { return kyokumen.sente; } this.get_gote_mochigoma = function() { return kyokumen.gote; } this.get_sasite = function() { return kyokumen.sasite; } this.draw = function() { if (ctx == null || kyokumen == null) return; var banmen = kyokumen.banmen; // 盤面 var sente = kyokumen.sente; // 先手の持ち駒 var gote = kyokumen.gote; // 後手の持ち駒 var sasite = kyokumen.sasite; // 指し手 var sasite_pos = sasite.after; // 指し手の駒の位置 // ビューのクリア this.set_transform(1, 1, 0, 0); ctx.clearRect(0, 0, _val.VIEW_W, _val.VIEW_H); // 盤面の領域を取得 ban_top = (_val.VIEW_H - 9 * _val.MASU_H) / 2; ban_bottom = (_val.VIEW_H + 9 * _val.MASU_H) / 2; if (komadai_mode == 1) { // 駒台が下にある場合 ban_top -= _val.KOMADAI_H / 2; ban_bottom -= _val.KOMADAI_H / 2; } else if (komadai_mode == 2) { // 駒台が上にある場合 ban_top += _val.KOMADAI_H / 2; ban_bottom += _val.KOMADAI_H / 2; } // 枠の描画 var w = 9 * _val.MASU_W; var h = 9 * _val.MASU_H; this.set_transform(1, 1, ban_left, ban_top); for (var i = 0; i <= 9; i++) { ctx.beginPath(); ctx.moveTo(i * _val.MASU_W, 0); ctx.lineTo(i * _val.MASU_W, h); ctx.stroke(); ctx.beginPath(); ctx.moveTo(0, i * _val.MASU_H); ctx.lineTo(w, i * _val.MASU_H); ctx.stroke(); } // 駒の描画 var kama; var moji; var x, y; var sign; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.font = _str.KOMA_FONT; for (var i = 0; i < 81; i++) { koma = banmen[i]; if (koma == -1) continue; if (i == sasite_pos && b_kyouchou) // 指し手の強調 ctx.font = "bold " + _str.KOMA_FONT; x = 8 - i % 9; y = Math.floor(i / 9); x = _val.MASU_W * x + ban_left + _val.MASU_W / 2; y = _val.MASU_H * y + ban_top + _val.MASU_H / 2; if (i == pick.before) { this.set_transform(1, 1, x - _val.KOMA_L / 2, y - _val.KOMA_L / 2); ctx.fillStyle = _color.PICK; ctx.fillRect(0, 0, _val.KOMA_L, _val.KOMA_L); ctx.fillStyle = _color.KOMA; } sign = (koma < 16) ? 1 : -1; moji = _koma.MOJI[koma % 16]; if (moji.length == 2) { // 2文字の駒 this.set_transform(sign, sign * 0.5, x, y); ctx.fillText(moji.charAt(0), 0, -0.5 * _val.KOMA_L); ctx.fillText(moji.charAt(1), 0, 0.5 * _val.KOMA_L); } else if (moji.length == 1) { // 1文字の駒 this.set_transform(sign, sign, x, y); ctx.fillText(moji, 0, 0); } if (i == sasite_pos && b_kyouchou) // 指し手の強調を解除 ctx.font = _str.KOMA_FONT; } // 持駒の描画 var s; var h; var b_sente; ctx.textBaseline = "top"; for (sign = 1; sign >= -1; sign -= 2) { b_sente = (sign > 0); s = ((b_sente) ? _str.SENTE : _str.GOTE) + player_names[(b_sente) ? 0 : 2] + " " + ((b_sente && mochigoma_mode == 1 || !b_sente && mochigoma_mode == 2) ? _str.MOCHIGOMA_ALL : this.get_mochigoma_string((b_sente) ? sente : gote)); h = s.length * _val.KOMA_L / 2; if (b_sente) { this.set_transform(0.5, 0.5, ban_right + _val.KOMA_L / 2, ban_bottom - h); sente_mochigoma_h = h; } else { this.set_transform(-0.5, -0.5, ban_left - _val.KOMA_L / 2, ban_top + h); gote_mochigoma_h = h; } for (i = 0; i < s.length; i++) ctx.fillText(s[i], 0, i * _val.KOMA_L); } // 長い名前の描画 if (komadai_mode == 0) { ctx.textBaseline = "middle"; for (sign = 1; sign >= -1; sign -= 2) { s = player_names[(sign > 0) ? 1 : 3]; if (s == "") continue; this.set_transform(sign * 0.5, sign * 0.5, _val.VIEW_W / 2, (_val.VIEW_H + sign * ban_bottom) / 2); ctx.fillText(s, 0, 0); } } } this.get_mochigoma_string = function(mochigoma) { var s; var count; var n; var i; // 文字列のカウント n = mochigoma[_koma.Fu]; // 歩の数 count = (n <= 1) ? n : (n <= 10) ? 2 : 3; for (i = _koma.Ky; i <= _koma.Hi; i++) // 香から飛まで count += mochigoma[i]; if (count == 0) return _str.MOCHIGOMA_EMPTY; // 持ち駒の文字列を作成 s = ""; for (i = _koma.Hi; i >= _koma.Fu; i--) { // 飛から歩まで n = mochigoma[i]; if (n == 0) continue; s += _koma.MOJI[i]; if (n == 1) continue; if (i == _koma.Fu || count > 15) { // 漢数字表記 s += this.get_kansuuji_string(n); } else { // 羅列表記 while (n-- > 1) s += _koma.MOJI[i]; } } return s; } // 漢数字テキスト取得 this.get_kansuuji_string = function(n) { var s = (n >= 10) ? _str.KANSUUJI.charAt(10) : ""; if (n != 10) s += _str.KANSUUJI.charAt(n % 10); return s; } this.get_banmen_pos = function(x, y) { if (x < ban_left || x >= ban_right || y < ban_top || y >= ban_bottom) return -1; return 8 - Math.floor((x - ban_left) / _val.MASU_W) + 9 * Math.floor((y - ban_top) / _val.MASU_H); } this.in_sente_mochigoma = function(x, y) { x -= ban_right; y -= ban_bottom; return (x >= 0 && y < 0 && y >= -sente_mochigoma_h); } this.in_gote_mochigoma = function(x, y) { x -= ban_left; y -= ban_top; return (x < 0 && y >= 0 && y < gote_mochigoma_h); } this.in_komadai = function(x, y) { if (komadai_mode == 0) return false; return (komadai_mode == 1 && y >= _val.VIEW_H - _val.KOMADAI_H) || (komadai_mode == 2 && y < _val.KOMADAI_H); } this.get_banmen_koma = function(pos) { if (pos < 0 || pos >= 81) return -1; return kyokumen.banmen[pos]; } this.set_banmen_koma = function(pos, koma) { kyokumen.banmen[pos] = koma; } }