# Allison Obourn
# CS 115, Autumn 2021
# Lecture 36

# This program has a green background with a title at the top and a drawing
# area with a yellow background taking up the rest of the space. When the
# user clicks on this drawing area, holds their mouse down and moves it, a
# black line will be drawn following their mouse. When they lift up their
# mouse button again the line will stop. 

from ECGUI import *


# takes an event as a parameter and draws a line from the last x, y position
# of the mouse to the current x, y position of the mouse. Then makes the last
# position the current position. 
def draw(event):
    global last_x
    global last_y
    # only draw when the mouse is pressed down
    # otherwise don't do anything
    if clicked:
        x = event.x
        y = event.y
        panel.draw_line(x, y, last_x, last_y)
        # reset the last postion to the position we just moved to
        # so that  next time the line will be drawn back here not to
        # our starting point
        last_x = x
        last_y = y

# records when the user clicks down in the clicked variable and stores the
# current x, y of the click in the last_x and last_y variables. Takes an
# event as a parameter
def click_down(event):
    global clicked
    global last_x
    global last_y
    clicked = True
    last_x = event.x
    last_y = event.y

# takes an event as a parameter and records that the user is no longer
# pressing the mouse down in the clicked variable
def click_up(event):
    global clicked
    clicked = False

# creates a window with a title and drawing area
window = make_window("Lab 10", "green")
add_label(window, "Lab 10 GUI")
panel = add_canvas(window, 500, 300, "yellow")

# sets which functions should run when each mouse action occurs
panel["move"] = draw
panel["press"] = click_down
panel["release"] = click_up

# initialize variables so that our program thinks the user starts out
# not pressing the mouse button
clicked = False
last_x = 0
last_y = 0


