Skip to content

Instantly share code, notes, and snippets.

@themaxhero
Last active August 29, 2015 14:02
Show Gist options
  • Save themaxhero/d0b1c8659ad1cebf7fd4 to your computer and use it in GitHub Desktop.
Save themaxhero/d0b1c8659ad1cebf7fd4 to your computer and use it in GitHub Desktop.
RGSS Menu Class
#===============================================================================
# Game's Menu
#-------------------------------------------------------------------------------
=begin
License - MIT
Copyright (c) 2014 Marcelo Amancio de Lima Santos (maxhero)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=end
#===============================================================================
class Menu
attr_accessor :option_list,
:callbacks,
:x,
:y,
:index
#-----------------------------------------------------------------------------
# initialize method
#-----------------------------------------------------------------------------
def initialize(options,callbacks,x = 0,y = 0,index = 0)
@index = index
@x = x
@y = y
@back_signal = false
@option_list = options
@callbacks = callbacks
start
end
def start
create_objects
create_cursor
hide
setup_options
setup_cursor
draw_options
update_pos
update_cursor
display
end
#-----------------------------------------------------------------------------
# create methods
#-----------------------------------------------------------------------------
def create_objects
@options = Sprite.new
end
def create_cursor
@cursor = Sprite.new
@cx = 0
@cursor.zoom_x = 0.5
@cursor.zoom_y = 0.5
end
#-----------------------------------------------------------------------------
# calc methods
#-----------------------------------------------------------------------------
def calc_word_x(id)
id_word_width = text_size(@option_list[id]).width
biggest_word_width = text_size(@largest_word).width
return (biggest_word_width-id_word_width)/2
end
#-----------------------------------------------------------------------------
# set methods
#-----------------------------------------------------------------------------
=begin
def set_position(array)
@x = array[0]
@y = array[1]
end
=end
#-----------------------------------------------------------------------------
# setup methods
#-----------------------------------------------------------------------------
def setup_options
@largest_word = @option_list.max_by{|i| i.size}
width = text_size(@largest_word).width
height = text_size(@largest_word).height*@option_list.size
@options.bitmap = Bitmap.new(width,height)
@option_x = []
@option_y = []
for i in 0...@option_list.size
@option_x[i] = calc_word_x(i)
@option_y[i] = @options.y+(20*i)
end
end
def setup_cursor
@cursor.bitmap = Cache.title3("cursor")
@cur_posx = []
@cur_posy = []
for i in 0...@option_list.size
@cur_posx[i] = (@option_x[i])-40
@cur_posy[i] = (@option_y[i])+5
end
end
#-----------------------------------------------------------------------------
# draw methods
#-----------------------------------------------------------------------------
def draw_options
for i in 0...@option_list.size
@options.bitmap.mh_draw_text(@option_x[i],@option_y[i],@option_list[i])
end
end
#-----------------------------------------------------------------------------
# update methods
#-----------------------------------------------------------------------------
def update
update_input
update_cursor
update_pos
end
def update_input
if Input.trigger?(:C)
return run_callback(@index)
elsif Input.trigger?(:B)
Sound.play_cancel
return send_back_signal
end
if Input.trigger?(:UP)
Sound.play_cursor
@index = (@index-1) % @option_list.size
elsif Input.trigger?(:DOWN)
Sound.play_cursor
@index = (@index+1) % @option_list.size
end
end
def update_pos
@options.x = @x
@options.y = @y
end
def update_cursor
@cx = (@cx-1)%(12*2)
@cursor.x = @x+@cur_posx[@index] + (((@cx-12).abs)/1.5)
@cursor.y = @y+@cur_posy[@index]
end
#-----------------------------------------------------------------------------
#
#-----------------------------------------------------------------------------
def send_back_signal
@back_signal = true
end
def width
return @options.width
end
def height
return @options.height
end
def back_signal?
return @back_signal
end
def run_callback(id)
Graphics.update
Input.update
@callbacks[id].call
end
def display
@options.visible = true
@cursor.visible = true
end
def hide
@options.visible = false
@cursor.visible = false
end
def dispose
@cursor.dispose
@options.dispose
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment