<Serializable()> Public Class GraphicsObjects
    Implements IDisposable
    ' グラフィックオブジェクト
    Private gcollection As ArrayList

    Public Sub New()
        gcollection = New ArrayList
    End Sub

    Public Sub AddObject(ByVal obj As GraphicBase)
        gcollection.Add(obj)
    End Sub

    Public Sub DeleteObject(ByVal obj As GraphicBase)
        ' オブジェクトの削除
        gcollection.Remove(obj)
    End Sub

    Public Sub PreviousObject(ByVal obj As GraphicBase)
        ' オブジェクトを前面に移動
        Dim idx As Integer
        idx = gcollection.IndexOf(obj)
        If idx <= gcollection.Count - 2 Then
            Dim tmp As Object
            tmp = gcollection.Item(idx + 1)
            gcollection.Item(idx + 1) = obj
            gcollection.Item(idx) = tmp
        End If
    End Sub

    Public Sub BehindObject(ByVal obj As GraphicBase)
        ' オブジェクトを背面に移動
        Dim idx As Integer
        idx = gcollection.IndexOf(obj)
        If idx >= 1 Then
            Dim tmp As Object
            tmp = gcollection.Item(idx - 1)
            gcollection.Item(idx - 1) = obj
            gcollection.Item(idx) = tmp
        End If
    End Sub

    Public Sub Draw(ByVal g As Graphics)
        ' オブジェクトを後ろから順に前に向けて描画
        For Each obj As GraphicBase In gcollection
            obj.Draw(g, False)
        Next
    End Sub

    Public Function HitObject(ByVal position As PointF) As GraphicBase
        ' 指定された座標にヒットするオブジェクトを返す
        ' オブジェクトを前から後ろに向けて検索
        For i As Integer = gcollection.Count - 1 To 0 Step -1
            Dim obj As GraphicBase = DirectCast(gcollection(i), GraphicBase)
            If obj.isHit(position) Then
                ' ヒットした
                Return obj
            End If
        Next
    End Function

    Public Sub Dispose() Implements System.IDisposable.Dispose
        ' Disposeする
        For Each obj As GraphicBase In gcollection
            obj.Dispose()
        Next
    End Sub
End Class
