My project needs so many buttons. But for each button I make, it has to load every images even though I make same buttons like this :
auto btn = cocos2d::ui::Button::create("images/bone01.png","images/bone02.png","");
I think it's very wasteful in respect of system cost. I want to make only 1 texture2d* and use it when making buttons. I searched many times with keyword, 'cocos2d-x button texture...' but there is no answer I could find. Is there any method to use texture in creating Button, or should I make a class inherited by Sprite class which contains touchEventListner and function parameters? if so, how can I start to create function to use method like this :
btn->addTouchEventListener(CC_CALLBACK_0(HelloWorld::touchBoard, this));
just a little example would be okay to me.
=============================================
I made a singleton Class and put all Buttons, and tried to use it when it's needed. But failed because Button is autoreleased when it is created, so if I use it another cpp, like :
auto appleBtn = DataClass::getInstance()->_appleBtn;
an error occurs like this :
Expression : child->_parent == nullptr
I may succeed in this way by using retain() & release(), but I would rather to use safer way.
Although I do not have any experience with cocos2d, I assume it will load this texture in memory only once, and use this information for all buttons. Isn't it possible to make a button class and inherited all the same buttons from this class? Again I have no clue how cocos2d works but any other game engine this should be possible :)
Related
I searched on the forum and I don't find a solution for my issue. So I hope you can help me :)
I work on a personal project for SFC designing (Sequential functional chart) and I'm working with visual studio in SDI(I'm using MFC library). If you see the "design" of an SFC you can see the different elements needed to compose this. So you can find Step, Transition, and more. If I take a step for explaining my issue, after double click on the step a popup dialog is opened with the elements to define this step (actions on this step, and more). The issue is here, I can't see two or more step elements at the same time. I want to reuse the existing concept on other software, see this.
Step close
Step open
Dialog to add
My question is, how I can implement a dialog with my graphic element in mainframe (In this case, a step)? I don't know how I can insert a dialog with my element, I think I need to use CFormView, but I don't know.
This dialog needs to be resizable and reduce directly by the step graphic.
any idea?
Thank you in advance! Sorry for my English ..
Sorry, I think my request is not clear .. (Thank you for your answer)
The context, it's an SDI application with the Document/View architecture. Actually the view is derived from CScrollView.
So, in the document class, you have the different lists of components for make SFC (Steps, transitions and symbols ..). I'm working today on Step element.
The user inserts a new step, the step is draw on the view like this :
enter image description here
And now the user want change the events on this Step, for this after double click on the step the events editor is opened at right of step draw, like this :
enter image description here
For this, I've created a new dialog resource and create the class by wizard in CForwView derived class. In step attribute, you can find one instance of this derived class (The derived class of dialog).
But this doesn't work correctly, I think my method is bad. At the first try, I have sent the pointer of the current document to the "CFormView::Create" function for having the "Save" button active with the focus on the FormView. But after destroying the step, the instance of FormView is destroyed and the document with the instance of formview ...
No problem, you can use "Create" within CCreateContext a null pointer. But with the document or without I have a lot of problems (graphic design not correct in FormView, regularly (not systematic I have assert fail on Proc exchange (for differents reason)). But the "concept" is good, the editor follows the draw if I scroll, I can open or close the editor at any time and on any elements.
For the old capture, it's my SFC designer "model". My application is a complement to this application, so I want a similar design. I don't know how work my model application ..
On my application all is draw by MFC GDI, I don't use ActiveX or other tools.
So what is the correct way to implement one instance of editor by instance of step ?
For the implementation on this FormView I have :
- Make new dialog in ressource
- Create a derived class of CFormView with the created dialog
- Add one instance in attributes of Step element
- "OnDbClickOnStep" -> Call "Create" with the good position / size, pointer of mainview (in my case the CSrollView derived class)
- Done, FormView inserted in a mainview, I can edit my step events.
? Not done, I lost save button and other function linked to the document with the focus on a control in FormView. The app want a document with this view, how to override this ?
? Error in Proc exchange, for different reason...
You have an idea ?
You normally don't draw anything in the "main frame" (or the "MDI clild frames", in the case of a MDI application), this is done by the library, and imo sufficiently so. You display your data in a CView-derived class.
CView is the base class of all other MFC view classes. It's a simple graphical class - you need to paint it yourself in the OnDraw() member.
CScrollView is a descendant of CView, adding scrolling functionality (scroll-bars are automatically displayed if the scrollable area is bigger than the visible window area).
CFormView is a descendant of CScrollView, displaying a dialog resource-script, containing "controls" (edit-boxes, check-boxes, images, ActiveX etc).
As in your case the "controls" won't be initially known (except maybe for some few special cases) and rather programatically created, the resource script will most likely be empty, so using either CFormView or CScrollView will basically be the same. I would suggest starting with CFormView, and "downgrade" it to CScrollView if CFormView is not necessary or causes you troubles.
What are those "Step" items shown in the pics? ActiveX controls, child dialogs maybe? These work best as child controls on a dialog window. Are they already implemented, or they are just pics of some other software? Btw ActiveX is a way to define controls that can be used in other projects too, without having to include them in the project source.
I have a program that will act as a network-linked visualizer for a hardware system with no screen (think RaspberryPi). I currently have a QTabWidget with around 45-50 QPushButtons and textboxes (for numbers), but I'm bothered by the amount of connect statements I'll have to make. Is there a better way to make this many buttons? I've seen methods for using an array, but nothing with using a QTabWidget and the only thing I saw on that was creating a form.
I'm sorry I can't give you a proper answer with code. I have no proper internet so I have to do this on a phone.
Set up an array of buttons, maybe even a custom class derived from QPushButton with a signal that includes an identifier for the button, such as std::string name. You can then run a loop assigning your connects with something like:
connect(buttonArray[x], SIGNAL(buttonIsPushed(std::string) ), receivingObject, SLOT(buttonWasPushed(std::string)))
Of course this may be of little help if the receiving object has to pass on the signal as a signal.
I'm creating a game using Cocos2d-x. I'm currently creating a gameover menu, in that menu I need to be able to switch to both my menuscene and my gamescene (When I say switch to gamescene I only really only mean "to restart" the game). But circular dependencies stop me from being able to do this.
MenuScene needs to be able to useGameScene::create() in order to switch to the gamescene and the gameover menu needs to be able to use both of GameScene::create() or its restart funtion and MenuScene::create() which is giving me circular dependency problems
I can't separate my gameover menu to it's own file as I still need the GameScene dependency and GameScene would need gameover.
I can't combine them as GameScene then needs to depend on MenuScene
So my question is: How do I alternate between two scenes in cocos2d-x c++.
I read somewhere about pushing and popnig scenes in Director, but I don't really understand how that works, or if I could use that for my purpose.
Thank you in advance!
EDIT:
Now that I think about it, could I not just push mMenuScene to Director before switching to GameScene? That should work if I understand that push/pop mechanic correctly.
I think you might have a misconception of how complex this is, using the way I provided below you can and should definitely split your game over scene into its own file.
The scene replace is easy enough, just use the code below:
Including your file:
#include "MainGameScene.h"
Creating and switching scenes in your onClickListener:
auto gameOverScene = GameOverScene::createScene();
// use code below for hard replace
Director::getInstance()->replaceScene(gameOverScene );
// or use code below for transition fade replace
Director::getInstance()->replaceScene(TransitionFade::create(1, gameOverScene , Color3B(255, 255, 255)));
As for the restart functionality. I usually provide a callback to my game over scene that I call when the restart button has been clicked. Not that I ever swap out my scene completely for a mobile game over scene, but I still do it the same way regardless. So lets do steps (This assumes you seperated your game over scene into it's own file named GameOverScene :) ).
Store a function pointer in your GameOverScene.h to your reset method in MainGameScene:
std::function<void()> _resetCallback;
Set your function pointer from the main game scene, before running with the GameOverScene.
auto gameOverScene = GameOverScene::createGameOverScene();
gameOverScene->setResetCallback(std::bind(&MainGameScene::reset, this));
When your reset button is clicked, call the _resetCallback
void GameOverScene::onResetClicked(Ref* sender)
{
_resetCallback();
}
This should provide you with all the functionality you need to set up a what you want as well as remove the circular dependency that you have. I have used this way many times before and it always works. Let me know if this solution works for you.
Using SFML, I created a simple button class that I plan to develop into a more advanced GUI that will do things in a game I making. Right now I'm trying to decide how buttons interact with other objects in the game. I need these buttons to be able to interact with game objects, other buttons, really a lot of things that aren't even created yet. The solution I came up with was this:
template <typename T> class Button
{T* myLink;};//you can imagine functions that do things to the object at this address
This way the button has a link to what it plans to interact with, and I figured using templates was good because now I can tell the button what type it's going to be activating/changing. Next I wanted to make a Menu class that would just be a box that held a bunch of buttons. But this Menu class can't just have a single vector of Buttons because each Button is a different class depending on what type it was created with.
What's the best solution to this problem, should the Menu class be a template that can hold multiple types of Buttons, or should I be using inheritance somehow? If inheritence, please make a short example.
Thank You.
I think using Boost Signals would be a good solution. That way, your menu can hold a vector of buttons since they won't be depending on a type.
class Button
{
public:
boost::signal<void ()> pushed;
}
You can have a different signal for whatever "interactions" you need your button for.
I'm rather new to C++, I have a bit of experience with MCV programming in Java. im using GTKmm on C++
What I'm trying to do is writing an application for teaching assistants to submit and edit applications to various positions, and administrators to come in view, and accept these applications.
What I'm trying to do at the begging is create 3 'frames' (I'm working on the submitting application for students only at the moment)
This first will have 2 buttons 1 for selecting if you're a student/admin
Upon clicking you're a student I want to hide this frame and show my second frame
The second frame will have another 2 buttons one for creating an application, and the other for editing applications
My core problem is that I don't understand how to switch between the frames, I've written all the code for my Model, and understand everything I want it to do however I cant seem to find how to do this...
My only idea would be to create windows for each of these, make them look all nice w/e, then when a button is pressed have that window close and a string written to file I can access to see which button has been pressed, then open a new window accordingly. Should I do it like this or is there a better way?
I think I can suggest a better/more idiomatic option for any version >= GTK+ 3.10 - which, to be fair, arrived about half a year after the accepted answer.
If you want to switch between widgets one-at-a-time without any accessories like tabs, then a Gtk::Stack seems like a better option. Because it's specifically geared for one-at-a-time presentation, without any redundancy and (theoretical) overhead from a Notebook's manual tabbing features, which you'd just be disabling straight away! It's a container with multiple children, with one visible at any given moment, and of course methods to change the active child.
You can hook up your own widgets and/or events to manage which of the Stack's children is shown. Alternatively - albeit possibly just restoring the redundancy in this case - there's a StackSwitcher companion widget, which is pretty much a vertical tab-bar as seen in the GTK+ demo and GNOME Tweak Tool.
Easiest way is to use a Notebook widget. You can hide the tabs since you will be controlling which page is showing, using method set_show_tabs(false). Put the top level widget for each of your frames in a pane using method append_page(), and switch between them using set_current_page(). You might want to hide the notebook's bevel if it's distracting, using method set_show_bevel(false).
Use signals to make a widget (e.g. "I'm a student" button) on one page do something (e.g. go to the second page). If you don't know what this means or how to do it, go through the gtkmm tutorial, it will explain this and more.
A bit too late ! But here is my try :
Gtk::Notebook is great but it is not ideal in switching between app frames on menu item clicks. Gtk::Stack, since gtkmm 3.10, exists to mitigate this. Assuming you're using glade and Gtk::Builder :
class
class AppName : public Gtk::ApplicationWindow
{
public:
//...Your app methods and callbacks
void on_mb_itemname_selected(); // The call back for our menu item click/select
private:
//Builder which will help build the app from a .glade file
Glib::RefPtr<Gtk::Builder> _builder;
//...
//Your menu item to activate a particular frame
Gtk::MenuItem * _mb_itemname;
//Your handle to Gtk::Stack which is usually the stack for the whole app
Gtk::Stack * _app_stack;
//...
}
constructor
AppName::AppName(BaseObjectType *cobj,
Glib::RefPtr<Gtk::Builder>& ref_builder)
:Gtk::ApplicationWindow(cobj),_builder(ref_builder)
{
//.. Other setup
_builder->get_widget("your_glade_id_to_stack",_app_stack);
_builder->get_widget("your_glade_id_to_menu_item",_mb_itemname);
// Connect signal_select of our menu item to appropriate signal handler.
mb_itemname->signal_select().connect(
sigc::mem_fun(*this,&AppName::on_mb_itemname_selected));
}
our callback
void AppName::on_mb_itemname_selected()
{
// Change the visible child of the stack concerned.
Gtk::StackTransitionType ttype = STACK_TRANSITION_TYPE_NONE;
_app_stack->set_visible_child("your_widget_name",ttype);
// Note that widget name is not widget glade id.
// You can set your widget under name Packing -> Name
return;
}