#include "wx/wx.h"
class MyFrame : public wxFrame{
public:
MyFrame();
~MyFrame();
private:
//DECLARE_EVENT_TABLE()
};
class MyWindow : public wxWindow{
public:
void OnPaint(wxPaintEvent& event);
private:
DECLARE_EVENT_TABLE()
};
class MyApp : public wxApp
{
public:
MyApp();
~MyApp();
virtual bool OnInit();
void DrawSimpleShapes(wxDC& dc);
private:
MyFrame* m_frame = NULL;
//MyWindow* w = NULL;
};
MyFrame::MyFrame() : wxFrame(nullptr,wxID_ANY,"Rectangle",wxPoint(30,30),wxSize(800,600))
{
}
bool MyApp :: OnInit()
{
m_frame = new MyFrame();
m_frame->Show();
//w = new MyWindow();
//w->Show();
return true;
}
wxIMPLEMENT_APP(MyApp);
wxBEGIN_EVENT_TABLE(MyWindow,wxWindow)
EVT_PAINT(MyWindow::OnPaint)
wxEND_EVENT_TABLE()
MyFrame::~MyFrame()
{
}
MyApp::MyApp()
{
}
MyApp::~MyApp()
{
}
void MyWindow :: OnPaint(wxPaintEvent& event)
{
wxPaintDC dc(this);
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxRED_BRUSH);
wxSize sz = GetClientSize();
wxCoord w = 100, h = 50;
int x = wxMax(0,(sz.x-w)/2);
int y = wxMax(0,(sz.y - h)/2);
wxRect recToDraw(x,y,w,h);
dc.DrawRectangle(recToDraw);
}
I need some guidance learning wxWidgets. What's the problem with my code? When I run this code it does not print any rectangle. Instead it just print window only. I am new to wxWidgets library so it is difficult for me to find any errors. I cannot do any error handling in the wxWidgets.
There are multiple issues with the code posted, but I'll restrict this answer to the question that was asked. If you want to draw a rectangle on the applications frame, you need to
declare the OnPaint method in the frame class, and
alter the event table macro to set the OnPaint method to handle the paint event.
Here is a corrected example with these 2 changes:
#include "wx/wx.h"
class MyFrame : public wxFrame{
public:
MyFrame();
~MyFrame();
private:
void OnPaint(wxPaintEvent& event);
DECLARE_EVENT_TABLE()
};
class MyApp : public wxApp
{
public:
MyApp();
~MyApp();
virtual bool OnInit();
private:
MyFrame* m_frame = NULL;
};
MyFrame::MyFrame() : wxFrame(nullptr,wxID_ANY,"Rectangle",wxPoint(30,30),wxSize(800,600))
{
}
bool MyApp :: OnInit()
{
m_frame = new MyFrame();
m_frame->Show();
return true;
}
wxIMPLEMENT_APP(MyApp);
wxBEGIN_EVENT_TABLE(MyFrame,wxFrame)
EVT_PAINT(MyFrame::OnPaint)
wxEND_EVENT_TABLE()
MyFrame::~MyFrame()
{
}
MyApp::MyApp()
{
}
MyApp::~MyApp()
{
}
void MyFrame :: OnPaint(wxPaintEvent& event)
{
wxPaintDC dc(this);
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxRED_BRUSH);
wxSize sz = GetClientSize();
wxCoord w = 100, h = 50;
int x = wxMax(0,(sz.x-w)/2);
int y = wxMax(0,(sz.y - h)/2);
wxRect recToDraw(x,y,w,h);
dc.DrawRectangle(recToDraw);
}
In the code you posted, you had an extra MyWindow class, but that class was never used anywhere.
Related
I'm kinda new to C++ however, this was a code that was provided to me and I was told to do some edits but very few. I Keep getting the error for two lines of code:
MyFrame() : wxFrame((wxFrame *)NULL, -1, wxT("wxTimerDemo " + wxDataTime::Now().Format("%c"))
, wxPoint(50,50), wxSize(500,300))
and
dc.DrawText(wxT("Testing"), 40, y);
The top one is the only code edited which was followed exactly as it should've.
Full code:
// Render timer - use of a timer
// http://wiki.wxwidgets.org/Making_a_render_loop
#include <wx/sizer.h>
#include <wx/wx.h>
#include <wx/timer.h>
// prototypes
class BasicDrawPane;
class MyFrame;
// class definitions
class RenderTimer : public wxTimer
{
BasicDrawPane* pane;
public:
RenderTimer(BasicDrawPane* pane);
void Notify();
void start();
};
#define wxT(x)
class BasicDrawPane : public wxPanel
{
public:
BasicDrawPane(wxFrame* parent);
void paintEvent(wxPaintEvent& evt);
void render( wxDC& dc );
DECLARE_EVENT_TABLE()
};
class MyApp: public wxApp
{
bool OnInit();
MyFrame* frame;
public:
};
RenderTimer::RenderTimer(BasicDrawPane* pane) : wxTimer()
{
RenderTimer::pane = pane;
}
void RenderTimer::Notify()
{
pane->Refresh();
}
void RenderTimer::start()
{
wxTimer::Start(10);
}
IMPLEMENT_APP(MyApp)
class MyFrame : public wxFrame
{
RenderTimer* timer;
BasicDrawPane* drawPane;
public:
MyFrame() : wxFrame((wxFrame *)NULL, -1, wxT("wxTimerDemo " + wxDataTime::Now().Format("%c"))
, wxPoint(50,50), wxSize(500,300))
{
drawPane = new BasicDrawPane( this );
timer = new RenderTimer(drawPane);
Show();
timer->start();
}
~MyFrame()
{
delete timer;
}
void onClose(wxCloseEvent& evt)
{
timer->Stop();
evt.Skip();
}
DECLARE_EVENT_TABLE()
};
// event table for MyFrame
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_CLOSE(MyFrame::onClose)
END_EVENT_TABLE()
bool MyApp::OnInit()
{
frame = new MyFrame();
frame->Show();
return true;
}
// event table for BasicDrawPane
BEGIN_EVENT_TABLE(BasicDrawPane, wxPanel)
EVT_PAINT(BasicDrawPane::paintEvent)
END_EVENT_TABLE()
BasicDrawPane::BasicDrawPane(wxFrame* parent) :
wxPanel(parent)
{
}
void BasicDrawPane::paintEvent(wxPaintEvent& evt)
{
wxPaintDC dc(this);
render(dc);
}
void BasicDrawPane::render( wxDC& dc )
{
static int y = 0;
static int y_speed = 2;
y += y_speed;
if(y<0) y_speed = 2;
if(y>200) y_speed = -2;
dc.SetBackground( *wxWHITE_BRUSH );
dc.Clear();
dc.DrawText(wxT("Testing"), 40, y);
}
Thanks for any help.
#define wxT(x) transforms any x to blank.
dc.DrawText(wxT("Testing"), 40, y); becomes dc.DrawText( , 40, y); and now the error is obvious.
You should define the macro like
#define wxT(x) x
or
#define wxT(x) (x)
Defining the macro wxT is not a good use case, it should be defined when you #include <wx/string.h>, thus #define wxT(x) should be removed from your code. For more information visit #define wxT ( string ). Note that since wxWidgets 2.9.0 you shouldn't use wxT() anymore in your program sources (it was previously required if you wanted to support Unicode).
My problem is that I have written this code inside Game::HandleInput() method but I cannot make the sf::Mouse::getPosition() method to get the mouse coordinates relative to window. Without argument, I don't get an error. However, ship doesn't rotate properly. I have tried getPosition(m_window) and getPosition(&m_window). I am getting this error:
no instance of overloaded function "sf::Mouse::getPosition" matches the argument list
EDIT: UPDATED THE WINDOW.H
Window.h:
class Window{
//Constructers
public:
Window();
Window(const std::string& l_title, const sf::Vector2u& l_size);
...
private:
sf::RenderWindow m_window;
...
}
EDIT: ADDED THE FULL CODE OF WINDOW.CPP:
Window.cpp:
#include "Window.h"
Window::Window() {
Setup("Window", sf::Vector2u(640, 480));
}
Window::Window(const std::string& l_title, const sf::Vector2u& l_size) {
Setup(l_title, l_size);
}
Window::~Window() {
Destroy();
}
void Window::Setup(const std::string& l_title,
const sf::Vector2u& l_size)
{
m_windowTitle = l_title;
m_windowSize = l_size;
m_isFullscreen = false;
m_isDone = false;
Create();
}
void Window::Create() {
auto style = (m_isFullscreen ? sf::Style::Fullscreen
: sf::Style::Default);
m_window.create({ m_windowSize.x, m_windowSize.y, 32 },
m_windowTitle, style);
}
void Window::Destroy() {
m_window.close();
}
void Window::Update() {
sf::Event event;
while (m_window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
m_isDone = true;
}
else if (event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::F5)
{
ToggleFullscreen();
}
}
}
void Window::ToggleFullscreen() {
m_isFullscreen = !m_isFullscreen;
Destroy();
Create();
}
void Window::BeginDraw() { m_window.clear(sf::Color::Black); }
void Window::EndDraw() { m_window.display(); }
bool Window::IsDone() { return m_isDone; }
bool Window::IsFullscreen() { return m_isFullscreen; }
sf::Vector2u Window::GetWindowSize() { return m_windowSize; }
void Window::Draw(sf::Drawable& l_drawable){
m_window.draw(l_drawable);
}
EDIT: UPDATED THE GAME.H
Game.h:
class Game{
public:
Game();
~Game();
void HandleInput();
void Update();
void Render();
Window* GetWindow();
private:
...
Window m_window;
...
}
EDIT: UPDATED THE GAME.CPP
Game.cpp:
Game::Game() : m_window("Multiplayer Space Shooter Game", sf::Vector2u(800, 600)) {
// Setting up class members.
m_shipText.loadFromFile("C:\\Users\\AliTeo\\Desktop\\Piksel çalışmaları\\ship_pixel2.png");
m_ship.setTexture(m_shipText);
m_ship.setOrigin(m_shipText.getSize().x / 2, m_shipText.getSize().y / 2);
m_ship.setPosition(320, 240);
}
void Game::HandleInput() {
...
//Get the angle between ship and mouse.
//Error if there is an argument in getPosition()
m_angle = atan2(sf::Mouse::getPosition().y - m_ship.getPosition().y, sf::Mouse::getPosition().x - m_ship.getPosition().x); //TO DO: getPosition(&Relative To)
m_angle *= 180 / m_PI;
...
}
Window* Game::GetWindow() { return &m_window; }
EDIT: ADDED THE MAIN.CPP
Main.cpp
int main() {
Game game;
while (!game.GetWindow()->IsDone()) {
game.HandleInput();
game.Update();
game.Render();
}
}
First let me give you what I assume is a minimal example reproducing your problem:
#include <SFML/Graphics.hpp>
class MyWindow {
public:
MyWindow()
: m_window({800, 600, 32}, "my window title") {}
private:
sf::RenderWindow m_window;
};
int main() {
MyWindow window;
sf::Mouse::getPosition(window);
}
This code doesn't compile (and admittedly wouldn't do anything interesting when compiled, but that's not the point). I suspect it'd give you the same error that you're currently having if you tried to compile it.
Please note that this is what we expect when we talk about a MCVE: this code is short, simple, exhibits the error and would compile if not because of it.
Besides, it makes the error painfully clear, and if you tried to come up with a MCVE yourself, you may have solved your problem without having to post a question here, which would certainly save you time.
Contrast with your code:
m_angle = atan2(sf::Mouse::getPosition().y - m_ship.getPosition().y
,sf::Mouse::getPosition().x - m_ship.getPosition().x
);
//TO DO: getPosition(Relative To)
This code is legal, but you explained that it is incorrect and you wanted to turn it into something along those lines:
m_angle = atan2(sf::Mouse::getPosition(m_window).y - m_ship.getPosition().y
,sf::Mouse::getPosition(m_window).x - m_ship.getPosition().x
);
//TO DO: getPosition(Relative To)
... which doesn't compile.
However, in this scope m_window is a Window not a sf::RenderWindow!
The problem is that you're passing a reference to an object (MyWindow in my example, Window in your case) that encapsulates a sf::RenderWindow, but isn't convertible to sf::Window& itself.
Therefore, you can't pass it to sf::Mouse::getPosition which expects either nothing or a sf::Window&, but certainly not a Window& or a MyWindow&.
There are a lot of ways of fixing this. Two of which are presented below:
#include <SFML/Graphics.hpp>
class MyWindow {
public:
MyWindow()
: m_window({800, 600, 32}, "my window title") {}
// you could add an accessor
const sf::Window& getSfmlWindow() const { return m_window; }
// you may also expose a method to get the mouse position
// relatively to this window
const sf::Vector2i getMousePosition() const {
return sf::Mouse::getPosition(m_window);
}
private:
sf::RenderWindow m_window;
};
int main() {
MyWindow window;
sf::Vector2i mouse_position;
// this won't work! window isn't convertible to sf::Window&
// mouse_position = sf::Mouse::getPosition(window);
// using the accessor
mouse_position = sf::Mouse::getPosition(window.getSfmlWindow());
// or the exposed method
mouse_position = window.getMousePosition();
}
can't change the color of a background i have this simple class :
here is the c++ file :
#include "HelloWorldScene.h"
USING_NS_CC;
HelloWorld::HelloWorld()
{
;
}
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !LayerColor::initWithColor(Color4B(20,0,0,255)) )
{
return false;
}
winSize = Director::getInstance()->getWinSize();
visibleSize = Director::getInstance()->getVisibleSize();
origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu, 1);
schedule( schedule_selector(HelloWorld::tick) );
return true;
}
void HelloWorld::onExit()
{
LayerColor::onExit();
}
void HelloWorld::onEnter()
{
LayerColor::onEnter();
auto cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("interface/sprites.plist", "interface/sprites.png");
SpriteBatchNode* batch = SpriteBatchNode::create("interface/sprites.png");
this->addChild(batch,BATCH_Z);
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
}
}
void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
}
}
void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
startAnim = true;
};
}
void HelloWorld::tick(float dt)
{
if(startAnim)
{
}
}
void HelloWorld::draw()
{
LayerColor::draw();
}
HelloWorld::~HelloWorld()
{
// Removes Touch Event Listener
_eventDispatcher->removeEventListener(_touchListener);
}
void HelloWorld::menuCloseCallback(Object* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
and the h file :
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class GameObj;
class ReelGameObj;
class HelloWorld : public cocos2d::LayerColor
{
public:
HelloWorld();
~HelloWorld();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(Object* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
void tick(float dt);
virtual void draw();
virtual void onEnter();
virtual void onExit();
void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
protected:
cocos2d::CustomCommand _customCommand;
void onDraw();
private:
cocos2d::EventListenerTouchOneByOne* _touchListener;
cocos2d::Size winSize;
cocos2d::Size visibleSize;
cocos2d::Point origin;
GameObj* pMainWindowObjCenter;
ReelGameObj* pReelGameObj;
bool startAnim;
};
#endif // __HELLOWORLD_SCENE_H__
and nothing no color in the background , why ?
im working on windows with VC 2012
I think your color is too dark. Try changing the values to (255,25,255,255) and check the result.
I created a sample project (in Beta 2), and only changed these lines:
.h:
class HelloWorld : public cocos2d::LayerColor
.cpp:
if( !LayerColor::initWithColor(Color4B(255,255,255,255)) )
The result is a white background.
I cleaned up and updated your code (I use cocos2dx 2.2.1) and I could change the layer's color to red.
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayerColor
{
public:
HelloWorld();
~HelloWorld();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
virtual void onEnter();
void draw();
private:
cocos2d::CCSize winSize;
cocos2d::CCSize visibleSize;
cocos2d::CCPoint origin;
bool startAnim;
};
#endif // __HELLOWORLD_SCENE_H__
The cpp file
#include "HelloWorldScene.h"
USING_NS_CC;
HelloWorld::HelloWorld()
{}
HelloWorld::~HelloWorld()
{}
CCScene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = CCScene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
ccColor4B c = ccc4(255,0,0,255);
if ( !CCLayerColor::initWithColor(c) )
{
return false;
}
return true;
}
void HelloWorld::onEnter()
{
CCLayerColor::onEnter();
}
void HelloWorld::draw()
{
CCLayerColor::draw();
}
Is there any way to blur a widget in Qt? For instance, supose I want to create a 'Loading...' dialog and blur the background (not active window).
This answer is in a series of my overlay-related answers: first, second, third.
It requires some care if you wish for it to work on all platforms. You can't apply effects directly to top-level windows. The hierarchy needs to look as follows:
ContainerWidget
|
+----------+
| |
**Target** Overlay
You apply the effect to the Target widget (say, a QMainWindow). The ContainerWidget is a helper class that keeps the children occupying the full size of the widget. This obviates the need for an explicit zero-margin layout.
The below works, even on a Mac. It wouldn't, had you foregone the ContainerWidget. This works portably on Qt 5 only, unfortunately. On Qt 4, your "cross platform" support excludes Mac :( It works OK on Windows using either Qt 4 (4.8.5) or Qt 5.
// https://github.com/KubaO/stackoverflown/tree/master/questions/overlay-blur-19383427
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif
class OverlayWidget : public QWidget {
void newParent() {
if (!parent()) return;
parent()->installEventFilter(this);
raise();
}
public:
explicit OverlayWidget(QWidget *parent = {}) : QWidget(parent) {
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_TransparentForMouseEvents);
newParent();
}
protected:
//! Catches resize and child events from the parent widget
bool eventFilter(QObject *obj, QEvent *ev) override {
if (obj == parent()) {
if (ev->type() == QEvent::Resize)
resize(static_cast<QResizeEvent*>(ev)->size());
else if (ev->type() == QEvent::ChildAdded)
raise();
}
return QWidget::eventFilter(obj, ev);
}
//! Tracks parent widget changes
bool event(QEvent *ev) override {
if (ev->type() == QEvent::ParentAboutToChange) {
if (parent()) parent()->removeEventFilter(this);
}
else if (ev->type() == QEvent::ParentChange)
newParent();
return QWidget::event(ev);
}
};
class ContainerWidget : public QWidget
{
public:
explicit ContainerWidget(QWidget *parent = {}) : QWidget(parent) {}
void setSize(QObject *obj) {
if (obj->isWidgetType()) static_cast<QWidget*>(obj)->setGeometry(rect());
}
protected:
//! Resizes children to fill the extent of this widget
bool event(QEvent *ev) override {
if (ev->type() == QEvent::ChildAdded) {
setSize(static_cast<QChildEvent*>(ev)->child());
}
return QWidget::event(ev);
}
//! Keeps the children appropriately sized
void resizeEvent(QResizeEvent *) override {
for(auto obj : children()) setSize(obj);
}
};
class LoadingOverlay : public OverlayWidget
{
public:
LoadingOverlay(QWidget *parent = {}) : OverlayWidget{parent} {
setAttribute(Qt::WA_TranslucentBackground);
}
protected:
void paintEvent(QPaintEvent *) override {
QPainter p{this};
p.fillRect(rect(), {100, 100, 100, 128});
p.setPen({200, 200, 255});
p.setFont({"arial,helvetica", 48});
p.drawText(rect(), "Loading...", Qt::AlignHCenter | Qt::AlignTop);
}
};
namespace compat {
#if QT_VERSION >= QT_VERSION_CHECK(5,4,0)
using QT_PREPEND_NAMESPACE(QTimer);
#else
using Q_QTimer = QT_PREPEND_NAMESPACE(QTimer);
class QTimer : public Q_QTimer {
public:
QTimer(QTimer *parent = nullptr) : Q_QTimer(parent) {}
template <typename F> static void singleShot(int period, F &&fun) {
struct Helper : public QObject {
F fun;
QBasicTimer timer;
void timerEvent(QTimerEvent *event) override {
if (event->timerId() != timer.timerId()) return;
fun();
deleteLater();
}
Helper(int period, F &&fun) : fun(std::forward<F>(fun)) {
timer.start(period, this);
}
};
new Helper(period, std::forward<F>(fun));
}
};
#endif
}
int main(int argc, char *argv[])
{
QApplication a{argc, argv};
ContainerWidget base;
QLabel label("Dewey, Cheatem and Howe, LLC.", &base);
label.setFont({"times,times new roman", 32});
label.setAlignment(Qt::AlignCenter);
label.setGraphicsEffect(new QGraphicsBlurEffect);
LoadingOverlay overlay(&base);
base.show();
compat::QTimer::singleShot(2000, [&]{
overlay.hide();
label.setGraphicsEffect({});
});
return a.exec();
}
See QGraphicsBlurEffect Class and QWidget::setGraphicsEffect().
You can refer to this article if you want to apply blur effect on an image. After you create your blurred image you can draw it in QWidget::paintEvent() function.
I'm trying to create a button that is derived from the base class CCMenuItemImage. I want this button to be able to call it's function when it's first touched instead of after the touch ends. However, trying to subclass, I get an error saying it's an invald conversion.
button.ccp:
#include "button.h"
void Button::selected(){
CCLOG("SELECTED");
}
void Button::unselected(){
CCLOG("UNSELECTED");
}
Button* Button::create(const char *normalImage, const char *selectedImage, const char *disabledImage, CCObject* target, SEL_MenuHandler selector) {
Button *button = new Button();
if (button && button->initWithNormalImage(normalImage, selectedImage, disabledImage, NULL, NULL))
{
button->autorelease();
return button;
}
CC_SAFE_DELETE(button);
return NULL;
}
button.h:
#ifndef BUTTON_H
#define BUTTON_H
#include "cocos2d.h"
class Button : public cocos2d::CCMenuItemImage{
public:
virtual void selected();
virtual void unselected();
};
#endif
SinglePlayer.ccp piece:
Button *left1 = Button::create("turncircle.png","turncircle.png", this, menu_selector(SinglePlayer::turning));
MenuItem select() is triggered on touch finished by default.
You need to subclass CCSprite with Touch registered with dispatcher and overwrite the ccTouchBegan
What I can understand is that you are trying to do manual control with the touches of your CCMenuItemImage. Actually all the touches are being handled in CCMenu not in MenuItem so you have to inherit CCMenu rather CCMenuItemImage to override the touches.
In my game I had this problem with CCTableView and CCMenuItem where MenuItem was a prioritised in taking gestures. So I tweaked it with inheriting CCMenu.
It also contain some extra code but just to make everything gets intact I am pasting everything.
ScrollMenu.h Class
class ScrollMenu:public CCMenu
{
public:
ScrollMenu();
virtual ~ScrollMenu(){};
bool isMovedGesture_;
bool istabBar_;
CCMenuItem * previousSelectedItem_;
static ScrollMenu* create(CCMenuItem* item,...);
virtual void registerWithTouchDispatcher();
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
virtual void ccTouchEnded(CCTouch *touch, CCEvent* event);
CREATE_FUNC(ScrollMenu);
};
class ScrollMenuLoader : public cocos2d::extension::CCNodeLoader
{
public:
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(ScrollMenuLoader, loader);
protected:
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(ScrollMenu);
};
ScrollMenu.cpp Class
#include "ScrollMenu.h"
ScrollMenu* ScrollMenu::create(CCMenuItem* item, ...)
{
va_list args;
va_start(args, item);
ScrollMenu *pRet = new ScrollMenu();
if (pRet && pRet->initWithItems(item,args))
{
pRet->autorelease();
va_end(args);
return pRet;
}
va_end(args);
CC_SAFE_DELETE(pRet);
return NULL;
}
ScrollMenu::ScrollMenu()
{
isMovedGesture_ = false;
}
void ScrollMenu::registerWithTouchDispatcher()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
}
bool ScrollMenu::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
return CCMenu::ccTouchBegan(touch, event);
}
void ScrollMenu::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
CC_UNUSED_PARAM(event);
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchMoved] -- invalid state");
isMovedGesture_ = true;
}
void ScrollMenu::ccTouchEnded(CCTouch *touch, CCEvent* event)
{
CC_UNUSED_PARAM(touch);
CC_UNUSED_PARAM(event);
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchEnded] -- invalid state");
CCMenuItem * currentItem = this->itemForTouch(touch);
if(!currentItem && isMovedGesture_ && m_pSelectedItem)
{
if(!istabBar_ || (previousSelectedItem_ && previousSelectedItem_ != m_pSelectedItem))
{
m_pSelectedItem->unselected();
}
}
else if(currentItem)
{
if(currentItem == m_pSelectedItem)
{
if(!isMovedGesture_)
{
m_pSelectedItem->activate();
previousSelectedItem_ = m_pSelectedItem;
}
else{
if(previousSelectedItem_ != m_pSelectedItem)
{
m_pSelectedItem->unselected();
}
}
}
else
{
if(isMovedGesture_)
{
m_pSelectedItem->unselected();
m_pSelectedItem = currentItem;
m_pSelectedItem->activate();
previousSelectedItem_ = m_pSelectedItem;
}
}
if (!istabBar_) {
currentItem->unselected();
}
}
m_eState = kCCMenuStateWaiting;
isMovedGesture_ = false;
}