Techioz Blog

Ruby Gosu ライブラリの描画関数の切り替え

概要

現在、マウスの左ボタンを押すとリンクされるfont.draw要素のリストを表示するのに苦労しています。ボタンが押されていて、mouse_x/y が正しい位置にある場合に描画が設定されるように設定しましたが、マウスの左ボタンを押すのをやめるとすぐにそれらが消えますが、これは理にかなっています。

マウスダウンでこれらの描画を設定/切り替える方法があるかどうか疑問に思っていますが、これを示すリソースや例を見つけるのに苦労していますが、今学期の作業では教えられませんでした。

現在のコードセットは次のとおりです。

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

これを button_down 内に描画するように切り替えてみましたが、実際には button_down 内に描画できないことに気付きました。それ以外の場合、描画を true に設定して値を与えようとしましたが、もちろん機能しませんでした。

どんな助けでも大助かりです!!

解決策

そこで、1 時間座って壁を見つめていた後、gosu に組み込まれている更新手順を使用する必要があることに気付きました。

@draw_tracks_enabled 値を false に初期化し、これを使用します。

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

これを true (表示する別の項目を押した場合は false) にします。次に、単純な if @draw_tracks1_enabled == true を記述して、トラックを描画します。