Made BaseButton class
Buttons now children of BaseButton Buttons now render over and down modes Text buttons now exist Buttons render differently
This commit is contained in:
29
BaseButtonInstance.cpp
Normal file
29
BaseButtonInstance.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "BaseButtonInstance.h"
|
||||
|
||||
bool floatBottom;
|
||||
bool floatRight;
|
||||
bool floatCenter;
|
||||
|
||||
BaseButtonInstance::BaseButtonInstance(void)
|
||||
{
|
||||
}
|
||||
|
||||
BaseButtonInstance::~BaseButtonInstance(void)
|
||||
{
|
||||
}
|
||||
|
||||
void BaseButtonInstance::drawObj(RenderDevice* rd, Vector2 mousePos, bool mouseDown){}
|
||||
|
||||
bool BaseButtonInstance::mouseInArea(float point1x, float point1y, float point2x, float point2y, float mousex, float mousey)
|
||||
{
|
||||
|
||||
|
||||
if(mousex >= point1x && mousey >= point1y)
|
||||
{
|
||||
if(mousex < point2x && mousey < point2y)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
15
BaseButtonInstance.h
Normal file
15
BaseButtonInstance.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
#include "instance.h"
|
||||
|
||||
class BaseButtonInstance : public Instance
|
||||
{
|
||||
public:
|
||||
BaseButtonInstance(void);
|
||||
~BaseButtonInstance(void);
|
||||
virtual void drawObj(RenderDevice* rd, Vector2 mousePos, bool mouseDown);
|
||||
bool floatBottom;
|
||||
bool floatRight;
|
||||
bool floatCenter;
|
||||
protected:
|
||||
bool mouseInArea(float, float, float, float, float, float);
|
||||
};
|
||||
@@ -230,6 +230,10 @@
|
||||
Name="Source Files"
|
||||
Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\BaseButtonInstance.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\ButtonListener.cpp"
|
||||
>
|
||||
@@ -279,6 +283,10 @@
|
||||
Name="Header Files"
|
||||
Filter="h;hpp;hxx;hm;inl"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\BaseButtonInstance.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\ButtonListener.h"
|
||||
>
|
||||
|
||||
@@ -5,6 +5,8 @@ G3D::TextureRef image_ovr = NULL;
|
||||
int openGLID_ovr = 0;
|
||||
G3D::TextureRef image_dn = NULL;
|
||||
int openGLID_dn = 0;
|
||||
Vector2 size;
|
||||
Vector2 position;
|
||||
ImageButtonInstance::ImageButtonInstance(G3D::TextureRef newImage = NULL, G3D::TextureRef overImage = NULL, G3D::TextureRef downImage = NULL)
|
||||
{
|
||||
|
||||
@@ -14,8 +16,53 @@ ImageButtonInstance::ImageButtonInstance(G3D::TextureRef newImage = NULL, G3D::T
|
||||
openGLID_ovr = image_ovr->getOpenGLID();
|
||||
image_dn = downImage;
|
||||
openGLID_dn = image_dn->getOpenGLID();
|
||||
Vector2 size = Vector2(0,0);
|
||||
Vector2 position = Vector2(0,0);
|
||||
floatCenter = false;
|
||||
floatBottom = false;
|
||||
floatRight = false;
|
||||
|
||||
className = "ImageButton";
|
||||
}
|
||||
|
||||
ImageButtonInstance::~ImageButtonInstance(void)
|
||||
{
|
||||
}
|
||||
|
||||
void ImageButtonInstance::drawObj(RenderDevice* rd, Vector2 mousePos, bool mouseDown)
|
||||
{
|
||||
Vector2 positionRelative = position;
|
||||
int renderimage = openGLID;
|
||||
if(mouseInArea(position.x, position.y, position.x + size.x, position.y + size.y, mousePos.x, mousePos.y))
|
||||
{
|
||||
if(mouseDown && openGLID_dn != 0)
|
||||
{
|
||||
renderimage = openGLID_dn;
|
||||
}
|
||||
else if(openGLID_ovr != 0)
|
||||
{
|
||||
renderimage = openGLID_ovr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
rd->pushState();
|
||||
rd->beforePrimitive();
|
||||
glEnable( GL_TEXTURE_2D );
|
||||
glEnable(GL_BLEND);// you enable blending function
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glBindTexture( GL_TEXTURE_2D, renderimage);
|
||||
glBegin( GL_QUADS );
|
||||
glTexCoord2d(0.0,0.0);
|
||||
glVertex2f( positionRelative.x, positionRelative.y );
|
||||
glTexCoord2d( 1.0,0.0 );
|
||||
glVertex2f( positionRelative.x + size.x, positionRelative.y );
|
||||
glTexCoord2d( 1.0,1.0 );
|
||||
glVertex2f( positionRelative.x + size.x, positionRelative.y + size.y );
|
||||
glTexCoord2d( 0.0,1.0 );
|
||||
glVertex2f( positionRelative.x, positionRelative.y + size.y );
|
||||
glEnd();
|
||||
glDisable( GL_TEXTURE_2D );
|
||||
rd->afterPrimitive();
|
||||
rd->popState();
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
#pragma once
|
||||
#include "instance.h"
|
||||
#include "BaseButtonInstance.h"
|
||||
|
||||
class ImageButtonInstance :
|
||||
public Instance
|
||||
class ImageButtonInstance : public BaseButtonInstance
|
||||
{
|
||||
public:
|
||||
ImageButtonInstance(G3D::TextureRef,G3D::TextureRef,G3D::TextureRef);
|
||||
~ImageButtonInstance(void);
|
||||
void drawObj(RenderDevice*, Vector2, bool);
|
||||
Vector2 size;
|
||||
Vector2 position;
|
||||
};
|
||||
|
||||
@@ -18,9 +18,7 @@ bool centeredWithinBox;
|
||||
std::string title;
|
||||
G3D::GFontRef* font;
|
||||
int textSize;
|
||||
bool floatBottom;
|
||||
bool floatRight;
|
||||
bool floatCenter;
|
||||
|
||||
bool visible;
|
||||
ButtonListener* buttonListener;
|
||||
|
||||
@@ -35,6 +33,7 @@ TextButtonInstance::TextButtonInstance(void)
|
||||
textOutlineColor = Color4(0, 0, 0, 0);
|
||||
boxColor = Color4(0.6F,0.6F,0.6F,0.4F);
|
||||
boxOutlineColor = Color4(0, 0, 0, 0);
|
||||
setAllColorsSame();
|
||||
textSize = 12;
|
||||
floatBottom = false;
|
||||
floatRight = false;
|
||||
@@ -43,6 +42,18 @@ TextButtonInstance::TextButtonInstance(void)
|
||||
className = "TextButton";
|
||||
}
|
||||
|
||||
void TextButtonInstance::setAllColorsSame()
|
||||
{
|
||||
textColorOvr = textColor;
|
||||
textOutlineColorOvr = textOutlineColor;
|
||||
boxColorOvr = boxColor;
|
||||
boxOutlineColorOvr = boxOutlineColor;
|
||||
textColorDn = textColor;
|
||||
textOutlineColorDn = textOutlineColor;
|
||||
boxColorDn = boxColor;
|
||||
boxOutlineColorDn = boxOutlineColor;
|
||||
}
|
||||
|
||||
TextButtonInstance::~TextButtonInstance(void)
|
||||
{
|
||||
delete buttonListener;
|
||||
@@ -61,7 +72,7 @@ void TextButtonInstance::onClick()
|
||||
}
|
||||
}
|
||||
|
||||
void TextButtonInstance::drawObj(RenderDevice* rd)
|
||||
void TextButtonInstance::drawObj(RenderDevice* rd, Vector2 mousePos, bool mouseDown)
|
||||
{
|
||||
Vector3 point1;
|
||||
Vector3 point2;
|
||||
@@ -76,8 +87,24 @@ void TextButtonInstance::drawObj(RenderDevice* rd)
|
||||
point1 = Vector3(boxBegin.x, boxBegin.y,0);
|
||||
point2 = Vector3(boxEnd.x, boxEnd.y,0);
|
||||
}
|
||||
Draw::box(Box(point1, point2), rd, boxColor, boxOutlineColor);
|
||||
Vector2 RelativeTo = Vector2(point1.x + fontLocationRelativeTo.x, point1.y + fontLocationRelativeTo.y);
|
||||
font->draw2D(rd, title, RelativeTo, textSize, textColor, textOutlineColor);
|
||||
if(mouseInArea(point1.x, point1.y, point2.x, point2.y, mousePos.x, mousePos.y) && mouseDown)
|
||||
{
|
||||
Draw::box(Box(point1, point2), rd, boxColorDn, boxOutlineColorDn);
|
||||
font->draw2D(rd, title, RelativeTo, textSize, textColorDn, textOutlineColorDn);
|
||||
}
|
||||
else if(mouseInArea(point1.x, point1.y, point2.x, point2.y, mousePos.x, mousePos.y))
|
||||
{
|
||||
Draw::box(Box(point1, point2), rd, boxColorOvr, boxOutlineColorOvr);
|
||||
font->draw2D(rd, title, RelativeTo, textSize, textColorOvr, textOutlineColorOvr);
|
||||
}
|
||||
else
|
||||
{
|
||||
Draw::box(Box(point1, point2), rd, boxColor, boxOutlineColor);
|
||||
font->draw2D(rd, title, RelativeTo, textSize, textColor, textOutlineColor);
|
||||
}
|
||||
}
|
||||
|
||||
void doNullCheck()
|
||||
{
|
||||
}
|
||||
@@ -1,14 +1,14 @@
|
||||
#pragma once
|
||||
#include "instance.h"
|
||||
#include "BaseButtonInstance.h"
|
||||
#pragma once
|
||||
#include "ButtonListener.h"
|
||||
class ButtonListener;
|
||||
class TextButtonInstance :
|
||||
public Instance
|
||||
class TextButtonInstance : public BaseButtonInstance
|
||||
{
|
||||
public:
|
||||
TextButtonInstance(void);
|
||||
~TextButtonInstance(void);
|
||||
void setAllColorsSame(void);
|
||||
Vector2 boxBegin;
|
||||
Vector2 boxEnd;
|
||||
Vector2 fontLocationRelativeTo;
|
||||
@@ -27,11 +27,9 @@ public:
|
||||
bool centeredWithinBox;
|
||||
std::string title;
|
||||
G3D::GFontRef font;
|
||||
bool floatBottom;
|
||||
bool floatRight;
|
||||
bool visible;
|
||||
int textSize;
|
||||
void drawObj(G3D::RenderDevice*);
|
||||
void drawObj(RenderDevice*, Vector2, bool);
|
||||
void setButtonListener(ButtonListener*);
|
||||
void onClick();
|
||||
};
|
||||
68
main.cpp
68
main.cpp
@@ -15,6 +15,7 @@
|
||||
#include "resource.h"
|
||||
#include "PhysicalInstance.h"
|
||||
#include "TextButtonInstance.h"
|
||||
#include "ImageButtonInstance.h"
|
||||
|
||||
|
||||
#if G3D_VER < 61000
|
||||
@@ -139,6 +140,14 @@ TextButtonInstance* makeTextButton()
|
||||
return part;
|
||||
}
|
||||
|
||||
ImageButtonInstance* makeImageButton(G3D::TextureRef newImage = NULL, G3D::TextureRef overImage = NULL, G3D::TextureRef downImage = NULL)
|
||||
{
|
||||
ImageButtonInstance* part = new ImageButtonInstance(newImage,overImage, downImage);
|
||||
instances.push_back(part);
|
||||
instances_2D.push_back(part);
|
||||
return part;
|
||||
}
|
||||
|
||||
|
||||
void initGUI()
|
||||
{
|
||||
@@ -152,6 +161,7 @@ void initGUI()
|
||||
button->textOutlineColor = Color4::clear();
|
||||
button->title = "Hopper";
|
||||
button->fontLocationRelativeTo = Vector2(10, 3);
|
||||
button->setAllColorsSame();
|
||||
|
||||
button = makeTextButton();
|
||||
button->boxBegin = Vector2(0, -48);
|
||||
@@ -163,6 +173,7 @@ void initGUI()
|
||||
button->textOutlineColor = Color4::clear();
|
||||
button->title = "Controller";
|
||||
button->fontLocationRelativeTo = Vector2(10, 3);
|
||||
button->setAllColorsSame();
|
||||
|
||||
button = makeTextButton();
|
||||
button->boxBegin = Vector2(0, -72);
|
||||
@@ -174,6 +185,7 @@ void initGUI()
|
||||
button->textOutlineColor = Color4::clear();
|
||||
button->title = "Color";
|
||||
button->fontLocationRelativeTo = Vector2(10, 3);
|
||||
button->setAllColorsSame();
|
||||
|
||||
button = makeTextButton();
|
||||
button->boxBegin = Vector2(0, -96);
|
||||
@@ -185,6 +197,7 @@ void initGUI()
|
||||
button->textOutlineColor = Color4::clear();
|
||||
button->title = "Surface";
|
||||
button->fontLocationRelativeTo = Vector2(10, 3);
|
||||
button->setAllColorsSame();
|
||||
|
||||
button = makeTextButton();
|
||||
button->boxBegin = Vector2(0, -120);
|
||||
@@ -196,6 +209,7 @@ void initGUI()
|
||||
button->boxOutlineColor = Color3(0,255,255);
|
||||
button->title = "Model";
|
||||
button->fontLocationRelativeTo = Vector2(10, 3);
|
||||
button->setAllColorsSame();
|
||||
|
||||
button = makeTextButton();
|
||||
button->boxBegin = Vector2(0, 0);
|
||||
@@ -208,6 +222,7 @@ void initGUI()
|
||||
button->title = "File";
|
||||
button->textSize = 16;
|
||||
button->fontLocationRelativeTo = Vector2(10, 0);
|
||||
button->setAllColorsSame();
|
||||
|
||||
button = makeTextButton();
|
||||
button->boxBegin = Vector2(125, 0);
|
||||
@@ -220,6 +235,7 @@ void initGUI()
|
||||
button->title = "Edit";
|
||||
button->textSize = 16;
|
||||
button->fontLocationRelativeTo = Vector2(10, 0);
|
||||
button->setAllColorsSame();
|
||||
|
||||
button = makeTextButton();
|
||||
button->boxBegin = Vector2(250, 0);
|
||||
@@ -232,6 +248,7 @@ void initGUI()
|
||||
button->title = "View";
|
||||
button->textSize = 16;
|
||||
button->fontLocationRelativeTo = Vector2(10, 0);
|
||||
button->setAllColorsSame();
|
||||
|
||||
button = makeTextButton();
|
||||
button->boxBegin = Vector2(375, 0);
|
||||
@@ -244,6 +261,7 @@ void initGUI()
|
||||
button->title = "Insert";
|
||||
button->textSize = 16;
|
||||
button->fontLocationRelativeTo = Vector2(10, 0);
|
||||
button->setAllColorsSame();
|
||||
|
||||
button = makeTextButton();
|
||||
button->boxBegin = Vector2(500, 0);
|
||||
@@ -256,6 +274,12 @@ void initGUI()
|
||||
button->title = "Format";
|
||||
button->textSize = 16;
|
||||
button->fontLocationRelativeTo = Vector2(10, 0);
|
||||
button->setAllColorsSame();
|
||||
|
||||
ImageButtonInstance* instance = makeImageButton(go, go_ovr, go_dn);
|
||||
instance->size = Vector2(60,60);
|
||||
instance->position = Vector2(10, 25);
|
||||
instance->parent = dataModel;
|
||||
}
|
||||
|
||||
void Demo::onInit() {
|
||||
@@ -635,10 +659,10 @@ void drawButtons(RenderDevice* rd)
|
||||
for(size_t i = 0; i < instances_2D.size(); i++)
|
||||
{
|
||||
Instance* instance = instances_2D.at(i);
|
||||
if(instance->className == "TextButton" && instance->parent == dataModel)
|
||||
if((instance->className == "TextButton" || instance->className == "ImageButton") && instance->parent == dataModel)
|
||||
{
|
||||
TextButtonInstance* tbi = (TextButtonInstance*)instance;
|
||||
tbi->drawObj(rd);
|
||||
BaseButtonInstance* tbi = (BaseButtonInstance*)instance;
|
||||
tbi->drawObj(rd, Vector2(mousex, mousey), mouseButton1Down);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -717,7 +741,7 @@ void Demo::onGraphics(RenderDevice* rd) {
|
||||
rd->window()->getRelativeMouseState(mousepos, num);
|
||||
|
||||
bool mouseOnScreen = true;
|
||||
if(mousepos.x < 5 || mousepos.y < 5 || mousepos.x > rd->getViewport().width()-5 || mousepos.y > rd->getViewport().height()-5)
|
||||
if(mousepos.x < 1 || mousepos.y < 1 || mousepos.x >= rd->getViewport().width()-1 || mousepos.y >= rd->getViewport().height()-1)
|
||||
{
|
||||
mouseOnScreen = false;
|
||||
rd->window()->setInputCaptureCount(0);
|
||||
@@ -846,42 +870,10 @@ void Demo::onGraphics(RenderDevice* rd) {
|
||||
//app->debugFont->draw2D("Debug Mode Enabled", Vector2(0,30), 20, Color3::white(), Color3::black());
|
||||
|
||||
|
||||
|
||||
drawButtons(rd);
|
||||
rd->pushState();
|
||||
rd->beforePrimitive();
|
||||
|
||||
|
||||
|
||||
glEnable( GL_TEXTURE_2D );
|
||||
glEnable(GL_BLEND);// you enable blending function
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
if(mouseInArea(10,25,70,85))
|
||||
{
|
||||
if(mouseButton1Down)
|
||||
glBindTexture( GL_TEXTURE_2D, go_dn_id);
|
||||
else
|
||||
glBindTexture( GL_TEXTURE_2D, go_ovr_id);
|
||||
}
|
||||
else
|
||||
glBindTexture( GL_TEXTURE_2D, go_id);
|
||||
|
||||
glBegin( GL_QUADS );
|
||||
glTexCoord2d(0.0,0.0);
|
||||
glVertex2f( 10, 0+offset );
|
||||
glTexCoord2d( 1.0,0.0 );
|
||||
glVertex2f( 70, 0+offset );
|
||||
glTexCoord2d( 1.0,1.0 );
|
||||
glVertex2f( 70, 60+offset );
|
||||
glTexCoord2d( 0.0,1.0 );
|
||||
glVertex2f( 10, 60+offset );
|
||||
glEnd();
|
||||
|
||||
glDisable( GL_TEXTURE_2D );
|
||||
|
||||
|
||||
|
||||
|
||||
if(showMouse && mouseOnScreen)
|
||||
{
|
||||
glEnable( GL_TEXTURE_2D );
|
||||
@@ -915,7 +907,7 @@ void Demo::onGraphics(RenderDevice* rd) {
|
||||
|
||||
|
||||
|
||||
drawButtons(rd);
|
||||
|
||||
|
||||
app->renderDevice->pop2D();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user