(experimental) it is now possible to select navigation link with keyboard
[xreader.git] / ewbillboard.d
blobaaba3162ceae988f90279fdd83b43b1d57329efa
1 /* Written by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
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/>.
17 module ewbillboard;
19 import iv.cmdcon;
20 import iv.nanovega;
21 import iv.nanovega.blendish;
22 //import arsd.color;
24 import xreadercfg;
26 import eworld;
29 // ////////////////////////////////////////////////////////////////////////// //
30 __gshared Galaxy universe;
33 // ////////////////////////////////////////////////////////////////////////// //
34 final class PlanetInfoBillboard {
35 private:
36 string[2][] lines;
37 float[] lineH; // line height
38 float[2] ltblW = 0;
40 string pdsc;
41 float pdscH;
43 string[3][] market;
44 float[] mlineH;
45 float[3] mtblW = 0; // market widthes
47 float billH = 0, billW = 0;
48 NVGContext vg;
50 public:
51 this (NVGContext avg, ref Galaxy.Planet p) {
52 assert(avg !is null);
53 vg = avg;
54 scope(exit) vg = null;
55 vg.fontFaceId(galmapFont);
56 vg.textAlign(NVGTextAlign.H.Left, NVGTextAlign.V.Baseline);
57 vg.fontSize(fGalMapSize);
58 vg.textAlign(NVGTextAlign.H.Left, NVGTextAlign.V.Top);
59 buildPlanetData(p);
62 void draw (NVGContext avg, float mx=10, float my=10) {
63 if (avg is null) return;
64 vg = avg;
65 scope(exit) vg = null;
67 vg.save();
68 scope(exit) vg.restore;
70 auto obfn = bndGetFont();
71 scope(exit) bndSetFont(obfn);
73 vg.resetTransform();
74 vg.resetScissor;
76 version(none) {
77 vg.fontFaceId(uiFont);
78 vg.textAlign(NVGTextAlign.H.Left, NVGTextAlign.V.Top);
79 vg.fontSize(16);
80 float[4] b = void;
81 float adv;
82 adv = vg.textBounds(0, 0, "Woo", b[]);
83 conwriteln("adv=", adv, "; b=", b[]);
86 bndSetFont(galmapFont);
87 vg.fontFaceId(galmapFont);
88 vg.textAlign(NVGTextAlign.H.Left, NVGTextAlign.V.Baseline);
89 vg.fontSize(fGalMapSize);
91 vg.bndMenuBackground(mx, my, billW+8, billH+8, BND_CORNER_NONE);
92 vg.beginPath();
93 float y = my+4;
94 // planet info
95 foreach (immutable idx; 0..lines.length) {
96 float x = mx+4;
97 vg.fillColor(nvgRGBA(255, 255, 255));
98 vg.textAlign(NVGTextAlign.H.Right, NVGTextAlign.V.Top);
99 x += ltblW[0];
100 vg.text(x, y, lines[idx][0]);
101 x += 4;
102 vg.fillColor(nvgRGBA(255, 255, 0));
103 vg.textAlign(NVGTextAlign.H.Left, NVGTextAlign.V.Top);
104 vg.text(x, y, lines[idx][1]);
105 y += lineH[idx];
107 // planet descrition
108 vg.fillColor(nvgRGBA(255, 127, 0));
109 vg.textAlign(NVGTextAlign.H.Left, NVGTextAlign.V.Top);
110 y += 8;
111 vg.text(mx+4, y, pdsc);
112 y += pdscH+4;
113 // market data
114 vg.fillColor(nvgRGBA(255, 255, 255));
115 foreach (immutable idx; 0..market.length) {
116 float x = mx+4;
117 vg.textAlign(NVGTextAlign.H.Left, NVGTextAlign.V.Top);
118 vg.text(x, y, market[idx][0]);
119 x += mtblW[0]+8+mtblW[1];
120 vg.textAlign(NVGTextAlign.H.Right, NVGTextAlign.V.Top);
121 vg.text(x, y, market[idx][1]);
122 x += mtblW[2]+8;
123 vg.text(x, y, market[idx][2]);
124 vg.textAlign(NVGTextAlign.H.Left, NVGTextAlign.V.Top);
125 x += 1;
126 vg.text(x, y, Galaxy.good(idx).unitName);
127 y += mlineH[idx];
129 vg.fill();
132 private:
133 void putf(A...) (string label, string fmt, in A args) {
134 import std.algorithm : max;
135 import std.string : format;
136 auto s = format(fmt, args);
137 lines.length += 1;
138 lines[$-1][0] = label;
139 lines[$-1][1] = s;
140 float[4] b = void;
141 // label
142 vg.textBounds(0, 0, label, b[]);
143 float hh = b[3]-b[1];
144 float ww = b[2]-b[0];
145 ltblW[0] = max(ltblW[0], ww);
146 // text
147 vg.textBounds(0, 0, s, b[]);
148 hh = max(hh, b[3]-b[1]);
149 ww = b[2]-b[0];
150 ltblW[1] = max(ltblW[1], ww);
151 // totals
152 lineH ~= hh;
153 billH += hh;
154 billW = max(billW, ltblW[0]+4+ltblW[1]);
157 void putd (string dsc) {
158 import std.algorithm : max;
159 pdsc = dsc;
160 float[4] b = void;
161 vg.textBounds(0, 0, dsc, b[]);
162 float hh = b[3]-b[1];
163 float ww = b[2]-b[0];
164 // totals
165 pdscH = hh;
166 billH += hh;
167 billW = max(billW, ww);
170 void putm(T : ulong) (in ref Galaxy.Planet p, T idx) {
171 import std.algorithm : max;
172 import std.string : format;
173 if (idx < 0 || idx > Galaxy.Goods.max) return;
174 auto good = Galaxy.good(idx);
175 market.length += 1;
176 market[$-1][0] = good.name;
177 market[$-1][1] = "%s.%s".format(p.price[idx]/10, p.price[idx]%10);
178 market[$-1][2] = "%s".format(p.quantity[idx]);
179 float[4] b = void;
180 float hh = 0, wt = 8*2;
181 foreach (immutable n, string s; market[$-1]) {
182 vg.textBounds(0, 0, s, b[]);
183 hh = max(hh, b[3]-b[1]);
184 mtblW[n] = max(mtblW[n], b[2]-b[0]);
185 wt += mtblW[n];
187 vg.textBounds(0, 0, "kg", b[]);
188 hh = max(hh, b[3]-b[1]);
189 wt += b[2]-b[0]+2;
190 mlineH ~= hh;
191 billH += hh;
192 billW = max(billW, wt);
195 void buildPlanetData (ref Galaxy.Planet p) {
196 putf("System:", "%s", p.name);
197 putf("Position:", "(%s,%s)", p.x, p.y);
198 putf("Economy:", "%s", p.economyName);
199 putf("Government:", "%s", p.govName);
200 putf("Tech Level:", "%s", p.techlev+1);
201 putf("Turnover:", "%s", p.productivity);
202 putf("Radius:", "%s", p.radius);
203 putf("Population:", "%s.%s Billion", p.population/10, p.population%10);
204 putd(p.description);
205 billH += 16;
206 foreach (immutable idx; 0..Galaxy.Goods.max+1) putm(p, idx);
207 version(none) {
208 float[4] b;
209 float a;
210 a = vg.textBounds(0, 0, "m", b[]);
211 conwriteln(a, " : ", b[]);
212 a = vg.textBounds(0, 0, "o", b[]);
213 conwriteln(a, " : ", b[]);
214 a = vg.textBounds(0, 0, "mo", b[]);
215 conwriteln(a, " : ", b[]);
216 debug(nanovg_fons_kern_test) NVGKTT(vg);
222 // ////////////////////////////////////////////////////////////////////////// //
223 __gshared ulong lastGalSeed = 0;
224 __gshared ubyte lastPlanetNum = 0;
225 __gshared PlanetInfoBillboard planetBill;
228 void drawGalaxy (NVGContext vg) {
229 if (vg is null) return;
231 if (planetBill is null || lastGalSeed != universe.galSeed || lastPlanetNum != universe.currPlanet.number) {
232 lastGalSeed = universe.galSeed;
233 lastPlanetNum = universe.currPlanet.number;
234 planetBill.destroy;
235 planetBill = new PlanetInfoBillboard(vg, universe.currPlanet);
238 vg.save();
239 scope(exit) vg.restore;
240 vg.resetScissor;
242 vg.fontFaceId(uiFont);
243 vg.textAlign(NVGTextAlign.H.Left, NVGTextAlign.V.Baseline);
244 vg.fontSize(fsizeUI);
246 //vg.beginFrame(GWidth, GHeight, 1);
247 //vg.scissor(0, 0, GWidth, GHeight);
248 //vg.resetTransform();
249 //vg.resetScissor;
250 //vg.translate((GWidth-256*2-8)/2, (GHeight-256*2-8)/2);
252 float mx = (GWidth-256*2-8)/2.0;
253 float my = (GHeight-256*2-8)/1.0-8;
255 vg.bndMenuBackground(mx, my, 256*2+8, 256*2+8, BND_CORNER_NONE);
257 vg.beginPath();
258 vg.fillColor(nvgRGBA(255, 255, 255));
259 foreach (const ref p; universe.planets) {
260 vg.circle(mx+p.x*2+4, my+p.y*2+4, 1);
262 vg.fill();
264 vg.beginPath();
265 vg.strokeColor(nvgRGBA(255, 255, 0));
266 vg.circle(mx+universe.currPlanet.x*2+4, my+universe.currPlanet.y*2+4, 4);
267 vg.stroke();
269 // show planet data
270 planetBill.draw(vg);
272 //vg.endFrame();