changeset 37:d2fb660ca49f

Remove "opts" argument from Layer methods.
author Mikhail Kryshen <mikhail@kryshen.net>
date Fri, 09 Jul 2010 01:08:45 +0400
parents 5413b188d112
children af3187fdc44d
files src/kryshen/indyvon/component.clj src/kryshen/indyvon/core.clj src/kryshen/indyvon/demo.clj src/kryshen/indyvon/layers.clj
diffstat 4 files changed, 65 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/src/kryshen/indyvon/component.clj	Thu Jul 08 07:03:24 2010 +0400
+++ b/src/kryshen/indyvon/component.clj	Fri Jul 09 01:08:45 2010 +0400
@@ -28,13 +28,13 @@
                 *update* #(.repaint component)
                 *bounds* bounds
                 *clip* bounds]
-        (render! layer nil)
+        (render! layer)
         (commit event-dispatcher)))))
 
 (defn preferred-size [component layer]
   (binding [*target* component
             *font-context*' (font-context component)]
-    (let [s (size layer nil)]
+    (let [s (size layer)]
       (Dimension. (:width s) (:height s)))))
 
 (defn make-jpanel
--- a/src/kryshen/indyvon/core.clj	Thu Jul 08 07:03:24 2010 +0400
+++ b/src/kryshen/indyvon/core.clj	Fri Jul 09 01:08:45 2010 +0400
@@ -30,8 +30,8 @@
 
 (defprotocol Layer
   "Basic UI element."
-  (render! [this opts])
-  (size [this opts]))
+  (render! [this])
+  (size [this]))
 
 ;; TODO: modifiers
 (defrecord MouseEvent [id when x y x-on-screen y-on-screen button])
@@ -48,18 +48,18 @@
 
 (defprotocol Anchored
   "Provide anchor point for Layers. Used by viewport."
-  (anchor [this h-align v-align opts]
+  (anchor [this h-align v-align]
      "Anchor point: [x y], h-align could be :left, :center or :right,
       v-align is :top, :center or :bottom"))
 
 ;; Default implementation of Anchored for any Layer.
 (extend-protocol Anchored
   kryshen.indyvon.core.Layer
-  (anchor [this h-align v-align opts]
+  (anchor [this h-align v-align]
           (if (and (= h-align :left)
                    (= v-align :top))
             (Location. 0 0)
-            (let [size (size this opts)]
+            (let [size (size this)]
               (Location.
                (case h-align
                      :top 0
@@ -78,23 +78,29 @@
     (.setColor (:fore-color theme))
     (.setFont (:font theme))))
 
-(defn intersect [b1 b2]
-  (let [x11 (:x b1)
-        y11 (:y b1)
-        x12 (+ x11 (:width b1))
-        y12 (+ y11 (:height b1))
-        x21 (:x b2)
-        y21 (:y b2)
-        x22 (+ x21 (:width b2))
-        y22 (+ y21 (:height b2))
-        x1 (max x11 x21)
-        y1 (max y11 y21)
-        x2 (min x12 x22)
-        y2 (min y12 y22)]
-    (Bounds. x1 y1 (- x2 x1) (- y2 y1))))
+(defn intersect
+  ([b1 b2]
+     (let [x1 (:x b1)
+           y1 (:y b1)
+           x2 (:x b2)
+           y2 (:y b2)]
+       (intersect x1 y1 (+ x1 (:width b1)) (+ y1 (:height b1))
+                  x2 y2 (+ x2 (:width b2)) (+ y2 (:height b2)))))
+  ([x11 y11 x12 y12, x21 y21 x22 y22]
+     (let [x1 (max x11 x21)
+           y1 (max y11 y21)
+           x2 (min x12 x22)
+           y2 (min y12 y22)]
+       (Bounds. x1 y1 (- x2 x1) (- y2 y1)))))
+
+(defn- create-graphics
+  ([]
+     (create-graphics 0 0 (:width *bounds*) (:height *bounds*)))
+  ([x y w h]
+     (apply-theme (.create *graphics* x y w h) *theme*)))
 
 (defn with-translate* [x y w h f & args]
-  (let [graphics (apply-theme (.create *graphics* x y w h) *theme*)
+  (let [graphics (create-graphics x y w h)
         bounds (Bounds. (+ x (:x *bounds*))
                         (+ y (:y *bounds*))
                         w h)]
@@ -109,8 +115,6 @@
 (defmacro with-translate [x y w h & body]
   `(with-translate* ~x ~y ~w ~h (fn [] ~@body)))
 
-
-
 (defn with-handlers* [handle handlers f & args]
   (apply with-bindings*
          {#'*event-dispatcher*
@@ -136,14 +140,17 @@
     [(:x geometry) (:y geometry) (:width geometry) (:height geometry)]))
 
 (defn draw!
-  "Draw a layer. Geometry is either a map or vector [x y] or
-   [x y width height]."
-  [layer geometry & args]
-  (let [[x y w h] (geometry-vec geometry)
-        size (if-not (and w h) (size layer args))
-        w (or w (:width size))
-        h (or h (:height size))]
-    (with-translate* x y w h render! layer args)))
+  ([layer]
+     (let [graphics (create-graphics)]
+       (try
+         (with-bindings* {#'*graphics* graphics} render! layer)
+         (finally
+          (.dispose graphics)))))
+  ([layer x y]
+     (let [size (size layer)]
+       (draw! layer x y (:width size) (:height size))))
+  ([layer x y width height]
+     (with-translate* x y width height render! layer)))
 
 ;;
 ;; EventDispatcher implementation
--- a/src/kryshen/indyvon/demo.clj	Thu Jul 08 07:03:24 2010 +0400
+++ b/src/kryshen/indyvon/demo.clj	Fri Jul 09 01:08:45 2010 +0400
@@ -18,7 +18,7 @@
 (def layer1
      (reify
       Layer
-      (render! [layer opts]
+      (render! [layer]
          (with-handlers layer
            (doto *graphics*
              (.setColor Color/RED)
@@ -26,20 +26,20 @@
            (:mouse-entered e (println e))
            (:mouse-exited e (println e))
            (:mouse-moved e (println e))))
-      (size [layer opts] (Size. 30 20))))
+      (size [layer] (Size. 30 20))))
 
 (def layer1b (border-layer layer1 2 3))
 
 (def layer2
      (reify
       Layer
-      (render! [layer opts]
+      (render! [layer]
          (doto *graphics*
            (.setColor Color/YELLOW)
            (.fillRect 0 0 (:width *bounds*) (:height *bounds*)))
-         (draw! layer1b [10 5])
-         (draw! layer1 [55 5]))
-      (size [layer opts] (Size. 70 65))))
+         (draw! layer1b 10 5)
+         (draw! layer1 55 5))
+      (size [layer] (Size. 70 65))))
 
 (def layer3
      (border-layer (text-layer "Sample\ntext" :right :center)))
@@ -54,8 +54,8 @@
            fl (ref (fps-layer 0.0))]
        (reify
         Layer
-        (render! [layer opts]
-           (render! @fl nil)
+        (render! [layer]
+           (draw! @fl)
            (dosync
             (alter frames + 1)
             (let [time (System/currentTimeMillis)
@@ -64,20 +64,20 @@
                 (ref-set fl (fps-layer (/ @frames elapsed)))
                 (ref-set frames 0)
                 (ref-set last time)))))
-        (size [layer opts] (size @fl nil)))))
+        (size [layer] (size @fl)))))
 
 (def layer
      (reify
       Layer
-      (render! [layer opts]
+      (render! [layer]
          (*update*)
          (doto *graphics*
            (.setColor (rand-nth [Color/BLACK Color/BLUE Color/RED]))
            (.drawLine 0 0 (:width *bounds*) (:height *bounds*)))
-         (draw! layer2 [15 20])
-         (draw! layer3 [100 100 80 50])
-         (render! fps nil))
-      (size [layer opts] (Size. 400 300))))
+         (draw! layer2 15 20)
+         (draw! layer3 100 100 80 50)
+         (draw! fps))
+      (size [layer] (Size. 400 300))))
 
 (defn -main []
   (doto frame
@@ -87,3 +87,4 @@
     (.. (getContentPane) (add (make-jpanel (viewport layer))))
     (.pack)
     (.setVisible true)))
+
--- a/src/kryshen/indyvon/layers.clj	Thu Jul 08 07:03:24 2010 +0400
+++ b/src/kryshen/indyvon/layers.clj	Fri Jul 09 01:08:45 2010 +0400
@@ -33,17 +33,16 @@
   ([content width gap]
      (let [offset (+ width gap)]
        (reify Layer
-        (render! [l opts]
+        (render! [l]
            (let [w (:width *bounds*)
                  h (:height *bounds*)]
              (.setColor *graphics* (:border-color *theme*))
              (doseq [i (range 0 width)]
                (.drawRect *graphics* i i (- w 1 i i) (- h 1 i i)))
-             (apply draw! content
-                    [offset offset (- w offset offset) (- h offset offset)]
-                    opts)))
-        (size [l opts]
-           (let [s (size content opts)]
+             (draw! content
+                    offset offset (- w offset offset) (- h offset offset))))
+        (size [l]
+           (let [s (size content)]
              (Size. (+ (:width s) offset offset)
                     (+ (:height s) offset offset))))))))
 
@@ -69,7 +68,7 @@
   ([text h-align v-align]
      (let [lines (re-split #"\r\n|\n|\r|\u0085|\u2028|\u2029" text)]
        (reify Layer
-        (render! [layer opts]
+        (render! [layer]
            (let [w (:width *bounds*)
                  h (:height *bounds*)
                  font (.getFont *graphics*)
@@ -82,7 +81,7 @@
                        x (align-x (.getAdvance layout) w h-align)]
                    (.draw layout *graphics* x (+ y ascent))
                    (recur (next layouts) (+ y lh)))))))
-        (size [layer opts]
+        (size [layer]
            (let [layouts (layout-text lines (:font *theme*) *font-context*)
                  width (text-width layouts)
                  height (text-height layouts)]
@@ -100,9 +99,9 @@
         last-height (ref 0)]
     (reify
      Layer
-     (render! [layer opts]
+     (render! [layer]
         (with-handlers layer
-         (let [anchor (anchor content h-align v-align opts)
+         (let [anchor (anchor content h-align v-align)
                width (:width *bounds*)
                height (:height *bounds*)]
            (dosync
@@ -110,8 +109,7 @@
             (alter y + (align-y height @last-height v-align))
             (ref-set last-width width)
             (ref-set last-height height))
-           (apply draw! content
-                  [(- 0 @x (:x anchor)) (- 0 @y (:y anchor))] opts))
+           (draw! content (- 0 @x (:x anchor)) (- 0 @y (:y anchor))))
          (:mouse-pressed e
           (dosync
            (ref-set fix-x (:x-on-screen e))
@@ -126,4 +124,4 @@
            (ref-set fix-x (:x-on-screen e))
            (ref-set fix-y (:y-on-screen e)))
           (*update*))))
-     (size [layer opts] (size content opts))))))
+     (size [layer] (size content))))))