CircleScrollBar for Irr

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
dalerank2
Posts: 121
Joined: Thu Dec 03, 2009 7:41 am

CircleScrollBar for Irr

Post by dalerank2 »

Yet one gui element for Irr

return event EGET_SCROLL_BAR_CHANGED

Image
Image
Image

Need for compile class CNrpScrollBar and CNrpRotatableImage


Header

Code: Select all

/**********************************************
   Copyright (C) 2009-2010 Dalerank and Gilly Oster
 
   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
   arising from the use of this software.
 
   Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be  appreciated but is not required.
   2. Altered source versions must be plainly marked as such, and must not be  misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.
*******************************************************/

#include "NrpScrollBar.h"
namespace irr
{

namespace gui
{

class CNrpRotatableImage;

class CNrpCircleScrollBar : public CNrpScrollBar
{
public:

	//! constructor
	CNrpCircleScrollBar( IGUIEnvironment* environment,
						 IGUIElement* parent, s32 id, 
						 core::rect<s32> rectangle,
						 bool noclip=false);

	//! destructor
	virtual ~CNrpCircleScrollBar();

	//! called if an event happened.
	virtual bool OnEvent(const SEvent& event);

	//! draws the element and its children
	virtual void draw();

	//! sets the position of the scrollbar
	virtual void setPos(s32 pos);

	//! gets the current position of the scrollbar
	virtual s32 getPos() const;

	//! sets the texture which draw
	virtual void setTexture( video::ITexture* texture );

	virtual void setSliderTexture( video::ITexture* texture );

	void updateAbsolutePosition();

private:
	f32 GetCurrentAngle_( const core::position2di& mPos );
	bool mouse2rotate_;
	f32 startAngle_, aPos;
	int circleCounter_;

	gui::CNrpRotatableImage* rImage_;
};


}//namespace gui

}//namespace irr
Source

Code: Select all

#include "NrpCircleScrollBar.h"
#include "NrpRotatableImage.h"

#include <irrlicht.h>

namespace irr
{

namespace gui
{

CNrpCircleScrollBar::CNrpCircleScrollBar(IGUIEnvironment* environment,
								 		 IGUIElement* parent, s32 id, 
										 core::rect<s32> rectangle,
										 bool noclip) : CNrpScrollBar( false, environment, parent, id, rectangle )
{
#ifdef _DEBUG
	setDebugName("CNrpCircleScrollBar");
#endif

	while( Children.getSize() )
		   (*Children.begin())->remove();

	UpButton = NULL;
	DownButton = NULL;

	mouse2rotate_ = false;
	Min = 0;
	Max = 100;
	startAngle_ = 0;
	circleCounter_ = 0;
	aPos = 0;

	rImage_ = new CNrpRotatableImage( core::recti( 0, 0, AbsoluteRect.getWidth(), AbsoluteRect.getHeight() ), environment, -1, this );		
}

CNrpCircleScrollBar::~CNrpCircleScrollBar()
{
}

void CNrpCircleScrollBar::setPos( s32 pos )
{
	aPos = (f32)core::s32_clamp ( pos, Min, Max );

	startAngle_ = pos / 360.f;

	rImage_->SetRotate( startAngle_ );
}

void CNrpCircleScrollBar::updateAbsolutePosition()
{
	IGUIElement::updateAbsolutePosition();

	setPos ( Pos );
}

bool CNrpCircleScrollBar::OnEvent( const SEvent& event )
{
	switch( event.EventType )
	{
	case EET_GUI_EVENT:
		if(  event.GUIEvent.EventType == EGET_ELEMENT_LEFT )
		{
			mouse2rotate_ = false;
			return true;
		}
	break;

	case EET_MOUSE_INPUT_EVENT:
		if( event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN )
		{
			mouse2rotate_ = true;	
			startAngle_ = GetCurrentAngle_( core::position2di( event.MouseInput.X, event.MouseInput.Y) );
			return true;
		}

		if( event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP )
		{
			mouse2rotate_ = false;
			return true;
		}

		if( event.MouseInput.Event == EMIE_MOUSE_MOVED && mouse2rotate_ )
		{
			f32 offset = GetCurrentAngle_( core::position2di( event.MouseInput.X, event.MouseInput.Y) ) - startAngle_;
									
			if( aPos + offset >= Min && aPos + offset < Max )
			{
				startAngle_ += offset;
				aPos += offset;
				rImage_->SetRotate( rImage_->getRotate() - offset );

				SEvent newEvent;
				newEvent.EventType = EET_GUI_EVENT;
				newEvent.GUIEvent.Caller = this;
				newEvent.GUIEvent.Element = 0;
				newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
				Parent->OnEvent(newEvent);
				return true;
			}
			else
			{
#ifdef _DEBUG
				OutputDebugString( IntToStr( (int)(offset + startAngle_) ).c_str() );
				OutputDebugString( "\n" );
#endif				
				mouse2rotate_ = false;
			}
		}
		break;

	}

	return false;
}

f32 CNrpCircleScrollBar::GetCurrentAngle_( const core::position2di& mPos )
{
	f32 angle=startAngle_;
	f32 dst = (f32)mPos.getDistanceFrom(  AbsoluteRect.getCenter() );
	f32 rad = AbsoluteRect.getWidth() / 2.f;

	if( dst > 0.2 * rad && dst < rad )
	{
		angle = atan2f( (f32)(mPos.Y - AbsoluteRect.getCenter().Y),
				        (f32)(mPos.X - AbsoluteRect.getCenter().X) ) * 180 / core::PI + 90;
	}
	
	if( angle < 0 )
		angle = 360 + angle;


	if( angle + circleCounter_ * 360 - startAngle_ < -100 )
		circleCounter_++;
	else if( angle + circleCounter_ * 360 - startAngle_ > 100 )
		circleCounter_--;

	return angle + circleCounter_ * 360;
}

void CNrpCircleScrollBar::setTexture( video::ITexture* texture )
{
	CNrpScrollBar::setTexture( texture );

}

irr::s32 CNrpCircleScrollBar::getPos() const
{
	return (s32)aPos;
}

void CNrpCircleScrollBar::setSliderTexture( video::ITexture* texture )
{
	CNrpScrollBar::setSliderTexture( texture );
	rImage_->SetTexture( texture );
}

void CNrpCircleScrollBar::draw()
{
	if( IsVisible )
	{
		if( texture_ )
		{
			core::recti srcRect = core::recti( core::position2di( 0, 0 ), texture_->getSize() );
			Environment->getVideoDriver()->draw2DImage( texture_, AbsoluteRect, srcRect, 0, 0, true );
		}

		rImage_->draw();

		IGUIElement::draw();
	}
}

}//namesapce gui

}//namesapce irr
All source with parent may download from depositfile.com (600k)
http://depositfiles.com/files/503k5efb5
Last edited by dalerank2 on Fri May 14, 2010 5:23 am, edited 2 times in total.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

interesting, just for shits and giggles what's the license you're slapping on this beauty?
dalerank2
Posts: 121
Joined: Thu Dec 03, 2009 7:41 am

Post by dalerank2 »

not any license - open source only
dalerank2
Posts: 121
Joined: Thu Dec 03, 2009 7:41 am

Post by dalerank2 »

I use it for my project, have decided to share code
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Better make it a proper license, e.g. zlib. That avoids situatiosn where suddenly someone else claims to have written this code.
kjkrum
Posts: 26
Joined: Wed Oct 29, 2003 5:34 pm

Post by kjkrum »

"Open source" means nothing without a license. You need to specify how people are allowed to use your source. Like, can I put this in a commercial project? Do I need to share the source of my project if I use this? Legally, nobody can do anything with your code unless you license it.

These might be good places to start:

http://en.wikipedia.org/wiki/Comparison ... e_licenses
http://developer.kde.org/documentation/ ... mmary.html
dalerank2
Posts: 121
Joined: Thu Dec 03, 2009 7:41 am

Post by dalerank2 »

Added zlib like license
dalerank2
Posts: 121
Joined: Thu Dec 03, 2009 7:41 am

Post by dalerank2 »

replace source code with license in archive
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

kjkrum wrote:"Open source" means nothing without a license. You need to specify how people are allowed to use your source. Like, can I put this in a commercial project? Do I need to share the source of my project if I use this? Legally, nobody can do anything with your code unless you license it.

These might be good places to start:

http://en.wikipedia.org/wiki/Comparison ... e_licenses
http://developer.kde.org/documentation/ ... mmary.html
Well, I'd have interpreted this as being in the Public Domain. This is also a clearly defined term, but contains some unpleasant things for the original author. So it's generally better to use a zlib/MIT license instead, which prevents code stealing.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

I don't know why but I love the MIT license, just the name and origin of it alone sounds good to me. It's a shame that many code snippets aren't properly licensed, like hybrid I would assume public domain or even claim it as my own in defense had no one else made proper claim.

It's the nessesary evil of copyrights I suppose, they should just make a law that requires a zlib/MIT license for unlicensed old code IMHO. Or public domain I suppose if nobody defines it. At any rate legally I imagine until the moment you claim a license you could get away with using it up until that point without breaking the law. At least in this country.

I dislike many open sourced licenses for a similar reason, like public domain they actually strip the creator of some specific rights in certain cases. Not to mention for any of us foolish enough to attempt novice coding as a main source of income unable to use the majority of useful code which is vital to lone programming.

I try not to get attached to my code unless it's something worth money and private, there is no reason we should all have to write our own engines for everything and that kind of unprogressive bullshit.
Mani2010
Posts: 107
Joined: Sat Jan 16, 2010 4:35 pm
Location: London,UK
Contact:

Post by Mani2010 »

can't get this to work properly...

Added it to example 1 in the framework. The slider image only appears when the angle is divisible by 90 degrees.

so i can see the image at 90deg,180deg,270deg,360deg and so on.

also say the circle scrollbar is a clock face, the slider image (when it is visible) appears only at 6 o'clock and 3 o'clock. Alternating between these two positions.

can someone post a driver file, or just the lines where they created and setup the circle scroll bar?
dalerank2
Posts: 121
Joined: Thu Dec 03, 2009 7:41 am

Post by dalerank2 »

ok... Later I will place here full example with bin and source
Mani2010
Posts: 107
Joined: Sat Jan 16, 2010 4:35 pm
Location: London,UK
Contact:

Post by Mani2010 »

hey thats great, thanks
Mani2010
Posts: 107
Joined: Sat Jan 16, 2010 4:35 pm
Location: London,UK
Contact:

Post by Mani2010 »

Appreciate that driver file/example when you can :),
am trying to use this to rotate the character in my game
Post Reply