2014-01-16 09:32:05 +00:00
#region = = = = = = = = = = = = = = = = = = Namespaces
using System ;
2012-06-04 23:42:13 +00:00
using System.Collections.Generic ;
using System.Drawing ;
using System.Windows.Forms ;
2014-12-03 23:15:26 +00:00
using CodeImp.DoomBuilder.Config ;
2012-06-04 23:42:13 +00:00
using CodeImp.DoomBuilder.Editing ;
using CodeImp.DoomBuilder.Rendering ;
using CodeImp.DoomBuilder.Geometry ;
using CodeImp.DoomBuilder.Actions ;
using CodeImp.DoomBuilder.Windows ;
using CodeImp.DoomBuilder.Map ;
2014-01-16 09:32:05 +00:00
#endregion
namespace CodeImp.DoomBuilder.BuilderModes
2012-06-04 23:42:13 +00:00
{
2013-09-11 09:47:53 +00:00
[ EditMode ( DisplayName = "Draw Rectangle Mode" ,
SwitchAction = "drawrectanglemode" ,
2014-02-28 14:32:20 +00:00
ButtonImage = "DrawRectangleMode.png" , //mxd
2014-02-26 14:11:06 +00:00
ButtonOrder = int . MinValue + 3 , //mxd
ButtonGroup = "000_drawing" , //mxd
2013-09-11 09:47:53 +00:00
AllowCopyPaste = false ,
Volatile = true ,
Optional = false ) ]
public class DrawRectangleMode : DrawGeometryMode
{
2014-01-16 09:32:05 +00:00
#region = = = = = = = = = = = = = = = = = = Variables
protected HintLabel hintLabel ;
2013-09-11 09:47:53 +00:00
protected int bevelWidth ;
protected int currentBevelWidth ;
protected int subdivisions ;
2014-01-16 09:32:05 +00:00
protected int maxSubdivisions ;
protected int minSubdivisions ;
2013-09-11 09:47:53 +00:00
protected string undoName = "Rectangle draw" ;
protected string shapeName = "rectangle" ;
protected Vector2D start ;
protected Vector2D end ;
protected int width ;
protected int height ;
2014-01-16 09:32:05 +00:00
//interface
private DrawRectangleOptionsPanel panel ;
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor / Disposer
2013-09-11 09:47:53 +00:00
2014-12-03 23:15:26 +00:00
public DrawRectangleMode ( )
{
2013-09-11 09:47:53 +00:00
snaptogrid = true ;
2014-12-03 23:15:26 +00:00
SetupInterface ( ) ;
2013-09-11 09:47:53 +00:00
}
2014-12-03 23:15:26 +00:00
public override void Dispose ( )
{
2013-09-11 09:47:53 +00:00
if ( ! isdisposed & & hintLabel ! = null )
hintLabel . Dispose ( ) ;
base . Dispose ( ) ;
}
2014-01-16 09:32:05 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Settings panel
2014-12-03 23:15:26 +00:00
protected virtual void SetupInterface ( )
{
2014-01-16 09:32:05 +00:00
maxSubdivisions = 16 ;
//Add options docker
panel = new DrawRectangleOptionsPanel ( ) ;
panel . MaxSubdivisions = maxSubdivisions ;
2014-02-28 14:32:20 +00:00
panel . MinSubdivisions = minSubdivisions ;
panel . MaxBevelWidth = ( int ) General . Map . FormatInterface . MaxCoordinate ;
panel . MinBevelWidth = ( int ) General . Map . FormatInterface . MinCoordinate ;
2014-01-16 09:32:05 +00:00
panel . OnValueChanged + = OptionsPanelOnValueChanged ;
}
2014-12-03 23:15:26 +00:00
protected virtual void AddInterface ( )
{
2014-02-28 14:32:20 +00:00
panel . Register ( ) ;
2014-01-16 09:32:05 +00:00
bevelWidth = panel . BevelWidth ;
subdivisions = panel . Subdivisions ;
}
2014-12-03 23:15:26 +00:00
protected virtual void RemoveInterface ( )
{
2014-02-28 14:32:20 +00:00
panel . Unregister ( ) ;
2014-01-16 09:32:05 +00:00
}
#endregion
#region = = = = = = = = = = = = = = = = = = Methods
2014-12-03 23:15:26 +00:00
override protected void Update ( )
{
2013-09-11 09:47:53 +00:00
PixelColor stitchcolor = General . Colors . Highlight ;
PixelColor losecolor = General . Colors . Selection ;
2014-08-28 11:16:33 +00:00
snaptogrid = General . Interface . ShiftState ^ General . Interface . SnapToGrid ;
2013-09-11 09:47:53 +00:00
snaptonearest = General . Interface . CtrlState ^ General . Interface . AutoMerge ;
DrawnVertex curp = GetCurrentPosition ( ) ;
float vsize = ( renderer . VertexSize + 1.0f ) / renderer . Scale ;
// Render drawing lines
2014-12-03 23:15:26 +00:00
if ( renderer . StartOverlay ( true ) )
{
2013-09-11 09:47:53 +00:00
PixelColor color = snaptonearest ? stitchcolor : losecolor ;
2014-12-03 23:15:26 +00:00
if ( points . Count = = 1 )
{
UpdateReferencePoints ( points [ 0 ] , curp ) ;
Vector2D [ ] shape = GetShape ( start , end ) ;
2013-09-11 09:47:53 +00:00
//render shape
for ( int i = 1 ; i < shape . Length ; i + + )
renderer . RenderLine ( shape [ i - 1 ] , shape [ i ] , LINE_THICKNESS , color , true ) ;
//vertices
for ( int i = 0 ; i < shape . Length ; i + + )
renderer . RenderRectangleFilled ( new RectangleF ( shape [ i ] . x - vsize , shape [ i ] . y - vsize , vsize * 2.0f , vsize * 2.0f ) , color , true ) ;
//and labels
Vector2D [ ] labelCoords = new [ ] { start , new Vector2D ( end . x , start . y ) , end , new Vector2D ( start . x , end . y ) , start } ;
2014-12-03 23:15:26 +00:00
for ( int i = 1 ; i < 5 ; i + + )
{
2014-11-12 11:22:14 +00:00
SetLabelPosition ( labels [ i - 1 ] , labelCoords [ i ] , labelCoords [ i - 1 ] ) ;
2013-09-11 09:47:53 +00:00
renderer . RenderText ( labels [ i - 1 ] . TextLabel ) ;
}
//got beveled corners?
2014-12-03 23:15:26 +00:00
if ( shape . Length > 5 )
{
2013-09-11 09:47:53 +00:00
//render hint
2014-12-03 23:15:26 +00:00
if ( width > 64 * vsize & & height > 16 * vsize )
{
2013-09-11 09:47:53 +00:00
float vPos = start . y + height / 2.0f ;
hintLabel . Start = new Vector2D ( start . x , vPos ) ;
hintLabel . End = new Vector2D ( end . x , vPos ) ;
2014-12-03 23:15:26 +00:00
hintLabel . Text = GetHintText ( ) ;
2013-09-11 09:47:53 +00:00
renderer . RenderText ( hintLabel . TextLabel ) ;
}
//and shape corners
for ( int i = 0 ; i < 4 ; i + + )
2014-01-16 09:32:05 +00:00
renderer . RenderRectangleFilled ( new RectangleF ( labelCoords [ i ] . x - vsize , labelCoords [ i ] . y - vsize , vsize * 2.0f , vsize * 2.0f ) , General . Colors . InfoLine , true ) ;
2013-09-11 09:47:53 +00:00
}
2014-12-03 23:15:26 +00:00
}
else
{
2013-09-11 09:47:53 +00:00
// Render vertex at cursor
renderer . RenderRectangleFilled ( new RectangleF ( curp . pos . x - vsize , curp . pos . y - vsize , vsize * 2.0f , vsize * 2.0f ) , color , true ) ;
}
// Done
renderer . Finish ( ) ;
}
// Done
renderer . Present ( ) ;
}
2014-12-03 23:15:26 +00:00
protected virtual Vector2D [ ] GetShape ( Vector2D pStart , Vector2D pEnd )
{
2013-09-11 09:47:53 +00:00
//no shape
2014-12-03 23:15:26 +00:00
if ( pStart = = pEnd )
{
2013-09-11 09:47:53 +00:00
currentBevelWidth = 0 ;
return new Vector2D [ 0 ] ;
}
2012-06-06 00:38:28 +00:00
2013-03-18 13:52:27 +00:00
//line
2014-12-03 23:15:26 +00:00
if ( pEnd . x = = pStart . x | | pEnd . y = = pStart . y )
{
2013-03-18 13:52:27 +00:00
currentBevelWidth = 0 ;
2013-09-09 14:03:02 +00:00
return new [ ] { pStart , pEnd } ;
2013-03-18 13:52:27 +00:00
}
2013-09-11 09:47:53 +00:00
//no corners
2014-12-03 23:15:26 +00:00
if ( bevelWidth = = 0 )
{
2013-09-11 09:47:53 +00:00
currentBevelWidth = 0 ;
2014-02-03 11:19:12 +00:00
return new [ ] { pStart , new Vector2D ( ( int ) pStart . x , ( int ) pEnd . y ) , pEnd , new Vector2D ( ( int ) pEnd . x , ( int ) pStart . y ) , pStart } ;
2013-09-11 09:47:53 +00:00
}
2012-06-06 00:38:28 +00:00
2014-02-03 11:19:12 +00:00
//got corners. TODO: check point order
2013-09-11 09:47:53 +00:00
bool reverse = false ;
currentBevelWidth = Math . Min ( Math . Abs ( bevelWidth ) , Math . Min ( width , height ) / 2 ) ;
2014-12-03 23:15:26 +00:00
if ( bevelWidth < 0 )
{
2013-09-11 09:47:53 +00:00
currentBevelWidth * = - 1 ;
reverse = true ;
}
2012-06-06 00:38:28 +00:00
2014-01-16 09:32:05 +00:00
List < Vector2D > shape = new List < Vector2D > ( ) ;
2012-06-06 00:38:28 +00:00
2013-09-11 09:47:53 +00:00
//top-left corner
2014-12-03 23:15:26 +00:00
shape . AddRange ( GetCornerPoints ( pStart , currentBevelWidth , currentBevelWidth , ! reverse ) ) ;
2012-06-06 00:38:28 +00:00
2013-09-11 09:47:53 +00:00
//top-right corner
2014-12-03 23:15:26 +00:00
shape . AddRange ( GetCornerPoints ( new Vector2D ( pEnd . x , pStart . y ) , - currentBevelWidth , currentBevelWidth , reverse ) ) ;
2012-06-06 00:38:28 +00:00
2013-09-11 09:47:53 +00:00
//bottom-right corner
2014-12-03 23:15:26 +00:00
shape . AddRange ( GetCornerPoints ( pEnd , - currentBevelWidth , - currentBevelWidth , ! reverse ) ) ;
2012-06-06 00:38:28 +00:00
2013-09-11 09:47:53 +00:00
//bottom-left corner
2014-12-03 23:15:26 +00:00
shape . AddRange ( GetCornerPoints ( new Vector2D ( pStart . x , pEnd . y ) , currentBevelWidth , - currentBevelWidth , reverse ) ) ;
2012-06-06 00:38:28 +00:00
2013-09-11 09:47:53 +00:00
//closing point
2014-01-16 09:32:05 +00:00
shape . Add ( shape [ 0 ] ) ;
2012-06-06 00:38:28 +00:00
2014-01-16 09:32:05 +00:00
return shape . ToArray ( ) ;
2013-09-11 09:47:53 +00:00
}
2012-06-06 00:38:28 +00:00
2014-12-03 23:15:26 +00:00
private Vector2D [ ] GetCornerPoints ( Vector2D startPoint , int bevel_width , int bevel_height , bool reverse )
{
2013-09-11 09:47:53 +00:00
Vector2D [ ] points ;
Vector2D center = ( bevelWidth > 0 ? new Vector2D ( startPoint . x + bevel_width , startPoint . y + bevel_height ) : startPoint ) ;
float curAngle = Angle2D . PI ;
2012-06-06 00:38:28 +00:00
2013-09-11 09:47:53 +00:00
int steps = subdivisions + 2 ;
points = new Vector2D [ steps ] ;
2013-04-11 09:27:16 +00:00
float stepAngle = Angle2D . PIHALF / ( subdivisions + 1 ) ;
2012-06-06 00:38:28 +00:00
2014-12-03 23:15:26 +00:00
for ( int i = 0 ; i < steps ; i + + )
{
2013-09-11 09:47:53 +00:00
points [ i ] = new Vector2D ( center . x + ( float ) Math . Sin ( curAngle ) * bevel_width , center . y + ( float ) Math . Cos ( curAngle ) * bevel_height ) ;
curAngle + = stepAngle ;
}
if ( reverse ) Array . Reverse ( points ) ;
return points ;
}
2014-12-03 23:15:26 +00:00
protected virtual string GetHintText ( )
{
2013-09-11 09:47:53 +00:00
return "BVL: " + bevelWidth + "; SUB: " + subdivisions ;
}
//update top-left and bottom-right points, which define drawing shape
2014-12-03 23:15:26 +00:00
private void UpdateReferencePoints ( DrawnVertex p1 , DrawnVertex p2 )
{
2014-01-17 09:44:08 +00:00
if ( ! p1 . pos . IsFinite ( ) | | ! p2 . pos . IsFinite ( ) ) return ;
2014-12-03 23:15:26 +00:00
if ( p1 . pos . x < p2 . pos . x )
{
2013-09-11 09:47:53 +00:00
start . x = p1 . pos . x ;
end . x = p2 . pos . x ;
2014-12-03 23:15:26 +00:00
}
else
{
2013-09-11 09:47:53 +00:00
start . x = p2 . pos . x ;
end . x = p1 . pos . x ;
}
2014-12-03 23:15:26 +00:00
if ( p1 . pos . y < p2 . pos . y )
{
2013-09-11 09:47:53 +00:00
start . y = p1 . pos . y ;
end . y = p2 . pos . y ;
2014-12-03 23:15:26 +00:00
}
else
{
2013-09-11 09:47:53 +00:00
start . y = p2 . pos . y ;
end . y = p1 . pos . y ;
}
width = ( int ) ( end . x - start . x ) ;
height = ( int ) ( end . y - start . y ) ;
}
// This draws a point at a specific location
2014-12-03 23:15:26 +00:00
override public bool DrawPointAt ( Vector2D pos , bool stitch , bool stitchline )
{
2013-09-11 09:47:53 +00:00
if ( pos . x < General . Map . Config . LeftBoundary | | pos . x > General . Map . Config . RightBoundary | |
pos . y > General . Map . Config . TopBoundary | | pos . y < General . Map . Config . BottomBoundary )
return false ;
DrawnVertex newpoint = new DrawnVertex ( ) ;
newpoint . pos = pos ;
newpoint . stitch = true ; //stitch
newpoint . stitchline = stitchline ;
points . Add ( newpoint ) ;
2014-12-03 23:15:26 +00:00
if ( points . Count = = 1 ) //add point and labels
{
2013-09-09 14:03:02 +00:00
labels . AddRange ( new [ ] { new LineLengthLabel ( false ) , new LineLengthLabel ( false ) , new LineLengthLabel ( false ) , new LineLengthLabel ( false ) } ) ;
2013-09-11 09:47:53 +00:00
hintLabel = new HintLabel ( ) ;
Update ( ) ;
2014-12-03 23:15:26 +00:00
}
else if ( points [ 0 ] . pos = = points [ 1 ] . pos ) //nothing is drawn
{
2013-09-11 09:47:53 +00:00
points = new List < DrawnVertex > ( ) ;
FinishDraw ( ) ;
2014-12-03 23:15:26 +00:00
}
else
{
2013-09-11 09:47:53 +00:00
//create vertices for final shape.
2014-12-03 23:15:26 +00:00
UpdateReferencePoints ( points [ 0 ] , newpoint ) ;
2013-09-11 09:47:53 +00:00
points = new List < DrawnVertex > ( ) ; //clear points
2014-12-03 23:15:26 +00:00
Vector2D [ ] shape = GetShape ( start , end ) ;
2013-09-11 09:47:53 +00:00
2014-12-03 23:15:26 +00:00
foreach ( Vector2D t in shape ) base . DrawPointAt ( t , true , true ) ;
2013-09-11 09:47:53 +00:00
FinishDraw ( ) ;
}
return true ;
}
2014-12-03 23:15:26 +00:00
override public void RemovePoint ( )
{
2013-09-11 09:47:53 +00:00
if ( points . Count > 0 ) points . RemoveAt ( points . Count - 1 ) ;
if ( labels . Count > 0 ) labels = new List < LineLengthLabel > ( ) ;
Update ( ) ;
}
2012-06-06 00:38:28 +00:00
2014-01-16 09:32:05 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Events
2014-12-03 23:15:26 +00:00
public override void OnEngage ( )
{
2014-01-16 09:32:05 +00:00
base . OnEngage ( ) ;
2014-12-03 23:15:26 +00:00
AddInterface ( ) ;
2014-01-16 09:32:05 +00:00
}
2014-12-03 23:15:26 +00:00
public override void OnDisengage ( )
{
RemoveInterface ( ) ;
2014-01-16 09:32:05 +00:00
base . OnDisengage ( ) ;
}
2014-12-03 23:15:26 +00:00
override public void OnAccept ( )
{
2013-09-11 09:47:53 +00:00
Cursor . Current = Cursors . AppStarting ;
General . Settings . FindDefaultDrawSettings ( ) ;
2012-06-04 23:42:13 +00:00
2013-09-11 09:47:53 +00:00
// When we have a rectangle or a line
2014-12-03 23:15:26 +00:00
if ( points . Count > 4 | | points . Count = = 2 )
{
2013-09-11 09:47:53 +00:00
// Make undo for the draw
General . Map . UndoRedo . CreateUndo ( undoName ) ;
2012-06-04 23:42:13 +00:00
2013-09-11 09:47:53 +00:00
// Make an analysis and show info
string [ ] adjectives = new [ ] { "gloomy" , "sad" , "unhappy" , "lonely" , "troubled" , "depressed" , "heartsick" , "glum" , "pessimistic" , "bitter" , "downcast" } ; // aaand my english vocabulary ends here :)
string word = adjectives [ new Random ( ) . Next ( adjectives . Length - 1 ) ] ;
string a = ( word [ 0 ] = = 'u' ? "an " : "a " ) ;
2012-06-04 23:42:13 +00:00
2014-12-03 23:15:26 +00:00
General . Interface . DisplayStatus ( StatusType . Action , "Created " + a + word + " " + shapeName + "." ) ;
2012-06-04 23:42:13 +00:00
2013-09-11 09:47:53 +00:00
// Make the drawing
2014-12-03 23:15:26 +00:00
if ( ! Tools . DrawLines ( points , true , BuilderPlug . Me . AutoAlignTextureOffsetsOnCreate ) )
{
2013-09-11 09:47:53 +00:00
// Drawing failed
// NOTE: I have to call this twice, because the first time only cancels this volatile mode
General . Map . UndoRedo . WithdrawUndo ( ) ;
General . Map . UndoRedo . WithdrawUndo ( ) ;
return ;
}
2012-06-04 23:42:13 +00:00
2013-09-11 09:47:53 +00:00
// Snap to map format accuracy
General . Map . Map . SnapAllToAccuracy ( ) ;
2012-06-04 23:42:13 +00:00
2013-09-11 09:47:53 +00:00
// Clear selection
General . Map . Map . ClearAllSelected ( ) ;
2012-06-04 23:42:13 +00:00
2013-09-11 09:47:53 +00:00
// Update cached values
General . Map . Map . Update ( ) ;
2012-06-04 23:42:13 +00:00
2013-09-11 09:47:53 +00:00
// Edit new sectors?
List < Sector > newsectors = General . Map . Map . GetMarkedSectors ( true ) ;
if ( BuilderPlug . Me . EditNewSector & & ( newsectors . Count > 0 ) )
General . Interface . ShowEditSectors ( newsectors ) ;
2012-06-04 23:42:13 +00:00
2013-09-11 09:47:53 +00:00
// Update the used textures
General . Map . Data . UpdateUsedTextures ( ) ;
2012-06-04 23:42:13 +00:00
2013-09-11 09:47:53 +00:00
// Map is changed
General . Map . IsChanged = true ;
}
2012-06-04 23:42:13 +00:00
2013-09-11 09:47:53 +00:00
// Done
Cursor . Current = Cursors . Default ;
2012-06-04 23:42:13 +00:00
2013-09-11 09:47:53 +00:00
// Return to original mode
General . Editing . ChangeMode ( General . Editing . PreviousStableMode . Name ) ;
}
2012-06-04 23:42:13 +00:00
2014-12-03 23:15:26 +00:00
public override void OnHelp ( )
{
2014-02-10 09:26:13 +00:00
General . ShowHelp ( "/gzdb/features/classic_modes/mode_drawrect.html" ) ;
}
2014-12-03 23:15:26 +00:00
private void OptionsPanelOnValueChanged ( object sender , EventArgs eventArgs )
{
2014-01-16 09:32:05 +00:00
bevelWidth = panel . BevelWidth ;
subdivisions = panel . Subdivisions ;
Update ( ) ;
}
#endregion
#region = = = = = = = = = = = = = = = = = = Actions
2013-09-11 09:47:53 +00:00
[BeginAction("increasesubdivlevel")]
2014-12-03 23:15:26 +00:00
protected virtual void IncreaseSubdivLevel ( )
{
if ( subdivisions < maxSubdivisions )
{
2013-09-11 09:47:53 +00:00
subdivisions + + ;
2014-01-16 09:32:05 +00:00
panel . Subdivisions = subdivisions ;
2013-09-11 09:47:53 +00:00
Update ( ) ;
}
}
[BeginAction("decreasesubdivlevel")]
2014-12-03 23:15:26 +00:00
protected virtual void DecreaseSubdivLevel ( )
{
if ( subdivisions > minSubdivisions )
{
2013-09-11 09:47:53 +00:00
subdivisions - - ;
2014-01-16 09:32:05 +00:00
panel . Subdivisions = subdivisions ;
2013-09-11 09:47:53 +00:00
Update ( ) ;
}
}
[BeginAction("increasebevel")]
2014-12-03 23:15:26 +00:00
protected virtual void IncreaseBevel ( )
{
if ( points . Count < 2 | | currentBevelWidth = = bevelWidth | | bevelWidth < 0 )
{
2014-02-28 14:32:20 +00:00
bevelWidth = Math . Min ( bevelWidth + General . Map . Grid . GridSize , panel . MaxBevelWidth ) ;
2014-01-16 09:32:05 +00:00
panel . BevelWidth = bevelWidth ;
2013-09-11 09:47:53 +00:00
Update ( ) ;
}
}
[BeginAction("decreasebevel")]
2014-12-03 23:15:26 +00:00
protected virtual void DecreaseBevel ( )
{
if ( currentBevelWidth = = bevelWidth | | bevelWidth > 0 )
{
2014-02-28 14:32:20 +00:00
bevelWidth = Math . Max ( bevelWidth - General . Map . Grid . GridSize , panel . MinBevelWidth ) ;
2014-01-16 09:32:05 +00:00
panel . BevelWidth = bevelWidth ;
2013-09-11 09:47:53 +00:00
Update ( ) ;
}
}
2014-01-16 09:32:05 +00:00
#endregion
2013-09-11 09:47:53 +00:00
}
2012-06-04 23:42:13 +00:00
}