Bind
C++ UI framework for Arduino
 
Loading...
Searching...
No Matches
BindDialog.hpp
1#ifndef __BINDDIALOG_HPP
2#define __BINDDIALOG_HPP
3#include "BindView.hpp"
4#include "BindUtils.hpp"
5#define DIALOG_TEXT_BUFFER_SIZE 64
6#if defined(__AVR__)
7#include <avr/pgmspace.h>
8const char DEFAULT_TITLE[] PROGMEM = "Confirmation";
9const char DEFAULT_MESSAGE[] PROGMEM = "Are you sure?";
10const char DEFAULT_OK[] PROGMEM = "OK";
11const char DEFAULT_CANCEL[] PROGMEM = "Cancel";
12#else
13const char DEFAULT_TITLE[] = "Confirmation";
14const char DEFAULT_MESSAGE[] = "Are you sure?";
15const char DEFAULT_OK[] = "OK";
16const char DEFAULT_CANCEL[] = "Cancel";
17#endif
26enum DialogType {
27 TEXT_INPUT,
28 PASSWORD_INPUT,
29 NO_INPUT_TEXT,
30};
31
49class BindDialog : public BindView
50{
51public:
52
53 BindDialog(DialogType type = NO_INPUT_TEXT)
54 : dialogType(type), title(nullptr), message(nullptr), primaryButton("OK"), secondaryButton("Cancel")
55 {
56 this->tag = tagIndex++;
57 #if defined(__AVR__)
58 char buffer[16];
59 strcpy_P(buffer, DEFAULT_TITLE);
60 setTitle(buffer);
61 strcpy_P(buffer, DEFAULT_MESSAGE);
62 setMessage(buffer);
63 strcpy_P(buffer, DEFAULT_OK);
64 setPrimaryButton(buffer);
65 strcpy_P(buffer, DEFAULT_CANCEL);
66 setSecondaryButton(buffer);
67 #else
68 setTitle(DEFAULT_TITLE);
69 setMessage(DEFAULT_MESSAGE);
70 setPrimaryButton(DEFAULT_OK);
71 setSecondaryButton(DEFAULT_CANCEL);
72 #endif
73 }
74
80 void setTitle(const char *cstr)
81 {
82 title = cstr;
83 }
84
90 void setMessage(const char *cstr)
91 {
92 message = cstr;
93 }
94
100 void setPrimaryButton(const char *cstr)
101 {
102 primaryButton = cstr;
103 }
104
110 void setSecondaryButton(const char *cstr)
111 {
112 secondaryButton = cstr;
113 }
114
129 void setCallback(void (*callback)(bool))
130 {
131 this->callback.simple = callback;
132 isTextCallback = false;
133 }
134
151 void setCallback(void (*callback)(bool, const char *))
152 {
153 this->callback.withText = callback;
154 isTextCallback = true;
155 }
156
157 void invokeCallback(bool result, const char *text)
158 {
159 this->hasResult = true;
160 this->accepted = result;
161
162 if ((dialogType == TEXT_INPUT || dialogType == PASSWORD_INPUT) && isTextCallback){
163 setTextResult(text, strlen(text));
164 if (callback.withText != nullptr)
165 {
166 callback.withText(result, text);
167 return;
168 }
169 }else{
170 if (callback.simple != nullptr)
171 {
172 callback.simple(result);
173 }
174 }
175
176 }
177
178 void invokeCallback(bool result)
179 {
180 this->hasResult = true;
181 this->accepted = result;
182 if (dialogType == NO_INPUT_TEXT && callback.simple != nullptr){
183 callback.simple(result);
184 }
185 }
186
195 const char* getResultText() const {
196 return resultTextBuffer; // Always return the buffer
197 }
198
199 uint16_t getBytes(uint8_t *out) override
200 {
201 offset = 0;
202 uint8_t dialogTypeU = (uint8_t)dialogType;
203 copyAndOffset(out, &offset, &objID, sizeof(objID));
204 copyAndOffset(out, &offset, &tag, sizeof(tag));
205 copyAndOffset(out, &offset, &cmdId, sizeof(cmdId));
206 copyAndOffset(out, &offset, &dialogTypeU, sizeof(dialogTypeU));
207 copyAndOffset(out, &offset, &singleButton, sizeof(singleButton));
208
209 copyStringWithLength(out, &offset, title);
210 copyStringWithLength(out, &offset, message);
211 copyStringWithLength(out, &offset, primaryButton);
212 copyStringWithLength(out, &offset, secondaryButton);
213
214 return offset;
215 }
216
217 uint8_t cmdId = 0;
218 bool accepted = false;
219 bool hasResult = false;
220 bool singleButton = false;
221
222private:
223 char resultTextBuffer[DIALOG_TEXT_BUFFER_SIZE];
224 uint8_t objID = BIND_ID_DIALOG;
225 uint16_t offset = 0;
226 int strLength = 0;
227 const char *title;
228 const char *message;
229 const char *primaryButton = "OK";
230 const char *secondaryButton = "Cancel";
231 DialogType dialogType;
232 static int16_t tagIndex;
233 typedef union {
234 void (*simple)(bool);
235 void (*withText)(bool, const char *);
236 } DialogCallback;
237 DialogCallback callback;
238 bool isTextCallback = false;
239
240 void setTextResult(const char *cstr, int length) {
241 length = min(length, DIALOG_TEXT_BUFFER_SIZE - 1);
242 strncpy(resultTextBuffer, cstr, length);
243 resultTextBuffer[length] = '\0';
244 }
245};
246
247#endif // __BINDDIALOG_HPP
BindDialog class represents a dialog box for use with BindCanvas app.
Definition BindDialog.hpp:50
bool singleButton
Flag to indicate if the dialog has a single button.
Definition BindDialog.hpp:220
void setMessage(const char *cstr)
Set the message text for the dialog.
Definition BindDialog.hpp:90
void setCallback(void(*callback)(bool, const char *))
Set the callback function for the dialog with text input.
Definition BindDialog.hpp:151
void setTitle(const char *cstr)
Set the title text for the dialog.
Definition BindDialog.hpp:80
const char * getResultText() const
Get the result text entered by the user.
Definition BindDialog.hpp:195
uint16_t getBytes(uint8_t *out) override
Retrieves the bytes representing the BindView for synchronization.
Definition BindDialog.hpp:199
bool accepted
Result of the user interaction with the dialog.
Definition BindDialog.hpp:218
void setSecondaryButton(const char *cstr)
Set the secondary button text (e.g. "Cancel") for the dialog.
Definition BindDialog.hpp:110
void setPrimaryButton(const char *cstr)
Set the primary button text (e.g. "OK") for the dialog.
Definition BindDialog.hpp:100
uint8_t cmdId
Command ID for the dialog. See the notes for possible cmdId values.
Definition BindDialog.hpp:217
bool hasResult
Flag to indicate if the dialog has a result. The resets to false after each sync.
Definition BindDialog.hpp:219
void setCallback(void(*callback)(bool))
Set the callback function for the dialog.
Definition BindDialog.hpp:129
Definition BindView.hpp:26