Toggling Draw functions in Ruby Gosu Library

44 views Asked by At

I'm currently struggling to display a list of font.draw elements that will be linked to left mouse button down. I've set the draw to be set to if button down and mouse_x/y are in the right position but as soon as you stop pressing left mouse button they disappear, which makes sense.

I'm wondering if there was a way to set/toggle these draws on mouse down and I've been struggling to find any resources or examples that show this and it was never taught during this semester's work.

Here is the current code set in place;

def draw
        music_file = File.new("albums.txt", "r")
        albums = read_albums(music_file)
        artworks = read_artworks(albums)
        music_file.close() 
        @info_font.draw("mouse_x: #{mouse_x}", 0, 590, ZOrder::UI, 1.0, 1.0, Gosu::Color::BLACK) #debug
        @info_font.draw("mouse_y: #{mouse_y}", 100, 590, ZOrder::UI, 1.0, 1.0, Gosu::Color::BLACK) #debug
        draw_background() 
        draw_albums(albums)
        drawtracks_main(albums) #this is the issue I'm trying to fix
    end
    def drawtracks_main(albums)
        if (Gosu.button_down? Gosu::MsLeft and mouse_over_album1(mouse_x, mouse_y))
            draw_tracks(albums[0].tracks) == true
        end
    end
def draw_tracks(tracks)
        count = tracks.length
        index = 0
        ypos = 19
        xpos = 585
        while index < count
            @track_font.draw((tracks[index].name), xpos, ypos, ZOrder::UI, 1.0, 1.0, Gosu::Color::BLACK) 
            index += 1
            ypos += 20
        end
    end
    def mouse_over_album1(mouse_x, mouse_y)
        if ((mouse_x > 19 and mouse_x < 287) and (mouse_y > 19 and mouse_y < 287))
            true
        else false
        end
    end

I've tried switching this to draw inside the button_down but I've noticed you can't actually draw in button_down. Otherwise I've tried setting the draw to true, to try and give it a value but that of course has not worked.

Any help would be massive!!

1

There are 1 answers

0
Lucy Pavano On

So after sitting and staring at the wall for the hour, I realized that I should be using the inbuilt update procedure in gosu.

Initializing a @draw_tracks_enabled value to false, then using this;;

def update
  if Gosu.button_down?(Gosu::MsLeft)
    if mouse_over_album1(mouse_x, mouse_y)
      @draw_tracks1_enabled = true
    else 
      @draw_tracks1_enabled = false
    end 
  end
end

to turn it true (or false if you were to press on another item to be displayed). You can then write a simple if @draw_tracks1_enabled == true then draw the tracks.