View Single Post
Old 10-17-2015, 03:33 AM   #71
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,687
Default User Interface: Drawing a "Button"

Drawing a Button with some special functions.

The caption text is automatically sized according to the button dimensions (a very non-perfect algorithm)

The button text color is given in a single variable: just eight colors.

The color of the button is set by gfx_r, gfx_g, gfx_b before calling the function.

-Michael


Code:
function button(x,y,w,h,tc,s) (
  radius1 = 10;
  gfx_roundrect(x,y,w,h,radius1);
  gfx_roundrect(x+1,y+1,w-2,h-2,radius1-2);
  gfx_roundrect(x+2,y+2,w-4,h-4,radius1-3);
  gfx_roundrect(x+3,y+3,w-6,h-6,radius1-4);
  gfx_rect(x+4, y+4, w-6, h-6);
  stack_push(gfx_r);
  stack_push(gfx_g);
  stack_push(gfx_b);
  gfx_r = tc & 1;
  gfx_g = (tc >> 1) & 1;
  gfx_b = (tc >> 2) & 1;
  w > h ? w = h;
  gfx_setfont(1,"Arial",w,'b');  
  gfx_x = x + (w - gfx_texth/2) / 2;
  gfx_y = y + (h - gfx_texth) / 2; 
  gfx_drawstr(s);
  stack_pop(gfx_b);
  stack_pop(gfx_g);
  stack_pop(gfx_r);
);
In a User interface I caught mouse clicks on a button simply by

Code:
mouse_cap == 1 ? (
  m_cap == 0 ? (
    m_cap = 1 ;
    m_x = mouse_x;
    m_y = mouse_y;
  
    (m_x>button_x) && (m_x<button_x+button_width) && 
    (m_y>button_y) && (m_y<button_y+button_height) ? (
     
       // place on-click code here
     
    );
   ); 
 ) : (
  m_cap = 0;
);
If you want to do multiple buttons it would be appropriate to manage an array of button area definitions by some button_create() function and loop though this array.

Maybe an object-like code using namespaces can be done, as well.

edit: improved code using the button text as a parameter.

-Michael

Last edited by mschnell; 10-25-2015 at 03:12 PM.
mschnell is offline   Reply With Quote