-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
299 lines (264 loc) · 8.32 KB
/
input.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package gox
import (
"github.com/mohsengreen1388/gox/pkg/codepointtext"
rl "github.com/gen2brain/raylib-go/raylib"
"golang.org/x/exp/slices"
"strings"
)
type Input struct {
X, Y, Width, Hight int32
RectangleBody rl.Rectangle
checkIsActive bool
text string
textSlice []string
mouseX int
mouseY int
pointermoveForPressKey float32
textPostion rl.Vector2
textDirectionSub float32
mesureCounter []float32
lenText int
LimitText int32
Placeholder string
PlaceholderDefulte string
PlaceholderColor rl.Color
selectRecPostion rl.Vector2
selectRecSize rl.Vector2
textWidth int
space int8
selectState bool
countWalke int32
Font rl.Font
FontSize float32
Background rl.Color
TextColor rl.Color
borderColor rl.Color
BorderColorActive rl.Color
BorderColorUnactive rl.Color
app *App
ReadOnly bool
MemberModal bool
}
func (in *Input) Draw(x, y, width, hight int32) {
in.mouseX = int(rl.GetMouseX())
in.mouseY = int(rl.GetMouseY())
in.PlaceholderColor.A = 120
in.body(x, y, width, hight)
}
func (in *Input) body(x, y, width, hight int32) {
recX := float32(x)
recY := float32(y)
recWidth := float32(width)
recHight := float32(hight)
space := float32(15)
in.textPostion = rl.Vector2{float32(recX + float32(space) - in.textDirectionSub), float32(recY + space)}
if recX == 0 {
recX = 5
} // margin right
in.RectangleBody = rl.Rectangle{recX, recY, recWidth, recHight}
rl.DrawRectangleRounded(in.RectangleBody, 0.3, 0, in.Background)
rl.DrawRectangleRoundedLines(in.RectangleBody, 0.3, 0, 3, in.borderColor)
rl.BeginScissorMode(in.RectangleBody.ToInt32().X, in.RectangleBody.ToInt32().Y, in.RectangleBody.ToInt32().Width, in.RectangleBody.ToInt32().Height)
rl.DrawTextEx(in.Font, in.text, in.textPostion, in.FontSize, float32(in.space), in.TextColor)
rl.DrawTextEx(in.Font, in.Placeholder, in.textPostion, in.FontSize, float32(in.space), in.PlaceholderColor)
if in.checkIsActive {
in.textSetInput(recX, recY, recWidth, recHight, space)
in.pointerMove()
in.deleteKey()
// Todo list
//in.SelectText()
}
rl.EndScissorMode()
if !in.ReadOnly {
in.event()
}
}
func (in *Input) event() {
if len(in.textSlice) == 0 {
in.Placeholder = in.PlaceholderDefulte
} else {
in.Placeholder = ""
}
CheckCollisionRecsByMouseIsTrue := rl.CheckCollisionRecs(in.RectangleBody, rl.Rectangle{float32(in.mouseX), float32(in.mouseY), 0, 0})
if CheckCollisionRecsByMouseIsTrue && in.isMemberModal() {
in.movePointerByMouse()
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
in.checkIsActive = true
in.borderColor = in.BorderColorActive
}
}
if !CheckCollisionRecsByMouseIsTrue {
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
in.checkIsActive = false
in.borderColor = in.BorderColorUnactive
rl.SetMouseCursor(rl.MouseCursorArrow)
}
}
}
func (in *Input) textSetInput(recX, recY, recWidth, recHight, space float32) {
rl.SetMouseCursor(rl.MouseCursorIBeam)
key := rl.GetCharPressed()
if key > 20 && in.checkIsActive {
if in.lenText < 0 {
in.textSlice = append(in.textSlice, string(key))
} else {
in.textSlice = slices.Insert(in.textSlice, in.lenText, string(key))
}
measureFontKey := rl.MeasureTextEx(in.Font, string(key), in.FontSize, float32(in.space))
in.pointermoveForPressKey += measureFontKey.X + float32(in.space)
in.mesureCounter = slices.Insert(in.mesureCounter, in.lenText, measureFontKey.X+float32(in.space))
in.lenText++
in.text = strings.Join(in.textSlice, "")
// Todo list
// in.textencode()
if int(in.pointermoveForPressKey) > int(in.RectangleBody.Width/1.1) {
in.textDirectionSub -= -(measureFontKey.X + float32(in.space))
}
}
rl.DrawLineEx(rl.Vector2{in.textPostion.X + float32(in.pointermoveForPressKey), recY + 12}, rl.Vector2{in.textPostion.X + float32(in.pointermoveForPressKey), recY + 40}, float32(in.space), rl.Black)
}
func (in *Input) pointerMove() {
keyForMove := rl.GetKeyPressed()
if keyForMove == rl.KeyRight || keyForMove == rl.KeyLeft {
if keyForMove == rl.KeyRight {
if in.lenText < len(in.textSlice) {
in.pointermoveForPressKey += in.mesureCounter[in.lenText]
if in.RectangleBody.Width <= in.pointermoveForPressKey {
in.textDirectionSub += in.mesureCounter[in.lenText]
}
in.lenText++
}
}
if keyForMove == rl.KeyLeft {
if in.lenText > 0 {
in.lenText--
in.pointermoveForPressKey -= in.mesureCounter[in.lenText]
if in.textDirectionSub > 0 {
in.textDirectionSub -= in.mesureCounter[in.lenText]
}
}
}
}
}
// Todo list
func (in *Input) flash() {}
func (in *Input) movePointerByMouse() {
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
if !in.selectState {
in.selectState = true
} else {
in.selectState = false
}
in.textWidth = 0
for _, value := range in.mesureCounter {
in.textWidth += int(value)
}
if in.mesureCounter != nil && len(in.textSlice) != 0 {
coordinatesRec := int(in.mouseX) - int(in.textPostion.X)
postionPerWord := coordinatesRec / (in.textWidth / len(in.textSlice))
if postionPerWord > len(in.textSlice) {
postionPerWord = len(in.textSlice)
}
if postionPerWord >= 0 {
in.lenText = postionPerWord
wholeTextThatSelectWidth := 0.0
for _, vulue := range in.mesureCounter[0:postionPerWord] {
wholeTextThatSelectWidth += float64(vulue)
}
in.pointermoveForPressKey = float32(wholeTextThatSelectWidth)
}
}
}
}
func (in *Input) deleteKey() {
if rl.IsKeyPressed(rl.KeyBackspace) {
if in.lenText > 0 {
in.lenText--
if len(in.mesureCounter) >= 0 {
in.pointermoveForPressKey -= in.mesureCounter[in.lenText]
}
sliceTextRemove := slices.Delete(in.textSlice, in.lenText, in.lenText+int(1))
in.textSlice = sliceTextRemove
sliceMesurRemove := slices.Delete(in.mesureCounter, in.lenText, in.lenText+int(1))
in.mesureCounter = sliceMesurRemove
in.text = strings.Join(in.textSlice, "")
}
}
}
// Todo list
func (in *Input) selectText() {
in.selectRecPostion.Y = 22
in.selectRecSize.Y = 28
in.selectRecPostion.X = float32(in.pointermoveForPressKey + in.textPostion.X)
mouseDirection := rl.GetMouseDelta()
rl.DrawRectangleV(in.selectRecPostion, rl.Vector2{in.selectRecSize.X, in.selectRecSize.Y}, in.PlaceholderColor)
if rl.IsMouseButtonDown(rl.MouseLeftButton) {
if in.selectState {
in.selectRecPostion.X = 0
in.selectRecSize.X = 0
return
}
if (in.textPostion.X + float32(in.textWidth)) >= float32(in.mouseX) {
if mouseDirection.X >= 0 && in.selectRecSize.X <= float32(in.textWidth) {
if in.lenText <= len(in.mesureCounter) {
in.selectRecSize.X = in.mesureCounter[in.lenText]
in.selectRecPostion.X += 1
}
}
}
if mouseDirection.X <= 0 {
}
}
}
func (in *Input) SetPosition(x, y int32) {
in.X = x
in.Y = y
}
func (in *Input) Coord() rl.Rectangle {
return rl.NewRectangle(float32(in.X), float32(in.Y), float32(in.Width), float32(in.Hight))
}
func (in *Input) DrawInterface(x, y, width, hight int32) {
in.mouseX = int(rl.GetMouseX())
in.mouseY = int(rl.GetMouseY())
in.PlaceholderColor.A = 120
in.body(x, y, width, hight)
}
func (in *Input) isMemberModal() bool {
if in.MemberModal {
return true
}
return !in.app.Lock
}
func (in *Input) EnableMemberModal() {
in.MemberModal = true
}
func (in *Input) DisableMemberModal() {
in.MemberModal = false
}
func (in *Input) SetText(text string) {
in.clearText()
for _, charKey := range text {
if in.lenText < 0 {
in.textSlice = append(in.textSlice, string(charKey))
} else {
in.textSlice = slices.Insert(in.textSlice, in.lenText, string(charKey))
}
measureFontKey := rl.MeasureTextEx(in.Font, string(charKey), in.FontSize, float32(in.space))
in.pointermoveForPressKey += measureFontKey.X + float32(in.space)
in.mesureCounter = slices.Insert(in.mesureCounter, in.lenText, measureFontKey.X+float32(in.space))
in.lenText++
in.text = strings.Join(in.textSlice, "")
}
}
func (in *Input) clearText() {
in.textSlice = nil
in.text = ""
in.lenText = 0
}
func (in *Input) GetText() string {
return in.text
}
// Todo list
func (in *Input) textencode() {
in.text = in.app.CodePoint.Genrate(in.text, codepointtext.LeftToRight)
}