Pseudocode Task

Hint: Identify the parts of the pseudocode that relate to event detection, event subscription, and event handling in a simple drag-and-draw program.

PROGRAM SimpleDragDraw
 
  DEFINE canvasWidth = 600
  DEFINE canvasHeight = 400
  DEFINE penColor = "black"
  DEFINE penThickness = 2
 
  INITIALIZE canvas (width: canvasWidth, height: canvasHeight)
  INITIALIZE pen (color: penColor, thickness: penThickness)
  MOVE_PEN_SILENTLY_TO(canvasWidth/2, canvasHeight/2)
  SET penState TO "up"
 
  PROCEDURE handleMouseDrag(xCoord, yCoord):
    IF penState IS "down":
      DRAW_LINE_TO(xCoord, yCoord) // Pen draws only if it's down
    ENDIF
    MOVE_PEN_SILENTLY_TO(xCoord, yCoord) // Pen position always updates
  END PROCEDURE
 
  PROCEDURE handleMouseButtonPress(xCoord, yCoord):
    MOVE_PEN_SILENTLY_TO(xCoord, yCoord) // Move to click start point
    SET penState TO "down"               // Prepare to draw
  END PROCEDURE
 
  PROCEDURE handleMouseButtonRelease(xCoord, yCoord):
    IF penState IS "down": // If it was drawing, ensure final point is part of line
        DRAW_LINE_TO(xCoord, yCoord)
    ENDIF
    SET penState TO "up"                 // Stop drawing
  END PROCEDURE
 
  PROCEDURE handleClearKeyPress():
    CLEAR_DRAWING_AREA()
    MOVE_PEN_SILENTLY_TO(canvasWidth/2, canvasHeight/2)
    SET penState TO "up"
  END PROCEDURE
 
  SUBSCRIBE mouse_drag_event TO handleMouseDrag
  SUBSCRIBE mouse_button_press_event TO handleMouseButtonPress
  SUBSCRIBE mouse_button_release_event TO handleMouseButtonRelease
  SUBSCRIBE key_press_event('c') TO handleClearKeyPress
 
  LISTEN_FOR_EVENTS()
 
END PROGRAM SimpleDragDraw