-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgiuplus.go
180 lines (159 loc) · 4.54 KB
/
giuplus.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
package giuplus
import (
"strings"
g "github.com/AllenDang/giu"
"github.com/AllenDang/imgui-go"
)
type TextEditor struct {
multiline bool
wordwrap bool
autoSelect bool
width float32
height float32
text string
selStart int
selEnd int
onActivate func(e *TextEditor)
}
// NewTextEditor creates a new text editor with given height and width. The onActivate callback
// is called when the editor is activated.
func NewTextEditor(width, height float32, onActivate func(e *TextEditor)) *TextEditor {
return &TextEditor{
multiline: false,
wordwrap: false,
width: width,
height: height,
autoSelect: true,
onActivate: onActivate,
}
}
// NewTextEditorMultiline creates a new multiline text editor with given width and height.
// Word-wrapping is ON by default.
func NewTextEditorMultiline(width, height float32) *TextEditor {
return &TextEditor{
multiline: true,
wordwrap: true,
width: width,
height: height,
autoSelect: true,
}
}
// SetOnActivate sets the onActivate callback which is called when the editor is activated.
func (e *TextEditor) SetOnActivate(callback func(e *TextEditor)) {
e.onActivate = callback
}
// Widget returns the undlerying GUI widget, which is either a InputText or InputTextMultiline.
func (e *TextEditor) Widget() g.Widget {
if e.multiline {
widget := g.InputTextMultiline(&e.text).Size(-1, 100).
Flags(imgui.InputTextFlagsCallbackAlways | imgui.InputTextFlagsCallbackCharFilter)
cbwidget := func(data imgui.InputTextCallbackData) int32 {
return WrapInputtextMultiline(e, data)
}
fullwidget := widget.Callback(cbwidget)
return fullwidget
}
widget := g.InputText(&e.text).Size(-1).
Callback(func(data imgui.InputTextCallbackData) int32 {
switch data.EventFlag() {
case imgui.InputTextFlagsCallbackAlways:
e.selStart = data.SelectionStart()
e.selEnd = data.SelectionEnd()
e.onActivate(e)
}
return 0
})
return widget
}
// Build builds the widget's layout. This is to satisfy Giu's custom widget interface.
func (e *TextEditor) Build() {
e.Widget().Build()
}
// SetText sets the editor text to the given UTF-8 string.
func (e *TextEditor) SetText(s string) {
e.text = s
}
// Text returns the text of the editor.
func (e *TextEditor) Text() string {
return e.text
}
// SetAutoSelect sets the automatic selection on focus feature.
func (e *TextEditor) SetAutoSelect(on bool) {
e.autoSelect = on
}
// AutoSelect returns true if text is automatically selected on focus, false otherwise.
func (e *TextEditor) AutoSelect() bool {
return e.autoSelect
}
// SetWordwrap sets the automatic word wrapping on or off.
func (e *TextEditor) SetWordwrap(on bool) {
e.wordwrap = on
}
// Wordwrap returns true if the automatic word wrapping is on, false otherwise.
func (e *TextEditor) Wordrap() bool {
return e.wordwrap
}
// Size returns the width and height of the editor. Widths and heights of -1 indicate maximum stretch.
func (e *TextEditor) Size() (float32, float32) {
return e.width, e.height
}
// SetSize sets the editor width and height. Widths and heights of -1 indicate maximum stretch.
func (e *TextEditor) SetSize(width, height float32) {
e.width = width
e.height = height
}
// WrapInputTextMultiline is a callback to wrap an input text multiline.
func WrapInputtextMultiline(e *TextEditor, data imgui.InputTextCallbackData) int32 {
switch data.EventFlag() {
case imgui.InputTextFlagsCallbackCharFilter:
c := data.EventChar()
if c == '\n' {
data.SetEventChar('\u07FF') // pivot character 2-bytes in UTF-8
}
case imgui.InputTextFlagsCallbackAlways:
// 0. turn every pivot byte sequence into \r\n
buff := data.Buffer()
buff2 := []byte(strings.ReplaceAll(string(buff), "\u07FF", "\r\n"))
for i := range buff {
buff[i] = buff2[i]
}
data.MarkBufferModified()
// 1. zap all newlines that are not preceeded by a CR (which was manually entered like above)
cr := false
for i, c := range buff {
if c == 10 && !cr {
buff[i] = 32
data.MarkBufferModified()
} else {
if c == 13 {
cr = true
} else {
cr = false
}
}
}
// 2. word break the whole buffer with the standard greedy algorithm
nl := 0
spc := 0
w := g.GetWidgetWidth(e.Widget())
for i, c := range buff {
if c == 10 {
nl = i
}
if c == 32 {
spc = i
}
if TextWidth(string(buff[nl:i])) > w && spc > 0 {
buff[spc] = 10
data.MarkBufferModified()
}
}
}
return 0
}
// TextWidth returns the width of the given text.
func TextWidth(s string) float32 {
w, _ := g.CalcTextSize(s)
return w
}
var _ g.Widget = &TextEditor{}