home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH5 / SRC / TWEENROB.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-02  |  22.5 KB  |  729 lines

  1. VERSION 4.00
  2. Begin VB.Form TweenForm 
  3.    Caption         =   "Tween"
  4.    ClientHeight    =   4590
  5.    ClientLeft      =   2040
  6.    ClientTop       =   1035
  7.    ClientWidth     =   4635
  8.    Height          =   5280
  9.    Left            =   1980
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   306
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   309
  14.    Top             =   405
  15.    Width           =   4755
  16.    Begin VB.CommandButton CmdTween 
  17.       Caption         =   "Tween"
  18.       Height          =   495
  19.       Left            =   3480
  20.       TabIndex        =   12
  21.       Top             =   480
  22.       Width           =   975
  23.    End
  24.    Begin VB.TextBox TweensText 
  25.       Height          =   285
  26.       Left            =   4200
  27.       TabIndex        =   10
  28.       Text            =   "4"
  29.       Top             =   0
  30.       Width           =   375
  31.    End
  32.    Begin VB.TextBox FPSText 
  33.       Height          =   285
  34.       Left            =   4080
  35.       TabIndex        =   9
  36.       Text            =   "20"
  37.       Top             =   1800
  38.       Width           =   375
  39.    End
  40.    Begin VB.CommandButton CmdPlay 
  41.       Caption         =   "Play"
  42.       Default         =   -1  'True
  43.       Height          =   495
  44.       Left            =   3480
  45.       TabIndex        =   7
  46.       Top             =   3480
  47.       Width           =   975
  48.    End
  49.    Begin VB.OptionButton PlayOption 
  50.       Caption         =   "Reversing"
  51.       Height          =   255
  52.       Index           =   2
  53.       Left            =   3360
  54.       TabIndex        =   4
  55.       Top             =   3000
  56.       Width           =   1095
  57.    End
  58.    Begin VB.OptionButton PlayOption 
  59.       Caption         =   "Looping"
  60.       Height          =   255
  61.       Index           =   1
  62.       Left            =   3360
  63.       TabIndex        =   3
  64.       Top             =   2640
  65.       Width           =   1095
  66.    End
  67.    Begin VB.OptionButton PlayOption 
  68.       Caption         =   "Once"
  69.       Height          =   255
  70.       Index           =   0
  71.       Left            =   3360
  72.       TabIndex        =   2
  73.       Top             =   2280
  74.       Value           =   -1  'True
  75.       Width           =   1095
  76.    End
  77.    Begin VB.HScrollBar SBar 
  78.       Height          =   255
  79.       Left            =   0
  80.       Max             =   1
  81.       Min             =   1
  82.       TabIndex        =   1
  83.       Top             =   3960
  84.       Value           =   1
  85.       Width           =   3255
  86.    End
  87.    Begin VB.PictureBox Canvas 
  88.       AutoRedraw      =   -1  'True
  89.       Height          =   3975
  90.       Left            =   0
  91.       ScaleHeight     =   261
  92.       ScaleMode       =   3  'Pixel
  93.       ScaleWidth      =   213
  94.       TabIndex        =   0
  95.       Top             =   0
  96.       Width           =   3255
  97.    End
  98.    Begin VB.Label Label1 
  99.       Caption         =   "Tweens:"
  100.       Height          =   255
  101.       Index           =   2
  102.       Left            =   3360
  103.       TabIndex        =   11
  104.       Top             =   0
  105.       Width           =   615
  106.    End
  107.    Begin VB.Label Label1 
  108.       Caption         =   "FPS:"
  109.       Height          =   255
  110.       Index           =   1
  111.       Left            =   3480
  112.       TabIndex        =   8
  113.       Top             =   1800
  114.       Width           =   375
  115.    End
  116.    Begin MSComDlg.CommonDialog FileDialog 
  117.       Left            =   2640
  118.       Top             =   4200
  119.       _version        =   65536
  120.       _extentx        =   847
  121.       _extenty        =   847
  122.       _stockprops     =   0
  123.       cancelerror     =   -1  'True
  124.    End
  125.    Begin VB.Label FrameLabel 
  126.       Alignment       =   2  'Center
  127.       BorderStyle     =   1  'Fixed Single
  128.       Caption         =   "1/1"
  129.       Height          =   255
  130.       Left            =   1680
  131.       TabIndex        =   6
  132.       Top             =   4320
  133.       Width           =   735
  134.    End
  135.    Begin VB.Label Label1 
  136.       Caption         =   "Frame:"
  137.       Height          =   255
  138.       Index           =   0
  139.       Left            =   1080
  140.       TabIndex        =   5
  141.       Top             =   4320
  142.       Width           =   495
  143.    End
  144.    Begin VB.Menu mnuFile 
  145.       Caption         =   "&File"
  146.       Begin VB.Menu mnuFileLoad 
  147.          Caption         =   "&Load..."
  148.          Shortcut        =   ^L
  149.       End
  150.       Begin VB.Menu mnuFileSave 
  151.          Caption         =   "&Save"
  152.          Shortcut        =   ^S
  153.       End
  154.       Begin VB.Menu mnuFileSaveAs 
  155.          Caption         =   "Save &As..."
  156.          Shortcut        =   ^A
  157.       End
  158.       Begin VB.Menu mnuFileSep 
  159.          Caption         =   "-"
  160.       End
  161.       Begin VB.Menu mnuFileExit 
  162.          Caption         =   "E&xit"
  163.       End
  164.    End
  165.    Begin VB.Menu mnuFileFrame 
  166.       Caption         =   "Frames"
  167.       Begin VB.Menu mnuFrameAfter 
  168.          Caption         =   "Insert &After"
  169.       End
  170.       Begin VB.Menu mnuFrameBefore 
  171.          Caption         =   "Insert &Before"
  172.       End
  173.       Begin VB.Menu mnuFrameSep 
  174.          Caption         =   "-"
  175.       End
  176.       Begin VB.Menu mnuFrameDelete 
  177.          Caption         =   "&Delete"
  178.          Enabled         =   0   'False
  179.       End
  180.    End
  181. Attribute VB_Name = "TweenForm"
  182. Attribute VB_Creatable = False
  183. Attribute VB_Exposed = False
  184. Option Explicit
  185. Dim NumFrames As Integer
  186. Dim Frames() As New RobotConfig
  187. Dim SelectedFrame As Integer
  188. Dim SelectingFrame As Boolean
  189. Dim FileLoaded As String
  190. Dim DataModified As Boolean
  191. Dim Playing As Boolean
  192. Dim Dragging As Boolean
  193. Dim DragPoint As Integer
  194. Dim DragX As Integer
  195. Dim DragY As Integer
  196. Dim AnchorX As Integer
  197. Dim AnchorY As Integer
  198. ' ************************************************
  199. ' Convert (X, Y) into the point in the direction
  200. ' of (X, Y) that is the correct distance from the
  201. ' anchor point. For example, when dragging an
  202. ' elbow, the point should be UARM_LEN distance
  203. ' from the shoulders.
  204. ' ************************************************
  205. Sub AdjustPoint(x As Single, y As Single)
  206. Dim dist As Single
  207. Dim factor As Single
  208. Dim dx As Single
  209. Dim dy As Single
  210.     ' Heads have no anchor point.
  211.     If DragPoint = PART_HEAD Then
  212.         DragX = x
  213.         DragY = y
  214.         Exit Sub
  215.     End If
  216.     dx = x - AnchorX
  217.     dy = y - AnchorY
  218.     dist = Sqr(dx * dx + dy * dy)
  219.     Select Case DragPoint
  220.         Case PART_LELBOW, PART_RELBOW
  221.             factor = UARM_LEN / dist
  222.         Case PART_LHAND, PART_RHAND
  223.             factor = LARM_LEN / dist
  224.         Case PART_LKNEE, PART_RKNEE
  225.             factor = ULEG_LEN / dist
  226.         Case PART_LFOOT, PART_RFOOT
  227.             factor = LLEG_LEN / dist
  228.     End Select
  229.     DragX = AnchorX + dx * factor
  230.     DragY = AnchorY + dy * factor
  231. End Sub
  232. ' ************************************************
  233. ' Return true if the data has not been modified,
  234. ' or the user has saved the changes, or the user
  235. ' wants to lose the changes.
  236. ' ************************************************
  237. Function DataSafe() As Boolean
  238. Dim ans As Integer
  239.     Do While DataModified
  240.         ans = MsgBox("The data has been modified." & _
  241.             " Do you want to save the changes?", _
  242.             vbYesNoCancel)
  243.         If ans = vbCancel Then Exit Do
  244.         If ans = vbNo Then
  245.             DataSafe = True
  246.             Exit Function
  247.         End If
  248.             
  249.         ' Otherwise save the data.
  250.         If FileLoaded <> "" Then
  251.             mnuFileSave_Click
  252.         Else
  253.             mnuFileSaveAs_Click
  254.         End If
  255.     Loop
  256.     DataSafe = Not DataModified
  257. End Function
  258. ' ************************************************
  259. ' Draw the highlight fot the drag.
  260. ' ************************************************
  261. Sub DrawDrag()
  262.     If DragPoint = PART_HEAD Then
  263.         Canvas.Line (DragX - NEAR, DragY - NEAR)-Step(NEAR2, NEAR2), , BF
  264.     Else
  265.         Canvas.Line (AnchorX, AnchorY)-(DragX, DragY)
  266.     End If
  267. End Sub
  268. ' ************************************************
  269. ' Draw the selected configuration.
  270. ' ************************************************
  271. Sub DrawSelected()
  272.     Canvas.Cls
  273.     Frames(SelectedFrame).Draw Canvas, True
  274. End Sub
  275. ' ************************************************
  276. ' Save a robot script into the file.
  277. ' ************************************************
  278. Sub SaveScript(fname As String)
  279. Dim fnum As Integer
  280. Dim i As Integer
  281.     On Error GoTo SaveScriptError
  282.     ' Open the file.
  283.     fnum = FreeFile
  284.     Open fname For Output As fnum
  285.     ' Write the number of frames.
  286.     Write #fnum, NumFrames
  287.     ' Write the parameters for each frame.
  288.     For i = 1 To NumFrames
  289.         Frames(i).FileWrite fnum
  290.     Next i
  291.     Close fnum
  292.     FileLoaded = fname
  293.     Caption = "Tween [" & fname & "]"
  294.     DataModified = False
  295.     Exit Sub
  296. SaveScriptError:
  297.     Beep
  298.     MsgBox "Error saving file " & fname & "." & _
  299.         vbCrLf & Format$(Err.Number) & " : " & _
  300.         Err.Description
  301.     Exit Sub
  302. End Sub
  303. ' ************************************************
  304. ' Load a robot script from the file.
  305. ' ************************************************
  306. Sub LoadScript(fname As String)
  307. Dim fnum As Integer
  308. Dim i As Integer
  309.     On Error GoTo SaveScriptError
  310.     ' Open the file.
  311.     fnum = FreeFile
  312.     Open fname For Input As fnum
  313.     ' Read the number of frames.
  314.     Input #fnum, NumFrames
  315.     ReDim Frames(1 To NumFrames)
  316.     SBar.Max = NumFrames
  317.     ' Read the parameters for each frame.
  318.     For i = 1 To NumFrames
  319.         Frames(i).FileInput fnum
  320.     Next i
  321.     Close fnum
  322.     SelectFrame 1
  323.     mnuFrameDelete.Enabled = (NumFrames > 1)
  324.     FileLoaded = fname
  325.     Caption = "Robot [" & fname & "]"
  326.     DataModified = False
  327.     Exit Sub
  328. SaveScriptError:
  329.     Beep
  330.     MsgBox "Error loading file " & fname & "." & _
  331.         vbCrLf & Format$(Err.Number) & " : " & _
  332.         Err.Description
  333.     Exit Sub
  334. End Sub
  335. ' ************************************************
  336. ' Select and display the indicated frame.
  337. ' ************************************************
  338. Sub SelectFrame(index As Integer)
  339.     SelectedFrame = index
  340.     SelectingFrame = True
  341.     SBar.Value = index
  342.     SelectingFrame = False
  343.     FrameLabel.Caption = Format$(index) & "/" & Format$(NumFrames)
  344.     DrawSelected
  345. End Sub
  346. ' ************************************************
  347. ' Set the point that anchors the selected control
  348. ' point. For example, when moving a hand the
  349. ' corresponding elbow is the control point.
  350. ' ************************************************
  351. Sub SetAnchor()
  352.     Select Case DragPoint
  353.         Case PART_HEAD  ' The head has no anchor.
  354.             AnchorX = -1
  355.         Case PART_LELBOW, PART_RELBOW
  356.             Frames(SelectedFrame).Position _
  357.                 PART_SHOULDERS, AnchorX, AnchorY
  358.         Case PART_LHAND
  359.             Frames(SelectedFrame).Position _
  360.                 PART_LELBOW, AnchorX, AnchorY
  361.         Case PART_RHAND
  362.             Frames(SelectedFrame).Position _
  363.                 PART_RELBOW, AnchorX, AnchorY
  364.         Case PART_LKNEE, PART_RKNEE
  365.             Frames(SelectedFrame).Position _
  366.                 PART_HIPS, AnchorX, AnchorY
  367.         Case PART_LFOOT
  368.             Frames(SelectedFrame).Position _
  369.                 PART_LKNEE, AnchorX, AnchorY
  370.         Case PART_RFOOT
  371.             Frames(SelectedFrame).Position _
  372.                 PART_RKNEE, AnchorX, AnchorY
  373.     End Select
  374. End Sub
  375. ' ***********************************************
  376. ' Give the form and all the picture boxes an
  377. ' hourglass cursor.
  378. ' ***********************************************
  379. Sub WaitStart()
  380.     MousePointer = vbHourglass
  381.     Canvas.MousePointer = vbHourglass
  382.     DoEvents
  383. End Sub
  384. ' ***********************************************
  385. ' Restore the mouse pointers for the form and all
  386. ' the picture boxes.
  387. ' ***********************************************
  388. Sub WaitEnd()
  389.     MousePointer = vbDefault
  390.     Canvas.MousePointer = vbDefault
  391. End Sub
  392. ' ************************************************
  393. ' Grab the nearest control point within distance
  394. ' NEAR of the mouse.
  395. ' ************************************************
  396. Private Sub Canvas_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  397. Dim i As Integer
  398. Dim best_i As Integer
  399. Dim best_dist As Long
  400. Dim dx As Long
  401. Dim dy As Long
  402. Dim dist As Long
  403. Dim fx As Integer
  404. Dim fy As Integer
  405.     ' Find the closest control point.
  406.     best_dist = NEAR + 1
  407.     For i = MIN_PART To MAX_CONTROL_PART
  408.         Frames(SelectedFrame).Position i, fx, fy
  409.         dx = x - fx
  410.         dy = y - fy
  411.         dist = Sqr(dx * dx + dy * dy)
  412.         If best_dist > dist Then
  413.             best_dist = dist
  414.             best_i = i
  415.         End If
  416.     Next i
  417.     ' If nothing is close enough, leave.
  418.     If best_dist > NEAR Then
  419.         Beep
  420.         Exit Sub
  421.     End If
  422.     ' Begin moving the control point.
  423.     Dragging = True
  424.     DragPoint = best_i
  425.     Canvas.DrawMode = vbInvert
  426.     SetAnchor
  427.     DragX = x
  428.     DragY = y
  429.     DrawDrag
  430. End Sub
  431. ' ************************************************
  432. ' Continue dragging a control point.
  433. ' ************************************************
  434. Private Sub Canvas_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  435.     If Not Dragging Then Exit Sub
  436.     ' Erase the old highlight.
  437.     DrawDrag
  438.     ' Draw the new highlight.
  439.     AdjustPoint x, y
  440.     DrawDrag
  441. End Sub
  442. ' ************************************************
  443. ' Finish dragging the control point.
  444. ' ************************************************
  445. Private Sub Canvas_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  446.     If Not Dragging Then Exit Sub
  447.     Dragging = False
  448.     ' Erase the old highlight.
  449.     DrawDrag
  450.     Canvas.DrawMode = vbCopyPen
  451.     ' Adjust the control point.
  452.     AdjustPoint x, y
  453.     Frames(SelectedFrame).MoveControlPoint _
  454.         DragPoint, AnchorX, AnchorY, DragX, DragY
  455.     DataModified = True
  456.     DrawSelected
  457. End Sub
  458. ' ************************************************
  459. ' Play the animation.
  460. ' ************************************************
  461. Private Sub CmdPlay_Click()
  462.     If Playing Then
  463.         Playing = False
  464.         CmdPlay.Caption = "Stopped"
  465.         CmdPlay.Enabled = False
  466.     Else
  467.         Playing = True
  468.         CmdPlay.Caption = "Stop"
  469.         PlayData
  470.         CmdPlay.Caption = "Play"
  471.         Playing = False
  472.         CmdPlay.Enabled = True
  473.         DrawSelected
  474.     End If
  475. End Sub
  476. ' ************************************************
  477. ' Play the animation.
  478. ' ************************************************
  479. Sub PlayData()
  480. Dim mpf As Long     ' Milliseconds per frame.
  481. Dim frame As Integer
  482. Dim next_time As Long
  483. Dim play_type As Integer
  484. Dim num As Integer
  485. Dim start_time As Single
  486. Dim stop_time As Single
  487.     ' See how fast we should go.
  488.     If Not IsNumeric(FPSText.Text) Then _
  489.         FPSText.Text = "10"
  490.     mpf = 1000 \ CLng(FPSText.Text)
  491.     ' See what kind of animation this should be.
  492.     For play_type = 0 To 2
  493.         If PlayOption(play_type).Value Then Exit For
  494.     Next play_type
  495.     If play_type > 2 Then play_type = 0
  496.     ' Start the animation.
  497.     start_time = Timer
  498.     next_time = GetTickCount()
  499.     Do While Playing
  500.         ' Show the frames.
  501.         For frame = 1 To NumFrames
  502.             If Not Playing Then Exit Do
  503.             num = num + 1
  504.             
  505.             ' Draw the frame.
  506.             Canvas.Cls
  507.             Frames(frame).Draw Canvas, False
  508.                 
  509.             ' Wait until it's time for the next frame.
  510.             next_time = next_time + mpf
  511.             WaitTill next_time
  512.         Next frame
  513.         ' If this is a one time deal, stop now.
  514.         If play_type = 0 Then Exit Do
  515.         
  516.         ' If this is a reversing run, go backwards.
  517.         If play_type = 2 Then
  518.             For frame = NumFrames - 1 To 2 Step -1
  519.                 If Not Playing Then Exit Do
  520.                 num = num + 1
  521.                 
  522.                 ' Draw the frame.
  523.                 Canvas.Cls
  524.                 Frames(frame).Draw Canvas, False
  525.                     
  526.                 ' Wait until it's time for the next frame.
  527.                 next_time = next_time + mpf
  528.                 WaitTill next_time
  529.             Next frame
  530.         End If
  531.     Loop
  532.     stop_time = Timer
  533.     MsgBox "Displayed" & Str$(num) & _
  534.         " frames in " & _
  535.         Format$(stop_time - start_time, "0.00") & _
  536.         " seconds (" & _
  537.         Format$(num / (stop_time - start_time), "0.00") & _
  538.         " FPS)."
  539. End Sub
  540. ' ************************************************
  541. ' Make the tweens.
  542. ' ************************************************
  543. Private Sub CmdTween_Click()
  544. Dim num_tweens As Integer
  545. Dim num_frames As Integer
  546. Dim frame1 As Integer
  547. Dim frame2 As Integer
  548. Dim frac1 As Single
  549. Dim i As Integer
  550. Dim frame As Integer
  551.     ' See how many tweens to make.
  552.     If Not IsNumeric(TweensText.Text) Then _
  553.         TweensText.Text = "4"
  554.     num_tweens = TweensText.Text
  555.     If num_tweens < 1 Then num_tweens = 1
  556.     ' Make room for the new frames.
  557.     num_frames = num_tweens * (NumFrames - 1) + NumFrames
  558.     ReDim Preserve Frames(1 To num_frames)
  559.     ' Spread the original frames out.
  560.     For frame = NumFrames To 2 Step -1
  561.         frame1 = num_tweens * (frame - 1) + frame
  562.         Frames(frame1).CopyFrame Frames(frame)
  563.     Next frame
  564.     ' Make the tweens.
  565.     For frame = 1 To NumFrames - 1
  566.         frame1 = num_tweens * (frame - 1) + frame
  567.         frame2 = frame1 + num_tweens + 1
  568.         For i = frame1 + 1 To frame2 - 1
  569.             frac1 = (frame2 - i) / (frame2 - frame1)
  570.             Frames(i).Tween frac1, _
  571.                 Frames(frame1), Frames(frame2)
  572.         Next i
  573.     Next frame
  574.     NumFrames = num_frames
  575.     SBar.Max = NumFrames
  576.     SelectFrame num_tweens * (SelectedFrame - 1) + _
  577.         SelectedFrame
  578.     DataModified = True
  579. End Sub
  580. Private Sub Form_Load()
  581.     ' Create a single default frame.
  582.     NumFrames = 1
  583.     ReDim Frames(1 To NumFrames)
  584.     With Frames(1)
  585.         .SetParameters _
  586.             Canvas.ScaleWidth / 2, _
  587.             (Canvas.ScaleHeight - .MaxHeight) / 2 + _
  588.                 .HeadRoom, _
  589.             210, -30, 150, 30, 240, -60, 255, -75
  590.     End With
  591.     ' Position the scroll bar.
  592.     SBar.Top = Canvas.Top + Canvas.Height + 1
  593.     SelectFrame 1
  594. End Sub
  595. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  596.     Cancel = Not DataSafe()
  597. End Sub
  598. Private Sub Form_Unload(Cancel As Integer)
  599.     End
  600. End Sub
  601. Private Sub mnuFileExit_Click()
  602.     Unload Me
  603. End Sub
  604. ' ************************************************
  605. ' Load a robot script file.
  606. ' ************************************************
  607. Private Sub mnuFileLoad_Click()
  608. Dim fname As String
  609.     If Not DataSafe() Then Exit Sub
  610.     ' Allow the user to pick a file.
  611.     On Error Resume Next
  612.     FileDialog.FilterIndex = 1
  613.     FileDialog.filename = "*.ROB"
  614.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  615.     FileDialog.ShowOpen
  616.     If Err.Number = cdlCancel Then
  617.         Exit Sub
  618.     ElseIf Err.Number <> 0 Then
  619.         Beep
  620.         MsgBox "Error selecting file.", , vbExclamation
  621.         Exit Sub
  622.     End If
  623.     On Error GoTo 0
  624.     fname = Trim$(FileDialog.filename)
  625.     FileDialog.InitDir = Left$(fname, Len(fname) _
  626.         - Len(FileDialog.FileTitle) - 1)
  627.     ' Load the robot script file.
  628.     WaitStart
  629.     LoadScript fname
  630.     WaitEnd
  631. End Sub
  632. ' ************************************************
  633. ' Save the robot script file.
  634. ' ************************************************
  635. Private Sub mnuFileSave_Click()
  636.     If FileLoaded = "" Then
  637.         mnuFileSaveAs_Click
  638.         Exit Sub
  639.     End If
  640.     WaitStart
  641.     SaveScript FileLoaded
  642.     WaitEnd
  643. End Sub
  644. ' ************************************************
  645. ' Save the robot script file with a new name.
  646. ' ************************************************
  647. Private Sub mnuFileSaveAs_Click()
  648. Dim fname As String
  649.     ' Allow the user to pick a file.
  650.     On Error Resume Next
  651.     FileDialog.FilterIndex = 1
  652.     FileDialog.filename = "*.ROB"
  653.     FileDialog.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  654.     FileDialog.ShowSave
  655.     If Err.Number = cdlCancel Then
  656.         Exit Sub
  657.     ElseIf Err.Number <> 0 Then
  658.         Beep
  659.         MsgBox "Error selecting file.", , vbExclamation
  660.         Exit Sub
  661.     End If
  662.     On Error GoTo 0
  663.     fname = Trim$(FileDialog.filename)
  664.     FileDialog.InitDir = Left$(fname, Len(fname) _
  665.         - Len(FileDialog.FileTitle) - 1)
  666.     ' Save the robot script file.
  667.     WaitStart
  668.     SaveScript fname
  669.     WaitEnd
  670. End Sub
  671. ' ************************************************
  672. ' Insert a frame next to the selected one.
  673. ' ************************************************
  674. Private Sub AddFrame()
  675. Dim i As Integer
  676.     NumFrames = NumFrames + 1
  677.     ReDim Preserve Frames(1 To NumFrames)
  678.     For i = NumFrames - 1 To SelectedFrame Step -1
  679.         Frames(i + 1).CopyFrame Frames(i)
  680.     Next i
  681.     SBar.Max = NumFrames
  682.     mnuFrameDelete.Enabled = (NumFrames > 1)
  683.     DataModified = True
  684. End Sub
  685. ' ************************************************
  686. ' Insert a frame after the selected one.
  687. ' ************************************************
  688. Private Sub mnuFrameAfter_Click()
  689.     AddFrame
  690.     SelectFrame SelectedFrame + 1
  691. End Sub
  692. ' ************************************************
  693. ' Insert a frame before the selected one.
  694. ' ************************************************
  695. Private Sub mnuFrameBefore_Click()
  696.     AddFrame
  697.     FrameLabel.Caption = Format$(SelectedFrame) & "/" & Format$(NumFrames)
  698. End Sub
  699. ' ************************************************
  700. ' Delete the selected frame.
  701. ' ************************************************
  702. Private Sub mnuFrameDelete_Click()
  703. Dim i As Integer
  704.     For i = SelectedFrame To NumFrames - 1
  705.         Frames(i).CopyFrame Frames(i + 1)
  706.     Next i
  707.     NumFrames = NumFrames - 1
  708.     ReDim Preserve Frames(1 To NumFrames)
  709.     SBar.Max = NumFrames
  710.     If SelectedFrame > NumFrames Then _
  711.        SelectedFrame = NumFrames
  712.     SelectFrame SelectedFrame
  713.     mnuFrameDelete.Enabled = (NumFrames > 1)
  714.     DataModified = True
  715. End Sub
  716. ' ************************************************
  717. ' Select a new frame.
  718. ' ************************************************
  719. Private Sub SBar_Change()
  720.     If SelectingFrame Then Exit Sub
  721.     SelectFrame SBar.Value
  722. End Sub
  723. ' ************************************************
  724. ' Select a new frame.
  725. ' ************************************************
  726. Private Sub SBar_Scroll()
  727.     SBar_Change
  728. End Sub
  729.