# 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


# takes an event and 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 panel.
# 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 on the panel. If the circle
# button has been clicked draws the outline of a circle at the x, y of the
# click on the panel.
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":
        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"



# 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"] = draw
panel["press"] = click_down
panel["release"] = click_up
bird_button["command"] = bird_helper
line_button["command"] = line_helper
circle_button["command"] = circle_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




