# Allison Obourn
# CS 115, Autumn 2021
# Lecture 37

# 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
# program starts it is in line mode and so when the user clicks on the
# 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. If the user clicks the bird button the program will
# switch to bird mode. In this mode the program will draw a bird at the x
# coordinate of the click when the user presses their mouse down on the
# canvas. If the user clicks the circle button it will draw the outline of
# a circle when they click on the canvas. If the user clicks the line button
# it will go back to drawing a line. 

from ECGUI import *
import animals

# These have to be global as we have to change them in our event handlers
# (functions that run when clicks or other events happen). We can pass extra
# data to these funcitons using lambdas but we still can't return from them
# or preserve changes made to variables
mode = "line" # action type that will happen when we click the canvas
last_x = 0    # most recent mouse x position
last_y = 0    # most recent mouse y position
clicked = False   # if the mouse button is pressed down

# takes an event and a canvas as parameters and if in line mode and the user
# has clicked down the mouse button draws a line from the last x, y position
# of the mouse to the current x, y position of the mouse on the passed in panel.
# Then makes the last position the current position. 
def draw(event, panel):
    global last_x
    global last_y
    # only draw when the mouse is pressed down
    # otherwise don't do anything
    if clicked and mode == "line":
        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 and a canvas as parameters. If the bird button has been pressed it
# draws a bird at the x of the click on the passed in canvas. If the circle
# button has been clicked draws the outline of a circle at the x, y of the
# click on the passed in canvas
def click_down(event, panel):
    global last_x
    global last_y
    global clicked
    clicked = True
    last_x = event.x
    last_y = event.y
    if mode == "bird":
        animals.bird(panel, event.x)
    elif mode == "circle":
        panel.draw_oval(event.x, event.y, 10, 10, "blue")

# 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

# sets our mode to bird so that next time the user clicks on the canvas
# a bird will be drawn 
def bird_helper():
    global mode
    mode = "bird"

# sets our mode to line so that next time the user clicks on the canvas
# a line will be drawn 
def line_helper():
    global mode
    mode = "line"

# sets our mode to circle so that next time the user clicks on the canvas
# a circle will be drawn 
def circle_helper():
    global mode
    mode = "circle"


def main():                  
    # creates a window with a title and button in a top row and a drawing area
    window = make_window("Lab 10", "green")
    # needed so we have the label and button in a row and the canvas below
    top_frame = add_frame(window)
    add_label(top_frame, "Lab 10 GUI", side="left")
    bird_button = add_button(top_frame, "bird", side="left")
    line_button = add_button(top_frame, "line")
    circle_button = add_button(top_frame, "circle")
    panel = add_canvas(window, 500, 300, "yellow")

    # sets which functions should run when each mouse action occurs
    panel["move"] = lambda event: draw(event, panel)
    panel["press"] = lambda event : click_down(event, panel)
    panel["release"] = click_up
    bird_button["command"] = bird_helper
    line_button["command"] = line_helper
    circle_button["command"] = circle_helper

    

main()


