Advertisement
roninator2

Efeberk's Show Text Box with a Message

Dec 7th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.34 KB | None | 0 0
  1. # Coder : efeberk
  2. # Mod : Roninator2
  3. #
  4. # Show text box with a message
  5. #
  6. # Script call:
  7. # show_textbox(x, y, width, height, variable, title = nil, txt = "Hello!")
  8. #
  9. # x : X position for the window
  10. # y : y position for the window
  11. # height : message window height
  12. # width : message window width
  13. # variable : which variable will be used to display text, 0 = not used
  14. # title : optional
  15. # txt : text to display if variable is not used.
  16. #
  17. # Sample : show_textbox(120, 50, 270, 48, 0, "PIN Code", "Looks like it says 25871")
  18. # Sample : show_textbox(120, 50, 270, 48, 10, "PIN Code", "")
  19. # Variable 10 = "Looks like it says 25871"
  20.  
  21. class Window_TextBox < Window_Base
  22.  
  23.   attr_accessor :enabled
  24.   attr_reader :finished
  25.  
  26.   def initialize(x, y, width, height, var, title, txt)
  27.     @txt = txt
  28.     @var = var
  29.     @title = title
  30.     @x = x
  31.     @y = y
  32.     @width = width
  33.     @finished = false
  34.     super(x, y, width, height)
  35.   end
  36.  
  37.   def update
  38.     self.contents.clear
  39.     update_textbox if @var != 0 || @txt != ""
  40.     update_keyboard
  41.   end
  42.  
  43.   def update_keyboard
  44.     if Input.trigger?(:C)
  45.       @finished = true
  46.       @text = ""
  47.     end
  48.   end
  49.  
  50.   def update_textbox
  51.     if @var != 0
  52.       draw_text_ex(@width / 2 - @var.size * 6, 0, @var)
  53.     else
  54.       draw_text_ex(0, 0, @txt)
  55.     end
  56.   end
  57.  
  58. end
  59.  
  60. class Scene_TextBox < Scene_Base
  61.  
  62.   def prepare(x, y, width, height, var, title, txt)
  63.     @x, @y, @width, @height, @var, @title, @txt = x, y, width, height, $game_variables[var.to_i], title, txt
  64.   end
  65.  
  66.   def start
  67.     super
  68.     create_background
  69.     create_textbox
  70.   end
  71.    
  72.   def create_background
  73.     @background_sprite = Spriteset_Map.new
  74.   end
  75.  
  76.   def create_textbox
  77.     if @title
  78.       @header = Window_Base.new(@x+@width/2-@title.size*6, @y-48, @title.size*12, 48)
  79.       @header.draw_text(0, 0, @header.contents_width, @header.contents_height, @title, 1)
  80.     end
  81.     @textbox = Window_TextBox.new(@x, @y, @width, @height, @var, @title, @txt)
  82.   end
  83.  
  84.   def update
  85.     super
  86.     if @textbox.finished then return_scene end
  87.   end
  88.  
  89. end
  90.  
  91. class Game_Interpreter
  92.  
  93.   def show_textbox(x, y, width, height, var = 0, title = nil, txt = "")
  94.     SceneManager.call(Scene_TextBox)
  95.     SceneManager.scene.prepare(x, y, width, height, var, title, txt)
  96.     Fiber.yield while SceneManager.scene_is?(Scene_TextBox)
  97.   end
  98.  
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement