123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- #pragma once
- #include <map>
- #if 1
- #include <functional>
- #else
- #include <boost/function.hpp>
- #endif
- #include "control/ZKListView.h"
- #include "entry/EasyUIContext.h"
- #include "utils/Log.h"
- struct TextExtent {
- int w;
- int h;
- };
- TextExtent GetTextExtent(ZKTextView* view, const std::string& str);
- std::string EllipsisText(ZKTextView* view, const std::string& str);
- class Field : protected IMEContext::IIMETextUpdateListener{
- public:
- typedef std::map<int, ZKListView::ZKListSubItem*> ItemMap;
- enum Names {
- ICON = 100,
- ONLY_TITLE,
- TITLE,
- VALUE,
- ARRAW,
- SWITCH,
- DIVIDER,
- PRIMARY_BUTTON,
- };
- typedef std::function<std::string (Field* f)> TextGetter;
- typedef std::function<bool (Field* f)> SwitchGetter;
- friend class Form;
- Field() {
- arraw_ = true;
- switch_ = false;
- divider_ = true;
- }
- virtual ~Field() {}
- Field& Title(const char* title) {
- title_ = title;
- return self();
- }
- Field& Title(const TextGetter& title_get) {
- title_get_ = title_get;
- return self();
- }
- Field& Icon(const std::string& icon) {
- icon_ = icon;
- return self();
- }
- Field& Arraw(bool arraw) {
- arraw_ = arraw;
- if (arraw) {
- switch_ = false;
- }
- return self();
- }
- Field& Divider(bool divider) {
- divider_ = divider;
- return self();
- }
- Field& Switch(const SwitchGetter& get) {
- switch_ = true;
- arraw_ = false;
- switch_get_ = get;
- return self();
- }
- Field& Text(const TextGetter& get) {
- get_ = get;
- return self();
- }
- std::string Text() {
- if (get_ == NULL) {
- return "";
- }
- return get_(this);
- }
- typedef std::function<void (Field* f)> ClickCallback;
- Field& Click(const ClickCallback& callback) {
- click_callback_ = callback;
- return self();
- }
- typedef std::function<void (Field* f, const std::string& str)> EditCallback;
- Field& Edit(const EditCallback& callback) {
- edit_callback_ = callback;
- return self();
- }
- protected:
- Field& self() {
- return *dynamic_cast<Field*>(this);
- }
- void Click() {
- if (click_callback_ != NULL) {
- click_callback_(&self());
- return;
- }
- if (edit_callback_ != NULL) {
- #if 1
- ime_info.text = get_ == NULL ? "" : get_(this);
- #else
- ime_info.text = "";
- #endif
- ime_info.isPassword = false;
- ime_info.imeTextType = IMEContext::E_IME_TEXT_TYPE_ALL;
- EASYUICONTEXT->showIME(&ime_info, this);
- }
- }
- virtual void onIMETextUpdate(const std::string &text) {
- if (edit_callback_ == NULL) {
- return;
- }
- edit_callback_(this, text);
- }
- virtual void Paint(ZKListView* list_view, ZKListView::ZKListItem *item,
- int index, ItemMap& item_map) {
- if (item_map[ICON] == nullptr) {
- LOGE("ICON item must not be null");
- return;
- }
- if (item_map[TITLE] == nullptr) {
- LOGE("TITLE item must not be null");
- return;
- }
- if (item_map[ONLY_TITLE] == nullptr) {
- LOGE("ONLY_TITLE item must not be null");
- return;
- }
- if (item_map[VALUE] == nullptr) {
- LOGE("VALUE item must not be null");
- return;
- }
- if (item_map[ARRAW] == nullptr) {
- LOGE("ARRAW item must not be null");
- return;
- }
- if (item_map[SWITCH] == nullptr) {
- LOGE("SWITCH item must not be null");
- return;
- }
- item_map[ICON]->setBackgroundPic(icon_.c_str());
- std::string title_str = title_;
- if (title_get_ != NULL) {
- title_str = title_get_(this);
- }
- item_map[ONLY_TITLE]->setText(icon_.empty() ? title_str : "");
- item_map[TITLE]->setText(
- EllipsisText(item_map[TITLE], icon_.empty() ? "" : title_str));
- std::string str = get_ == NULL ? "" : get_(this);
- if (arraw_ & !str.empty()) {
- str += " ";//箭头和文字同时存在,需要为箭头空出距离
- }
- item_map[VALUE]->setText(EllipsisText(item_map[VALUE], str));
- item_map[ARRAW]->setBackgroundPic(arraw_ ? "ic_arraw_right.png" : "");
- bool is_on = switch_get_ == NULL ? false : switch_get_(this);
- item_map[SWITCH]->setBackgroundPic(switch_ ?
- (is_on ? "settings/on_2.png" : "settings/off_2.png") : "");
- if (item_map[DIVIDER] != nullptr) {
- item_map[DIVIDER]->setAlpha(
- (divider_ && (index != list_view->getListItemCount() -1))
- ? 0 : 255);
- // item_map[DIVIDER]->setBackgroundColor(
- // (divider_ && (index != list_view->getListItemCount() -1))
- // ? 0xff2E3035 : 0);
- }
- }
- protected:
- std::string icon_;
- std::string title_;
- bool arraw_;
- bool divider_;
- bool switch_;
- ClickCallback click_callback_;
- EditCallback edit_callback_;
- IMEContext::SIMETextInfo ime_info;
- TextGetter get_;
- TextGetter title_get_;
- SwitchGetter switch_get_;
- friend class UIForm;
- };
- class FieldFactory {
- public:
- static Field& Text() {
- Field* field = new Field();
- return *field;
- }
- static Field& Placeholder() {
- Field* field = new Field();
- field->Arraw(false);
- field->Divider(false);
- return *field;
- }
- static Field& Text(const std::string& title) {
- Field* field = new Field();
- field->Title(title.c_str());
- return *field;
- }
- };
|