205 lines
7.7 KiB
C
205 lines
7.7 KiB
C
#include "include.h"
|
||
#include "func.h"
|
||
#include "func_calculator_alg.h"
|
||
|
||
#define TRACE_EN 1
|
||
|
||
#if TRACE_EN
|
||
#define TRACE(...) printf(__VA_ARGS__)
|
||
#else
|
||
#define TRACE(...)
|
||
#endif
|
||
|
||
enum {
|
||
COMPO_ID_NUM_DISP = 128,
|
||
};
|
||
|
||
typedef struct f_calculator_t_ {
|
||
|
||
} f_calculator_t;
|
||
|
||
#define CALCULATOR_DISP_BTN_ITEM_CNT ((int)(sizeof(tbl_calculator_disp_btn_item) / sizeof(tbl_calculator_disp_btn_item[0])))
|
||
#define CALCULATOR_DISP_NUMBER_MAX 32 //显示最大位数
|
||
|
||
#define CAL_FIRST_LINE_Y 49 //计算器按钮首行 中心点y的高度
|
||
#define CAL_FIRST_LINE_X 16 //计算器按钮首行 中心点x的水平距离
|
||
#define CAL_ITEM_OFF_Y 24 //按钮之间的高度间隔
|
||
#define CAL_ITEM_OFF_X 32 //按钮之间的宽度间隔
|
||
|
||
typedef struct calculator_disp_btn_item_t_ {
|
||
u32 res_addr;
|
||
u16 btn_id;
|
||
s16 x;
|
||
s16 y;
|
||
} calculator_disp_btn_item_t;
|
||
|
||
//按钮item,创建时遍历一下
|
||
static const calculator_disp_btn_item_t tbl_calculator_disp_btn_item[] = {
|
||
{UI_BUF_CALCULATOR_0_CLICK_BIN, BTN_0, CAL_ITEM_OFF_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 4},
|
||
{UI_BUF_CALCULATOR_POINT_CLICK_BIN, BTN_POINT, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *2, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 4},
|
||
{UI_BUF_CALCULATOR_EQUAL_CLICK_BIN, BTN_EQUAL, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *3, 110+23},
|
||
|
||
{UI_BUF_CALCULATOR_7_CLICK_BIN, BTN_7, CAL_FIRST_LINE_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 3},
|
||
{UI_BUF_CALCULATOR_8_CLICK_BIN, BTN_8, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 3},
|
||
{UI_BUF_CALCULATOR_9_CLICK_BIN, BTN_9, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *2, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 3},
|
||
|
||
{UI_BUF_CALCULATOR_4_CLICK_BIN, BTN_4, CAL_FIRST_LINE_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 2},
|
||
{UI_BUF_CALCULATOR_5_CLICK_BIN, BTN_5, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 2},
|
||
{UI_BUF_CALCULATOR_6_CLICK_BIN, BTN_6, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *2, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 2},
|
||
{UI_BUF_CALCULATOR_ADD_CLICK_BIN, BTN_ADD, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *3, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 2},
|
||
|
||
{UI_BUF_CALCULATOR_1_CLICK_BIN, BTN_1, CAL_FIRST_LINE_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y},
|
||
{UI_BUF_CALCULATOR_2_CLICK_BIN, BTN_2, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y},
|
||
{UI_BUF_CALCULATOR_3_CLICK_BIN, BTN_3, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *2, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y},
|
||
{UI_BUF_CALCULATOR_REDUCE_CLICK_BIN, BTN_SUB, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *3, CAL_FIRST_LINE_Y+ CAL_ITEM_OFF_Y},
|
||
|
||
// {UI_BUF_CALCULATOR_MINUS_CLICK_BIN, BTN_OPPOSITE, 43, 332},
|
||
// {UI_BUF_CALCULATOR_CE_CLICK_BIN, BTN_CE, 43, 100},
|
||
{UI_BUF_CALCULATOR_C_CLICK_BIN, BTN_C, CAL_FIRST_LINE_X, CAL_FIRST_LINE_Y},
|
||
{UI_BUF_CALCULATOR_DIVIDED_CLICK_BIN, BTN_DIV, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X, CAL_FIRST_LINE_Y},
|
||
{UI_BUF_CALCULATOR_MULTIPLY_CLICK_BIN, BTN_MUL, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *2, CAL_FIRST_LINE_Y},
|
||
{UI_BUF_CALCULATOR_DEL_CLICK_BIN, BTN_DEL, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *3, CAL_FIRST_LINE_Y },
|
||
|
||
};
|
||
|
||
//创建计算器窗体,创建窗体中不要使用功能结构体 func_cb.f_cb
|
||
compo_form_t *func_calculator_form_create(void)
|
||
{
|
||
//新建窗体和背景
|
||
compo_form_t *frm = compo_form_create(true);
|
||
compo_picturebox_t *pic = compo_picturebox_create(frm, UI_BUF_CALCULATOR_BG_BIN);
|
||
compo_picturebox_set_pos(pic, GUI_SCREEN_CENTER_X, 38+59);
|
||
|
||
//创建按钮
|
||
compo_button_t *btn;
|
||
for (u8 idx_btn = 0; idx_btn < CALCULATOR_DISP_BTN_ITEM_CNT; idx_btn++) {
|
||
|
||
if (tbl_calculator_disp_btn_item[idx_btn].res_addr) {
|
||
btn = compo_button_create_by_image(frm, tbl_calculator_disp_btn_item[idx_btn].res_addr);
|
||
compo_button_set_pos(btn, tbl_calculator_disp_btn_item[idx_btn].x, tbl_calculator_disp_btn_item[idx_btn].y);
|
||
} else {
|
||
btn = compo_button_create(frm);
|
||
compo_button_set_location(btn, tbl_calculator_disp_btn_item[idx_btn].x, tbl_calculator_disp_btn_item[idx_btn].y, 30, 22);
|
||
}
|
||
compo_setid(btn, tbl_calculator_disp_btn_item[idx_btn].btn_id);
|
||
compo_button_set_visible(btn, false);
|
||
|
||
}
|
||
|
||
//创建文本
|
||
compo_textbox_t *txt;
|
||
txt = compo_textbox_create(frm, CALCULATOR_DISP_NUMBER_MAX);
|
||
compo_setid(txt, COMPO_ID_NUM_DISP);
|
||
compo_textbox_set_font(txt,UI_BUF_0FONT_FONT_DIN_LIGHT_NUM_15_BIN);
|
||
compo_textbox_set_pos(txt, GUI_SCREEN_CENTER_X, 18);
|
||
compo_textbox_set_autosize(txt, true);
|
||
compo_textbox_set(txt, gcal_get_show_str());
|
||
|
||
return frm;
|
||
}
|
||
|
||
//计算器功能事件处理
|
||
static void func_calculator_process(void)
|
||
{
|
||
func_process();
|
||
}
|
||
|
||
//按钮触摸效果
|
||
static void func_calculator_button_press_handle(void)
|
||
{
|
||
|
||
compo_button_t *btn = compo_getobj_byid(compo_get_button_id());
|
||
if (btn) {
|
||
compo_button_set_visible(btn, true);
|
||
}
|
||
}
|
||
|
||
//按钮释放效果
|
||
static void func_calculator_button_release_handle(void)
|
||
{
|
||
|
||
u16 hold_id = gcal_get_holding_operator();
|
||
component_t *compo = (component_t *)compo_pool_get_top();
|
||
while (compo != NULL) {
|
||
if (compo->type == COMPO_TYPE_BUTTON && compo->id != hold_id) {
|
||
compo_button_t *btn = (compo_button_t *)compo;
|
||
compo_button_set_visible(btn, false);
|
||
}
|
||
compo = compo_get_next(compo);
|
||
}
|
||
}
|
||
|
||
//单击按钮
|
||
static void func_calculator_button_click_handler(void)
|
||
{
|
||
int id = compo_get_button_id();
|
||
compo_textbox_t *txt = compo_getobj_byid(COMPO_ID_NUM_DISP);
|
||
gcalc_btn_click_handler(id);
|
||
compo_textbox_set(txt, gcal_get_show_str());
|
||
|
||
func_calculator_button_release_handle();
|
||
}
|
||
|
||
|
||
//计算器功能消息处理
|
||
static void func_calculator_message(size_msg_t msg)
|
||
{
|
||
switch (msg) {
|
||
case MSG_CTP_TOUCH:
|
||
func_calculator_button_press_handle();
|
||
break;
|
||
|
||
case MSG_CTP_SHORT_UP:
|
||
case MSG_CTP_SHORT_DOWN:
|
||
case MSG_CTP_SHORT_LEFT:
|
||
case MSG_CTP_LONG:
|
||
func_calculator_button_release_handle();
|
||
if (func_cb.flag_sort) {
|
||
func_message(msg);
|
||
}
|
||
break;
|
||
|
||
case MSG_CTP_SHORT_RIGHT:
|
||
func_calculator_button_release_handle();
|
||
func_message(msg);
|
||
break;
|
||
|
||
case MSG_CTP_CLICK:
|
||
func_calculator_button_click_handler();
|
||
break;
|
||
|
||
default:
|
||
func_message(msg);
|
||
break;
|
||
}
|
||
}
|
||
|
||
//进入计算器功能
|
||
static void func_calculator_enter(void)
|
||
{
|
||
func_cb.f_cb = func_zalloc(sizeof(f_calculator_t));
|
||
func_cb.frm_main = func_calculator_form_create();
|
||
|
||
if (gcal_cb_init() == false) {
|
||
halt(HALT_MALLOC);
|
||
}
|
||
}
|
||
|
||
//退出计算器功能
|
||
static void func_calculator_exit(void)
|
||
{
|
||
gcal_cb_destroy();
|
||
}
|
||
|
||
//计算器功能
|
||
void func_calculator(void)
|
||
{
|
||
printf("%s\n", __func__);
|
||
func_calculator_enter();
|
||
while (func_cb.sta == FUNC_CALCULATOR) {
|
||
func_calculator_process();
|
||
func_calculator_message(msg_dequeue());
|
||
}
|
||
func_calculator_exit();
|
||
}
|