   [state]
   (app/title! "brevis")
   (app/vsync! true)
-  (app/key-repeat! false)
+  ;(app/key-repeat! true)
   (enable :blend)
   (enable :depth-test)
   (init-box-graphic)
   (enable :lighting)
   (enable :light0)
   (blend-func :src-alpha :one-minus-src-alpha))
+
+
+(def shift-key-down (atom false))
+(defn sin [n] (float (Math/sin n)))
+(defn cos [n] (float (Math/cos n)))
 
 (defn reshape
   "Reshape after the window is resized."

  #_(println button)
  state)

(defn mouse-wheel
  "Respond to a mousewheel movement. dw is +/- depending on scroll-up or down."
  [dw state]
  #_(println dw)
  state)

;; ## Start a brevis instance

(defn start-gui [initialize update]
  "Start the simulation with a GUI."
  (app/start
   {:reshape reshape, :init init, :mouse-drag mouse-drag, :key-press key-press, :update update, :display display
    ;:mouse-move    (fn [[[dx dy] [x y]] state] (println )
    ;:mouse-up       (fn [[x y] button state] (println button) state)
    ;:mouse-click   (fn [[x y] button state] (println button) state)

               (= (:translate-mode state) :x) {:shift-x (+ (:shift-x state) displacement)}               
               (= (:translate-mode state) :y) {:shift-y (+ (:shift-y state) displacement)}               
               (= (:translate-mode state) :z) {:shift-z (+ (:shift-z state) displacement)})))))

(defn key-press
  "Update the state in response to a keypress."
  [key state]
  (cond
    (= :lshift key) (reset! shift-key-down true)
    (= "p" key) (do (app/pause!)
                  state)
    (= "o" key) (do (screenshot (str "brevis_screenshot_" (System/currentTimeMillis) ".png"))
                  state)
    (= :escape key)
    (do (app/stop!)
      (assoc state 
             :terminated? true)
      ))
  (println @shift-key-down))

(defn key-release
  "Update the state in response to the release of a key"
  [key state]
  (cond
    (= :lshift key) (reset! shift-key-down false)))

;; ## Input handling
(defn mouse-drag
  "Rotate the world."
  [[dx dy] _ button state]
  (def rads (/ (Math/PI) 180))
  (def thetaY (*(:rot-y state) rads))
  (def sY (sin thetaY))
  (def cY (cos thetaY))
  (def thetaX (* (:rot-x state) rads))
  (def sX (sin thetaX))
  (def cX (cos thetaX))
  
  (if @shift-key-down
    (cond 
      ; Rotate
      (= :left button)
      (assoc state
             :rot-x (+ (:rot-x state) dy)
             :rot-y (+ (:rot-y state) dx))
      (= :center button)
      (assoc state
             :shift-x (+ (:shift-x state) (* dx cY))
             :shift-y (- (:shift-y state) (* dy cX))
             :shift-z (+ (:shift-z state) (* dx sY))
             )
      ; Zoom
      (= :right button)
      (assoc state
             :shift-x (+ (:shift-x state) (* (/ dy 6) (* sY -1)))
             :shift-y (+ (:shift-y state) (* (/ dy 6) sX))
             :shift-z (+ (:shift-z state) (* (/ dy 6) cY))           
             ))))


(defn mouse-wheel
  "Respond to a mousewheel movement. dw is +/- depending on scroll-up or down."
  [dw state]
  (def rads (/ (Math/PI) 180))
  (def thetaY (*(:rot-y state) rads))
  (def sY (sin thetaY))
  (def cY (cos thetaY))
  (def thetaX (* (:rot-x state) rads))
  (def sX (sin thetaX))
  (def cX (cos thetaX))
  
  (assoc state
         :shift-z (+ (:shift-z state) (* (/ dw 6) cY))
         :shift-x (+ (:shift-x state) (* (/ dw 6) (* sY -1)))
         :shift-y (+ (:shift-y state) (* (/ dw 6) sX))
         ))

         

#_(defn turnAround
  [rot-x ]
  (if (or (= rot-x 90) (= rot-x -90))
    ;z translation to be 0
    )
  (if (= rot-x 180)
    ;z translation goes negative
    )
  (if (or (= rot-y 90) (= rot-y -90))
    ;z translation to be 0
    )
  (if (= rot-x 180)
    ;z translation goes negative
    )
  )



#_(defn key-press
  "Update the state in response to a keypress."
  [key state]
  (cond
    
   (= "q" key) (assoc state
                      :rotate-mode :x)
   (= "w" key) (assoc state
