[XP] Window_InputNumber Edit

Started by Wecoc, February 07, 2013, 12:20:31 am

Previous topic - Next topic

Wecoc

February 07, 2013, 12:20:31 am Last Edit: February 08, 2013, 12:27:48 am by Wecoc
Window_InputNumber Edit
Authors: Wecoc
Version: 1.1
Type: Message Add-On
Key Term: Message Add-on



Introduction

Today I bring a relatively simple edit of Window_InputNumber.
What it does is allow other characters besides the default numbers.
There are some examples and facts in the script, but you can add those you want at the top of the code.
Examples on the script:
default => default system, 0 to 9
binary => 0 and 1
hexa => 0 to 9 and A to F
upcase_letters => space and UPCASE letters
all_letters => space, UPCASE letters and lowcase letters
all_characters => soace, UPCASE letters, lowcase letters, 0 to 9, +, - , *, /, !, #, $, %, &, @, point, comma, dash



Features


  • Easy configurable

  • Automatic cursor width

  • You can use words too (for example "Hello", "World")




Screenshots

Spoiler: ShowHide






Script

Window_Input Edit 1.1
Spoiler: ShowHide

#==============================================================================
# Window_InputNumber Edit by Wecoc (version 1.1)
#==============================================================================

class Game_Temp
  attr_accessor :input_type
  alias input_type_initialize initialize
  def initialize
    input_type_initialize
    @input_type = "default"
  end
end

class Window_InputNumber < Window_Base
  def initialize(digits_max,type="default")
    case type
    when "default"
      @character_array = ["0","1","2","3","4","5","6","7","8","9"]
    when "binary"
      @character_array = ["0","1"]
    when "hexa"
      @character_array = ["0","1","2","3","4","5","6","7","8","9",
                          "A","B","C","D","E","F"]
    when "upcase_letters"
      @character_array = [" ","A","B","C","D","E","F","G","H","I",
                          "J","K","L","M","N","O","P","Q","R","S",
                          "T","U","V","W","X","Y","Z"]
    when "all_letters"
      @character_array = [" ","A","B","C","D","E","F","G","H","I",
                          "J","K","L","M","N","O","P","Q","R","S",
                          "T","U","V","W","X","Y","Z","a","b","c",
                          "d","e","f","g","h","i","j","k","l","m",
                          "n","o","p","q","r","s","t","u","v","w",
                          "x","y","z"]
    when "all_characters"
      @character_array = [" ","A","B","C","D","E","F","G","H","I",
                          "J","K","L","M","N","O","P","Q","R","S",
                          "T","U","V","W","X","Y","Z","a","b","c",
                          "d","e","f","g","h","i","j","k","l","m",
                          "n","o","p","q","r","s","t","u","v","w",
                          "x","y","z","0","1","2","3","4","5","6",
                          "7","8","9","+","-","*","/","!","#","$",
                          "%","&","@",".",",","-"]
    # when...
    end
    @digits_max = digits_max
    @type = type
    @letter_array = []
    @digits_max.times{@letter_array.push(0)}
    dummy_bitmap = Bitmap.new(32, 32)
   
    @cursor_width = 8
    for i in 0...@character_array.size
      letter_size = dummy_bitmap.text_size(@character_array[i]).width + 8
      @cursor_width = [@cursor_width, letter_size].max
    end
    dummy_bitmap.dispose
    super(0, 0, @cursor_width * @digits_max + 32, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.z += 9999
    self.opacity = 0
    @index = 0
    refresh
    update_cursor_rect
  end

  def letter(index)
    return @character_array[@letter_array[index]]
  end

  def increase_letter_array(index)
    if @letter_array[index] == (@character_array.size-1)
      @letter_array[index] = 0
    else
      @letter_array[index] += 1
    end
  end

  def decrease_letter_array(index)
    if @letter_array[index] == 0
      @letter_array[index] = (@character_array.size-1)
    else
      @letter_array[index] -= 1
    end
  end

  def update_cursor_rect
    self.cursor_rect.set(@index * @cursor_width, 0, @cursor_width, 32)
  end

  def update
    super
    if Input.repeat?(Input::UP)
      $game_system.se_play($data_system.cursor_se)
      increase_letter_array(@index)
      refresh
    end
    if Input.repeat?(Input::DOWN)
      $game_system.se_play($data_system.cursor_se)
      decrease_letter_array(@index)
      refresh
    end
    if Input.repeat?(Input::RIGHT)
      if @digits_max >= 2
        $game_system.se_play($data_system.cursor_se)
        @index = (@index + 1) % @digits_max
      end
    end
    if Input.repeat?(Input::LEFT)
      if @digits_max >= 2
        $game_system.se_play($data_system.cursor_se)
        @index = (@index + @digits_max - 1) % @digits_max
      end
    end
    if Input.trigger?(Input::B)
      @letter_array[@index] = 0
      refresh
    end
    update_cursor_rect
  end

  def refresh
    self.contents.clear
    self.contents.font.color = normal_color
    for i in 0...@digits_max
      self.contents.draw_text(i*@cursor_width,0,@cursor_width,32,letter(i),1)
    end
  end

  def number
    @number = []
    for i in 0...@digits_max
      @number.push(letter(i))
    end
    if @number.to_s.to_i != 0 and @number.to_s != "0"
      return @number.to_s.to_i
    else
      return @number.to_s
    end
  end

  def number=(number)
    if number != 0
      if number.is_a?(Integer)
        num = number.to_s
      else
        num = number
      end
      @array = num.scan(/./)
      if @array.size != @digits_max
        begin
          @array.unshift(@character_array[0])
        end until @array.size == @digits_max
      end
      for i in 0...@digits_max
        for j in 0..@character_array.size
          if @array[i] == @character_array[j]
            @letter_array[i] = j
          end
        end
      end
    end
    refresh
  end
end


Window_Message Edit
Spoiler: ShowHide
#==============================================================================
# ** Window_Message EDIT
#==============================================================================

class Window_Message < Window_Selectable
  def refresh
    self.contents.clear
    self.contents.font.color = normal_color
    x = y = 0
    @cursor_width = 0
    if $game_temp.choice_start == 0
      x = 8
    end
    if $game_temp.message_text != nil
      text = $game_temp.message_text
      begin
        last_text = text.clone
        text.gsub!(/\\[Vv]\[([0-9]+)\]/) { $game_variables[$1.to_i] }
      end until text == last_text
      text.gsub!(/\\[Nn]\[([0-9]+)\]/) do
        $game_actors[$1.to_i] != nil ? $game_actors[$1.to_i].name : ""
      end
      text.gsub!(/\\\\/) { "\000" }
      text.gsub!(/\\[Cc]\[([0-9]+)\]/) { "\001[#{$1}]" }
      text.gsub!(/\\[Gg]/) { "\002" }
      while ((c = text.slice!(/./m)) != nil)
        if c == "\000"
          c = "\\"
        end
        if c == "\001"
          text.sub!(/\[([0-9]+)\]/, "")
          color = $1.to_i
          if color >= 0 and color <= 7
            self.contents.font.color = text_color(color)
          end
          next
        end
        if c == "\002"
          if @gold_window == nil
            @gold_window = Window_Gold.new
            @gold_window.x = 560 - @gold_window.width
            if $game_temp.in_battle
              @gold_window.y = 192
            else
              @gold_window.y = self.y >= 128 ? 32 : 384
            end
            @gold_window.opacity = self.opacity
            @gold_window.back_opacity = self.back_opacity
          end
          next
        end
        if c == "\n"
          if y >= $game_temp.choice_start
            @cursor_width = [@cursor_width, x].max
          end
          y += 1
          x = 0
          if y >= $game_temp.choice_start
            x = 8
          end
          next
        end
        self.contents.draw_text(4 + x, 32 * y, 40, 32, c)
        x += self.contents.text_size(c).width
      end
    end
    if $game_temp.choice_max > 0
      @item_max = $game_temp.choice_max
      self.active = true
      self.index = 0
    end
    ######################### EDIT ############################
    if $game_temp.num_input_variable_id > 0
      digits_max = $game_temp.num_input_digits_max
      number = $game_variables[$game_temp.num_input_variable_id]
      @input_number_window = Window_InputNumber.new(digits_max, $game_temp.input_type)
      @input_number_window.number = number
      @input_number_window.x = self.x + 8
      @input_number_window.y = self.y + $game_temp.num_input_start * 32
    end
    ###########################################################
  end
  def update
    super
    if @fade_in
      self.contents_opacity += 24
      if @input_number_window != nil
        @input_number_window.contents_opacity += 24
      end
      if self.contents_opacity == 255
        @fade_in = false
      end
      return
    end
    ######################### EDIT ############################
    if @input_number_window != nil
      @input_number_window.update
      if Input.trigger?(Input::C)
        if @input_number_window.number.is_a?(Integer)
          input_develop = @input_number_window.number.to_s.gsub(/ /) { nil }
        else
          input_develop = @input_number_window.number.gsub(/ /) { nil }
        end
        if input_develop.empty?
          $game_system.se_play($data_system.buzzer_se)
          return
        end
        $game_system.se_play($data_system.decision_se)
        $game_variables[$game_temp.num_input_variable_id] =
          @input_number_window.number
        $game_map.need_refresh = true
        @input_number_window.dispose
        @input_number_window = nil
        terminate_message
      end
      return
    end
    ###########################################################
    if @contents_showing
      if $game_temp.choice_max == 0
        self.pause = true
      end
      if Input.trigger?(Input::B)
        if $game_temp.choice_max > 0 and $game_temp.choice_cancel_type > 0
          $game_system.se_play($data_system.cancel_se)
          $game_temp.choice_proc.call($game_temp.choice_cancel_type - 1)
          terminate_message
        end
      end
      if Input.trigger?(Input::C)
        if $game_temp.choice_max > 0
          $game_system.se_play($data_system.decision_se)
          $game_temp.choice_proc.call(self.index)
        end
        terminate_message
      end
      return
    end
    if @fade_out == false and $game_temp.message_text != nil
      @contents_showing = true
      $game_temp.message_window_showing = true
      reset_window
      refresh
      Graphics.frame_reset
      self.visible = true
      self.contents_opacity = 0
      if @input_number_window != nil
        @input_number_window.contents_opacity = 0
      end
      @fade_in = true
      return
    end
    if self.visible
      @fade_out = true
      self.opacity -= 48
      if self.opacity == 0
        self.visible = false
        @fade_out = false
        $game_temp.message_window_showing = false
      end
      return
    end
  end
end



Instructions

To call use the the numerical input command as always. If you want to change the type of input_number on game, you have to put this before the input call:
$game_temp.input_type = "(type)" (for example $game_temp.input_type = "upcase_letters")


Compatibility

See Author's Notes.


Credits and Thanks


  • Wecoc




Author's Notes

First script is the principal one and it has no compatibility problems. But second one controls calling (just as an example) and it's not compatible with message scripts. That's why they are separate. If you use some personalized message system, you can get the parts between ##### (edited parts) and paste on your script. If you have any problem with that don't hestitate to ask.

First script is also completely independent of the second one.

MOAL

Awesome, never seen a script like this anywhere before. It'll be very useful. :) *levels up*

LiTTleDRAgo

I didn't know if tag [RMXP] is allowed

Wecoc

February 07, 2013, 09:47:21 am #3 Last Edit: February 07, 2013, 11:05:23 am by Wecoc
Quote from: LiTTleDRAgo on February 07, 2013, 09:29:43 am
I didn't know if tag [RMXP] is allowed

Nope, it isn't. I did it accidentally, sorry. Fixed!

LiTTleDRAgo

bug, when you input number (not letters) to variable 1 (default)
the variable becomes string

@>Input Number : [0001], 6 digit(s)
@>Script : $game_variables[1] += 5


Wecoc

In conclusion, yesterday I had a very silly day. That bugfix was exactly the change I did to update version 1.0 to 1.1...
I posted the incorrect one. Ok that's it. I fixed the post again. Sorry, I will be more careful next time.