Advertisement
LiTTleDRAgo

[RGSS2] Scene_InputSimple

Mar 21st, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.15 KB | None | 0 0
  1. #==============================================================================
  2. # ** Scene_InputSimple
  3. # Version : 1.00
  4. # Author : LiTTleDRAgo
  5. #==============================================================================
  6. class Scene_InputSimple
  7.   #--------------------------------------------------------------------------
  8.   # * initialize
  9.   #--------------------------------------------------------------------------
  10.   def initialize(text, icon,digit_max, x=0 , y=0,can_cancel = true)
  11.     @text = text.to_s
  12.     @digit_max = digit_max
  13.     @pos_x = x
  14.     @pos_y = y
  15.     @can_cancel = can_cancel
  16.     cache = Cache
  17.     if icon[/Item(\d+)/i]
  18.       @icon = $data_items[$1.to_i]
  19.     elsif icon[/Armor(\d+)/i]
  20.       @icon = $data_armors[$1.to_i]
  21.     elsif icon[/Weapon(\d+)/i]
  22.       @icon = $data_weapons[$1.to_i]
  23.     elsif icon[/Skill(\d+)/i]
  24.       @icon = $data_skills[$1.to_i]
  25.     end
  26.   end
  27.   #--------------------------------------------------------------------------
  28.   # * main
  29.   #--------------------------------------------------------------------------
  30.   def main
  31.     create_bakground_map
  32.     create_things
  33.     create_transition
  34.     update while $scene == self
  35.     dispose
  36.   end
  37.   #--------------------------------------------------------------------------
  38.   # * create_bakground_map
  39.   #--------------------------------------------------------------------------
  40.   def create_bakground_map
  41.     @menuback_sprite = Sprite.new
  42.     @menuback_sprite.bitmap = $game_temp.background_bitmap
  43.     @menuback_sprite.color.set(16, 16, 16, 128)
  44.   end
  45.   #--------------------------------------------------------------------------
  46.   # * create_things
  47.   #--------------------------------------------------------------------------
  48.   def create_things
  49.     bitmap = Bitmap.new(400,400)
  50.     width = bitmap.text_size(@text).width
  51.     @window_input = Window_InputNumberXP.new(@digit_max)
  52.     text = @window_input.width
  53.     @window_master = Window_Base.new(@pos_x,@pos_y,width+text+60,64)
  54.     @window_input.x = @window_master.x + width + 12
  55.     @window_input.y = @window_master.y + 4
  56.     @all_window = [@window_master,@window_input,@menuback_sprite]
  57.     @window_master.contents = Bitmap.new(@window_master.width-32,@window_master.height-32)
  58.     @window_master.contents.draw_text(3,4,width,32,@text)
  59.     if @icon
  60.       @window_master.contents.draw_text(width+text,4,width,32,'x')
  61.       @window_master.draw_icon(@icon.icon_index, width+text+8,4, true)
  62.     end
  63.   end
  64.   #--------------------------------------------------------------------------
  65.   # * create_transition
  66.   #--------------------------------------------------------------------------
  67.   def create_transition
  68.     Graphics.transition
  69.   end
  70.   #--------------------------------------------------------------------------
  71.   # * Frame Update
  72.   #--------------------------------------------------------------------------
  73.   def update
  74.     update_grafis
  75.     update_sesungguhnya
  76.     @window_input.update
  77.   end
  78.   #--------------------------------------------------------------------------
  79.   # * update_grafis
  80.   #--------------------------------------------------------------------------
  81.   def update_grafis
  82.     [Graphics,Input].each {|s|s.update}
  83.   end
  84.   #--------------------------------------------------------------------------
  85.   # * update_sesungguhnya
  86.   #--------------------------------------------------------------------------
  87.   def update_sesungguhnya
  88.     if Input.trigger?(Input::C)
  89.       Sound.play_decision
  90.       $output_number = @window_input.number
  91.       $scene = Scene_Map.new
  92.     end
  93.     if Input.trigger?(Input::B)
  94.       return Sound.play_buzzer if !@can_cancel
  95.       Sound.play_cancel
  96.       $output_number = 0
  97.       $scene = Scene_Map.new
  98.     end
  99.   end
  100.   #--------------------------------------------------------------------------
  101.   # * Frame Dispose
  102.   #--------------------------------------------------------------------------
  103.   def dispose
  104.     @all_window.flatten.each {|s| s.dispose}
  105.   end
  106. end
  107.  
  108. #==============================================================================
  109. # ** Window_InputNumber
  110. #------------------------------------------------------------------------------
  111. #  This window is for inputting numbers, and is used within the
  112. #  message window.
  113. #==============================================================================
  114.  
  115. class Window_InputNumberXP < Window_Base
  116.   #--------------------------------------------------------------------------
  117.   # * Object Initialization
  118.   #     digits_max : digit count
  119.   #--------------------------------------------------------------------------
  120.   def initialize(digits_max)
  121.     @digits_max = digits_max
  122.     @number = 0
  123.     # Calculate cursor width from number width (0-9 equal width and postulate)
  124.     dummy_bitmap = Bitmap.new(32, 32)
  125.     @cursor_width = dummy_bitmap.text_size("0").width + 8
  126.     dummy_bitmap.dispose
  127.     super(0, 0, @cursor_width * @digits_max + 32, 64)
  128.     self.contents = Bitmap.new(width - 32, height - 32)
  129.     self.z += 9999
  130.     self.opacity = 0
  131.     @index = 0
  132.     refresh
  133.     update_cursor_rect
  134.   end
  135.   #--------------------------------------------------------------------------
  136.   # * Get Number
  137.   #--------------------------------------------------------------------------
  138.   def number
  139.     return @number
  140.   end
  141.   #--------------------------------------------------------------------------
  142.   # * Set Number
  143.   #     number : new number
  144.   #--------------------------------------------------------------------------
  145.   def number=(number)
  146.     @number = [[number, 0].max, 10 ** @digits_max - 1].min
  147.     refresh
  148.   end
  149.   #--------------------------------------------------------------------------
  150.   # * Cursor Rectangle Update
  151.   #--------------------------------------------------------------------------
  152.   def update_cursor_rect
  153.     self.cursor_rect.set(@index * @cursor_width, 0, @cursor_width, 32)
  154.   end
  155.   #--------------------------------------------------------------------------
  156.   # * Frame Update
  157.   #--------------------------------------------------------------------------
  158.   def update
  159.     super
  160.     if Input.repeat?(Input::UP) or Input.repeat?(Input::DOWN)
  161.        Sound.play_cursor
  162.       place = 10 ** (@digits_max - 1 - @index)
  163.       n = @number / place % 10
  164.       @number -= n * place
  165.       n = (n + 1) % 10 if Input.repeat?(Input::UP)
  166.       n = (n + 9) % 10 if Input.repeat?(Input::DOWN)
  167.       @number += n * place
  168.       refresh
  169.     end
  170.     if Input.repeat?(Input::RIGHT)
  171.       if @digits_max >= 2
  172.          Sound.play_cursor
  173.         @index = (@index + 1) % @digits_max
  174.       end
  175.     end
  176.     if Input.repeat?(Input::LEFT)
  177.       if @digits_max >= 2
  178.          Sound.play_cursor
  179.         @index = (@index + @digits_max - 1) % @digits_max
  180.       end
  181.     end
  182.     update_cursor_rect
  183.   end
  184.   #--------------------------------------------------------------------------
  185.   # * Refresh
  186.   #--------------------------------------------------------------------------
  187.   def refresh
  188.     self.contents.clear
  189.     self.contents.font.color = normal_color
  190.     s = sprintf("%0*d", @digits_max, @number)
  191.     for i in 0...@digits_max
  192.       self.contents.draw_text(i * @cursor_width + 4, 0, 32, 32, s[i,1])
  193.     end
  194.   end
  195. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement