Cocos2d-x how to create image button with title and font - ios

Hi i am converting my game from Corona SDK to Cocos2d-x 3.0 alpha.
I need to create an image button with text on it. It was very simple in Corona SDK with widget.newButton, which takes all x, y, size, font, image etc in single function.
Now i couldn't find any alternate to this in Cocos2d-x. The closest thing i have found in it is MenuItemImage
auto closeItem = MenuItemImage::create(
"blank.png",
"blank-selected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2));
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu, 1);
It takes the images and event, but i cannot set title and font on it. Anyone has idea how to set title and font on it?

You can use MenuItemFont or MenuItemLabel.
For example:
MenuItemFont::setFontName( "Marker Felt" );
MenuItemFont::setFontSize( 34 );
auto label = LabelBMFont::create( "go back", "fonts/bitmapFontTest3.fnt" );
auto back = MenuItemLabel::create(label, CC_CALLBACK_1(MenuLayer4::backCallback, this) );
or
MenuItemFont::setFontSize( 34 );
MenuItemFont::setFontName("Marker Felt");
auto item6 = MenuItemFont::create("Bugs", CC_CALLBACK_1(MenuLayerMainMenu::menuCallbackBugsTest, this));
For more information, see MenuTest.ccp
update
You can simply new a Label, and add it to you MenuItemImage, such as:
LabelTTF* closeLabel = LabelTTF::create("close", "Marker Felt", 28);
closeItem->addChild(closeLabel);
And you may need to adjust the label's position.

It's fairly simple, this works in at least cocos2d-x 3.10 and 3.16, but if you're looking to make a button with cocos2d, use the built in cocos2d::ui namespace for Button.
cocos2d::ui::Button* button = cocos2d::ui::Button::create();
//button textures
button->loadTextures(
"<your default texture>.png",
"<your pressed texture>.png",
"<your disabled texture>.png",
cocos2d::ui::Widget::TextureResType::PLIST
);
//text content, font, and size
button->setTitleText("Touch me!");
button->setTitleFontName("arial");
button->setTitleFontSize(24.0f);
//bind a callback with a lambda
auto touch_handler = [](cocos2d::Ref* ref, cocos2d::ui::Widget::TouchEventType evt)
{
if (evt == cocos2d::ui::Widget::TouchEventType::ENDED)
{
//do your callback logic here, ie play a sound or vibrate
}
};
button->addTouchEventListener(touch_handler);
//then finally, like a Nodes, add it to your scene
my_scene->addChild(button);
The textures you'll load are ideally packed into a texture with something like TexturePacker, otherwise you'll change it to LOCAL instead of PLIST. The textures will be used when the button is default, or when there's a touch, or when the button is disabled.

Related

Xamarin - Button with text and image in absolute layout results in mis-aligned elements

I am trying to create a button in a Xamarin Forms cross-platform app (iOS and Android), where the button has both text and an image. The XAML is pretty straightforward:
<AbsoluteLayout ...>
<Labels and backgrounds and such>
<Button x:Name="HomeButton2" TranslationX="6" TranslationY="100"
BackgroundColor="#efeff4" TextColor="#4a4a4a"
WidthRequest="58" HeightRequest="76"
ContentLayout="Top,5"
Image="TaskBar_Assets_HomeButtonIcon.png" Text="Home"
Clicked="HomeButton_Clicked" />
</AbsoluteLayout>
but what I get on the iPad is a button where both the image and the text are strangely pushed over to the side:
(the source image "TaskBar_Assets_HomeButtonIcon.png" is 47 x 44 so it should fit fine in the overall button area)
The only way I can find to make this look better, is to make a custom control based on Button, and then I can see that several of the properties of the underlying UIButton seem wonky:
The Control.ImageView.Frame is all zeroes:
Control.ImageView = <UIImageView: 0x12df52940; frame = (0 0; 0 0);
clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO;
layer = <CALayer: 0x173623180>>
The Control.ImageEdgeInsets and .TitleEdgeInsets look odd (the right + left seem like they leave no space for the image or text):
Control.ImageEdgeInsets = {-8.9501953125, 20.33935546875, 8.9501953125, -20.33935546875}
Control.TitleEdgeInsets = {22, -23.5, -22, 23.5}
What I'm doing, is adjusting the Control.ImageEdgeInsets so that Control.ImageEdgeInsets.Left is equal to the half of the (button width minus the image width) and setting Control.ImageEdgeInsets.Right to zero (for no particular reason except that it works)
and then figuring out what to set Control.TitleEdgeInsets was done with trial & error, I ended up with values related to the width of the "Home" text (41 pixels):
Control.ImageEdgeInsets updated to {-8.9501953125, 5.5, 8.9501953125, 0}
Control.TitleEdgeInsets updated to {22, -50, -22, -9}
That results in a reasonable button look:
although it looks like I need to do more trial & error to get the text "Home" actually centered.
But why do I need to go through all this? Why doesn't the button just display text & image correctly in the first place?
And if I do have to go through all this, why are the values for Left & Right for ImageEdgeInsets and TitleEdgeInsets so different?
Most of the articles I have read about images on buttons suggest constructing your own using an Image and a Label in a grid using a gesture recognizer to handle the tap.
You could also try a button and an image in a grid.
Use Margin to adjust placement.
I try and stay clear of absolute layout.
Here's the ButtonRenderer source code from Xamarin.Froms.If you set the ContentLayout to Top, the below codes will be run:
void ComputeEdgeInsets(UIButton button, Button.ButtonContentLayout layout)
{
...
var horizontalImageOffset = labelWidth / 2;
var horizontalTitleOffset = imageWidth / 2;
...
button.ImageEdgeInsets = new UIEdgeInsets(-imageVertOffset, horizontalImageOffset, imageVertOffset, -horizontalImageOffset);
button.TitleEdgeInsets = new UIEdgeInsets(titleVertOffset, -horizontalTitleOffset, -titleVertOffset, horizontalTitleOffset);
}
As the codes show, the Left offset of image is horizontalImageOffset which is labelWidth / 2. The Left offset of title is horizontalTitleOffset which is imageWidth / 2.
So, when the text is wider, the left offset of image will be bigger. When the image is wider, the left offset of text will be bigger.
Edit:
In native iOS, the default layout is like the left image: Image is at left and Label is at right. In Xamarin for Top setting, Xamarin moves the Image up and right, moves the Label down and left to makes them like the right image. I paint this illustration, hope it clear.

How to programmatically create a UISlider like that of Apple Music in Swift

I'm looking to create a UISlider like the one below:
I've seen code like the following to change the image, but ideally I'd like to forgo using an image and create a basic downward line programmatically.
var thumbImage : UIImage = UIImage(named: "yourImage")!
var size = CGSizeMake( thumbImage.size.width * ratio, thumbImage.size.height * ratio )
self.setThumbImage( self.imageWithImage(thumbImage, scaledToSize: size), forState: UIControlState.Normal )
A UISlider is designed to use an image for the "thumb" control. If you want to draw the thumb control programmatically you will need to create your own control. A slider isn't that complicated. It would be fairly straightforward. (But it would be a heck of a lot easier to just use the standard slider control with a red line as the thumb image.)

awesome wm taglist size

Can't find any manual about changing width of taglist element size.
Taglist element is wider than icons I had set. It looks really awful =(
Screenshot: http://s12.postimg.org/fkva7xywd/Screenshot_16_02_2014_16_04_07.png
Taglist element consist of icon, text and gap between them. This gap defined in awful.widget.common.list_update function in line
m = wibox.layout.margin(tb, 4, 4)
Despite you have no text, double gap is still here. To solve this problem you can copy list_update function to your rc file, fix it and send as fifth(!) argument in awful.widget.taglist.
just tell your imagebox not to be resizable, example:
wibox.widget.imagebox(beautiful.clock, false)
or you can even resize you wibox:
mywibox[s] = awful.wibox({ position = "top", screen = s, height = 32 })
you just need to modify height value
or another method using wibox.layout.constraint:
clock_icon = wibox.widget.imagebox(beautiful.clock, true)
local const = wibox.layout.constraint()
const:set_widget(clock_icon)
const:set_strategy("exact")
const:set_height(16)
then, instead of adding your icon to the layout, just add the constraint.

Corona tabbar rendering precision

I'm trying to build a tabbar in corona only using custom graphics for up/down states, and background. I find that corona adds left and right padding as the attached image & basic setup below shows. The images are both 32x32 and should fill the space exactly, instead there are black lines at either end and the buttons are forced to overlap.
I've tried every option available in the docs but with no success. Does anyone know if there's an undocumented option that overrides the automatic positioning of tabbar buttons?
-- table to setup buttons
local tabButtons = {
{ up="icon1.png", down="icon1-down.png", width = 32, height = 32, cornerRadius=0, onPress=onFirstView, selected=true },
{ up="icon2.png", down="icon2-down.png", width = 32, height = 32, cornerRadius=0, onPress=onSecondView },
}
-- create the actual tabBar widget
local tabBar = widget.newTabBar{
width=64, height=32,
buttons = tabButtons
}
Here's the output:
This is a known bug and is being addressed.

Zoom-In and Zoom-Out can not resize?

I'm using MFC Print preview dialog. Before I used BMP image to represent those zoom buttons and they worked fine, but now I want to use the text labeling instead. But no matter what value I use, the size remains the same when I run the program. Here below what I have:
IDD_FILE_KHANH_PRINT_PREVIEW DIALOG 0, 0, 219, 19
STYLE DS_SETFONT | WS_CHILD
FONT 8, "MS Sans Serif"
BEGIN
PUSHBUTTON "&Print",AFX_ID_PREVIEW_PRINT,2,3,36,12
PUSHBUTTON "Zoom &In",AFX_ID_PREVIEW_ZOOMIN,40,3,46,50
PUSHBUTTON "Zoom &Out",AFX_ID_PREVIEW_ZOOMOUT,70,3,46,12
PUSHBUTTON "Pre&v Page",AFX_ID_PREVIEW_PREV,100,3,46,12
PUSHBUTTON "&Next Page",AFX_ID_PREVIEW_NEXT,150,3,46,12
CONTROL "Landscape",IDC_LANDSCAPE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,230,3,67,13,WS_EX_CLIENTEDGE
PUSHBUTTON "&Close",AFX_ID_PREVIEW_CLOSE,300,3,35,13
END
SO I change the values for two buttons, and one is working while zoom-in does not work, for example:
PUSHBUTTON "&Print",AFX_ID_PREVIEW_PRINT,2,3,136,12
this one works because i can see the Print button has wider length. So I change the value from 36 to 136.
PUSHBUTTON "Zoom &In",AFX_ID_PREVIEW_ZOOMIN,40,3,146,50
But this one does not work because the ZoomIn remains the same size and here I change 46 to 146.
I believe this is only the place that I assign the values for the buttons. thanks.
The height of your "Zoom &In" button is 50 but the dialog height itself is only 19. You should change the the height of the button so it is 12 just like the others.
It seems I made a mistake. Before I used bitmap image to represent those buttons and when I decide to use text labeling and forgot this function below that loads the bitmap of the zooming buttons. So after commenting out the m_zoomIn and m_zoomOut statements, they work now.
int CKhanhPrintPreview::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CPreviewView::OnCreate(lpCreateStruct) == -1)
return -1;
m_pToolBar->EnableToolTips( TRUE );
m_zoomIn.AutoLoad(AFX_ID_PREVIEW_ZOOMIN, m_pToolBar, IDB_PREV_ZOOMIN );
m_zoomOut.AutoLoad(AFX_ID_PREVIEW_ZOOMOUT, m_pToolBar, IDB_PREV_ZOOMOUT );
return 0;
}
thanks.

Resources