Skip to content

Commit

Permalink
BGUI changes
Browse files Browse the repository at this point in the history
-Added callbacks for all widgets based on events. So you can have a
callback for gui_event_pressed or another one for gui_event_hover.
-Added missing getters for some widgets.
-Fixed window event propagation. You shouldn't be able to press buttons
below another window now.
-Windows now get reordered when press on. So the one you press gets
pushed to top.
-Tab fixes for GSstdraw.cpp
-Quadratic bezier curve now uses width given by draw_set_curve_width().
Now I will try to implement that for all curves.
  • Loading branch information
TheExDeus committed Jan 16, 2015
1 parent 7f11034 commit 6eabf52
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 85 deletions.
71 changes: 52 additions & 19 deletions ENIGMAsystem/SHELL/Graphics_Systems/General/GScurves.cpp
Expand Up @@ -37,7 +37,7 @@ namespace enigma{
int pr_curve_detail = 20;
int pr_curve_mode = GL_LINE_STRIP;
int pr_spline_points = 0;
int pr_curve_width = 1;
gs_scalar pr_curve_width = 1;

struct splinePoint {
gs_scalar x,y,al;
Expand All @@ -50,9 +50,9 @@ static std::stack< int > startedSplinesMode;
namespace enigma_user
{

void draw_set_curve_width(int width)
void draw_set_curve_width(gs_scalar width)
{
pr_curve_width=width;
pr_curve_width = (width<1.0?1.0:width);
}

void draw_set_curve_mode(int mode)
Expand All @@ -62,7 +62,7 @@ void draw_set_curve_mode(int mode)

void draw_set_curve_detail(int detail)
{
pr_curve_detail = detail;
pr_curve_detail = (detail<1?1:detail);
}

void draw_bezier_quadratic(gs_scalar x1, gs_scalar y1, gs_scalar x2, gs_scalar y2, gs_scalar x3, gs_scalar y3)
Expand Down Expand Up @@ -127,20 +127,53 @@ void draw_bezier_cubic_color(gs_scalar x1, gs_scalar y1, gs_scalar x2, gs_scalar
int col;
gs_scalar x, y, al;
float a2, b2, a = 1.0, b = 0.0, det = 1.0/(gs_scalar)pr_curve_detail;
draw_primitive_begin(pr_curve_mode);
for(int i = 0; i <= pr_curve_detail; ++i)
{
a2 = a*a; b2 = b*b;
x = x1*a2*a + x2*3*a2*b + x3*3*a*b2 + x4*b2*b;
y = y1*a2*a + y2*3*a2*b + y3*3*a*b2 + y4*b2*b;
al = al1 + (al2-al1)*b;
col = merge_color(c1, c2, b);
draw_vertex_color(x, y,col,al);

a -= det;
b = 1.0 - a;
if (pr_curve_width == 1){
draw_primitive_begin(pr_curve_mode);
for(int i = 0; i <= pr_curve_detail; ++i)
{
a2 = a*a; b2 = b*b;
x = x1*a2*a + x2*3*a2*b + x3*3*a*b2 + x4*b2*b;
y = y1*a2*a + y2*3*a2*b + y3*3*a*b2 + y4*b2*b;
al = al1 + (al2-al1)*b;
col = merge_color(c1, c2, b);
draw_vertex_color(x, y,col,al);

a -= det;
b = 1.0 - a;
}
draw_primitive_end();
}else{ //Thick line
gs_scalar dw = pr_curve_width/2.0;
gs_scalar px, py;
x = x1, y = y1;
a -= det;
b = 1.0 - a;
al = al1, col = c1;

draw_primitive_begin(pr_trianglestrip);
for(int i = 1; i <= pr_curve_detail; ++i)
{
a2 = a*a; b2 = b*b;
px = x, py = y;
x = x1*a2*a + x2*3*a2*b + x3*3*a*b2 + x4*b2*b;
y = y1*a2*a + y2*3*a2*b + y3*3*a*b2 + y4*b2*b;
double dir = fmod(atan2(py-y,x-px)+2*M_PI,2*M_PI);
double cv = cos(dir-M_PI/2.0), sv = -sin(dir-M_PI/2.0);
draw_vertex_color(px+dw*cv, py+dw*sv, col, al);
draw_vertex_color(px-dw*cv, py-dw*sv, col, al);

al = al1 + (al2-al1)*b;
col = merge_color(c1, c2, b);

draw_vertex_color(x+dw*cv, y+dw*sv, col, al);
draw_vertex_color(x-dw*cv, y-dw*sv, col, al);

a -= det;
b = 1.0 - a;
}
draw_primitive_end();
}
draw_primitive_end();
}

//NOTICE:
Expand Down Expand Up @@ -172,7 +205,7 @@ gs_scalar draw_bezier_cubic_y(gs_scalar x1, gs_scalar y1, gs_scalar x2, gs_scala
}

//The following function is used in other drawing functions for all splines
//it takes 4 points. Two control points which are at the beggining and the end, and the two points which it actually draws through
//it takes 4 points. Two control points which are at the beginning and the end, and the two points which it actually draws through
//in the middle
void draw_spline_part(gs_scalar x1, gs_scalar y1, gs_scalar x2, gs_scalar y2, gs_scalar x3, gs_scalar y3, gs_scalar x4, gs_scalar y4)
{
Expand Down Expand Up @@ -239,7 +272,7 @@ void draw_spline3_color(gs_scalar x1, gs_scalar y1, gs_scalar x2, gs_scalar y2,
al1 = a1 + (a2-a1)*0.5;
draw_primitive_begin(pr_curve_mode);
//As I need 4 points to draw 1 line, then I will just take 2 control points from the existing ones
//Color and alpha is interpolated in the middle for now (so it doesn't take into account length of each seperate segment)
//Color and alpha is interpolated in the middle for now (so it doesn't take into account length of each separate segment)
draw_spline_part_color(x1, y1, x1, y1, x2, y2, x3, y3, c1, col1, a1, al1);
draw_spline_part_color(x1, y1, x2, y2, x3, y3, x3, y3, col1, c2, al1, a2);
draw_primitive_end();
Expand All @@ -263,7 +296,7 @@ void draw_spline3c_color(gs_scalar x1, gs_scalar y1, gs_scalar x2, gs_scalar y2,
al1 = a1 + (a2-a1)*0.5;
draw_primitive_begin(pr_curve_mode);
//As I need 4 points to draw 1 line, then I will just take 2 control points from the existing ones
//Color and alpha is interpolated in the middle for now (so it doesn't take into account length of each seperate segment)
//Color and alpha is interpolated in the middle for now (so it doesn't take into account length of each separate segment)
draw_spline_part_color(x1, y1, x2, y2, x3, y3, x4, y4, c1, col1, a1, al1);
draw_spline_part_color(x2, y2, x3, y3, x4, y4, x5, y5, col1, c2, al1, a2);
draw_primitive_end();
Expand Down
2 changes: 1 addition & 1 deletion ENIGMAsystem/SHELL/Graphics_Systems/General/GScurves.h
Expand Up @@ -29,7 +29,7 @@ void draw_bezier_cubic(gs_scalar x1, gs_scalar y1,gs_scalar x2, gs_scalar y2,gs_
void draw_bezier_cubic_color(gs_scalar x1, gs_scalar y1,gs_scalar x2, gs_scalar y2,gs_scalar x3, gs_scalar y3, gs_scalar x4, gs_scalar y4, int c1, int c2, gs_scalar a1, gs_scalar a2);
void draw_set_curve_mode(int mode);
void draw_set_curve_detail(int detail);
void draw_set_curve_width(int width);
void draw_set_curve_width(gs_scalar width);

gs_scalar draw_bezier_quadratic_x(gs_scalar x1, gs_scalar y1,gs_scalar x2, gs_scalar y2,gs_scalar x3, gs_scalar y3, float t);
gs_scalar draw_bezier_quadratic_y(gs_scalar x1, gs_scalar y1,gs_scalar x2, gs_scalar y2,gs_scalar x3, gs_scalar y3, float t);
Expand Down
38 changes: 19 additions & 19 deletions ENIGMAsystem/SHELL/Graphics_Systems/General/GSstdraw.cpp
Expand Up @@ -66,7 +66,7 @@ void draw_line(gs_scalar x1, gs_scalar y1,gs_scalar x2, gs_scalar y2)

void draw_line_color(gs_scalar x1, gs_scalar y1,gs_scalar x2, gs_scalar y2, int c1, int c2)
{
gs_scalar alpha = draw_get_alpha();
gs_scalar alpha = draw_get_alpha();
draw_primitive_begin(pr_linestrip);
draw_vertex_color(x1, y1, c1, alpha);
draw_vertex_color(x2, y2, c2, alpha);
Expand All @@ -75,29 +75,29 @@ void draw_line_color(gs_scalar x1, gs_scalar y1,gs_scalar x2, gs_scalar y2, int

void draw_line_width(gs_scalar x1, gs_scalar y1,gs_scalar x2, gs_scalar y2, float width)
{
double dir = fmod(atan2(y1-y2,x2-x1)+2*M_PI,2*M_PI);
double cv = cos(dir-M_PI/2.0), sv = -sin(dir-M_PI/2.0);
double dw = width/2.0;
draw_primitive_begin(pr_trianglestrip);
double dir = fmod(atan2(y1-y2,x2-x1)+2*M_PI,2*M_PI);
double cv = cos(dir-M_PI/2.0), sv = -sin(dir-M_PI/2.0);
double dw = width/2.0;
draw_primitive_begin(pr_trianglestrip);
draw_vertex(x1+dw*cv, y1+dw*sv);
draw_vertex(x1-dw*cv, y1-dw*sv);
draw_vertex(x2+dw*cv, y2+dw*sv);
draw_vertex(x2-dw*cv, y2-dw*sv);
draw_vertex(x2+dw*cv, y2+dw*sv);
draw_vertex(x2-dw*cv, y2-dw*sv);
draw_primitive_end();
}

void draw_line_width_color(gs_scalar x1, gs_scalar y1,gs_scalar x2, gs_scalar y2, float width, int c1, int c2)
{
gs_scalar alpha = draw_get_alpha();
double dir = fmod(atan2(y1-y2,x2-x1)+2*M_PI,2*M_PI);
double cv = cos(dir-M_PI/2.0), sv = -sin(dir-M_PI/2.0);
double dw = width/2.0;
draw_primitive_begin(pr_trianglestrip);
draw_vertex_color(x1+dw*cv, y1+dw*sv, c1, alpha);
draw_vertex_color(x1-dw*cv, y1-dw*sv, c1, alpha);
draw_vertex_color(x2+dw*cv, y2+dw*sv, c2, alpha);
draw_vertex_color(x2-dw*cv, y2-dw*sv, c2, alpha);
draw_primitive_end();
gs_scalar alpha = draw_get_alpha();
double dir = fmod(atan2(y1-y2,x2-x1)+2*M_PI,2*M_PI);
double cv = cos(dir-M_PI/2.0), sv = -sin(dir-M_PI/2.0);
double dw = width/2.0;
draw_primitive_begin(pr_trianglestrip);
draw_vertex_color(x1+dw*cv, y1+dw*sv, c1, alpha);
draw_vertex_color(x1-dw*cv, y1-dw*sv, c1, alpha);
draw_vertex_color(x2+dw*cv, y2+dw*sv, c2, alpha);
draw_vertex_color(x2-dw*cv, y2-dw*sv, c2, alpha);
draw_primitive_end();
}

void draw_rectangle(gs_scalar x1, gs_scalar y1,gs_scalar x2, gs_scalar y2, bool outline)
Expand Down Expand Up @@ -360,9 +360,9 @@ void draw_sector(gs_scalar x, gs_scalar y, gs_scalar rx, gs_scalar ry, float a1,
// TODO(JoshDreamland): Replace with settings macro to get from preferred unit to radians
a1 *= M_PI/180;
a2 *= M_PI/180;

gs_scalar pr = 2*M_PI/enigma::circleprecision;

if (outline) {
draw_primitive_begin(pr_linestrip);
draw_vertex(x, y);
Expand Down
Expand Up @@ -57,18 +57,27 @@ namespace gui
style_id = gui_style_button; //Default style
enigma_user::gui_style_set_font_halign(style_id, enigma_user::gui_state_all, enigma_user::fa_center);
enigma_user::gui_style_set_font_valign(style_id, enigma_user::gui_state_all, enigma_user::fa_middle);
callback.fill(-1); //Default callbacks don't exist (so it doesn't call any script)
}

void gui_button::callback_execute(int event){
if (callback[event] != -1){
enigma_user::script_execute(callback[event], id, active, state, event);
}
}

//Update all possible button states (hover, click, toggle etc.)
void gui_button::update(gs_scalar ox, gs_scalar oy, gs_scalar tx, gs_scalar ty){
if (box.point_inside(tx-ox,ty-oy) && gui::windowStopPropagation == false){
callback_execute(enigma_user::gui_event_hover);
gui::windowStopPropagation = true;
if (enigma_user::mouse_check_button_pressed(enigma_user::mb_left)){
if (active == false){
state = enigma_user::gui_state_active;
}else{
state = enigma_user::gui_state_on_active;
}
callback_execute(enigma_user::gui_event_pressed);
}else{
if (state != enigma_user::gui_state_active && state != enigma_user::gui_state_on_active){
if (active == false){
Expand All @@ -83,9 +92,7 @@ namespace gui
}else{
active = true;
}
if (callback != -1){
enigma_user::script_execute(callback, id, active);
}
callback_execute(enigma_user::gui_event_released);
if (togglable == false){
active = false;
}
Expand Down Expand Up @@ -193,8 +200,12 @@ namespace enigma_user
gui::gui_buttons[id].update_text_pos();
}

void gui_button_set_callback(int id, int script_id){
gui::gui_buttons[id].callback = script_id;
void gui_button_set_callback(int id, int event, int script_id){
if (event == enigma_user::gui_event_all){
gui::gui_buttons[id].callback.fill(script_id);
}else{
gui::gui_buttons[id].callback[event] = script_id;
}
}

void gui_button_set_style(int id, int style_id){
Expand Down Expand Up @@ -234,8 +245,12 @@ namespace enigma_user
return gui::gui_buttons[id].visible;
}

int gui_button_get_callback(int id){
return gui::gui_buttons[id].callback;
int gui_button_get_callback(int id, int event){
return gui::gui_buttons[id].callback[event];
}

int gui_button_get_parent(int id){
return gui::gui_buttons[id].parent_id;
}

gs_scalar gui_button_get_width(int id){
Expand Down
Expand Up @@ -39,7 +39,7 @@ namespace gui
bool visible = true;
bool active = false; //Is button pressed
bool togglable = false; //Is button a toggle button
int callback = -1; //Script to run when clicked
array<int,4> callback; //Script to run on event

int parent_id = -1; //ID of the parent of some kind (probably a window). It won't render with gui_draw_buttons() if it is.

Expand All @@ -51,6 +51,7 @@ namespace gui
void update(gs_scalar ox = 0, gs_scalar oy = 0, gs_scalar tx = enigma_user::mouse_x, gs_scalar ty = enigma_user::mouse_y);
void draw(gs_scalar ox = 0, gs_scalar oy = 0);
void update_text_pos(int state = -1);
void callback_execute(int event);
};
}

Expand Down
27 changes: 19 additions & 8 deletions ENIGMAsystem/SHELL/Universal_System/Extensions/BasicGUI/include.h
Expand Up @@ -27,6 +27,7 @@ using std::pair;

namespace enigma_user
{
///NOTE: CALLBACKS AND STYLES CAN BE A SECURITY RISK IF STATE IS NOT THE DEFINED ENUM's. NO CHECKING TAKES PLACE DURING RUNTIME
enum {
gui_state_default,
gui_state_on,
Expand All @@ -37,6 +38,14 @@ namespace enigma_user
gui_state_all
};

enum {
gui_event_pressed,
gui_event_released,
gui_event_hover,
gui_event_drag,
gui_event_all
};

///BUTTONS
int gui_button_create(gs_scalar x, gs_scalar y, gs_scalar w, gs_scalar h, string text);
int gui_button_create();
Expand All @@ -50,15 +59,16 @@ namespace enigma_user
void gui_button_set_size(int id, gs_scalar w, gs_scalar h);
void gui_button_set_style(int id, int style_id);

void gui_button_set_callback(int id, int script_id);
void gui_button_set_callback(int id, int event, int script_id);
void gui_button_set_togglable(int id, bool togglable);
void gui_button_set_visible(int id, bool visible);
void gui_button_set_active(int id, bool active);

//Getters
int gui_button_get_style(int id);
int gui_button_get_state(int id);
int gui_button_get_callback(int id);
int gui_button_get_callback(int id, int event);
int gui_button_get_parent(int id);
bool gui_button_get_active(int id);
bool gui_button_get_visible(int id);
bool gui_button_get_togglable(int id);
Expand All @@ -84,14 +94,15 @@ namespace enigma_user
void gui_toggle_set_size(int id, gs_scalar w, gs_scalar h);
void gui_toggle_set_style(int id, int style_id);

void gui_toggle_set_callback(int id, int script_id);
void gui_toggle_set_callback(int id, int event, int script_id);
void gui_toggle_set_visible(int id, bool visible);
void gui_toggle_set_active(int id, bool active);

//Getters
int gui_toggle_get_style(int id);
int gui_toggle_get_state(int id);
int gui_toggle_get_callback(int id);
int gui_toggle_get_callback(int id, int event);
int gui_toggle_get_parent(int id);
bool gui_toggle_get_active(int id);
bool gui_toggle_get_visible(int id);
gs_scalar gui_toggle_get_width(int id);
Expand All @@ -117,7 +128,7 @@ namespace enigma_user
void gui_slider_set_style(int id, int style_id);
void gui_slider_set_indicator_style(int id, int indicator_style_id);

void gui_slider_set_callback(int id, int script_id);
void gui_slider_set_callback(int id, int event, int script_id);
void gui_slider_set_visible(int id, bool visible);
void gui_slider_set_active(int id, bool active);
void gui_slider_set_value(int id, double value);
Expand All @@ -130,7 +141,7 @@ namespace enigma_user
int gui_slider_get_indicator_style(int id);

int gui_slider_get_state(int id);
int gui_slider_get_callback(int id);
int gui_slider_get_callback(int id, int event);
bool gui_slider_get_active(int id);
bool gui_slider_get_visible(int id);
gs_scalar gui_slider_get_width(int id);
Expand Down Expand Up @@ -160,7 +171,7 @@ namespace enigma_user

//Setters
void gui_window_set_style(int id, int style_id);
void gui_window_set_callback(int id, int script_id);
void gui_window_set_callback(int id, int event, int script_id);
void gui_window_set_draggable(int id, bool draggable);
void gui_window_set_visible(int id, bool visible);

Expand All @@ -172,7 +183,7 @@ namespace enigma_user
//Getters
int gui_window_get_style(int id);
int gui_window_get_state(int id);
int gui_window_get_callback(int id);
int gui_window_get_callback(int id, int event);
bool gui_window_get_draggable(int id);
bool gui_window_get_visible(int id);
gs_scalar gui_window_get_width(int id);
Expand Down

0 comments on commit 6eabf52

Please sign in to comment.