1 /* Written by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>f
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 import std
.concurrency
;
23 import arsd
.simpledisplay
;
27 import iv
.nanovg
.oui
.blendish
;
28 import iv
.nanovg
.perf
;
51 //version = debug_draw;
54 // ////////////////////////////////////////////////////////////////////////// //
55 void run (string bookFileName
) {
59 __gshared
bool fpsVisible
= false;
61 string newBookFileName
;
63 if (GWidth
< MinWinWidth
) GWidth
= MinWinWidth
;
64 if (GHeight
< MinWinHeight
) GHeight
= MinWinHeight
;
68 booktext
= loadBook(bookFileName
);
70 //setOpenGLContextVersion(3, 2); // up to GLSL 150
71 setOpenGLContextVersion(2, 0); // it's enough
72 //openGLContextCompatible = false;
74 auto sdwindow
= new SimpleWindow(GWidth
, GHeight
, booktext
.title
~" \xe2\x80\x94 "~booktext
.authorFirst
~" "~booktext
.authorLast
, OpenGlOptions
.yes
, Resizablity
.allowResizing
);
75 sdwindow
.hideCursor(); // we will do our own
76 sdwindow
.setMinSize(MinWinWidth
, MinWinHeight
);
78 auto stt
= MonoTime
.currTime
;
79 auto prevt
= MonoTime
.currTime
;
82 int textHeight
= GHeight
-8;
83 bool doSaveCheck
= false;
84 MonoTime nextSaveTime
;
86 MonoTime nextIfTime
= MonoTime
.currTime
;
88 BookInfo
[] recentFiles
;
89 BookMetadata bookmeta
;
91 int toMove
= 0; // for smooth scroller
95 int newYLine
= -1; // index
97 bool newYFade
= false;
98 MonoTime nextFadeTime
;
100 int curImg
= -1, curImgWhite
= -1;
101 int mouseX
= -666, mouseY
= -666;
102 bool mouseHigh
= false;
103 bool mouseHidden
= false;
104 auto lastMMove
= MonoTime
.currTime
;
105 float mouseAlpha
= 1.0f;
106 int mouseFadingAway
= false;
108 bool needRedrawFlag
= true;
110 bool firstFormat
= true;
112 __gshared
bool inGalaxyMap
= false;
114 void delegate (int item
) onPopupSelect
;
115 int shipModelIndex
= 0;
116 bool popupNoShipKill
= false;
118 void refresh () { needRedrawFlag
= true; }
119 void refreshed () { needRedrawFlag
= false; }
122 return (needRedrawFlag ||
(currPopup
!is null && currPopup
.needRedraw
));
125 @property bool inMenu () { return (currPopup
!is null); }
126 void closeMenu () { if (currPopup
!is null) currPopup
.destroy
; currPopup
= null; onPopupSelect
= null; }
128 auto childTid
= spawn(&reformatThreadFn
, thisTid
);
129 childTid
.setMaxMailboxSize(128, OnCrowding
.block
);
130 thisTid
.setMaxMailboxSize(128, OnCrowding
.block
);
132 void setShip (int idx
) {
133 if (eliteShipFiles
.length
) {
134 import core
.memory
: GC
;
135 if (idx
>= eliteShipFiles
.length
) idx
= cast(int)eliteShipFiles
.length
-1;
136 if (idx
< 0) idx
= 0;
137 if (shipModelIndex
== idx
&& shipModel
!is null) return;
139 shipModelIndex
= idx
;
140 if (shipModel
!is null) {
141 shipModel
.glUnload();
142 shipModel
.freeData();
149 shipModel
= new EliteModel(eliteShipFiles
[shipModelIndex
]);
150 shipModel
.glUpload();
151 shipModel
.freeImages();
152 } catch (Exception e
) {}
153 //shipModel = eliteShips[shipModelIndex];
158 void ensureShipModel () {
159 if (eliteShipFiles
.length
&& shipModel
is null) {
160 import std
.random
: uniform
;
161 setShip(uniform
!"[)"(0, eliteShipFiles
.length
));
165 void freeShipModel () {
166 if (!showShip
&& !inMenu
&& shipModel
!is null) {
167 import core
.memory
: GC
;
168 shipModel
.glUnload();
169 shipModel
.freeData();
176 void doSaveState (bool forced
=false) {
178 if (formatWorks
!= 0) return;
179 if (!doSaveCheck
) return;
180 auto ct
= MonoTime
.currTime
;
181 if (ct
< nextSaveTime
) return;
183 if (laytext
is null || laytext
.lineCount
== 0 || formatWorks
!= 0) return;
186 auto fo
= VFile(stateFileName
, "w");
187 if (laytext
!is null && laytext
.lineCount
) {
188 auto lnum
= laytext
.findLineAtY(topY
);
189 if (lnum
>= 0) fo
.writeln("wordindex=", laytext
.line(lnum
).wstart
);
191 } catch (Exception
) {}
195 void stateChanged () {
198 nextSaveTime
= MonoTime
.currTime
+10.seconds
;
202 void hardScrollBy (int delta
) {
203 if (delta
== 0 || laytext
is null || laytext
.lineCount
== 0) return;
206 if (topY
>= laytext
.textHeight
-textHeight
) topY
= cast(int)laytext
.textHeight
-textHeight
;
207 if (topY
< 0) topY
= 0;
214 void scrollBy (int delta
) {
215 if (delta
== 0 || laytext
is null || laytext
.lineCount
== 0) return;
220 // scrolling up, mark top line
221 newYLine
= laytext
.findLineAtY(topY
);
223 // scrolling down, mark bottom line
224 newYLine
= laytext
.findLineAtY(topY
+textHeight
-2);
228 writeln("scrollBy: delta=", delta
, "; newYLine=", newYLine
, "; topLine=", laytext
.findLineAtY(topY
));
230 if (newYLine
< 0) newYFade
= false;
234 if (laytext
is null) return;
242 void goTo (uint widx
) {
243 if (laytext
is null) return;
244 auto lidx
= laytext
.findLineWithWord(widx
);
246 assert(lidx
< laytext
.lineCount
);
248 if (topY
!= laytext
.line(lidx
).y
) {
249 topY
= laytext
.line(lidx
).y
;
255 writeln("goto: widx=", widx
, "; lidx=", lidx
, "; fwn=", laytext
.line(lidx
).wstart
, "; topY=", topY
);
256 auto lnum
= laytext
.findLineAtY(topY
);
257 writeln(" newlnum=", lnum
, "; lidx.y=", laytext
.line(lidx
).y
, "; lnum.y=", laytext
.line(lnum
).y
);
265 xiniParse(VFile(stateFileName
),
268 if (widx
>= 0) goTo(widx
);
269 } catch (Exception
) {}
272 void loadAndFormat (string filename
) {
273 assert(formatWorks
<= 0);
277 //formatWorks = -1; //FIXME
283 //sdwindow.redrawOpenGlSceneNow();
284 //booktext = loadBook(newBookFileName);
285 //newBookFileName = null;
287 //if (formatWorks < 0) formatWorks = 1; else ++formatWorks;
289 //writeln("*** loading new book: '", filename, "'");
290 childTid
.send(ReformatWork(null, filename
, GWidth
, GHeight
));
295 if (formatWorks
< 0) formatWorks
= 1; else ++formatWorks
;
296 childTid
.send(ReformatWork(cast(shared)booktext
, null, GWidth
, GHeight
));
300 void formatComplete (ref ReformatWorkComplete w
) {
301 scope(exit
) { import core
.memory
: GC
; GC
.collect(); GC
.minimize(); }
303 auto lt
= cast(LayText
)w
.laytext
;
304 scope(exit
) if (lt
) lt
.freeMemory();
307 BookMetadata meta
= cast(BookMetadata
)w
.meta
;
310 BookText
bt = cast(BookText
)w
.booktext
;
312 if (bt !is booktext
) {
316 sdwindow
.title
= booktext
.title
~" \xe2\x80\x94 "~booktext
.authorFirst
~" "~booktext
.authorLast
;
317 } else if (bookmeta
is null) {
321 if (isQuitRequested || formatWorks
<= 0) return;
325 if (formatWorks
== 0) reformat();
329 if (formatWorks
!= 0) return;
333 if (!firstFormat
&& laytext
!is null && laytext
.lineCount
) {
334 auto lidx
= laytext
.findLineAtY(topY
);
335 if (lidx
>= 0) widx
= laytext
.line(lidx
).wstart
;
337 if (laytext
!is null) laytext
.freeMemory();
351 void pushPosition () {
352 if (laytext
is null || laytext
.lineCount
== 0) return;
353 auto lidx
= laytext
.findLineAtY(topY
);
354 if (lidx
>= 0) posstack
~= laytext
.line(lidx
).wstart
;
357 void popPosition () {
358 if (posstack
.length
== 0) return;
359 auto widx
= posstack
[$-1];
360 posstack
.length
-= 1;
361 posstack
.assumeSafeAppend
;
365 void gotoSection (int sn
) {
366 if (laytext
is null || laytext
.lineCount
== 0 || bookmeta
is null) return;
367 if (sn
< 0 || sn
>= bookmeta
.sections
.length
) return;
368 auto lidx
= laytext
.findLineWithWord(bookmeta
.sections
[sn
].wordidx
);
370 auto newY
= laytext
.line(lidx
).y
;
379 version(X11
) sdwindow
.closeQuery
= delegate () { concmd("quit"); };
381 void closeWindow () {
382 doSaveState(true); // forced state save
383 if (!sdwindow
.closed
&& vg
!is null) {
384 if (curImg
>= 0) { vg
.deleteImage(curImg
); curImg
= -1; }
385 if (curImgWhite
>= 0) { vg
.deleteImage(curImgWhite
); curImgWhite
= -1; }
393 sdwindow
.visibleForTheFirstTime
= delegate () {
394 sdwindow
.setAsCurrentOpenGlContext(); // make this window active
395 sdwindow
.vsync
= false;
396 //sdwindow.useGLFinish = false;
397 //glbindLoadFunctions();
400 uint flags
= NVG_DEBUG
;
401 if (flagNanoAA
) flags |
= NVG_ANTIALIAS
;
402 if (flagNanoSS
) flags |
= NVG_STENCIL_STROKES
;
403 vg
= createGL2NVG(flags
);
406 writeln("Could not init nanovg.");
411 curImg
= createCursorImage(vg
);
412 curImgWhite
= createCursorImage(vg
, true);
413 fps
= new PerfGraph("Frame Time", PerfGraph
.Style
.FPS
, "ui");
414 } catch (Exception e
) {
415 import std
.stdio
: stderr
;
416 stderr
.writeln("ERROR: ", e
.msg
);
424 sdwindow
.redrawOpenGlScene();
428 void relayout (bool forced
=false) {
429 if (laytext
!is null) {
431 auto lidx
= laytext
.findLineAtY(topY
);
432 if (lidx
>= 0) widx
= laytext
.line(lidx
).wstart
;
433 int maxWidth
= GWidth
-4-2-BND_SCROLLBAR_WIDTH
-2;
434 //if (maxWidth < MinWinWidth) maxWidth = MinWinWidth;
437 auto stt
= MonoTime
.currTime
;
438 laytext
.relayout(maxWidth
, forced
);
439 auto ett
= MonoTime
.currTime
-stt
;
440 writeln("relayouted in ", ett
.total
!"msecs", " milliseconds; lines:", laytext
.lineCount
, "; words:", laytext
.nextWordIndex
);
447 sdwindow
.windowResized
= delegate (int w
, int h
) {
448 //conwriteln("w=", w, "; h=", h);
449 //if (w < MinWinWidth) w = MinWinWidth;
450 //if (h < MinWinHeight) h = MinWinHeight;
451 glViewport(0, 0, w
, h
);
454 textHeight
= GHeight
-8;
460 void drawShipName () {
461 if (shipModel
is null || shipModel
.name
.length
== 0) return;
462 vg
.fontFaceId(uiFont
);
463 vg
.textAlign(NVGTextAlign
.H
.Left
, NVGTextAlign
.V
.Baseline
);
464 vg
.fontSize(fsizeUI
);
465 auto w
= vg
.bndLabelWidth(-1, shipModel
.name
)+8;
466 float h
= BND_WIDGET_HEIGHT
+8;
467 float mx
= (GWidth
-w
)/2.0;
468 float my
= (GHeight
-h
)-8;
469 vg
.bndMenuBackground(mx
, my
, w
, h
, BND_CORNER_NONE
);
470 vg
.bndMenuItem(mx
+4, my
+4, w
-8, BND_WIDGET_HEIGHT
, BND_DEFAULT
, -1, shipModel
.name
);
471 if (shipModel
.dispName
&& shipModel
.dispName
!= shipModel
.name
) {
472 my
-= BND_WIDGET_HEIGHT
+16;
473 w
= vg
.bndLabelWidth(-1, shipModel
.dispName
)+8;
475 vg
.bndMenuBackground(mx
, my
, w
, h
, BND_CORNER_NONE
);
476 vg
.bndMenuItem(mx
+4, my
+4, w
-8, BND_WIDGET_HEIGHT
, BND_DEFAULT
, -1, shipModel
.dispName
);
480 void createSectionMenu () {
482 //writeln("lc=", laytext.lineCount, "; bm: ", (bookmeta !is null));
483 if (laytext
is null || laytext
.lineCount
== 0 || bookmeta
is null) { freeShipModel(); return; }
484 currPopup
= new PopupMenu(vg
, "Sections"d
, () {
486 foreach (const ref sc
; bookmeta
.sections
) items
~= sc
.name
;
489 //writeln(currPopup.items.length);
490 // find current section
491 currPopup
.curItemIdx
= 0;
492 auto lidx
= laytext
.findLineAtY(topY
);
493 if (lidx
>= 0 && bookmeta
.sections
.length
> 0) {
494 foreach (immutable sidx
, const ref sc
; bookmeta
.sections
) {
495 auto sline
= laytext
.findLineWithWord(sc
.wordidx
);
496 if (sline
>= 0 && lidx
>= sline
) currPopup
.curItemIdx
= cast(int)sidx
;
499 onPopupSelect
= (int item
) { gotoSection(item
); };
502 void createQuitMenu (bool wantYes
) {
504 currPopup
= new PopupMenu(vg
, "Quit?"d
, () {
505 return ["Yes"d
, "No"d
];
507 currPopup
.curItemIdx
= (wantYes ?
0 : 1);
508 onPopupSelect
= (int item
) { if (item
== 0) concmd("quit"); };
511 void createRecentMenu () {
513 if (recentFiles
.length
== 0) recentFiles
= loadDetailedHistory();
514 if (recentFiles
.length
== 0) { freeShipModel(); return; }
515 currPopup
= new PopupMenu(vg
, "Recent files"d
, () {
516 import std
.conv
: to
;
518 foreach (const ref BookInfo bi
; recentFiles
) {
520 if (bi
.seqname
.length
) {
522 if (bi
.seqnum
) { s
~= to
!string(bi
.seqnum
); s
~= ": "; }
523 //writeln(bi.seqname);
527 if (bi
.author
.length
) { s
~= " \xe2\x80\x94 "; s
~= bi
.author
; }
532 currPopup
.curItemIdx
= cast(int)recentFiles
.length
-1;
533 onPopupSelect
= (int item
) {
534 newBookFileName
= recentFiles
[item
].diskfile
;
535 popupNoShipKill
= true;
539 bool menuKey (KeyEvent event
) {
540 if (formatWorks
!= 0) return false;
541 if (!inMenu
) return false;
542 if (inGalaxyMap
) return false;
543 if (!event
.pressed
) return false;
544 auto res
= currPopup
.onKey(event
);
545 if (res
== PopupMenu
.Close
) {
549 } else if (res
>= 0) {
550 if (onPopupSelect
!is null) onPopupSelect(res
);
552 if (popupNoShipKill
) popupNoShipKill
= false; else freeShipModel();
558 bool menuMouse (MouseEvent event
) {
559 if (formatWorks
!= 0) return false;
560 if (!inMenu
) return false;
561 if (inGalaxyMap
) return false;
562 auto res
= currPopup
.onMouse(event
);
563 if (res
== PopupMenu
.Close
) {
567 } else if (res
>= 0) {
568 if (onPopupSelect
!is null) onPopupSelect(res
);
570 if (popupNoShipKill
) popupNoShipKill
= false; else freeShipModel();
576 bool readerKey (KeyEvent event
) {
577 if (formatWorks
!= 0) return false;
578 if (!event
.pressed
) {
580 case Key
.Up
: arrowDir
= 0; return true;
581 case Key
.Down
: arrowDir
= 0; return true;
588 if (event
.modifierState
&ModifierState
.shift
) {
589 //goto case Key.PageUp;
590 hardScrollBy(toMove
); toMove
= 0;
591 scrollBy(-textHeight
/3*2);
593 //goto case Key.PageDown;
594 hardScrollBy(toMove
); toMove
= 0;
595 scrollBy(textHeight
/3*2);
602 hardScrollBy(toMove
); toMove
= 0;
603 hardScrollBy(-(textHeight
> 32 ? textHeight
-32 : textHeight
));
606 hardScrollBy(toMove
); toMove
= 0;
607 hardScrollBy(textHeight
> 32 ? textHeight
-32 : textHeight
);
625 bool controlKey (KeyEvent event
) {
626 if (!event
.pressed
) return false;
629 if (inGalaxyMap
) { inGalaxyMap
= false; refresh(); return true; }
630 if (inMenu
) { closeMenu(); freeShipModel(); refresh(); return true; }
631 if (showShip
) { showShip
= false; freeShipModel(); refresh(); return true; }
633 createQuitMenu(true);
636 case Key
.P
: if (event
.modifierState
== ModifierState
.ctrl
) { concmd("r_fps toggle"); refresh(); return true; } break;
637 case Key
.I
: if (event
.modifierState
== ModifierState
.ctrl
) { interAllowed
= !interAllowed
; refresh(); return true; } break;
638 case Key
.N
: if (event
.modifierState
== ModifierState
.ctrl
) { sbLeft
= !sbLeft
; refresh(); return true; } break;
639 case Key
.C
: if (event
.modifierState
== ModifierState
.ctrl
) { if (addIf()) refresh(); return true; } break;
641 if (event
.modifierState
== ModifierState
.ctrl
&& eliteShipFiles
.length
> 0) {
643 showShip
= !showShip
;
644 if (showShip
) ensureShipModel(); else freeShipModel();
650 case Key
.Q
: if (event
.modifierState
== ModifierState
.ctrl
) { concmd("quit"); return true; } break;
651 case Key
.B
: if (formatWorks
== 0 && !inMenu
&& !inGalaxyMap
&& event
.modifierState
== ModifierState
.ctrl
) { pushPosition(); return true; } break;
653 if (formatWorks
== 0 && !showShip
&& !inMenu
&& !inGalaxyMap
) {
660 if (formatWorks
== 0 && event
.modifierState
== ModifierState
.alt
) {
667 if (!inMenu
&& !showShip
) {
668 inGalaxyMap
= !inGalaxyMap
;
673 if (formatWorks
== 0 && event
.modifierState
== ModifierState
.ctrl
) relayout(true);
675 case Key
.Home
: if (showShip
) { setShip(0); return true; } break;
676 case Key
.End
: if (showShip
) { setShip(cast(int)eliteShipFiles
.length
); return true; } break;
677 case Key
.Up
: case Key
.Left
: if (showShip
) { setShip(shipModelIndex
-1); return true; } break;
678 case Key
.Down
: case Key
.Right
: if (showShip
) { setShip(shipModelIndex
+1); return true; } break;
684 static int startX () { pragma(inline
, true); return (sbLeft ?
2+BND_SCROLLBAR_WIDTH
+2 : 4); }
685 static int endX () { pragma(inline
, true); return startX
+GWidth
-4-2-BND_SCROLLBAR_WIDTH
-2-1; }
687 int startY () { pragma(inline
, true); return (GHeight
-textHeight
)/2; }
688 int endY () { pragma(inline
, true); return startY
+textHeight
-1; }
690 sdwindow
.redrawOpenGlScene
= delegate () {
691 if (isQuitRequested
) return;
693 oglResizeConsole(GWidth
/2, GHeight
/2, 2);
696 conwriteln("cnt=", cnt++);
699 //glClearColor(0, 0, 0, 0);
700 //glClearColor(0.18, 0.18, 0.18, 0);
701 glClearColor(colorBack
.r
, colorBack
.g
, colorBack
.b
, 0);
702 glClear(glNVGClearFlags|EliteModel
.glClearFlags
);
705 needRedrawFlag
= (fps
!is null && fpsVisible
);
706 if (vg
is null) return;
708 scope(exit
) vg
.releaseImages();
709 vg
.beginFrame(GWidth
, GHeight
, 1);
713 float curHeight
= (laytext
!is null ? topY
: 0);
714 float th
= (laytext
!is null ? laytext
.textHeight
-textHeight
: 0);
715 if (th
<= 0) { curHeight
= 0; th
= 1; }
716 float sz
= cast(float)(GHeight
-4)/th
;
717 if (sz
> 1) sz
= 1; else if (sz
< 0.05) sz
= 0.05;
718 int sx
= (sbLeft ?
2 : GWidth
-BND_SCROLLBAR_WIDTH
-2);
719 vg
.bndScrollSlider(sx
, 2, BND_SCROLLBAR_WIDTH
, GHeight
-4, BND_DEFAULT
, curHeight
/th
, sz
);
721 if (laytext
is null) {
722 if (shipModel
is null) {
724 vg
.fillColor(colorText
);
725 int drawY
= (GHeight
-textHeight
)/2;
726 vg
.intersectScissor((sbLeft ?
2+BND_SCROLLBAR_WIDTH
+2 : 4), drawY
, GWidth
-4-2-BND_SCROLLBAR_WIDTH
-2, textHeight
);
727 vg
.fontFaceId(textFont
);
728 vg
.fontSize(fsizeText
);
729 vg
.textAlign(NVGTextAlign
.H
.Center
, NVGTextAlign
.V
.Middle
);
730 vg
.text(GWidth
/2, GHeight
/2, "REFORMATTING");
735 if (laytext
!is null && laytext
.lineCount
) {
737 vg
.fillColor(colorText
);
739 vg
.intersectScissor((sbLeft ?
2+BND_SCROLLBAR_WIDTH
+2 : 4), drawY
, GWidth
-4-2-BND_SCROLLBAR_WIDTH
-2, textHeight
);
740 //FIXME: not GHeight!
741 int lidx
= laytext
.findLineAtY(topY
);
742 if (lidx
>= 0 && lidx
< laytext
.lineCount
) {
743 drawY
-= topY
-laytext
.line(lidx
).y
;
744 vg
.textAlign(NVGTextAlign
.H
.Left
, NVGTextAlign
.V
.Baseline
);
745 LayFontStyle lastStyle
;
747 while (lidx
< laytext
.lineCount
&& drawY
< GHeight
) {
748 auto ln
= laytext
.line(lidx
);
749 foreach (ref LayWord w
; laytext
.lineWords(lidx
)) {
750 if (lastStyle
!= w
.style
) {
751 if (w
.style
.fontface
!= lastStyle
.fontface
) vg
.fontFace(laytext
.fontFace(w
.style
.fontface
));
752 vg
.fontSize(w
.style
.fontsize
);
753 auto c
= NVGColor(w
.style
.color
);
759 if (newYFade
&& newYLine
== lidx
) vg
.fillColor(nvgLerpRGBA(colorText
, colorTextHi
, newYAlpha
));
760 auto oid
= w
.objectIdx
;
763 laytext
.objects
[oid
].draw(vg
, startx
+w
.x
, drawY
+ln
.h
+ln
.desc
);
766 vg
.text(startx
+w
.x
, drawY
+ln
.h
+ln
.desc
, laytext
.wordText(w
));
768 //TODO: draw lines over whitespace
769 if (lastStyle
.underline
) vg
.rect(startx
+w
.x
, drawY
+ln
.h
+ln
.desc
+1, w
.w
, 1);
770 if (lastStyle
.strike
) vg
.rect(startx
+w
.x
, drawY
+ln
.h
+ln
.desc
-w
.asc
/2, w
.w
, 1);
771 if (lastStyle
.overline
) vg
.rect(startx
+w
.x
, drawY
+ln
.h
+ln
.desc
-w
.asc
-1, w
.w
, 1);
772 version(debug_draw
) {
776 vg
.strokeColor(nvgRGB(0, 0, 255));
777 vg
.rect(startx
+w
.x
, drawY
, w
.w
, w
.h
);
781 if (newYFade
&& newYLine
== lidx
) vg
.fillColor(NVGColor(lastStyle
.color
));
791 if (colorDim
.a
!= 1) {
792 //vg.scissor(0, 0, GWidth, GHeight);
795 vg
.fillColor(colorDim
);
796 vg
.rect(0, 0, GWidth
, GHeight
);
800 // dim more if menu is active
801 if (inMenu || showShip || formatWorks
!= 0) {
802 //vg.scissor(0, 0, GWidth, GHeight);
805 //vg.globalAlpha(0.5);
806 vg
.fillColor(nvgRGBA(0, 0, 0, (showShip || formatWorks
!= 0 ?
127 : 64)));
807 vg
.rect(0, 0, GWidth
, GHeight
);
810 if (shipModel
!is null) {
812 float zz
= shipModel
.bbox
[1].z
-shipModel
.bbox
[0].z
;
826 drawModel(shipAngle
, shipModel
);
827 vg
.beginFrame(GWidth
, GHeight
, 1);
831 if (formatWorks
== 0) {
833 //vg.beginFrame(GWidth, GHeight, 1);
834 //vg.scissor(0, 0, GWidth, GHeight);
840 if (fps
!is null && fpsVisible
) {
841 //vg.beginFrame(GWidth, GHeight, 1);
842 //vg.scissor(0, 0, GWidth, GHeight);
844 fps
.render(vg
, GWidth
-fps
.width
-4, GHeight
-fps
.height
-4);
847 if (inGalaxyMap
) drawGalaxy(vg
);
849 if (curImg
>= 0 && !mouseHidden
) {
851 //vg.beginFrame(GWidth, GHeight, 1);
853 //vg.scissor(0, 0, GWidth, GHeight);
855 vg
.imageSize(curImg
, &w
, &h
);
856 if (mouseFadingAway
) {
858 if (mouseAlpha
<= 0) { mouseFadingAway
= false; mouseHidden
= true; }
862 vg
.globalAlpha(mouseAlpha
);
864 vg
.fillPaint(vg
.imagePattern(mouseX
, mouseY
, w
, h
, 0, curImg
, 1));
866 vg
.fillPaint(vg
.imagePattern(mouseX
, mouseY
, w
, h
, 0, curImgWhite
, 1));
868 vg
.rect(mouseX
, mouseY
, w
, h
);
873 vg.fillPaint(vg.imagePattern(mouseX, mouseY, w, h, 0, curImgWhite, 0.4));
874 vg.rect(mouseX, mouseY, w, h);
884 void processThreads () {
885 ReformatWorkComplete wd
;
887 bool workDone
= false;
888 auto res
= receiveTimeout(Duration
.zero
,
892 (ReformatWorkComplete w
) {
897 if (!res
) { assert(!workDone
); break; }
898 if (workDone
) { workDone
= false; formatComplete(wd
); }
902 auto lastTimerEventTime
= MonoTime
.currTime
;
903 bool somethingVisible
= true;
905 sdwindow
.visibilityChanged
= delegate (bool vis
) {
906 //import core.stdc.stdio; printf("VISCHANGED: %s\n", (vis ? "tan" : "ona").ptr);
907 somethingVisible
= vis
;
910 conRegVar
!fpsVisible("r_fps", "show fps indicator", (self
, valstr
) { refresh(); });
911 conRegVar
!inGalaxyMap("r_galaxymap", "show Elite galaxy map", (self
, valstr
) { refresh(); });
913 conRegVar
!interAllowed("r_interference", "show interference", (self
, valstr
) { refresh(); });
914 conRegVar
!sbLeft("r_sbleft", "show scrollbar at the left side", (self
, valstr
) { refresh(); });
916 conRegVar
!showShip("r_showship", "show Elite ship", (self
, valstr
) {
917 if (eliteShipFiles
.length
== 0) return false;
921 if (showShip
) ensureShipModel(); else freeShipModel();
927 if (currPopup
!is null) {
933 onPopupSelect
= null;
934 })("menu_close", "close current popup menu");
937 if (formatWorks
== 0 && !showShip
&& !inGalaxyMap
) {
944 })("menu_section", "show section menu");
947 if (formatWorks
== 0 && !showShip
&& !inGalaxyMap
) {
954 })("menu_recent", "show recent menu");
956 sdwindow
.eventLoop(1000/34,
959 if (sdwindow
.closed
) return;
961 if (isQuitRequested
) { closeWindow(); return; }
962 auto ctt
= MonoTime
.currTime
;
965 auto spass
= (ctt
-lastTimerEventTime
).total
!"msecs";
966 //if (spass >= 30) { import core.stdc.stdio; printf("WARNING: too long frame time: %u\n", cast(uint)spass); }
967 //{ import core.stdc.stdio; printf("FRAME TIME: %u\n", cast(uint)spass); }
968 lastTimerEventTime
= ctt
;
971 //curt = MonoTime.currTime;
973 //auto secs = cast(double)((curt-stt).total!"msecs")/1000.0;
974 auto dt = cast(double)((curt
-prevt
).total
!"msecs")/1000.0;
975 if (fps
!is null) fps
.update(dt);
979 if (formatWorks
== 0) {
983 if (sc
> Delta
) sc
= Delta
;
986 nextFadeTime
= ctt
+500.msecs
;
988 } else if (toMove
> 0) {
990 if (sc
> Delta
) sc
= Delta
;
993 nextFadeTime
= ctt
+500.msecs
;
995 } else if (arrowDir
) {
996 hardScrollBy(arrowDir
*6*2);
1001 if (ctt
>= nextFadeTime
) {
1002 if ((newYAlpha
-= 0.1) <= 0) {
1005 nextFadeTime
= ctt
+25.msecs
;
1011 // interference processing
1012 if (ctt
>= nextIfTime
) {
1013 import std
.random
: uniform
;
1014 if (uniform
!"[]"(0, 100) >= 50) { if (addIf()) refresh(); }
1015 nextIfTime
+= (uniform
!"[]"(50, 1500)).msecs
;
1017 if (processIfs()) refresh();
1019 if (shipModel
!is null) {
1021 if (shipAngle
< 359) shipAngle
+= 360;
1025 if (!mouseFadingAway
) {
1026 if (!mouseHidden
&& !mouseHigh
) {
1027 if ((ctt
-lastMMove
).total
!"msecs" > 2500) {
1028 //mouseHidden = true;
1029 mouseFadingAway
= true;
1035 if (somethingVisible
) {
1036 // sadly, to keep framerate we have to redraw each frame, or driver will throw us out of the ship
1037 if (/*needRedraw*/true) sdwindow
.redrawOpenGlSceneNow();
1043 if (newBookFileName
.length
&& formatWorks
== 0) {
1044 doSaveState(true); // forced state save
1047 loadAndFormat(newBookFileName
);
1048 newBookFileName
= null;
1049 sdwindow
.redrawOpenGlSceneNow();
1053 delegate (KeyEvent event
) {
1054 if (sdwindow
.closed
) return;
1055 if (conKeyEvent(event
)) return;
1056 if (event
.key
== Key
.PadEnter
) event
.key
= Key
.Enter
;
1057 if ((event
.modifierState
&ModifierState
.numLock
) == 0) {
1058 switch (event
.key
) {
1059 case Key
.Pad0
: event
.key
= Key
.Insert
; break;
1060 case Key
.PadDot
: event
.key
= Key
.Delete
; break;
1061 case Key
.Pad1
: event
.key
= Key
.End
; break;
1062 case Key
.Pad2
: event
.key
= Key
.Down
; break;
1063 case Key
.Pad3
: event
.key
= Key
.PageDown
; break;
1064 case Key
.Pad4
: event
.key
= Key
.Left
; break;
1065 case Key
.Pad6
: event
.key
= Key
.Right
; break;
1066 case Key
.Pad7
: event
.key
= Key
.Home
; break;
1067 case Key
.Pad8
: event
.key
= Key
.Up
; break;
1068 case Key
.Pad9
: event
.key
= Key
.PageUp
; break;
1069 //case Key.PadEnter: event.key = Key.Enter; break;
1073 if (controlKey(event
)) return;
1074 if (menuKey(event
)) return;
1075 if (readerKey(event
)) return;
1077 delegate (MouseEvent event
) {
1078 if (sdwindow
.closed
) return;
1080 int linkAt (int msx
, int msy
) {
1081 if (laytext
!is null && bookmeta
!is null) {
1082 if (msx
>= startX
&& msx
<= endX
&& msy
>= startY
&& msy
<= endY
) {
1083 auto widx
= laytext
.wordAtXY(msx
-startX
, topY
+msy
-startY
);
1085 //writeln("word at (", msx-startX, ",", msy-startY, "): ", widx);
1086 auto w
= laytext
.wordByIndex(widx
);
1088 //writeln("word #", widx, "; href=", w.style.href);
1089 if (!w
.style
.href
) break;
1090 if (auto hr
= w
.wordNum
in bookmeta
.hrefs
) {
1091 dstring href
= hr
.name
;
1092 if (href
.length
> 1 && href
[0] == '#') {
1094 foreach (const ref id
; bookmeta
.ids
) {
1095 if (id
.name
== href
) {
1101 //writeln("id '", hr.name, "' not found!");
1114 lastMMove
= MonoTime
.currTime
;
1115 if (mouseHidden || mouseFadingAway
) {
1116 mouseHidden
= false;
1117 mouseFadingAway
= false;
1121 if (mouseX
!= event
.x || mouseY
!= event
.y
) {
1126 if (!menuMouse(event
) && !showShip
) {
1127 if (event
.type
== MouseEventType
.buttonPressed
) {
1128 switch (event
.button
) {
1129 case MouseButton
.wheelUp
: hardScrollBy(-42); break;
1130 case MouseButton
.wheelDown
: hardScrollBy(42); break;
1131 case MouseButton
.left
:
1132 auto wid
= linkAt(event
.x
, event
.y
);
1138 case MouseButton
.right
:
1145 mouseHigh
= (linkAt(event
.x
, event
.y
) >= 0);
1147 delegate (dchar ch
) {
1148 if (sdwindow
.closed
) return;
1149 if (conCharEvent(ch
)) return;
1150 if (ch
== '`') { concmd("r_console tan"); return; }
1155 childTid
.send(QuitWork());
1156 while (formatWorks
>= 0) processThreads();
1160 // ////////////////////////////////////////////////////////////////////////// //
1161 void main (string
[] args
) {
1164 universe
= Galaxy(0);
1166 if (args
.length
== 1) {
1169 foreach (string line
; VFile(buildPath(RcDir
, ".lastfile")).byLineCopy
) {
1170 if (!line
.isComment
) lnn
= line
;
1172 if (lnn
.length
) args
~= lnn
;
1173 } catch (Exception
) {}
1175 if (args
.length
== 1) assert(0, "no filename");