Sign in to follow this  
  • entries
    29
  • comments
    35
  • views
    17847

Some progress

Sign in to follow this  
walle

275 views

Hello everyone, I got some progress, not on the toolbox(or well in a way). I just got my SlidingControl to work, it's basically a control just that if you tell it to slide back and it is docked, well yea then it does just that :)
The control is able to slide in any direction you can dock a control, that is any direction Up, Down, Left and Right. If the control is not docked or docked to fill then it does nothing.
Then I just inherit all my controls that I want to be able to slide from this control, in the level editor it will be at least two, one for the tiles, the "toolbox" and another one for the scripting box. I might add one to the right side to, containing map data, name background image etc.

Well I'm under the impression that no journal is popular without images so I'll post a screen shot and better up, I'll post the executable and the code too [smile].

Here is the image:

To the left is a simple SlidingControl it has the delay of 2 seconds, to the right you see my early toolbox, it is click able and so, you can expand the tabs and select the items, but no more.

And here is the executable:
The file
You need .net 2.0 to run it. It is in .zip format.

And finally the code:
[source lang=c#]
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace MyToolboxTest
{
///
/// Represents a control that slides back to were it is docked after a delay. Only slides if docked
///

class SlidingControl : UserControl
{
#region // Enumerators

///
/// Represents a state the control can be in
///

enum State
{
Open,
Closed,
Opening,
Closing
};

///
/// Represents a direction the control can open/clise in, is based on the DockStyle so it can be changed when the docking changes
///

enum Direction
{
Up = 1,
Down = 2,
Left = 3,
Right = 4
};

#endregion

#region // Private variables

private Timer delayTimer;
private Timer moveTimer;
private State state;
private Direction slideDirection;
private int step;
private Size originalSize;
private bool slide;

#endregion

#region // Constructor

public SlidingControl()
{
delayTimer = new Timer();
delayTimer.Interval = 2000; // wait for 2 seconds before closing
delayTimer.Tick += new EventHandler(delayTimer_Tick);
delayTimer.Start();
moveTimer = new Timer();
moveTimer.Interval = 10;
moveTimer.Tick += new EventHandler(moveTimer_Tick);
state = State.Open;
slideDirection = Direction.Right;
step = 2;
slide = true;

// Windows init
Location = new Point(0, 0);
this.Size = new Size(150, 150);
originalSize = Size;
}

#endregion

#region // Public functions


///
/// Starts the sliding, either open or close
///

public void StartMovement()
{
if (slide)
{
if (state == State.Open)
{
state = State.Closing;
}
else if (state == State.Closed)
{
state = State.Opening;
}

moveTimer.Start();
}
}

#endregion

#region // Event handlers

///
/// This happens when the cursor have been outside the control longer than the delay
///

///
///
void delayTimer_Tick(object sender, EventArgs e)
{
if (slide)
{
if (Dock != DockStyle.None && Dock != DockStyle.Fill)
{
delayTimer.Stop();
StartMovement();
}
}
}

///
/// This is what is happening every move tick (every 10 milliseconds when moveTimer is running)
///

///
///
void moveTimer_Tick(object sender, EventArgs e)
{
if (slide)
{
if (state == State.Opening)
{
if (slideDirection == Direction.Left || slideDirection == Direction.Right)
{
this.Width += step;
if (Width >= originalSize.Width)
{
Width = originalSize.Width;
moveTimer.Stop();
state = State.Open;
}
}
else if (slideDirection == Direction.Up || slideDirection == Direction.Down)
{
this.Height += step;
if (Height >= originalSize.Height)
{
Height = originalSize.Height;
moveTimer.Stop();
state = State.Open;
}
}
}
else if (state == State.Closing)
{
if (slideDirection == Direction.Left || slideDirection == Direction.Right)
{
this.Width -= step;
if (Width <= 1)
{
Width = 1;
moveTimer.Stop();
state = State.Closed;
}
}
else if (slideDirection == Direction.Up || slideDirection == Direction.Down)
{
this.Height -= step;
if (Height <= 1)
{
Height = 1;
moveTimer.Stop();
state = State.Closed;
}
}
}
}
}

#endregion

#region // Properties

///
/// Gets or sets the delay before the control beginst to slide
///

public int Delay
{
get { return this.moveTimer.Interval; }
set { this.moveTimer.Interval = value; }
}

///
/// Gets or sets if the control should slide or not
///

public bool Slide
{
get { return this.slide; }
set { this.slide = value; }
}

///
/// Gets or sets the pixels to open/close the control with
///

public int Step
{
get { return this.step; }
set { this.step = value; }
}

#endregion

#region // Control overrides

protected override void OnMouseEnter(EventArgs e)
{
if (slide)
{
delayTimer.Stop();
moveTimer.Start();
if (state != State.Open) { state = State.Opening; }
base.OnMouseEnter(e);
}
}

protected override void OnMouseLeave(EventArgs e)
{
if (slide)
{
if (Dock != DockStyle.None && Dock != DockStyle.Fill)
{
delayTimer.Start();
}
base.OnMouseLeave(e);
}
}

public override DockStyle Dock
{
get
{
return base.Dock;
}
set
{
if (value != DockStyle.Fill)
{
this.slideDirection = (Direction)value; // This is possible because the Direction enum is based on the DockStyle enum
}
base.Dock = value;
}
}

#endregion
}
}



I hope the code can be useful to someone.

No one told me otherwise on including the script in the tiles, so I'll go for that. That was about it from me today, I'll post a update when I get my toolbox up and running. Until then, cya m8s. :)
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now