GuiListViewEx WIP

This commit is contained in:
Sergio Martínez
2018-10-29 19:41:51 +01:00
parent a85979661f
commit 82ccf18759
2 changed files with 216 additions and 4 deletions

View File

@ -55,6 +55,14 @@ int main()
bool forceSquaredChecked = false;
//int *enableElements = (int *)malloc(6*sizeof(int));
int enableElements[6] = {0, 0, 0, 0, 0, 0};
for (int i = 0; i < 6; i++)
{
enableElements[i] = 1;
}
Color colorPickerValue = RED;
//----------------------------------------------------------------------------------
@ -81,12 +89,13 @@ int main()
// raygui: controls drawing
//----------------------------------------------------------------------------------
if (dropDown000EditMode || dropDown001EditMode) GuiLock();
GuiDisable();
//if (GuiSpinner((Rectangle){ 25, 75, 125, 30 }, &Spinner001Value, 0, 100, 25, spinnerEditMode)) spinnerEditMode = !spinnerEditMode;
if (GuiValueBox((Rectangle){ 25, 125, 125, 30 }, &ValueBox002Value, 0, 100, valueBoxEditMode)) valueBoxEditMode = !valueBoxEditMode;
if (GuiTextBox((Rectangle){ 25, 175, 125, 30 }, TextBox003Text, 64, textBoxEditMode)) textBoxEditMode = !textBoxEditMode;
if (GuiListView((Rectangle){ 175, 25, 120, 100 }, ListView004TextList, 6, &ListView004Active, listViewEditMode)) listViewEditMode = !listViewEditMode;
//if (GuiListView((Rectangle){ 175, 25, 120, 100 }, ListView004TextList, 6, &ListView004Active, listViewEditMode)) listViewEditMode = !listViewEditMode;
if (GuiListViewEx((Rectangle){ 175, 25, 120, 100 }, ListView004TextList, enableElements, 6, &ListView004Active, listViewEditMode)) listViewEditMode = !listViewEditMode;
if (GuiButton((Rectangle){ 25, 225, 125, 30 }, "SAMPLE TEXT")) Button005();
if (GuiTextBoxMulti((Rectangle){ 325, 25, 225, 175 }, TextBox006Text, 141, multiTextBoxEditMode)) multiTextBoxEditMode = !multiTextBoxEditMode;
@ -113,7 +122,7 @@ int main()
EndDrawing();
//----------------------------------------------------------------------------------
}
//free(enableElements);
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context

View File

@ -408,7 +408,8 @@ RAYGUIDEF void GuiStatusBar(Rectangle bounds, const char *text, int offsetX);
RAYGUIDEF void GuiDummyRec(Rectangle bounds, const char *text); // Dummy control for placeholders
// Advance controls set
RAYGUIDEF bool GuiListView(Rectangle bounds, const char **text, int count, int *active, bool editMode); // List View control, returns selected list element index
RAYGUIDEF bool GuiListView(Rectangle bounds, const char **text, int count, int *active, bool editMode); // List View control, returns selected list element index
RAYGUIDEF bool GuiListViewEx(Rectangle bounds, const char **text, int *enableElements, int count, int *active, 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
@ -3079,6 +3080,208 @@ RAYGUIDEF bool GuiListView(Rectangle bounds, const char **text, int count, int *
return pressed;
}
RAYGUIDEF bool GuiListViewEx(Rectangle bounds, const char **text, int *enableElements, int count, int *active, bool editMode)
{
#define LISTVIEW_LINE_THICK 1
bool enableList = false;
if (enableElements != NULL) enableList = true;
GuiControlState state = guiState;
bool pressed = false;
static int startIndex = 0;
bool useScrollBar = true;
bool pressedKey = false;
int visibleElements = bounds.height/(style[LISTVIEW_ELEMENTS_HEIGHT] + style[LISTVIEW_ELEMENTS_PADDING]);
int endIndex = startIndex + visibleElements;
int auxActive = *active;
float barHeight = bounds.height;
float minBarHeight = 10;
float barPosY = 0;
// All the elements fit inside ListView and dont need scrollbar.
if (visibleElements >= count)
{
useScrollBar = false;
startIndex = 0;
endIndex = count;
}
// Update control
//--------------------------------------------------------------------
if ((state != DISABLED) && !guiLocked) // && !guiLocked
{
Vector2 mousePoint = GetMousePosition();
if (editMode)
{
state = PRESSED;
// Change active with keys
if (IsKeyPressed(KEY_UP))
{
if (auxActive > 0)
{
auxActive--;
if ((useScrollBar) && (auxActive < startIndex)) startIndex--;
}
pressedKey = true;
}
else if (IsKeyPressed(KEY_DOWN))
{
if (auxActive < count - 1)
{
auxActive++;
if ((useScrollBar) && (auxActive >= endIndex)) startIndex++;
}
pressedKey = true;
}
if (useScrollBar)
{
endIndex = startIndex + visibleElements;
int wheel = GetMouseWheelMove();
if (wheel < 0 && endIndex < count) startIndex -= wheel;
else if (wheel > 0 && startIndex > 0) startIndex -= wheel;
if (pressedKey)
{
pressedKey = false;
if ((auxActive < startIndex) || (auxActive >= endIndex)) startIndex = auxActive;
}
if (startIndex < 0) startIndex = 0;
else if (startIndex > (count - (endIndex - startIndex)))
{
startIndex = count - (endIndex - startIndex);
}
endIndex = startIndex + visibleElements;
if (endIndex > count) endIndex = count;
}
}
// ------------------------------------------------------------------------------------
// Note: Changing editMode
if (!editMode)
{
if (CheckCollisionPointRec(mousePoint, bounds))
{
state = FOCUSED;
if (IsMouseButtonPressed(0)) pressed = true;
int wheel = GetMouseWheelMove();
if (wheel)
{
startIndex -= wheel;
if (startIndex < 0) startIndex = 0;
else if (startIndex > (count - (endIndex - startIndex)))
{
startIndex = count - (endIndex - startIndex);
}
pressed = true;
}
}
}
else
{
if (!CheckCollisionPointRec(mousePoint, bounds) && IsMouseButtonPressed(0)) pressed = true;
}
}
// Calculamos el porcentaje de elementos visibles, y aplicamos el mismo porcentaje al tamaño de la barra original.
// Hay que tener en cuenta un valor mínimo para que la barra no sea de 1 px nunca y también que no sea mayor que la altura máxima.
if (useScrollBar)
{
float percentVisible = (endIndex - startIndex)*100/count;
barHeight *= percentVisible/100;
if (barHeight < minBarHeight) barHeight = minBarHeight;
else if (barHeight > bounds.height) barHeight = bounds.height;
// Posición Y a la que dibujamos la barra.
barPosY = bounds.y + startIndex*((bounds.height - barHeight)/(count - (endIndex - startIndex)));
}
//--------------------------------------------------------------------
// Draw control
//--------------------------------------------------------------------
// 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;
}
// Draw GuiListElements
for (int i = startIndex; i < endIndex; i++)
{
if (enableList && enableElements[i] == 0)
{
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);
}
else
{
if (i == auxActive && editMode)
{
if (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, true) == false) auxActive = -1;
}
else
{
if (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, true) == true) auxActive = i;
}
}
}
// Draw scrollBar background
if (useScrollBar) DrawRectangle(bounds.x, bounds.y, style[LISTVIEW_BAR_WIDTH], bounds.height, Fade(GetColor(style[DEFAULT_BORDER_COLOR_DISABLED]), guiAlpha));
// Draw ListView states
switch (state)
{
case NORMAL:
{
if (useScrollBar) DrawRectangle(bounds.x, barPosY, style[LISTVIEW_BAR_WIDTH], barHeight, Fade(GetColor(style[SLIDERBAR_BORDER_COLOR_NORMAL]), guiAlpha));
DrawRectangleLinesEx(bounds, LISTVIEW_LINE_THICK, Fade(GetColor(style[LISTVIEW_BORDER_COLOR_NORMAL]), guiAlpha));
if(auxActive >= startIndex && auxActive < endIndex) GuiListElement((Rectangle){ posX, bounds.y + style[LISTVIEW_ELEMENTS_PADDING] + LISTVIEW_LINE_THICK + (auxActive - startIndex)*(style[LISTVIEW_ELEMENTS_HEIGHT] + style[LISTVIEW_ELEMENTS_PADDING]), elementWidth, style[LISTVIEW_ELEMENTS_HEIGHT] }, text[auxActive], true, false);
} break;
case FOCUSED:
{
if (useScrollBar) DrawRectangle(bounds.x, barPosY, style[LISTVIEW_BAR_WIDTH], barHeight, Fade(GetColor(style[SLIDERBAR_BASE_COLOR_FOCUSED]), guiAlpha));
DrawRectangleLinesEx(bounds, LISTVIEW_LINE_THICK, Fade(GetColor(style[LISTVIEW_BORDER_COLOR_FOCUSED]), guiAlpha));
if(auxActive >= startIndex && auxActive < endIndex) GuiListElement((Rectangle){ posX, bounds.y + style[LISTVIEW_ELEMENTS_PADDING] + LISTVIEW_LINE_THICK + (auxActive - startIndex)*(style[LISTVIEW_ELEMENTS_HEIGHT] + style[LISTVIEW_ELEMENTS_PADDING]), elementWidth, style[LISTVIEW_ELEMENTS_HEIGHT] }, text[auxActive], true, false);
} break;
case PRESSED:
{
if (useScrollBar) DrawRectangle(bounds.x, barPosY, style[LISTVIEW_BAR_WIDTH], barHeight, Fade(GetColor(style[SLIDERBAR_BASE_COLOR_PRESSED]), guiAlpha));
DrawRectangleLinesEx(bounds, LISTVIEW_LINE_THICK, Fade(GetColor(style[LISTVIEW_BORDER_COLOR_PRESSED]), guiAlpha));
} break;
case DISABLED:
{
if (useScrollBar) DrawRectangle(bounds.x, barPosY, style[LISTVIEW_BAR_WIDTH], barHeight, Fade(GetColor(style[LISTVIEW_BASE_COLOR_DISABLED]), guiAlpha));
DrawRectangleLinesEx(bounds, LISTVIEW_LINE_THICK, Fade(GetColor(style[LISTVIEW_BORDER_COLOR_DISABLED]), guiAlpha));
} break;
default: break;
}
//--------------------------------------------------------------------
*active = auxActive;
return pressed;
}
// Color Panel control
RAYGUIDEF Color GuiColorPanel(Rectangle bounds, Color color)
{