Added function GenerateCodeFromRGL()

Some code tweaks and comments review
This commit is contained in:
Ray San
2018-04-06 11:55:13 +02:00
parent 7c14e7547a
commit d463be524c

View File

@ -92,10 +92,11 @@ const char *controlTypeNameShort[] = { "lbl", "btn", "ibtn", "tggl", "tgroup", "
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module specific Functions Declaration // Module specific Functions Declaration
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
static void DrawGrid2D(int divsX, int divsY); // Draw 2d grid with horizontal and vertical lines depending on the screen size static void DrawGrid2D(int divsX, int divsY); // Draw 2d grid with horizontal and vertical lines depending on the screen size
static void SaveLayoutRGL(const char *fileName, bool binary); // Save gui layout project information static void SaveLayoutRGL(const char *fileName, bool binary); // Save gui layout project information
static void LoadLayoutRGL(const char *fileName); // Load gui layout project information static void LoadLayoutRGL(const char *fileName); // Load gui layout project information
static void GenerateLayoutCode(const char *fileName , bool noStaticData); // Generate C code for gui layout static void GenerateCode(const char *fileName , bool noStaticData); // Generate C code for gui layout
static void GenerateCodeFromRGL(const char *fileName); // Generate C code from .rgl file
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Main Entry point // Main Entry point
@ -535,6 +536,7 @@ int main()
if (offsetY >= GRID_LINE_SPACING/2) defaultRec[selectedType].y += (GRID_LINE_SPACING - offsetY); if (offsetY >= GRID_LINE_SPACING/2) defaultRec[selectedType].y += (GRID_LINE_SPACING - offsetY);
else defaultRec[selectedType].y -= offsetY; else defaultRec[selectedType].y -= offsetY;
// Snap rectangle size to closer snap point sizes // Snap rectangle size to closer snap point sizes
/* /*
offsetX = defaultRec[selectedType].width%GRID_LINE_SPACING; offsetX = defaultRec[selectedType].width%GRID_LINE_SPACING;
@ -551,7 +553,6 @@ int main()
// Resize the controller aplying the snap // Resize the controller aplying the snap
if (!textEditMode && IsKeyPressed(KEY_R) && selectedControl != -1) if (!textEditMode && IsKeyPressed(KEY_R) && selectedControl != -1)
{ {
int offsetX = layout[selectedControl].rec.width%GRID_LINE_SPACING; int offsetX = layout[selectedControl].rec.width%GRID_LINE_SPACING;
int offsetY = layout[selectedControl].rec.height%GRID_LINE_SPACING; int offsetY = layout[selectedControl].rec.height%GRID_LINE_SPACING;
@ -560,7 +561,6 @@ int main()
if (offsetY >= GRID_LINE_SPACING/2) layout[selectedControl].rec.height += (GRID_LINE_SPACING - offsetY); if (offsetY >= GRID_LINE_SPACING/2) layout[selectedControl].rec.height += (GRID_LINE_SPACING - offsetY);
else layout[selectedControl].rec.height -= offsetY; else layout[selectedControl].rec.height -= offsetY;
} }
// Check if control has text to edit // Check if control has text to edit
@ -612,10 +612,7 @@ int main()
lockMode = true; lockMode = true;
saveControlSelected = selectedControl; saveControlSelected = selectedControl;
} }
else if (IsKeyPressed(KEY_SPACE) && (selectedControl != -1)) else if (IsKeyPressed(KEY_SPACE) && (selectedControl != -1)) lockMode = false;
{
lockMode = false;
}
// Checks if mouse is over an anchor // Checks if mouse is over an anchor
for (int i = 1; i < MAX_ANCHOR_POINTS; i++) for (int i = 1; i < MAX_ANCHOR_POINTS; i++)
@ -711,6 +708,7 @@ int main()
anchorNewPos = false; anchorNewPos = false;
} }
controlDrag = false; controlDrag = false;
selectedAnchor = -1; selectedAnchor = -1;
anchorMode = false; anchorMode = false;
@ -746,10 +744,8 @@ int main()
anchorMode = false; anchorMode = false;
} }
if (IsKeyPressed(KEY_H)) // Hide/Unhide selected anchor linked controls
{ if (IsKeyPressed(KEY_H)) anchors[selectedAnchor].hidding = !anchors[selectedAnchor].hidding;
anchors[selectedAnchor].hidding = !anchors[selectedAnchor].hidding;
}
} }
} }
@ -794,8 +790,6 @@ int main()
else if (layout[selectedControl].rec.height <= 20) layout[selectedControl].rec.height = 20; else if (layout[selectedControl].rec.height <= 20) layout[selectedControl].rec.height = 20;
} }
// TODO: Draw global app screen limits (black rectangle with black default anchor)
// Shows or hides the grid if not in textEditMode // Shows or hides the grid if not in textEditMode
if (IsKeyPressed(KEY_G) && (!textEditMode)) showGrid = !showGrid; if (IsKeyPressed(KEY_G) && (!textEditMode)) showGrid = !showGrid;
@ -852,17 +846,18 @@ int main()
const char *filters[] = { "*.rgl" }; const char *filters[] = { "*.rgl" };
const char *fileName = tinyfd_openFileDialog("Load raygui layout file", "", 1, filters, "raygui Layout Files (*.rgl)", 0); const char *fileName = tinyfd_openFileDialog("Load raygui layout file", "", 1, filters, "raygui Layout Files (*.rgl)", 0);
if (fileName != NULL) if (fileName != NULL) LoadLayoutRGL(fileName);
{
LoadLayoutRGL(fileName);
// Setup by default some anchor value because logic is always trying to access layout[i].ap->id
// if layout[i].ap == NULL, program crashes
//for (int i = 0; i < controlsCounter; i++) layout[i].ap = &anchors[0];
}
} }
if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_ENTER)) GenerateLayoutCode("test_layout.c", true); if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_ENTER))
{
// Save file dialog
// TODO: Support additional languages (.lua, .go...) code generation
const char *filters[] = { "*.c", "*.go", "*.lua" };
const char *fileName = tinyfd_saveFileDialog("Generate code file", "layout.c", 3, filters, "Code file");
if (fileName != NULL) GenerateCode(fileName, true);
}
// Tracemap texture control logic // Tracemap texture control logic
if (tracemap.id > 0) if (tracemap.id > 0)
@ -905,7 +900,6 @@ int main()
{ {
tracemapRec.height -= 10*GetMouseWheelMove(); tracemapRec.height -= 10*GetMouseWheelMove();
tracemapRec.width -= 10*GetMouseWheelMove(); tracemapRec.width -= 10*GetMouseWheelMove();
} }
tracemap.height = tracemapRec.height; tracemap.height = tracemapRec.height;
@ -937,6 +931,8 @@ int main()
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
// TODO: Draw global app screen limits (black rectangle with black default anchor)
if (showGrid) DrawGrid2D(GetScreenWidth()/13, GetScreenHeight()/13); if (showGrid) DrawGrid2D(GetScreenWidth()/13, GetScreenHeight()/13);
if (tracemap.id > 0) DrawTexture(tracemap, tracemapRec.x, tracemapRec.y, Fade(WHITE, tracemapFade)); if (tracemap.id > 0) DrawTexture(tracemap, tracemapRec.x, tracemapRec.y, Fade(WHITE, tracemapFade));
@ -973,8 +969,6 @@ int main()
if ((layout[i].ap->id == selectedAnchor) && (layout[i].ap->id > 0)) DrawLine(layout[i].ap->x, layout[i].ap->y, layout[i].ap->x + layout[i].rec.x, layout[i].ap->y + layout[i].rec.y, RED); if ((layout[i].ap->id == selectedAnchor) && (layout[i].ap->id > 0)) DrawLine(layout[i].ap->x, layout[i].ap->y, layout[i].ap->x + layout[i].rec.x, layout[i].ap->y + layout[i].rec.y, RED);
} }
else if ((layout[i].ap->id == selectedAnchor) && (layout[i].ap->id > 0)) DrawLine(layout[i].ap->x, layout[i].ap->y, layout[i].ap->x + layout[i].rec.x, layout[i].ap->y + layout[i].rec.y, BLUE); else if ((layout[i].ap->id == selectedAnchor) && (layout[i].ap->id > 0)) DrawLine(layout[i].ap->x, layout[i].ap->y, layout[i].ap->x + layout[i].rec.x, layout[i].ap->y + layout[i].rec.y, BLUE);
// Draw Control anchor information
// DrawText(FormatText("Id: %i | X: %i | Y: %i | Enabled: %i", layout[0].ap->id, layout[0].ap->x, layout[0].ap->y, layout[0].ap->enabled), 100, 100, style[DEFAULT_TEXT_SIZE], RED);
} }
// Draws the defaultRec[selectedType] of the control selected // Draws the defaultRec[selectedType] of the control selected
@ -1005,11 +999,8 @@ int main()
} }
} }
// Draw the tracemap controler // Draw the tracemap rectangle
if (tracemapEditMode) if (tracemapEditMode) DrawRectangleLines(tracemapRec.x, tracemapRec.y, tracemapRec.width, tracemapRec.height, RED);
{
DrawRectangleLines(tracemapRec.x, tracemapRec.y, tracemapRec.width, tracemapRec.height, RED);
}
/* /*
// Draw the list of controls // Draw the list of controls
@ -1061,13 +1052,17 @@ int main()
} }
} }
// Draw selected control selection rectangle (transparent RED)
if ((selectedControl != -1) && (selectedControl < controlsCounter)) if ((selectedControl != -1) && (selectedControl < controlsCounter))
{ {
DrawRectangleRec((Rectangle){ layout[selectedControl].ap->x + layout[selectedControl].rec.x, layout[selectedControl].ap->y + layout[selectedControl].rec.y, layout[selectedControl].rec.width, layout[selectedControl].rec.height }, Fade(RED, 0.5f)); DrawRectangleRec((Rectangle){ layout[selectedControl].ap->x + layout[selectedControl].rec.x, layout[selectedControl].ap->y + layout[selectedControl].rec.y, layout[selectedControl].rec.width, layout[selectedControl].rec.height }, Fade(RED, 0.5f));
// Draw anchor lines (if not hidden)
if (layout[selectedControl].ap->id > 0 && !layout[selectedControl].ap->hidding) DrawLine(layout[selectedControl].ap->x, layout[selectedControl].ap->y, layout[selectedControl].ap->x + layout[selectedControl].rec.x, layout[selectedControl].ap->y + layout[selectedControl].rec.y, RED); if (layout[selectedControl].ap->id > 0 && !layout[selectedControl].ap->hidding) DrawLine(layout[selectedControl].ap->x, layout[selectedControl].ap->y, layout[selectedControl].ap->x + layout[selectedControl].rec.x, layout[selectedControl].ap->y + layout[selectedControl].rec.y, RED);
else if (layout[selectedControl].ap->id > 0 && layout[selectedControl].ap->hidding) DrawLine(layout[selectedControl].ap->x, layout[selectedControl].ap->y, layout[selectedControl].ap->x + layout[selectedControl].rec.x, layout[selectedControl].ap->y + layout[selectedControl].rec.y, BLUE); else if (layout[selectedControl].ap->id > 0 && layout[selectedControl].ap->hidding) DrawLine(layout[selectedControl].ap->x, layout[selectedControl].ap->y, layout[selectedControl].ap->x + layout[selectedControl].rec.x, layout[selectedControl].ap->y + layout[selectedControl].rec.y, BLUE);
} }
// Draw cursor (control mode or anchor mode)
if ((selectedControl == -1) && (selectedAnchor == -1)) if ((selectedControl == -1) && (selectedAnchor == -1))
{ {
if (anchorMode) if (anchorMode)
@ -1083,7 +1078,7 @@ int main()
} }
} }
// Draws the cursor of textEditMode // Draw cursor on textEditMode
if (textEditMode) if (textEditMode)
{ {
if (((framesCounter/20)%2) == 0) if (((framesCounter/20)%2) == 0)
@ -1097,28 +1092,32 @@ int main()
} }
} }
// Draw anchor linking line
if (anchorLinkMode) DrawLine(anchors[linkedAnchor].x, anchors[linkedAnchor].y, mouseX, mouseY, BLACK); if (anchorLinkMode) DrawLine(anchors[linkedAnchor].x, anchors[linkedAnchor].y, mouseX, mouseY, BLACK);
// Draw the help list // Draw the help list (by default is out of screen)
DrawRectangleRec((Rectangle){ helpPosX + 20, 20, 260, 350 }, GetColor(style[DEFAULT_BACKGROUND_COLOR])); if (helpPosX > -280)
GuiGroupBox((Rectangle){ helpPosX + 20, 20, 260, 350 }, "Shortcuts"); {
GuiLabel((Rectangle){ helpPosX + 30, 30, 0, 0 }, "G - Show/hide grid"); DrawRectangleRec((Rectangle){ helpPosX + 20, 20, 260, 350 }, GetColor(style[DEFAULT_BACKGROUND_COLOR]));
GuiLabel((Rectangle){ helpPosX + 30, 50, 0, 0 }, "S - Toggle snap"); GuiGroupBox((Rectangle){ helpPosX + 20, 20, 260, 350 }, "Shortcuts");
GuiLabel((Rectangle){ helpPosX + 30, 70, 0, 0 }, "R - Resize to grid"); GuiLabel((Rectangle){ helpPosX + 30, 30, 0, 0 }, "G - Show/hide grid");
GuiLabel((Rectangle){ helpPosX + 30, 90, 0, 0 }, "A - Anchor mode"); GuiLabel((Rectangle){ helpPosX + 30, 50, 0, 0 }, "S - Toggle snap");
GuiLabel((Rectangle){ helpPosX + 30, 110, 0, 0 }, "H - Hide controls of selected anchor"); GuiLabel((Rectangle){ helpPosX + 30, 70, 0, 0 }, "R - Resize to grid");
GuiLabel((Rectangle){ helpPosX + 30, 130, 0, 0 }, "U - Unlink anchor"); GuiLabel((Rectangle){ helpPosX + 30, 90, 0, 0 }, "A - Anchor mode");
GuiLabel((Rectangle){ helpPosX + 30, 150, 0, 0 }, "Space - Lock/unlock control"); GuiLabel((Rectangle){ helpPosX + 30, 110, 0, 0 }, "H - Hide controls of selected anchor");
GuiLabel((Rectangle){ helpPosX + 30, 170, 0, 0 }, "T - Enter text mode(if possible)"); GuiLabel((Rectangle){ helpPosX + 30, 130, 0, 0 }, "U - Unlink anchor");
GuiLabel((Rectangle){ helpPosX + 30, 190, 0, 0 }, "Enter - Exit text mode"); GuiLabel((Rectangle){ helpPosX + 30, 150, 0, 0 }, "Space - Lock/unlock control");
GuiLabel((Rectangle){ helpPosX + 30, 210, 0, 0 }, "Delete - Delete a control"); GuiLabel((Rectangle){ helpPosX + 30, 170, 0, 0 }, "T - Enter text mode(if possible)");
GuiLabel((Rectangle){ helpPosX + 30, 230, 0, 0 }, "Arrows - Modify width/height"); GuiLabel((Rectangle){ helpPosX + 30, 190, 0, 0 }, "Enter - Exit text mode");
GuiLabel((Rectangle){ helpPosX + 30, 250, 0, 0 }, "L. Ctrl + Arrows - Modify width/height(smooth)"); GuiLabel((Rectangle){ helpPosX + 30, 210, 0, 0 }, "Delete - Delete a control");
GuiLabel((Rectangle){ helpPosX + 30, 270, 0, 0 }, "L. Alt + Arrows - Modify position"); GuiLabel((Rectangle){ helpPosX + 30, 230, 0, 0 }, "Arrows - Modify width/height");
GuiLabel((Rectangle){ helpPosX + 30, 290, 0, 0 }, "L. Ctrl + Enter - Export layout to code"); GuiLabel((Rectangle){ helpPosX + 30, 250, 0, 0 }, "L. Ctrl + Arrows - Modify width/height(smooth)");
GuiLabel((Rectangle){ helpPosX + 30, 310, 0, 0 }, "L. Ctrl + S - Save layout(.rgl)"); GuiLabel((Rectangle){ helpPosX + 30, 270, 0, 0 }, "L. Alt + Arrows - Modify position");
GuiLabel((Rectangle){ helpPosX + 30, 330, 0, 0 }, "L. Ctrl + O - Open layout(.rgl)"); GuiLabel((Rectangle){ helpPosX + 30, 290, 0, 0 }, "L. Ctrl + Enter - Export layout to code");
GuiLabel((Rectangle){ helpPosX + 30, 350, 0, 0 }, "L. Ctrl + D - Duplicate selected control"); GuiLabel((Rectangle){ helpPosX + 30, 310, 0, 0 }, "L. Ctrl + S - Save layout(.rgl)");
GuiLabel((Rectangle){ helpPosX + 30, 330, 0, 0 }, "L. Ctrl + O - Open layout(.rgl)");
GuiLabel((Rectangle){ helpPosX + 30, 350, 0, 0 }, "L. Ctrl + D - Duplicate selected control");
}
// Draw status bar bottom with debug information // Draw status bar bottom with debug information
GuiStatusBar((Rectangle){ 0, GetScreenHeight() - 24, 125, 24}, FormatText("Controls count: %i", controlsCounter), 20); GuiStatusBar((Rectangle){ 0, GetScreenHeight() - 24, 125, 24}, FormatText("Controls count: %i", controlsCounter), 20);
@ -1255,7 +1254,7 @@ static void SaveLayoutRGL(const char *fileName, bool binary)
} }
// Import gui layout project information // Import gui layout project information
// NOTE: Imported from text file // NOTE: Updates global variable: layout
static void LoadLayoutRGL(const char *fileName) static void LoadLayoutRGL(const char *fileName)
{ {
char buffer[256]; char buffer[256];
@ -1357,7 +1356,7 @@ static void LoadLayoutRGL(const char *fileName)
} }
// Generate C code for gui layout // Generate C code for gui layout
static void GenerateLayoutCode(const char *fileName , bool noStaticData) static void GenerateCode(const char *fileName , bool noStaticData)
{ {
#define RGL_TOOL_NAME "rGuiLayout" #define RGL_TOOL_NAME "rGuiLayout"
#define RGL_TOOL_DESCRIPTION "tool_name" #define RGL_TOOL_DESCRIPTION "tool_name"
@ -1610,3 +1609,22 @@ static void GenerateLayoutCode(const char *fileName , bool noStaticData)
fclose(ftool); fclose(ftool);
} }
// Generate C code from .rgl file
static void GenerateCodeFromRGL(const char *fileName)
{
if (IsFileExtension(fileName, ".rgl"))
{
LoadLayoutRGL(fileName); // Updates global: layout
int len = strlen(fileName);
char outName[256] = "\0";
strcpy(outName, fileName);
outName[len - 3] = 'c';
outName[len - 2] = '\0';
// Generate C code for gui layout
GenerateCode(outName, true);
}
else printf("Input RGL file not valid\n");
}