[Horus] Model selection screen refactoring
This commit is contained in:
@@ -19,10 +19,9 @@
|
||||
*/
|
||||
|
||||
#include "opentx.h"
|
||||
#include "storage/modelslist.h"
|
||||
|
||||
#define CATEGORIES_WIDTH 140
|
||||
#define MODELCELL_WIDTH 153
|
||||
#define MODELCELL_HEIGHT 61
|
||||
|
||||
enum ModelSelectMode {
|
||||
MODE_SELECT_CATEGORY,
|
||||
@@ -31,9 +30,11 @@ enum ModelSelectMode {
|
||||
};
|
||||
|
||||
uint8_t selectMode;
|
||||
uint8_t currentCategory;
|
||||
char selectedFilename[LEN_MODEL_FILENAME+1];
|
||||
char selectedCategory[LEN_MODEL_FILENAME+1];
|
||||
ModelsList modelslist;
|
||||
|
||||
ModelsCategory * currentCategory;
|
||||
int currentCategoryIndex;
|
||||
ModelCell * currentModel;
|
||||
|
||||
void drawCategory(coord_t y, const char * name, bool selected)
|
||||
{
|
||||
@@ -47,50 +48,46 @@ void drawCategory(coord_t y, const char * name, bool selected)
|
||||
lcdDrawText(MENUS_MARGIN_LEFT, y, name, MENU_TITLE_COLOR);
|
||||
}
|
||||
|
||||
void drawModel(coord_t x, coord_t y, const char * name, bool selected, bool current)
|
||||
void drawModel(coord_t x, coord_t y, ModelCell * model, bool current, bool selected)
|
||||
{
|
||||
ModelHeader header;
|
||||
const char * error = readModel(name, (uint8_t *)&header, sizeof(header));
|
||||
if (error) {
|
||||
lcdDrawText(x+5, y+2, "(Invalid Model)", TEXT_COLOR);
|
||||
lcdDrawBitmapPattern(x+5, y+23, LBM_LIBRARY_SLOT, TEXT_COLOR);
|
||||
}
|
||||
else {
|
||||
lcdDrawSizedText(x+5, y+2, header.name, LEN_MODEL_NAME, ZCHAR|TEXT_COLOR);
|
||||
putsTimer(x+104, y+41, 0, TEXT_COLOR|LEFT);
|
||||
for (int i=0; i<4; i++) {
|
||||
lcdDrawBitmapPattern(x+104+i*11, y+25, LBM_SCORE0, TITLE_BGCOLOR);
|
||||
}
|
||||
GET_FILENAME(filename, BITMAPS_PATH, header.bitmap, BITMAPS_EXT);
|
||||
const BitmapBuffer * bitmap = BitmapBuffer::load(filename);
|
||||
if (bitmap) {
|
||||
lcd->drawScaledBitmap(bitmap, x+5, y+24, 64, 32);
|
||||
delete bitmap;
|
||||
}
|
||||
else {
|
||||
lcdDrawBitmapPattern(x+5, y+23, LBM_LIBRARY_SLOT, TEXT_COLOR);
|
||||
}
|
||||
}
|
||||
lcdDrawSolidHorizontalLine(x+5, y+19, 143, LINE_COLOR);
|
||||
lcd->drawBitmap(x+1, y+1, &model->buffer);
|
||||
if (current) {
|
||||
lcdDrawBitmapPattern(x+66, y+42, LBM_ACTIVE_MODEL, TITLE_BGCOLOR);
|
||||
lcd->drawBitmapPattern(x+66, y+43, LBM_ACTIVE_MODEL, TITLE_BGCOLOR);
|
||||
}
|
||||
if (selected) {
|
||||
lcdDrawSolidRect(x, y, MODELCELL_WIDTH, MODELCELL_HEIGHT, 1, TITLE_BGCOLOR);
|
||||
drawShadow(x, y, MODELCELL_WIDTH, MODELCELL_HEIGHT);
|
||||
lcdDrawSolidRect(x, y, MODELCELL_WIDTH+2, MODELCELL_HEIGHT+2, 1, TITLE_BGCOLOR);
|
||||
drawShadow(x, y, MODELCELL_WIDTH+2, MODELCELL_HEIGHT+2);
|
||||
}
|
||||
}
|
||||
|
||||
#define MODEL_INDEX() (menuVerticalPosition*2+menuHorizontalPosition)
|
||||
|
||||
void setCurrentCategory(unsigned int index)
|
||||
{
|
||||
currentCategoryIndex = index;
|
||||
std::list<ModelsCategory *>::iterator it = modelslist.categories.begin();
|
||||
std::advance(it, index);
|
||||
currentCategory = *it;
|
||||
}
|
||||
|
||||
void setCurrentModel(unsigned int index)
|
||||
{
|
||||
std::list<ModelCell *>::iterator it = currentCategory->begin();
|
||||
std::advance(it, MODEL_INDEX());
|
||||
currentModel = *it;
|
||||
}
|
||||
|
||||
void onCategorySelectMenu(const char * result)
|
||||
{
|
||||
if (result == STR_CREATE_MODEL) {
|
||||
storageCheck(true);
|
||||
createModel(currentCategory);
|
||||
currentModel = modelslist.addModel(currentCategory, createModel());
|
||||
selectMode = MODE_SELECT_MODEL;
|
||||
menuVerticalPosition = 255;
|
||||
menuVerticalPosition = currentCategory->size() - 1;
|
||||
}
|
||||
else if (result == STR_CREATE_CATEGORY) {
|
||||
storageInsertCategory("Category", -1);
|
||||
currentCategory = modelslist.createCategory();
|
||||
menuVerticalPosition = currentCategoryIndex = modelslist.categories.size() - 1;
|
||||
}
|
||||
else if (result == STR_RENAME_CATEGORY) {
|
||||
selectMode = MODE_RENAME_CATEGORY;
|
||||
@@ -98,14 +95,15 @@ void onCategorySelectMenu(const char * result)
|
||||
editNameCursorPos = 0;
|
||||
}
|
||||
else if (result == STR_DELETE_CATEGORY) {
|
||||
storageRemoveCategory(currentCategory--);
|
||||
modelslist.removeCategory(currentCategory);
|
||||
setCurrentCategory(currentCategoryIndex-1);
|
||||
}
|
||||
}
|
||||
|
||||
void onModelSelectMenu(const char * result)
|
||||
{
|
||||
if (result == STR_SELECT_MODEL) {
|
||||
memcpy(g_eeGeneral.currModelFilename, selectedFilename, LEN_MODEL_FILENAME);
|
||||
memcpy(g_eeGeneral.currModelFilename, currentModel->name, LEN_MODEL_FILENAME);
|
||||
storageDirty(EE_GENERAL);
|
||||
storageCheck(true);
|
||||
loadModel(g_eeGeneral.currModelFilename);
|
||||
@@ -113,35 +111,39 @@ void onModelSelectMenu(const char * result)
|
||||
}
|
||||
else if (result == STR_DELETE_MODEL) {
|
||||
POPUP_CONFIRMATION(STR_DELETEMODEL);
|
||||
SET_WARNING_INFO(selectedFilename, LEN_MODEL_FILENAME, 0);
|
||||
SET_WARNING_INFO(currentModel->name, LEN_MODEL_FILENAME, 0);
|
||||
}
|
||||
else if (result == STR_CHANGE_CATEGORY) {
|
||||
|
||||
}
|
||||
else if (result == STR_DUPLICATE_MODEL) {
|
||||
char duplicatedFilename[LEN_MODEL_FILENAME+1];
|
||||
memcpy(duplicatedFilename, selectedFilename, sizeof(duplicatedFilename));
|
||||
memcpy(duplicatedFilename, currentModel->name, sizeof(duplicatedFilename));
|
||||
if (findNextFileIndex(duplicatedFilename, MODELS_PATH)) {
|
||||
sdCopyFile(selectedFilename, MODELS_PATH, duplicatedFilename, MODELS_PATH);
|
||||
storageInsertModel(duplicatedFilename, currentCategory, -1);
|
||||
sdCopyFile(currentModel->name, MODELS_PATH, duplicatedFilename, MODELS_PATH);
|
||||
modelslist.addModel(currentCategory, duplicatedFilename);
|
||||
menuVerticalPosition = currentCategory->size() - 1;
|
||||
setCurrentModel(menuVerticalPosition);
|
||||
}
|
||||
else {
|
||||
POPUP_WARNING("Invalid Filename");
|
||||
POPUP_WARNING("Invalid File");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define MODEL_INDEX() (menuVerticalPosition*2+menuHorizontalPosition)
|
||||
|
||||
bool menuModelSelect(evt_t event)
|
||||
{
|
||||
if (warningResult) {
|
||||
warningResult = 0;
|
||||
int modelIndex = MODEL_INDEX();
|
||||
storageRemoveModel(currentCategory, modelIndex);
|
||||
modelslist.removeModel(currentCategory, currentModel);
|
||||
s_copyMode = 0;
|
||||
event = EVT_REFRESH;
|
||||
if (modelIndex > 0) {
|
||||
modelIndex--;
|
||||
menuVerticalPosition = modelIndex / 2;
|
||||
menuHorizontalPosition = modelIndex & 1;
|
||||
setCurrentModel(modelIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,7 +154,10 @@ bool menuModelSelect(evt_t event)
|
||||
|
||||
case EVT_ENTRY:
|
||||
selectMode = MODE_SELECT_CATEGORY;
|
||||
currentCategory = menuVerticalPosition = 0;
|
||||
modelslist.load();
|
||||
menuVerticalPosition = 0;
|
||||
setCurrentCategory(0);
|
||||
setCurrentModel(0);
|
||||
break;
|
||||
|
||||
case EVT_KEY_FIRST(KEY_EXIT):
|
||||
@@ -162,7 +167,7 @@ bool menuModelSelect(evt_t event)
|
||||
return false;
|
||||
case MODE_SELECT_MODEL:
|
||||
selectMode = MODE_SELECT_CATEGORY;
|
||||
menuVerticalPosition = currentCategory;
|
||||
menuVerticalPosition = currentCategoryIndex;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
@@ -173,6 +178,7 @@ bool menuModelSelect(evt_t event)
|
||||
case MODE_SELECT_CATEGORY:
|
||||
selectMode = MODE_SELECT_MODEL;
|
||||
menuVerticalPosition = 0;
|
||||
setCurrentModel(menuVerticalPosition);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
@@ -183,7 +189,7 @@ bool menuModelSelect(evt_t event)
|
||||
POPUP_MENU_ADD_ITEM(STR_CREATE_MODEL);
|
||||
POPUP_MENU_ADD_ITEM(STR_CREATE_CATEGORY);
|
||||
POPUP_MENU_ADD_ITEM(STR_RENAME_CATEGORY);
|
||||
if (currentCategory > 0) {
|
||||
if (currentCategoryIndex > 0) {
|
||||
POPUP_MENU_ADD_ITEM(STR_DELETE_CATEGORY);
|
||||
}
|
||||
popupMenuHandler = onCategorySelectMenu;
|
||||
@@ -191,15 +197,16 @@ bool menuModelSelect(evt_t event)
|
||||
else if (selectMode == MODE_SELECT_MODEL) {
|
||||
killEvents(event);
|
||||
ModelHeader header;
|
||||
const char * error = readModel(selectedFilename, (uint8_t *)&header, sizeof(header));
|
||||
const char * error = readModel(currentModel->name, (uint8_t *)&header, sizeof(header));
|
||||
if (!error) {
|
||||
if (strncmp(selectedFilename, g_eeGeneral.currModelFilename, LEN_MODEL_FILENAME) != 0) {
|
||||
if (strncmp(currentModel->name, g_eeGeneral.currModelFilename, LEN_MODEL_FILENAME) != 0) {
|
||||
POPUP_MENU_ADD_ITEM(STR_SELECT_MODEL);
|
||||
}
|
||||
POPUP_MENU_ADD_ITEM(STR_DUPLICATE_MODEL);
|
||||
}
|
||||
// POPUP_MENU_ADD_SD_ITEM(STR_BACKUP_MODEL);
|
||||
// POPUP_MENU_ADD_ITEM(STR_MOVE_MODEL);
|
||||
POPUP_MENU_ADD_ITEM(STR_CHANGE_CATEGORY);
|
||||
POPUP_MENU_ADD_ITEM(STR_DELETE_MODEL);
|
||||
// POPUP_MENU_ADD_ITEM(STR_CREATE_MODEL);
|
||||
// POPUP_MENU_ADD_ITEM(STR_RESTORE_MODEL);
|
||||
@@ -219,85 +226,63 @@ bool menuModelSelect(evt_t event)
|
||||
lcdDrawSolidFilledRect(0, MENU_FOOTER_TOP, LCD_W, MENU_FOOTER_HEIGHT, HEADER_BGCOLOR);
|
||||
|
||||
// Categories
|
||||
StorageModelsList storage;
|
||||
const char * error = storageOpenModelsList(&storage);
|
||||
if (!error) {
|
||||
bool result = true;
|
||||
coord_t y = MENU_HEADER_HEIGHT+10;
|
||||
int index = 0;
|
||||
while (1) {
|
||||
char line[LEN_MODEL_FILENAME+1];
|
||||
result = storageReadNextCategory(&storage, line, LEN_MODEL_FILENAME);
|
||||
if (!result)
|
||||
break;
|
||||
if (y < LCD_H) {
|
||||
if (selectMode == MODE_RENAME_CATEGORY && currentCategory == index) {
|
||||
lcdDrawSolidFilledRect(0, y-INVERT_VERT_MARGIN, CATEGORIES_WIDTH, INVERT_LINE_HEIGHT, TEXT_BGCOLOR);
|
||||
editName(MENUS_MARGIN_LEFT, y, selectedCategory, LEN_MODEL_FILENAME, event, 1, 0);
|
||||
if (s_editMode == 0 || event == EVT_KEY_BREAK(KEY_EXIT)) {
|
||||
storageRenameCategory(currentCategory, selectedCategory);
|
||||
selectMode = MODE_SELECT_CATEGORY;
|
||||
putEvent(EVT_REFRESH);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (currentCategory == index) {
|
||||
memset(selectedCategory, 0, sizeof(selectedCategory));
|
||||
strncpy(selectedCategory, line, sizeof(selectedCategory));
|
||||
}
|
||||
drawCategory(y, line, currentCategory==index);
|
||||
coord_t y = MENU_HEADER_HEIGHT+4;
|
||||
for (std::list<ModelsCategory *>::iterator it = modelslist.categories.begin(); it != modelslist.categories.end(); ++it, y+=FH) {
|
||||
if (y < MENU_FOOTER_TOP - 5) {
|
||||
if (selectMode == MODE_RENAME_CATEGORY && currentCategory == *it) {
|
||||
lcdDrawSolidFilledRect(0, y-INVERT_VERT_MARGIN, CATEGORIES_WIDTH, INVERT_LINE_HEIGHT, TEXT_BGCOLOR);
|
||||
editName(MENUS_MARGIN_LEFT, y, currentCategory->name, LEN_MODEL_FILENAME, event, 1, 0);
|
||||
if (s_editMode == 0 || event == EVT_KEY_BREAK(KEY_EXIT)) {
|
||||
// storageRenameCategory(currentCategory, selectedCategory);
|
||||
selectMode = MODE_SELECT_CATEGORY;
|
||||
putEvent(EVT_REFRESH);
|
||||
}
|
||||
}
|
||||
y += FH;
|
||||
index++;
|
||||
else {
|
||||
drawCategory(y, (*it)->name, currentCategory==*it);
|
||||
}
|
||||
}
|
||||
if (selectMode == MODE_SELECT_CATEGORY) {
|
||||
if (navigate(event, index, 9)) {
|
||||
putEvent(EVT_REFRESH);
|
||||
currentCategory = menuVerticalPosition;
|
||||
}
|
||||
}
|
||||
|
||||
if (selectMode == MODE_SELECT_CATEGORY) {
|
||||
if (navigate(event, modelslist.categories.size(), 9)) {
|
||||
putEvent(EVT_REFRESH);
|
||||
setCurrentCategory(menuVerticalPosition);
|
||||
}
|
||||
}
|
||||
|
||||
// Models
|
||||
if (!error) {
|
||||
bool result = storageSeekCategory(&storage, currentCategory);
|
||||
coord_t y = MENU_HEADER_HEIGHT+7;
|
||||
int count = 0;
|
||||
while (result) {
|
||||
char line[LEN_MODEL_FILENAME+1];
|
||||
result = storageReadNextModel(&storage, line, LEN_MODEL_FILENAME);
|
||||
if (!result) {
|
||||
break;
|
||||
int index = 0;
|
||||
y = MENU_HEADER_HEIGHT+7;
|
||||
for (ModelsCategory::iterator it = currentCategory->begin(); it != currentCategory->end(); ++it, ++index) {
|
||||
if (index >= menuVerticalOffset*2 && index < (menuVerticalOffset+3)*2) {
|
||||
bool selected = (selectMode==MODE_SELECT_MODEL && index==menuVerticalPosition*2+menuHorizontalPosition);
|
||||
bool current = !strncmp((*it)->name, g_eeGeneral.currModelFilename, LEN_MODEL_FILENAME);
|
||||
if (index & 1) {
|
||||
drawModel(CATEGORIES_WIDTH+MENUS_MARGIN_LEFT+162, y, *it, current, selected);
|
||||
y += 66;
|
||||
}
|
||||
if (count >= menuVerticalOffset*2 && count < (menuVerticalOffset+3)*2) {
|
||||
bool selected = (selectMode==MODE_SELECT_MODEL && menuVerticalPosition*2+menuHorizontalPosition==count);
|
||||
bool current = (strncmp(line, g_eeGeneral.currModelFilename, LEN_MODEL_FILENAME) == 0);
|
||||
if (count & 1) {
|
||||
drawModel(CATEGORIES_WIDTH+MENUS_MARGIN_LEFT+162, y, line, selected, current);
|
||||
y += 66;
|
||||
}
|
||||
else {
|
||||
drawModel(CATEGORIES_WIDTH+MENUS_MARGIN_LEFT+1, y, line, selected, current);
|
||||
}
|
||||
if (selected) {
|
||||
memcpy(selectedFilename, line, sizeof(selectedFilename));
|
||||
}
|
||||
}
|
||||
count++;
|
||||
}
|
||||
if (selectMode == MODE_SELECT_MODEL) {
|
||||
if (count == 0) {
|
||||
selectMode = MODE_SELECT_CATEGORY;
|
||||
menuVerticalPosition = currentCategory;
|
||||
putEvent(EVT_REFRESH);
|
||||
}
|
||||
else if (navigate(event, count, 3, 2)) {
|
||||
putEvent(EVT_REFRESH);
|
||||
else {
|
||||
drawModel(CATEGORIES_WIDTH+MENUS_MARGIN_LEFT+1, y, *it, current, selected);
|
||||
}
|
||||
}
|
||||
drawVerticalScrollbar(DEFAULT_SCROLLBAR_X, MENU_HEADER_HEIGHT+7, MENU_FOOTER_TOP-MENU_HEADER_HEIGHT-15, menuVerticalOffset, (count+1)/2, 3);
|
||||
}
|
||||
if (selectMode == MODE_SELECT_MODEL) {
|
||||
if (index == 0) {
|
||||
selectMode = MODE_SELECT_CATEGORY;
|
||||
menuVerticalPosition = currentCategoryIndex;
|
||||
putEvent(EVT_REFRESH);
|
||||
}
|
||||
else if (navigate(event, index, 3, 2)) {
|
||||
putEvent(EVT_REFRESH);
|
||||
setCurrentModel(MODEL_INDEX());
|
||||
}
|
||||
if (currentModel) {
|
||||
lcd->drawText(CATEGORIES_WIDTH+MENUS_MARGIN_LEFT, MENU_FOOTER_TOP+2, currentModel->name, TEXT_INVERTED_COLOR);
|
||||
}
|
||||
}
|
||||
|
||||
drawVerticalScrollbar(DEFAULT_SCROLLBAR_X, MENU_HEADER_HEIGHT+7, MENU_FOOTER_TOP-MENU_HEADER_HEIGHT-15, menuVerticalOffset, (index+1)/2, 3);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user