summaryrefslogtreecommitdiff
path: root/indra/newview/lltoolbrush.cpp
blob: e2b6924aebb8856ac3929c68107059838e67f2ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/**
 * @file lltoolbrush.cpp
 * @brief Implementation of the toolbrushes
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "lltoolbrush.h"
#include "lltoolselectland.h"

// library headers
#include "llgl.h"
#include "llnotificationsutil.h"
#include "llrender.h"
#include "message.h"

#include "llagent.h"
#include "llcallbacklist.h"
#include "llviewercontrol.h"
#include "llfloatertools.h"
#include "llregionposition.h"
#include "llstatusbar.h"
#include "llsurface.h"
#include "llsurfacepatch.h"
#include "lltoolmgr.h"
#include "llui.h"
#include "llviewerparcelmgr.h"
#include "llviewerparceloverlay.h"
#include "llviewerregion.h"
#include "llviewerwindow.h"
#include "llworld.h"
#include "llappviewer.h"
#include "llparcel.h"
#include "roles_constants.h"
#include "llglheaders.h"

const std::string REGION_BLOCKS_TERRAFORM_MSG = "This region does not allow terraforming.\n"
                "You will need to buy land in another part of the world to terraform it.";


///============================================================================
/// Local function declarations, constants, enums, and typedefs
///============================================================================

const S32 LAND_BRUSH_SIZE_COUNT = 3;
const F32 LAND_BRUSH_SIZE[LAND_BRUSH_SIZE_COUNT] = {1.0f, 2.0f, 4.0f};

enum
{
    E_LAND_LEVEL    = 0,
    E_LAND_RAISE    = 1,
    E_LAND_LOWER    = 2,
    E_LAND_SMOOTH   = 3,
    E_LAND_NOISE    = 4,
    E_LAND_REVERT   = 5,
    E_LAND_INVALID  = 6,
};
const LLColor4 OVERLAY_COLOR(1.0f, 1.0f, 1.0f, 1.0f);

///============================================================================
/// Class LLToolBrushLand
///============================================================================

// constructor
LLToolBrushLand::LLToolBrushLand()
:   LLTool(std::string("Land")),
    mStartingZ( 0.0f ),
    mMouseX( 0 ),
    mMouseY(0),
    mGotHover(false),
    mBrushSelected(false)
{
    mBrushSize = gSavedSettings.getF32("LandBrushSize");
}


U8 LLToolBrushLand::getBrushIndex()
{
    // find the best index for desired size
    // (compatibility with old sims, brush_index is now depricated - DEV-8252)
    U8 index = 0;
    for (U8 i = 0; i < LAND_BRUSH_SIZE_COUNT; i++)
    {
        if (mBrushSize > LAND_BRUSH_SIZE[i])
        {
            index = i;
        }
    }

    return index;
}

void LLToolBrushLand::modifyLandAtPointGlobal(const LLVector3d &pos_global,
                                              MASK mask)
{
    S32 radioAction = gSavedSettings.getS32("RadioLandBrushAction");

    mLastAffectedRegions.clear();
    determineAffectedRegions(mLastAffectedRegions, pos_global);
    for(region_list_t::iterator iter = mLastAffectedRegions.begin();
        iter != mLastAffectedRegions.end(); ++iter)
    {
        LLViewerRegion* regionp = *iter;
        //bool is_changed = false;
        LLVector3 pos_region = regionp->getPosRegionFromGlobal(pos_global);
        LLSurface &land = regionp->getLand();
        char action = E_LAND_LEVEL;
        switch (radioAction)
        {
        case 0:
        //  // average toward mStartingZ
            action = E_LAND_LEVEL;
            break;
        case 1:
            action = E_LAND_RAISE;
            break;
        case 2:
            action = E_LAND_LOWER;
            break;
        case 3:
            action = E_LAND_SMOOTH;
            break;
        case 4:
            action = E_LAND_NOISE;
            break;
        case 5:
            action = E_LAND_REVERT;
            break;
        default:
            action = E_LAND_INVALID;
            break;
        }

        // Don't send a message to the region if nothing changed.
        //if(!is_changed) continue;

        // Now to update the patch information so it will redraw correctly.
        LLSurfacePatch *patchp= land.resolvePatchRegion(pos_region);
        if (patchp)
        {
            patchp->dirtyZ();
        }

        // Also force the property lines to update, normals to recompute, etc.
        regionp->forceUpdate();

        // tell the simulator what we've done
        F32 seconds = (1.0f / gFPSClamped) * gSavedSettings.getF32("LandBrushForce");
        F32 x_pos = (F32)pos_region.mV[VX];
        F32 y_pos = (F32)pos_region.mV[VY];
        LLMessageSystem* msg = gMessageSystem;
        msg->newMessageFast(_PREHASH_ModifyLand);
        msg->nextBlockFast(_PREHASH_AgentData);
        msg->addUUIDFast(_PREHASH_AgentID, gAgent.getID());
        msg->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
        msg->nextBlockFast(_PREHASH_ModifyBlock);
        msg->addU8Fast(_PREHASH_Action, (U8)action);
        msg->addU8Fast(_PREHASH_BrushSize, getBrushIndex());
        msg->addF32Fast(_PREHASH_Seconds, seconds);
        msg->addF32Fast(_PREHASH_Height, mStartingZ);
        msg->nextBlockFast(_PREHASH_ParcelData);
        msg->addS32Fast(_PREHASH_LocalID, -1);
        msg->addF32Fast(_PREHASH_West, x_pos );
        msg->addF32Fast(_PREHASH_South, y_pos );
        msg->addF32Fast(_PREHASH_East, x_pos );
        msg->addF32Fast(_PREHASH_North, y_pos );
        msg->nextBlock("ModifyBlockExtended");
        msg->addF32("BrushSize", mBrushSize);
        msg->sendMessage(regionp->getHost());
    }
}

void LLToolBrushLand::modifyLandInSelectionGlobal()
{
    if (LLViewerParcelMgr::getInstance()->selectionEmpty())
    {
        return;
    }

    if (LLToolMgr::getInstance()->getCurrentTool() == LLToolSelectLand::getInstance())
    {
        // selecting land, don't do anything
        return;
    }

    LLVector3d min;
    LLVector3d max;

    LLViewerParcelMgr::getInstance()->getSelection(min, max);

    S32 radioAction = gSavedSettings.getS32("RadioLandBrushAction");

    mLastAffectedRegions.clear();

    determineAffectedRegions(mLastAffectedRegions, LLVector3d(min.mdV[VX], min.mdV[VY], 0));
    determineAffectedRegions(mLastAffectedRegions, LLVector3d(min.mdV[VX], max.mdV[VY], 0));
    determineAffectedRegions(mLastAffectedRegions, LLVector3d(max.mdV[VX], min.mdV[VY], 0));
    determineAffectedRegions(mLastAffectedRegions, LLVector3d(max.mdV[VX], max.mdV[VY], 0));

    LLRegionPosition mid_point_region((min + max) * 0.5);
    LLViewerRegion* center_region = mid_point_region.getRegion();
    if (center_region)
    {
        LLVector3 pos_region = mid_point_region.getPositionRegion();
        U32 grids = center_region->getLand().mGridsPerEdge;
        S32 i = llclamp( (S32)pos_region.mV[VX], 0, (S32)grids );
        S32 j = llclamp( (S32)pos_region.mV[VY], 0, (S32)grids );
        mStartingZ = center_region->getLand().getZ(i+j*grids);
    }
    else
    {
        mStartingZ = 0.f;
    }

    // Stop if our selection include a no-terraform region
    for(region_list_t::iterator iter = mLastAffectedRegions.begin();
        iter != mLastAffectedRegions.end(); ++iter)
    {
        LLViewerRegion* regionp = *iter;
        if (!canTerraformRegion(regionp))
        {
            alertNoTerraformRegion(regionp);
            return;
        }
    }

    for(region_list_t::iterator iter = mLastAffectedRegions.begin();
        iter != mLastAffectedRegions.end(); ++iter)
    {
        LLViewerRegion* regionp = *iter;
        //bool is_changed = false;
        LLVector3 min_region = regionp->getPosRegionFromGlobal(min);
        LLVector3 max_region = regionp->getPosRegionFromGlobal(max);

        min_region.clamp(0.f, regionp->getWidth());
        max_region.clamp(0.f, regionp->getWidth());
        F32 seconds = gSavedSettings.getF32("LandBrushForce");

        LLSurface &land = regionp->getLand();
        char action = E_LAND_LEVEL;
        switch (radioAction)
        {
        case 0:
        //  // average toward mStartingZ
            action = E_LAND_LEVEL;
            seconds *= 0.25f;
            break;
        case 1:
            action = E_LAND_RAISE;
            seconds *= 0.25f;
            break;
        case 2:
            action = E_LAND_LOWER;
            seconds *= 0.25f;
            break;
        case 3:
            action = E_LAND_SMOOTH;
            seconds *= 5.0f;
            break;
        case 4:
            action = E_LAND_NOISE;
            seconds *= 0.5f;
            break;
        case 5:
            action = E_LAND_REVERT;
            seconds = 0.5f;
            break;
        default:
            //action = E_LAND_INVALID;
            //seconds = 0.0f;
            return;
            break;
        }

        // Don't send a message to the region if nothing changed.
        //if(!is_changed) continue;

        // Now to update the patch information so it will redraw correctly.
        LLSurfacePatch *patchp= land.resolvePatchRegion(min_region);
        if (patchp)
        {
            patchp->dirtyZ();
        }

        // Also force the property lines to update, normals to recompute, etc.
        regionp->forceUpdate();

        // tell the simulator what we've done
        LLMessageSystem* msg = gMessageSystem;
        msg->newMessageFast(_PREHASH_ModifyLand);
        msg->nextBlockFast(_PREHASH_AgentData);
        msg->addUUIDFast(_PREHASH_AgentID, gAgent.getID());
        msg->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
        msg->nextBlockFast(_PREHASH_ModifyBlock);
        msg->addU8Fast(_PREHASH_Action, (U8)action);
        msg->addU8Fast(_PREHASH_BrushSize, getBrushIndex());
        msg->addF32Fast(_PREHASH_Seconds, seconds);
        msg->addF32Fast(_PREHASH_Height, mStartingZ);

        bool parcel_selected = LLViewerParcelMgr::getInstance()->getParcelSelection()->getWholeParcelSelected();
        LLParcel* selected_parcel = LLViewerParcelMgr::getInstance()->getParcelSelection()->getParcel();

        if (parcel_selected && selected_parcel)
        {
            msg->nextBlockFast(_PREHASH_ParcelData);
            msg->addS32Fast(_PREHASH_LocalID, selected_parcel->getLocalID());
            msg->addF32Fast(_PREHASH_West,  min_region.mV[VX] );
            msg->addF32Fast(_PREHASH_South, min_region.mV[VY] );
            msg->addF32Fast(_PREHASH_East,  max_region.mV[VX] );
            msg->addF32Fast(_PREHASH_North, max_region.mV[VY] );
        }
        else
        {
            msg->nextBlockFast(_PREHASH_ParcelData);
            msg->addS32Fast(_PREHASH_LocalID, -1);
            msg->addF32Fast(_PREHASH_West,  min_region.mV[VX] );
            msg->addF32Fast(_PREHASH_South, min_region.mV[VY] );
            msg->addF32Fast(_PREHASH_East,  max_region.mV[VX] );
            msg->addF32Fast(_PREHASH_North, max_region.mV[VY] );
        }

        msg->nextBlock("ModifyBlockExtended");
        msg->addF32("BrushSize", mBrushSize);

        msg->sendMessage(regionp->getHost());
    }
}

void LLToolBrushLand::brush( void )
{
    LLVector3d spot;
    if( gViewerWindow->mousePointOnLandGlobal( mMouseX, mMouseY, &spot ) )
    {
        // Round to nearest X,Y grid
        spot.mdV[VX] = floor( spot.mdV[VX] + 0.5 );
        spot.mdV[VY] = floor( spot.mdV[VY] + 0.5 );

        modifyLandAtPointGlobal(spot, gKeyboard->currentMask(true));
    }
}

bool LLToolBrushLand::handleMouseDown(S32 x, S32 y, MASK mask)
{
    bool handled = false;

    // Find the z value of the initial click.
    LLVector3d spot;
    if( gViewerWindow->mousePointOnLandGlobal( x, y, &spot ) )
    {
        // Round to nearest X,Y grid
        spot.mdV[VX] = floor( spot.mdV[VX] + 0.5 );
        spot.mdV[VY] = floor( spot.mdV[VY] + 0.5 );

        LLRegionPosition region_position( spot );
        LLViewerRegion* regionp = region_position.getRegion();

        if (!canTerraformRegion(regionp))
        {
            alertNoTerraformRegion(regionp);
            return true;
        }

        if (!canTerraformParcel(regionp))
        {
            alertNoTerraformParcel();
        }

        LLVector3 pos_region = region_position.getPositionRegion();
        U32 grids = regionp->getLand().mGridsPerEdge;
        S32 i = llclamp( (S32)pos_region.mV[VX], 0, (S32)grids );
        S32 j = llclamp( (S32)pos_region.mV[VY], 0, (S32)grids );
        mStartingZ = regionp->getLand().getZ(i+j*grids);
        mMouseX = x;
        mMouseY = y;
        gIdleCallbacks.addFunction( &LLToolBrushLand::onIdle, (void*)this );
        setMouseCapture( true );

        LLViewerParcelMgr::getInstance()->setSelectionVisible(false);
        handled = true;
    }

    return handled;
}

bool LLToolBrushLand::handleHover( S32 x, S32 y, MASK mask )
{
    LL_DEBUGS("UserInput") << "hover handled by LLToolBrushLand ("
                                << (hasMouseCapture() ? "active":"inactive")
                                << ")" << LL_ENDL;
    mMouseX = x;
    mMouseY = y;
    mGotHover = true;
    gViewerWindow->setCursor(UI_CURSOR_TOOLLAND);

    LLVector3d spot;
    if( gViewerWindow->mousePointOnLandGlobal( mMouseX, mMouseY, &spot ) )
    {

        spot.mdV[VX] = floor( spot.mdV[VX] + 0.5 );
        spot.mdV[VY] = floor( spot.mdV[VY] + 0.5 );

        LLViewerParcelMgr::getInstance()->setHoverParcel(spot);
    }
    return true;
}

bool LLToolBrushLand::handleMouseUp(S32 x, S32 y, MASK mask)
{
    bool handled = false;
    mLastAffectedRegions.clear();
    if( hasMouseCapture() )
    {
        // Release the mouse
        setMouseCapture( false );

        LLViewerParcelMgr::getInstance()->setSelectionVisible(true);

        gIdleCallbacks.deleteFunction( &LLToolBrushLand::onIdle, (void*)this );
        handled = true;
    }

    return handled;
}

void LLToolBrushLand::handleSelect()
{
    gEditMenuHandler = this;

    gFloaterTools->setStatusText("modifyland");
//  if (!mBrushSelected)
    {
        mBrushSelected = true;
    }
}


void LLToolBrushLand::handleDeselect()
{
    if( gEditMenuHandler == this )
    {
        gEditMenuHandler = NULL;
    }
    LLViewerParcelMgr::getInstance()->setSelectionVisible(true);
    mBrushSelected = false;
}

// Draw the area that will be affected.
void LLToolBrushLand::render()
{
    if(mGotHover)
    {
        //LL_INFOS() << "LLToolBrushLand::render()" << LL_ENDL;
        LLVector3d spot;
        if(gViewerWindow->mousePointOnLandGlobal(mMouseX, mMouseY, &spot))
        {
            spot.mdV[VX] = floor( spot.mdV[VX] + 0.5 );
            spot.mdV[VY] = floor( spot.mdV[VY] + 0.5 );

            mBrushSize = gSavedSettings.getF32("LandBrushSize");

            region_list_t regions;
            determineAffectedRegions(regions, spot);

            // Now, for each region, render the overlay
            LLVector3 pos_world = gAgent.getRegion()->getPosRegionFromGlobal(spot);
            for(region_list_t::iterator iter = regions.begin();
                iter != regions.end(); ++iter)
            {
                LLViewerRegion* region = *iter;
                renderOverlay(region->getLand(),
                              region->getPosRegionFromGlobal(spot),
                              pos_world);
            }
        }
        mGotHover = false;
    }
}

/*
 * Draw vertical lines from each vertex straight up in world space
 * with lengths indicating the current "strength" slider.
 * Decorate the tops and bottoms of the lines like this:
 *
 *      Raise        Revert
 *      /|\           ___
 *       |             |
 *       |             |
 *
 *      Rough        Smooth
 *      /|\           ___
 *       |             |
 *       |             |
 *      \|/..........._|_
 *
 *      Lower        Flatten
 *       |             |
 *       |             |
 *      \|/..........._|_
 */
void LLToolBrushLand::renderOverlay(LLSurface& land, const LLVector3& pos_region,
                                    const LLVector3& pos_world)
{
    gGL.matrixMode(LLRender::MM_MODELVIEW);
    gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
    LLGLDepthTest mDepthTest(GL_TRUE);
    gGL.pushMatrix();
    gGL.color4fv(OVERLAY_COLOR.mV);
    gGL.translatef(0.0f, 0.0f, 1.0f);

    S32 i = (S32) pos_region.mV[VX];
    S32 j = (S32) pos_region.mV[VY];
    S32 half_edge = llfloor(mBrushSize);
    S32 radioAction = gSavedSettings.getS32("RadioLandBrushAction");
    F32 force = gSavedSettings.getF32("LandBrushForce"); // .1 to 100?

    gGL.begin(LLRender::LINES);
    for(S32 di = -half_edge; di <= half_edge; di++)
    {
        if((i+di) < 0 || (i+di) >= (S32)land.mGridsPerEdge) continue;
        for(S32 dj = -half_edge; dj <= half_edge; dj++)
        {
            if( (j+dj) < 0 || (j+dj) >= (S32)land.mGridsPerEdge ) continue;
            const F32
                wx = pos_world.mV[VX] + di,
                wy = pos_world.mV[VY] + dj,
                wz = land.getZ((i+di)+(j+dj)*land.mGridsPerEdge),
                norm_dist = sqrt((float)di*di + dj*dj) / half_edge,
                force_scale = sqrt(2.f) - norm_dist, // 1 at center, 0 at corner
                wz2 = wz + .2 + (.2 + force/100) * force_scale, // top vertex
                tic = .075f; // arrowhead size
            // vertical line
            gGL.vertex3f(wx, wy, wz);
            gGL.vertex3f(wx, wy, wz2);
            if(radioAction == E_LAND_RAISE || radioAction == E_LAND_NOISE) // up arrow
            {
                gGL.vertex3f(wx, wy, wz2);
                gGL.vertex3f(wx+tic, wy, wz2-tic);
                gGL.vertex3f(wx, wy, wz2);
                gGL.vertex3f(wx-tic, wy, wz2-tic);
            }
            if(radioAction == E_LAND_LOWER || radioAction == E_LAND_NOISE) // down arrow
            {
                gGL.vertex3f(wx, wy, wz);
                gGL.vertex3f(wx+tic, wy, wz+tic);
                gGL.vertex3f(wx, wy, wz);
                gGL.vertex3f(wx-tic, wy, wz+tic);
            }
            if(radioAction == E_LAND_REVERT || radioAction == E_LAND_SMOOTH) // flat top
            {
                gGL.vertex3f(wx-tic, wy, wz2);
                gGL.vertex3f(wx+tic, wy, wz2);
            }
            if(radioAction == E_LAND_LEVEL || radioAction == E_LAND_SMOOTH) // flat bottom
            {
                gGL.vertex3f(wx-tic, wy, wz);
                gGL.vertex3f(wx+tic, wy, wz);
            }
        }
    }
    gGL.end();

    gGL.popMatrix();
}

void LLToolBrushLand::determineAffectedRegions(region_list_t& regions,
                                               const LLVector3d& spot ) const
{
    LLVector3d corner(spot);
    corner.mdV[VX] -= (mBrushSize / 2);
    corner.mdV[VY] -= (mBrushSize / 2);
    LLViewerRegion* region = NULL;
    region = LLWorld::getInstance()->getRegionFromPosGlobal(corner);
    if(region && regions.find(region) == regions.end())
    {
        regions.insert(region);
    }
    corner.mdV[VY] += mBrushSize;
    region = LLWorld::getInstance()->getRegionFromPosGlobal(corner);
    if(region && regions.find(region) == regions.end())
    {
        regions.insert(region);
    }
    corner.mdV[VX] += mBrushSize;
    region = LLWorld::getInstance()->getRegionFromPosGlobal(corner);
    if(region && regions.find(region) == regions.end())
    {
        regions.insert(region);
    }
    corner.mdV[VY] -= mBrushSize;
    region = LLWorld::getInstance()->getRegionFromPosGlobal(corner);
    if(region && regions.find(region) == regions.end())
    {
        regions.insert(region);
    }
}

// static
void LLToolBrushLand::onIdle( void* brush_tool )
{
    LLToolBrushLand* self = reinterpret_cast<LLToolBrushLand*>(brush_tool);

    if( LLToolMgr::getInstance()->getCurrentTool() == self )
    {
        self->brush();
    }
    else
    {
        gIdleCallbacks.deleteFunction( &LLToolBrushLand::onIdle, self );
    }
}

void LLToolBrushLand::onMouseCaptureLost()
{
    gIdleCallbacks.deleteFunction(&LLToolBrushLand::onIdle, this);
}

// static
void LLToolBrushLand::undo()
{
    for(region_list_t::iterator iter = mLastAffectedRegions.begin();
        iter != mLastAffectedRegions.end(); ++iter)
    {
        LLViewerRegion* regionp = *iter;
        gMessageSystem->newMessageFast(_PREHASH_UndoLand);
        gMessageSystem->nextBlockFast(_PREHASH_AgentData);
        gMessageSystem->addUUIDFast(_PREHASH_AgentID, gAgent.getID() );
        gMessageSystem->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
        gMessageSystem->sendMessage(regionp->getHost());
    }
}

// static
/*
void LLToolBrushLand::redo()
{
    for(region_list_t::iterator iter = mLastAffectedRegions.begin();
        iter != mLastAffectedRegions.end(); ++iter)
    {
        LLViewerRegion* regionp = *iter;
        gMessageSystem->newMessageFast(_PREHASH_RedoLand);
        gMessageSystem->nextBlockFast(_PREHASH_AgentData);
        gMessageSystem->addUUIDFast(_PREHASH_AgentID, gAgent.getID() );
        gMessageSystem->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
        gMessageSystem->sendMessage(regionp->getHost());
    }
}*/

// static
bool LLToolBrushLand::canTerraformRegion(LLViewerRegion* regionp) const
{
    if (!regionp) return false;
    if (regionp->canManageEstate()) return true;
    return !regionp->getRegionFlag(REGION_FLAGS_BLOCK_TERRAFORM);
}

// static
bool LLToolBrushLand::canTerraformParcel(LLViewerRegion* regionp) const
{
    LLParcel* selected_parcel = LLViewerParcelMgr::getInstance()->getHoverParcel();
    bool is_terraform_allowed = false;
    if (selected_parcel)
    {
        bool owner_release = LLViewerParcelMgr::isParcelOwnedByAgent(selected_parcel, GP_LAND_ALLOW_EDIT_LAND);
        is_terraform_allowed = ( gAgent.canManageEstate() || (selected_parcel->getOwnerID() == regionp->getOwner()) || owner_release);
    }

    return is_terraform_allowed;
}


// static
void LLToolBrushLand::alertNoTerraformRegion(LLViewerRegion* regionp)
{
    if (!regionp) return;

    LLSD args;
    args["REGION"] = regionp->getName();
    LLNotificationsUtil::add("RegionNoTerraforming", args);

}

// static
void LLToolBrushLand::alertNoTerraformParcel()
{
    LLParcel* selected_parcel = LLViewerParcelMgr::getInstance()->getHoverParcel();
    if (selected_parcel)
    {
        LLSD args;
        args["PARCEL"] = selected_parcel->getName();
        LLNotificationsUtil::add("ParcelNoTerraforming", args);
    }

}

///============================================================================
/// Local function definitions
///============================================================================