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

  1. VERSION 4.00
  2. Begin VB.Form TweenForm 
  3.    Caption         =   "TweenEnd"
  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 mnuFileSep1 
  159.          Caption         =   "-"
  160.       End
  161.       Begin VB.Menu mnuFileNew 
  162.          Caption         =   "&New"
  163.          Shortcut        =   ^N
  164.       End
  165.       Begin VB.Menu mnuFileSep2 
  166.          Caption         =   "-"
  167.       End
  168.       Begin VB.Menu mnuFileExit 
  169.          Caption         =   "E&xit"
  170.       End
  171.    End
  172.    Begin VB.Menu mnuFrame 
  173.       Caption         =   "Frame"
  174.       Begin VB.Menu mnuFrameAfter 
  175.          Caption         =   "Insert &After"
  176.       End
  177.       Begin VB.Menu mnuFrameBefore 
  178.          Caption         =   "Insert &Before"
  179.       End
  180.       Begin VB.Menu mnuFrameSep 
  181.          Caption         =   "-"
  182.       End
  183.       Begin VB.Menu mnuFrameClear 
  184.          Caption         =   "&Clear"
  185.       End
  186.       Begin VB.Menu mnuFrameDelete 
  187.          Caption         =   "&Delete"
  188.          Enabled         =   0   'False
  189.       End
  190.    End
  191. Attribute VB_Name = "TweenForm"
  192. Attribute VB_Creatable = False
  193. Attribute VB_Exposed = False
  194. Option Explicit
  195. Dim NumFrames As Integer
  196. Dim Frames() As PolylineFrame
  197. Dim FileLoaded As String
  198. Dim DataModified As Boolean
  199. Dim Playing As Boolean
  200. Dim SelectedFrame As Integer
  201. Dim SelectingFrame As Boolean
  202. Dim Drawing As Boolean
  203. Dim StartX As Integer
  204. Dim StartY As Integer
  205. Dim LastX As Integer
  206. Dim LastY As Integer
  207. ' ************************************************
  208. ' Insert a frame next to the selected one.
  209. ' ************************************************
  210. Private Sub AddFrame()
  211. Dim i As Integer
  212.     NumFrames = NumFrames + 1
  213.     ReDim Preserve Frames(1 To NumFrames)
  214.     For i = NumFrames - 1 To SelectedFrame Step -1
  215.         CopyFrame i, i + 1
  216.     Next i
  217.     SBar.Max = NumFrames
  218.     mnuFrameDelete.Enabled = (NumFrames > 1)
  219.     DataModified = True
  220. End Sub
  221. ' ************************************************
  222. ' Copy a polyline from frame1 to frame2.
  223. ' ************************************************
  224. Sub CopyFrame(frame1 As Integer, frame2 As Integer)
  225. Dim pline As Integer
  226. Dim point As Integer
  227.     Frames(frame2).NumPolylines = Frames(frame1).NumPolylines
  228.     If Frames(frame2).NumPolylines < 1 Then
  229.         Erase Frames(frame2).Poly
  230.     Else
  231.         ReDim Frames(frame2).Poly(1 To Frames(frame2).NumPolylines)
  232.     End If
  233.     For pline = 1 To Frames(frame2).NumPolylines
  234.         With Frames(frame2).Poly(pline)
  235.             .NumPoints = Frames(frame1).Poly(pline).NumPoints
  236.             If .NumPoints < 1 Then
  237.                 Erase .X
  238.                 Erase .Y
  239.             Else
  240.                 ReDim .X(1 To .NumPoints)
  241.                 ReDim .Y(1 To .NumPoints)
  242.             End If
  243.             For point = 1 To .NumPoints
  244.                 .X(point) = Frames(frame1).Poly(pline).X(point)
  245.                 .Y(point) = Frames(frame1).Poly(pline).Y(point)
  246.             Next point
  247.         End With
  248.     Next pline
  249. End Sub
  250. ' ************************************************
  251. ' Return true if the data has not been modified,
  252. ' or the user has saved the changes, or the user
  253. ' wants to lose the changes.
  254. ' ************************************************
  255. Function DataSafe() As Boolean
  256. Dim ans As Integer
  257.     Do While DataModified
  258.         ans = MsgBox("The data has been modified." & _
  259.             " Do you want to save the changes?", _
  260.             vbYesNoCancel)
  261.         If ans = vbCancel Then Exit Do
  262.         If ans = vbNo Then
  263.             DataSafe = True
  264.             Exit Function
  265.         End If
  266.             
  267.         ' Otherwise save the data.
  268.         If FileLoaded <> "" Then
  269.             mnuFileSave_Click
  270.         Else
  271.             mnuFileSaveAs_Click
  272.         End If
  273.     Loop
  274.     DataSafe = Not DataModified
  275. End Function
  276. ' ************************************************
  277. ' Draw the indicated frame.
  278. ' ************************************************
  279. Sub DrawFrame(frame As Integer)
  280. Dim pline As Integer
  281. Dim point As Integer
  282.     Canvas.Cls
  283.     For pline = 1 To Frames(frame).NumPolylines
  284.         With Frames(frame).Poly(pline)
  285.             If .NumPoints >= 2 Then
  286.                 Canvas.Line (.X(1), .Y(1))-(.X(2), .Y(2))
  287.                 For point = 3 To .NumPoints
  288.                     Canvas.Line -(.X(point), .Y(point))
  289.                 Next point
  290.             End If
  291.         End With
  292.     Next pline
  293. End Sub
  294. ' ************************************************
  295. ' Save the data.
  296. ' ************************************************
  297. Sub SaveData(fname As String)
  298. Dim fnum As Integer
  299. Dim frame As Integer
  300. Dim pline As Integer
  301. Dim point As Integer
  302.     On Error GoTo SaveDataError
  303.     ' Open the file.
  304.     fnum = FreeFile
  305.     Open fname For Output As fnum
  306.     ' Save the number of frames.
  307.     Write #fnum, NumFrames
  308.     ' Save each frame.
  309.     For frame = 1 To NumFrames
  310.         With Frames(frame)
  311.             ' Save the number of polylines.
  312.             Write #fnum, .NumPolylines
  313.                     
  314.             ' Save each polyline.
  315.             For pline = 1 To .NumPolylines
  316.                 With .Poly(pline)
  317.                     ' Save the number of points.
  318.                     Write #fnum, .NumPoints
  319.                     For point = 1 To .NumPoints
  320.                         Write #fnum, .X(point), .Y(point)
  321.                     Next point
  322.                 End With
  323.             Next pline
  324.         End With
  325.     Next frame
  326.     Close fnum
  327.     FileLoaded = fname
  328.     Caption = "TweenEnd [" & fname & "]"
  329.     DataModified = False
  330.     Exit Sub
  331. SaveDataError:
  332.     Beep
  333.     MsgBox "Error saving file " & fname & "." & _
  334.         vbCrLf & Format$(Err.Number) & " : " & _
  335.         Err.Description
  336.     Exit Sub
  337. End Sub
  338. ' ************************************************
  339. ' Load polyline frames from the file.
  340. ' ************************************************
  341. Sub LoadData(fname As String)
  342. Dim fnum As Integer
  343. Dim frame As Integer
  344. Dim pline As Integer
  345. Dim point As Integer
  346.     On Error GoTo SaveDataError
  347.     ' Open the file.
  348.     fnum = FreeFile
  349.     Open fname For Input As fnum
  350.     ' Read the number of frames.
  351.     Input #fnum, NumFrames
  352.     ReDim Frames(1 To NumFrames)
  353.     SBar.Max = NumFrames
  354.     ' Read each frame.
  355.     For frame = 1 To NumFrames
  356.         With Frames(frame)
  357.             ' Read the number of polylines.
  358.             Input #fnum, .NumPolylines
  359.             ReDim .Poly(1 To .NumPolylines)
  360.                     
  361.             ' Read each polyline.
  362.             For pline = 1 To .NumPolylines
  363.                 With .Poly(pline)
  364.                     ' Read the number of points.
  365.                     Input #fnum, .NumPoints
  366.                     ReDim .X(1 To .NumPoints)
  367.                     ReDim .Y(1 To .NumPoints)
  368.                     For point = 1 To .NumPoints
  369.                         Input #fnum, .X(point), .Y(point)
  370.                     Next point
  371.                 End With
  372.             Next pline
  373.         End With
  374.     Next frame
  375.     Close fnum
  376.     SelectFrame 1
  377.     FileLoaded = fname
  378.     Caption = "TweenEnd [" & fname & "]"
  379.     DataModified = False
  380.     Exit Sub
  381. SaveDataError:
  382.     Beep
  383.     MsgBox "Error loading file " & fname & "." & _
  384.         vbCrLf & Format$(Err.Number) & " : " & _
  385.         Err.Description
  386.     Exit Sub
  387. End Sub
  388. ' ************************************************
  389. ' Select and display the indicated frame.
  390. ' ************************************************
  391. Sub SelectFrame(num As Integer)
  392.     SelectedFrame = num
  393.     ' If we're drawing, stop drawing.
  394.     If Drawing Then
  395.         Canvas.DrawMode = vbCopyPen
  396.         Drawing = False
  397.     End If
  398.     DrawFrame SelectedFrame
  399.     FrameLabel.Caption = Format$(SelectedFrame) _
  400.          & "/" & Format$(NumFrames)
  401.     SelectingFrame = True
  402.     SBar.Value = SelectedFrame
  403.     SelectingFrame = False
  404. End Sub
  405. ' ************************************************
  406. ' Create the tweens between two key frames using
  407. ' endpoint interpolation.
  408. ' ************************************************
  409. Sub MakeTweens(key1 As Integer, key2 As Integer)
  410. Dim frac1 As Single
  411. Dim frac2 As Single
  412. Dim tween As Integer
  413. Dim pline As Integer
  414. Dim point As Integer
  415.     For tween = key1 + 1 To key2 - 1
  416.         frac1 = (key2 - tween) / (key2 - key1)
  417.         frac2 = 1# - frac1
  418.         
  419.         Frames(tween).NumPolylines = Frames(key1).NumPolylines
  420.         ReDim Frames(tween).Poly(1 To Frames(tween).NumPolylines)
  421.         For pline = 1 To Frames(tween).NumPolylines
  422.             With Frames(tween).Poly(pline)
  423.                 .NumPoints = Frames(key1).Poly(pline).NumPoints
  424.                 ReDim .X(1 To .NumPoints)
  425.                 ReDim .Y(1 To .NumPoints)
  426.                 For point = 1 To .NumPoints
  427.                     .X(point) = frac1 * Frames(key1).Poly(pline).X(point) + frac2 * Frames(key2).Poly(pline).X(point)
  428.                     .Y(point) = frac1 * Frames(key1).Poly(pline).Y(point) + frac2 * Frames(key2).Poly(pline).Y(point)
  429.                 Next point
  430.             End With
  431.         Next pline
  432.     Next tween
  433. End Sub
  434. ' ***********************************************
  435. ' Give the form and all the picture boxes an
  436. ' hourglass cursor.
  437. ' ***********************************************
  438. Sub WaitStart()
  439.     MousePointer = vbHourglass
  440.     Canvas.MousePointer = vbHourglass
  441.     DoEvents
  442. End Sub
  443. ' ***********************************************
  444. ' Restore the mouse pointers for the form and all
  445. ' the picture boxes.
  446. ' ***********************************************
  447. Sub WaitEnd()
  448.     MousePointer = vbDefault
  449.     Canvas.MousePointer = vbDefault
  450. End Sub
  451. Private Sub Canvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  452.     If Drawing And Button = vbRightButton Then
  453.         ' End the previous polyline.
  454.         Canvas.Line (StartX, StartY)-(LastX, LastY)
  455.         Canvas.DrawMode = vbCopyPen
  456.         Drawing = False
  457.         Exit Sub
  458.     End If
  459.     ' See if this is the start of a new polyline.
  460.     If Drawing Then
  461.         ' Nope. Erase the previous line.
  462.         Canvas.Line (StartX, StartY)-(LastX, LastY)
  463.     Else
  464.         ' Start a new polyline.
  465.         With Frames(SelectedFrame)
  466.             .NumPolylines = .NumPolylines + 1
  467.             ReDim Preserve .Poly(1 To .NumPolylines)
  468.             With .Poly(.NumPolylines)
  469.                 .NumPoints = 1
  470.                 ReDim .X(1 To 1)
  471.                 ReDim .Y(1 To 1)
  472.                 .X(1) = X
  473.                 .Y(1) = Y
  474.             End With
  475.         End With
  476.         Canvas.DrawMode = vbInvert
  477.         Drawing = True
  478.         DataModified = True
  479.         StartX = X
  480.         StartY = Y
  481.     End If
  482.     LastX = X
  483.     LastY = Y
  484.     Canvas.Line (StartX, StartY)-(LastX, LastY)
  485. End Sub
  486. Private Sub Canvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  487.     If Not Drawing Then Exit Sub
  488.     Canvas.Line (StartX, StartY)-(LastX, LastY)
  489.     LastX = X
  490.     LastY = Y
  491.     Canvas.Line (StartX, StartY)-(LastX, LastY)
  492. End Sub
  493. Private Sub Canvas_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  494.     If Not Drawing Then Exit Sub
  495.     Canvas.Line (StartX, StartY)-(LastX, LastY)
  496.     Canvas.DrawMode = vbCopyPen
  497.     Canvas.Line (StartX, StartY)-(X, Y)
  498.     Canvas.DrawMode = vbInvert
  499.     With Frames(SelectedFrame)
  500.         With .Poly(.NumPolylines)
  501.             .NumPoints = .NumPoints + 1
  502.             ReDim Preserve .X(1 To .NumPoints)
  503.             ReDim Preserve .Y(1 To .NumPoints)
  504.             .X(.NumPoints) = X
  505.             .Y(.NumPoints) = Y
  506.         End With
  507.     End With
  508.     DataModified = True
  509.     StartX = X
  510.     StartY = Y
  511. End Sub
  512. ' ************************************************
  513. ' Play the animation.
  514. ' ************************************************
  515. Private Sub CmdPlay_Click()
  516.     If Playing Then
  517.         Playing = False
  518.         CmdPlay.Caption = "Stopped"
  519.         CmdPlay.Enabled = False
  520.     Else
  521.         Playing = True
  522.         CmdPlay.Caption = "Stop"
  523.         PlayData
  524.         CmdPlay.Caption = "Play"
  525.         Playing = False
  526.         CmdPlay.Enabled = True
  527.         DrawFrame SelectedFrame
  528.     End If
  529. End Sub
  530. ' ************************************************
  531. ' Play the animation.
  532. ' ************************************************
  533. Sub PlayData()
  534. Dim mpf As Long     ' Milliseconds per frame.
  535. Dim frame As Integer
  536. Dim next_time As Long
  537. Dim play_type As Integer
  538. Dim num As Integer
  539. Dim start_time As Single
  540. Dim stop_time As Single
  541.     ' See how fast we should go.
  542.     If Not IsNumeric(FPSText.Text) Then _
  543.         FPSText.Text = "10"
  544.     mpf = 1000 \ CLng(FPSText.Text)
  545.     ' See what kind of animation this should be.
  546.     For play_type = 0 To 2
  547.         If PlayOption(play_type).Value Then Exit For
  548.     Next play_type
  549.     If play_type > 2 Then play_type = 0
  550.     ' Start the animation.
  551.     start_time = Timer
  552.     next_time = GetTickCount()
  553.     Do While Playing
  554.         ' Show the frames.
  555.         For frame = 1 To NumFrames
  556.             If Not Playing Then Exit Do
  557.             num = num + 1
  558.             
  559.             ' Draw the frame.
  560.             DrawFrame frame
  561.                 
  562.             ' Wait until it's time for the next frame.
  563.             next_time = next_time + mpf
  564.             WaitTill next_time
  565.         Next frame
  566.         ' If this is a one time deal, stop now.
  567.         If play_type = 0 Then Exit Do
  568.         
  569.         ' If this is a reversing run, go backwards.
  570.         If play_type = 2 Then
  571.             For frame = NumFrames - 1 To 2 Step -1
  572.                 If Not Playing Then Exit Do
  573.                 num = num + 1
  574.                 
  575.                 ' Draw the frame.
  576.                 DrawFrame frame
  577.                     
  578.                 ' Wait until it's time for the next frame.
  579.                 next_time = next_time + mpf
  580.                 WaitTill next_time
  581.             Next frame
  582.         End If
  583.     Loop
  584.     stop_time = Timer
  585.     MsgBox "Displayed" & Str$(num) & _
  586.         " frames in " & _
  587.         Format$(stop_time - start_time, "0.00") & _
  588.         " seconds (" & _
  589.         Format$(num / (stop_time - start_time), "0.00") & _
  590.         " FPS)."
  591. End Sub
  592. ' ************************************************
  593. ' Make the tweens.
  594. ' ************************************************
  595. Private Sub CmdTween_Click()
  596. Dim num_tweens As Integer
  597. Dim old_frames As Integer
  598. Dim frame1 As Integer
  599. Dim frame2 As Integer
  600. Dim frame As Integer
  601.     ' See how many tweens to make.
  602.     If Not IsNumeric(TweensText.Text) Then _
  603.         TweensText.Text = "4"
  604.     num_tweens = TweensText.Text
  605.     If num_tweens < 1 Then num_tweens = 1
  606.     ' Make room for the new frames.
  607.     old_frames = NumFrames
  608.     NumFrames = num_tweens * (NumFrames - 1) + NumFrames
  609.     ReDim Preserve Frames(1 To NumFrames)
  610.     ' Spread the original frames out.
  611.     For frame = old_frames To 2 Step -1
  612.         CopyFrame frame, _
  613.             num_tweens * (frame - 1) + frame
  614.     Next frame
  615.     ' Make the tweens.
  616.     For frame = 1 To old_frames - 1
  617.         frame1 = num_tweens * (frame - 1) + frame
  618.         frame2 = frame1 + num_tweens + 1
  619.         MakeTweens frame1, frame2
  620.     Next frame
  621.     SBar.Max = NumFrames
  622.     SelectFrame num_tweens * (SelectedFrame - 1) + _
  623.         SelectedFrame
  624.     DataModified = True
  625. End Sub
  626. Private Sub Form_Load()
  627.     ' Position the scroll bar.
  628.     SBar.Top = Canvas.Top + Canvas.Height + 1
  629.     ' Create an empty frame.
  630.     mnuFileNew_Click
  631. End Sub
  632. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  633.     Cancel = Not DataSafe()
  634. End Sub
  635. Private Sub Form_Unload(Cancel As Integer)
  636.     End
  637. End Sub
  638. Private Sub mnuFileExit_Click()
  639.     Unload Me
  640. End Sub
  641. ' ************************************************
  642. ' Load a data file.
  643. ' ************************************************
  644. Private Sub mnuFileLoad_Click()
  645. Dim fname As String
  646.     If Not DataSafe() Then Exit Sub
  647.     ' Allow the user to pick a file.
  648.     On Error Resume Next
  649.     FileDialog.FilterIndex = 1
  650.     FileDialog.filename = "*.TWE"
  651.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  652.     FileDialog.ShowOpen
  653.     If Err.Number = cdlCancel Then
  654.         Exit Sub
  655.     ElseIf Err.Number <> 0 Then
  656.         Beep
  657.         MsgBox "Error selecting file.", , vbExclamation
  658.         Exit Sub
  659.     End If
  660.     On Error GoTo 0
  661.     fname = Trim$(FileDialog.filename)
  662.     FileDialog.InitDir = Left$(fname, Len(fname) _
  663.         - Len(FileDialog.FileTitle) - 1)
  664.     ' Load the data file.
  665.     WaitStart
  666.     LoadData fname
  667.     WaitEnd
  668.     FrameLabel.Caption = Format$(SelectedFrame) _
  669.          & "/" & Format$(NumFrames)
  670. End Sub
  671. ' ************************************************
  672. ' Clear out all the data.
  673. ' ************************************************
  674. Private Sub mnuFileNew_Click()
  675.     If Not DataSafe() Then Exit Sub
  676.     NumFrames = 1
  677.     ReDim Frames(1 To NumFrames)
  678.     Frames(1).NumPolylines = 0
  679.     SBar.Max = NumFrames
  680.     SelectFrame 1
  681. End Sub
  682. ' ************************************************
  683. ' Save the data file.
  684. ' ************************************************
  685. Private Sub mnuFileSave_Click()
  686.     If FileLoaded = "" Then
  687.         mnuFileSaveAs_Click
  688.         Exit Sub
  689.     End If
  690.     WaitStart
  691.     SaveData FileLoaded
  692.     WaitEnd
  693. End Sub
  694. ' ************************************************
  695. ' Save the data file with a new name.
  696. ' ************************************************
  697. Private Sub mnuFileSaveAs_Click()
  698. Dim fname As String
  699.     ' Allow the user to pick a file.
  700.     On Error Resume Next
  701.     FileDialog.FilterIndex = 1
  702.     FileDialog.filename = "*.TWE"
  703.     FileDialog.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  704.     FileDialog.ShowSave
  705.     If Err.Number = cdlCancel Then
  706.         Exit Sub
  707.     ElseIf Err.Number <> 0 Then
  708.         Beep
  709.         MsgBox "Error selecting file.", , vbExclamation
  710.         Exit Sub
  711.     End If
  712.     On Error GoTo 0
  713.     fname = Trim$(FileDialog.filename)
  714.     FileDialog.InitDir = Left$(fname, Len(fname) _
  715.         - Len(FileDialog.FileTitle) - 1)
  716.     ' Save the script file.
  717.     WaitStart
  718.     SaveData fname
  719.     WaitEnd
  720. End Sub
  721. ' ************************************************
  722. ' Insert a frame after the selected one.
  723. ' ************************************************
  724. Private Sub mnuFrameAfter_Click()
  725.     AddFrame
  726.     SelectFrame SelectedFrame + 1
  727. End Sub
  728. ' ************************************************
  729. ' Insert a frame before the selected one.
  730. ' ************************************************
  731. Private Sub mnuFrameBefore_Click()
  732.     AddFrame
  733.     FrameLabel.Caption = Format$(SelectedFrame) & "/" & Format$(NumFrames)
  734. End Sub
  735. ' ************************************************
  736. ' Remove the polylines from the selected frame.
  737. ' ************************************************
  738. Private Sub mnuFrameClear_Click()
  739. Dim i As Integer
  740.     With Frames(SelectedFrame)
  741.         .NumPolylines = 0
  742.         Erase .Poly
  743.     End With
  744.     SelectFrame SelectedFrame
  745.     DataModified = True
  746. End Sub
  747. ' ************************************************
  748. ' Delete the selected frame.
  749. ' ************************************************
  750. Private Sub mnuFrameDelete_Click()
  751. Dim i As Integer
  752.     For i = SelectedFrame To NumFrames - 1
  753.         CopyFrame i + 1, i
  754.     Next i
  755.     NumFrames = NumFrames - 1
  756.     ReDim Preserve Frames(1 To NumFrames)
  757.     SBar.Max = NumFrames
  758.     If SelectedFrame > NumFrames Then _
  759.        SelectedFrame = NumFrames
  760.     SelectFrame SelectedFrame
  761.     mnuFrameDelete.Enabled = (NumFrames > 1)
  762.     DataModified = True
  763. End Sub
  764. ' ************************************************
  765. ' Select a new frame.
  766. ' ************************************************
  767. Private Sub SBar_Change()
  768.     If SelectingFrame Then Exit Sub
  769.     SelectFrame SBar.Value
  770. End Sub
  771. ' ************************************************
  772. ' Select a new frame.
  773. ' ************************************************
  774. Private Sub SBar_Scroll()
  775.     SBar_Change
  776. End Sub
  777.