/* paint program using the new intuition functions */

(when (not (kick 37)) (write 'I need v37+!') (exit))

(string title)
(set title 'Xpaint++ v0.1 by $#%!')
(set maxx 320) (set maxy 200)
(set col 1) (set tool 0) (set ox -1) (set oy -1)
(set f 0) (set mode 0) (set off 30)

(set depth (+ (req title 'number of colours?' '4|8|16|2') 1))
(set flags (* (req title 'mode?' 'hires|lowres') $8000))
(set flags (+ flags (* (req title 'lace?' 'yes|no') 4)))
(set maxy (+ maxy (* (req title 'pal?' 'yes|no') 56)))
(if (> flags $7FFF) (set maxx (* maxx 2)))
(if (and flags 4) (set maxy (* maxy 2)))

(screen maxx maxy depth flags title)
(win 0 1 maxx (- maxy 1) $268 $100E title)
(line 0 (- off 1) maxx (- off 1) 1)
(gadget 1 5 15 55 'colour')
(gadget 2 65 15 55 'tools')
(gadget 3 125 15 55 'mode')
(gadget 4 185 15 55 'clear')
(gadget 5 245 15 55 'about')
(set noquit 1)

(defun ymouse () (if (or (< (set d (mousey)) off) (> d maxy)) off d))

(defun dogad ()
  (select (gadid)
    1 (set col (req title 'pick colour:' '1|2|3|4|5|6|7|0'))
    2 (do
        (set tool (req title 'use tool:' 'poly|line|box|dot|free'))
        (set ox -1) (set oy -1) (set f 0)
      )
    3 (set mode (req title 'mode:' 'cycle|mirror|norm'))
    4 (box 0 off maxx maxy 0)
    5 (req title 'Xpaint++ written in YAX!' 'ok')
  )
)

(defun dotool (x y)
  (if (eq ox -1) (set ox x))
  (if (eq oy -1) (set oy y))
  (select mode
    1 (set col (if (eq col 15) 1 (+ col 1)))
    2 (set tool 4)
  )
  (select tool
    0 (do
        (while (eq (mouse) 1)
          (set ox x) (set oy y)
          (line ox oy (set x (mousex)) (set y (ymouse)) col)
        )
      )
    1 (line ox oy x y col)
    2 (do (if f (line ox oy x y col)) (set f (- 1 f)))
    3 (do (if f (box ox oy x y col)) (set f (- 1 f)))
    4 (do
        (box x y (+ x 2) (+ y 2) col)
        (if (eq mode 2) (box (- maxx x) y (+ (- maxx x) 2) (+ y 2) col))
      )
  )
  (set ox x) (set oy y)
)

(while noquit
  (select (message)
    $200 (set noquit 0)
    $040 (dogad)
    $020 (dogad)
    $008 (dotool (mousex) (ymouse))
  )
)
