(include "sci.sh")
(use "Main")
(use "Obj")
(script 997)



(class IconI of Obj
    (properties
        view -1
        loop -1
        cel -1
        nsLeft 0
        nsTop -1
        nsRight 0
        nsBottom 0
        state 1
        cursor -1
        type 16384
        message -1
        modifiers 0
        signal 1
    )

    (method (right theNsLeft theNsTop)
        (var temp0[7])
        (if (paramTotal)
            = nsRight (+ (= nsLeft theNsLeft) CelWide(view loop cel))
            = nsBottom (+ (= nsTop theNsTop) CelHigh(view loop cel))
        )
        DrawCel(view loop cel nsLeft nsTop -1)
    )


    (method (enable param1)
        (var newEvent, temp1, temp2)
        return 
            (if ((paramTotal and param1) and (& signal $0001))
                = temp1 1
                DrawCel(view loop temp1 nsLeft nsTop -1)
                Graph(12 nsTop nsLeft nsBottom nsRight 1)
                (while (<> (send ((= newEvent (Event:new()))):type) 2)
                    (send newEvent:localize())
                    (if ((self:helpStr(newEvent)))
                        (if (not temp1)
                            = temp1 1
                            DrawCel(view loop temp1 nsLeft nsTop -1)
                            Graph(12 nsTop nsLeft nsBottom nsRight 1)
                        )
                    )(else
                        (if (temp1)
                            = temp1 0
                            DrawCel(view loop temp1 nsLeft nsTop -1)
                            Graph(12 nsTop nsLeft nsBottom nsRight 1)
                        )
                    )
                    (send newEvent:dispose())
                )
                (send newEvent:dispose())
                (if (== temp1 1)
                    DrawCel(view loop 0 nsLeft nsTop -1)
                    Graph(12 nsTop nsLeft nsBottom nsRight 1)
                )
                return temp1
            )(else
                return 1
            )
    )


    (method (setCursor param1)
        (var temp0, temp1, temp2, temp3, temp4)
        = temp0 (+ nsTop 1)
        = temp1 (+ nsLeft 1)
        = temp2 (- nsBottom 2)
        = temp3 (- nsRight 3)
        Graph(4 temp0 temp1 temp0 temp3 param1 -1 -1)
        Graph(4 temp0 temp3 temp2 temp3 param1 -1 -1)
        Graph(4 temp2 temp3 temp2 temp1 param1 -1 -1)
        Graph(4 temp2 temp1 temp0 temp1 param1 -1 -1)
        Graph(12 - nsTop 2 - nsLeft 2 + nsBottom 3 + nsRight 3 1)
    )


    (method (helpStr param1)
        return 
            (if (((>= (send param1:x) nsLeft) and (>= (send param1:y) nsTop)) and (<= (send param1:x) nsRight))
                <= (send param1:y) nsBottom
            )(else
                0
            )
    )


    (method (maskView)
        (var theState)
        = theState state
        = state (& state $fffa)
        (if (& theState $0001)
            = state (| state $0004)
        )
        (if (& theState $0004)
            = state (| state $0001)
        )
    )

)
(class IconBar of Set
    (properties
        elements 0
        size 0
        state 1
        maskLoop 0
        underBits 0
        maskCel 0
        highlightColor 0
        lowlightColor 0
        show 0
        getDistToPt 0
        deletePt 0
        movePt 0
        port 0
        window 0
    )

    (method (doit)
        (var temp0, temp1, temp2, temp3)
        (asm
code_0581:  pushi   #new
            pushi   0
            class   Event
            send    4
            sat     temp0
            bnt     code_067c
            pushi   #localize
            pushi   0
            send    4
            pushi   #firstTrue
            pushi   2
            pushi   178
            lst     temp0
            self    8
            sat     temp2
            pushi   #type
            pushi   0
            lat     temp0
            send    4
            push    
            dup     
            ldi     0
            eq?     
            bnt     code_05e1
            pushi   #y
            pushi   0
            lat     temp0
            send    4
            push    
            pToa    maskLoop
            gt?     
            bnt     code_05c6
            ldi     0
            aTop    highlightColor
            jmp     code_067c
            jmp     code_0671
code_05c6:  lat     temp2
            bnt     code_0671
            push    
            pToa    show
            ne?     
            bnt     code_0671
            ldi     0
            aTop    highlightColor
            pushi   #setCursor
            pushi   1
            lst     temp2
            self    6
            jmp     code_0671
code_05e1:  dup     
            ldi     1
            eq?     
            bnt     code_05fe
            lat     temp2
            bnt     code_0671
            pushi   #enable
            pushi   2
            push    
            pushi   1
            self    8
            bnt     code_0671
            jmp     code_067c
            jmp     code_0671
code_05fe:  dup     
            ldi     4
            eq?     
            bnt     code_0671
            pushi   #message
            pushi   0
            lat     temp0
            send    4
            sat     temp1
            push    
            ldi     27
            eq?     
            bnt     code_061b
            jmp     code_067c
            jmp     code_0671
code_061b:  lst     temp1
            ldi     13
            eq?     
            bnt     code_0631
            pushi   #enable
            pushi   1
            lst     temp2
            self    6
            jmp     code_067c
            jmp     code_0671
code_0631:  lst     temp1
            ldi     19200
            eq?     
            bt      code_0651
            lst     temp1
            ldi     3840
            eq?     
            bnt     code_065a
            pushi   #modifiers
            pushi   0
            lat     temp0
            send    4
            push    
            ldi     3
            and     
            bnt     code_065a
code_0651:  pushi   #advance
            pushi   0
            self    4
            jmp     code_0671
code_065a:  lst     temp1
            ldi     19712
            eq?     
            bt      code_066b
            lst     temp1
            ldi     9
            eq?     
            bnt     code_0671
code_066b:  pushi   #cycle
            pushi   0
            self    4
code_0671:  toss    
            pushi   #dispose
            pushi   0
            lat     temp0
            send    4
            jmp     code_0581
code_067c:  pushi   1
            lst     temp0
            callk   IsObject, 2
            bnt     code_06a6
            pTos    highlightColor
            pToa    maskLoop
            lt?     
            bnt     code_069f
            pushi   #x
            pushi   0
            lat     temp0
            send    4
            aTop    maskCel
            pushi   #y
            pushi   0
            lat     temp0
            send    4
            aTop    highlightColor
code_069f:  pushi   #dispose
            pushi   0
            lat     temp0
            send    4
code_06a6:  pushi   #highlight
            pushi   4
            pushi   #cursor
            pushi   0
            pToa    lowlightColor
            send    4
            push    
            pushi   1
            pTos    maskCel
            pTos    highlightColor
            lag     gGame
            send    12
            pushi   #clean
            pushi   0
            self    4
            ret     
        )
    )


    (method (handleEvent pEvent)
        (var temp0)
        (send pEvent:localize())
        (if (((not (send pEvent:type) and (< (send pEvent:y) maskLoop)) and not (= temp0 0)) or (((== (send pEvent:type) 4) and (== (send pEvent:message) 27)) and (= temp0 1)))
            (if (temp0)
                (send gGame:highlight(global19 1 (+ (send lowlightColor:nsLeft) (/ (- (send lowlightColor:nsRight) (send lowlightColor:nsLeft)) 2)) (- (send lowlightColor:nsBottom) 13)))
            )
            = maskCel (send pEvent:x)
            = highlightColor (send pEvent:y)
            (self:right())
        )(else
            (if (== (send pEvent:type) 1)
                (if (& (send pEvent:modifiers) $0003)
                    (self:getDistToLine())
                    (send pEvent:claimed(1))
                )(else
                    (if (& (send pEvent:modifiers) $0004)
                        (self:saveForUndo())
                        (send pEvent:claimed(1))
                    )(else
                        (if (IsObject(lowlightColor))
                            (send pEvent:
                                type((send lowlightColor:type))
                                message((send lowlightColor:message))
                            )
                        )
                    )
                )
            )
        )
    )


    (method (right)
        (var temp0, temp1, temp2, temp3, temp4, temp5, temp6, temp7)
        (send gGame:highlight(999 1))
        = temp0 (self:at(0))
        = maskLoop CelHigh((send temp0:view) (send temp0:loop) (send temp0:cel))
        (if (gTheNewDialog)
            (send gTheNewDialog:dispose())
        )
        = port GetPort()
        SetPort(0)
        = underBits Graph(7 0 0 maskLoop 320 1)
        = temp1 PicNotValid()
        PicNotValid(1)
        = temp3 0
        = temp4 0
        = temp5 FirstNode(elements)
        (while (temp5)
            = temp6 NextNode(temp5)
            = temp7 NodeValue(temp5)
            (if (not IsObject(temp7))
                return 
            )
            (if (<= (send temp7:nsRight) 0)
                (send temp7:right(temp3 temp4))
                = temp3 (send temp7:nsRight)
            )(else
                (send temp7:right())
            )
            = temp5 temp6
        )
        (if (deletePt)
            = temp3 (+ (/ (- (- (send movePt:nsRight) (send movePt:nsLeft)) CelWide((send deletePt:view) 1 (send deletePt:cel))) 2) (send movePt:nsLeft))
            = temp4 (+ (/ (- (- (send movePt:nsBottom) (send movePt:nsTop)) CelHigh((send deletePt:view) 1 (send deletePt:cel))) 2) (send movePt:nsTop))
            DrawCel((send deletePt:view) 1 (send deletePt:cel) temp3 temp4 -1)
        )
        PicNotValid(temp1)
        Graph(12 0 0 maskLoop 320 1)
        (self:setCursor(lowlightColor))
        (send gGame:highlight(global19 (+ (send lowlightColor:nsLeft) (/ (- (send lowlightColor:nsLeft) (send lowlightColor:nsRight)) 2))))
        (self:doit())
    )


    (method (clean)
        Graph(11 0 0 10 320 1 0)
        Graph(8 underBits)
        Graph(12 0 0 maskLoop 320 1)
        SetPort(port)
        = maskLoop 0
    )


    (method (cycle)
        (var temp0)
        = temp0 (self:at((+ 1 (self:indexOf(show)))))
        (if (not IsObject(temp0))
            = temp0 NodeValue((self:first()))
        )
        (send gGame:highlight(global19 1 (+ (send temp0:nsLeft) (/ (- (send temp0:nsRight) (send temp0:nsLeft)) 2)) (- (send temp0:nsBottom) 3)))
        (self:setCursor(temp0))
    )


    (method (advance)
        (var temp0)
        = temp0 (self:at((- (self:indexOf(show)) 1)))
        (if (not IsObject(temp0))
            = temp0 NodeValue((self:last()))
        )
        (send gGame:highlight(global19 1 (+ (send temp0:nsLeft) (/ (- (send temp0:nsRight) (send temp0:nsLeft)) 2)) (- (send temp0:nsBottom) 3)))
        (self:setCursor(temp0))
    )


    (method (enable param1 param2)
        (asm
            pushi   157
            pushi   1
            lsp     paramTotal
            ldi     2
            ge?     
            bnt     code_07a1
            lap     param2
code_07a1:  push    
            lap     param1
            send    6
            bnt     code_07be
            pushi   #signal
            pushi   0
            lap     param1
            send    4
            push    
            ldi     2
            and     
            not     
            bnt     code_07bc
            lap     param1
            aTop    lowlightColor
code_07bc:  ldi     1
code_07be:  ret     
        )
    )


    (method (setCursor theShow)
        (var temp0)
        (if (IsObject(show))
            = temp0 
                (if (window)
                    (send window:back)
                )(else
                    5
                )
            (send show:setCursor(temp0))
        )
        = show theShow
        = temp0 12
        (send theShow:setCursor(1))
    )


    (method (saveForUndo)
        (if (<> lowlightColor (self:at(0)))
            = getDistToPt lowlightColor
            = lowlightColor (self:at(0))
        )(else
            (if (getDistToPt)
                = lowlightColor getDistToPt
            )
        )
        (send gGame:highlight((send lowlightColor:cursor) 1))
    )


    (method (getDistToLine)
        (var theLowlightColor)
        = theLowlightColor lowlightColor
        (while (& (send ((= theLowlightColor (self:at((% (+ (self:indexOf(theLowlightColor)) 1) size))))):signal) $0002)
        )
        = lowlightColor theLowlightColor
        (send gGame:highlight((send lowlightColor:cursor) 1))
    )

)
