UltimateZoneBuilder/Source/Core/Controls/ImageSelectorControl.cs
MaxED 898c8e6166 Open Map Form was unable to pick game configuration when selected wad contained maps in several map formats.
Texture selector control: fixed a crash when trying to refresh current image when the map was no longer available.
2013-11-27 15:38:03 +00:00

252 lines
6 KiB
C#

#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.Drawing;
using System.Windows.Forms;
using SlimDX;
#endregion
namespace CodeImp.DoomBuilder.Controls
{
/// <summary>
/// Abstract control that provides a list of images.
/// </summary>
public abstract partial class ImageSelectorControl : UserControl
{
#region ================== Variables
public event EventHandler OnValueChanged; //mxd
private Bitmap bmp;
private bool ismouseinside;
private MouseButtons button;
private string previousImageName; //mxd
#endregion
#region ================== Properties
public string TextureName { get { return name.Text; } set { name.Text = value; } }
#endregion
#region ================== Constructor / Destructor
// Constructor
public ImageSelectorControl()
{
// Initialize
InitializeComponent();
}
// Setup
public virtual void Initialize()
{
// set the max length of texture names
name.MaxLength = General.Map.Config.MaxTextureNamelength;
}
#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;
}
// 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)
{
//ispressed = false;
preview.BackColor = SystemColors.Highlight;
ShowPreview(FindImage(name.Text));
if(button == MouseButtons.Right)
{
name.Text = "-";
}
else if(button == MouseButtons.Left)
{
name.Text = BrowseImage(name.Text);
}
}
// Name text changed
private void name_TextChanged(object sender, EventArgs e)
{
// Show it centered
ShowPreview(FindImage(name.Text));
}
// Mouse pressed
private void preview_MouseDown(object sender, MouseEventArgs e)
{
button = e.Button;
if((button == MouseButtons.Left) || ((button == MouseButtons.Right)))
{
//ispressed = true;
preview.BackColor = AdjustedColor(SystemColors.Highlight, 0.2f);
ShowPreview(FindImage(name.Text));
}
}
// Mouse released
private void preview_MouseUp(object sender, MouseEventArgs e)
{
//ispressed = false;
ShowPreview(FindImage(name.Text));
}
// Mouse leaves
private void preview_MouseLeave(object sender, EventArgs e)
{
//ispressed = false;
ismouseinside = false;
preview.BackColor = SystemColors.AppWorkspace;
}
// Mouse enters
private void preview_MouseEnter(object sender, EventArgs e)
{
ismouseinside = true;
preview.BackColor = SystemColors.Highlight;
ShowPreview(FindImage(name.Text));
}
// Mouse moves
private void preview_MouseMove(object sender, MouseEventArgs e)
{
if(!ismouseinside)
{
ismouseinside = true;
preview.BackColor = SystemColors.Highlight;
ShowPreview(FindImage(name.Text));
}
}
//mxd
private void timer_Tick(object sender, EventArgs e) {
Refresh();
}
#endregion
#region ================== Methods
// This refreshes the control
new public void Refresh()
{
if(General.Map == null) return;
ShowPreview(FindImage(name.Text));
base.Refresh();
}
// This redraws the image preview
private void ShowPreview(Image image)
{
// Dispose old image
preview.BackgroundImage = null;
if(bmp != null)
{
bmp.Dispose();
bmp = null;
}
if(image != null)
{
// Show it centered
General.DisplayZoomedImage(preview, image);
preview.Refresh();
}
//mxd. Dispatch event
if(OnValueChanged != null && previousImageName != name.Text) {
previousImageName = name.Text;
OnValueChanged(this, EventArgs.Empty);
}
}
//mxd
protected void DisplayImageSize(float width, float height) {
if(!General.Settings.ShowTextureSizes || !this.Enabled || width == 0 || height == 0) {
labelSize.Visible = false;
return;
}
labelSize.Visible = true;
labelSize.Text = width + "x" + height;
}
// 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);
// 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;
}
else
{
// Nothing given, keep original value
return original;
}
}
// This brightens or darkens a color
private 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 float Saturate(float v)
{
if(v < 0f) return 0f; else if(v > 1f) return 1f; else return v;
}
#endregion
}
}