' D. de Haas
' Amsterdam, Netherlands
' 29 October 2002
' ddhadam@yahoo.com

Option Explicit On 
Option Strict On

Imports system
Imports System.Drawing
Imports System.Windows.Forms
Imports DdH.Graphics

Public Class Tetris
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

        Me.KeyPreview = True

        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.DoubleBuffer, True)
        Me.SetStyle(ControlStyles.UserPaint, True)
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ListBoxKeys As System.Windows.Forms.ListBox
    Friend WithEvents TetrisMenu As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem9 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem10 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem11 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem12 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem13 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem14 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem15 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem16 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem17 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem18 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem19 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem20 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem21 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem22 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem23 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem24 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem25 As System.Windows.Forms.MenuItem
    Friend WithEvents LabelScore As System.Windows.Forms.Label
    Friend WithEvents MenuItem26 As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ListBoxKeys = New System.Windows.Forms.ListBox()
        Me.TetrisMenu = New System.Windows.Forms.MainMenu()
        Me.MenuItem1 = New System.Windows.Forms.MenuItem()
        Me.MenuItem2 = New System.Windows.Forms.MenuItem()
        Me.MenuItem3 = New System.Windows.Forms.MenuItem()
        Me.MenuItem4 = New System.Windows.Forms.MenuItem()
        Me.MenuItem5 = New System.Windows.Forms.MenuItem()
        Me.MenuItem6 = New System.Windows.Forms.MenuItem()
        Me.MenuItem7 = New System.Windows.Forms.MenuItem()
        Me.MenuItem8 = New System.Windows.Forms.MenuItem()
        Me.MenuItem9 = New System.Windows.Forms.MenuItem()
        Me.MenuItem10 = New System.Windows.Forms.MenuItem()
        Me.MenuItem11 = New System.Windows.Forms.MenuItem()
        Me.MenuItem12 = New System.Windows.Forms.MenuItem()
        Me.MenuItem13 = New System.Windows.Forms.MenuItem()
        Me.MenuItem14 = New System.Windows.Forms.MenuItem()
        Me.MenuItem15 = New System.Windows.Forms.MenuItem()
        Me.MenuItem16 = New System.Windows.Forms.MenuItem()
        Me.MenuItem17 = New System.Windows.Forms.MenuItem()
        Me.MenuItem18 = New System.Windows.Forms.MenuItem()
        Me.MenuItem19 = New System.Windows.Forms.MenuItem()
        Me.MenuItem20 = New System.Windows.Forms.MenuItem()
        Me.MenuItem21 = New System.Windows.Forms.MenuItem()
        Me.MenuItem22 = New System.Windows.Forms.MenuItem()
        Me.MenuItem23 = New System.Windows.Forms.MenuItem()
        Me.MenuItem24 = New System.Windows.Forms.MenuItem()
        Me.MenuItem25 = New System.Windows.Forms.MenuItem()
        Me.MenuItem26 = New System.Windows.Forms.MenuItem()
        Me.LabelScore = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'ListBoxKeys
        '
        Me.ListBoxKeys.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.ListBoxKeys.Enabled = False
        Me.ListBoxKeys.Items.AddRange(New Object() {"Moving, use keys:", "Left, Right, Down", "", "Rotating, use keys:", "A, S", "", "Pauze: P"})
        Me.ListBoxKeys.Location = New System.Drawing.Point(8, 16)
        Me.ListBoxKeys.Name = "ListBoxKeys"
        Me.ListBoxKeys.Size = New System.Drawing.Size(96, 93)
        Me.ListBoxKeys.TabIndex = 1
        '
        'TetrisMenu
        '
        Me.TetrisMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem3, Me.MenuItem14, Me.MenuItem21, Me.MenuItem26})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2})
        Me.MenuItem1.Text = "Game"
        '
        'MenuItem2
        '
        Me.MenuItem2.Index = 0
        Me.MenuItem2.Text = "New"
        '
        'MenuItem3
        '
        Me.MenuItem3.Index = 1
        Me.MenuItem3.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem4, Me.MenuItem5, Me.MenuItem6, Me.MenuItem7, Me.MenuItem8, Me.MenuItem9, Me.MenuItem10, Me.MenuItem11, Me.MenuItem12, Me.MenuItem13})
        Me.MenuItem3.Text = "Level"
        '
        'MenuItem4
        '
        Me.MenuItem4.Checked = True
        Me.MenuItem4.Index = 0
        Me.MenuItem4.Text = "1"
        '
        'MenuItem5
        '
        Me.MenuItem5.Index = 1
        Me.MenuItem5.Text = "2"
        '
        'MenuItem6
        '
        Me.MenuItem6.Index = 2
        Me.MenuItem6.Text = "3"
        '
        'MenuItem7
        '
        Me.MenuItem7.Index = 3
        Me.MenuItem7.Text = "4"
        '
        'MenuItem8
        '
        Me.MenuItem8.Index = 4
        Me.MenuItem8.Text = "5"
        '
        'MenuItem9
        '
        Me.MenuItem9.Index = 5
        Me.MenuItem9.Text = "6"
        '
        'MenuItem10
        '
        Me.MenuItem10.Index = 6
        Me.MenuItem10.Text = "7"
        '
        'MenuItem11
        '
        Me.MenuItem11.Index = 7
        Me.MenuItem11.Text = "8"
        '
        'MenuItem12
        '
        Me.MenuItem12.Index = 8
        Me.MenuItem12.Text = "9"
        '
        'MenuItem13
        '
        Me.MenuItem13.Index = 9
        Me.MenuItem13.Text = "10"
        '
        'MenuItem14
        '
        Me.MenuItem14.Index = 2
        Me.MenuItem14.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem15, Me.MenuItem16, Me.MenuItem17, Me.MenuItem18, Me.MenuItem19, Me.MenuItem20})
        Me.MenuItem14.Text = "BlockSize"
        '
        'MenuItem15
        '
        Me.MenuItem15.Index = 0
        Me.MenuItem15.Text = "5"
        '
        'MenuItem16
        '
        Me.MenuItem16.Index = 1
        Me.MenuItem16.Text = "10"
        '
        'MenuItem17
        '
        Me.MenuItem17.Checked = True
        Me.MenuItem17.Index = 2
        Me.MenuItem17.Text = "15"
        '
        'MenuItem18
        '
        Me.MenuItem18.Index = 3
        Me.MenuItem18.Text = "20"
        '
        'MenuItem19
        '
        Me.MenuItem19.Index = 4
        Me.MenuItem19.Text = "25"
        '
        'MenuItem20
        '
        Me.MenuItem20.Index = 5
        Me.MenuItem20.Text = "30"
        '
        'MenuItem21
        '
        Me.MenuItem21.Index = 3
        Me.MenuItem21.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem22, Me.MenuItem23, Me.MenuItem24, Me.MenuItem25})
        Me.MenuItem21.Text = "FieldSize"
        '
        'MenuItem22
        '
        Me.MenuItem22.Index = 0
        Me.MenuItem22.Text = "20 * 20"
        '
        'MenuItem23
        '
        Me.MenuItem23.Checked = True
        Me.MenuItem23.Index = 1
        Me.MenuItem23.Text = "10 * 20"
        '
        'MenuItem24
        '
        Me.MenuItem24.Index = 2
        Me.MenuItem24.Text = "10 * 10"
        '
        'MenuItem25
        '
        Me.MenuItem25.Index = 3
        Me.MenuItem25.Text = "5 * 10"
        '
        'MenuItem26
        '
        Me.MenuItem26.Index = 4
        Me.MenuItem26.Text = "Highscores"
        '
        'LabelScore
        '
        Me.LabelScore.AutoSize = True
        Me.LabelScore.Location = New System.Drawing.Point(8, 0)
        Me.LabelScore.Name = "LabelScore"
        Me.LabelScore.Size = New System.Drawing.Size(0, 13)
        Me.LabelScore.TabIndex = 2
        '
        'Tetris
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(266, 117)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.LabelScore, Me.ListBoxKeys})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.KeyPreview = True
        Me.MaximizeBox = False
        Me.Menu = Me.TetrisMenu
        Me.MinimizeBox = False
        Me.Name = "Tetris"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Tetris.NET"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private TetrisGame As DdH.TetrisGameArea
    Private hs As New DdH.HighScore(String.Concat(IO.Directory.GetCurrentDirectory, "\Tetris.NET.dat"))

    ' Default settings
    ' if you change these, also change the checked menuitems
    Private GameWidth, GameHeight As Integer
    Private GameSize As Integer = 1
    Private GameBlockSize As Integer = 15
    Private GameLevel As Integer = 1

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        ' Draw the TetrisGameArea on the form
        e.Graphics.Flush(System.Drawing.Drawing2D.FlushIntention.Flush)

        If Not (TetrisGame Is Nothing) Then
            TetrisGame.Draw(e.Graphics)

            LabelScore.Text = String.Concat("Level: ", TetrisGame.Level.ToString, " - Lines: ", TetrisGame.LinesCleared.ToString, " - Score: ", TetrisGame.Score.ToString)
        End If
    End Sub

    Private Sub TetrisGame_Redraw(ByVal region As System.Drawing.Region)
        ' Invoke a form redraw, only for the Game Region part

        Me.Invalidate(region)
    End Sub

    Private Sub TetrisGame_GameOver()
        ' Hurry, press 'New Game' to play again

        Dim fInput As New InputName()
        fInput.ShowDialog()

        ' Update HighScores
        hs.Add(fInput.PlayerName, TetrisGame.Score, TetrisGame.LinesCleared, TetrisGame.Level)
        hs.Save()

        Me.MenuItem1.Enabled = True
        Me.MenuItem3.Enabled = True
        Me.MenuItem14.Enabled = True
        Me.MenuItem21.Enabled = True
        Me.MenuItem26.Enabled = True
    End Sub

    Private Sub Tetris_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        ' Capture Keys

        If Not (TetrisGame Is Nothing) Then
            Select Case e.KeyCode
                Case Keys.Left, Keys.NumPad4
                    TetrisGame.Move(DdH.Graphics.Shape.ShapeMove.Left)

                Case Keys.Right, Keys.NumPad6
                    TetrisGame.Move(DdH.Graphics.Shape.ShapeMove.Right)

                Case Keys.Down, Keys.NumPad2
                    TetrisGame.Move(DdH.Graphics.Shape.ShapeMove.Down)

                Case Keys.A
                    TetrisGame.Rotate(DdH.Graphics.Shape.ShapeRotate.CounterClockWise)

                Case Keys.S
                    TetrisGame.Rotate(DdH.Graphics.Shape.ShapeRotate.ClockWise)

                Case Keys.P
                    TetrisGame.Pause = Not TetrisGame.Pause

            End Select
        End If
    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        ' New Game

        Me.MenuItem1.Enabled = False
        Me.MenuItem3.Enabled = False
        Me.MenuItem14.Enabled = False
        Me.MenuItem21.Enabled = False
        Me.MenuItem26.Enabled = False

        Dim x, y, w, h As Integer
        x = 120
        y = 16

        ' Caluculate new form size
        Select Case GameSize
            Case 0
                GameWidth = 20
                GameHeight = 20
            Case 1
                GameWidth = 10
                GameHeight = 20
            Case 2
                GameWidth = 10
                GameHeight = 10
            Case 3
                GameWidth = 5
                GameHeight = 10

        End Select
        Dim tt As Integer = Me.ClientSize.Width
        w = x + ((GameWidth + 7) * GameBlockSize)
        h = y + ((GameHeight + 1) * GameBlockSize)
        If w < 260 Then w = 260
        If h < 115 Then h = 115

        ' Set form size
        Dim sz As New System.Drawing.Size(w, h)
        Me.ClientSize = sz

        ' Centre form
        Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) \ 2
        Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) \ 2

        ' New Tetris game
        TetrisGame = New DdH.TetrisGameArea(x, y, GameBlockSize, GameWidth, GameHeight)

        ' Event handler for TetrisGame
        AddHandler TetrisGame.Redraw, AddressOf TetrisGame_Redraw
        AddHandler TetrisGame.GameOver, AddressOf TetrisGame_GameOver

        ' Let's Go playing
        TetrisGame.Level = GameLevel
        TetrisGame.Start()

        Me.Invalidate()
    End Sub

    Private Sub Menu3Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click, MenuItem5.Click, MenuItem6.Click, MenuItem7.Click, MenuItem8.Click, MenuItem9.Click, MenuItem10.Click, MenuItem11.Click, MenuItem12.Click, MenuItem13.Click
        ' Level

        Me.MenuItem3.MenuItems(GameLevel - 1).Checked = False

        Dim s As String = CType(sender, MenuItem).Text
        GameLevel = CType(s, Integer)

        CType(sender, MenuItem).Checked = True
    End Sub

    Private Sub Menu14Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem15.Click, MenuItem16.Click, MenuItem17.Click, MenuItem18.Click, MenuItem19.Click, MenuItem20.Click
        ' Blocksize

        Me.MenuItem14.MenuItems((GameBlockSize \ 5) - 1).Checked = False

        GameBlockSize = (CType(sender, MenuItem).Index + 1) * 5

        CType(sender, MenuItem).Checked = True
    End Sub

    Private Sub Menu21Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem22.Click, MenuItem23.Click, MenuItem24.Click, MenuItem25.Click
        ' FieldSize

        Me.MenuItem21.MenuItems(GameSize).Checked = False

        GameSize = CType(sender, MenuItem).Index

        CType(sender, MenuItem).Checked = True
    End Sub

    Private Sub MenuItem26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem26.Click
        ' HighScores

        Dim fHighScores As New HighScore()
        fHighScores.Score = hs

        fHighScores.ShowDialog()
    End Sub

End Class
