Improving rGuiLayout tool -WIP-

This commit is contained in:
Ray San
2018-02-08 14:01:58 +01:00
parent 9398de700b
commit de9a6ccb0e
3 changed files with 489 additions and 9 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 395 B

253
tools/rGuiLayout/easings.h Normal file
View File

@ -0,0 +1,253 @@
/*******************************************************************************************
*
* raylib easings (header only file)
*
* Useful easing functions for values animation
*
* This header uses:
* #define EASINGS_STATIC_INLINE // Inlines all functions code, so it runs faster.
* // This requires lots of memory on system.
* How to use:
* The four inputs t,b,c,d are defined as follows:
* t = current time (in any unit measure, but same unit as duration)
* b = starting value to interpolate
* c = the total change in value of b that needs to occur
* d = total time it should take to complete (duration)
*
* Example:
*
* int currentTime = 0;
* int duration = 100;
* float startPositionX = 0.0f;
* float finalPositionX = 30.0f;
* float currentPositionX = startPositionX;
*
* while (currentPositionX < finalPositionX)
* {
* currentPositionX = EaseSineIn(currentTime, startPositionX, finalPositionX - startPositionX, duration);
* currentTime++;
* }
*
* A port of Robert Penner's easing equations to C (http://robertpenner.com/easing/)
*
* Robert Penner License
* ---------------------------------------------------------------------------------
* Open source under the BSD License.
*
* Copyright (c) 2001 Robert Penner. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the author nor the names of contributors may be used
* to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ---------------------------------------------------------------------------------
*
* Copyright (c) 2015 Ramon Santamaria
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
**********************************************************************************************/
#ifndef EASINGS_H
#define EASINGS_H
#define EASINGS_STATIC_INLINE // NOTE: By default, compile functions as static inline
#if defined(EASINGS_STATIC_INLINE)
#define EASEDEF static inline
#else
#define EASEDEF extern
#endif
#include <math.h> // Required for: sin(), cos(), sqrt(), pow()
#ifdef __cplusplus
extern "C" { // Prevents name mangling of functions
#endif
// Linear Easing functions
EASEDEF float EaseLinearNone(float t, float b, float c, float d) { return (c*t/d + b); }
EASEDEF float EaseLinearIn(float t, float b, float c, float d) { return (c*t/d + b); }
EASEDEF float EaseLinearOut(float t, float b, float c, float d) { return (c*t/d + b); }
EASEDEF float EaseLinearInOut(float t,float b, float c, float d) { return (c*t/d + b); }
// Sine Easing functions
EASEDEF float EaseSineIn(float t, float b, float c, float d) { return (-c*cos(t/d*(PI/2)) + c + b); }
EASEDEF float EaseSineOut(float t, float b, float c, float d) { return (c*sin(t/d*(PI/2)) + b); }
EASEDEF float EaseSineInOut(float t, float b, float c, float d) { return (-c/2*(cos(PI*t/d) - 1) + b); }
// Circular Easing functions
EASEDEF float EaseCircIn(float t, float b, float c, float d) { return (-c*(sqrt(1 - (t/=d)*t) - 1) + b); }
EASEDEF float EaseCircOut(float t, float b, float c, float d) { return (c*sqrt(1 - (t=t/d-1)*t) + b); }
EASEDEF float EaseCircInOut(float t, float b, float c, float d)
{
if ((t/=d/2) < 1) return (-c/2*(sqrt(1 - t*t) - 1) + b);
return (c/2*(sqrt(1 - t*(t-=2)) + 1) + b);
}
// Cubic Easing functions
EASEDEF float EaseCubicIn(float t, float b, float c, float d) { return (c*(t/=d)*t*t + b); }
EASEDEF float EaseCubicOut(float t, float b, float c, float d) { return (c*((t=t/d-1)*t*t + 1) + b); }
EASEDEF float EaseCubicInOut(float t, float b, float c, float d)
{
if ((t/=d/2) < 1) return (c/2*t*t*t + b);
return (c/2*((t-=2)*t*t + 2) + b);
}
// Quadratic Easing functions
EASEDEF float EaseQuadIn(float t, float b, float c, float d) { return (c*(t/=d)*t + b); }
EASEDEF float EaseQuadOut(float t, float b, float c, float d) { return (-c*(t/=d)*(t-2) + b); }
EASEDEF float EaseQuadInOut(float t, float b, float c, float d)
{
if ((t/=d/2) < 1) return (((c/2)*(t*t)) + b);
return (-c/2*(((t-2)*(--t)) - 1) + b);
}
// Exponential Easing functions
EASEDEF float EaseExpoIn(float t, float b, float c, float d) { return (t == 0) ? b : (c*pow(2, 10*(t/d - 1)) + b); }
EASEDEF float EaseExpoOut(float t, float b, float c, float d) { return (t == d) ? (b + c) : (c*(-pow(2, -10*t/d) + 1) + b); }
EASEDEF float EaseExpoInOut(float t, float b, float c, float d)
{
if (t == 0) return b;
if (t == d) return (b + c);
if ((t/=d/2) < 1) return (c/2*pow(2, 10*(t - 1)) + b);
return (c/2*(-pow(2, -10*--t) + 2) + b);
}
// Back Easing functions
EASEDEF float EaseBackIn(float t, float b, float c, float d)
{
float s = 1.70158f;
float postFix = t/=d;
return (c*(postFix)*t*((s + 1)*t - s) + b);
}
EASEDEF float EaseBackOut(float t, float b, float c, float d)
{
float s = 1.70158f;
return (c*((t=t/d-1)*t*((s + 1)*t + s) + 1) + b);
}
EASEDEF float EaseBackInOut(float t, float b, float c, float d)
{
float s = 1.70158f;
if ((t/=d/2) < 1) return (c/2*(t*t*(((s*=(1.525f)) + 1)*t - s)) + b);
float postFix = t-=2;
return (c/2*((postFix)*t*(((s*=(1.525f)) + 1)*t + s) + 2) + b);
}
// Bounce Easing functions
EASEDEF float EaseBounceOut(float t, float b, float c, float d)
{
if ((t/=d) < (1/2.75f))
{
return (c*(7.5625f*t*t) + b);
}
else if (t < (2/2.75f))
{
float postFix = t-=(1.5f/2.75f);
return (c*(7.5625f*(postFix)*t + 0.75f) + b);
}
else if (t < (2.5/2.75))
{
float postFix = t-=(2.25f/2.75f);
return (c*(7.5625f*(postFix)*t + 0.9375f) + b);
}
else
{
float postFix = t-=(2.625f/2.75f);
return (c*(7.5625f*(postFix)*t + 0.984375f) + b);
}
}
EASEDEF float EaseBounceIn(float t, float b, float c, float d) { return (c - EaseBounceOut(d-t, 0, c, d) + b); }
EASEDEF float EaseBounceInOut(float t, float b, float c, float d)
{
if (t < d/2) return (EaseBounceIn(t*2, 0, c, d)*0.5f + b);
else return (EaseBounceOut(t*2-d, 0, c, d)*0.5f + c*0.5f + b);
}
// Elastic Easing functions
EASEDEF float EaseElasticIn(float t, float b, float c, float d)
{
if (t == 0) return b;
if ((t/=d) == 1) return (b + c);
float p = d*0.3f;
float a = c;
float s = p/4;
float postFix = a*pow(2, 10*(t-=1));
return (-(postFix*sin((t*d-s)*(2*PI)/p )) + b);
}
EASEDEF float EaseElasticOut(float t, float b, float c, float d)
{
if (t == 0) return b;
if ((t/=d) == 1) return (b + c);
float p = d*0.3f;
float a = c;
float s = p/4;
return (a*pow(2,-10*t)*sin((t*d-s)*(2*PI)/p) + c + b);
}
EASEDEF float EaseElasticInOut(float t, float b, float c, float d)
{
if (t == 0) return b;
if ((t/=d/2) == 2) return (b + c);
float p = d*(0.3f*1.5f);
float a = c;
float s = p/4;
if (t < 1)
{
float postFix = a*pow(2, 10*(t-=1));
return -0.5f*(postFix*sin((t*d-s)*(2*PI)/p)) + b;
}
float postFix = a*pow(2, -10*(t-=1));
return (postFix*sin((t*d-s)*(2*PI)/p)*0.5f + c + b);
}
#ifdef __cplusplus
}
#endif
#endif // EASINGS_H

View File

@ -12,12 +12,14 @@
#include "raylib.h"
#define RAYGUI_IMPLEMENTATION
#include "../../src/raygui.h"
#define RAYGUI_STYLE_SAVE_LOAD
#include "raygui.h"
#include "easings.h"
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
#define MAX_GUI_CONTROLS 128 // Maximum number of gui controls
#define MAX_GUI_CONTROLS 64 // Maximum number of gui controls
#define GRID_LINE_SPACING 10 // Grid line spacing in pixels
#define GRID_ALPHA 0.1f // Grid lines alpha amount
@ -28,6 +30,7 @@
typedef enum {
LABEL = 0,
BUTTON,
IMAGEBUTTON,
TOGGLE,
TOGGLEGROUP,
SLIDER,
@ -36,14 +39,17 @@ typedef enum {
SPINNER,
COMBOBOX,
CHECKBOX,
TEXTBOX
TEXTBOX,
LISTVIEW,
COLORPICKER
} GuiControlType;
// Gui control type
typedef struct {
int id;
GuiControlType type;
int type;
Rectangle rec;
char text[32];
} GuiControl;
//----------------------------------------------------------------------------------
@ -57,9 +63,9 @@ static int controlsCounter = 0;
//Rectangle defaultControlWidth[] = { };
const char *controlTypeName[] = { "LABEL", "BUTTON", "TOGGLE", "TOGGLEGROUP", "SLIDER", "SLIDERBAR", "PROGRESSBAR", "SPINNER", "COMBOBOX", "CHECKBOX", "TEXTBOX" };
const char *controlTypeNameLow[] = { "Label", "Button", "Toggle", "ToggleGroup", "Slider", "SliderBar", "ProgressBar", "Spinner", "ComboBox", "CheckBox", "TextBox" };
const char *controlTypeNameShort[] = { "lbl", "btn", "tggl", "tgroup", "sldr", "sldrb", "prgssb", "spnr", "combox", "chkbox", "txtbox" };
const char *controlTypeName[] = { "LABEL", "BUTTON", "IMAGEBUTTON", "TOGGLE", "TOGGLEGROUP", "SLIDER", "SLIDERBAR", "PROGRESSBAR", "SPINNER", "COMBOBOX", "CHECKBOX", "TEXTBOX", "LISTVIEW", "COLORPICKER" };
const char *controlTypeNameLow[] = { "Label", "Button", "ImageButton", "Toggle", "ToggleGroup", "Slider", "SliderBar", "ProgressBar", "Spinner", "ComboBox", "CheckBox", "TextBox", "ListView", "ColorPicker" };
const char *controlTypeNameShort[] = { "lbl", "btn", "ibtn", "tggl", "tgroup", "sldr", "sldrb", "prgssb", "spnr", "combox", "chkbox", "txtbox", "lstvw", "clrpckr" };
//----------------------------------------------------------------------------------
// Module specific Functions Declaration
@ -87,6 +93,104 @@ int main()
int selectedType = BUTTON;
int mouseX, mouseY;
int selectedTypeDraw = LABEL;
bool controlCollision = false;
Rectangle recDraw = { 0, 0, 100, 30 };
Rectangle listViewControls = { -200, 0, 140, 500 };
int counterListViewControls = 0;
int startPosXListViewControls = -200;
int deltaPosXListViewControls = 0;
Rectangle listViewControlsCounter = { GetScreenWidth() + 140, 0, 140, 500 };
int counterListViewControlsCounter = 0;
int startPosXListViewControlsCounter = GetScreenWidth() + 140;
int deltaPosXListViewControlsCounter = 0;
const char *guiControls[14] = {
"LABEL",
"BUTTON",
"IMAGEBUTTON",
"TOGGLE",
"TOGGLEGROUP",
"SLIDER",
"SLIDERBAR",
"PROGRESSBAR",
"SPINNER",
"COMBOBOX",
"CHECKBOX",
"TEXTBOX",
"LISTVIEW",
"COLORPICKER"
};
const char *guiControlsCounter[64] = {
"00",
"01",
"02",
"03",
"04",
"05",
"06",
"07",
"08",
"09",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
"23",
"24",
"25",
"26",
"27",
"28",
"29",
"30",
"31",
"32",
"33",
"34",
"35",
"36",
"37",
"38",
"39",
"40",
"41",
"42",
"43",
"44",
"45",
"46",
"47",
"48",
"49",
"50",
"51",
"52",
"53",
"54",
"55",
"56",
"57",
"58",
"59",
"60",
"61",
"62",
"63"
};
GuiLoadStyleImage("default_light.png");
Texture2D texture = LoadTexture("default_light.png");
SetTargetFPS(120);
//--------------------------------------------------------------------------------------
@ -97,8 +201,65 @@ int main()
//----------------------------------------------------------------------------------
mouseX = GetMouseX();
mouseY = GetMouseY();
// Updates the recDraw position
recDraw.x = mouseX - recDraw.width/2;
recDraw.y = mouseY - recDraw.height/2;
// Checks if the recDraw is colliding with the list of the controls
if (CheckCollisionPointRec(GetMousePosition(), listViewControls))controlCollision = true;
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && (controlSelected == -1))
// Toggle on the controlListView
if (IsKeyPressed(KEY_TAB))
{
startPosXListViewControls = listViewControls.x;
deltaPosXListViewControls = 0 - startPosXListViewControls;
counterListViewControls = 0;
}
if (IsKeyDown(KEY_TAB))
{
counterListViewControls++;
if (counterListViewControls >= 60) counterListViewControls = 60;
listViewControls.x = (int)EaseCubicInOut(counterListViewControls, startPosXListViewControls, deltaPosXListViewControls, 60);
}
else if (IsKeyReleased(KEY_TAB))
{
startPosXListViewControls = listViewControls.x;
deltaPosXListViewControls = -200 - startPosXListViewControls;
counterListViewControls = 0;
}
else
{
counterListViewControls++;
if (counterListViewControls >= 60) counterListViewControls = 60;
listViewControls.x = (int)EaseCubicInOut(counterListViewControls, startPosXListViewControls, deltaPosXListViewControls, 60);
}
// Toggle on the controlListViewCounter
if (IsKeyPressed(KEY_LEFT_SHIFT))
{
startPosXListViewControlsCounter = listViewControlsCounter.x;
deltaPosXListViewControlsCounter = GetScreenWidth() -listViewControlsCounter.width - startPosXListViewControlsCounter;
counterListViewControlsCounter = 0;
}
if (IsKeyDown(KEY_LEFT_SHIFT))
{
counterListViewControlsCounter++;
if (counterListViewControlsCounter >= 60) counterListViewControlsCounter = 60;
listViewControlsCounter.x = (int)EaseCubicInOut(counterListViewControlsCounter, startPosXListViewControlsCounter, deltaPosXListViewControlsCounter, 60);
}
else if (IsKeyReleased(KEY_LEFT_SHIFT))
{
startPosXListViewControlsCounter = listViewControlsCounter.x;
deltaPosXListViewControlsCounter = GetScreenWidth() + 140 - startPosXListViewControlsCounter;
counterListViewControlsCounter = 0;
}
else
{
counterListViewControlsCounter++;
if (counterListViewControlsCounter >= 60) counterListViewControlsCounter = 60;
listViewControlsCounter.x = (int)EaseCubicInOut(counterListViewControlsCounter, startPosXListViewControlsCounter, deltaPosXListViewControlsCounter, 60);
}
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && (controlSelected == -1) && !controlCollision)
{
// Add new control (button)
layout[controlsCounter].id = controlsCounter;
@ -120,6 +281,9 @@ int main()
if (controlSelected != -1)
{
//Disables the recDraw
controlCollision = true;
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
{
layout[controlSelected].rec.x = mouseX - layout[controlSelected].rec.width/2;
@ -190,6 +354,7 @@ int main()
}
else
{
/*
if (IsKeyPressed(KEY_LEFT))
{
selectedType--;
@ -200,6 +365,14 @@ int main()
selectedType++;
if (selectedType > TEXTBOX) selectedType = TEXTBOX;
}
*/
//Enables the recDraw
if (!CheckCollisionPointRec(GetMousePosition(), listViewControls))controlCollision = false;
// Updates the selectedType with the MouseWheel
selectedType -= GetMouseWheelMove();
if (selectedType < LABEL) selectedType = LABEL;
else if (selectedType > COLORPICKER) selectedType = COLORPICKER;
selectedTypeDraw = selectedType;
}
if (IsKeyPressed(KEY_S)) snapMode = !snapMode;
@ -216,8 +389,28 @@ int main()
if (offsetY >= GRID_LINE_SPACING/2) mouseY += (GRID_LINE_SPACING - offsetY);
else mouseY -= offsetY;
// SnapMode of the DrawingControls
// Snap rectangle position to closer snap point
offsetX = recDraw.x%GRID_LINE_SPACING;
offsetY = recDraw.y%GRID_LINE_SPACING;
if (offsetX >= GRID_LINE_SPACING/2) recDraw.x += (GRID_LINE_SPACING - offsetX);
else recDraw.x -= offsetX;
if (offsetY >= GRID_LINE_SPACING/2) recDraw.y += (GRID_LINE_SPACING - offsetY);
else recDraw.y -= offsetY;
// Snap rectangle size to closer snap point sizes
offsetX = recDraw.width%GRID_LINE_SPACING;
offsetY = recDraw.height%GRID_LINE_SPACING;
if (offsetX >= GRID_LINE_SPACING/2) recDraw.width += (GRID_LINE_SPACING - offsetX);
else recDraw.width -= offsetX;
if (offsetY >= GRID_LINE_SPACING/2) recDraw.height += (GRID_LINE_SPACING - offsetY);
else recDraw.height -= offsetY;
}
if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_S)) SaveGuiLayout("test_layout.rlyt");
if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_O)) LoadGuiLayout("test_layout.rlyt");
if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_ENTER)) GenerateGuiLayoutCode("test_layout.c");
@ -237,12 +430,35 @@ int main()
DrawLine(mouseX, mouseY - 8, mouseX, mouseY + 8, RED);
}
// Draws the recDraw of the control selected
if(!controlCollision)
switch (selectedTypeDraw)
{
case LABEL: GuiLabel(recDraw, "TEXT SAMPLE"); break;
case BUTTON: GuiButton(recDraw, "BUTTON"); break;
case IMAGEBUTTON: GuiImageButton(recDraw, texture); break;
case TOGGLE: GuiToggleButton(recDraw, "TOGGLE", false); break;
case TOGGLEGROUP: GuiToggleGroup(recDraw, list, 3, 1); break;
case SLIDER: GuiSlider(recDraw, 40, 0, 100); break;
case SLIDERBAR: GuiSliderBar(recDraw, 40, 0, 100); break;
case PROGRESSBAR: GuiProgressBar(recDraw, 40, 0, 100); break;
case SPINNER: GuiSpinner(recDraw, 40, 0, 100); break;
case COMBOBOX: GuiComboBox(recDraw, list, 3, 1); break;
case CHECKBOX: GuiCheckBox(recDraw, false); break;
case TEXTBOX: GuiTextBox(recDraw, "test text", 32); break;
case LISTVIEW: GuiListView(recDraw, guiControls, 14, 1); break;
case COLORPICKER: GuiColorPicker(recDraw, RED); break;
default: break;
}
for (int i = 0; i < controlsCounter; i++)
{
// Draws the Controls when placed on the grid.
switch (layout[i].type)
{
case LABEL: GuiLabel(layout[i].rec, "TEXT SAMPLE"); break;
case BUTTON: GuiButton(layout[i].rec, "BUTTON"); break;
case IMAGEBUTTON: GuiImageButton(layout[i].rec, texture); break;
case TOGGLE: GuiToggleButton(layout[i].rec, "TOGGLE", false); break;
case TOGGLEGROUP: GuiToggleGroup(layout[i].rec, list, 3, 1); break;
case SLIDER: GuiSlider(layout[i].rec, 40, 0, 100); break;
@ -252,10 +468,21 @@ int main()
case COMBOBOX: GuiComboBox(layout[i].rec, list, 3, 1); break;
case CHECKBOX: GuiCheckBox(layout[i].rec, false); break;
case TEXTBOX: GuiTextBox(layout[i].rec, "test text", 32); break;
case LISTVIEW: GuiListView(layout[i].rec, guiControls, 14, 1); break;
case COLORPICKER: GuiColorPicker(layout[i].rec, RED); break;
default: break;
}
}
// Draw the list of controls
DrawRectangleRec(listViewControls, Fade(WHITE, 0.7f));
selectedType = GuiListView(listViewControls, guiControls, 14, selectedType);
// Draw the list of controlsCounter
DrawRectangleRec(listViewControlsCounter, Fade(WHITE, 0.7f));
GuiListView(listViewControlsCounter, guiControlsCounter, controlsCounter, -1);
if ((controlSelected != -1) && (controlSelected < controlsCounter)) DrawRectangleRec(layout[controlSelected].rec, Fade(RED, 0.5f));
// Debug information