Mega Code Archive

 
Categories / VB.Net Tutorial / Windows
 

Demonstrates microsoft agent

'Visual Basic.NET How to Program, Second Edition 'by Harvey M. Deitel (Author), Paul J. Deitel (Author), Tem R. Nieto (Author) ' Publisher: Prentice Hall; 2 edition (December 11, 2001) ' Language: English ' ISBN-10: 0130293636 ' ISBN-13: 978-0130293633 Imports System.IO Imports System.Collections Imports System.Windows.Forms Public Class FrmAgent    Inherits System.Windows.Forms.Form    ' options    Friend WithEvents GroupBox1 As GroupBox    Friend WithEvents cmdSpeak As Button    Friend WithEvents mainAgent As AxAgentObjects.AxAgent    ' input boxes    Friend WithEvents txtLocation As TextBox    Friend WithEvents txtSpeech As TextBox    Friend WithEvents characterCombo As System.Windows.Forms.ComboBox    Friend WithEvents actionsCombo As System.Windows.Forms.ComboBox    ' current agent object    Private mSpeaker As AgentObjects.IAgentCtlCharacter #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    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.    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()       Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(FrmAgent))       Me.txtLocation = New System.Windows.Forms.TextBox()       Me.GroupBox1 = New System.Windows.Forms.GroupBox()       Me.characterCombo = New System.Windows.Forms.ComboBox()       Me.cmdSpeak = New System.Windows.Forms.Button()       Me.txtSpeech = New System.Windows.Forms.TextBox()       Me.actionsCombo = New System.Windows.Forms.ComboBox()       Me.mainAgent = New AxAgentObjects.AxAgent()       Me.GroupBox1.SuspendLayout()       CType(Me.mainAgent, System.ComponentModel.ISupportInitialize).BeginInit()       Me.SuspendLayout()       '       'txtLocation       '       Me.txtLocation.Location = New System.Drawing.Point(16, 24)       Me.txtLocation.Name = "txtLocation"       Me.txtLocation.Size = New System.Drawing.Size(272, 20)       Me.txtLocation.TabIndex = 0       Me.txtLocation.Text = "C:\WINNT\msagent\chars\"       '       'GroupBox1       '       Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.characterCombo, Me.txtLocation})       Me.GroupBox1.Location = New System.Drawing.Point(8, 104)       Me.GroupBox1.Name = "GroupBox1"       Me.GroupBox1.Size = New System.Drawing.Size(440, 56)       Me.GroupBox1.TabIndex = 1       Me.GroupBox1.TabStop = False       Me.GroupBox1.Text = "Charcter Name/Location"       '       'characterCombo       '       Me.characterCombo.Enabled = False       Me.characterCombo.Items.AddRange(New Object() {"Genie", "Merlin", "Peedy", "Robby"})       Me.characterCombo.Location = New System.Drawing.Point(304, 24)       Me.characterCombo.Name = "characterCombo"       Me.characterCombo.Size = New System.Drawing.Size(128, 21)       Me.characterCombo.TabIndex = 1       Me.characterCombo.Text = "Select A Character"       '       'cmdSpeak       '       Me.cmdSpeak.Enabled = False       Me.cmdSpeak.Location = New System.Drawing.Point(328, 24)       Me.cmdSpeak.Name = "cmdSpeak"       Me.cmdSpeak.Size = New System.Drawing.Size(104, 32)       Me.cmdSpeak.TabIndex = 3       Me.cmdSpeak.Text = "Speak"       '       'txtSpeech       '       Me.txtSpeech.Enabled = False       Me.txtSpeech.Location = New System.Drawing.Point(8, 16)       Me.txtSpeech.Multiline = True       Me.txtSpeech.Name = "txtSpeech"       Me.txtSpeech.Size = New System.Drawing.Size(280, 80)       Me.txtSpeech.TabIndex = 5       Me.txtSpeech.Text = ""       '       'actionsCombo       '       Me.actionsCombo.Enabled = False       Me.actionsCombo.Location = New System.Drawing.Point(312, 72)       Me.actionsCombo.Name = "actionsCombo"       Me.actionsCombo.Size = New System.Drawing.Size(128, 21)       Me.actionsCombo.TabIndex = 7       Me.actionsCombo.Text = "Select an Action"       '       'mainAgent       '       Me.mainAgent.Enabled = True       Me.mainAgent.Location = New System.Drawing.Point(304, 24)       Me.mainAgent.Name = "mainAgent"       Me.mainAgent.OcxState = CType(resources.GetObject("mainAgent.OcxState"), System.Windows.Forms.AxHost.State)       Me.mainAgent.Size = New System.Drawing.Size(32, 32)       Me.mainAgent.TabIndex = 8       '       'FrmAgent       '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)       Me.ClientSize = New System.Drawing.Size(448, 165)       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.mainAgent, Me.actionsCombo, Me.txtSpeech, Me.cmdSpeak, Me.GroupBox1})       Me.Name = "FrmAgent"       Me.Text = "Microsoft Agent"       Me.GroupBox1.ResumeLayout(False)       CType(Me.mainAgent, System.ComponentModel.ISupportInitialize).EndInit()       Me.ResumeLayout(False)    End Sub #End Region    ' keyDown event handler for locationTextBox    Private Sub txtLocation_KeyDown(ByVal sender As _       Object, ByVal e As System.Windows.Forms.KeyEventArgs) _       Handles txtLocation.KeyDown       If e.KeyCode = Keys.Enter Then          ' set character location to text box value          Dim location As String = txtLocation.Text          ' initialize characters          Try             ' load characters into agent object             mainAgent.Characters.Load( _                "Genie", location + "Genie.acs")             mainAgent.Characters.Load( _                "Merlin", location + "Merlin.acs")             mainAgent.Characters.Load( _                "Peedy", location + "Peedy.acs")             mainAgent.Characters.Load( _                "Robby", location + "Robby.acs")             ' disable TextBox location and enable other controls             txtLocation.Enabled = False             txtSpeech.Enabled = True             cmdSpeak.Enabled = True             characterCombo.Enabled = True             actionsCombo.Enabled = True             ' set current character to Genie and show             mSpeaker = mainAgent.Characters("Genie")             GetAnimationNames() ' obtain animation name list             mSpeaker.Show(0)          Catch fileNotFound As FileNotFoundException             MessageBox.Show("Invalid character location", _                "Error", MessageBoxButtons.OK, _                MessageBoxIcon.Error)          End Try       End If    End Sub ' txtLocation_KeyDown    ' speak button event handler    Private Sub cmdSpeak_Click(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles cmdSpeak.Click       ' if TextBox is empty, have character ask       ' user to type words into TextBox, otherwise       ' have character say words in TextBox       If txtSpeech.Text = "" Then          mSpeaker.Speak( _             "Please type the words you want me to speak", "")       Else          mSpeaker.Speak(txtSpeech.Text, "")       End If    End Sub ' cmdSpeak_Click    ' click event for agent    Private Sub mainAgent_ClickEvent(ByVal sender As Object, _       ByVal e As AxAgentObjects._AgentEvents_ClickEvent) _       Handles mainAgent.ClickEvent       mSpeaker.Play("Confused")       mSpeaker.Speak("Why are you poking me?", "")       mSpeaker.Play("RestPose")    End Sub ' mainAgent_ClickEvent    ' comboBox changed event, switch active agent    Private Sub characterCombo_SelectedIndexChanged(ByVal _       sender As Object, ByVal e As System.EventArgs) Handles _       characterCombo.SelectedIndexChanged       ChangeCharacter(characterCombo.Text)    End Sub ' characterCombo_SelectedIndexChanged    ' hide current character and show new    Private Sub ChangeCharacter(ByVal name As String)       mSpeaker.Hide(0)       mSpeaker = mainAgent.Characters(name)       GetAnimationNames() ' regenerate animation name list       mSpeaker.Show(0)    End Sub ' ChangeCharacter    ' get animation names and store in arraylist    Private Sub GetAnimationNames()       ' ensure thread safety       SyncLock (Me)          ' get animation names          Dim enumerator As IEnumerator = _             mainAgent.Characters.Character( _             mSpeaker.Name).AnimationNames.GetEnumerator()          Dim voiceString As String          ' clear cboActions combo box          actionsCombo.Items.Clear()          mSpeaker.Commands.RemoveAll()          ' copy enumeration to ArrayList          While enumerator.MoveNext()             ' remove underscores in speech string             voiceString = Convert.ToString(enumerator.Current)             voiceString = voiceString.Replace("_", "underscore")             actionsCombo.Items.Add(enumerator.Current)             ' add all animations as voice enabled commands             mSpeaker.Commands.Add(Convert.ToString( _                enumerator.Current), , voiceString, True, False)          End While          ' add custom command          mSpeaker.Commands.Add("MoveToMouse", "MoveToMouse", _             "MoveToMouse", True, True)       End SyncLock    End Sub ' GetAnimationNames    ' user selects new action    Private Sub actionsCombo_SelectedIndexChanged(ByVal sender _       As System.Object, ByVal e As System.EventArgs) _       Handles actionsCombo.SelectedIndexChanged       mSpeaker.Stop()       mSpeaker.Play(actionsCombo.Text)       mSpeaker.Play("RestPose")    End Sub ' actionsCombo_SelectedIndexChanged    ' handles agent commands    Private Sub mainAgent_Command(ByVal sender As Object, _       ByVal e As AxAgentObjects._AgentEvents_CommandEvent) _       Handles mainAgent.Command       ' get UserInput object       Dim command As AgentObjects.IAgentCtlUserInput = _          CType(e.userInput, AgentObjects.IAgentCtlUserInput)       ' change character if user speaks character name       If (command.Voice = "Peedy" OrElse _          command.Voice = "Robby" OrElse _          command.Voice = "Merlin" OrElse _          command.Voice = "Genie") Then          ChangeCharacter(command.Voice)          Return       End If       ' send agent to mouse       If command.Name = "MoveToMouse" Then          mSpeaker.MoveTo(Convert.ToInt16( _             Cursor.Position.X - 60), Convert.ToInt16( _             Cursor.Position.Y - 60))          Return       End If       ' play new animation       mSpeaker.Stop()       mSpeaker.Play(command.Name)    End Sub ' mainAgent_Command End Class ' FrmAgent ' ************************************************************* ' * (C) Copyright 2002 by Deitel & Associates, Inc.           * ' *     and Prentice Hall.                                    * ' * All Rights Reserved.                                      * ' *                                                           * ' * DISCLAIMER: The authors and publisher of this book have   * ' * used their best efforts in preparing the book. These      * ' * efforts include the development, research, and testing of * ' * the theories and programs to determine their              * ' * effectiveness. The authors and publisher make no warranty * ' * of any kind, expressed or implied, with regard to these   * ' * programs or to the documentation contained in these books.* ' * The authors and publisher shall not be liable in any event* ' * for incidental or consequential damages in connection     * ' * with, or arising out of, the furnishing, performance, or  * ' * use of these programs.                                    * ' *************************************************************