UltimateZoneBuilder/Source/Core/Controls/ImageSelectorControl.cs

282 lines
7.2 KiB
C#
Raw Normal View History

#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.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using CodeImp.DoomBuilder.Data;
using SlimDX;
#endregion
namespace CodeImp.DoomBuilder.Controls
{
2009-05-21 08:18:34 +00:00
/// <summary>
/// Abstract control that provides a list of images.
/// </summary>
public abstract partial class ImageSelectorControl : UserControl
{
#region ================== Variables
2013-03-18 13:52:27 +00:00
public event EventHandler OnValueChanged; //mxd
private MouseButtons button;
private ImageData image; //mxd
private string previousimagename; //mxd
protected bool multipletextures; //mxd
protected bool usepreviews = true; //mxd
#endregion
#region ================== Properties
public string TextureName { get { return name.Text; } set { name.Text = value; } }
public bool UsePreviews { get { return usepreviews; } set { usepreviews = value; } } //mxd
[Browsable(false)]
public bool MultipleTextures { get { return multipletextures; } set { multipletextures = value; } }
#endregion
#region ================== Constructor / Destructor
// Constructor
protected ImageSelectorControl()
{
// Initialize
InitializeComponent();
}
// Setup
public virtual void Initialize()
{
// set the max length of texture names
name.MaxLength = General.Map.Config.MaxTextureNameLength;
if(General.Settings.CapitalizeTextureNames) this.name.CharacterCasing = CharacterCasing.Upper; //mxd
labelSize.BackColor = Color.FromArgb(196, labelSize.BackColor);
}
#endregion
#region ================== Events
// When resized
private void ImageSelectorControl_Resize(object sender, EventArgs e)
{
// Fixed size
preview.Width = this.ClientSize.Width;
preview.Height = this.ClientSize.Height - name.Height - 4;
name.Width = this.ClientSize.Width;
name.Top = this.ClientSize.Height - name.Height;
togglefullname.Left = preview.Right - togglefullname.Width - 1; //mxd
togglefullname.Top = preview.Bottom - togglefullname.Height - 1; //mxd
}
// Layout change
private void ImageSelectorControl_Layout(object sender, LayoutEventArgs e)
{
ImageSelectorControl_Resize(sender, EventArgs.Empty);
}
// Image clicked
private void preview_Click(object sender, EventArgs e)
{
imagebox.BackColor = SystemColors.Highlight;
switch (button)
{
case MouseButtons.Right: name.Text = "-"; break;
case MouseButtons.Left: name.Text = BrowseImage(name.Text); break;
}
}
// Name text changed
private void name_TextChanged(object sender, EventArgs e)
{
// Show it centered
ShowPreview(FindImage(name.Text));
// Update tooltip (mxd)
tooltip.SetToolTip(imagebox, name.Text);
}
// Mouse pressed
private void preview_MouseDown(object sender, MouseEventArgs e)
{
button = e.Button;
if((button == MouseButtons.Left) || ((button == MouseButtons.Right)))
{
imagebox.BackColor = AdjustedColor(SystemColors.Highlight, 0.2f);
}
}
// Mouse leaves
private void preview_MouseLeave(object sender, EventArgs e)
{
imagebox.BackColor = SystemColors.AppWorkspace;
imagebox.Highlighted = false;
}
// Mouse enters
private void preview_MouseEnter(object sender, EventArgs e)
{
imagebox.BackColor = SystemColors.Highlight;
imagebox.Highlighted = true;
}
//mxd
private void timer_Tick(object sender, EventArgs e)
{
Refresh();
}
//mxd
private void ImageSelectorControl_EnabledChanged(object sender, EventArgs e)
{
labelSize.Visible = !(!General.Settings.ShowTextureSizes || !this.Enabled || string.IsNullOrEmpty(labelSize.Text));
}
//mxd
private void togglefullname_Click(object sender, EventArgs e)
{
// Toggle between short and full name
name.Text = (name.Text == image.ShortName ? image.Name : image.ShortName);
// Update icon and tooltip
UpdateToggleImageNameButton(image);
}
#endregion
#region ================== Methods
// This refreshes the control
new public void Refresh()
{
if(General.Map == null) return;
ShowPreview(FindImage(name.Text));
base.Refresh();
}
Added, Texture Browser: added "Show textures in subdirectories" checkbox (enabled by default). When enabled, textures from current PK3/PK7/Directory resource directory and it's subdirectories will be shown. Otherwise, only textures from current directory will be shown. Removed, Texture Browser: removed "Show image sizes" checkbox. "Show texture and flat sizes in browsers" preferences setting is now used instead. Fixed, Things mode: event line between pre-last and the last PatrolPoint was not drawn. Fixed, Things mode: highlight range for sizeless things (things with "fixedsize" game configuration property) was calculated incorrectly. Fixed: fixed a crash when opening Script Editor after using "Open map in current wad" command to switch to UDMF map with SCRIPTS lump when current script configuration was not saved in the wad's .dbs file. Fixed: map closing events were not triggered when using "Open map in current wad" command, which could potentially result in plugin crashes/incorrect behavior. Fixed: Sector Drawing overrides panel could trigger an exception when closing the map during resource loading. Internal: added "Debug + Profiler" solution configuration, added 2 profiling methods to DebugConsole. Internal: rewrote MainForm.DisplayStatus() / StatusInfo to handle selection info in a more structured way. Fixed, internal: some destructors could potentially be executed more than once potentially leading to exceptions. Other destructors were not called at all. Updated ZDoom_DECORATE.cfg.
2015-09-16 12:10:43 +00:00
//mxd
public void StopUpdate()
{
timer.Stop();
}
// This redraws the image preview
private void ShowPreview(Image image)
{
// Dispose old image
imagebox.Image = null;
if(image != null)
{
// Show it centered
imagebox.Image = image;
imagebox.Refresh();
}
2013-03-18 13:52:27 +00:00
//mxd. Dispatch event
if(OnValueChanged != null && previousimagename != name.Text)
{
previousimagename = name.Text;
OnValueChanged(this, EventArgs.Empty);
}
}
//mxd
protected void DisplayImageSize(float width, float height)
{
width = Math.Abs(width);
height = Math.Abs(height);
labelSize.Text = (width > 0 && height > 0) ? width + "x" + height : string.Empty;
ImageSelectorControl_EnabledChanged(this, EventArgs.Empty);
}
// This must determine and return the image to show
protected abstract Image FindImage(string imagename);
// This must show the image browser and return the selected texture name
protected abstract string BrowseImage(string imagename);
protected void UpdateToggleImageNameButton(ImageData image)
{
this.image = image;
// Update visibility
if(!General.Map.Config.UseLongTextureNames || image == null || !image.HasLongName)
{
togglefullname.Visible = false;
return;
}
// Update icon and tooltip
togglefullname.Visible = true;
if (image.ShortName == name.Text)
{
togglefullname.Image = Properties.Resources.Expand;
tooltip.SetToolTip(togglefullname, "Switch to full name");
}
else
{
togglefullname.Image = Properties.Resources.Collapse;
tooltip.SetToolTip(togglefullname, "Switch to short name");
}
}
// This determines the result value
public string GetResult(string original)
{
// Anyting entered?
if(name.Text.Trim().Length > 0)
{
// Return the new value
return name.Text;
}
// Nothing given, keep original value
return original;
}
// This brightens or darkens a color
private static Color AdjustedColor(Color c, float amount)
{
Color4 cc = new Color4(c);
// Adjust color
cc.Red = Saturate((cc.Red * (1f + amount)) + (amount * 0.5f));
cc.Green = Saturate((cc.Green * (1f + amount)) + (amount * 0.5f));
cc.Blue = Saturate((cc.Blue * (1f + amount)) + (amount * 0.5f));
// Return result
return Color.FromArgb(cc.ToArgb());
}
// This clamps a value between 0 and 1
private static float Saturate(float v)
{
if(v < 0f) return 0f;
if(v > 1f) return 1f;
return v;
}
#endregion
}
}