1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple C++ switch

Discussion in 'Prepar3D' started by Capn_Geoff, 19 Jun 2017.

  1. Capn_Geoff

    Capn_Geoff

    Joined:
    15 Jan 2007
    Messages:
    74
    I use a ton of very simple 2 position switches. In the old days of FSX
    if (underway_switch_sw == 1) {
    SHOW_LISTELEMENT(pgauge->elements_list[0], 2);
    HIDE_LISTELEMENT(pgauge->elements_list[0], 1);
    }

    this would work to switch the visuals of the switch. But there is no SHOW_LISTELEMENT in P3D.
    The only thing I can find that's close is:
    HIDE_IMAGE(underway_switch_plist1[0]);
    SHOW_IMAGE(underway_switch_plist2[0]);

    But these two functions want an element - whatever type that is (I haven't a clue). I've tried using just about anything the compiler wouldn't complain about. The only example I've found is from the SDK:
    if(gs_var.var_value.n == 0)
    {
    HIDE_IMAGE((&gs_slider));
    HIDE_IMAGE((&gs_background));
    }

    But there is no clue as to what gs_slider or gs_background are. Any help greatly appreciated!
     
  2. razorvans

    razorvans

    Joined:
    16 Jan 2014
    Messages:
    8
    You could try instead of using the SHOW/HIDE LISTELEMENT. Change your object from STATIC ELEMENT to ICON.

    Cheers

    Mauricio
     
  3. ddawson

    ddawson Resource contributor

    Joined:
    27 Sep 2006
    Messages:
    610
    Country:
    canada
    The best place for the SHOW_IMAGE and HIDE_IMAGE macros is in the callback function for the element in question.
    Something like this:

    double FSAPI callback5( PELEMENT_ICON pelement)
    {
    if ( active == 0 ) { HIDE_IMAGE(pelement) ; }
    else { SHOW_IMAGE(pelement) ; }
    return pelement->source_var.var_value.n;
    }
     
  4. Capn_Geoff

    Capn_Geoff

    Joined:
    15 Jan 2007
    Messages:
    74
    Without going into too much boring detail below is, I think, the relevant code.
    A couple of things to note: First - I can change the appearance of the switch when you switch to the panel that it is on at first, but it will not change after the first time. I can set it to on, off, or both visually. Second I know the code for the Show/Hide Image is being executed because I put print statement in both side of the switch update (callback). Any idea why the image refuses to change?

    GAUGE_CALLBACK underway_switch_update;
    GAUGE_HEADER_FS700(GAUGE_W, underway_switch_gauge_name, &underway_switch_list, underway_switch_mouse_rect,underway_switch_update, 0,0,0 );



    MOUSE_BEGIN(underway_switch_mouse_rect, 0, 0, 0)
    MOUSE_CHILD_FUNCT(0, 0, 50, 75, CURSOR_CROSSHAIR, MOUSE_LEFTSINGLE, underway_switch_mcb)​
    MOUSE_END


    MAKE_ICON(underway_switch_off_icon,
    PANEL_UNDERWAY_SW_OFF,
    NULL,
    NULL,
    IMAGE_USE_TRANSPARENCY | IMAGE_USE_ERASE,
    0,
    0, 0,
    MODULE_VAR_NONE, NULL,
    ICON_SWITCH_TYPE_SET_CUR_ICON,
    1, //1 original
    0,
    0)​
    PELEMENT_HEADER underway_switch_plist1[] = {&underway_switch_off_icon.header,NULL};

    MAKE_ICON(underway_switch_on_icon,
    PANEL_UNDERWAY_SW_ON,
    underway_switch_plist1,
    NULL,
    IMAGE_USE_TRANSPARENCY | IMAGE_USE_ERASE,
    0,
    0, 0,
    MODULE_VAR_NONE, NULL,
    ICON_SWITCH_TYPE_SET_CUR_ICON,
    1, //1 original
    0,
    0)​

    PELEMENT_HEADER underway_switch_plist2[] = { &underway_switch_on_icon.header,NULL };


    MAKE_STATIC ( underway_switch_off_static,
    PANEL_UNDERWAY_SW_OFF, //PANEL_SW_BG, //PANEL_UNDERWAY_SW_OFF, //PANEL_UNDERWAY_SW_OFF, //PANEL_SW_BG, //LAUNCH_LIGHT_ON_BMP, //PANEL_UNDERWAY_SW_OFF,
    underway_switch_plist2,
    NULL,
    IMAGE_USE_TRANSPARENCY, // | IMAGE_USE_ERASE, //erase | transparent,
    0,
    0,0 )​

    PELEMENT_HEADER underway_switch_list = &underway_switch_off_static.header;

    void FSAPI underway_switch_update(PGAUGEHDR pgauge, SINT32 service_id, UINT_PTR extra_data) {
    if (underway_switch_sw == 1) {
    HIDE_IMAGE((&underway_switch_off_icon));
    SHOW_IMAGE((&underway_switch_on_icon));​
    } else {
    HIDE_IMAGE((&underway_switch_on_icon));
    SHOW_IMAGE((&underway_switch_off_icon));​
    }​
    }
     
  5. ddawson

    ddawson Resource contributor

    Joined:
    27 Sep 2006
    Messages:
    610
    Country:
    canada
    If you #define PANEL_UNDERWAY_SW_OFF and PANEL_UNDERWAY_SW_ON with consecutive resource numbers, you can do this and the work will be done for you by the callback function:

    FLOAT64 FSAPI switch0_icon_cb( PELEMENT_ICON pelement)
    {
    return (FLOAT64)underway_switch_sw;
    }

    MAKE_ICON(underway_switch_off_icon,PANEL_UNDERWAY_SW_OFF,NULL,NULL,IMAGE_USE_ERASE | IMAGE_USE_TRANSPARENCY ,0,0,0,MODULE_VAR_NONE,
    switch0_icon_cb,ICON_SWITCH_TYPE_STEP_TO,
    2,0,0)

    PELEMENT_HEADER underway_switch_plist1[] = {&underway_switch_off_icon.header,NULL};

    MAKE_STATIC ( underway_switch_off_static,
    PANEL_UNDERWAY_SW_OFF, //PANEL_SW_BG, //PANEL_UNDERWAY_SW_OFF, //PANEL_UNDERWAY_SW_OFF, //PANEL_SW_BG, //LAUNCH_LIGHT_ON_BMP, //PANEL_UNDERWAY_SW_OFF,
    underway_switch_plist1,
    NULL,
    IMAGE_USE_TRANSPARENCY, // | IMAGE_USE_ERASE, //erase | transparent,
    0,
    0,0 )

    PELEMENT_HEADER underway_switch_list = &underway_switch_off_static.header;
     
  6. Capn_Geoff

    Capn_Geoff

    Joined:
    15 Jan 2007
    Messages:
    74
    Wow! What an idiot! I tried using a multi switch, but somehow I missed that the MAKE_ICON had a separate callback. Jeeze - 5 days wasted. Well not really, I learned more than I cared to about ICONs. In your earlier post I was like "What - how do you return a float from a void callback? And only one argument?" Now I see it. Thank you so very much! Now I can get on with the FSX to P3dv4 conversion.

    SOLVED!
     

Share This Page