-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:overtone/overtone
- Loading branch information
Showing
7 changed files
with
301 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
(ns overtone.gui.adjustment-popup | ||
(:use [seesaw core color font]) | ||
(:require [seesaw.bind :as bind]) | ||
(:import javax.swing.JWindow)) | ||
|
||
(def ^{:private true} OFFSET 12) | ||
(def ^{:private true} BORDER 8) | ||
|
||
(def ^{:private true} label-font (font :name "Arial" :size 14 :style :bold)) | ||
(def ^{:private true} value-font (font :name "Arial" :size 14 :style :bold)) | ||
|
||
(defn adjustment-popup-for | ||
"Create a temporary popup window showing a changing value for a bindable widget. | ||
Example: | ||
(adjustment-popup-for widget \"Volume:\") | ||
" | ||
[widget popup-label] | ||
(invoke-now | ||
(let [popup (JWindow.) | ||
txt-label (label :id :popup-label :text popup-label :font label-font) | ||
val-label (label :id :popup-value :text (value widget) :font value-font | ||
:foreground (color 0 140 236)) | ||
body (border-panel :border BORDER | ||
:north txt-label | ||
:center (flow-panel :align :center :items [val-label]))] | ||
(doto popup | ||
(.setBackground (color :black)) | ||
(.add body) | ||
(.pack)) | ||
(bind/bind widget val-label) | ||
(listen widget | ||
:mouse-pressed (fn [ev] | ||
(let [{:keys [x y]} (bean (.getLocationOnScreen widget)) | ||
w (.getWidth widget) | ||
h (.getHeight widget) | ||
popup-y (- (+ y (/ h 2)) (/ (.getHeight popup) 2)) | ||
popup-x (+ x w OFFSET)] | ||
(move! popup :to [popup-x popup-y]) | ||
(show! popup))) | ||
:mouse-released (fn [_] | ||
(hide! popup))) | ||
(bind/bind widget (bind/b-do [_] (repaint! widget))) | ||
popup))) | ||
|
||
|
||
(comment | ||
|
||
(require '[seesaw.core :as saw]) | ||
(use 'overtone.gui.adjustment-popup) | ||
(use 'overtone.gui.dial) | ||
(def s (saw/slider :value 50 :min 0 :max 100 :orientation :vertical)) | ||
(def s-adj (adjustment-popup-for s "Value:")) | ||
|
||
(def d (dial :value 50 :min 0 :max 100)) | ||
(def d-adj (adjustment-popup-for d "Pan:")) | ||
(def f (saw/frame :title "testing" :minimum-size [200 :by 400] | ||
:content (saw/vertical-panel :items [s d]))) | ||
(saw/show! f) | ||
|
||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
(ns overtone.gui.spinner-label | ||
(:use [seesaw.core] | ||
[seesaw.behave :only [when-mouse-dragged]] | ||
[seesaw.meta :only [get-meta put-meta!]] | ||
[seesaw.options :only [apply-options option-map default-option]] | ||
[seesaw.keymap :only [map-key]] | ||
[seesaw.widget-options :only [WidgetOptionProvider]] | ||
[seesaw.value :only [Value value* value!*]] | ||
[seesaw.selection :only [Selection]]) | ||
(:require [seesaw.bind :as bind])) | ||
|
||
(defn spinner-label-proxy [] | ||
(proxy [javax.swing.JLabel] [])) | ||
|
||
(def ^{:private true} SpinnerLabelClass (class (spinner-label-proxy))) | ||
|
||
(defn- make-state [this] | ||
{ :model (atom nil) | ||
:unbind (atom (fn [])) | ||
:update (fn [v this] | ||
(.setText | ||
this | ||
(if (number? v) | ||
(format "%.2f" v) ; TODO make format configurable | ||
(str v))))}) | ||
|
||
(defn- get-model [this] @(:model (get-meta this ::state))) | ||
(defn- set-model [this m] | ||
(let [{:keys [model unbind update]} (get-meta this ::state)] | ||
(@unbind) | ||
(reset! model m) | ||
(reset! unbind (bind/bind m (bind/b-do* update this))) | ||
(update (.getValue m) this))) | ||
|
||
(defn spinner-label | ||
"Same API as #'seesaw.core/spinner, but only a label is displayed and the | ||
current value is modified by dragging the mouse up and down over it." | ||
[& opts] | ||
(let [widget (spinner-label-proxy) | ||
state (make-state widget)] | ||
(put-meta! widget ::state state) | ||
(set-model widget (spinner-model 0.0)) | ||
(when-mouse-dragged | ||
widget | ||
:start (fn [_] | ||
(config! widget :background :lightyellow)) | ||
:drag (fn [e [dx dy]] | ||
(let [m @(:model state) | ||
next (.getNextValue m) | ||
prev (.getPreviousValue m)] | ||
(cond | ||
(and next (neg? dy)) (.setValue m next) | ||
(and prev (pos? dy)) (.setValue m prev)))) | ||
:finish (fn [_] | ||
(-> widget | ||
(config! :opaque? false) | ||
repaint!))) | ||
(apply-options | ||
widget | ||
(concat | ||
[:cursor :n-resize | ||
:tip "Drag to adjust"] | ||
opts)) | ||
widget)) | ||
|
||
(def spinner-label-options | ||
(merge | ||
label-options | ||
(option-map | ||
(default-option :model | ||
set-model | ||
get-model | ||
"A spinner-model")))) | ||
|
||
(extend-type (do SpinnerLabelClass) | ||
WidgetOptionProvider | ||
(get-widget-option-map* [this] [spinner-label-options]) | ||
(get-layout-option-map* [this] nil) | ||
|
||
Value | ||
(container?* [this] false) | ||
(value* [this] (.getValue (get-model this))) | ||
(value!* [this v] (.setValue (get-model this) v)) | ||
|
||
Selection | ||
(get-selection [this] [(value this)]) | ||
(set-selection [this [v]] (value! this v)) | ||
|
||
bind/ToBindable | ||
(to-bindable* [this] (config this :model))) | ||
|
||
(comment | ||
(use 'overtone.gui.spinner-label | ||
'seesaw.core | ||
'seesaw.border | ||
'seesaw.dev) | ||
(require '[seesaw.bind :as bind]) | ||
(def sl (spinner-label | ||
:id :my-spin-label | ||
:halign :center | ||
:border [(line-border :color :darkgrey :thickness 1)] | ||
:model (spinner-model 10.0 :from 0.0 :to 100.0 :by 0.5))) | ||
(selection! sl 99.5) | ||
(bind/bind sl (bind/b-do [v] (println "new value " (value sl) ", " (selection sl)))) | ||
(-> (frame :content sl) pack! show!) | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.