-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.clj
80 lines (73 loc) · 3.41 KB
/
box.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(ns strigui.box
(:require [clojure2d.core :as c2d]
[strigui.widget :as wdg]))
(set! *warn-on-reflection* true)
(def ^:private default-font-size 15)
(defn box-coord
"Computes the full box coordinates.
Returns the vector [x y border-width border-heigth]"
[canvas text {:keys [^long x ^long y ^long height ^long width font-size]}]
(let [size (if (number? font-size) font-size default-font-size)
text-box (c2d/with-canvas-> canvas
(c2d/set-font-attributes size)
(c2d/text-bounding-box text))
text-width (nth text-box 2)
text-heigth (nth text-box 3)
btn-w (* text-width 1.8)
btn-h (* text-heigth 1.8)
border-width (if (and (number? width) (< btn-w width)) width btn-w)
border-heigth (if (and (number? height) (< btn-h height)) height btn-h)]
[x y border-width border-heigth]))
(defn box-draw-text
"Draws the text of the box"
[canvas text {:keys [^long x ^long y color ^long width font-style font-size] :as args}]
(let [style (if (empty? font-style) :bold (first font-style))
size (if (number? font-size) font-size default-font-size)
[_ _ border-width border-heigth] (box-coord canvas text args)
[_ text-y text-width _] (c2d/with-canvas-> canvas
(c2d/set-font-attributes size style)
(c2d/text-bounding-box text))
background-color (if (> (count color) 0) (first color) :grey)
foreground-color (if (> (count color) 1) (nth color 1) :black)
x-offset (if (and (number? width) (>= border-width width))
(/ (- border-width text-width) 2.0)
(* border-width 0.12))]
(c2d/with-canvas-> canvas
(c2d/set-color background-color)
(c2d/rect x y border-width border-heigth)
(c2d/set-font-attributes size style)
(c2d/set-color foreground-color)
(c2d/text text (+ x x-offset) (- y (* text-y 1.5))))))
(defn box-draw
"canvas - clojure2d canvas
text - text displayed inside the input
x - x coordinate of top left corner
y - y coordinate of top left corner
color - vector consisting of [background-color font-color]
min-width - the minimum width"
([args] (apply box-draw args))
([canvas text args]
(let [{:keys [^long x ^long y color ^long min-width]} args
[_ _ border-width border-heigth] (box-coord canvas text {:x x :y y :min-width min-width})
background-color (if (> (count color) 0) (first color) :grey)]
(c2d/with-canvas-> canvas
(c2d/set-color background-color)
(c2d/rect x y border-width border-heigth))
(box-draw-text canvas text args)
[x y border-width border-heigth])))
(defn box-border
([canvas color strength x y w h]
(box-border canvas color strength x y w h true))
([canvas color strength x y w h no-fill]
(when (> strength 0)
(c2d/with-canvas-> canvas
(c2d/set-color color)
(c2d/rect (- x strength) (- y strength) (+ w (* 2 strength)) (+ h (* 2 strength)) no-fill))
(box-border canvas color (- strength 1) x y w h no-fill))))
(defn box-draw-border
([box canvas] (box-draw-border box canvas :black 1))
([box canvas color] (box-draw-border box canvas color 1))
([box canvas color strength] (box-draw-border box canvas color strength false))
([box canvas color strength fill]
(let [[x y w h] (wdg/coord box canvas)]
(box-border canvas color strength x y w h (not fill)))))