view src/net/kryshen/indyvon/demo.clj @ 80:880ae8e03408

Measure time intervals between repaints. Experiments.
author Mikhail Kryshen <mikhail@kryshen.net>
date Wed, 01 Sep 2010 22:25:55 +0400
parents 1ca7872b889b
children 5d2153e8a28d
line wrap: on
line source

;;
;; Copyright 2010 Mikhail Kryshen <mikhail@kryshen.net>
;;
;; This file is part of Indyvon.
;;
;; Indyvon is free software: you can redistribute it and/or modify it
;; under the terms of the GNU Lesser General Public License version 3
;; only, as published by the Free Software Foundation.
;;
;; Indyvon is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public
;; License along with Indyvon.  If not, see
;; <http://www.gnu.org/licenses/>.
;;

(ns net.kryshen.indyvon.demo
  (:gen-class)
  (:use
   (net.kryshen.indyvon core layers component))
  (:import
   (net.kryshen.indyvon.core Size)
   (java.awt Color)
   (javax.swing JFrame)))

(defn draw-button!
  "Draws button immediately (but uses callback for button action
   unlike IMGUI)."
  [id content callback & args]
  (with-handlers id
    (let [shadow-offset 2
          padding 4
          border-width 1
          offset (if (picked? id) (/ shadow-offset 2) 0)
          ^Color color (:alt-back-color *theme*)
          color (if (hovered? id) (.brighter color) color)
          width (- *width* shadow-offset)
          height (- *height* shadow-offset)]
      (with-color Color/BLACK
        (.fillRect *graphics* shadow-offset shadow-offset width height))
      (with-color color
        (.fillRect *graphics* offset offset width height))
      (draw! (border content border-width padding)
             offset offset width height))
    ;; Event handlers
    (:mouse-entered _ (repaint))
    (:mouse-exited _ (repaint))
    (:mouse-pressed _ (repaint))
    (:mouse-released _ (repaint))
    (:mouse-clicked _ (apply callback args))))

(defn combine-colors [^Color color1 ^Color color2 c]
  (case c
    0.0 color1
    1.0 color2
    (let [rgb1 (.getRGBComponents color1 nil)
          rgb2 (.getRGBComponents color2 nil)
          rgb (float-array (map #(+ (* (- 1 c) %1) (* c %2)) rgb1 rgb2))]
      (Color. (aget rgb 0) (aget rgb 1) (aget rgb 2) (aget rgb 3)))))

(defn animate
  "Changes atom value according to specified range, speed, and current
   frame interval. Invokes repaint if change happens."
  [atom from to speed]
  (let [prev @atom
        state (cond
               (zero? speed) :stop
               (= prev from) (if (pos? speed) :start :stop)               
               (= prev to) (if (neg? speed) :start :stop)
               :default :continue)]
    ;;(println prev from to speed state *interval*)
    (if (= state :stop)
       prev
       (let [interval (if (= state :start) 1 *interval*)
             step (* speed interval 1E-9)
             val (swap! atom #(-> % (+ step) (max from) (min to)))]
         (repaint)
         val))))

(defn button
  "Create animated button layer."
  [content callback & args]
  (let [padding 4
        border-width 1
        shadow-offset 2
        face (border content padding border-width)
        highlight (atom 0)
        animation-speed (atom 0)]
    (interval-layer
     (reify
      Layer
      (render! [button]
        (with-handlers button
          (let [hovered (hovered? button)
                offset (if (picked? button) (/ shadow-offset 2) 0)
                color (combine-colors
                       (:alt-back-color *theme*) Color/WHITE
                       (animate highlight 0.0 1.0 @animation-speed))
                width (- *width* shadow-offset)
                height (- *height* shadow-offset)]
            (with-color Color/BLACK
              (.fillRect *graphics*
                         shadow-offset shadow-offset
                         width height))
            (with-color color
              (.fillRect *graphics* offset offset width height))
            (draw! (border content border-width padding)
                   offset offset width height))
          ;; Event handlers
          (:mouse-entered _
            (reset! animation-speed 4)
            (repaint))
          (:mouse-exited _
            (reset! animation-speed -2)
            (repaint))
          (:mouse-pressed _ (repaint))
          (:mouse-released _ (repaint))
          (:mouse-clicked _ (apply callback args))))
     (layer-size [button]
        (let [face-size (layer-size face)]
          (Size. (+ (:width face-size) shadow-offset)
                 (+ (:height face-size) shadow-offset))))))))

(def button1 (button (label "Animated button 1")
                     println "Animated button 1 clicked"))

(def button2 (button (label "Animated button 2")
                     println "Animated button 2 clicked"))

(def layer1
  (reify
   Layer
   (render! [layer]
      (with-handlers layer
        (with-color (if (hovered? layer) Color/ORANGE Color/RED)
          (.fillRect *graphics* 0 0 *width* *height*))
        (:mouse-entered e
         (repaint)
         (println e))
        (:mouse-exited e
         (repaint)
         (println e))
        (:mouse-moved e
         (println e))))
   (layer-size [layer]
      (Size. 30 20))))

(def layer1b (border layer1 2 3))

(def layer2
  (reify
   Layer
   (render! [layer]
      (doto *graphics*
        (.setColor Color/YELLOW)
        (.fillRect 0 0 *width* *height*))
      (with-rotate 0.5 0 0
        (draw! layer1b 30 25))
      (draw! layer1 55 5))
   (layer-size [layer]
      (Size. 70 65))))

(def layer2m (miniature layer2 30 30))

(def layer3 (border (label "Sample\ntext" :right :center)))

(def layer
  (reify
   Layer
   (render! [layer]
      ;;(repaint)
      ;;(println (format "%.3f" (/ *interval* 1E9)))
      (doto *graphics*
        ;; Random color to see when repaint happens.
        (.setColor (rand-nth [Color/BLACK Color/BLUE Color/RED]))
        (.drawLine 0 0 *width* *height*)
        (.drawLine *width* 0 0 *height*))
      (draw! layer2 15 20)
      (draw! layer2m 120 50)
      (draw! layer3 100 100 80 50)
      (draw! button1 50 160)
      (with-rotate (/ Math/PI 6) 250 200
        (draw! button1 200 160))
      (draw! button2 50 250)
      (with-bounds 100 200 140 30
        (draw-button! :button
         (label "Immediate button" :center :center)
         #(println "Button clicked!"))))
   (layer-size [layer]
      (Size. 400 300))))

;; Main viewport
(def vp (viewport layer))

;; Miniature (rendered asynchronously)
(def vp-miniature (border (viewport-miniature vp 100 75)))

;; Main scene
(def scene
  (fps-layer
   (decorate-layer vp [_]
     (draw! vp)
     (draw! vp-miniature (- *width* 105) 5))))

(defn show-frame [layer]
  (doto (make-jframe "Test" layer)
    (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
    (.setVisible true)))

(defn -main []
  (println "Try to drag the viewport.")
  (show-frame scene))