Button

Type     [gd]

class

Inheritance

Button ➝ GroupDisplay ➝ Object

Overview

  Creates text button that can be tapped. It is a Group that contains RoundRect (or Rect or Circle) and Text objects. Default click effect is invertin color.

See also [bg]

Group, Display


Syntax


local btn = Button(text [, onPressfunc] [, option] ])


  • text (required : string )  string that is displayed on button


  • onPressFunc (optional : function )  function that is called when the button is pressed.

The first parameter of the called function is button object.

The second parameter of the called function is touch event table.


  • option (optional : table) = {

fontSize = n, -- default : 50 pixels

textColor = color,     -- default color : Color.WHITE

margin = n, -- in pixels, default value is fontsize*0.5 pixels

fill = color,                 -- background color (default : Color.GREEN)

strokeColor = color, -- default color : Color.LIGHT_GREEN

strokeWidth = n, -- border line width in pixel (default: fontsize*0.15 pixels)

effect = string, -- 'invertColor'(default), 'shrink', 'expand', 'none'


           -- shape of the background

shape = string         -- 'roundRect'(default), 'rect', 'circle'

width = n, -- width of the button if shape is 'roundRect' or 'rect'

height = n, -- height of the button if shape is 'roundRect' or 'rect'


radius = n, -- radius of the button if shape is 'circle'

}


Note : The width and height of the button is automatically set using the fontSize and margin. If the user want to force the width/height regardless of the font size, set width or height field of option table.


Methods



(getter) method

description

return

obj:getString()

return current text string of the button

string

obj:getFontSize()

return current font size of the button text 

number


(setter) method

description

return

obj:setString(str)

change the text of button (button is resized if necessary)

self

obj:setFontSize(n)

change font size of the text on button (button is resized if necessary)

self

obj:setTextColor(color)

set color of the text with Color object

self

obj:setStrokeWidth(n)

set width of the outline of the button

self

obj:setStrokeColor(color)

set color of the outline of the button with Color object

self

obj:fill(color)

set color of inner area with Color object

self

obj:setWidth(n) 

force button width to n regardless of font size (If button has circular shape, this method does not work)

self

obj:setHeight(n)

force button height to n (If button has circular shape, this method does not work)

self

obj:setRadius(n)

change the radius of button (If button has rectangular shape, this method does not work)

self


user-defined method


custom method

description

return

obj:onPress(e)

define callback function that is called when the button is pressed.

self

obj:onRelease(e)

define callback function that is called when the button is released.

self



Examples


local btn1 = Button('None') -- nothing happens when pressed

local btn2 = Button('None',{fontSize=100}) -- nothing happens when pressed


-- define callback function when button is created


local btn = Button('Click me', function(self,e)

    print('pressed')

end)


--or



-- create button and define callback functions later


local btn2 = Button('Click me', {fontSize=100})


function btn2:onPress(e)

    print('pressed')

end


function btn2:onRelease(e)

    print('released')

end


-- set predefined callback function when button is created


local function onPress(self,e)

    print('clicked')

end

local btn = Button('Click me', onPress)


-- define callback function when button is created and add onRelease() method later.


local btn = Button('Click me', function(self,e)

    print('pressed')

end, {fontSize=100, effect=false})


function btn:onRelease(e)

    print('released')

end


댓글 없음:

댓글 쓰기