Option Strict Off
Option Explicit On

Imports System.Xml

Friend Class file561
    '// Start of graphic 0x231
    '  btn Buttons18936[5];   // swapped when read
    '  btn Buttons18876[4];   //    ""      ...
    '  btn Buttons18828[3];   //    ""      ...
    '  btn Buttons18792[2];   //    ""      ...
    '  btn Buttons18768[5];   //    ""      ...
    '  btn Buttons18708[4];   //    ""      ...
    '  btn Buttons18660[3];   //    ""      ...
    '  btn Buttons18624[2]; //      ""    ...
    ' all above = editable

    '  i8  Byte18600[8];    // RectPos swapped when read        ...
    '  i8  Byte18592[8];    // RectPos swapped when read        ...
    '  i8  Byte18584[8];    // RectPos swapped when read        ...
    '  i8  Byte18576[8];    // RectPos swapped when read        ...
    '  i16 Word18568[4];    // deltax[dir] NESW swapped when read        ...
    '  i16 Word18560[4];    // deltay[dir] NESW swapped when read         ...
    '  i16 Word18552[28];   // Swapped when read.  Magic buttons???         ...
    '  MOVEBUTN MoveButn18496[4]; // MF, MR, MB, ML Swapped when read
    '  ui8 DropAreas[16];//18464 X andY in screen coordinates
    '  i8  Byte18448[8];     // KeyXlate swapped when read        ...
    '  i8  Byte18440[12];    //  KeyXlate swapped when read        ...
    '  KeyXlate Byte18428[7];//  KeyXlate swapped when read        ...
    '  i8  Byte18400[28];    //  KeyXlate swapped when read

    ' all below = editable
    '  btn Buttons18372[4];  //
    '  btn Buttons18324[9];  // Swapped when read... chest buttons
    '  btn Buttons18216[13]; // Swapped when read
    '  btn Buttons18060[9];  // Swapped when read
    '  btn Buttons17952[5];  // Swapped when read
    '  btn Buttons17892[5];  // Swapped when read
    '  btn Buttons17832[3];  // Swapped when read         ...
    '  btn Buttons17796[3];  // Swapped when read  [864-4*12]
    '  btn Buttons17760[38]; // Swapped when read
    '  btn Buttons17304[9];  // Swapped when read         ...
    '  btn Buttons17196[20]; // Swapped when read         ...
    '  btn Buttons16956[2];  // Swapped when read         ...
    '  btn Buttons16932[4];  // Swapped when read
    '                        // End of graphic 0x231


    Public itemdata As IO.MemoryStream
    Public itemloc As Integer 'Now 0 based

    'UPGRADE_WARNING: Arrays can't be declared with New. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC9D3AE5-6B95-4B43-91C7-28276302A5E8"'
    Private csbbuttons() As csbButton = ADGEDotNetUt.ArrayUt(Of csbButton).NewArray1(152)
    Private byte18600(228) As Byte

    Private cnt_csbbuttons As Integer



    Public Function csbbutton(ByVal X As Short) As csbButton
        csbbutton = csbbuttons(X)
    End Function



    Function read_byte() As Byte
        itemdata.Position = itemloc
        read_byte = New IO.BinaryReader(itemdata).ReadByte()
        itemloc = itemloc + 1
    End Function

    Function read_int() As Integer
        Dim b1 As Byte
        Dim b2 As Byte
        b2 = read_byte()
        b1 = read_byte()
        read_int = b2
        read_int = read_int * 256
        read_int = read_int + b1
    End Function

    Sub write_byte(ByRef num As Byte)
        itemdata.Position = itemloc
        itemdata.WriteByte(num)
        itemloc = itemloc + 1
    End Sub

    Sub write_int(ByRef num As Integer)
        Dim b1 As Byte
        Dim b2 As Byte
        Dim l1 As Integer
        l1 = num And &HFF00
        b1 = l1 / &H100
        b2 = num And &HFF
        write_byte(b1)
        write_byte(b2)
    End Sub



    Public Sub read()
        Dim i As Integer

        REM http://www.dungeon-master.com/forum/viewtopic.php?p=53531#53531

        If itemdata.Length = 2004 Then
            cnt_csbbuttons = 120 ' DM
        Else
            cnt_csbbuttons = 124 ' DM CSB
        End If

        itemloc = 0

        '28 dialog buttons
        For i = 1 To 28
            csbbuttons(i).word0 = read_int()
            csbbuttons(i).x1 = read_int()
            csbbuttons(i).x2 = read_int()
            csbbuttons(i).y1 = read_int()
            csbbuttons(i).y2 = read_int()
            csbbuttons(i).button = read_int()
        Next

        For i = 1 To 228
            byte18600(i) = read_byte()
        Next

        '124 game buttons
        For i = 1 To cnt_csbbuttons
            csbbuttons(i + 28).word0 = read_int()
            csbbuttons(i + 28).x1 = read_int()
            csbbuttons(i + 28).x2 = read_int()
            csbbuttons(i + 28).y1 = read_int()
            csbbuttons(i + 28).y2 = read_int()
            csbbuttons(i + 28).button = read_int()
        Next

        initcsbbuttonnames()
    End Sub



    Public Sub update()
        Dim i As Short

        itemdata = New IO.MemoryStream
        itemloc = 0

        For i = 1 To 28
            write_int((csbbuttons(i).word0))
            write_int((csbbuttons(i).x1))
            write_int((csbbuttons(i).x2))
            write_int((csbbuttons(i).y1))
            write_int((csbbuttons(i).y2))
            write_int((csbbuttons(i).button))
        Next

        For i = 1 To 228
            write_byte(byte18600(i))
        Next

        For i = 1 To cnt_csbbuttons
            write_int((csbbuttons(i + 28).word0))
            write_int((csbbuttons(i + 28).x1))
            write_int((csbbuttons(i + 28).x2))
            write_int((csbbuttons(i + 28).y1))
            write_int((csbbuttons(i + 28).y2))
            write_int((csbbuttons(i + 28).button))
        Next

        graphics.setgitemsize(561, itemdata.Length)
        graphics.setgitemdata(561, itemdata)
    End Sub


    Public Sub initcsbbuttonnames()

        Dim i As Short
        Dim nodescription As String

        nodescription = ""
        i = 1
#If True Then
        Dim xmli As New XmlDocument
        xmli.Load(IO.Path.Combine(Application.StartupPath, "_csbbuttons.xml"))
        For Each elbtn As XmlElement In xmli.SelectNodes("/csbbuttons/button")
            csbbuttons(i).name = MyXUt.GetElementText(elbtn.SelectSingleNode("name"), "")
            csbbuttons(i).description = MyXUt.GetElementText(elbtn.SelectSingleNode("desc"), "")
            csbbuttons(i).samplepic = MyXUt.GetElementText(elbtn.SelectSingleNode("samplepic"), "")
            i = i + 1
            If i > 152 Then Exit Sub
        Next
#Else
		csbbuttons(i).name = "Top left dialog button"
        csbbuttons(i).description = nodescription
        i = i + 1
		csbbuttons(i).name = "Top right dialog button"
		csbbuttons(i).description = nodescription
        i = i + 1
		csbbuttons(i).name = "Lower left dialog button"
		csbbuttons(i).description = nodescription
        i = i + 1
		csbbuttons(i).name = "Lower right dialog button"
		csbbuttons(i).description = nodescription
        i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
        i = i + 1
		csbbuttons(i).name = "Top dialog button"
		csbbuttons(i).description = nodescription
        i = i + 1
		csbbuttons(i).name = "Lower left dialog button"
		csbbuttons(i).description = nodescription
        i = i + 1
		csbbuttons(i).name = "Lower right dialog button"
		csbbuttons(i).description = nodescription
        i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
        i = i + 1
		csbbuttons(i).name = "Top dialog button"
		csbbuttons(i).description = nodescription
        i = i + 1
		csbbuttons(i).name = "Bottom dialog button"
		csbbuttons(i).description = nodescription
        csbbuttons(i).samplepic = "btnEdtDialogue"
        i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
        csbbuttons(i).samplepic = "btnEdtDialogue"
        i = i + 1
		csbbuttons(i).name = "Bottom dialog button"
		csbbuttons(i).description = nodescription
        csbbuttons(i).samplepic = "btnEdtDialogue"
        i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
        csbbuttons(i).samplepic = "btnEdtDialogue"
        i = i + 1
		csbbuttons(i).name = "Top left minidialog button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Top right minidialog button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Lower left minidialog button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Lower right minidialog button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Top minidialog button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Lower left minidialog button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Lower right minidialog button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Top minidialog button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Bottom minidialog button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Bottom minidialog button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Ressurect hero button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Reincarnate hero button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Cancel hero button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Chest item 1"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Chest item 2"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Chest item 3"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Chest item 4"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Chest item 5"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Chest item 6"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Chest item 7"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Chest item 8"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 1 name"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 2 name"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 3 name"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 4 name"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 1 left hand"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 1 right hand"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 2 left hand"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 2 right hand"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 3 left hand"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 3 right hand"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 4 left hand"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 4 right hand"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Spell rune 1"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Spell rune 2"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Spell rune 3"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Spell rune 4"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Spell rune 5"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Spell rune 6"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Cast spell"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Delete rune"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 1 attack"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 2 attack"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 3 attack"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 4 attack"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Cancel attack"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Attack 1"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Attack 2"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Attack 3"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "?? full screen"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "?? full screen"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "?? viewport"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "?? viewport"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "?? full screen"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv Save button"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv Sleep"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv cancel"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv left hand"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv right hand"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv head"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv chest"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv legs"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv feet"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv pouch 2"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv Mouth"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv eye"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv quiver 2,1"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv quiver 1,2"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv quiver 2,2"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv necklace"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv pouch 1"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv quiver 1,1"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 1,1"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 2,2"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 2,3"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 2,4"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 2,5"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 2,6"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 2,7"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 2,8"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 2,9"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 1,2"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 1,3"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 1,4"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 1,5"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 1,6"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 1,7"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 1,8"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv backpack 1,9"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Inv lower right box ??"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Move turn left"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Move forward"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Move turn right"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Move left"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Move back"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Move right"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "?? viewport"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "?? near full screen"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 1 face"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 2 face"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 3 face"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 4 face"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Top left player"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Top right player"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Lower right player"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Lower left player"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 1 section"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 2 section"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 3 section"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 4 section"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 1 bars"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 2 bars"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 3 bars"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Player 4 bars"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Spell area"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Attack area"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "?? lower left dot"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "?? inventory status bottom"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Entrance enter dungeon"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Entrance resume"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Entrance credits"
		csbbuttons(i).description = nodescription
		i = i + 1
		csbbuttons(i).name = "Unused"
		csbbuttons(i).description = nodescription
#End If
    End Sub
End Class