Using other Joysticks / GamePads in XNA.

So I have been wanting to use A gamepad that is not an XBOX 360 gamepad. As I don’t have an XBOX and didn’t feel like
getting an XBOX controller, when I have a perfectly good gamepad already. There where a couple of libraries already that used MDX.

But you have to include the DirectInput library and have to turn off LoaderLock errors. I didn’t want to do that, so I wrote my
own simple Joystick library that uses Win32’s joyGetPosEx function. It is simple gets 5 axes and upto 32 buttons, and a pov
hat. So that is more then enough for what I wanted so I got the Axes and Button support put together in my library.

The library is small and easy to use, see the example below.

JoystickState joyState = Joystick.GetState(JoystickID.Joystick1);
float x = joyState.Axis.X;
float y = joyState.Axis.Y;

bool button1 = (joyState.Buttons.Button1 == ButtonState.Pressed);
bool button2 = (joyState.Buttons.Button2 == ButtonState.Pressed);
bool button30 = (joyState.Buttons.Button30 == ButtonState.Pressed);

and it is as easy as that. It is similar to the GamePad library that comes with XNA.

Though this only works on a PC running Windows XP or better.
Besides these gamepad’s / joysticks are not even supported on the XBOX.

Also since it does not use DirectInput there is no support for rumbling or
anyother controller features, just 5 axes and 32 buttons.

JoystickLib.zip
Filesize: 9kb

The Database Class.

Everyone probably has a favorite Database library they use, whether you use something like
a database abstraction layer like this or use
something homebuilt or just use direct database access throughout your scripts (which
if you are the latter you should really look into some kind of library, make things easier on yourself)
you have someway that you like to connect to databases.

Well I use my own database script, and that is what this post is for just putting this out there for
anyone who might like it. It is a simple single class script, using it is quite simple as well, so check
it out below.

So to show you how using the script works I have posted a couple of examples below.

require_once "Database.php";
$db = new Database("localhost", "user", "pass", "dbname");

$db->where('title', 'Test')->orderBy('id')->get('myrows');

while($row = $db->read()) {
	echo $row['id'] . " - " . $row['title'] . "<br />";
}

Now it is quite simple how it works.

Firstly we tell the database class that we are going to be using a where statment which will be
formated as (WHERE ‘title’ = ‘Test’ you can use whereLike to get WHERE ‘title’ LIKE ‘Test’) after
that we tell it to order the results by the id (ORDER BY id) and then get all columns from the table
myrows so the whole sql query will look like this.

SELECT * FROM myrows WHERE title = 'Test' ORDER BY id;

Now lets say we want to get only the id and title of the rows well we can do this then.

$db->where('title', 'Test')->orderBy('id')->getSelect('myrows', 'id, title');
$rows = $db->readToEnd();

Simply as that, there are a couple of other methods and such which you can find out about by
reading though the comments and such in the code. Enjoy.

Database.zip
Filesize: 3kb

 

Extending .NET Controls. Part 1 - CustomListBox

Sometimes you need a control, not just any control but something that is a little of that and a little
of this, but you just can’t find what you need. So do you build your own control? You could but that
is only if you want something that is completely your’s built from the ground up how you want it.

But what if all you need is this control with something else added, then why not just
extend that control, override one maybe two functions and have all new functionality of that control?

Well that is what I’m going to be doing here today. I will be extending a ListBox control to create
a custom listbox that has your standard Text like a regular listbox but also has a Description text
that will site below your main text, we will also be adding an image to it that will show up on the left
side, so lets get started.

First before we start let me point out a couple of requirements for this task.

  1. You’ll need at least the .NET Framework 2.0+ or Mono 2.0+
  2. You’ll need a IDE like Visual Studio, SharpDevelop, or MonoDevelop, or an Editor and the C# compiler.
  3. You’ll need to have experience in C# and .NET. Although all the code is on this page it you want to use this article to extend other controls you will need the know-how to do so.

So lets get our CustomListBox class all setup. Here is what it looks like.

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Window.Forms;

namespace Example.Controls {
	public class CustomListBox : ListBox {
		public CustomListBox() {
			this.ItemHeight = 35;
			this.DrawMode = DrawMode.OwnerDrawFixed;
		}
		
	}
}

Obviously you can set-up your namespace to whatever you want it to be.

So that is what we have so far. Notice that how declare are class CustomListBox
and then also extend the ListBox class. This is where all of our functionality will
be coming from so we don’t have to worry about coding in scrolling, and all those other little
things.

Now before be go any further on are ListBox class lets create a new class that will store
are information for each listbox item. I will call this class CustomListBoxItem
but you can choose whatever you want.

So here is what that class looks like.

	public class CustomListBoxItem {
		int imageIndex = -1;
		string text;
		string description;
		
		public int ImageIndex {
			get {
				return imageIndex;
			}
			set {
				imageIndex = value;
			}
		}
		
		public string Text {
			get {
				return text;
			}
			set {
				text = value;
			}
		}
		
		public string Description {
			get {
				return description;
			}
			set {
				description = value;
			}
		}
	}

Now that we have information for our Items we can get to work on drawing
are new ListBox.

So one thing we will want to do is add a ImageList to are ListBox class like so.

ImageList imageList1;

public ImageList ImageList {
	get {
		return imageList1;
	}
	set {
		imageList1 = value;
	}
}

We will place that inside of our CustomListBox class just above the class Constructor.

Now the only thing left to do is override the OnDrawItem method and draw are items.

protected override void OnDrawItem(DrawItemEventArgs e) {
	if(this.Items.Count > 0) {
		CustomListBoxItem item = (CustomListBoxItem)this.Items[e.Index];
			
		e.DrawBackground();
		e.DrawFocusRectangle();
		Color foreColor = this.ForeColor;
		int xOffset = 2;
				
		if((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
			foreColor = SystemColors.HighlightText;
		}
				
		if(item.ImageIndex >= 0 && imageList1.Images.Count > 0) {
			e.Graphics.DrawImage(imageList1.Images[item.ImageIndex], 2, e.Bounds.Y + 2);
			xOffset += imageList1.Images[item.ImageIndex].Width + 2;
		}
				
		e.Graphics.DrawString(item.Text, this.Font, new SolidBrush(foreColor), new Point(xOffset, e.Bounds.Y + 5));
		e.Graphics.DrawString(item.Description, this.Font, new SolidBrush(Color.LightGray), new Point(xOffset, e.Bounds.Y + 20));
	}
}

So as you can see we first check to see that the ListBox has items so we don’t get an
error while trying to call a item that does not exists.

After that we need to actually get are item out of the listbox items.

Which is where this comes into play.

CustomListBoxItem item = (CustomListBoxItem)this.Items[e.Index];

The ListBoxItemCollection is a collection of objects so you will need to cast the object
as your class, just make sure you only add your class to the items.

Next we call e.DrawBackground() and e.DrawFocusRectangle(),
these will take care of drawing are background when the item is selected and the focus
rectangle.

After this we check to see if the item is selected so we can use the correct font color.

Then we move on to checking if we have an image to use, and if we do we then draw the image
and change the offset for the text.

After that all we have to do is draw are text, note how we use e.Bounds.Y + 5 to draw the y position
of the text in each item. This is because when we draw the text it is relative to the control position
and not the item position so we add e.Bounds.Y to fix that.

Also you may want to use a different font for the description maybe one smaller or something like that.
And that’s all you can compile your control into a .DLL or include the source file in your application
to use your new control, Enjoy.

Here is a screenshot of what the ListBox looks like.

For you convience both classes.

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Example {
	public class CustomListBox : ListBox {
		ImageList imageList1;
		
		public ImageList ImageList {
			get {
				return imageList1;
			}
			set {
				imageList1 = value;
			}
		}
		
		public CustomListBox() {
			this.ItemHeight = 35;
			this.DrawMode = DrawMode.OwnerDrawFixed;
		}
		
		protected override void OnDrawItem(DrawItemEventArgs e) {
			if(this.Items.Count > 0) {
				CustomListBoxItem item = (CustomListBoxItem)this.Items[e.Index];
			
				e.DrawBackground();
				e.DrawFocusRectangle();
				Color foreColor = this.ForeColor;
				int xOffset = 2;
				
				if((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
					foreColor = SystemColors.HighlightText;
				}
				
				if(item.ImageIndex >= 0 && imageList1.Images.Count > 0) {
					e.Graphics.DrawImage(imageList1.Images[item.ImageIndex], 2, e.Bounds.Y + 2);
					xOffset += imageList1.Images[item.ImageIndex].Width + 2;
				}
				
				e.Graphics.DrawString(item.Text, this.Font, new SolidBrush(foreColor), new Point(xOffset, e.Bounds.Y + 5));
				e.Graphics.DrawString(item.Description, this.Font, new SolidBrush(Color.LightGray), new Point(xOffset, e.Bounds.Y + 20));
			}
		}
	}
	
	public class CustomListBoxItem {
		int imageIndex = -1;
		string text;
		string description;
		
		public int ImageIndex {
			get {
				return imageIndex;
			}
			set {
				imageIndex = value;
			}
		}
		
		public string Text {
			get {
				return text;
			}
			set {
				text = value;
			}
		}
		
		public string Description {
			get {
				return description;
			}
			set {
				description = value;
			}
		}
	}
}

Web Edit Alpha.

Hello again.

This is just a quick post announcing that my Web Edit edit is being released in a Alpha/Beta status,
the editor works fine, there are a couple of things here and there that are a little hinky by overall it is
working quite well.

A quick bit on the requirements and a download.

Requirements include.

  • Windows XP or later
  • .NET Framework 3.5 (I can change it so it only depends on the 2.0 framework)
  • About 2MB harddrive spaces
  • Some free time

And here is the download.

webedit.zip
Filesize: 192kb
Download

Web Edit

Hello,

So today I got another C# app for you, this time I build a small PHP/HTML/CSS/JS Editor called Web Edit. I will release it soon with source and all.
Anyhow this first version, was build in under 24 hours. The things that aren’t working are the debugging, code folding, and code completion.

The code completion might be something that won’t be in for a while, the code folding I should be able to have up in an hour or so for all the languages.

The editor is built using the ICSharpCode.TextEditorControl from SharpDevelop. It is a great and quite easy control to use, I was originally making my own
editor control but that was just going to take forever so I stopped and used theres. Besides theres has more features then I could ever build into an edit control.

Just wanted to show so screenshots so far and will update soon with a link to a download.

View WebEdit Screenshot
(Click image for full view)

Small MVC Framework.

So I was sitting at home, kinda bored and was curious to find out how the MVC libraries worked. So I build my own, in just a few hours.
Any who I kinda like it, it is very lightweight but could use a lot of improvements, I will post a screenshot below of my Micro blog created with it
as well as the download to the source.

It has a pretty simple database class. Using it is as easy as one of the following.

$this->db->Where('id', '1')->Get('myTable');
$rows = $this->db->AllRows();

$this->db->WhereLike('title', '%searchstring')->Get('myTable');

while($row = $this->db->NextRow()) {
	echo "Found match at {$row['title']}<br />";
}

$this->db->OrderBy('id' 'DESC')->Limit('10')->Get('myTable');
$rows = $this->db->AllRows();

Those a just a couple examples.

This framework is a lot like CodeIgnighter, views are loaded by $this->views->Load(‘viewName’)
You can use it with or without a Model class the model correnspoding with your Controller is loaded into your
controller as $this->model.

Easy stuff, but I had fun making it, I’m going to try to build something with it, as well as write some documentation,
so hopefully someone else can find a use for this. Even though there are much better Frameworks.

Enjoy.

Small Blog written with my own Framework

MicroFramework.zip
Filesize: 14kb
Download MicroFrame

Update

Well, if you have been to the site before as you may have noticed I have overhauled the site once again. I think this
version looks oh so much better then the last two, and hopefully I can stick with this one.

I will be continuing the polishing of the site, adding, removing, changing things here and there, should be
good. I even update the search before it used the default search page.

That is all.

Free stuff for the .NET Framework.

Hello, I have something for anybody who feels like using them, I have a couple of Free Controls and a INI File reader / writer.

So firstly these controls maybe a little rough in some places, but I wrote the 2 controls, and the INI reader
and writer in the sum of about 3 - 4 hours, not bad I guess.

Anyhow the first control is one I made previously before it is the Notification Bar A simple to use control for
display infomation, small messages, search boxes, whaterever something to kinda replace the pop-up, it
is just like the ones seen on IE 6+, Firefox, and Google Chrome browsers.

The next control is the ExtendedListBox, it allows you to use an image, your standard text, and a small
description that gets placed below the text. Quite a simple control, very quick to build but worth
something nevertheless.

Now the next two are not controls but a utility of sorts, the INIReader and INIWriter that allow you to read
and write INI files similar to how you might read and write XML files with the XMLTextReader / Writer
classes. Quite simple you should be able to just drop them in and understand how they work.

Now to finish it off here is a couple of examples how to use the controls and utilities and a small screen
shot with the two controls in action,  and below that is a download to the controls (Full source included.)

Notification Bar

NotificationBar nb = new NotificationBar();
nb.Name = "notificationBar1";
nb.Text = "This is a quick test";
nb.ResizeToFixText = true;
nb.Show(true); //True will show / hide with sliding animation. False will just show and hide.

this.Controls.Add(nb);

Button button1 = new Button();
button1.Name = "button1";
button1.Text = "Test";
button1.Location = new Point(nb.Width - 105, 2);
button1.Show();
nb.Controls.Add(button1);

The ExtededListBox
The Extended list box can be used nearly exactly like a regular list box So I will skip that.

INIReader / INIWriter

INIWriter iniWriter = new INIWriter("test.ini");
iniWriter.AddNode("Group1"); //Add a group node.
iniWriter.AddNode("FirstName", "Group1", "Cory"); //Add a key/value node to Group1
iniWriter.AddNode("LastName", "Group1", "Borrow"); //Add a key/value node to Group1
iniWriter.Save();

INIReader iniReader = new INIReader("test.ini");
ININode node = null;

while((node = iniReader.Read()) != null) {
    if(node.NodeType == ININodeType.KeyValue) {
        MessageBox.Show(string.Format("Key: {0}, Value: {1}", node.Name, node.Value));
    }
}




CoryBorrow.Controls.zip
Filesize: 39kb
Download CoryBorrow.Controls.zip

SimpleAudio update.

Just a small post to let whomever know that I slightly updated the SimpleAudio app, added comments and the like, and have added the source code into the
download. So if you want you can download the update SimpleAudio player and the source. The source code is C# and requires the .NET Framework.

SimpleAudio.zip
Filesize: 599kb

Download SimpleAudio

Small bit of info.

So as you can see from my blog, I am still working on it.

But a couple of cool features I just wanted to point out to you.
For instance if you are using Chrome / Chromium or Safari you should
see a non web-safe font for the title of each post, as I am using the CSS3 web fonts.

The features in CSS3 are quite nice and it is good to see these features
being implemented into browsers already.

That’s all just wanted to say the site is under construction.

Hello and SimpleAudio

Hello welcome to my new blog. I have gone through a few blog engines in the past few months and
dropping all the old content along the way.

So here is the first post on me newest blog engine (Expression Engine) and hopefully the one to stay.
With all the out of the way I have a download for you as well, it is a simple audio player named
SimpleAudio. It supports the basics of playing audio play / pause, previous, next, and a volume control.

Well, it also displays a list of songs in your play list. But that is it simple and to the point.
It’s written in C# and uses the IrrKlang library to play audio.
It currently supports .mp3, .ogg, .mod, .xm, .wav, .it, .s3d.

    SimpleAudio Requirements
  • Windows 2000 or better (Possibly Linux too over Mono.)
  • .NET Framework 2.0+
  • 1 MB free hard drive space

Below you will find a screenshot and a download. Enjoy.

SimpleAudio Screenshot

SimpleAudio.zip
Created: Feb 24 2009
Filesize: 293kb
Download

Page 1 of 1 pages