11package haven .res .ui .surv ;
22
3+ import haven .Area ;
34import haven .BGL ;
45import haven .Button ;
56import haven .Coord ;
3637import static javax .media .opengl .fixedfunc .GLPointerFunc .GL_VERTEX_ARRAY ;
3738import modification .dev ;
3839import java .awt .Color ;
40+ import java .awt .event .MouseEvent ;
3941import java .nio .FloatBuffer ;
4042import java .nio .ShortBuffer ;
43+ import java .util .stream .IntStream ;
4144
4245public class LandSurvey extends Window {
43- static {
44- dev .checkFileVersion ("ui/surv" , 42 );
45- }
46-
47- final Coord ul ;
48- final Coord br ;
46+ public final Area area ;
47+ public final Data data ;
48+ private boolean inited = false ;
4949 MapView mv ;
5050 Display dsp ;
5151 final FastMesh ol ;
@@ -55,41 +55,39 @@ public class LandSurvey extends Window {
5555 final Label wlbl ;
5656 final Label dlbl ;
5757 final HSlider zset ;
58- final float gran ;
59- int tz ;
6058 int defz ;
6159
6260 public IButton plus ;
6361 public IButton minus ;
6462 public Label zvalue ;
6563 public Label value ;
6664
67- public LandSurvey (Coord ul , Coord br , float gran , int tz ) {
65+ public LandSurvey (Area area , Data data ) {
6866 super (Coord .z , "Land survey" , "Land survey" , true );
69- this .ul = ul ;
70- this .br = br ;
71- this .gran = gran ;
72- this .tz = tz ;
73- this .dloc = Location .xlate (new Coord3f (this .ul .x * (float ) tilesz .x , -this .ul .y * (float ) tilesz .y , 0 ));
67+ this .area = area ;
68+ this .data = data ;
69+ this .dloc = Location .xlate (new Coord3f (area .ul .x * (float ) tilesz .x , -area .ul .y * (float ) tilesz .y , 0 ));
7470 VertexArray olv = new VertexArray (FloatBuffer .wrap (new float []{
7571 0 , 0 , 0 ,
76- (br .x - ul .x ) * (float ) tilesz .x , 0 , 0 ,
77- (br .x - ul .x ) * (float ) tilesz .x , -(br .y - ul .y ) * (float )
78- MCache .tilesz .y , 0 , 0 , -(br .y - ul .y ) * (float ) tilesz .y , 0 ,
72+ (area . br .x - area . ul .x ) * (float ) tilesz .x , 0 , 0 ,
73+ (area . br .x - area . ul .x ) * (float ) tilesz .x , -(area . br .y - area . ul .y ) * (float )
74+ MCache .tilesz .y , 0 , 0 , -(area . br .y - area . ul .y ) * (float ) tilesz .y , 0 ,
7975 }));
8076 ol = new FastMesh (new VertexBuf (olv ), ShortBuffer .wrap (new short []{
8177 0 , 3 , 1 ,
8278 1 , 3 , 2 ,
8379 }));
84- albl = add (new Label (String .format ("Area: %d m\u00b2 " , ( br . x - ul . x ) * ( br . y - ul . y ))), 0 , 0 );
80+ albl = add (new Label (String .format ("Area: %d m\u00b2 " , area . area ( ))), 0 , 0 );
8581 zdlbl = add (new Label ("..." ), UI .scale (0 , 15 ));
8682 wlbl = add (new Label ("..." ), UI .scale (0 , 30 ));
8783 dlbl = add (new Label ("..." ), UI .scale (0 , 45 ));
88- zset = add (new HSlider (UI .scale (225 ), -1 , 1 , tz ) {
84+ defz = data .dz [0 ];
85+ zset = add (new HSlider (UI .scale (225 ), -1 , 1 , data .dz [0 ]) {
8986 public void changed () {
90- LandSurvey .this .tz = val ; changeVal (String .format ("%s%d" , (val > defz ) ? "+" : "" , val - defz ));
87+ IntStream .range (0 , data .dz .length ).forEach (i -> data .dz [i ] = val );
88+ changeVal (String .format ("%s%d" , (val > defz ) ? "+" : "" , val - defz ));
9189 upd = true ;
92- sendtz = Utils . rtime () + 0.5 ;
90+ send () ;
9391 }
9492
9593 public Object tooltip (Coord c , Widget prev ) {
@@ -99,16 +97,22 @@ public Object tooltip(Coord c, Widget prev) {
9997 zvalue = add (new Label ("Z" ), UI .scale (0 , 70 ));
10098 value = add (new Label ("..." ) {
10199 public boolean mousewheel (Coord c , int amount ) {
102- final int v ; if (ui .modshift ) v = amount * 10 ;
103- else if (ui .modctrl ) v = amount * 5 ;
104- else v = amount ; wheel (-v ); return (true );
100+ final int v ;
101+ if (ui .modshift )
102+ v = amount * 10 ;
103+ else if (ui .modctrl )
104+ v = amount * 5 ;
105+ else
106+ v = amount ;
107+ wheel (-v );
108+ return (true );
105109 }
106110 }, UI .scale (0 , 70 ));
107111 plus = add (new IButton (Theme .fullres ("buttons/circular/small/add" ), this ::plus ), UI .scale (0 , 70 ));
108112 minus = add (new IButton (Theme .fullres ("buttons/circular/small/sub" ), this ::minus ), UI .scale (0 , 70 ));
109113 add (new Button (UI .scale (100 ), "Make level" ) {
110114 public void click () {
111- LandSurvey .this .wdgmsg ("lvl" , LandSurvey . this . tz / ( gran * 11 ) );
115+ LandSurvey .this .wdgmsg ("lvl" );
112116 }
113117 }, UI .scale (0 , 90 ));
114118 add (new Button (UI .scale (100 ), "Remove" ) {
@@ -120,7 +124,7 @@ public void click() {
120124 }
121125
122126 public void changeVal (String text ) {
123- zvalue .settext ("z: " + tz );
127+ zvalue .settext ("z: " + data . dz [ 0 ] );
124128 value .settext (text );
125129 value .move (UI .scale (new Coord (asz .x / 2 , value .c .y )), 0.5 , 0 );
126130 plus .move (UI .scale (new Coord (value .c .x + value .sz .x + 5 , value .c .y )));
@@ -143,19 +147,24 @@ public void wheel(int a) {
143147 }
144148
145149 public static Widget mkwidget (UI ui , Object ... args ) {
146- Coord ul = (Coord ) args [0 ];
147- Coord br = (Coord ) args [1 ];
148- float gran = ((Number ) args [3 ]).floatValue () / 11 ;
149- int tz = (args [2 ] == null ) ? Integer .MIN_VALUE : Math .round (((Number ) args [2 ]).floatValue () * gran * 11 );
150- return (new LandSurvey (ul , br , gran , tz ));
150+ Area area = Area .corn ((Coord )args [0 ], (Coord )args [1 ]);
151+ float gran = ((Number )args [2 ]).floatValue () / 11 ;
152+ Data data = new Data (Area .corni (area .ul , area .br ), gran );
153+ LandSurvey srv = new LandSurvey (area , data );
154+ if (args [3 ] != null ) {
155+ data .decode (Utils .iv (args [3 ]), (byte [])args [4 ]);
156+ srv .inited = true ;
157+ }
158+ return (srv );
151159 }
152160
153161 protected void attached () {
154162 super .attached ();
155163 this .mv = getparent (GameUI .class ).map ;
156- this .defz = autoz ();
157- changeVal (zset .val - defz + "" );
158164 this .dsp = new Display ();
165+ //s_dsp = mv.drawadd(dsp);
166+ //select(area);
167+ //mode(new Idle());
159168 }
160169
161170 class Display implements Rendered {
@@ -167,7 +176,7 @@ class Display implements Rendered {
167176
168177 Display () {
169178 map = mv .ui .sess .glob .map ;
170- area = (br .x - ul .x + 1 ) * (br .y - ul .y + 1 );
179+ area = (LandSurvey . this . area . br .x - LandSurvey . this . area . ul .x + 1 ) * (LandSurvey . this . area . br .y - LandSurvey . this . area . ul .y + 1 );
171180 cposb = Utils .mkfbuf (area * 3 );
172181 ccolb = Utils .mkfbuf (area * 4 );
173182 update ();
@@ -192,11 +201,11 @@ void update() {
192201 cposb .rewind ();
193202 ccolb .rewind ();
194203 Coord c = new Coord ();
195- float tz = LandSurvey .this .tz / gran ;
196- for (c .y = ul .y ; c .y <= br .y ; c .y ++) {
197- for (c .x = ul .x ; c .x <= br .x ; c .x ++) {
204+ float tz = LandSurvey .this .data . dz [ 0 ] / data . gran ;
205+ for (c .y = LandSurvey . this . area . ul .y ; c .y <= LandSurvey . this . area . br .y ; c .y ++) {
206+ for (c .x = LandSurvey . this . area . ul .x ; c .x <= LandSurvey . this . area . br .x ; c .x ++) {
198207 float z = (float ) map .getfz (c );
199- cposb .put ((c .x - ul .x ) * (float ) tilesz .x ).put (-(c .y - ul .y ) * (float ) tilesz .y ).put (tz );
208+ cposb .put ((c .x - LandSurvey . this . area . ul .x ) * (float ) tilesz .x ).put (-(c .y - LandSurvey . this . area . ul .y ) * (float ) tilesz .y ).put (tz );
200209 if (Math .abs (tz - z ) < E ) {
201210 ccolb .put (0 ).put (1 ).put (0 ).put (1 );
202211 } else if (tz < z ) {
@@ -219,59 +228,72 @@ public boolean setup(RenderList rls) {
219228 }
220229 }
221230
222- private int autoz () {
231+ private void initsurf () {
232+ MCache map = mv .ui .sess .glob .map ;
233+ for (Coord vc : data .varea )
234+ data .wz [data .varea .ridx (vc )] = data .dz [data .varea .ridx (vc )] = (int ) Math .round (map .getfz (vc ) * data .gran );
235+ data .seq ++;
236+ upd = true ;
237+ }
238+
239+ private void initplane () {
223240 MCache map = mv .ui .sess .glob .map ;
224241 double zs = 0 ;
225242 int nv = 0 ;
226- Coord c = new Coord ();
227- for (c .y = ul .y ; c .y <= br .y ; c .y ++) {
228- for (c .x = ul .x ; c .x <= br .x ; c .x ++) {
229- zs += map .getfz (c );
230- nv ++;
231- }
243+ for (Coord vc : data .varea ) {
244+ zs += map .getfz (vc );
245+ nv ++;
232246 }
233- return ((int ) Math .round (zs / nv ));
247+ int z = Math .round ((float ) (zs / nv ) * data .gran );
248+ for (int i = 0 ; i < data .wz .length ; i ++)
249+ data .wz [i ] = data .dz [i ] = z ;
250+ data .seq ++;
251+ upd = true ;
234252 }
235253
236- private boolean upd = true ;
237-
238254 private void updmap () {
239255 MCache map = mv .ui .sess .glob .map ;
240- Coord c = new Coord ();
241256 int min = Integer .MAX_VALUE , max = Integer .MIN_VALUE ;
242- int sd = 0 , hn = 0 ; for (c .y = ul .y ; c .y <= br .y ; c .y ++) {
243- for (c .x = ul .x ; c .x <= br .x ; c .x ++) {
244- int z = (int ) Math .round (map .getfz (c ) * gran );
245- min = Math .min (min , z );
246- max = Math .max (max , z );
247- sd += tz - z ;
248- if (z > tz ) hn += z - tz ;
249- }
250- } zset .min = min - Math .round (11 * gran );
251- zset .max = max + Math .round (11 * gran );
257+ int sd = 0 , hn = 0 ;
258+ for (Coord vc : data .varea ) {
259+ int vz = Math .round ((float ) map .getfz (vc ) * data .gran );
260+ int tz = data .dz [data .varea .ridx (vc )];
261+ min = Math .min (min , vz ); max = Math .max (max , vz );
262+ sd += tz - vz ;
263+ if (vz > tz )
264+ hn += vz - tz ;
265+ }
252266 zdlbl .settext (String .format ("Peak to trough: %.1f m" , (max - min ) / 10.0 ));
253- if (sd >= 0 ) wlbl .settext (String .format ("Units of soil required: %d" , sd ));
254- else wlbl .settext (String .format ("Units of soil left over: %d" , -sd ));
255- dlbl .settext (String .format ("Units of soil to dig: %d" , hn )); dsp .update ();
267+ if (sd >= 0 )
268+ wlbl .settext (String .format ("Units of soil required: %d" , sd ));
269+ else
270+ wlbl .settext (String .format ("Units of soil left over: %d" , -sd ));
271+ dlbl .settext (String .format ("Units of soil to dig: %d" , hn ));
256272 }
257273
258- private double sendtz = 0 ;
274+ private void send () {
275+ wdgmsg ("data" , data .encode ());
276+ }
277+
278+ private boolean upd = true ;
279+ private int mapseq = -1 ;
259280 private static final GLState olmat = GLState .compose (new ColState (new Color (255 , 0 , 0 , 64 )), Rendered .eyesort , new DepthOffset (-2 , -2 ));
260281 private int olseq = -1 ;
261282
262283 public void tick (double dt ) {
263- if (tz == Integer . MIN_VALUE ) {
284+ if (! inited ) {
264285 try {
265- zset . val = defz = tz = autoz ();
266- changeVal ( 0 + "" );
286+ initplane ();
287+ send ( );
267288 olseq = mv .ui .sess .glob .map .olseq ;
268- upd = true ;
269- } catch (Loading l ) {
270- }
271- } else {
272- if (upd || (olseq != mv .ui .sess .glob .map .olseq )) {
289+ inited = true ;
290+ } catch (Loading l ) {}
291+ }
292+ if ( inited ) {
293+ if (upd || (mapseq != mv . ui . sess . glob . map . chseq ) || ( olseq != mv .ui .sess .glob .map .olseq )) {
273294 try {
274295 updmap ();
296+ mapseq = mv .ui .sess .glob .map .chseq ;
275297 olseq = mv .ui .sess .glob .map .olseq ;
276298 upd = false ;
277299 } catch (Loading l ) {
@@ -280,21 +302,25 @@ public void tick(double dt) {
280302
281303 if (olseq != -1 ) {
282304 mv .drawadd (dsp );
283- mv .drawadd (GLState .compose (olmat , Location .xlate (new Coord3f (ul .x * (float ) tilesz .x , -ul .y * (float ) tilesz .y , tz ))).apply (ol ));
305+ mv .drawadd (GLState .compose (olmat , Location .xlate (new Coord3f (area . ul .x * (float ) tilesz .x , -area . ul .y * (float ) tilesz .y , data . dz [ 0 ] ))).apply (ol ));
284306 }
285307 }
286- if ((sendtz != 0 ) && (Utils .rtime () > sendtz )) {
287- wdgmsg ("tz" , tz / (gran * 11 ));
288- sendtz = 0 ;
289- }
290308 super .tick (dt );
291309 }
292310
293311 public void uimsg (String name , Object ... args ) {
294312 if (name == "tz" ) {
295- tz = Math .round (((Number ) args [0 ]).floatValue () * gran ); zset .val = tz ; upd = true ;
313+ data .decode (Utils .iv (args [0 ]), (byte []) args [1 ]);
314+ upd = true ;
296315 } else {
297316 super .uimsg (name , args );
298317 }
299318 }
319+
320+ public void destroy () {
321+ //mode(null);
322+ //if (s_dsp != null)
323+ // s_dsp.remove();
324+ super .destroy ();
325+ }
300326}
0 commit comments