Share Your Code

Example MessageBox code

Posted by KenRogoway, Posted on October 23, 2011, Last updated October 24, 2011

GitHub URL: 
https://github.com/KenRogoway/MessageBox

A common feature in a lot of games and applications is the use of a MessageBox to display some text to the user and provide then with different buttons to signify what action they want to take. Something like a "Do you want to Exit?" with a Yes and No button.

I have posted source for the cMessageBox.lua module along with sample tests showing the features of this code. The code depends on the ui.lua module (included in the download).

The code supports generic message boxes where no images are required, or custom bitmap based message boxes that can optionally have bitmap buttons.

If anyone finds any issues or has any suggests to add to the functionality please let me know.

From the comments section in cMessageBox.lua:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-- newMessageBox( params )
 
-- Parameters:
-- modal                        (optional, defaults to true)
-- msgBoxType           (optional, defaults to "MB_Okay") Can be any of the following:
--                                      "MB_Okay", "MB_OkayCancel", "MB_YesNo", "MB_YesNoCancel"
-- background           (optional, if no background image is specified then display.newRect() will be used)
-- onPress                      (optional)
-- onRelease            (required)
-- rectCornerRadius     (optional, only used if no background is specified)
-- rectStroke           (optional, only used if no background is specified)
-- rectFillColor        (optional, only used if no background is specified)
-- rectStrokeColor      (optional, only used if no background is specified)
-- width                        (optional, defaults to 1/2 of the display.contentWidth)
-- height                       (optional, defaults to 1/2 of the display.contentHeight)
-- borderSizeX          (optional, defaults to kMsgBoxDefault_BorderSizeX)
-- borderSizeY          (optional, defaults to kMsgBoxDefault_BorderSizeY)
-- captionText          (optional)
-- captionSize          (optional, defaults to kMsgBoxDefault_CaptionSize)
-- captionColor         (optional, defaults to kMsgBoxDefault_CaptionColor)
-- messageText          (optional)
-- messageSize          (optional, defaults to kMsgBoxDefault_MessageSize)
-- messageColor         (optional, defaults to kMsgBoxDefault_MessageColor)
-- messageJustify       (optional, defaults to display.CenterReferencePoint)
-- buttonImages         (optional, if no button image array is specified then buttons will be created using newRect())

Typical use of the cMessageBox code to create a message box:

1
2
3
4
5
6
7
8
9
10
11
12
  local testMsgBox = cMessageBox.newMessageBox{
    msgBoxType = "MB_OkayCancel",
    width = math.floor( display.contentWidth*0.6 ),
    height= math.floor( display.contentHeight*0.6 ),
    captionText = "Example "..event.id,
    messageText = "Sample of a message box using the defaults.",
    messageJustify = display.CenterReferencePoint,
    onEvent = onMessageBoxDone,
    messageColor = { 0, 0, 0, 255 }
    }
            
  gameGroup:insert( testMsgBox )

The code is under the MIT license so you are free to do whatever you want with it per the terms of that license.


Replies

doubleslashdesign
User offline. Last seen 1 day 14 hours ago. Offline
Joined: 27 Nov 2010

The actual GitHub Link above is

https://github.com/KenRogoway/MessageBox

KenRogoway
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 17 Jan 2011

Thanks, @doubleslashdesign, I've fixed the link.

jamiebilly
User offline. Last seen 35 weeks 4 days ago. Offline
Joined: 17 Jan 2011

I get the following error in the debugger.

"WARNING: object.stageHeight has been deprecated. Use object.contentHeight instead"

I don't see that exact command in there though. Any ideas?

KenRogoway
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 17 Jan 2011

I checked and this isn't used anywhere in the example.

Do you get a file and line number for the warning? Also, this is just a warning so you should be able to ignore it.

What version of the SDK are you using? This code works with 591 and above.

jamiebilly
User offline. Last seen 35 weeks 4 days ago. Offline
Joined: 17 Jan 2011

I'm using SDK 703. I noticed that the warning shows up once for each line in the message box. So the captionText invokes one and the messageText invokes another. If my messageText is more than one line, I get more warnings.

KenRogoway
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 17 Jan 2011

can you copy your call to cMessageBox here? I'll give it a try

jamiebilly
User offline. Last seen 35 weeks 4 days ago. Offline
Joined: 17 Jan 2011

Thanks in advance for your help. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
--Message Box Vars
local msgDialog = "Get the player to the goal."
local msgCaption = "Object:"
local msgHeight = 260
local msgWidth = 260
local msgSize = 14
        
--Message Box
local playerMsgBox = nil
local onMessageBoxDone = function( event )
     if event.phase == "release" then
                print( "Message Box button id", tostring( event.id ) )
                playerMsgBox.isVisible = false
                display.remove( playerMsgBox )
                playerMsgBox = nil
        end
                return true
        end
        
playerMsgBox = cMessageBox.newMessageBox{
msgBoxType = "MB_Okay",
width = msgHeight,
height= msgHeight,
captionText = msgCaption,
messageText = msgDialog,
messageSize = msgSize,
messageJustify = display.CenterReferencePoint,
onEvent = onMessageBoxDone,
messageColor = { 0, 0, 0, 255 }
    }
playGroup:insert(playerMsgBox)

KenRogoway
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 17 Jan 2011

@jamiebilly,

I copied your code into the cSceneTest.lua and it works fine.

If you email me I can send you a screen shot and the modified cSceneTest.lua so you can test it yourself.

I ran it on Build 7.04

Ken [at] HomebrewSoftware [dot] com

KenRogoway
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 17 Jan 2011

I have put up a new version of the cMessageBox module that now supports bitmap fonts using bmf.lua, and also lets you specify the x, y of the message box if you don't want it centered on the screen.

http://www.homebrewsoftware.com/Download/CoronaSDK/HomebrewMessageBox_v1_1.zip