01-05-2022, 04:25 AM
(This post was last modified: 01-07-2022, 03:03 AM by DerVVulfman.)
Keyboard Name Input - Updated
based on Vinardo's original work (October 2008)
Version: 1.0
based on Vinardo's original work (October 2008)
Version: 1.0
Introduction
This script replaces the default Name Entry system. No longer will the player be forced to highlight and select individual letters from a display to spell out a name, and allow the name to be typed using the keyboard itself.
Script
The Script
Code:
#==============================================================================
# ** Keyboard Name Input - Updated
# edit by DerVVulfman
# based on original work by vinardo (v 1.0 - October 15, 2008)
#------------------------------------------------------------------------------
# Requires Keyboard Input Module by SephirothSpawn
#==============================================================================
module NameKeyboard
# Basic configs
DELAY = 4 # Delay in frames per keystroke
BLANK = "_" # Space used to represent missing/empty character
# Set the Fonts used in the window
FONTNAME = "Arial"
FONTSIZE = 24
# Set the horitontal spacing and character height for each letter
FONTHEIGHT = 32
FONTWIDTH = 22
end
#==============================================================================
# ** Window_NameEdit
#------------------------------------------------------------------------------
# This window is used to edit your name on the input name screen.
#==============================================================================
class Window_NameEdit < Window_Base
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
attr_reader :name # name
attr_reader :index # cursor position
#--------------------------------------------------------------------------
# * Object Initialization
# actor : actor
# max_char : maximum number of characters
#--------------------------------------------------------------------------
def initialize(actor, max_char)
super(20, 80, 600, 128)
# Set bitmap contents and font
self.contents = Bitmap.new(width - 32, height - 32)
self.contents.font.name = NameKeyboard::FONTNAME
self.contents.font.size = NameKeyboard::FONTSIZE
# Set character spacing
@fheight = NameKeyboard::FONTHEIGHT
@fwidth = NameKeyboard::FONTWIDTH
# Set actor data
@actor = actor
@name = actor.name
@max_char = max_char
# Fit name within maximum number of characters
name_array = @name.split(//)[0...@max_char]
@name = ""
for i in 0...name_array.size
@name += name_array[i]
end
@default_name = @name
@index = name_array.size
refresh
update_cursor_rect
end
#--------------------------------------------------------------------------
# * Return to Default Name
#--------------------------------------------------------------------------
def restore_default
@name = @default_name
@index = @name.split(//).size
refresh
update_cursor_rect
end
#--------------------------------------------------------------------------
# * Add Character
# character : text character to be added
#--------------------------------------------------------------------------
def add(character)
return unless (@index < @max_char and character != "")
@name += character
@index += 1
refresh
update_cursor_rect
end
#--------------------------------------------------------------------------
# * Delete Character
#--------------------------------------------------------------------------
def back
return unless @index > 0
# Delete 1 text character
name_array = @name.split(//)
@name = ""
for i in 0...name_array.size-1
@name += name_array[i]
end
@index -= 1
refresh
update_cursor_rect
end
#--------------------------------------------------------------------------
# * Refresh
#--------------------------------------------------------------------------
def refresh
self.contents.clear
# Draw name
name_array = @name.split(//)
for i in 0...@max_char
c = name_array[i]
c = NameKeyboard::BLANK if c.nil?
x = 320 - @max_char * (@fwidth/2) + i * @fwidth
self.contents.draw_text(x, 32, @fwidth, @fheight, c, 1)
end
# Draw graphic
draw_actor_graphic(@actor, 320 - @max_char * (@fwidth/2) - 40, 80)
end
#--------------------------------------------------------------------------
# * Cursor Rectangle Update
#--------------------------------------------------------------------------
def update_cursor_rect
x = 320 - @max_char * (@fwidth/2) + @index * @fwidth
self.cursor_rect.set(x, 32, @fwidth, @fheight)
end
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
super
update_cursor_rect
end
end
#==============================================================================
# ** Scene_Name
#------------------------------------------------------------------------------
# This class performs name input screen processing.
#==============================================================================
class Scene_Name
#--------------------------------------------------------------------------
# * Main Processing
#--------------------------------------------------------------------------
def main
@actor = $game_actors[$game_temp.name_actor_id]
@waiting = 0
@edit_window = Window_NameEdit.new(@actor, $game_temp.name_max_char)
@spriteset = Spriteset_Map.new
Graphics.transition
loop do
Graphics.update
Input.update
update
break if $scene != self
end
Graphics.freeze
@edit_window.dispose
@spriteset.dispose
end
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
# Update windows
@edit_window.update
# Get keyboard input
letter = Input.get_letters
number = Input.get_numbers
key = Input.get_key
# Accept input if delay is over
if @waiting == 0
update_input(letter, number, key)
# Otherwise, decrease delay-
else
@waiting -= 1 if @waiting > 0
end
update_enter
end
#--------------------------------------------------------------------------
# * Frame Update (when wait timer expired)
# letter : letter pressed
# number : number pressed
# key : miscellaneous key pressed
#--------------------------------------------------------------------------
def update_input(letter, number, key)
update_letters(letter) # Test and process letter input
update_numbers(number) # Test and process number input
update_keys(key) # Test and process misc key input
update_backspace # Test and process backspace key
update_escape # Test and process escape key
end
#--------------------------------------------------------------------------
# * Frame Update (when the letter keys were depressed)
# letter : letter pressed
#--------------------------------------------------------------------------
def update_letters(letter)
# Exit of no key pressed
return if letter.nil?
# Play Decision SE, add to the edit window, and set the delay
$game_system.se_play($data_system.decision_se)
@edit_window.add(Input.get_letters)
@waiting = NameKeyboard::DELAY
end
#--------------------------------------------------------------------------
# * Frame Update (when the number keys were depressed)
# number : number pressed
#--------------------------------------------------------------------------
def update_numbers(number)
# Exit of no key pressed
return if number.nil?
# Play Decision SE, add to the edit window, and set the delay
$game_system.se_play($data_system.decision_se)
@edit_window.add(Input.get_numbers)
@waiting = NameKeyboard::DELAY
end
#--------------------------------------------------------------------------
# * Frame Update (when the miscellaneous keys were depressed)
# key : miscellaneous key pressed
#--------------------------------------------------------------------------
def update_keys(key)
# Exit of no key pressed
return if key.nil?
# Play Decision SE, add to the edit window, and set the delay
$game_system.se_play($data_system.decision_se)
@edit_window.add(Input.get_key)
@waiting = NameKeyboard::DELAY
end
#--------------------------------------------------------------------------
# * Frame Update (when the backspace key was depressed)
#--------------------------------------------------------------------------
def update_backspace
# Exit unless backspace key pressed
return unless Input.get_function == 'Back'
# Play Decision SE, add to the edit window, and set the delay
$game_system.se_play($data_system.cancel_se)
@edit_window.back
@waiting = NameKeyboard::DELAY
end
#--------------------------------------------------------------------------
# * Frame Update (when the escape key was depressed)
#--------------------------------------------------------------------------
def update_escape
# Exit unless escape key pressed
return unless Input.get_function == 'Esc'
# Play Decision SE, add to the edit window, and set the delay
@edit_window.restore_default
$game_system.se_play($data_system.buzzer_se)
@waiting = NameKeyboard::DELAY
end
#--------------------------------------------------------------------------
# * Frame Update (when the enter key was depressed)
#--------------------------------------------------------------------------
def update_enter
# Exit unless enter key pressed
return unless Input.get_function == 'Enter'
# Play Decision SE, set the actor's name, and exit to the map
@actor.name = @edit_window.name
$game_system.se_play($data_system.decision_se)
$scene = Scene_Map.new
end
end
Requires SephirothSpawn's Input module
The Script
Code:
#==============================================================================
# ** Input
#------------------------------------------------------------------------------
# A module that handles input data from a gamepad or keyboard.
#==============================================================================
module Input
#--------------------------------------------------------------------------
Back = 8
Shift = 16
Ctrl = 17
Alt = 18
Esc = 27
Space = 32
Numberpad = (33..40).to_a + [12, 45]
Numbers = (48..57).to_a
Letters = (65..90).to_a
FKeys = (112..123).to_a
Collon = 186
Equal = 187
Comma = 188
Underscore = 189
Dot = 190
Backslash = 191
LB = 219
RB = 221
Enter = 13
Quote = 222
#--------------------------------------------------------------------------
def Input.getstate(key)
unless Win32API.new("user32","GetKeyState",['i'],'i').call(key).between?(0, 1)
return true
end
return false
end
#--------------------------------------------------------------------------
def Input.get_direction
for key in Numberpad
if Input.getstate(key)
case key
when 45 ; return "0" # Same As Insert
when 33 ; return "9" # Same As Page Up
when 34 ; return "3" # Same As Page Down
when 35 ; return "1" # Same As Page End
when 36 ; return "7" # Same As Page Home
when 37 ; return "4" # Same As Left
when 38 ; return "8" # Same As Up
when 39 ; return "6" # Same As Right
when 40 ; return "2" # Same As Down
when 12 ; return "5"
end
end
end
end
#--------------------------------------------------------------------------
def Input.get_function
if Input.getstate(Back)
return 'Back'
elsif Input.getstate(Backslash)
return '/'
elsif Input.getstate(Esc)
return 'Esc'
elsif Input.getstate(Ctrl)
return 'Ctrl'
elsif Input.getstate(Alt)
return 'Alt'
elsif Input.getstate(Enter)
return 'Enter'
end
for key in FKeys
if Input.getstate(key)
num = key - 111
return 'F' + num.to_s
end
end
return nil
end
#--------------------------------------------------------------------------
def Input.get_letters
for key in Letters
if Input.getstate(key)
return (Input.getstate(Shift) ? key.chr.upcase : key.chr.downcase)
end
end
return nil
end
#--------------------------------------------------------------------------
def Input.get_numbers
return nil if Input.getstate(Shift)
for key in Numbers
return key.chr if Input.getstate(key)
end
return nil
end
#--------------------------------------------------------------------------
def Input.get_key
if Input.getstate(Space)
return ' '
elsif Input.getstate(Backslash) and Input.getstate(Shift)
return '?'
elsif Input.getstate(LB)
return (Input.getstate(Shift) ? '{' : '[')
elsif Input.getstate(RB)
return (Input.getstate(Shift) ? '}' : ']')
elsif Input.getstate(Comma)
return (Input.getstate(Shift) ? '<' : ',')
elsif Input.getstate(Dot)
return (Input.getstate(Shift) ? '>' : '.')
elsif Input.getstate(Collon)
return (Input.getstate(Shift) ? ':' : ';')
elsif Input.getstate(Equal)
return (Input.getstate(Shift) ? '+' : '=')
elsif Input.getstate(Underscore)
return (Input.getstate(Shift) ? '_' : '-')
elsif Input.getstate(Quote)
return (Input.getstate(Shift) ? "\"" : "'")
end
for key in Numbers
if Input.getstate(key) and Input.getstate(Shift)
case key
when 48 ; return ')'
when 49 ; return '!'
when 50 ; return '@'
when 51 ; return '#'
when 52 ; return '$'
when 53 ; return '%'
when 54 ; return '^'
when 55 ; return '&'
when 56 ; return '*'
when 57 ; return '('
end
end
end
return nil
end
end
FAQ
Pretty basic with a simple configuration script. Other than that, it is plug-and-play
Compatibility
Meant for the default RPGMaker XP system, replaces the default Name Edit system
Terms and Conditions
Free for use, even in commercial projects. Only due credit is required.
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)
Above are clickable links