removed some debug code
[xreader.git] / ewbillboard.d
blobc95b07331d22cb96e72e914ea3ea21d2499e3570
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.nanovg;
20 import iv.nanovg.oui.blendish;
21 //import arsd.color;
23 import xreadercfg;
25 import eworld;
28 // ////////////////////////////////////////////////////////////////////////// //
29 __gshared Galaxy universe;
32 // ////////////////////////////////////////////////////////////////////////// //
33 final class PlanetInfoBillboard {
34 private:
35 string[2][] lines;
36 float[] lineH; // line height
37 float[2] ltblW = 0;
39 string pdsc;
40 float pdscH;
42 string[3][] market;
43 float[] mlineH;
44 float[3] mtblW = 0; // market widthes
46 float billH = 0, billW = 0;
47 NVGContext vg;
49 public:
50 this (NVGContext avg, ref Galaxy.Planet p) {
51 assert(avg !is null);
52 vg = avg;
53 scope(exit) vg = null;
54 vg.fontFaceId(galmapFont);
55 vg.textAlign(NVGAlign.Left|NVGAlign.Baseline);
56 vg.fontSize(fGalMapSize);
57 vg.textAlign(NVGAlign.Left|NVGAlign.Top);
58 buildPlanetData(p);
61 void draw (NVGContext avg, float mx=10, float my=10) {
62 if (avg is null) return;
63 vg = avg;
64 scope(exit) vg = null;
66 vg.save();
67 scope(exit) vg.restore;
69 auto obfn = bndGetFont();
70 scope(exit) bndSetFont(obfn);
72 vg.resetTransform();
73 vg.resetScissor;
75 version(none) {
76 vg.fontFaceId(uiFont);
77 vg.textAlign(NVGAlign.Left|NVGAlign.Top);
78 vg.fontSize(16);
79 float[4] b = void;
80 float adv;
81 adv = vg.textBounds(0, 0, "Woo", b[]);
82 { import std.stdio; writeln("adv=", adv, "; b=", b[]); }
85 bndSetFont(galmapFont);
86 vg.fontFaceId(galmapFont);
87 vg.textAlign(NVGAlign.Left|NVGAlign.Baseline);
88 vg.fontSize(fGalMapSize);
90 vg.bndMenuBackground(mx, my, billW+8, billH+8, BND_CORNER_NONE);
91 vg.beginPath();
92 float y = my+4;
93 // planet info
94 foreach (immutable idx; 0..lines.length) {
95 float x = mx+4;
96 vg.fillColor(nvgRGBA(255, 255, 255));
97 vg.textAlign(NVGAlign.Right|NVGAlign.Top);
98 x += ltblW[0];
99 vg.text(x, y, lines[idx][0]);
100 x += 4;
101 vg.fillColor(nvgRGBA(255, 255, 0));
102 vg.textAlign(NVGAlign.Left|NVGAlign.Top);
103 vg.text(x, y, lines[idx][1]);
104 y += lineH[idx];
106 // planet descrition
107 vg.fillColor(nvgRGBA(255, 127, 0));
108 vg.textAlign(NVGAlign.Left|NVGAlign.Top);
109 y += 8;
110 vg.text(mx+4, y, pdsc);
111 y += pdscH+4;
112 // market data
113 vg.fillColor(nvgRGBA(255, 255, 255));
114 foreach (immutable idx; 0..market.length) {
115 float x = mx+4;
116 vg.textAlign(NVGAlign.Left|NVGAlign.Top);
117 vg.text(x, y, market[idx][0]);
118 x += mtblW[0]+8+mtblW[1];
119 vg.textAlign(NVGAlign.Right|NVGAlign.Top);
120 vg.text(x, y, market[idx][1]);
121 x += mtblW[2]+8;
122 vg.text(x, y, market[idx][2]);
123 vg.textAlign(NVGAlign.Left|NVGAlign.Top);
124 x += 1;
125 vg.text(x, y, Galaxy.good(idx).unitName);
126 y += mlineH[idx];
128 vg.fill();
131 private:
132 void putf(A...) (string label, string fmt, in A args) {
133 import std.algorithm : max;
134 import std.string : format;
135 auto s = format(fmt, args);
136 lines.length += 1;
137 lines[$-1][0] = label;
138 lines[$-1][1] = s;
139 float[4] b = void;
140 // label
141 vg.textBounds(0, 0, label, b[]);
142 float hh = b[3]-b[1];
143 float ww = b[2]-b[0];
144 ltblW[0] = max(ltblW[0], ww);
145 // text
146 vg.textBounds(0, 0, s, b[]);
147 hh = max(hh, b[3]-b[1]);
148 ww = b[2]-b[0];
149 ltblW[1] = max(ltblW[1], ww);
150 // totals
151 lineH ~= hh;
152 billH += hh;
153 billW = max(billW, ltblW[0]+4+ltblW[1]);
156 void putd (string dsc) {
157 import std.algorithm : max;
158 pdsc = dsc;
159 float[4] b = void;
160 vg.textBounds(0, 0, dsc, b[]);
161 float hh = b[3]-b[1];
162 float ww = b[2]-b[0];
163 // totals
164 pdscH = hh;
165 billH += hh;
166 billW = max(billW, ww);
169 void putm(T : ulong) (in ref Galaxy.Planet p, T idx) {
170 import std.algorithm : max;
171 import std.string : format;
172 if (idx < 0 || idx > Galaxy.Goods.max) return;
173 auto good = Galaxy.good(idx);
174 market.length += 1;
175 market[$-1][0] = good.name;
176 market[$-1][1] = "%s.%s".format(p.price[idx]/10, p.price[idx]%10);
177 market[$-1][2] = "%s".format(p.quantity[idx]);
178 float[4] b = void;
179 float hh = 0, wt = 8*2;
180 foreach (immutable n, string s; market[$-1]) {
181 vg.textBounds(0, 0, s, b[]);
182 hh = max(hh, b[3]-b[1]);
183 mtblW[n] = max(mtblW[n], b[2]-b[0]);
184 wt += mtblW[n];
186 vg.textBounds(0, 0, "kg", b[]);
187 hh = max(hh, b[3]-b[1]);
188 wt += b[2]-b[0]+2;
189 mlineH ~= hh;
190 billH += hh;
191 billW = max(billW, wt);
194 void buildPlanetData (ref Galaxy.Planet p) {
195 putf("System:", "%s", p.name);
196 putf("Position:", "(%s,%s)", p.x, p.y);
197 putf("Economy:", "%s", p.economyName);
198 putf("Government:", "%s", p.govName);
199 putf("Tech Level:", "%s", p.techlev+1);
200 putf("Turnover:", "%s", p.productivity);
201 putf("Radius:", "%s", p.radius);
202 putf("Population:", "%s.%s Billion", p.population/10, p.population%10);
203 putd(p.description);
204 billH += 16;
205 foreach (immutable idx; 0..Galaxy.Goods.max+1) putm(p, idx);
206 version(none) {
207 float[4] b;
208 float a;
209 a = vg.textBounds(0, 0, "m", b[]);
210 writeln(a, " : ", b[]);
211 a = vg.textBounds(0, 0, "o", b[]);
212 writeln(a, " : ", b[]);
213 a = vg.textBounds(0, 0, "mo", b[]);
214 writeln(a, " : ", b[]);
215 debug(nanovg_fons_kern_test) NVGKTT(vg);
221 // ////////////////////////////////////////////////////////////////////////// //
222 __gshared ulong lastGalSeed = 0;
223 __gshared ubyte lastPlanetNum = 0;
224 __gshared PlanetInfoBillboard planetBill;
227 void drawGalaxy (NVGContext vg) {
228 if (vg is null) return;
230 if (planetBill is null || lastGalSeed != universe.galSeed || lastPlanetNum != universe.currPlanet.number) {
231 lastGalSeed = universe.galSeed;
232 lastPlanetNum = universe.currPlanet.number;
233 planetBill.destroy;
234 planetBill = new PlanetInfoBillboard(vg, universe.currPlanet);
237 vg.fontFaceId(uiFont);
238 vg.textAlign(NVGAlign.Left|NVGAlign.Baseline);
239 vg.fontSize(fsizeUI);
241 vg.beginFrame(GWidth, GHeight, 1);
242 //vg.scissor(0, 0, GWidth, GHeight);
243 //vg.resetTransform();
244 //vg.resetScissor;
245 //vg.translate((GWidth-256*2-8)/2, (GHeight-256*2-8)/2);
247 float mx = (GWidth-256*2-8)/2.0;
248 float my = (GHeight-256*2-8)/1.0-8;
250 vg.bndMenuBackground(mx, my, 256*2+8, 256*2+8, BND_CORNER_NONE);
252 vg.beginPath();
253 vg.fillColor(nvgRGBA(255, 255, 255));
254 foreach (const ref p; universe.planets) {
255 vg.circle(mx+p.x*2+4, my+p.y*2+4, 1);
257 vg.fill();
259 vg.beginPath();
260 vg.strokeColor(nvgRGBA(255, 255, 0));
261 vg.circle(mx+universe.currPlanet.x*2+4, my+universe.currPlanet.y*2+4, 4);
262 vg.stroke();
264 // show planet data
265 planetBill.draw(vg);
267 vg.endFrame();