none
Problem with Events and interactivity from Small Basic Curriculum RRS feed

  • Question

  • Hi people,

    I'm a beginner in small basic and i have a problem understanding the code in example 3.5 from the beginner curriculum (events and interactivity)http://msdn.microsoft.com/nl-nl/beginner/gg597501(en-us).aspx.

    In the code behind the "DRAW" button the variable draw is set to 1 and paint() subroutine is called.

    In the paint() there is a check for draw =1 and the subroutine mousedrag is called from the mousemove event.

    After the 1e call to the paint() the mousedrag() keeps running.

    My questions:

    1. Is it normal that the mousedrag() keeps running on every mousemove even after i clicked another button like "add cirkel". And if not how can i stop it?

    2. I do not understand why there is a draw =1 check in the paint().  When is this not true? I can't get the code to get into the ELSE part of the paint(). I see no difference leaving it out and just do the call to the mousedrag(). This draw = 1 check is for deactivating the drawline function after you clicked another button ( I think). This check  takes place in the mousedrag() also. What am I not seeing?

    3. There is a isleftbuttondown check in the ELSE part o the paint(). Why?? What do i have to do to get this check to be true. I know i have to keep the left button down, but when and where and above all why?

    Thank you

    Greetings from holland

     

     

    GraphicsWindow.Hide()
    w = 620
    h = 450
    GraphicsWindow.CanResize = "False"
    GraphicsWindow.Width = w
    GraphicsWindow.Height = h
    GraphicsWindow.Top = (Desktop.Height-h) / 2
    GraphicsWindow.Left = (Desktop.Width-w) / 2
    GraphicsWindow.Show()
    GraphicsWindow.Title = "Events and interactivity"
    GUI()
    Controls.ButtonClicked = MouseAction
    Sub GUI  
      GraphicsWindow.DrawRectangle(10, 10, 600, 320) 
      GraphicsWindow.DrawRectangle(10, 340, 200, 100)
      GraphicsWindow.DrawRectangle(10, 340, 600, 100)
      GraphicsWindow.DrawRectangle(370, 340, 150, 100)   
      Triangle = Controls.AddButton("Triangle", 40, 345)
      Controls.SetSize(Triangle, 120, 30)
      Rectangle = Controls.AddButton("Rectangle",40,375)
      Controls.SetSize(Rectangle, 120, 30) 
      Circle = Controls.AddButton("Circle", 40, 405)
      Controls.SetSize(Circle, 120, 30)
      Rotate = Controls.AddButton("Rotate", 230, 360)
      Controls.SetSize(Rotate, 60, 60)
      Zoom = Controls.AddButton("Zoom", 290, 360)
      Controls.SetSize(Zoom, 60, 60)
      FreeHand = Controls.AddButton("Draw", 390, 360)
      Controls.SetSize(FreeHand, 60, 60)   
      Clear = Controls.AddButton("Clear", 450, 360)
      Controls.SetSize(Clear, 60, 60)
      Exit = Controls.AddButton("Exit", 530, 360)
      Controls.SetSize(Exit, 60, 60)     
    EndSub 
    Sub MouseAction
      x = GraphicsWindow.MouseX
      y = GraphicsWindow.MouseY   
      GraphicsWindow.PenWidth = 1  
      If x > 40 And x < 160 Then     
        GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()  
        If y > 345 And y < 375 Then        
          draw = 0
          j = 0
          z = 1
          tri = Shapes.AddTriangle(20, 20, 100, 100, 150, 20)
          Shapes.Move(tri, 80, 100)
        EndIf   
        If y > 375 And y < 405 Then 
          draw = 0      
          j = 0  
          z = 2   
          rect = Shapes.AddRectangle(100, 100)
          Shapes.Move(rect, 250, 150)
        EndIf     
        If y > 405 And y < 435 Then
          draw = 0
          j = 0       
          z = 3       
          circ = Shapes.AddEllipse(100, 100)
          Shapes.Move(circ, 400, 150)
        EndIf       
      EndIf     
      If y > 360 And y < 420 Then        
        If x > 230 And x < 290 Then           
          draw = 0    
          If z = 1 Then         
            Shapes.Rotate(tri, 30 + m)
          Else
            If z = 2 Then      
              Shapes.Rotate(rect,30 + m)   
            Else
              If z = 3 Then      
                Shapes.Rotate(circ, 30 + m)     
              Endif            
            Endif  
          Endif 
          m = m + 30              
        EndIf       
        If x > 290 And x < 390 Then
          draw = 0          
          i = 0.1 + j
          If i < 0.4 Then   
            If z = 1 Then         
              Shapes.Zoom(tri, 1 + i, 1 + i)
            Else
              If z = 2 Then      
                Shapes.Zoom(rect, 1 + i, 1 + i)
              Else
                If z = 3 Then      
                  Shapes.Zoom(circ, 1 + i, 1 + i)       
                EndIf     
              EndIf  
            EndIf      
            j = j + 0.1
          EndIf 
        EndIf          
        If x > 390 And x < 450 Then
          draw = 1
          Paint()
        EndIf      
        If x > 450 And x < 510 Then
          draw = 0
          j = 0
          GraphicsWindow.Clear()    
          GraphicsWindow.BrushColor = "Blue"
          GUI()
          draw = 0
        EndIf      
        If x > 530 And x < 590 Then
          draw = 0
          Program.End()
        EndIf      
      EndIf      
    EndSub
    Sub Paint
      If draw = 1 Then     
        GraphicsWindow.MouseMove = MouseDrag          
      Else
        If Mouse.IsLeftButtonDown Then     
          MouseAction()    
        EndIf
      EndIf     
    EndSub 
    
    Sub MouseDrag  
      If draw = 1 then  
        x = GraphicsWindow.MouseX
        y = GraphicsWindow.MouseY    
        If x > 10 And x < 600 And y > 10 And y < 320 Then    
          If Mouse.IsLeftButtonDown Then
            GraphicsWindow.DrawLine(OrgX, OrgY, x, y)
          EndIf     
        EndIf  
        OrgX = x
        OrgY = y
      EndIf
    EndSub

     


    Friday, December 16, 2011 3:26 PM

Answers

  • Hi,

    1. Is it normal that the mousedrag() keeps running on every mousemove even after i clicked another button like "add cirkel". And if not how can i stop it?

    Even though MouseDrag is called every time the mouse moves it only does anything if the variable draw = 1.  Draw is set to 0 (therefore no drawing) if any other button is pressed.  This is reasonable for an event to be called, but doesn't actually do anything depending on the current state of the program.

    2. I do not understand why there is a draw =1 check in the paint().  When is this not true? I can't get the code to get into the ELSE part of the paint(). I see no difference leaving it out and just do the call to the mousedrag(). This draw = 1 check is for deactivating the drawline function after you clicked another button ( I think). This check  takes place in the mousedrag() also. What am I not seeing?

    You are right I think, Paint() is only called directly after draw is set to 1 so the code never gets into the Else.

    3. There is a isleftbuttondown check in the ELSE part o the paint(). Why?? What do i have to do to get this check to be true. I know i have to keep the left button down, but when and where and above all why?

    Same as above - it cannot ever get there.

    Well spotted - my guess is that the code was developed bit by bit as is usual and at some point the author changed the logic a bit and redundant code was left behind.  Also using the coordinates of the buttons is a bit out of date now that we have Controls.LastClickedButton.

     

    • Marked as answer by Dutch Beginner Friday, December 16, 2011 3:53 PM
    Friday, December 16, 2011 3:47 PM
    Moderator

All replies

  • Hi,

    1. Is it normal that the mousedrag() keeps running on every mousemove even after i clicked another button like "add cirkel". And if not how can i stop it?

    Even though MouseDrag is called every time the mouse moves it only does anything if the variable draw = 1.  Draw is set to 0 (therefore no drawing) if any other button is pressed.  This is reasonable for an event to be called, but doesn't actually do anything depending on the current state of the program.

    2. I do not understand why there is a draw =1 check in the paint().  When is this not true? I can't get the code to get into the ELSE part of the paint(). I see no difference leaving it out and just do the call to the mousedrag(). This draw = 1 check is for deactivating the drawline function after you clicked another button ( I think). This check  takes place in the mousedrag() also. What am I not seeing?

    You are right I think, Paint() is only called directly after draw is set to 1 so the code never gets into the Else.

    3. There is a isleftbuttondown check in the ELSE part o the paint(). Why?? What do i have to do to get this check to be true. I know i have to keep the left button down, but when and where and above all why?

    Same as above - it cannot ever get there.

    Well spotted - my guess is that the code was developed bit by bit as is usual and at some point the author changed the logic a bit and redundant code was left behind.  Also using the coordinates of the buttons is a bit out of date now that we have Controls.LastClickedButton.

     

    • Marked as answer by Dutch Beginner Friday, December 16, 2011 3:53 PM
    Friday, December 16, 2011 3:47 PM
    Moderator
  • Thank you litdev for your quick respons. You saved me a headache.

     

    Friday, December 16, 2011 3:59 PM
  • Hello litdev and Dutch
    I'm just studying this exercise in section 3.4 of Curriculum (Spanish)
    And I am really a headache
    After a few pages of events and interactivity (in Introduction and Curriculum) presented this exercise of 137 sentences.
    They include the Controls function just is explained in section 3.5
    Perhaps I would help a few simple to understand these functions. If not I shall more to understand it.
    Best regards

    carlosfmur - Buenos Aires
    Monday, January 2, 2012 10:38 PM