Skip to content

Instantly share code, notes, and snippets.

@RPGP1
Created March 14, 2014 13:13
Show Gist options
  • Save RPGP1/9547431 to your computer and use it in GitHub Desktop.
Save RPGP1/9547431 to your computer and use it in GitHub Desktop.
# coding: UTF-8
require_relative '../mydxlibrary/push_keys'
class MenuBase < Sprite
attr_accessor :alp, :alp2
attr_reader :bgImage, :child, :parent
def initialize(x, y, bgImage)
super(x, y, RenderTarget.new(bgImage.width, bgImage.height))
@bgImage = bgImage
@child = nil
@parent = nil
@alp = 224
@alp2 = 72
self
end
def draw
self.image.draw(0,0,@bgImage,-10000)
self.alpha = (@child ? @alp2 : @alp)
super
@child.draw if @child
nil
end
def update(keys = Input.push_keys)
close if keys.include?(K_X)
@child.update(keys) if @child
nil
end
def close
@child.close if @child
@parent.child = nil
end
def bgImage=(v)
self.image = RenderTarget.new(v.width, v.height)
@bgImage = v
GC.start
end
def child=(v)
if @child != v
@child.end if @child
v.start if v
@child = v
@child.parent = self if @child
end
end
def start;end
def end;end
end
class CursorMenu < MenuBase
attr_accessor :cursor, :cur_pos, :cur_x, :cur_y
def initialize(x, y, bgImage, cursorImage, cursor_pos, cursor_x = 0, cursor_y = 0)
super(x, y, bgImage)
@cursor = cursorImage
@cur_pos = cursor_pos
#[[10, [10, 20, 30]], [30, [10, nil, 30]]]
#のように、カーソルの行き得るx座標とそれに対応するy座標の配列を指定
#nilにするとそこには止まらず、一つ次の位置に移動
@cur_x = cursor_x #cursor_posで何番目のx座標か
@cur_y = cursor_y #      y座標か
self
end
def draw
self.image.draw(@curpos[@cur_x][0], @cur_pos[@cur_x][1][@cur_y], @cursor, 10000)
super
end
def update(keys = Input.push_keys)
close if keys.include?(K_X)
unless @child
#@childが無ければカーソル移動
loop do
@cur_x += Input.push_x(keys) #キーに応じて@cur_xを増減
x_last = @cur_pos.size #@cur_xが
@cur_x = x_last if @cur_x < 0 #  0未満なら最後に
@cur_x = 0 if @cur_x > x_last #  最後を超えてたら0に
break if @cur_pos[@cur_x][1][@cur_y] #nilじゃ無ければ終了
end
loop do
@cur_y += Input.push_y(keys) #キーに応じて@cur_yを増減
y_last = @cur_pos[@cur_x][1].size #@cur_xが
@cur_y = y_last if @cur_y < 0 #  0未満なら最後に
@cur_y = 0 if @cur_y > y_last #  最後を超えてたら0に
break if @cur_pos[@cur_x][1][@cur_y] #nilじゃ無ければ終了
end
submit if keys.include?(K_RETURN)
else
@child.update(keys)
end
end
def submit;end
def cursorImage;@cursor;end
def cursorImage=(v);@cursor = v;end
def cursor_pos;@cur_pos;end
def cursor_pos=(v);@cur_pos = v;end
def cursor_x;@cur_x;end
def cursor_x=(v);@cur_x = v;end
def cursor_y;@cur_y;end
def cursor_y=(v);@cur_y = v;end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment