Class: Miyako::Choices

Inherits:
Delegator
  • Object
show all
Includes:
Animation, Layout, SpriteBase
Defined in:
lib/Miyako/API/choices.rb

Overview

選択肢を管理するクラス

選択肢は、Shapeクラスから生成したスプライトもしくは画像で構成される

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Layout

#add_snap_child, #bottom, #bottom!, #center, #center!, #centering, #centering!, #copy_layout, #delete_snap_child, #get_snap_children, #get_snap_sprite, #h, #include_snap_child?, #init_layout, #layout_dispose, #left, #left!, #middle, #middle!, #move, #move!, #move_to, #move_to!, #on_move, #outside_bottom, #outside_bottom!, #outside_left, #outside_left!, #outside_right, #outside_right!, #outside_top, #outside_top!, #pos, #relative_move_to, #relative_move_to!, #reset_snap, #right, #right!, #segment, #set_layout_size, #set_snap_children, #set_snap_sprite, #size, #snap, #top, #top!, #update_layout, #w, #x, #y

Methods included from Animation

[], []=, anim_hash, reset, start, stop, update, update_animation

Methods included from SpriteBase

#bitmap, #hide, #image_rect, #image_size, #ox, #oy, #part_rect, #render_xy, #render_xy_to, #show, #to_unit, #update

Constructor Details

#initializeChoices

インスタンスを生成する

返却値

生成された Choices クラスのインスタンス



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/Miyako/API/choices.rb', line 167

def initialize
  init_layout
  @choices = []
  @name_to_choice = {}
  @layout_spaces = {}
  @now = nil
  @non_select = false
  @last_selected = nil
  @result = nil
  @visible = true
  @org_pos = Point.new(0,0)
  set_layout_size(1, 1)
end

Instance Attribute Details

#choicesObject (readonly)

選択肢配列の集合



161
162
163
# File 'lib/Miyako/API/choices.rb', line 161

def choices
  @choices
end

#layout_spacesObject (readonly)

選択肢の位置決めに使うレイアウト空間をハッシュで管理



163
164
165
# File 'lib/Miyako/API/choices.rb', line 163

def layout_spaces
  @layout_spaces
end

#name_to_choiceObject (readonly)

名前と選択肢を関連づけているハッシュ



162
163
164
# File 'lib/Miyako/API/choices.rb', line 162

def name_to_choice
  @name_to_choice
end

#visibleObject

レンダリングの可否(true->描画 false->非描画)



160
161
162
# File 'lib/Miyako/API/choices.rb', line 160

def visible
  @visible
end

Class Method Details

.create_choice(body, body_selected = nil, selected = false, body_disable = nil, enable = true, name = nil) ⇒ Object

選択肢を作成するChoice 構造体のインスタンスを作成する構造体には、引数bodyと、必ず true を返す条件ブロックが登録されている。残りは nil name引数の省略時にはnilが渡され、内部で、オブジェクトIDを文字列化したものを入れる

body

選択肢を示す画像

body_selected

選択肢を示す画像(選択時)。デフォルトはnil

selected

生成時に選択されているときはtrue、そうでないときはfalseを設定する

body_disable

選択肢を示す画像(選択不可時)。デフォルトはnil

enable

生成時に選択可能なときはtrue、不可の時はfalseを設定する

name

選択肢の名前

返却値

生成された Choice構造体のインスタンス



210
211
212
213
214
215
216
217
218
219
# File 'lib/Miyako/API/choices.rb', line 210

def Choices.create_choice(body, body_selected = nil, selected = false, body_disable = nil, enable = true, name = nil)
  choice = Choice.new(body, body_selected, body_disable, Proc.new{ true }, enable, selected,
                      nil, nil, nil, nil, nil, nil, {}, nil, nil)
  choice.left = choice
  choice.right = choice
  choice.up = choice
  choice.down = choice
  choice.name = name || choice.object_id.to_s
  return choice
end

Instance Method Details

#__getobj__Object



181
182
183
# File 'lib/Miyako/API/choices.rb', line 181

def __getobj__
  @choices
end

#__setobj__(obj) ⇒ Object



185
186
# File 'lib/Miyako/API/choices.rb', line 185

def __setobj__(obj)
end

#any_select?Boolean

選択肢が選択状態かを問い合わせる

現在、選択肢が選択状態か非選択状態(non_selectメソッド呼び出しなど)かを問い合わせる

返却値

選択状態ならtrue、非選択状態ならfalseを返す

Returns:

  • (Boolean)


354
355
356
# File 'lib/Miyako/API/choices.rb', line 354

def any_select?
  return !@non_select
end

#attach(x, y) ⇒ Object

画面上の座標から、該当する選択肢を変更する

マウスカーソル位置などの座標から、座標を含む選択肢を選択状態にする該当する場所が無ければfalseを返すまだ選択を開始していないときはfalseを返す

x

x方向位置

y

y方向位置

返却値

選択肢が見つかったときはtrue、見つからなかったときはfalseを返す



392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/Miyako/API/choices.rb', line 392

def attach(x, y)
  return false unless @now
  obj = @now.base.detect{|ch| ch.selected ? ch.body_selected.broad_rect.in_range?(x, y) : ch.body.broad_rect.in_range?(x, y) }
  return false unless obj
  @non_select = false
  @last_selected = @now
  @now.selected = false
  update_choices(@now, obj)
  @now = obj
  @now.selected = true
  return true
end

#attach?(x, y) ⇒ Boolean

画面上の座標から、該当する選択肢があるかどうかを問い合わせる

マウスカーソル位置などの座標から、座標を含む選択肢があるときはtrue、無いときはfalseを返すまだ選択を開始していないときはfalseを返す

x

x方向位置

y

y方向位置

返却値

選択肢が見つかったときはtrue、見つからなかったときはfalseを返す

Returns:

  • (Boolean)


411
412
413
414
415
# File 'lib/Miyako/API/choices.rb', line 411

def attach?(x, y)
  return false unless @now
  obj = @now.base.detect{|ch| ch.selected ? ch.body_selected.broad_rect.in_range?(x, y) : ch.body.broad_rect.in_range?(x, y) }
  return obj ? true : false
end

#attributeObject

現在選択している選択肢の属性をアクセスする

属性を編集・参照できるハッシュを取得するまだ選択が開始されていなければnilが返る

返却値

属性(対応するChoice#attributeメソッドの値)



320
321
322
323
# File 'lib/Miyako/API/choices.rb', line 320

def attribute
  return nil unless @now
  return @now.attribute
end

#bodyObject

選択肢本体を取得する

選択肢の表示対象となるインスタンスを取得するChoice構造体にbody_selectedが設定されている時はbody_selected、そうでなければbodyを返すまだ選択が開始されていなければnilが返る

返却値

選択肢本体(選択時)



302
303
304
305
# File 'lib/Miyako/API/choices.rb', line 302

def body
  return nil unless @now
  return @now.body_selected ? @now.body_selected : @now.body
end

#broad_rectObject

現在登録している選択肢の最大の大きさを矩形で取得する

現在インスタンスが所持している選択肢全てから左上座標、右下座標を取得し、矩形の形式で返す但し、選択肢が一つも登録されていない時はRect(0,0,1,1)を返す

返却値

生成された矩形(Rect構造体のインスタンス)



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/Miyako/API/choices.rb', line 483

def broad_rect
  return Rect.new(0, 0, 1, 1) if @choices.length == 0
  xx = []
  yy = []
  @choices.each{|ch|
    ch.each{|cc|
      r = cc.broad_rect
      xx << r.x
      yy << r.y
      xx << r.x + r.w
      yy << r.y + r.h
    }
  }
  min_x, max_x = xx.minmax
  min_y, max_y = yy.minmax
  return Rect.new(min_x, min_y, max_x-min_x, max_y-min_y)
end

#choicing?Boolean

選択を開始しているかどうかを問い合わせる

start_choiceメソッドを呼び出して、コマンド選択が始まっているかどうかを問い合わせ、始まっているときはtrueを返す

返却値

選択を開始しているときはtrueを返す

Returns:

  • (Boolean)


258
259
260
# File 'lib/Miyako/API/choices.rb', line 258

def choicing?
  return @now != nil
end

#create_choices(choices) ⇒ Object

選択肢を登録する

選択肢集合(画面に一度に表示する選択肢群、Choice 構造体の配列)を選択肢リストに登録するまたこのとき、対象の選択肢をChoices#[]メソッドで参照できるようになる

choices

選択肢(Choice構造体)の配列

返却値

レシーバ



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/Miyako/API/choices.rb', line 226

def create_choices(choices)
  choices.each{|v|
    v.base = choices
    @name_to_choice[v.name] = v
  }
  @choices.push(choices)
  @last_selected = @choices[0][0] if (@choices.length == 1 && @last_selcted == nil)
  rect = self.broad_rect
  set_layout_size(*rect.size)
  return self
end

#disposeObject

選択肢データを解放する



249
250
251
252
253
# File 'lib/Miyako/API/choices.rb', line 249

def dispose
  @choices.each{|c| c.clear }
  @choices.clear
  @choices = []
end

#down_choiceObject

選択肢を下移動させる

但し、まだ選択が開始されていなければ何もしない

返却値

自分自身を返す



553
554
555
556
557
558
559
560
561
562
# File 'lib/Miyako/API/choices.rb', line 553

def down_choice
  return self unless @now
  @last_selected = @now
  @now.selected = false
  obj = @now.down
  update_choices(@now, obj)
  @now = obj
  @now.selected = true
  return self
end

#enable?Boolean

選択肢が選択可能かどうかを問い合わせる

現在指している選択肢が選択可能か選択不可かを問い合わせる

返却値

選択可能ならtrue、選択不可ならfalseを返す

Returns:

  • (Boolean)


361
362
363
364
365
# File 'lib/Miyako/API/choices.rb', line 361

def enable?
  return false if @non_select
  return false unless @now
  return @now.enable
end

#end_choice(command_box = nil) ⇒ Object

選択を終了する

選択の終了処理を行う引数に選択に使用したテキストボックスを渡し、選択状態にあるしたChoice構造体にend_select_procブロックを渡しているとき、そのブロックを評価する(そのとき、引数とした渡ってきたテキストボックスをブロック引数に取る)。

command_box

選択に使用したテキストボックス。デフォルトはnil



287
288
289
290
291
292
293
294
295
# File 'lib/Miyako/API/choices.rb', line 287

def end_choice(command_box = nil)
  return unless @now
  return @now.end_select_proc.call(command_box) if (command_box != nil && @now.end_select_proc != nil)
  @result = @now.result
  @now.selected = false
  @last_selected = @now
  @now = nil
  @non_select = true
end

#initialize_copy(obj) ⇒ Object

:nodoc:



188
189
190
191
# File 'lib/Miyako/API/choices.rb', line 188

def initialize_copy(obj) #:nodoc:

  @choices = @choices.dup
  copy_layout
end

#left_choiceObject

選択肢を左移動させる

但し、まだ選択が開始されていなければ何もしない

返却値

自分自身を返す



511
512
513
514
515
516
517
518
519
520
# File 'lib/Miyako/API/choices.rb', line 511

def left_choice
  return self unless @now
  @last_selected = @now
  @now.selected = false
  obj = @now.left
  update_choices(@now, obj)
  @now = obj
  @now.selected = true
  return self
end

#non_selectObject

選択肢を非選択状態に変更する

現在の選択状態を、全部選択していない状態にする

返却値

自分自身を返す



345
346
347
348
349
# File 'lib/Miyako/API/choices.rb', line 345

def non_select
  @now.base.each{|c| c.selected = false } if @now
  @non_select = true
  return self
end

#ohObject

mixin されたインスタンスの部分矩形高を取得する

返却値

インスタンスの高さ(デフォルトは0)



609
610
611
# File 'lib/Miyako/API/choices.rb', line 609

def oh
  return self.size[1]
end

#owObject

mixin されたインスタンスの部分矩形幅を取得する

返却値

インスタンスの幅(デフォルトは0)



603
604
605
# File 'lib/Miyako/API/choices.rb', line 603

def ow
  return self.size[0]
end

#rectObject

現在登録している選択肢の大きさを矩形で取得する

内容はbroad_rectメソッドの結果と同じ

返却値

生成された矩形(Rect構造体のインスタンス)



504
505
506
# File 'lib/Miyako/API/choices.rb', line 504

def rect
  return self.broad_rect
end

#regist_layout_space(name, layout_space) ⇒ Object

名前から選択肢を参照する

create_choicesメソッドで登録した選択肢を、名前からもとめる登録されていない名前を渡したときはnilを返す

name

登録されているの名前。文字列

返却値

対応する選択肢インスタンス



243
244
245
246
# File 'lib/Miyako/API/choices.rb', line 243

def regist_layout_space(name, layout_space)
  @layout_spaces[name] = layout_space
  layout_space.snap(self)
end

#renderObject

画面に描画を指示する

現在表示できる選択肢を、現在の状態で描画するよう指示するブロック付きで呼び出し可能(レシーバに対応したSpriteUnit構造体が引数として得られるので、補正をかけることが出来る。ブロックの引数は、|インスタンスのSpriteUnit, 画面のSpriteUnit|となる。visibleメソッドの値がfalseのとき、選択が開始されていない時は描画されない。

返却値

自分自身を返す



432
433
434
435
436
437
# File 'lib/Miyako/API/choices.rb', line 432

def render
  return unless @visible
  return self unless @now
  @now.base.each{|c| c.render }
  return self
end

#render_to(dst) ⇒ Object

画像に描画を指示する

現在表示できる選択肢を、現在の状態で描画するよう指示するブロック付きで呼び出し可能(レシーバに対応したSpriteUnit構造体が引数として得られるので、補正をかけることが出来る。ブロックの引数は、|インスタンスのSpriteUnit, 画像のSpriteUnit|となる。visibleメソッドの値がfalseのとき、選択が開始されていない時は描画されない。

dst

描画対象の画像インスタンス

返却値

自分自身を返す



446
447
448
449
450
451
# File 'lib/Miyako/API/choices.rb', line 446

def render_to(dst)
  return self unless @visible
  return self unless @now
  @now.base.each{|c| c.render_to(dst) }
  return self
end

#resetObject

選択肢のアニメーションの再生位置を最初に戻す

但し、まだ選択が開始されていなければ何もしない

返却値

自分自身を返す



585
586
587
588
589
# File 'lib/Miyako/API/choices.rb', line 585

def reset
  return self unless @now
  @now.base.each{|c| c.reset }
  return self
end

#resultObject

選択結果を取得する

現在の選択肢が所持している結果インスタンスを返すまだ選択が開始されていなければnilが返る

返却値

選択結果



311
312
313
314
# File 'lib/Miyako/API/choices.rb', line 311

def result
  return @result unless @now
  return @now.result
end

#right_choiceObject

選択肢を右移動させる

但し、まだ選択が開始されていなければ何もしない

返却値

自分自身を返す



525
526
527
528
529
530
531
532
533
534
# File 'lib/Miyako/API/choices.rb', line 525

def right_choice
  return self unless @now
  @last_selected = @now
  @now.selected = false
  obj = @now.right
  update_choices(@now, obj)
  @now = obj
  @now.selected = true
  return self
end

#select(x, y) ⇒ Object

選択肢を変更する

指定の位置の現在の選択状態を、選択状態にする

x

x方向位置

y

y方向位置

返却値

自分自身を返す

Raises:



372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/Miyako/API/choices.rb', line 372

def select(x, y)
  raise MiyakoError, "Not select yet!" unless @now
  raise MiyakoValueError, "Illegal choice position! [#{x}][#{y}]" if (x < 0 || x >= @choices.length || y < 0 || y >= @choices[x].length)
  @non_select = false
  @last_selected = @now
  @now.selected = false
  obj = @choices[x][y]
  update_choices(@now, obj)
  @now = obj
  @now.selected = true
  return self
end

#startObject

選択肢のアニメーションを開始する

但し、まだ選択が開始されていなければ何もしない

返却値

自分自身を返す



567
568
569
570
571
# File 'lib/Miyako/API/choices.rb', line 567

def start
  return self unless @now
  @now.base.each{|c| c.start }
  return self
end

#start_choice(x = 0, y = 0) ⇒ Object

選択を開始する

選択肢の初期位置を指定することができる第1引数にnilを渡すと、最後に選択した選択肢が最初に選択状態にある選択肢となる(例) choices.start_choice # [0]で示す位置の選択肢を選択するchoices.start_choice(5,1) # [5]で示す位置の選択肢を選択するchoices.start_choice(nil) # 最後に選択した選択肢を選択する(未選択だったときは全省略時の呼び出しと等価)

x

初期位置(x 座標)。規定値は 0。nilを渡すと、最後に選択した選択肢が選ばれる。

y

初期位置(y 座標)。規定値は 0

Raises:



271
272
273
274
275
276
277
278
# File 'lib/Miyako/API/choices.rb', line 271

def start_choice(x = 0, y = 0)
  raise MiyakoValueError, "Illegal choice position! [#{x}][#{y}]" if (x != nil && (x < 0 || x >= @choices.length || y < 0 || y >= @choices[x].length))
  @now = x ? @choices[x][y] : @last_selected
  @now.selected = true
  @last_selected = @now
  @non_select = false
  @result = nil
end

#stopObject

選択肢のアニメーションを終了させる

但し、まだ選択が開始されていなければ何もしない

返却値

自分自身を返す



576
577
578
579
580
# File 'lib/Miyako/API/choices.rb', line 576

def stop
  return self unless @now
  @now.base.each{|c| c.stop }
  return self
end

#to_sprite {|sprite| ... } ⇒ Object

スプライトに変換した画像を表示する

すべてのパーツを貼り付けた、1枚のスプライトを返す引数1個のブロックを渡せば、スプライトに補正をかけることが出来るただし、選択が開始されていなければnilを返す

返却値

生成したスプライト

Yields:

  • (sprite)


458
459
460
461
462
463
464
465
466
467
# File 'lib/Miyako/API/choices.rb', line 458

def to_sprite
  return nil unless @now
  rect = self.broad_rect
  sprite = Sprite.new(:size=>rect.to_a[2,2], :type=>:ac)
  Drawing.fill(sprite, [0,0,0])
  Bitmap.ck_to_ac!(sprite, [0,0,0])
  self.render_to(sprite){|sunit, dunit| sunit.x -= rect.x; sunit.y -= rect.y }
  yield sprite if block_given?
  return sprite
end

#up_choiceObject

選択肢を上移動させる

但し、まだ選択が開始されていなければ何もしない

返却値

自分自身を返す



539
540
541
542
543
544
545
546
547
548
# File 'lib/Miyako/API/choices.rb', line 539

def up_choice
  return self unless @now
  @last_selected = @now
  @now.selected = false
  obj = @now.up
  update_choices(@now, obj)
  @now = obj
  @now.selected = true
  return self
end

#update_animationObject

選択肢のアニメーションを更新させる

(手動で更新する必要があるときに呼び出す) 但し、まだ選択が開始されていなければ何もしない

返却値

各選択肢のupdate_spriteメソッドを呼び出した結果を配列として返す

ただし、現在選択中の配列リストではないときはを返す



596
597
598
599
# File 'lib/Miyako/API/choices.rb', line 596

def update_animation
  return [false] unless @now
  @now.base.map{|c| c.update_animation }
end

#update_layout_positionObject

:nodoc:



193
194
195
196
197
# File 'lib/Miyako/API/choices.rb', line 193

def update_layout_position #:nodoc:

  dx = @layout.pos[0] - rect[0]
  dy = @layout.pos[1] - rect[1]
  @choices.each{|ch| ch.each{|cc| cc.move!(dx, dy) } }
end

#update_layout_sizeObject

レイアウト空間の大きさを更新する

生成後、選択肢を追加した後の全体の大きさをレイアウト空間の大きさとして更新するただし、create_choicesめそっどを呼び出したときはこのメソッドを自動的に呼び出している

返却値

自分自身を返す



473
474
475
476
477
# File 'lib/Miyako/API/choices.rb', line 473

def update_layout_size
  trect = self.broad_rect
  set_layout_size(*trect.size)
  self
end