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.Drawing;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
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>
|
2009-04-19 18:07:22 +00:00
|
|
|
public abstract partial class ImageSelectorControl : UserControl
|
|
|
|
{
|
|
|
|
#region ================== Variables
|
|
|
|
|
2013-03-18 13:52:27 +00:00
|
|
|
public event EventHandler OnValueChanged; //mxd
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
private Bitmap bmp;
|
|
|
|
private bool ismouseinside;
|
|
|
|
private MouseButtons button;
|
2013-07-09 11:29:10 +00:00
|
|
|
private string previousImageName; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
|
|
|
public string TextureName { get { return name.Text; } set { name.Text = value; } }
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Destructor
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
public ImageSelectorControl()
|
|
|
|
{
|
|
|
|
// Initialize
|
|
|
|
InitializeComponent();
|
2009-07-09 22:43:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup
|
|
|
|
public virtual void Initialize()
|
|
|
|
{
|
2009-06-08 18:52:56 +00:00
|
|
|
// set the max length of texture names
|
|
|
|
name.MaxLength = General.Map.Config.MaxTextureNamelength;
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
2012-07-28 20:36:28 +00:00
|
|
|
//ispressed = false;
|
2009-04-19 18:07:22 +00:00
|
|
|
preview.BackColor = SystemColors.Highlight;
|
|
|
|
ShowPreview(FindImage(name.Text));
|
|
|
|
if(button == MouseButtons.Right)
|
|
|
|
{
|
2013-08-26 10:51:33 +00:00
|
|
|
name.Text = "-";
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
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;
|
2013-08-26 10:51:33 +00:00
|
|
|
if((button == MouseButtons.Left) || ((button == MouseButtons.Right)))
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
2012-07-28 20:36:28 +00:00
|
|
|
//ispressed = true;
|
2009-04-19 18:07:22 +00:00
|
|
|
preview.BackColor = AdjustedColor(SystemColors.Highlight, 0.2f);
|
|
|
|
ShowPreview(FindImage(name.Text));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mouse released
|
|
|
|
private void preview_MouseUp(object sender, MouseEventArgs e)
|
|
|
|
{
|
2012-07-28 20:36:28 +00:00
|
|
|
//ispressed = false;
|
2009-04-19 18:07:22 +00:00
|
|
|
ShowPreview(FindImage(name.Text));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mouse leaves
|
|
|
|
private void preview_MouseLeave(object sender, EventArgs e)
|
|
|
|
{
|
2012-07-28 20:36:28 +00:00
|
|
|
//ispressed = false;
|
2009-04-19 18:07:22 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
// This refreshes the control
|
|
|
|
new public void Refresh()
|
|
|
|
{
|
|
|
|
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();
|
2013-07-09 11:29:10 +00:00
|
|
|
}
|
2013-03-18 13:52:27 +00:00
|
|
|
|
2013-07-09 11:29:10 +00:00
|
|
|
//mxd. Dispatch event
|
|
|
|
if(OnValueChanged != null && previousImageName != name.Text) {
|
|
|
|
previousImageName = name.Text;
|
|
|
|
OnValueChanged(this, EventArgs.Empty);
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-17 13:17:53 +00:00
|
|
|
|
|
|
|
//mxd
|
2013-06-17 14:09:29 +00:00
|
|
|
protected void DisplayImageSize(float width, float height) {
|
2013-07-08 13:13:28 +00:00
|
|
|
if(!General.Settings.ShowTextureSizes || width == 0 || height == 0) {
|
2013-06-17 13:17:53 +00:00
|
|
|
labelSize.Visible = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
labelSize.Visible = true;
|
|
|
|
labelSize.Text = width + "x" + height;
|
|
|
|
}
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|