Added mouse click functions to image button
Added destructor for images Deleting listener now on exit
This commit is contained in:
@@ -1,20 +1,34 @@
|
||||
#include "BaseButtonInstance.h"
|
||||
|
||||
bool floatBottom;
|
||||
bool floatRight;
|
||||
bool floatCenter;
|
||||
bool disabled;
|
||||
bool floatBottom = false;
|
||||
bool floatRight = false;
|
||||
bool floatCenter = false;
|
||||
bool disabled = false;
|
||||
ButtonListener* listener = NULL;
|
||||
|
||||
BaseButtonInstance::BaseButtonInstance(void)
|
||||
{
|
||||
listener = NULL;
|
||||
}
|
||||
|
||||
BaseButtonInstance::~BaseButtonInstance(void)
|
||||
{
|
||||
delete listener;
|
||||
}
|
||||
|
||||
void BaseButtonInstance::drawObj(RenderDevice* rd, Vector2 mousePos, bool mouseDown){}
|
||||
|
||||
bool BaseButtonInstance::mouseInButton(float mousex, float mousey, RenderDevice* rd){return false;}
|
||||
|
||||
void BaseButtonInstance::onMouseClick()
|
||||
{
|
||||
if(listener != NULL)
|
||||
{
|
||||
listener->onButton1MouseClick(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool BaseButtonInstance::mouseInArea(float point1x, float point1y, float point2x, float point2y, float mousex, float mousey)
|
||||
{
|
||||
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
#pragma once
|
||||
#include "instance.h"
|
||||
|
||||
#pragma once
|
||||
#include "ButtonListener.h"
|
||||
class BaseButtonInstance : public Instance
|
||||
{
|
||||
public:
|
||||
BaseButtonInstance(void);
|
||||
~BaseButtonInstance(void);
|
||||
virtual ~BaseButtonInstance(void);
|
||||
virtual void drawObj(RenderDevice* rd, Vector2 mousePos, bool mouseDown);
|
||||
virtual bool mouseInButton(float, float, RenderDevice* rd);
|
||||
virtual void onMouseClick();
|
||||
bool floatBottom;
|
||||
bool floatRight;
|
||||
bool floatCenter;
|
||||
bool disabled;
|
||||
protected:
|
||||
bool mouseInArea(float, float, float, float, float, float);
|
||||
class ButtonListener* listener;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "ButtonListener.h"
|
||||
|
||||
|
||||
ButtonListener::ButtonListener(void)
|
||||
{
|
||||
}
|
||||
@@ -8,7 +9,7 @@ ButtonListener::~ButtonListener(void)
|
||||
{
|
||||
}
|
||||
|
||||
void ButtonListener::onButton1MouseClick(TextButtonInstance* button)
|
||||
void ButtonListener::onButton1MouseClick(BaseButtonInstance* button)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
#pragma once
|
||||
#include "TextButtonInstance.h"
|
||||
class TextButtonInstance;
|
||||
#include "BaseButtonInstance.h"
|
||||
class BaseButtonInstance;
|
||||
class ButtonListener
|
||||
{
|
||||
public:
|
||||
ButtonListener(void);
|
||||
~ButtonListener(void);
|
||||
virtual void onButton1MouseClick(TextButtonInstance*);
|
||||
virtual void onButton1MouseClick(BaseButtonInstance*);
|
||||
//virtual void onMouseOver(); //TODO
|
||||
//virtual void onMouseOut(); //TODO
|
||||
//virtual void onButton1MouseDown(); //TODO
|
||||
|
||||
@@ -34,10 +34,51 @@ ImageButtonInstance::ImageButtonInstance(G3D::TextureRef newImage, G3D::TextureR
|
||||
|
||||
ImageButtonInstance::~ImageButtonInstance(void)
|
||||
{
|
||||
//Delete everything on destruction
|
||||
image.~ReferenceCountedPointer();
|
||||
delete image.getPointer();
|
||||
image_ovr.~ReferenceCountedPointer();
|
||||
delete image_ovr.getPointer();
|
||||
image_ds.~ReferenceCountedPointer();
|
||||
delete image_ds.getPointer();
|
||||
image_dn.~ReferenceCountedPointer();
|
||||
delete image_dn.getPointer();
|
||||
image = NULL;
|
||||
image_ovr = NULL;
|
||||
image_ds = NULL;
|
||||
image_dn = NULL;
|
||||
delete listener;
|
||||
listener = NULL;
|
||||
}
|
||||
|
||||
bool ImageButtonInstance::mouseInButton(float mousex, float mousey, RenderDevice* rd)
|
||||
{
|
||||
Vector2 positionRelative = position;
|
||||
if(floatRight && floatBottom)
|
||||
{
|
||||
positionRelative = Vector2(rd->getWidth() + position.x, rd->getHeight() + position.y);
|
||||
}
|
||||
else if(floatBottom)
|
||||
{
|
||||
positionRelative = Vector2(position.x, rd->getHeight() + position.y);
|
||||
}
|
||||
else if(floatRight)
|
||||
{
|
||||
positionRelative = Vector2(rd->getWidth() + position.x, position.y);
|
||||
}
|
||||
if(mousex >= positionRelative.x && mousey >= positionRelative.y)
|
||||
{
|
||||
if(mousex < positionRelative.x + size.x && mousey < positionRelative.y + size.y)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ImageButtonInstance::drawObj(RenderDevice* rd, Vector2 mousePos, bool mouseDown)
|
||||
{
|
||||
bool drawDisabledBox = false;
|
||||
Vector2 positionRelative = position;
|
||||
if(floatRight && floatBottom)
|
||||
{
|
||||
@@ -56,6 +97,8 @@ void ImageButtonInstance::drawObj(RenderDevice* rd, Vector2 mousePos, bool mouse
|
||||
{
|
||||
if(!image_ds.isNull())
|
||||
renderimage = openGLID_ds;
|
||||
else
|
||||
drawDisabledBox = true;
|
||||
}
|
||||
else if(mouseInArea(positionRelative.x, positionRelative.y, positionRelative.x + size.x, positionRelative.y + size.y, mousePos.x, mousePos.y))
|
||||
{
|
||||
@@ -89,4 +132,8 @@ void ImageButtonInstance::drawObj(RenderDevice* rd, Vector2 mousePos, bool mouse
|
||||
glDisable( GL_TEXTURE_2D );
|
||||
rd->afterPrimitive();
|
||||
rd->popState();
|
||||
if(drawDisabledBox)
|
||||
{
|
||||
Draw::box(Box(Vector3(positionRelative.x, positionRelative.y, 0), Vector3(positionRelative.x+size.x, positionRelative.y+size.y, 0)), rd, Color4(0.7,0.7,0.7,0.3), Color4::clear());
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
#include "BaseButtonInstance.h"
|
||||
|
||||
class ImageButtonInstance : public BaseButtonInstance
|
||||
{
|
||||
public:
|
||||
@@ -20,4 +19,5 @@ public:
|
||||
int openGLID_dn;
|
||||
G3D::TextureRef image_ds;
|
||||
int openGLID_ds;
|
||||
bool mouseInButton(float, float, RenderDevice*);
|
||||
};
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
|
||||
std::string name;
|
||||
Instance* parent;
|
||||
static std::string className = "Instance";
|
||||
static std::string className = "DataModel";
|
||||
|
||||
Instance::Instance(void)
|
||||
{
|
||||
name = "Default Game Instance";
|
||||
className = "Part";
|
||||
className = "DataModel";
|
||||
}
|
||||
|
||||
Instance::~Instance(void)
|
||||
|
||||
@@ -5,7 +5,7 @@ class Instance
|
||||
{
|
||||
public:
|
||||
Instance(void);
|
||||
~Instance(void);
|
||||
virtual ~Instance(void);
|
||||
std::string name;
|
||||
Instance* parent; // Another pointer.
|
||||
std::string className;
|
||||
|
||||
@@ -20,7 +20,6 @@ G3D::GFontRef* font;
|
||||
int textSize;
|
||||
|
||||
bool visible;
|
||||
ButtonListener* buttonListener;
|
||||
|
||||
TextButtonInstance::TextButtonInstance(void)
|
||||
{
|
||||
@@ -57,20 +56,6 @@ void TextButtonInstance::setAllColorsSame()
|
||||
|
||||
TextButtonInstance::~TextButtonInstance(void)
|
||||
{
|
||||
delete buttonListener;
|
||||
}
|
||||
|
||||
void TextButtonInstance::setButtonListener(ButtonListener* listener)
|
||||
{
|
||||
buttonListener = listener;
|
||||
}
|
||||
|
||||
void TextButtonInstance::onClick()
|
||||
{
|
||||
if(buttonListener != NULL)
|
||||
{
|
||||
buttonListener->onButton1MouseClick(this);
|
||||
}
|
||||
}
|
||||
|
||||
void TextButtonInstance::drawObj(RenderDevice* rd, Vector2 mousePos, bool mouseDown)
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
#pragma once
|
||||
#include "BaseButtonInstance.h"
|
||||
#pragma once
|
||||
#include "ButtonListener.h"
|
||||
class ButtonListener;
|
||||
class TextButtonInstance : public BaseButtonInstance
|
||||
{
|
||||
public:
|
||||
TextButtonInstance(void);
|
||||
~TextButtonInstance(void);
|
||||
void setAllColorsSame(void);
|
||||
void setAllColorsSame();
|
||||
Vector2 boxBegin;
|
||||
Vector2 boxEnd;
|
||||
Vector2 fontLocationRelativeTo;
|
||||
@@ -30,6 +27,4 @@ public:
|
||||
bool visible;
|
||||
int textSize;
|
||||
void drawObj(RenderDevice*, Vector2, bool);
|
||||
void setButtonListener(ButtonListener*);
|
||||
void onClick();
|
||||
};
|
||||
35
main.cpp
35
main.cpp
@@ -391,6 +391,7 @@ void initGUI()
|
||||
instance->floatRight = true;
|
||||
instance->position = Vector2(-77, -90);
|
||||
instance->parent = dataModel;
|
||||
instance->disabled = true;
|
||||
|
||||
instance = makeImageButton(
|
||||
Texture::fromFile(GetFileInPath("/content/images/CameraZoomOut.png")),
|
||||
@@ -737,6 +738,20 @@ void Demo::onUserInput(UserInput* ui) {
|
||||
right = true;
|
||||
}
|
||||
|
||||
if(ui->keyReleased(SDL_LEFT_MOUSE_KEY))
|
||||
{
|
||||
for(size_t i = 0; i < instances_2D.size(); i++)
|
||||
{
|
||||
if(instances_2D.at(i)->className == "TextButton" || instances_2D.at(i)->className == "ImageButton")
|
||||
{
|
||||
BaseButtonInstance* button = (BaseButtonInstance*)instances_2D.at(i);
|
||||
if(button->mouseInButton(ui->mouseXY().x, ui->mouseXY().y, app->renderDevice))
|
||||
{
|
||||
button->onMouseClick();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//readMouseGUIInput();
|
||||
@@ -806,9 +821,9 @@ void drawButtons(RenderDevice* rd)
|
||||
|
||||
void drawOutline(Vector3 from, Vector3 to, RenderDevice* rd, LightingParameters lighting, Vector3 size, Vector3 pos)
|
||||
{
|
||||
rd->setLight(0, NULL);
|
||||
rd->setAmbientLightColor(Color3(1,1,1));
|
||||
Color3 outline = Color3(0.098F,0.6F,1.0F);
|
||||
//rd->setLight(0, NULL);
|
||||
//rd->setAmbientLightColor(Color3(1,1,1));
|
||||
Color3 outline = Color3::cyan();//Color3(0.098F,0.6F,1.0F);
|
||||
float offsetSize = 0.05F;
|
||||
//X
|
||||
Draw::box(Box(Vector3(from.x - offsetSize, from.y + offsetSize, from.z + offsetSize), Vector3(to.x + offsetSize, from.y - offsetSize, from.z - offsetSize)), rd, outline, Color4::clear());
|
||||
@@ -831,6 +846,8 @@ void drawOutline(Vector3 from, Vector3 to, RenderDevice* rd, LightingParameters
|
||||
|
||||
if(mode == ARROWS)
|
||||
{
|
||||
rd->setLight(0, NULL);
|
||||
rd->setAmbientLightColor(Color3(1,1,1));
|
||||
float max = size.x;
|
||||
if(abs(size.y) > max)
|
||||
max = size.y;
|
||||
@@ -843,9 +860,14 @@ void drawOutline(Vector3 from, Vector3 to, RenderDevice* rd, LightingParameters
|
||||
Draw::arrow(pos, Vector3(0, (-1)-max, 0), rd);
|
||||
Draw::arrow(pos, Vector3((-1)-max, 0, 0), rd);
|
||||
Draw::arrow(pos, Vector3(0, 0, (-1)-max), rd);
|
||||
rd->setAmbientLightColor(lighting.ambient);
|
||||
rd->setLight(0, GLight::directional(lighting.lightDirection, lighting.lightColor));
|
||||
}
|
||||
else if(mode == RESIZE)
|
||||
{
|
||||
|
||||
rd->setLight(0, NULL);
|
||||
rd->setAmbientLightColor(Color3(1,1,1));
|
||||
Vector3 gamepoint = pos;
|
||||
Vector3 camerapoint = rd->getCameraToWorldMatrix().translation;
|
||||
float distance = pow(pow((double)gamepoint.x - (double)camerapoint.x, 2) + pow((double)gamepoint.y - (double)camerapoint.y, 2) + pow((double)gamepoint.z - (double)camerapoint.z, 2), 0.5);
|
||||
@@ -863,10 +885,10 @@ void drawOutline(Vector3 from, Vector3 to, RenderDevice* rd, LightingParameters
|
||||
Draw::sphere(Sphere(Vector3(pos.x, pos.y, pos.z + (size.z/2 + 1)), multiplier), rd, sphereColor, Color4::clear());
|
||||
Draw::sphere(Sphere(Vector3(pos.x, pos.y, pos.z - (size.z/2 + 1)), multiplier), rd, sphereColor, Color4::clear());
|
||||
}
|
||||
}
|
||||
|
||||
rd->setAmbientLightColor(lighting.ambient);
|
||||
rd->setLight(0, GLight::directional(lighting.lightDirection, lighting.lightColor));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Demo::onGraphics(RenderDevice* rd) {
|
||||
@@ -1108,7 +1130,8 @@ int main(int argc, char** argv) {
|
||||
settings.window.defaultIconFilename = GetFileInPath("/content/images/rico256c.png");
|
||||
settings.window.resizable = true;
|
||||
settings.writeLicenseFile = false;
|
||||
|
||||
settings.window.width = 841;
|
||||
settings.window.height = 639;
|
||||
//Using the damned SDL window now
|
||||
SDLWindow* wnd = new SDLWindow(settings.window);
|
||||
//wnd->setInputCaptureCount(200);
|
||||
|
||||
Reference in New Issue
Block a user