1. 打开PainterEngine设计器(请参照上一章节教程),拖动Label,Edit,Pushbutton控件,制作以下布局
2. 其中,身高的edit的ID设置为edit_stature
3. 其中,体重的edit的ID设置为edit_weight
4. 其中,显示结果的label的ID设置为label_result
5. 其中,计算结果的ID设置为pushbutton_execute
6. 点击debug->execute,将布局保存
7. 将以下代码复制替换到PX_Application.c中
#include "PainterEngine_Application.h"
#include "./architecture/PainterEngine_Designer.h"
PX_Application App;
PX_FontModule fm;
PX_Object *root,*ui_root,*designer;
PX_Object *edit_weight, *edit_stature, *label_result, *pushbutton_execute;
px_void PX_ApplicationExecute(PX_Object *pObject, PX_Object_Event e, px_void *ptr)
{
px_char content[64];
px_float w = PX_atof(PX_Object_EditGetText(edit_weight));//取得体重
px_float s = PX_atof(PX_Object_EditGetText(edit_stature));//取得身高
if (s>0)
{
px_float bmi = w / (s / 100.f) / (s / 100.f);//计算BMI
PX_sprintf1(content, sizeof(content), "你的BMI结果是:%1", PX_STRINGFORMAT_FLOAT(bmi));//构造字符串
PX_Object_LabelSetText(label_result, content);//设置label文本
}
}
px_bool PX_ApplicationInitialize(PX_Application *pApp,px_int screen_width,px_int screen_height)
{
PX_IO_Data data;
PX_ApplicationInitializeDefault(&pApp->runtime, screen_width, screen_height);//默认显示函数
PX_FontModuleInitialize(&pApp->runtime.mp_resources,&fm);//初始化字模
PX_LoadFontModuleFromFile(&fm,"gbk_18.pxf");//加载中文字模
root=PX_ObjectCreate(&pApp->runtime.mp_ui,PX_NULL,0,0,0,0,0,0);//创建根对象
ui_root=PX_ObjectCreate(&pApp->runtime.mp_ui,root,0,0,0,0,0,0);//创建ui根对象
designer=PX_DesignerCreate(&pApp->runtime.mp_game,&pApp->runtime.mp_ui,&pApp->runtime.mp_game,root,ui_root,PX_NULL,&fm);//创建设计器
data = PX_LoadFileToIOData("ui.json");//加载ui布局文件
PX_DesignerImport(designer, data.buffer);//设计器导入ui布局文件并部署到ui
PX_FreeIOData(&data);//释放加载数据
edit_weight = PX_ObjectGetObject(ui_root, ".edit_weight");//取得体重编辑框的对象指针
edit_stature = PX_ObjectGetObject(ui_root, ".edit_stature");//取得身高编辑框的对象指针
label_result = PX_ObjectGetObject(ui_root, ".label_result");//取得结果label的对象指针
pushbutton_execute = PX_ObjectGetObject(ui_root, ".pushbutton_execute");//取得pushbutton的对象指针
PX_ObjectRegisterEvent(pushbutton_execute, PX_OBJECT_EVENT_EXECUTE, PX_ApplicationExecute, PX_NULL);//绑定按钮事件
return PX_TRUE;
}
px_void PX_ApplicationUpdate(PX_Application *pApp,px_dword elpased)
{
PX_ObjectUpdate(root,elpased);
}
px_void PX_ApplicationRender(PX_Application *pApp,px_dword elpased)
{
px_surface *pRenderSurface=&pApp->runtime.RenderSurface;
PX_RuntimeRenderClear(&pApp->runtime,PX_OBJECT_UI_DEFAULT_BACKGROUNDCOLOR);
PX_ObjectRender(pRenderSurface,root,elpased);
}
px_void PX_ApplicationPostEvent(PX_Application *pApp,PX_Object_Event e)
{
PX_ApplicationEventDefault(&pApp->runtime, e);
PX_ObjectPostEvent(root,e);
}
8. 运行程序