UI

Published September 16, 2007
Advertisement
Added a simple button class. Seems to be working fine, but it involved two things that I'm not happy about:
-Delegates are now in the form:
void Delegate(float, float)
This is so the button even handlers can take in mouse coords to do boundary checks. Works fine for buttons, but now everything else that handled events has handlers with two unused parameters.
-The renderer class now has to know about buttons. At first I wanted this all to be abstracted away, so that the renderer would only need to store a geometry buffer for each button, but that didn't play nicely with texturing. Buttons have three textures (normal, mouse over, and clicked). Now the renderer needs to store a pointer to each button, so it can get the appropriate texture when it renders.

Pics (these three show a button in its normal state, with the mouse over it, and clicked, in that order):




Code:
Button.h
#ifndef BUTTON_H#define BUTTON_H#include #include #include #include "UI.h"#include "../../Graphics/Header Files/GeometryBuffer.h"namespace ISO{	namespace Graphics	{		class Renderer;	}	namespace UI	{		class Button		{		public:			Button(Graphics::Renderer* renderer, LPDIRECT3DDEVICE9 device, std::string texName, std::string texOverName, std::string texClickedName, float x, float y, float w, float h);			~Button();			int GetTextureIndex() const;			void OnClick(float mouseX, float mouseY);			void OnUnClick(float mouseX, float mouseY);			void OnMouseOver(float mouseX, float mouseY);		private:			int m_ActiveIndex, m_TextureIndex, m_TexOverIndex, m_TexClickedIndex;			AABB m_Bounds;			int m_Position;		};	}}#endif


Button.cpp
#include "../Header Files/Button.h"#include "../../Graphics/Header Files/Renderer.h"#include "../../Graphics/Header Files/Texture.h"#include "../../Main/Header Files/Event.h"extern ISO::Base::EventManager *g_Events;ISO::UI::Button::Button(Graphics::Renderer* renderer, LPDIRECT3DDEVICE9 device, std::string texName, std::string texOverName, std::string texClickedName, float x, float y, float w, float h): m_Bounds(x, y, x+w, y+h){	m_ActiveIndex = m_TextureIndex = renderer->AddTexture(new ISO::Graphics::Texture(device, texName));	m_TexOverIndex = renderer->AddTexture(new ISO::Graphics::Texture(device, texOverName));	m_TexClickedIndex = renderer->AddTexture(new ISO::Graphics::Texture(device, texClickedName));	m_Position = renderer->AddButton(this, x, y, x+w, y+h);	g_Events->RegisterEventHandler("mouseclick", this, &Button::OnClick);	g_Events->RegisterEventHandler("mouseup", this, &Button::OnUnClick);	g_Events->RegisterEventHandler("mouseover", this, &Button::OnMouseOver);}ISO::UI::Button::~Button(){}int ISO::UI::Button::GetTextureIndex() const{	return m_ActiveIndex;}void ISO::UI::Button::OnClick(float mouseX, float mouseY){	if (m_Bounds.IsIn(mouseX, mouseY))	{		m_ActiveIndex = m_TexClickedIndex;	}}void ISO::UI::Button::OnUnClick(float mouseX, float mouseY){	if (m_ActiveIndex == m_TexClickedIndex)	{		m_ActiveIndex = m_Bounds.IsIn(mouseX, mouseY) ? m_TexOverIndex : m_TextureIndex;	}}void ISO::UI::Button::OnMouseOver(float mouseX, float mouseY){	if (m_Bounds.IsIn(mouseX, mouseY) && m_ActiveIndex != m_TexClickedIndex)	{		m_ActiveIndex = m_TexOverIndex;	}	else if (m_ActiveIndex == m_TexOverIndex)	{		m_ActiveIndex = m_TextureIndex;	}}


Still to come:
-Editable geometry buffers, so I can edit tiles after creation.
-Font rendering
-Mouse handling to place tiles
Previous Entry Bad Behavior
0 likes 1 comments

Comments

Metorical
Not familiar with you design but it sounds like you want to encapsulate all your data in an event object. Then when you need to pass new data you can extend the event object and all your objects will then have access to the new data.
September 16, 2007 02:25 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement

Latest Entries

/facepalm

1706 views

Baby Steps

1269 views

...

720 views

Stuff

1306 views

Productivity++

1197 views

Rock Band FTW

1228 views

Seattle Opera

1273 views

Christ

1198 views

I maek gaem!

1173 views
Advertisement