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, #oh=, #ow=, #ox, #ox=, #oy, #oy=, #part_rect, #render_d, #render_xy, #render_xy_to, #show, #to_unit, #update

Constructor Details

#initializeChoices

インスタンスを生成する

返却値

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



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/Miyako/API/choices.rb', line 181

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)

選択肢配列の集合



175
176
177
# File 'lib/Miyako/API/choices.rb', line 175

def choices
  @choices
end

#layout_spacesObject (readonly)

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



177
178
179
# File 'lib/Miyako/API/choices.rb', line 177

def layout_spaces
  @layout_spaces
end

#name_to_choiceObject (readonly)

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



176
177
178
# File 'lib/Miyako/API/choices.rb', line 176

def name_to_choice
  @name_to_choice
end

#visibleObject

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



174
175
176
# File 'lib/Miyako/API/choices.rb', line 174

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構造体のインスタンス



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

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



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

def __getobj__
  @choices
end

#__setobj__(obj) ⇒ Object



199
200
# File 'lib/Miyako/API/choices.rb', line 199

def __setobj__(obj)
end

#any_select?Boolean

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

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

返却値

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

Returns:

  • (Boolean)


368
369
370
# File 'lib/Miyako/API/choices.rb', line 368

def any_select?
  return !@non_select
end

#attach(x, y) ⇒ Object

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

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

x

x方向位置

y

y方向位置

返却値

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



406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/Miyako/API/choices.rb', line 406

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)


425
426
427
428
429
# File 'lib/Miyako/API/choices.rb', line 425

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メソッドの値)



334
335
336
337
# File 'lib/Miyako/API/choices.rb', line 334

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

#bodyObject

選択肢本体を取得する

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

返却値

選択肢本体(選択時)



316
317
318
319
# File 'lib/Miyako/API/choices.rb', line 316

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

#broad_rectObject

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

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

返却値

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



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/Miyako/API/choices.rb', line 497

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)


272
273
274
# File 'lib/Miyako/API/choices.rb', line 272

def choicing?
  return @now != nil
end

#create_choices(choices) ⇒ Object

選択肢を登録する

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

choices

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

返却値

レシーバ



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/Miyako/API/choices.rb', line 240

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

選択肢データを解放する



263
264
265
266
267
# File 'lib/Miyako/API/choices.rb', line 263

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

#down_choiceObject

選択肢を下移動させる

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

返却値

自分自身を返す



567
568
569
570
571
572
573
574
575
576
# File 'lib/Miyako/API/choices.rb', line 567

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)


375
376
377
378
379
# File 'lib/Miyako/API/choices.rb', line 375

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



301
302
303
304
305
306
307
308
309
# File 'lib/Miyako/API/choices.rb', line 301

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:



202
203
204
205
# File 'lib/Miyako/API/choices.rb', line 202

def initialize_copy(obj) #:nodoc:
  @choices = @choices.dup
  copy_layout
end

#left_choiceObject

選択肢を左移動させる

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

返却値

自分自身を返す



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

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

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

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

返却値

自分自身を返す



359
360
361
362
363
# File 'lib/Miyako/API/choices.rb', line 359

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

#ohObject

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

返却値

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



623
624
625
# File 'lib/Miyako/API/choices.rb', line 623

def oh
  return self.size[1]
end

#owObject

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

返却値

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



617
618
619
# File 'lib/Miyako/API/choices.rb', line 617

def ow
  return self.size[0]
end

#rectObject

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

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

返却値

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



518
519
520
# File 'lib/Miyako/API/choices.rb', line 518

def rect
  return self.broad_rect
end

#regist_layout_space(name, layout_space) ⇒ Object

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

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

name

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

返却値

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



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

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

#renderObject

画面に描画を指示する

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

返却値

自分自身を返す



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

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

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

返却値

自分自身を返す



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

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

#resetObject

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

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

返却値

自分自身を返す



599
600
601
602
603
# File 'lib/Miyako/API/choices.rb', line 599

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

#resultObject

選択結果を取得する

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

返却値

選択結果



325
326
327
328
# File 'lib/Miyako/API/choices.rb', line 325

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

#right_choiceObject

選択肢を右移動させる

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

返却値

自分自身を返す



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

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:



386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/Miyako/API/choices.rb', line 386

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

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

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

返却値

自分自身を返す



581
582
583
584
585
# File 'lib/Miyako/API/choices.rb', line 581

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:



285
286
287
288
289
290
291
292
# File 'lib/Miyako/API/choices.rb', line 285

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

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

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

返却値

自分自身を返す



590
591
592
593
594
# File 'lib/Miyako/API/choices.rb', line 590

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

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

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

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

返却値

生成したスプライト

Yields:

  • (sprite)


472
473
474
475
476
477
478
479
480
481
# File 'lib/Miyako/API/choices.rb', line 472

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

選択肢を上移動させる

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

返却値

自分自身を返す



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

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メソッドを呼び出した結果を配列として返す

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



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

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

#update_layout_positionObject

:nodoc:



207
208
209
210
211
# File 'lib/Miyako/API/choices.rb', line 207

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めそっどを呼び出したときはこのメソッドを自動的に呼び出している

返却値

自分自身を返す



487
488
489
490
491
# File 'lib/Miyako/API/choices.rb', line 487

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