1. Widgets
  2. Switch

Widgets

Switch

The switch widget is a toggleable control that allows users to switch between two states (on/off). The configuration is at gmwSwitch, see gmwSwitchTheme.

You draw a switch with gmw_switch. The function returns 1 when the switch is toggled to the "on" position and 0 when it's in the "off" position. Like other widgets, you can also use the animated version gmw_switch_anim for smooth visual transitions.

Example

        #include <gama.h>

int light_on = 0; // Variable to store switch state
double light_switch_anim;

int main() {
    gm_init(500, 500, "Switch Example");
    
    do {
        // Draw the switch and update its state
         gmw_switch(0, 0.3, 0.2, 0.1, &light_on);
     // OR
         gmw_switch_anim(0, 0.3, 0.2, 0.1, &light_on, &ligh_switch_anim);
        
        // Draw based on switch state
        if(light_on) {
            gm_draw_text(-0.9, 0.8, "Light: ON", "", 0.1, GM_WHITE);
            gm_draw_circle(0, -0.2, 0.1, GM_YELLOW); // Light bulb on
        } else {
            gm_draw_text(-0.9, 0.8, "Light: OFF", "", 0.1, GM_GRAY);
            gm_draw_circle(0, -0.2, 0.1, GM_DARK_GRAY); // Light bulb off
        }
        
    } while(gm_yield());
    
    return 0;
}

      

Configuration

The switch appearance and behavior can be customized through the gmwSwitch global variable:

        // Change the switch colors when off
gmwSwitch.off.background = GM_RED;
gmwSwitch.off.border = GM_DARK_RED;

// Change the switch colors when on
gmwSwitch.on.background = GM_GREEN;
gmwSwitch.on.border = GM_DARK_GREEN;

// Adjust scale
gmwSwitch.scale = 1.2;

      

Tips

  • The switch returns its current state (1 for on, 0 for off)
  • Use the animated version for smoother visual transitions
  • The fourth parameter controls the size of the switch area
  • The switch automatically toggles when clicked

Reference

switch.h reference