Advertisement
snake5

new test GUI code

Dec 13th, 2015
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. _G.
  2. {
  3.     tex_background = TEXTURE("ui/bg1.png"),
  4.     tex_button_core_n = TEXTURE("ui/button_core.png"),
  5.     tex_button_core_h = TEXTURE("ui/button_core_h.png"),
  6.     tex_button_core_c = TEXTURE("ui/button_core_c.png"),
  7.     tex_button_core_f = TEXTURE("ui/button_core_f.png"),
  8. };
  9.  
  10. function ButtonShader( ctrl )
  11. {
  12.     tex = if( ctrl.clickedL, tex_button_core_c,
  13.         if( ctrl.hover, tex_button_core_h, tex_button_core_n ) );
  14.     ctrl.DTex( tex );
  15.     ctrl.DButton( 0, 0, ctrl.width, ctrl.height, 4, 4/128 );
  16.     ctrl.DFont( "core", ctrl.height / 2 );
  17.     ctrl.DText( ctrl.text, ctrl.width / 2,
  18.         ctrl.height / 2 + ctrl.InvIS( ctrl.clickedL ),
  19.         HALIGN_CENTER, VALIGN_CENTER );
  20.     if( ctrl.focused )
  21.     {
  22.         ctrl.DTex( tex_button_core_f );
  23.         ctrl.DButton( 0, 0, ctrl.width, ctrl.height, 4, 4/128 );
  24.     }
  25. }
  26.  
  27. function CreateButton( x, y, w, h, text )
  28. {
  29.     ctrl = this.CreateControl( x, y, w, h );
  30.     ocb = ctrl.eventCallback;
  31.     function button_callback( ev ) use( ocb )
  32.     {
  33.         if( ev.type == GUI_Event_BtnUp )
  34.         {
  35.             if( this.Hit( ev.x, ev.y ) && ev.button == GUI_MB_Left )
  36.                 this.InvokeCallbacks( "onclick" );
  37.             return 1;
  38.         }
  39.         return this!ocb( ev );
  40.     }
  41.     ctrl.eventCallback = button_callback;
  42.     ctrl.shaders.push( ButtonShader );
  43.     ctrl.text = text;
  44.     ctrl.focusable = true;
  45.     return ctrl;
  46. }
  47.  
  48. function YesNoQuestion( text )
  49. {
  50.     answer = null;
  51.     factor = 0.0;
  52.    
  53.     scr_overlay = ROOT.CreateScreen( GUI_ScrMode_Crop, 100, 100 ).{ z = 1000 };
  54.     scr_overlay.shaders.push(function( ctrl )
  55.     {
  56.         ctrl.DCol( 0, 0.5 );
  57.         ctrl.DQuad( 0, 0, ctrl.width, ctrl.height );
  58.     });
  59.     scr_bgr = ROOT.CreateScreen( GUI_ScrMode_Fit, 200, 100 ).{
  60.         z = 1001, xscale = 0.4, yscale = 0.4, text = text };
  61.     scr_bgr.shaders.push(function( ctrl ) use( factor )
  62.     {
  63.         // background
  64.         ctrl.DCol( 0.1, 0.3, 0.7, 0.5 );
  65.         ctrl.DQuad( 0, 0, 200, 100 );
  66.         // top border
  67.         ctrl.DCol( 0.7, 1 );
  68.         ctrl.DQuad( 0, 0, 200 * factor, 20 );
  69.         ctrl.DCol( 0.8, 1 );
  70.         ctrl.DQuad( 0, 20, 200 * factor, 21 );
  71.         ctrl.DCol( 0.0, 0.2 );
  72.         ctrl.DQuad( 0, 21, 200 * factor, 24 );
  73.         // question text
  74.         ctrl.DCol( 0.02, 1 * factor );
  75.         ctrl.DFont( "core", 10 );
  76.         ctrl.DText( ctrl.text, 5, 10, HALIGN_LEFT, VALIGN_CENTER );
  77.     });
  78.    
  79.     btn_yes = scr_bgr!CreateButton( 10, 70, 60, 20, "Yes" );
  80.     btn_yes.AddCallback( "onclick", function() use(answer){ answer = true; } );
  81.     btn_no = scr_bgr!CreateButton( 200-70, 70, 60, 20, "No" );
  82.     btn_no.AddCallback( "onclick", function() use(answer){ answer = false; } );
  83.    
  84.     SetFocusRoot( scr_bgr );
  85.    
  86.     function min(a,b){ return if(a<b,a,b); }
  87.     while( factor < 1 )
  88.     {
  89.         factor = min( factor + 0.05, 1 );
  90.         yield 0.01;
  91.     }
  92.    
  93.     while( answer === null )
  94.     {
  95.         yield;
  96.     }
  97.    
  98.     return answer;
  99. }
  100.  
  101. global scr_background = ROOT.CreateScreen( GUI_ScrMode_Crop, 512, 512 ).{ z = -1 };
  102. t = 0;
  103. scr_background.shaders.push(function( ctrl, dt ) use( t )
  104. {
  105.     t += dt;
  106.     ctrl.DTex( tex_background );
  107.     ctrl.DQuadExt( 0, 0, 512, 512, t * -0.02 );
  108.     process_threads();
  109. });
  110.  
  111. global scr_mainmenu = ROOT.CreateScreen( GUI_ScrMode_Fit, 100, 100 );
  112. scr_mainmenu.shaders.push(function( ctrl )
  113. {
  114.     ctrl.DCol( 0, 0.5 );
  115.     ctrl.DQuad( 5, 5, ctrl.width - 5, ctrl.height - 5 );
  116. });
  117.  
  118. global scr_button1 = scr_mainmenu!CreateButton( 10, 10, 40, 5, "Click me" );
  119. scr_button1.AddCallback( "onclick", function()
  120. {
  121.     println( "clicked!" );
  122.     thread YesNoQuestion( "Do you want to do something?" );
  123. });
  124.  
  125. global scr_button2 = scr_mainmenu!CreateButton( 10, 20, 40, 5, "Click me 2" );
  126. scr_button2.AddCallback( "onclick", function()
  127. {
  128.     println( "clicked 2!" );
  129.     thread YesNoQuestion( "Do you want to do something?" );
  130. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement