Window Additions

Started by fugibo, July 15, 2010, 01:11:55 pm

Previous topic - Next topic

fugibo


=begin
- With this script, you can now create animations:
Window_Animation.resize 200, 200 # resize a window to 200x200
Window_Animation.move 50, 50 # move a window to (50, 50)

# this one makes a window hover up and down
Window_Animation.new do |window|
 window.displacement_x = window.displacement_y = Math::sin((Graphics.framecount % 360) * Math::PI / 180)
end

- You can also add objects to windows as thus:
window << object # the method named "insert_<object class>" is called, i.e. for a string "insert_String" would be called
window << animation # adds the animation, so that window#update will call it
=end
class Window_Animation
   
 def self.resize width, height
   return self.new do |window|
     if window.width > width
       window.width -= 2
     elsif window.width < width
       window.width += 2
     end

     if window.height > height
       window.height -= 2
     elsif window.height < height
       window.height += 2
     end
   end
 end
 
 def self.move x, y
   return self.new do |window|
     if window.x > x
       window.x -= 1
     elsif window.x < x
       window.x += 1
     end
     
     if window.y > y
       window.y -= 1
     elsif window.y < y
       window.y += 1
     end
   end
 end
 
 def self.proc &block
   return self.new(block)
 end
 
 def initialize &block
   @block = block
   @hash = {}
 end
 
 def call window
   @block.call window
 end
 
end

class Window
 
 attr_reader :displacement_x, :displacement_y
 
 alias fugibo_window_additions_initialize_alias initialize
 def initialize *args
   fugibo_window_additions_initialize_alias(args)
   @animations = []
   @displacement_x = 0
   @displacement_y = 0
 end
 
 def << object
   send 'insert_#{object.class}', object
 end
 
 def insert_Window_Animation animation
   @animations << animation
 end
 
 def insert_Proc proc
   @animations << Window_Animation.proc(proc)
 end
 
 alias fugibo_window_additions_update_alias update
 def update
   fugibo_window_additions_update_alias()
   @animations.each do |animation|
     animation.call(self)
   end
 end
 
 def displacement_x= x
   self.x += x - @displacement_x
   @displacement_x = x
   
   return x
 end
 
 def displacement_y= y
   self.y += y - @displacement_y
   @displacement_y = y
   
   return y
 end
 
end


a scripter's "tool." some methods added to the Window class and a new class (Window_Animation, not sure if that name's taken already). neat example (assuming the code works, which it should:)


w = Window.new
w.width = 160
w.height = 120
w.x = 240
w.y = 180

w << Window_Animation.move 120, 80
w << Window_Animation.resize 320, 240
w << Window_Animation.new do |window|
 w.angle = Graphics.framecount % 40 - 20
end

loop do
 Input.update
 Graphics.update
 w.update
 
 if Input.trigger? Input::C
   w.destroy
   break
 end
end