-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
1140 lines (1037 loc) · 32.1 KB
/
main.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
/*
* PhotonText very alpha (codename anode) by plasticgaming99
* (c)opyright plasticgaming99, 2023-2024
*/
import (
"bytes"
"fmt"
"image"
"image/color"
"log"
"math"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"golang.org/x/text/language"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text"
textv2 "github.com/hajimehoshi/ebiten/v2/text/v2"
"github.com/plasticgaming99/photon/assets/phfonts"
"github.com/plasticgaming99/photon/assets/phicons"
"github.com/plasticgaming99/photon/modules/dyntypes"
"github.com/plasticgaming99/photon/modules/plastk"
"github.com/hugolgst/rich-go/client"
"golang.design/x/clipboard"
)
/* basic */
var (
screenWidth = 640
screenHeight = 480
mplusNormalFont font.Face
mplusBigFont font.Face
mplusSmallFont font.Face
HackGenFont font.Face
smallHackGenFont font.Face
smallHackGenV2 *textv2.GoTextFaceSource
photontext = []string{}
undohist = []undoPreserver{}
photoncmd = string("")
cmdresult = string("")
clearresult = int(0)
rellines = int(0)
textrepeatness = int(0)
cursornowx = int(1)
cursornowy = int(1)
cursorxeffort = int(0)
closewindow = false
clickrepeated = false
modalmode = false
returncode = string("\n")
returntype = string("")
// options
hanzenlock = true
hanzenlockstat = false
limitterenabled = true
limitterlevel = int(3)
dbgmode = false
editmode = int(1)
editorforcused = true
commandlineforcused = false
editingfile = string("")
// Textures
sideBar *ebiten.Image
infoBar *ebiten.Image
commandLine *ebiten.Image
cursorimg = ebiten.NewImage(2, 15)
topopbar *ebiten.Image
filesmenubutton = ebiten.NewImage(80, 20)
linessep *ebiten.Image
scrollbar *ebiten.Image
scrollbit *ebiten.Image
// Texture options
sideBarop = &ebiten.DrawImageOptions{}
)
/* Texture options */
var (
topopBarSize = 20
infoBarSize = 20
commandlineSize = 20
scrollbarwidth = 18
)
var (
synctps = &sync.WaitGroup{}
synctps2 = &sync.WaitGroup{}
)
type undoPreserver struct {
textlet string
cursorx int
cursory int
}
func saveUndoAppend(in string) undoPreserver {
toappend := undoPreserver{
textlet: in,
cursorx: cursornowx - 1,
cursory: cursornowy - 1,
}
return toappend
}
func repeatingKeyPressed(key ebiten.Key) bool {
var (
delay = ebiten.TPS() / 2
interval = ebiten.TPS() / 18
)
d := inpututil.KeyPressDuration(key)
if d == 1 {
return true
}
if d >= delay && (d-delay)%interval == 0 {
return true
}
return false
}
func checkMixedKanjiLength(kantext string, length int) (int, int, int) {
kantext = string([]rune(kantext)[0 : length-1])
kanji := (len(kantext) - len([]rune(kantext))) / 2
nonkanji := len([]rune(kantext)) - kanji
tab := strings.Count(kantext, " ")
return nonkanji, kanji - tab, tab
}
// renew image. if same size, return same image
func renewimg(image *ebiten.Image, targetwidth int, targetheight int, targetcolor color.Color) *ebiten.Image {
widthnow := image.Bounds().Dx()
heightnow := image.Bounds().Dy()
if widthnow != targetwidth || heightnow != targetheight {
newimage := ebiten.NewImage(targetwidth, targetheight)
newimage.Fill(targetcolor)
return newimage
}
return image
}
// func
func init() {
const dpi = 144
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
var iconphoton []image.Image
ebiten.SetVsyncEnabled(true)
ebiten.SetScreenClearedEveryFrame(false)
iconphotonreader := bytes.NewReader(phicons.PhotonIcon16)
_, iconphoton16, err := ebitenutil.NewImageFromReader(iconphotonreader)
phloginfo(err)
iconphotonreader = bytes.NewReader(phicons.PhotonIcon32)
_, iconphoton32, err := ebitenutil.NewImageFromReader(iconphotonreader)
phloginfo(err)
iconphotonreader = bytes.NewReader(phicons.PhotonIcon48)
_, iconphoton48, err := ebitenutil.NewImageFromReader(iconphotonreader)
phloginfo(err)
iconphotonreader = bytes.NewReader(phicons.PhotonIcon128)
_, iconphoton128, err := ebitenutil.NewImageFromReader(iconphotonreader)
phloginfo(err)
iconphoton = append(iconphoton, iconphoton16, iconphoton32, iconphoton48, iconphoton128)
ebiten.SetWindowIcon(iconphoton)
/*100, 250, 500, 750, 1000 or your monitor's refresh rate*/
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
err = clipboard.Init()
if err != nil {
fmt.Println("**WARN** Clipboard is disabled.", err)
}
wg.Done()
}()
wg.Add(1)
go func() {
tt, err := opentype.Parse(phfonts.MPlus1pRegular_ttf)
if err != nil {
log.Fatal(err)
}
mplusNormalFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 12,
DPI: dpi,
Hinting: font.HintingVertical,
})
if err != nil {
log.Fatal(err)
}
mplusSmallFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 8,
DPI: dpi,
Hinting: font.HintingVertical,
})
if err != nil {
log.Fatal(err)
}
mplusBigFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 24,
DPI: dpi,
Hinting: font.HintingFull, // Use quantization to save glyph cache images.
})
if err != nil {
log.Fatal(err)
}
// Adjust the line height.
mplusBigFont = text.FaceWithLineHeight(mplusBigFont, 54)
wg.Done()
}()
wg.Add(1)
go func() {
tt, err := opentype.Parse(phfonts.HackGenRegular_ttf)
if err != nil {
log.Fatal(err)
}
HackGenFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 12,
DPI: dpi,
Hinting: font.HintingFull,
})
if err != nil {
log.Fatal(err)
}
smallHackGenFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 8,
DPI: dpi,
Hinting: font.HintingFull,
})
smallHackGenV2, err = textv2.NewGoTextFaceSource(bytes.NewReader(phfonts.HackGenRegular_ttf))
if err != nil {
log.Fatal(err)
}
wg.Done()
}()
// load file
wg.Add(1)
go func() {
if len(os.Args) >= 2 {
phload(os.Args[1])
}
wg.Done()
}()
wg.Wait()
// after loaded text to memory, if photontext
// has not any strings, init photontext with
// 1-line, 0-column text.
wg.Add(1)
go func() {
if len(photontext) == 0 {
photontext = append(photontext, "")
}
wg.Done()
}()
wg.Wait()
// Execute PhotonRC when its avaliable
{
home, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
}
phrcpath := home + "/.photonrc"
if err != nil {
panic(err)
}
_, err = os.Stat(phrcpath)
if err == nil {
fmt.Println("Using PhotonRC")
photonRC, err := sliceload(phrcpath)
if err != nil {
fmt.Println("PhotonRC initializing failed. Using default")
}
for i := 0; i < len(photonRC); i++ {
proceedcmd(photonRC[i])
}
photonRC = nil
}
}
{
// After executed PhotonRC, Initialize textures
sideBar = ebiten.NewImage(60, 100)
infoBar = ebiten.NewImage(100, infoBarSize)
commandLine = ebiten.NewImage(100, commandlineSize)
cursorimg = ebiten.NewImage(2, 15)
topopbar = ebiten.NewImage(4100, topopBarSize)
filesmenubutton = ebiten.NewImage(80, 20)
linessep = ebiten.NewImage(2, 100)
scrollbar = ebiten.NewImage(scrollbarwidth, 100)
scrollbit = ebiten.NewImage(scrollbarwidth, 100)
}
// Fill textures
{
/* init sidebar image. */
sideBar.Fill(color.RGBA{57, 57, 57, 255})
/* init information bar image */
infoBar.Fill(color.RGBA{87, 97, 87, 255})
/* init commandline image */
commandLine.Fill(color.RGBA{39, 39, 39, 255})
/* init cursor image */
cursorimg.Fill(color.RGBA{255, 255, 255, 5})
/* init top-op-bar image */
topopbar.Fill(color.RGBA{100, 100, 100, 255})
/* init top-op-bar "files" button */
filesmenubutton.Fill(color.RGBA{110, 110, 110, 255})
/* init line-bar separator */
linessep.Fill(color.RGBA{100, 100, 100, 255})
/* init scroll-bar */
scrollbar.Fill(color.RGBA{80, 80, 80, 255})
/* init scroll-bit */
scrollbit.Fill(color.RGBA{30, 30, 30, 255})
// Init texture options
sideBarop.GeoM.Translate(float64(0), float64(20))
}
}
type Editor struct {
/*counter int
kanjiText string
kanjiTextColor color.RGBA*/
rune2Input []rune
}
func checkcurx(line int) {
if len([]rune(photontext[line-1])) < cursornowx {
if photontext[line-1] == "" {
cursornowx = 1
} else {
cursornowx = len([]rune(photontext[line-1])) + 1
}
}
}
func (g *Editor) Update() error {
synctps.Add(1)
if plastk.MenuBarDetectClickedByID("fileswritebutton") {
proceedcmd("write")
}
if plastk.MenuBarDetectClickedByID("filesexitbutton") {
proceedcmd("q")
}
if plastk.MenuBarDetectClickedByID("editredobutton") {
proceedcmd("q")
}
// Update Text-info
// photonlines = len(photontext)
/*\
* detect cursor key actions
\*/
// Insert text
if editorforcused && !(ebiten.IsKeyPressed(ebiten.KeyControl)) {
g.rune2Input = ebiten.AppendInputChars(g.rune2Input[:0])
// Detect left side
if cursornowx == 1 {
photontext[cursornowy-1] = string(g.rune2Input) + photontext[cursornowy-1]
} else
// Detect right side
if cursornowx-1 == len([]rune(photontext[cursornowy-1])) {
photontext[cursornowy-1] = photontext[cursornowy-1] + string(g.rune2Input)
} else
// Other, Insert
{
photontext[cursornowy-1] = string([]rune(photontext[cursornowy-1])[:cursornowx-1]) + string(g.rune2Input) + string([]rune(photontext[cursornowy-1])[cursornowx-1:])
}
// Move cursornowx. with cjk support yay!
cursornowx += len(g.rune2Input)
}
if editorforcused {
// Check commandline is called
if (ebiten.IsKeyPressed(ebiten.KeyControl)) && (ebiten.IsKeyPressed(ebiten.KeyShift)) && (ebiten.IsKeyPressed(ebiten.KeyC)) {
editorforcused = false
commandlineforcused = true
} else
// Check upper text.
if (repeatingKeyPressed(ebiten.KeyUp)) && (cursornowy > 1) {
checkcurx(cursornowy - 1)
cursornowy--
} else
// Check lower text.
if (repeatingKeyPressed(ebiten.KeyDown)) && (cursornowy < len(photontext)) {
checkcurx(cursornowy + 1)
cursornowy++
} else if (repeatingKeyPressed(ebiten.KeyLeft)) && (cursornowx > 1) {
cursornowx--
} else if (repeatingKeyPressed(ebiten.KeyRight)) && (cursornowx <= len([]rune(photontext[cursornowy-1]))) {
cursornowx++
} else if (ebiten.IsKeyPressed(ebiten.KeyControl)) && (repeatingKeyPressed(ebiten.KeyC)) {
fmt.Println("c pressed")
} else if (repeatingKeyPressed(ebiten.KeyBackquote)) && (hanzenlock) {
if !hanzenlockstat {
hanzenlockstat = true
} else {
hanzenlockstat = false
}
} else if repeatingKeyPressed(ebiten.KeyHome) {
cursornowx = 1
} else if repeatingKeyPressed(ebiten.KeyEnd) {
cursornowx = len([]rune(photontext[cursornowy-1])) + 1
} else if ebiten.IsKeyPressed(ebiten.KeyControl) && repeatingKeyPressed(ebiten.KeyV) {
testslice := strings.Split(string(clipboard.Read(clipboard.FmtText)), "\n")
firsttext := string([]rune(photontext[cursornowy-1])[:cursornowx-1])
lasttext := string([]rune(photontext[cursornowy-1])[cursornowx-1:])
//{photontext[cursornowy-1] = string(g.rune2Input)}
fmt.Println(testslice)
if len(testslice) == 1 {
photontext[cursornowy-1] = firsttext + testslice[0] + lasttext
cursornowx = len([]rune(firsttext + testslice[0]))
fmt.Println("one")
} else {
for i := 0; i < len(testslice); i++ {
if i == 0 {
photontext[cursornowy-1] = firsttext + testslice[i]
} else {
{
photontext = append(photontext[:cursornowy], append([]string{testslice[i]}, photontext[cursornowy:]...)...)
/*photontext[cursornowy] = string([]rune(photontext[cursornowy-1])[cursornowx-1:])
photontext[cursornowy-1] = string([]rune(photontext[cursornowy-1])[:cursornowx-1])*/
cursornowy++
}
if i == len(testslice)-1 {
cursornowx = len([]rune(testslice[i])) + 1
}
}
fmt.Println(i)
}
}
} else if repeatingKeyPressed(ebiten.KeyTab) {
/*photontext[cursornowy-1] = photontext[cursornowy-1] + string(g.rune2Input) (legacy impl) */
// Detect text input
// Detect left side
if cursornowx == 1 {
photontext[cursornowy-1] = string(" ") + photontext[cursornowy-1]
} else
// Detect right side
if cursornowx-1 == len([]rune(photontext[cursornowy-1])) {
photontext[cursornowy-1] = photontext[cursornowy-1] + string(" ")
} else
// Other, Insert
{
photontext[cursornowy-1] = string([]rune(photontext[cursornowy-1])[:cursornowx-1]) + string(" ") + string([]rune(photontext[cursornowy-1])[cursornowx-1:])
}
// Move cursornowx. with cjk support yay!
cursornowx += len(" ")
} else
// New line
if (repeatingKeyPressed(ebiten.KeyEnter) || repeatingKeyPressed(ebiten.KeyNumpadEnter)) && !hanzenlockstat {
{
photontext = append(photontext[:cursornowy], append([]string{""}, photontext[cursornowy:]...)...)
photontext[cursornowy] = string([]rune(photontext[cursornowy-1])[cursornowx-1:])
photontext[cursornowy-1] = string([]rune(photontext[cursornowy-1])[:cursornowx-1])
cursornowy++
cursornowx = 1
}
cursornowx = 1
} else
// Line deletion.
if repeatingKeyPressed(ebiten.KeyBackspace) && !((len(photontext[0]) == 0) && (cursornowy == 1)) && !hanzenlockstat {
if (photontext[cursornowy-1] == "") && (len(photontext) != 1) {
cursornowx = len([]rune(photontext[cursornowy-2])) + 1
if cursornowy-1 < len(photontext)-1 {
copy(photontext[cursornowy-1:], photontext[cursornowy:])
}
photontext[len(photontext)-1] = ""
photontext = photontext[:len(photontext)-1]
cursornowx = len([]rune(photontext[cursornowy-2])) + 1
cursornowy--
} else {
if !((cursornowx == 1) && (cursornowy == 1)) || (cursornowx-1 == len([]rune(photontext[cursornowy-1]))) {
if cursornowx == 1 {
cursornowx = len([]rune(photontext[cursornowy-2])) + 1
photontext[cursornowy-2] = photontext[cursornowy-2] + photontext[cursornowy-1]
if cursornowy-1 < len(photontext)-1 {
copy(photontext[cursornowy-1:], photontext[cursornowy:])
}
photontext[len(photontext)-1] = ""
photontext = photontext[:len(photontext)-1]
cursornowy--
} else
// normal deletion
if cursornowx-1 == len([]rune(photontext[cursornowy-1])) {
// convert 2 rune
runes := []rune(photontext[cursornowy-1])
//save last char
////undohist = append(undohist, saveUndoAppend(string(runes[len(runes)-1:])))
////fmt.Println(undohist)
// delete last char
runes = runes[:len(runes)-1]
// convert rune 2 string and insert
photontext[cursornowy-1] = string(runes)
// Move to left
cursornowx--
} else {
// Convert to rune
runes := []rune(photontext[cursornowy-1])[:cursornowx-1]
//save last char
////undohist = append(undohist, saveUndoAppend(string(runes[len(runes)-1:])))
// Delete last
runes = runes[:len(runes)-1]
// Convert to string and insert
photontext[cursornowy-1] = string(runes) + string([]rune(photontext[cursornowy-1])[cursornowx-1:])
// Move to left
cursornowx--
}
}
}
}
} else
// If command-line is forcused
if commandlineforcused {
if ebiten.IsKeyPressed(ebiten.KeyEnter) {
cmdresult = proceedcmd(photoncmd)
clearresult += 10
photoncmd = ""
editorforcused = true
commandlineforcused = false
}
if (len([]rune(photoncmd)) >= 1) && (repeatingKeyPressed(ebiten.KeyBackspace)) {
cmdrune := []rune(photoncmd)[:len([]rune(photoncmd))-1]
photoncmd = string(cmdrune)
} else {
// detect text input
g.rune2Input = ebiten.AppendInputChars(g.rune2Input[:0])
// insert text
if string(g.rune2Input) != "" {
photoncmd += string(g.rune2Input)
}
}
}
/*\
* detect mouse wheel actions.
\*/
_, dy := ebiten.Wheel()
if (dy > 0) && (rellines > 0) {
rellines -= 3
} else if (dy < 0) && (rellines+3 < len(photontext)) {
rellines += 3
}
/*\
* Detect touch on buttons.
\*/
/*mousex, mousey := ebiten.CursorPosition()
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
clickrepeated = true
}
if clickrepeated && !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
if mousex < 80 && mousey < 20 {
fmt.Println("test!!!")
}
clickrepeated = false
}*/
/*\
* Detect cursor's position, and changes cursor shape
\*/
curx, cury := ebiten.CursorPosition()
windowx, windowy := ebiten.WindowSize()
if ((60 < curx) && (curx < windowx)) && ((20 < cury) && (cury < windowy-20)) {
ebiten.SetCursorShape(ebiten.CursorShapeText)
} else {
ebiten.SetCursorShape(ebiten.CursorShapeDefault)
}
if repeatingKeyPressed(ebiten.KeyA) {
fmt.Println("a pressed")
}
if closewindow {
return fmt.Errorf("close")
}
synctps.Done()
return nil
}
var (
prevcurx = int(0)
prevcury = int(0)
prevrell = int(0)
phburst = int(0)
)
var (
textx int
cursorxstart int
)
func (g *Editor) Draw(screen *ebiten.Image) {
synctps.Wait()
hackgen4info := &textv2.GoTextFace{
Source: smallHackGenV2,
Size: 16,
Language: language.Japanese,
}
if (prevcurx == cursornowx && prevcury == cursornowy && prevrell == rellines) && limitterenabled {
time.Sleep(time.Duration(phburst) * time.Millisecond)
if phburst < limitterlevel {
phburst += 1
}
} else {
if 0 <= phburst {
phburst = 0
}
}
prevcurx, prevcury, prevrell = cursornowx, cursornowy, rellines
screenWidth, screenHeight := ebiten.WindowSize()
screenHeight -= topopBarSize
if commandlineforcused || cmdresult != "" {
screenHeight -= 20
}
// Init screen
screen.Fill(color.RGBA{61, 61, 61, 255})
sideBar = renewimg(sideBar, 60, screenWidth, color.RGBA{57, 57, 57, 255})
screen.DrawImage(sideBar, sideBarop)
// Draw left information text
leftinfotxt := ""
leftinfotxt = "PhotonText alpha "
if hanzenlockstat {
leftinfotxt += "Hanzenlock "
}
// Draw right information text
rightinfotext := " " + returntype + " " + strconv.Itoa(cursornowy) + ":" + strconv.Itoa(cursornowx)
// draw editor text
Maxtext := int(math.Ceil(((float64(screenHeight) - 20) / 18)) - 1)
if int(Maxtext) >= len(photontext) {
textrepeatness = len(photontext) - 1
} else {
textrepeatness = int(Maxtext) - 1
}
// start line loop
for printext := 0; printext < len(photontext[rellines:]); {
if printext > int(Maxtext) || (len(photontext)-rellines) == 0 {
break
}
//slicedtext := []rune(photontext[printext+rellines])
textx = 55
text.Draw(screen, strconv.Itoa(printext+rellines+1), smallHackGenFont, textx+10, ((printext + 2) * 18), color.White)
textx = textx + (9 * len(strconv.Itoa(int(Maxtext)+rellines)))
textx += 20
cursorxstart = textx + 0
spaceSplit := strings.Split(photontext[printext+rellines], " ")
var samplue []textv2.Glyph
//textv2.Draw(screen, photontext[printext+rellines], hackgen4info, op)
for i := 0; i < len(spaceSplit); i++ {
samplue = textv2.AppendGlyphs(nil, tab2space(photontext[printext+rellines]), hackgen4info, nil)
}
for i, gl := range samplue {
op := &ebiten.DrawImageOptions{}
//op.GeoM.Translate(90, ((float64(printext)+1)*18)+5)
syntstat, endsat := ezSyntaxHighlight(photontext[printext+rellines])
if i >= endsat {
op.ColorScale.Reset()
} else if syntstat == "good" {
op.ColorScale.Scale(0, 0.5, 0, 0)
}
if gl.Image == nil {
continue
}
op.GeoM.Translate(float64(textx-1), ((float64(printext)+1)*18)+5)
op.GeoM.Translate(gl.X, gl.Y)
screen.DrawImage(gl.Image, op)
}
// start column loop
/*for textrepeat := 0; textrepeat < len(slicedtext); {
if string(" ") == string(slicedtext[textrepeat]) {
textx += 30
} else if len(string(slicedtext[textrepeat])) != 1 {
// If multi-byte text, print bigger
text.Draw(screen, string(slicedtext[textrepeat]), smallHackGenFont, textx-1, ((printext + 2) * 18), color.White)
textx += 15
} else {
// If not, print normally
text.Draw(screen, string(slicedtext[textrepeat]), smallHackGenFont, textx, ((printext + 2) * 18), color.White)
textx += 9
}
textrepeat++
}*/
printext++
}
// draw cursor
//nonkanj, kanj, tabs := checkMixedKanjiLength(photontext[cursornowy-1], cursornowx)
//cursorproceedx := (nonkanj*9 + kanj*15 + tabs*36) + cursorxstart
cursorop := &ebiten.DrawImageOptions{}
//cursorop.GeoM.Translate(float64(cursorproceedx), float64((cursornowy-(rellines))*18)+5)
txtx, _ := textv2.Measure(photontext[cursornowy-1][:cursornowx-1], hackgen4info, 0)
cursorop.GeoM.Translate(txtx+90, float64((cursornowy-(rellines))*18)+5)
screen.DrawImage(cursorimg, cursorop)
// Draw scroll bar base
scrollbar = renewimg(scrollbar, scrollbarwidth, screenHeight, color.RGBA{80, 80, 80, 255})
scrollbarop := &ebiten.DrawImageOptions{}
scrollbarop.GeoM.Translate(float64(screenWidth)-float64(scrollbarwidth), float64(topopBarSize))
screen.DrawImage(scrollbar, scrollbarop)
// Draw scroll bit
/* init scroll-bit */
var textsize int
{
textsize = len(photontext) + Maxtext
}
scrollbartext := float64(screenHeight-20) / float64((float64(textsize) / float64(Maxtext)))
if scrollbartext < 1 {
scrollbartext = 1
}
scrollbit = renewimg(scrollbit, scrollbarwidth, int(scrollbartext), color.RGBA{30, 30, 30, 255}) //ebiten.NewImage(25, int(scrollbartext))
scrollbitop := &ebiten.DrawImageOptions{}
scrollbitop.GeoM.Translate(float64(screenWidth)-float64(scrollbarwidth), float64((float64(screenHeight-20)/float64(textsize))*float64(rellines)+20))
screen.DrawImage(scrollbit, scrollbitop)
// Draw lines separator
linessepop := &ebiten.DrawImageOptions{}
linessepop.GeoM.Translate(float64(cursorxstart-5), 0)
linessep = renewimg(linessep, 2, screen.Bounds().Dy(), color.RGBA{100, 100, 100, 255})
screen.DrawImage(linessep, linessepop)
// Draw info-bar
infoBarop := &ebiten.DrawImageOptions{}
infoBarop.GeoM.Translate(0, float64(screenHeight))
infoBar = renewimg(infoBar, screen.Bounds().Dx(), 100, color.RGBA{87, 97, 87, 255})
screen.DrawImage(infoBar, infoBarop)
{
zurasu, padding := textv2.Measure(rightinfotext, hackgen4info, 0)
infoBarTextY := float64(screenHeight + ((infoBarSize - int(padding)) / 2))
//text.Draw(screen, leftinfotxt, smallHackGenFont, 5, screenHeight+infoBarSize-4, color.White)
op := &textv2.DrawOptions{}
op.GeoM.Translate(2, infoBarTextY)
textv2.Draw(screen, leftinfotxt, hackgen4info, op)
op = &textv2.DrawOptions{}
op.GeoM.Translate(float64(screenWidth-5)-zurasu, infoBarTextY)
//text.Draw(screen, rightinfotext, smallHackGenFont, screenWidth-((len(rightinfotext))*10), screenHeight+infoBarSize-4, color.White)
textv2.Draw(screen, rightinfotext, hackgen4info, op)
}
{
//[]string{"Files", "Save"}, []string{"Edit", "Undo"}, []string{"View"}
var mbfiles []plastk.MenuBarColumn
{
filestmp := plastk.MenuBarColumn{
ColumnType: "dropdown",
ColumnName: "Files",
}
savetmp := plastk.MenuBarColumn{
ColumnType: "button",
ColumnName: "Save",
ColumnID: "filessavebutton",
}
exittmp := plastk.MenuBarColumn{
ColumnType: "button",
ColumnName: "Exit",
ColumnID: "filesexitbutton",
}
mbfiles = append(mbfiles, filestmp, savetmp, exittmp)
}
var mbedit []plastk.MenuBarColumn
{
edittmp := plastk.MenuBarColumn{
ColumnType: "dropdown",
ColumnName: "Edit",
}
undotmp := plastk.MenuBarColumn{
ColumnType: "button",
ColumnName: "Undo",
ColumnID: "editundobutton",
}
redotmp := plastk.MenuBarColumn{
ColumnType: "button",
ColumnName: "Redo",
ColumnID: "editredobutton",
}
mbedit = append(mbedit, edittmp, undotmp, redotmp)
}
var mbabout []plastk.MenuBarColumn
mbabout = append(mbabout, plastk.MenuBarColumn{
ColumnType: "button",
ColumnName: "About",
},
)
plastk.DrawMenuBar(screen, color.RGBA{100, 100, 100, 255}, hackgen4info, 20, mbfiles, mbedit, mbabout)
}
// draw command-line
if commandlineforcused || cmdresult != "" {
commandlineop := &ebiten.DrawImageOptions{}
commandlineop.GeoM.Translate(float64(0), float64(screenHeight+commandlineSize))
commandLine = plastk.ReNewImg(commandLine, screenWidth, commandlineSize, color.RGBA{39, 39, 39, 255})
screen.DrawImage(commandLine, commandlineop)
if commandlineforcused {
text.Draw(screen, photoncmd, smallHackGenFont, 5, screenHeight+15+commandlineSize, color.White)
} else {
text.Draw(screen, cmdresult, smallHackGenFont, 5, screenHeight+15+commandlineSize, color.White)
}
}
// Draw info
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.ActualTPS(), ebiten.ActualFPS()))
/* Benchmark
if len(os.Args) >= 2 {
if os.Args[1] == "bench" {
os.Exit(0)
}
}*/
}
func (g *Editor) Layout(outsideWidth, outsideHeight int) (int, int) {
screenWidth, screenHeight := ebiten.WindowSize()
return screenWidth, screenHeight
}
func ezSyntaxHighlight(txt string) (string, int) {
splitted := strings.Split(txt, " ")
ret := len([]rune(splitted[0]))
if splitted[0] == "package" {
return "good", ret
}
if splitted[0] == "import" {
return "good", ret
}
if splitted[0] == "func" {
return "good", ret
}
if splitted[0] == "fn" {
return "good", ret
}
if splitted[0] == "var" {
return "good", ret
}
return "", 0
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("PhotonText(kari)")
ebiten.SetTPS(140)
go func() {
now := time.Now()
fmt.Println("photontext will booted with no error(s)")
loginloop:
err := client.Login("1199337296307163146")
if err != nil {
time.Sleep(20 * time.Second)
goto loginloop
}
success := bool(false)
activityloop:
state := strconv.Itoa(cursornowy) + string(":") + strconv.Itoa(cursornowx)
err = client.SetActivity(client.Activity{
Details: "Coding with PhotonText",
State: state,
LargeImage: "photon2",
LargeText: "PhotonText Logo",
Timestamps: &client.Timestamps{
Start: &now,
},
})
if err != nil {
fmt.Println(err)
goto loginloop
}
if !success {
fmt.Println("rich presence active")
success = true
}
time.Sleep(1 * time.Second)
goto activityloop
}()
go func() {
for {
looping := false
if clearresult == 0 {
if !looping {
cmdresult = ""
}
time.Sleep(1 * time.Second)
looping = true
} else if clearresult <= 10 {
looping = false
time.Sleep(1 * time.Second)
clearresult--
} else if clearresult > 10 {
looping = false
clearresult = 10
}
}
}()
go func() {
for {
if editingfile == "" {
ebiten.SetWindowTitle("PhotonText(kari)")
} else {
ebiten.SetWindowTitle(fmt.Sprint(editingfile, " - PhotonText(kari)"))
}
time.Sleep(500 * time.Millisecond)
}
}()
if err := ebiten.RunGame(&Editor{}); err != nil {
log.Fatal(err)
}
}
// Proceed command
func proceedcmd(command string) (returnstr string) {
command2slice := strings.Split(command, " ")
if len(command2slice) >= 1 {
cmd := command2slice[0]
// Save override
if cmd == "w" || cmd == "wr" || cmd == "wri" || cmd == "writ" || cmd == "write" {
if len(command2slice) >= 2 {
return "Too many arguments for command: " + cmd
} else {
phsave(editingfile)
return fmt.Sprint("Saved to ", editingfile)
}
} else
//
if cmd == "q" || cmd == "qu" || cmd == "qui" || cmd == "quit" {
ebiten.SetWindowClosingHandled(true)
closewindow = true
} else if cmd == "wq" {
proceedcmd("w")
proceedcmd("q")
} else if cmd == "version" {
return "PhotonText rolling " + runtime.Version()
} else
// Save with other name.
if cmd == "sav" || cmd == "save" || cmd == "savea" || cmd == "saveas" {
if len(command2slice) == 1 {
return "Too few arguments for command: " + cmd
} else if len(command2slice) >= 3 {
return "Too many arguments for command: " + cmd
} else /* when 2 args */ {
if strings.HasPrefix(command2slice[1], "~") {
home, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)