Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Scripting Tool: Scrollable Text

Status
Not open for further replies.
Scrollable Text
Version: 1.0

Introduction

This script is useful for windows that just displays information and doesn't select anything. You can set the speed the windows/sprite contents scroll. This is just two classes Window_Scrollable and Sprite Scrollable.

Screenshots

One looks like plain window and the other looks like some text on the screen.

Script

Code:
#==============================================================================
# ** Window_Scrollable
#------------------------------------------------------------------------------
#  This window class contains scroll functions.
#==============================================================================

class Window_Scrollable < Window_Base
  attr_accessor :speed
  attr_accessor :contents_rect
  attr_writer   :horizantal_scroll
  attr_writer   :vertical_scroll
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     x      : window x-coordinate
  #     y      : window y-coordinate
  #     width  : window width
  #     height : window height
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height)
    super(x, y, width, height)
    @contents_rect = Rect.new(0,0,width,height)
    @speed = 0
    @horizantal_scroll = false
    @vertical_scroll = false
  end
  #--------------------------------------------------------------------------
  # * Can be moved vertically?
  #--------------------------------------------------------------------------
  def vertical?
    return false if self.contents == nil
    return self.contents.height > self.height - 32
  end
  #--------------------------------------------------------------------------
  # * Can be moved horizantally?
  #--------------------------------------------------------------------------
  def horizantal?
    return false if self.contents == nil
    return self.contents.width > self.width - 32
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super
    return if not self.active
    if Input.repeat?(Input::UP)
      if self.oy > @contents_rect.y and @vertical_scroll and vertical?
        self.oy = [self.oy - speed, @contents_rect.y].max
      end
    end
    if Input.repeat?(Input::LEFT)
      if self.ox > @contents_rect.x and @horizantal_scroll and horizantal?
        self.ox = [self.ox - speed, @contents_rect.x].max
      end
    end
    if Input.repeat?(Input::DOWN)
      if self.oy < @contents_rect.height - (self.height - 32) and @vertical_scroll and vertical?
        self.oy = [self.oy + speed, @contents_rect.height].min
      end
    end
    if Input.repeat?(Input::RIGHT)
      if self.ox < @contents_rect.width - (self.width - 32)  and @horizantal_scroll and horizantal?
        self.ox = [self.ox - speed, @contents_rect.width].min
      end
    end
  end
end

Code:
class Scroll_Sprite < Sprite

  def initialize(x,y,width,height, viewport = nil)
    super(viewport)
    self.x = x
    self.y = y
    self.width = width
    self.height = height
  end
  
  def sx=(ox)
    self.src_rect.x = ox
  end
  
  def sy=(oy)
    self.src_rect.y = oy
  end
  
  def sx
    return self.src_rect.x
  end
  
  def sy
    return self.src_rect.y
  end
  
  def width
    return self.src_rect.width
  end
  
  def height
    return self.src_rect.height
  end
  
  def width=(width)
    self.src_rect.width = width
    @width = width
  end
  
  def height=(height)
    self.src_rect.height = height
    @height = height
  end
  
  def bitmap=(bitmap)
    super(bitmap)
    self.src_rect.width = @width
    self.src_rect.height = @height
  end
end

#==============================================================================
# ** Srite_Selectable
#------------------------------------------------------------------------------
#  This sprite class contains scroll functions.
#==============================================================================

class Sprite_Scrollable < Scroll_Sprite
  attr_accessor :speed
  attr_accessor :bitmap_rect
  attr_writer   :horizantal_scroll
  attr_writer   :vertical_scroll
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     x      : window x-coordinate
  #     y      : window y-coordinate
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height)
    super(x,y,width,height)
    @bitmap_rect = Rect.new(0,0,width,height)
    @speed = 0
    @horizantal_scroll = false
    @vertical_scroll = false
  end
  #--------------------------------------------------------------------------
  # * Get Normal Text Color
  #--------------------------------------------------------------------------
  def normal_color
    return Color.new(255, 255, 255, 255)
  end
  #--------------------------------------------------------------------------
  # * Get Disabled Text Color
  #--------------------------------------------------------------------------
  def disabled_color
    return Color.new(255, 255, 255, 128)
  end
  #--------------------------------------------------------------------------
  # * Get System Text Color
  #--------------------------------------------------------------------------
  def system_color
    return Color.new(192, 224, 255, 255)
  end
  
  def vertical?
    return false if self.bitmap == nil
    return self.bitmap.height > self.height
  end
      
  def horizantal?
    return false if self.bitmap == nil
    return self.bitmap.width > self.width
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super
    if Input.repeat?(Input::UP)
      if self.sy > @bitmap_rect.y and @vertical_scroll and vertical?
        self.sy = [self.sy - speed, @bitmap_rect.y].max
      end
    end
    if Input.repeat?(Input::LEFT)
      if self.sx > @bitmap_rect.x and @horizantal_scroll and horizantal?
        self.sx = [self.sx - speed, @bitmap_rect.x].max
      end
    end
    if Input.repeat?(Input::DOWN)
      if self.sy < @bitmap_rect.height - self.height and @vertical_scroll and vertical?
        self.sy = [self.sy + speed, @bitmap_rect.height].min
      end
    end
    if Input.repeat?(Input::RIGHT)
      if self.sx < @bitmap_rect.width - self.width and @horizantal_scroll and horizantal?
        self.sx = [self.sx - speed, @bitmap_rect.width].min
      end
    end
  end
end

Instructions

First you window must inherit from the class you wish to use

Window_Scrollable
for the window to be scrollable you must set the speed, which way the window is able to scroll, and the contents_rect

so in the initialize method or whatever

self.speed = speed
self.horizantal_scroll = boolean
self.vertical_scroll = boolean
self.contents_rect.set(0,0,width-32,height-32)

Sprite_Scrollable
This class is used for scrolling text, also used in my mission system for windows that have a header and adding a secondary window make everything look weird, this class was used to imitiate the contents of the window.

you set it up the same way,except contents_rect is replaced by bitmap_rect and to draw to the bitmap you use self.bitmap.draw_text, also included within the class is the normal color system color and disabled color

FAQ

Compatibility

Compatible with anything and everything

Author's Notes

Do not post I am confused what does this do or else as I stated this is a tool for scripters and not an actual script you plug into your project. If you need further instruction just ask.

Can be used for more advanced things.
 

Vent

Member

This is exactly what I've been looking for. Good timing ^_^


EDIT: Ok I have a question, how would I make my help window scroll?
 
UH, trickster, what exactly this script does? It makes possible for text be scrolleable, ok. So, i don't have to write something in a window and when it space is over create another window? Is this?
 
Status
Not open for further replies.

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top