import sas_pygame as sas


view = sas.createView(350, 500, "black","Tangram")
figur = sas.Rectangle(200,100, 120, 120, "white")
anleitung = sas.Text(130, 450, "Mit Mausklick drehen." , "white");
anleitung2 = sas.Text(130, 470, "Mit 'u' Parallelogramm umdrehen","white");
anleitung.setFont(12);
anleitung2.setFont(12);


def gibDreieckGross(pX, pY, pFarbe):
     pol = sas.Polygon([(pX, pY)], pFarbe) 
     pol.add(120,0)
     pol.add(60, -60)
     return pol

def gibDreieckMittel(pX, pY, pFarbe):
     pol = sas.Polygon([(pX, pY)], pFarbe) 
     pol.add(60,0)
     pol.add(60, -60)
     return pol

def gibDreieckKlein(pX, pY, pFarbe):
     pol = sas.Polygon([(pX, pY)], pFarbe) 
     pol.add(60,0)
     pol.add(30, -30)
     return pol

def gibRaute(pX, pY, pFarbe):
     pol = sas.Polygon([(pX, pY)], pFarbe); 
     pol.add(30,-30);
     pol.add(60, 0);
     pol.add(30,30);
     return pol

def gibParallelogramm(pX, pY, pFarbe):
     pol = sas.Polygon([(pX, pY)], pFarbe) 
     pol.add(60,0)
     pol.add(90, -30)
     pol.add(30, -30)
     return pol

def gibFigur(x, y, grundfigur):
    clickedFigure = None
    for figur in grundfigur:
        if figur.contains(x ,y):
            clickedFigure = figur
    return clickedFigure


def main():
    grundfigur = [
        gibDreieckGross(10, 80, "magenta"),
        gibDreieckGross(10, 160, "red"),
        gibDreieckMittel(10, 240, "green"),
        gibDreieckKlein(10, 290, "blue"),
        gibDreieckKlein(10, 340, "purple"),
        gibRaute(10, 390, "orange"),
        gibParallelogramm(10, 470, "yellow"),
    ]
    picked = None
    dragging = False
    moved = False
    lastX = lastY = 0
    while view.running:
        view.step()
        # Maus gedrückt: Figur auswählen
        if view.mousePressed():
            lastX, lastY = view.getMouseX(), view.getMouseY()
            picked = gibFigur(lastX, lastY, grundfigur)
            dragging = picked is not None
            moved = False
        # Maus bewegen während gedrückt: verschieben
        if dragging and picked is not None and view.mouseDragged():
            neuX, neuY = view.getMouseX(), view.getMouseY()
            dx, dy = neuX - lastX, neuY - lastY
            picked.move(dx, dy)
            moved = True
            lastX, lastY = neuX, neuY
        # Maus losgelassen: wenn nicht gezogen -> drehen, sonst nur beenden
        if picked is not None and view.mouseReleased():
            if not moved:
                picked.turn(45)
            else:
                moved = False
                dragging = False
            picked = None
        if view.keyPressed('u'):
            #Das Parallelogramm wird umgedreht
            grundfigur[6].flipHorizontal()
            view.keyBufferDelete()
      

    view.quit()
        

main()


