-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacks.clj
61 lines (54 loc) · 1.71 KB
/
stacks.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(ns strigui.stacks
(:require [clojure2d.core :as c2d]
[strigui.widget :as wdg]))
(def ^:private width-per-stack 41)
(defn draw-item-lines
[canvas val x y]
(loop [y-offset (- y 5)
curr-val val]
(when (> curr-val 0)
(c2d/with-canvas-> canvas
(c2d/set-color :green)
(c2d/set-stroke 3)
(c2d/line x y-offset (+ x 30) y-offset))
(recur (- y-offset 6) (- curr-val 1)))))
(defn draw-stack
[canvas val x y h]
(let [height (+ h 20)
x-offset (+ x 35)]
(c2d/with-canvas-> canvas
(c2d/set-color :black)
(c2d/set-stroke 2)
(c2d/line x y x (+ y height))
(c2d/line x-offset y x-offset (+ y height)))
(draw-item-lines canvas val (+ x 3) (+ y height))))
(defn height
[items]
(let [sum (apply + items)]
(* 3 (+ sum 6))))
(defn draw-stacks
[canvas stack-vals x y]
(loop [x-offset x
cur-index 0]
(when (< cur-index (count stack-vals))
(draw-stack canvas (nth stack-vals cur-index) x-offset y (height stack-vals))
(recur (+ x-offset 45) (inc cur-index)))))
(defrecord Stack [name value coordinates args]
wdg/Widget
(coord [this] (:coordinates this))
(value [this] (:value this))
(args [this] (:args this))
(widget-name [this] (:name this))
(draw [this canvas]
(let [[x y _ _] (wdg/coord this)]
(draw-stacks canvas (wdg/value this) x y)
this))
(redraw
[this canvas]
(wdg/draw this canvas)))
(defn create
[canvas name item-list args]
(let [width (* (count item-list) width-per-stack)
coord [(:x args) (:y args) width (height item-list)]
stack (Stack. name item-list coord args)]
stack))