Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Make the following corrections to the program hover _ button _ test.rb: - The button should have a black border around it when the mouse

Make the following corrections to the program hover_button_test.rb:
- The button should have a black border around it when the mouse is moved over it to highlight it.
- At the bottom of the screen there should be a display of the mouse x and y locations at all times (not just when the mouse is clicked)
- When the button is clicked the background should change to yellow, if the window area outside the button is clicked the background should change to white.
- Make sure the button works as per the test data below: Description
The screen should look as follows
when the mouse is NOT over the
button:
The screen should look as follows
when the mouse IS over the button:
end
WIN_WIDTH =640
WIN_HEIGHT =400
class DemoWindow Gosu::Window
def initialize
super(WIN_WIDTH, WIN_HEIGHT, false)
@background = Gosu::Color::WHITE
@button_font = Gosu::Font.new(20)
@info_font = Gosu::Font.new(10)
@button_highlight = Gosu::Color.rgb(170,170,170)
@button_normal = Gosu::Color::GREEN
@button_border = Gosu::Color::BLACK
@button_hovered = false
@button_rect = Rectangle.new(50,50,100,50)
end
(20)4 def draw
Gosu.draw_rect(o.,0, WIN_WIDTH, WIN_HEIGHT, @background, ZOrder::BA
# Draw the rectangle that provides the background
if @button_hovered
/home/hover_button_test.rb 1:1 Spaces: 2(Auto)
require 'rubygems'
require 'gosu'
module ZOrder
BACKGROUND, MIDDLE, TOP =*0..2
end
WIN_WIDTH =640
WIN_HEIGHT =400
class DemoWindow Gosu::Window
def initialize
super(WIN_WIDTH, WIN_HEIGHT, false)
@background = Gosu::Color::WHITE
@button_font = Gosu::Font.new(20)
@info_font = Gosu::Font.new(10)
@button_highlight = Gosu::Color.rgb(170,170,170)
@button_normal = Gosu::Color::GREEN
@button_border = Gosu::Color::BLACK
@button_hovered = false
@button_rect = Rectangle.new(50,50,100,50)
end
def draw
Gosu.draw_rect(0,0, WIN_WIDTH, WIN_HEIGHT, @background, ZOrder::BACKGROUND, mode=:default)
# Draw the rectangle that provides the background
if @button_hovered
Gosu.draw_rect(@button_rect.x -2, @button_rect.y -2, @button_rect.width +4, @button_rect.height +4, @button_border, ZOrder::TOP, mode=:default)
end
# Draw the button
color = @button_hovered ? @button_highlight : @button_normal
Gosu.draw_rect(@button_rect.x, @button_rect.y, @button_rect.width, @button_rect.height, color, ZOrder::TOP, mode=:default)
# Draw the button text
@button_font.draw("Click me",60,60, ZOrder::MIDDLE, 1.0,1.0, Gosu::Color::BLACK)
# Draw the mouse_x position
@info_font.draw("mouse_x: #{mouse_x}",0,350, ZOrder::TOP, 1.0,1.0, Gosu::Color::BLACK)
# Draw the mouse_y position
@info_font.draw("mouse_y: #{mouse_y}",0,370, ZOrder::TOP, 1.0,1.0, Gosu::Color::BLACK)
end
def needs_cursor?
true
end
def mouse_over_button?(mouse_x, mouse_y)
@button_rect.include?(mouse_x, mouse_y)
end
def button_down(id)
case id
when Gosu::MsLeft
if mouse_over_button?(mouse_x, mouse_y)
@background = Gosu::Color::YELLOW
else
@background = Gosu::Color::WHITE
end
end
end
def update
if mouse_over_button?(mouse_x, mouse_y)
@button_hovered = true
else
@button_hovered = false
end
end
end
DemoWindow.new.show
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions