# 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. 

from ECGUI import *
from animals import *


# takes an event as a parameter 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. 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 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 as a parameter. If the bird button has been pressed it draws a bird
# at the x of the click
def click_down(event):
    global clicked
    global last_x
    global last_y
    clicked = True
    last_x = event.x
    last_y = event.y
    if mode == "bird":
        bird(panel, event.x)

# 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 instead of a line
def bird_helper():
    global mode
    mode = "bird"

# 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")
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
bird_button["command"] = bird_helper

# initialize variables so that our program thinks the user starts out
# not pressing the mouse button
clicked = False
last_x = 0
last_y = 0
mode = "line" # needed so we can distinguish between line and bird and
              # not try to draw both every time


