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
;
49 //version = debug_draw;
52 // ////////////////////////////////////////////////////////////////////////// //
53 void run (string bookFileName
) {
57 bool fpsVisible
= false;
59 string newBookFileName
;
61 if (GWidth
< MinWinWidth
) GWidth
= MinWinWidth
;
62 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
;
81 float dt = 0, secs
= 0;
83 int textHeight
= GHeight
-8;
84 bool doSaveCheck
= false;
85 MonoTime nextSaveTime
;
87 MonoTime nextIfTime
= MonoTime
.currTime
;
89 BookInfo
[] recentFiles
;
90 BookMetadata bookmeta
;
92 int toMove
= 0; // for smooth scroller
96 int newYLine
= -1; // index
98 bool newYFade
= false;
99 MonoTime nextFadeTime
;
101 int curImg
= -1, curImgWhite
= -1;
102 int mouseX
= -666, mouseY
= -666;
103 bool mouseHigh
= false;
104 bool mouseHidden
= false;
105 auto lastMMove
= MonoTime
.currTime
;
107 bool needRedrawFlag
= true;
109 bool firstFormat
= true;
111 bool inGalaxyMap
= false;
113 void delegate (int item
) onPopupSelect
;
114 int shipModelIndex
= 0;
115 bool popupNoShipKill
= false;
117 void refresh () { needRedrawFlag
= true; }
118 void refreshed () { needRedrawFlag
= false; }
121 return (needRedrawFlag ||
(currPopup
!is null && currPopup
.needRedraw
));
124 @property bool inMenu () { return (currPopup
!is null); }
125 void closeMenu () { if (currPopup
!is null) currPopup
.destroy
; currPopup
= null; onPopupSelect
= null; }
127 auto childTid
= spawn(&reformatThreadFn
, thisTid
);
128 childTid
.setMaxMailboxSize(128, OnCrowding
.block
);
129 thisTid
.setMaxMailboxSize(128, OnCrowding
.block
);
131 void setShip (int idx
) {
132 if (eliteShipFiles
.length
) {
133 import core
.memory
: GC
;
134 if (idx
>= eliteShipFiles
.length
) idx
= cast(int)eliteShipFiles
.length
-1;
135 if (idx
< 0) idx
= 0;
136 if (shipModelIndex
== idx
&& shipModel
!is null) return;
138 shipModelIndex
= idx
;
139 if (shipModel
!is null) {
140 shipModel
.glUnload();
141 shipModel
.freeData();
148 shipModel
= new EliteModel(eliteShipFiles
[shipModelIndex
]);
149 shipModel
.glUpload();
150 shipModel
.freeImages();
151 } catch (Exception e
) {}
152 //shipModel = eliteShips[shipModelIndex];
157 void ensureShipModel () {
158 if (eliteShipFiles
.length
&& shipModel
is null) {
159 import std
.random
: uniform
;
160 setShip(uniform
!"[)"(0, eliteShipFiles
.length
));
164 void freeShipModel () {
165 if (!showShip
&& !inMenu
&& shipModel
!is null) {
166 import core
.memory
: GC
;
167 shipModel
.glUnload();
168 shipModel
.freeData();
175 void doSaveState (bool forced
=false) {
177 if (formatWorks
!= 0) return;
178 if (!doSaveCheck
) return;
179 auto ct
= MonoTime
.currTime
;
180 if (ct
< nextSaveTime
) return;
182 if (laytext
is null || laytext
.lineCount
== 0 || formatWorks
!= 0) return;
185 auto fo
= VFile(stateFileName
, "w");
186 if (laytext
!is null && laytext
.lineCount
) {
187 auto lnum
= laytext
.findLineAtY(topY
);
188 if (lnum
>= 0) fo
.writeln("wordindex=", laytext
.line(lnum
).wstart
);
190 } catch (Exception
) {}
194 void stateChanged () {
197 nextSaveTime
= MonoTime
.currTime
+10.seconds
;
201 void hardScrollBy (int delta
) {
202 if (delta
== 0 || laytext
is null || laytext
.lineCount
== 0) return;
205 if (topY
>= laytext
.textHeight
-textHeight
) topY
= cast(int)laytext
.textHeight
-textHeight
;
206 if (topY
< 0) topY
= 0;
213 void scrollBy (int delta
) {
214 if (delta
== 0 || laytext
is null || laytext
.lineCount
== 0) return;
219 // scrolling up, mark top line
220 newYLine
= laytext
.findLineAtY(topY
);
222 // scrolling down, mark bottom line
223 newYLine
= laytext
.findLineAtY(topY
+textHeight
-2);
227 writeln("scrollBy: delta=", delta
, "; newYLine=", newYLine
, "; topLine=", laytext
.findLineAtY(topY
));
229 if (newYLine
< 0) newYFade
= false;
233 if (laytext
is null) return;
241 void goTo (uint widx
) {
242 if (laytext
is null) return;
243 auto lidx
= laytext
.findLineWithWord(widx
);
245 assert(lidx
< laytext
.lineCount
);
247 if (topY
!= laytext
.line(lidx
).y
) {
248 topY
= laytext
.line(lidx
).y
;
254 writeln("goto: widx=", widx
, "; lidx=", lidx
, "; fwn=", laytext
.line(lidx
).wstart
, "; topY=", topY
);
255 auto lnum
= laytext
.findLineAtY(topY
);
256 writeln(" newlnum=", lnum
, "; lidx.y=", laytext
.line(lidx
).y
, "; lnum.y=", laytext
.line(lnum
).y
);
264 xiniParse(VFile(stateFileName
),
267 if (widx
>= 0) goTo(widx
);
268 } catch (Exception
) {}
271 void loadAndFormat (string filename
) {
272 assert(formatWorks
<= 0);
276 //formatWorks = -1; //FIXME
282 //sdwindow.redrawOpenGlSceneNow();
283 //booktext = loadBook(newBookFileName);
284 //newBookFileName = null;
286 //if (formatWorks < 0) formatWorks = 1; else ++formatWorks;
288 //writeln("*** loading new book: '", filename, "'");
289 childTid
.send(ReformatWork(null, filename
, GWidth
, GHeight
));
294 if (formatWorks
< 0) formatWorks
= 1; else ++formatWorks
;
295 childTid
.send(ReformatWork(cast(shared)booktext
, null, GWidth
, GHeight
));
299 void formatComplete (ref ReformatWorkComplete w
) {
300 scope(exit
) { import core
.memory
: GC
; GC
.collect(); GC
.minimize(); }
302 auto lt
= cast(LayText
)w
.laytext
;
303 scope(exit
) if (lt
) lt
.freeMemory();
306 BookMetadata meta
= cast(BookMetadata
)w
.meta
;
309 BookText
bt = cast(BookText
)w
.booktext
;
311 if (bt !is booktext
) {
315 sdwindow
.title
= booktext
.title
~" \xe2\x80\x94 "~booktext
.authorFirst
~" "~booktext
.authorLast
;
316 } else if (bookmeta
is null) {
320 if (doQuit || formatWorks
<= 0) return;
324 if (formatWorks
== 0) reformat();
328 if (formatWorks
!= 0) return;
332 if (!firstFormat
&& laytext
!is null && laytext
.lineCount
) {
333 auto lidx
= laytext
.findLineAtY(topY
);
334 if (lidx
>= 0) widx
= laytext
.line(lidx
).wstart
;
336 if (laytext
!is null) laytext
.freeMemory();
350 void pushPosition () {
351 if (laytext
is null || laytext
.lineCount
== 0) return;
352 auto lidx
= laytext
.findLineAtY(topY
);
353 if (lidx
>= 0) posstack
~= laytext
.line(lidx
).wstart
;
356 void popPosition () {
357 if (posstack
.length
== 0) return;
358 auto widx
= posstack
[$-1];
359 posstack
.length
-= 1;
360 posstack
.assumeSafeAppend
;
364 void gotoSection (int sn
) {
365 if (laytext
is null || laytext
.lineCount
== 0 || bookmeta
is null) return;
366 if (sn
< 0 || sn
>= bookmeta
.sections
.length
) return;
367 auto lidx
= laytext
.findLineWithWord(bookmeta
.sections
[sn
].wordidx
);
369 auto newY
= laytext
.line(lidx
).y
;
378 version(X11
) sdwindow
.closeQuery
= delegate () { doQuit
= true; };
380 void closeWindow () {
381 doSaveState(true); // forced state save
382 if (!sdwindow
.closed
&& vg
!is null) {
383 if (curImg
>= 0) { vg
.deleteImage(curImg
); curImg
= -1; }
384 if (curImgWhite
>= 0) { vg
.deleteImage(curImgWhite
); curImgWhite
= -1; }
392 sdwindow
.visibleForTheFirstTime
= delegate () {
393 sdwindow
.setAsCurrentOpenGlContext(); // make this window active
394 sdwindow
.vsync
= false;
395 //sdwindow.useGLFinish = false;
396 //glbindLoadFunctions();
399 uint flags
= NVG_DEBUG
;
400 if (flagNanoAA
) flags |
= NVG_ANTIALIAS
;
401 if (flagNanoSS
) flags |
= NVG_STENCIL_STROKES
;
402 vg
= createGL2NVG(flags
);
405 writeln("Could not init nanovg.");
410 curImg
= createCursorImage(vg
);
411 curImgWhite
= createCursorImage(vg
, true);
412 fps
= new PerfGraph("Frame Time", PerfGraph
.Style
.FPS
, "ui");
413 } catch (Exception e
) {
414 import std
.stdio
: stderr
;
415 stderr
.writeln("ERROR: ", e
.msg
);
423 sdwindow
.redrawOpenGlScene();
427 void relayout (bool forced
=false) {
428 if (laytext
!is null) {
430 auto lidx
= laytext
.findLineAtY(topY
);
431 if (lidx
>= 0) widx
= laytext
.line(lidx
).wstart
;
432 int maxWidth
= GWidth
-4-2-BND_SCROLLBAR_WIDTH
-2;
433 //if (maxWidth < MinWinWidth) maxWidth = MinWinWidth;
436 auto stt
= MonoTime
.currTime
;
437 laytext
.relayout(maxWidth
, forced
);
438 auto ett
= MonoTime
.currTime
-stt
;
439 writeln("relayouted in ", ett
.total
!"msecs", " milliseconds; lines:", laytext
.lineCount
, "; words:", laytext
.nextWordIndex
);
446 sdwindow
.windowResized
= delegate (int w
, int h
) {
447 //writeln("w=", w, "; h=", h);
448 //if (w < MinWinWidth) w = MinWinWidth;
449 //if (h < MinWinHeight) h = MinWinHeight;
450 glViewport(0, 0, w
, h
);
453 textHeight
= GHeight
-8;
458 void drawShipName () {
459 if (shipModel
is null || shipModel
.name
.length
== 0) return;
460 vg
.fontFaceId(uiFont
);
461 vg
.textAlign(NVGTextAlign
.H
.Left
, NVGTextAlign
.V
.Baseline
);
462 vg
.fontSize(fsizeUI
);
463 auto w
= vg
.bndLabelWidth(-1, shipModel
.name
)+8;
464 float h
= BND_WIDGET_HEIGHT
+8;
465 float mx
= (GWidth
-w
)/2.0;
466 float my
= (GHeight
-h
)-8;
467 vg
.bndMenuBackground(mx
, my
, w
, h
, BND_CORNER_NONE
);
468 vg
.bndMenuItem(mx
+4, my
+4, w
-8, BND_WIDGET_HEIGHT
, BND_DEFAULT
, -1, shipModel
.name
);
469 if (shipModel
.dispName
&& shipModel
.dispName
!= shipModel
.name
) {
470 my
-= BND_WIDGET_HEIGHT
+16;
471 w
= vg
.bndLabelWidth(-1, shipModel
.dispName
)+8;
473 vg
.bndMenuBackground(mx
, my
, w
, h
, BND_CORNER_NONE
);
474 vg
.bndMenuItem(mx
+4, my
+4, w
-8, BND_WIDGET_HEIGHT
, BND_DEFAULT
, -1, shipModel
.dispName
);
478 void createSectionMenu () {
480 //writeln("lc=", laytext.lineCount, "; bm: ", (bookmeta !is null));
481 if (laytext
is null || laytext
.lineCount
== 0 || bookmeta
is null) { freeShipModel(); return; }
482 currPopup
= new PopupMenu(vg
, "Sections"d
, () {
484 foreach (const ref sc
; bookmeta
.sections
) items
~= sc
.name
;
487 //writeln(currPopup.items.length);
488 // find current section
489 currPopup
.curItemIdx
= 0;
490 auto lidx
= laytext
.findLineAtY(topY
);
491 if (lidx
>= 0 && bookmeta
.sections
.length
> 0) {
492 foreach (immutable sidx
, const ref sc
; bookmeta
.sections
) {
493 auto sline
= laytext
.findLineWithWord(sc
.wordidx
);
494 if (sline
>= 0 && lidx
>= sline
) currPopup
.curItemIdx
= cast(int)sidx
;
497 onPopupSelect
= (int item
) { gotoSection(item
); };
500 void createQuitMenu (bool wantYes
) {
502 currPopup
= new PopupMenu(vg
, "Quit?"d
, () {
503 return ["Yes"d
, "No"d
];
505 currPopup
.curItemIdx
= (wantYes ?
0 : 1);
506 onPopupSelect
= (int item
) { if (item
== 0) doQuit
= true; };
509 void createRecentMenu () {
511 if (recentFiles
.length
== 0) recentFiles
= loadDetailedHistory();
512 if (recentFiles
.length
== 0) { freeShipModel(); return; }
513 currPopup
= new PopupMenu(vg
, "Recent files"d
, () {
514 import std
.conv
: to
;
516 foreach (const ref BookInfo bi
; recentFiles
) {
518 if (bi
.seqname
.length
) {
520 if (bi
.seqnum
) { s
~= to
!string(bi
.seqnum
); s
~= ": "; }
521 //writeln(bi.seqname);
525 if (bi
.author
.length
) { s
~= " \xe2\x80\x94 "; s
~= bi
.author
; }
530 currPopup
.curItemIdx
= cast(int)recentFiles
.length
-1;
531 onPopupSelect
= (int item
) {
532 newBookFileName
= recentFiles
[item
].diskfile
;
533 popupNoShipKill
= true;
537 bool menuKey (KeyEvent event
) {
538 if (formatWorks
!= 0) return false;
539 if (!inMenu
) return false;
540 if (inGalaxyMap
) return false;
541 if (!event
.pressed
) return false;
542 auto res
= currPopup
.onKey(event
);
543 if (res
== PopupMenu
.Close
) {
547 } else if (res
>= 0) {
548 if (onPopupSelect
!is null) onPopupSelect(res
);
550 if (popupNoShipKill
) popupNoShipKill
= false; else freeShipModel();
556 bool menuMouse (MouseEvent event
) {
557 if (formatWorks
!= 0) return false;
558 if (!inMenu
) return false;
559 if (inGalaxyMap
) return false;
560 auto res
= currPopup
.onMouse(event
);
561 if (res
== PopupMenu
.Close
) {
565 } else if (res
>= 0) {
566 if (onPopupSelect
!is null) onPopupSelect(res
);
568 if (popupNoShipKill
) popupNoShipKill
= false; else freeShipModel();
574 bool readerKey (KeyEvent event
) {
575 if (formatWorks
!= 0) return false;
576 if (!event
.pressed
) {
578 case Key
.Up
: arrowDir
= 0; return true;
579 case Key
.Down
: arrowDir
= 0; return true;
586 if (event
.modifierState
&ModifierState
.shift
) {
587 //goto case Key.PageUp;
588 hardScrollBy(toMove
); toMove
= 0;
589 scrollBy(-textHeight
/3*2);
591 //goto case Key.PageDown;
592 hardScrollBy(toMove
); toMove
= 0;
593 scrollBy(textHeight
/3*2);
600 hardScrollBy(toMove
); toMove
= 0;
601 hardScrollBy(-(textHeight
> 32 ? textHeight
-32 : textHeight
));
604 hardScrollBy(toMove
); toMove
= 0;
605 hardScrollBy(textHeight
> 32 ? textHeight
-32 : textHeight
);
623 bool controlKey (KeyEvent event
) {
624 if (!event
.pressed
) return false;
627 if (inGalaxyMap
) { inGalaxyMap
= false; refresh(); return true; }
628 if (inMenu
) { closeMenu(); freeShipModel(); refresh(); return true; }
629 if (showShip
) { showShip
= false; freeShipModel(); refresh(); return true; }
631 createQuitMenu(true);
634 case Key
.P
: if (event
.modifierState
== ModifierState
.ctrl
) { fpsVisible
= !fpsVisible
; refresh(); return true; } break;
635 case Key
.I
: if (event
.modifierState
== ModifierState
.ctrl
) { interAllowed
= !interAllowed
; refresh(); return true; } break;
636 case Key
.N
: if (event
.modifierState
== ModifierState
.ctrl
) { sbLeft
= !sbLeft
; refresh(); return true; } break;
637 case Key
.C
: if (event
.modifierState
== ModifierState
.ctrl
) { if (addIf()) refresh(); return true; } break;
639 if (event
.modifierState
== ModifierState
.ctrl
&& eliteShipFiles
.length
> 0) {
641 showShip
= !showShip
;
642 if (showShip
) ensureShipModel(); else freeShipModel();
648 case Key
.Q
: if (event
.modifierState
== ModifierState
.ctrl
) { doQuit
= true; return true; } break;
649 case Key
.B
: if (formatWorks
== 0 && !inMenu
&& !inGalaxyMap
&& event
.modifierState
== ModifierState
.ctrl
) { pushPosition(); return true; } break;
651 if (formatWorks
== 0 && !showShip
&& !inMenu
&& !inGalaxyMap
) {
658 if (formatWorks
== 0 && event
.modifierState
== ModifierState
.alt
) {
665 if (!inMenu
&& !showShip
) {
666 inGalaxyMap
= !inGalaxyMap
;
671 if (formatWorks
== 0 && event
.modifierState
== ModifierState
.ctrl
) relayout(true);
673 case Key
.Home
: if (showShip
) { setShip(0); return true; } break;
674 case Key
.End
: if (showShip
) { setShip(cast(int)eliteShipFiles
.length
); return true; } break;
675 case Key
.Up
: case Key
.Left
: if (showShip
) { setShip(shipModelIndex
-1); return true; } break;
676 case Key
.Down
: case Key
.Right
: if (showShip
) { setShip(shipModelIndex
+1); return true; } break;
682 static int startX () { pragma(inline
, true); return (sbLeft ?
2+BND_SCROLLBAR_WIDTH
+2 : 4); }
683 static int endX () { pragma(inline
, true); return startX
+GWidth
-4-2-BND_SCROLLBAR_WIDTH
-2-1; }
685 int startY () { pragma(inline
, true); return (GHeight
-textHeight
)/2; }
686 int endY () { pragma(inline
, true); return startY
+textHeight
-1; }
688 sdwindow
.redrawOpenGlScene
= delegate () {
692 curt
= MonoTime
.currTime
;
693 secs
= cast(double)((curt
-stt
).total
!"msecs")/1000.0;
694 dt = cast(double)((curt
-prevt
).total
!"msecs")/1000.0;
696 //glClearColor(0, 0, 0, 0);
697 //glClearColor(0.18, 0.18, 0.18, 0);
698 glClearColor(colorBack
.r
, colorBack
.g
, colorBack
.b
, 0);
699 glClear(glNVGClearFlags|EliteModel
.glClearFlags
);
702 needRedrawFlag
= (fps
!is null && fpsVisible
);
703 if (fps
!is null) fps
.update(dt);
704 if (vg
is null) return;
706 scope(exit
) vg
.releaseImages();
707 vg
.beginFrame(GWidth
, GHeight
, 1);
711 float curHeight
= (laytext
!is null ? topY
: 0);
712 float th
= (laytext
!is null ? laytext
.textHeight
-textHeight
: 0);
713 if (th
<= 0) { curHeight
= 0; th
= 1; }
714 float sz
= cast(float)(GHeight
-4)/th
;
715 if (sz
> 1) sz
= 1; else if (sz
< 0.05) sz
= 0.05;
716 int sx
= (sbLeft ?
2 : GWidth
-BND_SCROLLBAR_WIDTH
-2);
717 vg
.bndScrollBar(sx
, 2, BND_SCROLLBAR_WIDTH
, GHeight
-4, BND_DEFAULT
, curHeight
/th
, sz
);
719 if (laytext
is null) {
720 if (shipModel
is null) {
722 vg
.fillColor(colorText
);
723 int drawY
= (GHeight
-textHeight
)/2;
724 vg
.intersectScissor((sbLeft ?
2+BND_SCROLLBAR_WIDTH
+2 : 4), drawY
, GWidth
-4-2-BND_SCROLLBAR_WIDTH
-2, textHeight
);
725 vg
.fontFaceId(textFont
);
726 vg
.fontSize(fsizeText
);
727 vg
.textAlign(NVGTextAlign
.H
.Center
, NVGTextAlign
.V
.Middle
);
728 vg
.text(GWidth
/2, GHeight
/2, "REFORMATTING");
733 if (laytext
!is null && laytext
.lineCount
) {
735 vg
.fillColor(colorText
);
737 vg
.intersectScissor((sbLeft ?
2+BND_SCROLLBAR_WIDTH
+2 : 4), drawY
, GWidth
-4-2-BND_SCROLLBAR_WIDTH
-2, textHeight
);
738 //FIXME: not GHeight!
739 int lidx
= laytext
.findLineAtY(topY
);
740 if (lidx
>= 0 && lidx
< laytext
.lineCount
) {
741 drawY
-= topY
-laytext
.line(lidx
).y
;
742 vg
.textAlign(NVGTextAlign
.H
.Left
, NVGTextAlign
.V
.Baseline
);
743 LayFontStyle lastStyle
;
745 while (lidx
< laytext
.lineCount
&& drawY
< GHeight
) {
746 auto ln
= laytext
.line(lidx
);
747 foreach (ref LayWord w
; laytext
.lineWords(lidx
)) {
748 if (lastStyle
!= w
.style
) {
749 if (w
.style
.fontface
!= lastStyle
.fontface
) vg
.fontFace(laytext
.fontFace(w
.style
.fontface
));
750 vg
.fontSize(w
.style
.fontsize
);
751 auto c
= NVGColor(w
.style
.color
);
757 if (newYFade
&& newYLine
== lidx
) vg
.fillColor(nvgLerpRGBA(colorText
, colorTextHi
, newYAlpha
));
758 auto oid
= w
.objectIdx
;
761 laytext
.objects
[oid
].draw(vg
, startx
+w
.x
, drawY
+ln
.h
+ln
.desc
);
764 vg
.text(startx
+w
.x
, drawY
+ln
.h
+ln
.desc
, laytext
.wordText(w
));
766 //TODO: draw lines over whitespace
767 if (lastStyle
.underline
) vg
.rect(startx
+w
.x
, drawY
+ln
.h
+ln
.desc
+1, w
.w
, 1);
768 if (lastStyle
.strike
) vg
.rect(startx
+w
.x
, drawY
+ln
.h
+ln
.desc
-w
.asc
/2, w
.w
, 1);
769 if (lastStyle
.overline
) vg
.rect(startx
+w
.x
, drawY
+ln
.h
+ln
.desc
-w
.asc
-1, w
.w
, 1);
770 version(debug_draw
) {
774 vg
.strokeColor(nvgRGB(0, 0, 255));
775 vg
.rect(startx
+w
.x
, drawY
, w
.w
, w
.h
);
779 if (newYFade
&& newYLine
== lidx
) vg
.fillColor(NVGColor(lastStyle
.color
));
789 if (colorDim
.a
!= 1) {
790 //vg.scissor(0, 0, GWidth, GHeight);
793 vg
.fillColor(colorDim
);
794 vg
.rect(0, 0, GWidth
, GHeight
);
798 // dim more if menu is active
799 if (inMenu || showShip || formatWorks
!= 0) {
800 //vg.scissor(0, 0, GWidth, GHeight);
803 //vg.globalAlpha(0.5);
804 vg
.fillColor(nvgRGBA(0, 0, 0, (showShip || formatWorks
!= 0 ?
127 : 64)));
805 vg
.rect(0, 0, GWidth
, GHeight
);
808 if (shipModel
!is null) {
810 float zz
= shipModel
.bbox
[1].z
-shipModel
.bbox
[0].z
;
824 drawModel(shipAngle
, shipModel
);
825 vg
.beginFrame(GWidth
, GHeight
, 1);
829 if (formatWorks
== 0) {
831 //vg.beginFrame(GWidth, GHeight, 1);
832 //vg.scissor(0, 0, GWidth, GHeight);
838 if (fps
!is null && fpsVisible
) {
839 //vg.beginFrame(GWidth, GHeight, 1);
840 //vg.scissor(0, 0, GWidth, GHeight);
842 fps
.render(vg
, GWidth
-fps
.width
-4, GHeight
-fps
.height
-4);
845 if (inGalaxyMap
) drawGalaxy(vg
);
847 if (curImg
>= 0 && !mouseHidden
) {
849 //vg.beginFrame(GWidth, GHeight, 1);
851 //vg.scissor(0, 0, GWidth, GHeight);
853 vg
.imageSize(curImg
, &w
, &h
);
855 vg
.fillPaint(vg
.imagePattern(mouseX
, mouseY
, w
, h
, 0, curImg
, 1));
857 vg
.fillPaint(vg
.imagePattern(mouseX
, mouseY
, w
, h
, 0, curImgWhite
, 1));
859 vg
.rect(mouseX
, mouseY
, w
, h
);
864 vg.fillPaint(vg.imagePattern(mouseX, mouseY, w, h, 0, curImgWhite, 0.4));
865 vg.rect(mouseX, mouseY, w, h);
874 void processThreads () {
875 ReformatWorkComplete wd
;
877 bool workDone
= false;
878 auto res
= receiveTimeout(Duration
.zero
,
882 (ReformatWorkComplete w
) {
887 if (!res
) { assert(!workDone
); break; }
888 if (workDone
) { workDone
= false; formatComplete(wd
); }
892 sdwindow
.eventLoop(1000/35,
895 if (sdwindow
.closed
) return;
896 if (doQuit
) { closeWindow(); return; }
897 auto ctt
= MonoTime
.currTime
;
899 if (formatWorks
== 0) {
903 if (sc
> Delta
) sc
= Delta
;
906 nextFadeTime
= ctt
+500.msecs
;
907 } else if (toMove
> 0) {
909 if (sc
> Delta
) sc
= Delta
;
912 nextFadeTime
= ctt
+500.msecs
;
913 } else if (arrowDir
) {
914 hardScrollBy(arrowDir
*6*2);
918 if (ctt
>= nextFadeTime
) {
919 if ((newYAlpha
-= 0.1) <= 0) {
922 nextFadeTime
= ctt
+25.msecs
;
928 // interference processing
929 if (ctt
>= nextIfTime
) {
930 import std
.random
: uniform
;
931 if (uniform
!"[]"(0, 100) >= 50) { if (addIf()) refresh(); }
932 nextIfTime
+= (uniform
!"[]"(50, 1500)).msecs
;
934 if (processIfs()) refresh();
936 if (shipModel
!is null) {
938 if (shipAngle
< 359) shipAngle
+= 360;
942 if (!mouseHidden
&& !mouseHigh
) {
943 if ((ctt
-lastMMove
).total
!"msecs" > 2500) {
948 if (needRedraw
) sdwindow
.redrawOpenGlSceneNow();
951 if (newBookFileName
.length
&& formatWorks
== 0) {
952 doSaveState(true); // forced state save
955 loadAndFormat(newBookFileName
);
956 newBookFileName
= null;
957 sdwindow
.redrawOpenGlSceneNow();
961 delegate (KeyEvent event
) {
962 if (sdwindow
.closed
) return;
963 if (event
.key
== Key
.PadEnter
) event
.key
= Key
.Enter
;
964 if ((event
.modifierState
&ModifierState
.numLock
) == 0) {
966 case Key
.Pad0
: event
.key
= Key
.Insert
; break;
967 case Key
.PadDot
: event
.key
= Key
.Delete
; break;
968 case Key
.Pad1
: event
.key
= Key
.End
; break;
969 case Key
.Pad2
: event
.key
= Key
.Down
; break;
970 case Key
.Pad3
: event
.key
= Key
.PageDown
; break;
971 case Key
.Pad4
: event
.key
= Key
.Left
; break;
972 case Key
.Pad6
: event
.key
= Key
.Right
; break;
973 case Key
.Pad7
: event
.key
= Key
.Home
; break;
974 case Key
.Pad8
: event
.key
= Key
.Up
; break;
975 case Key
.Pad9
: event
.key
= Key
.PageUp
; break;
976 //case Key.PadEnter: event.key = Key.Enter; break;
980 if (controlKey(event
)) return;
981 if (menuKey(event
)) return;
982 if (readerKey(event
)) return;
984 delegate (MouseEvent event
) {
985 int linkAt (int msx
, int msy
) {
986 if (laytext
!is null && bookmeta
!is null) {
987 if (msx
>= startX
&& msx
<= endX
&& msy
>= startY
&& msy
<= endY
) {
988 auto widx
= laytext
.wordAtXY(msx
-startX
, topY
+msy
-startY
);
990 //writeln("word at (", msx-startX, ",", msy-startY, "): ", widx);
991 auto w
= laytext
.wordByIndex(widx
);
993 //writeln("word #", widx, "; href=", w.style.href);
994 if (!w
.style
.href
) break;
995 if (auto hr
= w
.wordNum
in bookmeta
.hrefs
) {
996 dstring href
= hr
.name
;
997 if (href
.length
> 1 && href
[0] == '#') {
999 foreach (const ref id
; bookmeta
.ids
) {
1000 if (id
.name
== href
) {
1006 //writeln("id '", hr.name, "' not found!");
1019 lastMMove
= MonoTime
.currTime
;
1021 mouseHidden
= false;
1024 if (mouseX
!= event
.x || mouseY
!= event
.y
) {
1029 if (!menuMouse(event
) && !showShip
) {
1030 if (event
.type
== MouseEventType
.buttonPressed
) {
1031 switch (event
.button
) {
1032 case MouseButton
.wheelUp
: hardScrollBy(-42); break;
1033 case MouseButton
.wheelDown
: hardScrollBy(42); break;
1034 case MouseButton
.left
:
1035 auto wid
= linkAt(event
.x
, event
.y
);
1041 case MouseButton
.right
:
1048 mouseHigh
= (linkAt(event
.x
, event
.y
) >= 0);
1050 delegate (dchar ch
) {
1051 //if (ch == 'q') { doQuit = true; return; }
1056 childTid
.send(QuitWork());
1057 while (formatWorks
>= 0) processThreads();
1061 // ////////////////////////////////////////////////////////////////////////// //
1062 void main (string
[] args
) {
1065 universe
= Galaxy(0);
1067 if (args
.length
== 1) {
1070 foreach (string line
; VFile(buildPath(RcDir
, ".lastfile")).byLineCopy
) {
1071 if (!line
.isComment
) lnn
= line
;
1073 if (lnn
.length
) args
~= lnn
;
1074 } catch (Exception
) {}
1076 if (args
.length
== 1) assert(0, "no filename");