view src/net/kryshen/indyvon/demo.clj @ 75:ddfde9cce39a

EventDispatcher can report hovered and picked states for handles.
author Mikhail Kryshen <mikhail@kryshen.net>
date Mon, 30 Aug 2010 20:44:23 +0400
parents a823dd0c2736
children 1ca7872b889b
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)))

(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 (text-layer "Sample\ntext" :right :center)))

(def layer
  (reify
   Layer
   (render! [layer]
      ;;(repaint)
      (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))
   (layer-size [layer]
      (Size. 400 300))))

(def vp (viewport layer))

(def root (fps-layer vp))

(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 root)
  (show-frame (fps-layer (viewport-miniature vp 80 60))))