-
Notifications
You must be signed in to change notification settings - Fork 0
/
colormap.go
236 lines (208 loc) · 6.09 KB
/
colormap.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) 2019, The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package giv
import (
"reflect"
"github.com/goki/gi/colormap"
"github.com/goki/gi/gi"
"github.com/goki/gi/oswin"
"github.com/goki/gi/oswin/mouse"
"github.com/goki/gi/units"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
"github.com/goki/mat32"
)
// ColorMapName provides a gui chooser of maps in AvailColorMaps
type ColorMapName string
/////////////////////////////////////////////////////////////////////////////
// ColorMapView
// ColorMapView is a widget that displays a ColorMap.
// Note that this is not a ValueView widget
type ColorMapView struct {
gi.WidgetBase
Orient mat32.Dims `desc:"orientation along which to display the spectrum"`
Map *colormap.Map `desc:"the colormap that we view"`
ColorMapSig ki.Signal `json:"-" xml:"-" view:"-" desc:"signal for color map -- triggers when new color map is set via chooser"`
}
var KiT_ColorMapView = kit.Types.AddType(&ColorMapView{}, nil)
// AddNewColorMapView adds a new colorview to given parent node, with given name.
func AddNewColorMapView(parent ki.Ki, name string, cmap *colormap.Map) *ColorMapView {
cv := parent.AddNewChild(KiT_ColorMapView, name).(*ColorMapView)
cv.Map = cmap
return cv
}
func (cv *ColorMapView) Disconnect() {
cv.WidgetBase.Disconnect()
cv.ColorMapSig.DisconnectAll()
}
// SetColorMap sets the color map and triggers a display update
func (cv *ColorMapView) SetColorMap(cmap *colormap.Map) {
cv.Map = cmap
cv.UpdateSig()
}
// SetColorMapAction sets the color map and triggers a display update
// and signals the ColorMapSig signal
func (cv *ColorMapView) SetColorMapAction(cmap *colormap.Map) {
cv.Map = cmap
cv.ColorMapSig.Emit(cv.This(), 0, nil)
cv.UpdateSig()
}
// ChooseColorMap pulls up a chooser to select a color map
func (cv *ColorMapView) ChooseColorMap() {
sl := colormap.AvailMapsList()
cur := ""
if cv.Map != nil {
cur = cv.Map.Name
}
SliceViewSelectDialog(cv.Viewport, &sl, cur, DlgOpts{Title: "Select a ColorMap", Prompt: "choose color map to use from among available list"}, nil,
cv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if sig == int64(gi.DialogAccepted) {
ddlg := send.Embed(gi.KiT_Dialog).(*gi.Dialog)
si := SliceViewSelectDialogValue(ddlg)
if si >= 0 {
nmap, ok := colormap.AvailMaps[sl[si]]
if ok {
cv.SetColorMapAction(nmap)
}
}
}
})
}
// MouseEvent handles button MouseEvent
func (cv *ColorMapView) MouseEvent() {
cv.ConnectEvent(oswin.MouseEvent, gi.RegPri, func(recv, send ki.Ki, sig int64, d interface{}) {
me := d.(*mouse.Event)
cvv := recv.(*ColorMapView)
if me.Button == mouse.Left {
switch me.Action {
case mouse.DoubleClick: // we just count as a regular click
fallthrough
case mouse.Press:
me.SetProcessed()
cvv.ChooseColorMap()
}
}
})
}
func (cv *ColorMapView) ConnectEvents2D() {
cv.MouseEvent()
cv.HoverTooltipEvent()
}
func (cv *ColorMapView) RenderColorMap() {
if cv.Map == nil {
cv.Map = colormap.StdMaps["ColdHot"]
}
rs := cv.Render()
rs.Lock()
pc := &rs.Paint
pos := cv.LayState.Alloc.Pos
sz := cv.LayState.Alloc.Size
pr := pos
sr := sz
sp := pr.Dim(cv.Orient)
lsz := sz.Dim(cv.Orient)
if cv.Map.Indexed {
nc := len(cv.Map.Colors)
inc := mat32.Ceil(lsz / float32(nc))
sr.SetDim(cv.Orient, inc)
for i := 0; i < nc; i++ {
clr := cv.Map.MapIndex(i)
p := float32(i) * inc
pr.SetDim(cv.Orient, sp+p)
pc.FillBoxColor(rs, pr, sr, clr)
}
} else {
inc := mat32.Ceil(lsz / 100)
if inc < 2 {
inc = 2
}
sr.SetDim(cv.Orient, inc)
for p := float32(0); p < lsz; p += inc {
val := p / (lsz - 1)
clr := cv.Map.Map(float64(val))
pr.SetDim(cv.Orient, sp+p)
pc.FillBoxColor(rs, pr, sr, clr)
}
}
rs.Unlock()
}
func (cv *ColorMapView) Render2D() {
if cv.FullReRenderIfNeeded() {
return
}
if cv.PushBounds() {
cv.This().(gi.Node2D).ConnectEvents2D()
cv.RenderColorMap()
cv.Render2DChildren()
cv.PopBounds()
} else {
cv.DisconnectAllEvents(gi.RegPri)
}
}
////////////////////////////////////////////////////////////////////////////////////////
// ColorMapValueView
// ValueView registers ColorMapValueView as the viewer of ColorMapName
func (mn ColorMapName) ValueView() ValueView {
vv := &ColorMapValueView{}
ki.InitNode(vv)
return vv
}
// ColorMapValueView presents an action for displaying a ColorMapName and selecting
// meshes from a ChooserDialog
type ColorMapValueView struct {
ValueViewBase
}
var KiT_ColorMapValueView = kit.Types.AddType(&ColorMapValueView{}, nil)
func (vv *ColorMapValueView) WidgetType() reflect.Type {
vv.WidgetTyp = gi.KiT_Action
return vv.WidgetTyp
}
func (vv *ColorMapValueView) UpdateWidget() {
if vv.Widget == nil {
return
}
ac := vv.Widget.(*gi.Action)
txt := kit.ToString(vv.Value.Interface())
if txt == "" {
txt = "(none, click to select)"
}
ac.SetText(txt)
}
func (vv *ColorMapValueView) ConfigWidget(widg gi.Node2D) {
vv.Widget = widg
vv.StdConfigWidget(widg)
ac := vv.Widget.(*gi.Action)
ac.SetProp("border-radius", units.NewPx(4))
ac.ActionSig.ConnectOnly(vv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
vvv, _ := recv.Embed(KiT_ColorMapValueView).(*ColorMapValueView)
ac := vvv.Widget.(*gi.Action)
vvv.Activate(ac.Viewport, nil, nil)
})
vv.UpdateWidget()
}
func (vv *ColorMapValueView) HasAction() bool {
return true
}
func (vv *ColorMapValueView) Activate(vp *gi.Viewport2D, dlgRecv ki.Ki, dlgFunc ki.RecvFunc) {
if vv.IsInactive() {
return
}
sl := colormap.AvailMapsList()
cur := kit.ToString(vv.Value.Interface())
desc, _ := vv.Tag("desc")
SliceViewSelectDialog(vp, &sl, cur, DlgOpts{Title: "Select a ColorMap", Prompt: desc}, nil,
vv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if sig == int64(gi.DialogAccepted) {
ddlg := send.Embed(gi.KiT_Dialog).(*gi.Dialog)
si := SliceViewSelectDialogValue(ddlg)
if si >= 0 {
vv.SetValue(sl[si])
vv.UpdateWidget()
}
}
if dlgRecv != nil && dlgFunc != nil {
dlgFunc(dlgRecv, send, sig, data)
}
})
}