From 1052e6a4cbab358df041dd307f5b04b308572afc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez?= Date: Wed, 31 Oct 2018 14:36:36 +0100 Subject: [PATCH] GuiListViewEx return focused element. Changing how the active elements draws when you are not editing the listview. --- examples/controls_review/controls_review.c | 4 +- src/raygui.h | 65 ++++++++++++++++------ 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/examples/controls_review/controls_review.c b/examples/controls_review/controls_review.c index 6a6a0e0..f94398e 100644 --- a/examples/controls_review/controls_review.c +++ b/examples/controls_review/controls_review.c @@ -56,6 +56,7 @@ int main() int listViewExScrollIndex = 0; int listViewExActive = -1; + int listViewExFocus = -1; const char *listViewExList[8] = { "This", "is", "a", "list view", "with", "disable", "elements", "amazing!" }; int listViewExElementsEnable[8] = {1, 0, 1, 1, 0, 0, 1}; bool listViewExEditMode = false; @@ -103,7 +104,8 @@ int main() // Second GUI column if (GuiListView((Rectangle){ 185, 25, 120, 100 }, listViewList, 6, &listViewScrollIndex, &listViewActive, listViewEditMode)) listViewEditMode = !listViewEditMode; - if (GuiListViewEx((Rectangle){ 185, 155, 120, 200 }, listViewExList, listViewExElementsEnable, 8, &listViewExScrollIndex, &listViewExActive, listViewExEditMode)) listViewExEditMode = !listViewExEditMode; + if (GuiListViewEx((Rectangle){ 185, 155, 120, 200 }, listViewExList, listViewExElementsEnable, 8, &listViewExScrollIndex, &listViewExActive, &listViewExFocus, listViewExEditMode)) listViewExEditMode = !listViewExEditMode; + if (listViewExFocus >= 0 && listViewExFocus < 8) DrawText(listViewExList[listViewExFocus], 195, 370, 20, RED); // Third GUI column if (GuiTextBoxMulti((Rectangle){ 325, 25, 225, 175 }, multiTextBoxText, 141, multiTextBoxEditMode)) multiTextBoxEditMode = !multiTextBoxEditMode; diff --git a/src/raygui.h b/src/raygui.h index 185ba7b..6fbd990 100644 --- a/src/raygui.h +++ b/src/raygui.h @@ -409,7 +409,7 @@ RAYGUIDEF void GuiDummyRec(Rectangle bounds, const char *text); // Advance controls set RAYGUIDEF bool GuiListView(Rectangle bounds, const char **text, int count, int*scrollIndex, int *active, bool editMode); // List View control, returns selected list element index -RAYGUIDEF bool GuiListViewEx(Rectangle bounds, const char **text, int *enabledElements, int count, int *scrollIndex, int *active, bool editMode); +RAYGUIDEF bool GuiListViewEx(Rectangle bounds, const char **text, int *enabledElements, int count, int *scrollIndex, int *active, int*focus, bool editMode); RAYGUIDEF Color GuiColorPicker(Rectangle bounds, Color color); // Color Picker control RAYGUIDEF bool GuiMessageBox(Rectangle bounds, const char *windowTitle, const char *message); // Message Box control, displays a message @@ -2870,13 +2870,20 @@ static bool GuiListElement(Rectangle bounds, const char *text, bool active, bool } break; case PRESSED: { - DrawRectangle(bounds.x, bounds.y, bounds.width, bounds.height, Fade(GetColor(style[LISTVIEW_BASE_COLOR_PRESSED]), guiAlpha)); - DrawRectangleLinesEx(bounds, LISTELEMENT_BORDER_WIDTH, Fade(GetColor(style[LISTVIEW_BORDER_COLOR_PRESSED]), guiAlpha)); - GuiDrawText(text, bounds.x + bounds.width/2 - textWidth/2, bounds.y + bounds.height/2 - textHeight/2, Fade(GetColor(style[LISTVIEW_TEXT_COLOR_PRESSED]), guiAlpha)); + DrawRectangle(bounds.x, bounds.y, bounds.width, bounds.height, Fade(GetColor(style[LISTVIEW_BASE_COLOR_PRESSED]), guiAlpha)); + DrawRectangleLinesEx(bounds, LISTELEMENT_BORDER_WIDTH, Fade(GetColor(style[LISTVIEW_BORDER_COLOR_PRESSED]), guiAlpha)); + GuiDrawText(text, bounds.x + bounds.width/2 - textWidth/2, bounds.y + bounds.height/2 - textHeight/2, Fade(GetColor(style[LISTVIEW_TEXT_COLOR_PRESSED]), guiAlpha)); } break; case DISABLED: { - GuiDrawText(text, bounds.x + bounds.width/2 - textWidth/2, bounds.y + bounds.height/2 - textHeight/2, Fade(GetColor(style[LISTVIEW_TEXT_COLOR_DISABLED]), guiAlpha)); + if (active) + { + DrawRectangle(bounds.x, bounds.y, bounds.width, bounds.height, Fade(GetColor(style[LISTVIEW_BASE_COLOR_DISABLED]), guiAlpha)); + DrawRectangleLinesEx(bounds, LISTELEMENT_BORDER_WIDTH, Fade(GetColor(style[LISTVIEW_BORDER_COLOR_NORMAL]), guiAlpha)); + GuiDrawText(text, bounds.x + bounds.width/2 - textWidth/2, bounds.y + bounds.height/2 - textHeight/2, Fade(GetColor(style[LISTVIEW_TEXT_COLOR_NORMAL]), guiAlpha)); + + } + else GuiDrawText(text, bounds.x + bounds.width/2 - textWidth/2, bounds.y + bounds.height/2 - textHeight/2, Fade(GetColor(style[LISTVIEW_TEXT_COLOR_DISABLED]), guiAlpha)); } break; default: break; } @@ -2888,15 +2895,18 @@ static bool GuiListElement(Rectangle bounds, const char *text, bool active, bool // List View control, returns selected list element index // int *startIndex) RAYGUIDEF bool GuiListView(Rectangle bounds, const char **text, int count, int*scrollIndex, int *active, bool editMode) { - return GuiListViewEx(bounds, text, NULL, count, scrollIndex, active, editMode); + int focusDefault = 0; + return GuiListViewEx(bounds, text, NULL, count, scrollIndex, active, &focusDefault, editMode); } -RAYGUIDEF bool GuiListViewEx(Rectangle bounds, const char **text, int *enabledElements, int count, int *scrollIndex, int *active, bool editMode) +RAYGUIDEF bool GuiListViewEx(Rectangle bounds, const char **text, int *enabledElements, int count, int *scrollIndex, int *active, int*focus, bool editMode) { #define LISTVIEW_LINE_THICK 1 bool usingEnableList = false; if (enabledElements != NULL) usingEnableList = true; + + int focusElement = -1; GuiControlState state = guiState; @@ -2927,6 +2937,15 @@ RAYGUIDEF bool GuiListViewEx(Rectangle bounds, const char **text, int *enabledEl endIndex = count; } + // Calculate position X and width to draw each element. + int posX = bounds.x + style[LISTVIEW_BAR_WIDTH] + style[LISTVIEW_ELEMENTS_PADDING]; + int elementWidth = bounds.width - style[LISTVIEW_BAR_WIDTH] - 2*style[LISTVIEW_ELEMENTS_PADDING] - LISTVIEW_LINE_THICK; + if (!useScrollBar) + { + posX = bounds.x + style[LISTVIEW_ELEMENTS_PADDING]; + elementWidth = bounds.width - 2*style[LISTVIEW_ELEMENTS_PADDING] - LISTVIEW_LINE_THICK; + } + if ((state != DISABLED) && !guiLocked) // && !guiLocked { Vector2 mousePoint = GetMousePosition(); @@ -3004,8 +3023,20 @@ RAYGUIDEF bool GuiListViewEx(Rectangle bounds, const char **text, int *enabledEl } else { - if (!CheckCollisionPointRec(mousePoint, bounds) && IsMouseButtonPressed(0)) pressed = true; + if (!CheckCollisionPointRec(mousePoint, bounds)) + { + if (IsMouseButtonPressed(0) || GetMouseWheelMove() != 0) pressed = true; + } } + + // Get focused element + for (int i = startIndex; i < endIndex; i++) + { + if (CheckCollisionPointRec(mousePoint, (Rectangle){ posX, bounds.y + style[LISTVIEW_ELEMENTS_PADDING] + LISTVIEW_LINE_THICK + (i - startIndex)*(style[LISTVIEW_ELEMENTS_HEIGHT] + style[LISTVIEW_ELEMENTS_PADDING]), elementWidth, style[LISTVIEW_ELEMENTS_HEIGHT] })) + { + focusElement = i; + } + } } // Calculamos el porcentaje de elementos visibles, y aplicamos el mismo porcentaje al tamaƱo de la barra original. @@ -3027,15 +3058,7 @@ RAYGUIDEF bool GuiListViewEx(Rectangle bounds, const char **text, int *enabledEl //-------------------------------------------------------------------- // Draw background - DrawRectangleRec(bounds, GetColor(style[DEFAULT_BACKGROUND_COLOR])); - - int posX = bounds.x + style[LISTVIEW_BAR_WIDTH] + style[LISTVIEW_ELEMENTS_PADDING]; - int elementWidth = bounds.width - style[LISTVIEW_BAR_WIDTH] - 2*style[LISTVIEW_ELEMENTS_PADDING] - LISTVIEW_LINE_THICK; - if (!useScrollBar) - { - posX = bounds.x + style[LISTVIEW_ELEMENTS_PADDING]; - elementWidth = bounds.width - 2*style[LISTVIEW_ELEMENTS_PADDING] - LISTVIEW_LINE_THICK; - } + DrawRectangleRec(bounds, GetColor(style[DEFAULT_BACKGROUND_COLOR])); // Draw scrollBar background if (useScrollBar) DrawRectangle(bounds.x, bounds.y, style[LISTVIEW_BAR_WIDTH], bounds.height, Fade(GetColor(style[DEFAULT_BORDER_COLOR_DISABLED]), guiAlpha)); @@ -3056,7 +3079,12 @@ RAYGUIDEF bool GuiListViewEx(Rectangle bounds, const char **text, int *enabledEl GuiListElement((Rectangle){ posX, bounds.y + style[LISTVIEW_ELEMENTS_PADDING] + LISTVIEW_LINE_THICK + (i - startIndex)*(style[LISTVIEW_ELEMENTS_HEIGHT] + style[LISTVIEW_ELEMENTS_PADDING]), elementWidth, style[LISTVIEW_ELEMENTS_HEIGHT] }, text[i], false, false); GuiEnable(); } - else if (i == auxActive) GuiListElement((Rectangle){ posX, bounds.y + style[LISTVIEW_ELEMENTS_PADDING] + LISTVIEW_LINE_THICK + (i - startIndex)*(style[LISTVIEW_ELEMENTS_HEIGHT] + style[LISTVIEW_ELEMENTS_PADDING]), elementWidth, style[LISTVIEW_ELEMENTS_HEIGHT] }, text[i], true, false); + else if (i == auxActive) + { + GuiDisable(); + GuiListElement((Rectangle){ posX, bounds.y + style[LISTVIEW_ELEMENTS_PADDING] + LISTVIEW_LINE_THICK + (i - startIndex)*(style[LISTVIEW_ELEMENTS_HEIGHT] + style[LISTVIEW_ELEMENTS_PADDING]), elementWidth, style[LISTVIEW_ELEMENTS_HEIGHT] }, text[i], true, false); + GuiEnable(); + } else GuiListElement((Rectangle){ posX, bounds.y + style[LISTVIEW_ELEMENTS_PADDING] + LISTVIEW_LINE_THICK + (i - startIndex)*(style[LISTVIEW_ELEMENTS_HEIGHT] + style[LISTVIEW_ELEMENTS_PADDING]), elementWidth, style[LISTVIEW_ELEMENTS_HEIGHT] }, text[i], false, false); } } break; @@ -3118,6 +3146,7 @@ RAYGUIDEF bool GuiListViewEx(Rectangle bounds, const char **text, int *enabledEl } //-------------------------------------------------------------------- *scrollIndex = startIndex; + *focus = focusElement; *active = auxActive; return pressed; }