2009-04-19 18:07:22 +00:00
#region = = = = = = = = = = = = = = = = = = Copyright ( c ) 2007 Pascal vd Heiden
/ *
* Copyright ( c ) 2007 Pascal vd Heiden , www . codeimp . com
* This program is released under GNU General Public License
*
* This program 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 General Public License for more details .
*
* /
#endregion
#region = = = = = = = = = = = = = = = = = = Namespaces
using System ;
using System.Collections.Generic ;
2013-11-21 10:53:11 +00:00
using System.Drawing ;
2009-04-19 18:07:22 +00:00
using System.Windows.Forms ;
using CodeImp.DoomBuilder.Map ;
2012-11-05 12:31:24 +00:00
using CodeImp.DoomBuilder.Types ;
2009-04-19 18:07:22 +00:00
#endregion
namespace CodeImp.DoomBuilder.Windows
{
2013-07-10 08:59:17 +00:00
internal partial class SectorEditForm : DelayedForm
2009-04-19 18:07:22 +00:00
{
2013-07-09 11:29:10 +00:00
#region = = = = = = = = = = = = = = = = = = Events
public event EventHandler OnValuesChanged ; //mxd
#endregion
#region = = = = = = = = = = = = = = = = = = Variables
2009-04-19 18:07:22 +00:00
private ICollection < Sector > sectors ;
2015-01-26 08:53:05 +00:00
private List < SectorProperties > sectorprops ; //mxd
private bool preventchanges ; //mxd
private bool undocreated ; //mxd
2013-07-09 11:29:10 +00:00
private struct SectorProperties //mxd
{
2014-05-05 14:24:57 +00:00
public readonly int Brightness ;
public readonly int FloorHeight ;
public readonly int CeilHeight ;
public readonly string FloorTexture ;
public readonly string CeilTexture ;
2013-07-09 11:29:10 +00:00
2014-12-03 23:15:26 +00:00
public SectorProperties ( Sector s )
{
2013-07-09 11:29:10 +00:00
Brightness = s . Brightness ;
FloorHeight = s . FloorHeight ;
CeilHeight = s . CeilHeight ;
FloorTexture = s . FloorTexture ;
CeilTexture = s . CeilTexture ;
}
}
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor
2009-04-19 18:07:22 +00:00
// Constructor
public SectorEditForm ( )
{
// Initialize
InitializeComponent ( ) ;
// Fill effects list
2014-09-04 12:34:26 +00:00
effect . GeneralizedOptions = General . Map . Config . GenEffectOptions ; //mxd
2009-04-19 18:07:22 +00:00
effect . AddInfo ( General . Map . Config . SortedSectorEffects . ToArray ( ) ) ;
// Initialize image selectors
floortex . Initialize ( ) ;
ceilingtex . Initialize ( ) ;
2009-07-10 19:15:22 +00:00
// Set steps for brightness field
brightness . StepValues = General . Map . Config . BrightnessLevels ;
2009-04-19 18:07:22 +00:00
}
2013-07-09 11:29:10 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Methods
2009-04-19 18:07:22 +00:00
// This sets up the form to edit the given sectors
public void Setup ( ICollection < Sector > sectors )
{
2015-01-26 08:53:05 +00:00
preventchanges = true ; //mxd
2017-02-08 12:18:01 +00:00
undocreated = false ;
// Keep this list
this . sectors = sectors ;
2009-04-19 18:07:22 +00:00
if ( sectors . Count > 1 ) this . Text = "Edit Sectors (" + sectors . Count + ")" ;
2015-01-26 08:53:05 +00:00
sectorprops = new List < SectorProperties > ( ) ; //mxd
2009-04-19 18:07:22 +00:00
2014-09-13 21:57:56 +00:00
//mxd. Set default height offset
heightoffset . Text = "0" ;
2009-04-19 18:07:22 +00:00
////////////////////////////////////////////////////////////////////////
// Set all options to the first sector properties
////////////////////////////////////////////////////////////////////////
// Get first sector
2014-09-13 21:57:56 +00:00
Sector sc = General . GetByIndex ( sectors , 0 ) ;
2009-04-19 18:07:22 +00:00
// Effects
effect . Value = sc . Effect ;
brightness . Text = sc . Brightness . ToString ( ) ;
// Floor/ceiling
floorheight . Text = sc . FloorHeight . ToString ( ) ;
ceilingheight . Text = sc . CeilHeight . ToString ( ) ;
floortex . TextureName = sc . FloorTexture ;
ceilingtex . TextureName = sc . CeilTexture ;
// Action
2013-08-28 08:29:06 +00:00
tagSelector . Setup ( UniversalType . SectorTag ) ; //mxd
2013-03-18 13:52:27 +00:00
tagSelector . SetTag ( sc . Tag ) ; //mxd
2009-04-19 18:07:22 +00:00
////////////////////////////////////////////////////////////////////////
// Now go for all sectors and change the options when a setting is different
////////////////////////////////////////////////////////////////////////
// Go for all sectors
foreach ( Sector s in sectors )
{
// Effects
if ( s . Effect ! = effect . Value ) effect . Empty = true ;
if ( s . Brightness . ToString ( ) ! = brightness . Text ) brightness . Text = "" ;
// Floor/Ceiling
if ( s . FloorHeight . ToString ( ) ! = floorheight . Text ) floorheight . Text = "" ;
if ( s . CeilHeight . ToString ( ) ! = ceilingheight . Text ) ceilingheight . Text = "" ;
2015-12-28 15:01:53 +00:00
if ( s . FloorTexture ! = floortex . TextureName )
2014-12-03 23:15:26 +00:00
{
2014-03-05 09:21:28 +00:00
floortex . MultipleTextures = true ; //mxd
floortex . TextureName = "" ;
}
2015-12-28 15:01:53 +00:00
if ( s . CeilTexture ! = ceilingtex . TextureName )
2014-12-03 23:15:26 +00:00
{
2014-03-05 09:21:28 +00:00
ceilingtex . MultipleTextures = true ; //mxd
ceilingtex . TextureName = "" ;
}
2009-04-19 18:07:22 +00:00
// Action
2013-03-18 13:52:27 +00:00
if ( s . Tag ! = sc . Tag ) tagSelector . ClearTag ( ) ; //mxd
2013-07-09 11:29:10 +00:00
//mxd. Store initial properties
2015-01-26 08:53:05 +00:00
sectorprops . Add ( new SectorProperties ( s ) ) ;
2009-04-19 18:07:22 +00:00
}
// Show sector height
UpdateSectorHeight ( ) ;
2013-07-09 11:29:10 +00:00
2015-01-26 08:53:05 +00:00
preventchanges = false ; //mxd
}
//mxd
private void MakeUndo ( )
{
if ( undocreated ) return ;
undocreated = true ;
//mxd. Make undo
General . Map . UndoRedo . CreateUndo ( "Edit " + ( sectors . Count > 1 ? sectors . Count + " sectors" : "sector" ) ) ;
2009-04-19 18:07:22 +00:00
}
// This updates the sector height field
private void UpdateSectorHeight ( )
{
int delta = 0 ;
2013-09-17 08:21:12 +00:00
int index = - 1 ; //mxd
int i = 0 ; //mxd
2009-04-19 18:07:22 +00:00
// Check all selected sectors
2014-12-03 23:15:26 +00:00
foreach ( Sector s in sectors )
{
if ( index = = - 1 )
{
2009-04-19 18:07:22 +00:00
// First sector in list
delta = s . CeilHeight - s . FloorHeight ;
2013-09-17 08:21:12 +00:00
index = i ; //mxd
2014-12-03 23:15:26 +00:00
}
else if ( delta ! = ( s . CeilHeight - s . FloorHeight ) )
{
2013-09-17 08:21:12 +00:00
// We can't show heights because the delta
// heights for the sectors is different
index = - 1 ;
break ;
2009-04-19 18:07:22 +00:00
}
2013-09-17 08:21:12 +00:00
i + + ;
2009-04-19 18:07:22 +00:00
}
2014-12-03 23:15:26 +00:00
if ( index > - 1 )
{
2015-01-26 08:53:05 +00:00
int fh = floorheight . GetResult ( sectorprops [ index ] . FloorHeight ) ; //mxd
int ch = ceilingheight . GetResult ( sectorprops [ index ] . CeilHeight ) ; //mxd
2009-04-19 18:07:22 +00:00
int height = ch - fh ;
sectorheight . Text = height . ToString ( ) ;
sectorheight . Visible = true ;
sectorheightlabel . Visible = true ;
2014-12-03 23:15:26 +00:00
}
else
{
2009-04-19 18:07:22 +00:00
sectorheight . Visible = false ;
sectorheightlabel . Visible = false ;
}
}
2014-09-13 21:57:56 +00:00
//mxd
private void UpdateCeilingHeight ( )
{
int i = 0 ;
2015-04-01 12:51:26 +00:00
int offset ;
2014-09-13 21:57:56 +00:00
2015-04-01 12:51:26 +00:00
if ( heightoffset . Text = = "++" | | heightoffset . Text = = "--" ) // Raise or lower by sector height
2014-12-03 23:15:26 +00:00
{
2015-04-01 12:51:26 +00:00
int sign = ( heightoffset . Text = = "++" ? 1 : - 1 ) ;
2015-12-28 15:01:53 +00:00
foreach ( Sector s in sectors )
2015-04-01 12:51:26 +00:00
{
offset = sectorprops [ i ] . CeilHeight - sectorprops [ i ] . FloorHeight ;
s . CeilHeight + = offset * sign ;
i + + ;
}
2014-09-13 21:57:56 +00:00
}
2015-04-01 12:51:26 +00:00
else
2014-09-13 21:57:56 +00:00
{
2015-04-01 12:51:26 +00:00
offset = heightoffset . GetResult ( 0 ) ;
//restore values
if ( string . IsNullOrEmpty ( ceilingheight . Text ) )
{
foreach ( Sector s in sectors )
s . CeilHeight = sectorprops [ i + + ] . CeilHeight + offset ;
}
else //update values
{
foreach ( Sector s in sectors )
s . CeilHeight = ceilingheight . GetResult ( sectorprops [ i + + ] . CeilHeight ) + offset ;
}
2014-09-13 21:57:56 +00:00
}
}
//mxd
private void UpdateFloorHeight ( )
{
int i = 0 ;
2015-04-01 12:51:26 +00:00
int offset ;
2014-09-13 21:57:56 +00:00
2015-04-01 12:51:26 +00:00
if ( heightoffset . Text = = "++" | | heightoffset . Text = = "--" )
2014-12-03 23:15:26 +00:00
{
2015-04-01 12:51:26 +00:00
// Raise or lower by sector height
int sign = ( heightoffset . Text = = "++" ? 1 : - 1 ) ;
2015-12-28 15:01:53 +00:00
foreach ( Sector s in sectors )
2015-04-01 12:51:26 +00:00
{
offset = sectorprops [ i ] . CeilHeight - sectorprops [ i ] . FloorHeight ;
s . FloorHeight + = offset * sign ;
i + + ;
}
}
else
2014-09-13 21:57:56 +00:00
{
2015-04-01 12:51:26 +00:00
offset = heightoffset . GetResult ( 0 ) ;
//restore values
if ( string . IsNullOrEmpty ( floorheight . Text ) )
{
2015-12-28 15:01:53 +00:00
foreach ( Sector s in sectors )
2015-04-01 12:51:26 +00:00
s . FloorHeight = sectorprops [ i + + ] . FloorHeight + offset ;
}
else //update values
{
2015-12-28 15:01:53 +00:00
foreach ( Sector s in sectors )
2015-04-01 12:51:26 +00:00
s . FloorHeight = floorheight . GetResult ( sectorprops [ i + + ] . FloorHeight ) + offset ;
}
2014-09-13 21:57:56 +00:00
}
}
2013-07-09 11:29:10 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Events
2009-04-19 18:07:22 +00:00
// OK clicked
private void apply_Click ( object sender , EventArgs e )
{
2013-07-09 11:29:10 +00:00
//mxd. Apply "Static" properties
2009-04-19 18:07:22 +00:00
// Verify the tag
2013-03-18 13:52:27 +00:00
tagSelector . ValidateTag ( ) ; //mxd
2014-12-03 23:15:26 +00:00
if ( ( tagSelector . GetTag ( 0 ) < General . Map . FormatInterface . MinTag ) | | ( tagSelector . GetTag ( 0 ) > General . Map . FormatInterface . MaxTag ) )
{
2009-04-19 18:07:22 +00:00
General . ShowWarningMessage ( "Sector tag must be between " + General . Map . FormatInterface . MinTag + " and " + General . Map . FormatInterface . MaxTag + "." , MessageBoxButtons . OK ) ;
return ;
}
// Verify the effect
2014-12-03 23:15:26 +00:00
if ( ( effect . Value < General . Map . FormatInterface . MinEffect ) | | ( effect . Value > General . Map . FormatInterface . MaxEffect ) )
{
2009-04-19 18:07:22 +00:00
General . ShowWarningMessage ( "Sector effect must be between " + General . Map . FormatInterface . MinEffect + " and " + General . Map . FormatInterface . MaxEffect + "." , MessageBoxButtons . OK ) ;
return ;
}
2015-01-26 08:53:05 +00:00
MakeUndo ( ) ; //mxd
2009-04-19 18:07:22 +00:00
// Go for all sectors
2014-08-05 11:03:55 +00:00
int tagoffset = 0 ; //mxd
2014-12-03 23:15:26 +00:00
foreach ( Sector s in sectors )
{
2009-04-19 18:07:22 +00:00
// Effects
if ( ! effect . Empty ) s . Effect = effect . Value ;
// Action
2014-08-11 08:35:39 +00:00
s . Tag = General . Clamp ( tagSelector . GetSmartTag ( s . Tag , tagoffset + + ) , General . Map . FormatInterface . MinTag , General . Map . FormatInterface . MaxTag ) ; //mxd
2009-04-19 18:07:22 +00:00
}
2013-07-09 11:29:10 +00:00
2009-04-19 18:07:22 +00:00
// Done
General . Map . IsChanged = true ;
2013-07-19 15:30:58 +00:00
if ( OnValuesChanged ! = null ) OnValuesChanged ( this , EventArgs . Empty ) ; //mxd
2009-04-19 18:07:22 +00:00
this . DialogResult = DialogResult . OK ;
this . Close ( ) ;
}
// Cancel clicked
private void cancel_Click ( object sender , EventArgs e )
{
2013-07-09 11:29:10 +00:00
//mxd. perform undo
2015-01-26 08:53:05 +00:00
if ( undocreated ) General . Map . UndoRedo . WithdrawUndo ( ) ;
2013-07-09 11:29:10 +00:00
// And be gone
2009-04-19 18:07:22 +00:00
this . DialogResult = DialogResult . Cancel ;
this . Close ( ) ;
}
// Browse Effect clicked
private void browseeffect_Click ( object sender , EventArgs e )
{
effect . Value = EffectBrowserForm . BrowseEffect ( this , effect . Value ) ;
}
2013-07-09 11:29:10 +00:00
// Help
2014-12-03 23:15:26 +00:00
private void SectorEditForm_HelpRequested ( object sender , HelpEventArgs hlpevent )
{
2013-07-09 11:29:10 +00:00
General . ShowHelp ( "w_sectoredit.html" ) ;
hlpevent . Handled = true ;
}
#endregion
2013-07-19 15:30:58 +00:00
#region = = = = = = = = = = = = = = = = = = mxd . Realtime Events
2013-07-09 11:29:10 +00:00
2009-04-19 18:07:22 +00:00
// Ceiling height changes
private void ceilingheight_TextChanged ( object sender , EventArgs e )
{
2015-01-26 08:53:05 +00:00
if ( preventchanges ) return ;
MakeUndo ( ) ; //mxd
2013-07-09 11:29:10 +00:00
2014-09-13 21:57:56 +00:00
UpdateCeilingHeight ( ) ;
2013-09-17 08:21:12 +00:00
UpdateSectorHeight ( ) ;
2013-07-09 11:29:10 +00:00
General . Map . IsChanged = true ;
if ( OnValuesChanged ! = null ) OnValuesChanged ( this , EventArgs . Empty ) ;
2009-04-19 18:07:22 +00:00
}
// Floor height changes
private void floorheight_TextChanged ( object sender , EventArgs e )
{
2015-01-26 08:53:05 +00:00
if ( preventchanges ) return ;
MakeUndo ( ) ; //mxd
2013-07-09 11:29:10 +00:00
2014-09-13 21:57:56 +00:00
UpdateFloorHeight ( ) ;
UpdateSectorHeight ( ) ;
General . Map . IsChanged = true ;
if ( OnValuesChanged ! = null ) OnValuesChanged ( this , EventArgs . Empty ) ;
}
// Height offset changes
private void heightoffset_WhenTextChanged ( object sender , EventArgs e )
{
2015-01-26 08:53:05 +00:00
if ( preventchanges ) return ;
MakeUndo ( ) ; //mxd
2013-07-09 11:29:10 +00:00
2014-09-13 21:57:56 +00:00
UpdateFloorHeight ( ) ;
UpdateCeilingHeight ( ) ;
2013-09-17 08:21:12 +00:00
UpdateSectorHeight ( ) ;
2013-07-09 11:29:10 +00:00
General . Map . IsChanged = true ;
if ( OnValuesChanged ! = null ) OnValuesChanged ( this , EventArgs . Empty ) ;
2009-04-19 18:07:22 +00:00
}
2014-12-03 23:15:26 +00:00
private void floortex_OnValueChanged ( object sender , EventArgs e )
{
2015-01-26 08:53:05 +00:00
if ( preventchanges ) return ;
MakeUndo ( ) ; //mxd
2013-07-09 11:29:10 +00:00
//restore values
2014-12-03 23:15:26 +00:00
if ( string . IsNullOrEmpty ( floortex . TextureName ) )
{
2013-07-09 11:29:10 +00:00
int i = 0 ;
2015-01-26 08:53:05 +00:00
foreach ( Sector s in sectors ) s . SetFloorTexture ( sectorprops [ i + + ] . FloorTexture ) ;
2014-12-03 23:15:26 +00:00
}
else //update values
{
foreach ( Sector s in sectors ) s . SetFloorTexture ( floortex . GetResult ( s . FloorTexture ) ) ;
2013-07-09 11:29:10 +00:00
}
// Update the used textures
General . Map . Data . UpdateUsedTextures ( ) ;
General . Map . IsChanged = true ;
if ( OnValuesChanged ! = null ) OnValuesChanged ( this , EventArgs . Empty ) ;
}
2014-12-03 23:15:26 +00:00
private void ceilingtex_OnValueChanged ( object sender , EventArgs e )
{
2015-01-26 08:53:05 +00:00
if ( preventchanges ) return ;
MakeUndo ( ) ; //mxd
2013-07-09 11:29:10 +00:00
//restore values
2014-12-03 23:15:26 +00:00
if ( string . IsNullOrEmpty ( ceilingtex . TextureName ) )
{
2013-07-09 11:29:10 +00:00
int i = 0 ;
2015-01-26 08:53:05 +00:00
foreach ( Sector s in sectors ) s . SetCeilTexture ( sectorprops [ i + + ] . CeilTexture ) ;
2014-12-03 23:15:26 +00:00
}
else //update values
{
foreach ( Sector s in sectors ) s . SetCeilTexture ( ceilingtex . GetResult ( s . CeilTexture ) ) ;
2013-07-09 11:29:10 +00:00
}
// Update the used textures
General . Map . Data . UpdateUsedTextures ( ) ;
General . Map . IsChanged = true ;
if ( OnValuesChanged ! = null ) OnValuesChanged ( this , EventArgs . Empty ) ;
2009-04-19 18:07:22 +00:00
}
2013-07-09 11:29:10 +00:00
2014-12-03 23:15:26 +00:00
private void brightness_WhenTextChanged ( object sender , EventArgs e )
{
2015-01-26 08:53:05 +00:00
if ( preventchanges ) return ;
MakeUndo ( ) ; //mxd
2013-08-22 15:30:50 +00:00
int i = 0 ;
2013-07-09 11:29:10 +00:00
//restore values
2014-12-03 23:15:26 +00:00
if ( string . IsNullOrEmpty ( brightness . Text ) )
{
2015-01-26 08:53:05 +00:00
foreach ( Sector s in sectors ) s . Brightness = sectorprops [ i + + ] . Brightness ;
2014-12-03 23:15:26 +00:00
}
else //update values
{
2013-07-09 11:29:10 +00:00
foreach ( Sector s in sectors )
2015-01-26 08:53:05 +00:00
s . Brightness = General . Clamp ( brightness . GetResult ( sectorprops [ i + + ] . Brightness ) , General . Map . FormatInterface . MinBrightness , General . Map . FormatInterface . MaxBrightness ) ;
2013-07-09 11:29:10 +00:00
}
General . Map . IsChanged = true ;
if ( OnValuesChanged ! = null ) OnValuesChanged ( this , EventArgs . Empty ) ;
}
#endregion
2013-11-21 10:53:11 +00:00
2009-04-19 18:07:22 +00:00
}
}