Advertisement
tempneff

Dropdown menu

Nov 16th, 2020
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Class creates a dropdown menu of n options
  2. the texture for the input arg should be a sprite of the same dimensions as the menua you are making...I fully realize this
  3. is a clunkcy way to do that..
  4. for example, in the scene where I call this Class I have
  5. //        var graphics = this.make.graphics();
  6. //          graphics.fillStyle(0xe4e4e4, 1);
  7. //          graphics.fillRoundedRect(0, 0, this.boxLength,this.boxHeight,2);
  8. //          graphics.generateTexture("box", this.boxLength,this.boxHeight);
  9. //          graphics.destroy();
  10. hope that makes sense
  11. I call it like this var dropMenu = new Menu({scene: this, x:390, y:275, key: "box",textColor: "#000", boxHeight:19, boxLength:108,options:menu,hoverTint: 0x87ceeb, startValue: ["Jack"], buttonFill: 0xe4e4e4})      
  12. where options are:  var options = ["Jack", "Mike", "Jeremy","Ngara", "Billy", "Dillon", "David"];
  13. get the selected values from data output or output variable
  14.  
  15. */
  16.  
  17. class Menu extends Phaser.GameObjects.Sprite{
  18.     constructor(config)
  19.     {
  20.             super(config.scene, config.x, config.y, config.key, config.options, config.boxHeight, config.boxLength, config.hoverTint, config.textColor, config.startValue, config.buttonFill);
  21.             config.scene.add.existing(this);// add to exisiting scene
  22.             config.scene.events.on('update', (time, delta) => { this.update(time, delta)}); // force parent scebe to call class update
  23.             this.scene = config.scene;// reference parent scene
  24.             this.x = config.x;// x
  25.             this.y = config.y;// y
  26.             this.key = config.key;// texture
  27.             this.options = config.options;// an array for the text labels for the menu buttons
  28.             this.output=config.startValue;// default meu value
  29.             this.boxHeight = config.boxHeight;// thickness of menu
  30.             this.boxLength = config.boxLength;// length of menu
  31.             this.textColor = config.textColor;// color of the text
  32.             this.hoverTint = config.hoverTint;// color to highlight during hover
  33.             this.buttonFill = config.buttonFill;
  34.             this.show = true;// bool that toggles menu visibility
  35.             this.setInteractive(); //main button interactive
  36.             this.drops = []; //array to hold menu sprites
  37.             this.create(); // create stuff
  38.  
  39.     }
  40. create(){
  41.     this.setDataEnabled();// data hold the selected menu button
  42.     this.data.set('option', this.output);
  43.     this.data.set('index', 0);
  44.     this.data.set('last option', this.output);
  45.    
  46.     for(var i=0; i<this.options.length; i++){ //loop that creates all the buttons and offsets the position
  47.          this.drops[i]=this.makeButton(this.scene, this.x,this.y-(i*this.boxHeight)-this.boxHeight,this.options[i], this.hoverTint, i);
  48.         }
  49.  
  50.     var graphics2 = this.scene.make.graphics();// graphic to create border sprite for main button
  51.     graphics2.lineStyle(1, 0x333333, 0.7); //border parameters
  52.     graphics2.strokeRoundedRect(0, 0, this.boxLength,this.boxHeight,2); //draw the shape
  53.     graphics2.generateTexture("mainOutline", this.boxLength, this.boxHeight);//turn it into a texture
  54.     graphics2.destroy();//destroy it to save resources
  55.  
  56.     this.text = this.scene.add.text(this.x-50,this.y-8,this.options[0],{color: this.textColor, fontFamily: "Arial", fontSize: "12px"});//label text for main button
  57.     var mainOutline = this.scene.add.image(this.x,this.y, "mainOutline").setScale(1.01).setAlpha(1);//boder for main button
  58.  
  59.     this.on("pointerover", (pointer)=>{
  60.         mainOutline.setTintFill(0xff0000);//hover effect on border
  61.     },this.scene);
  62.  
  63.     this.on("pointerdown",(pointer)=>{
  64.         this.show=!this.show;       //toggles show or hide the menu
  65.         mainOutline.clearTint()     //vlears border tint
  66.         for(var i=0;i<this.drops.length;i++){  //checks for which button is currently selected and tints it
  67.             if(this.drops[i][2].text==this.text.text){
  68.                 this.drops[i][0].setTint(this.hoverTint);
  69.             }
  70.             else{
  71.                 this.drops[i][0].clearTint(); //clears all others
  72.             }}
  73.     },this.scene)
  74.  
  75.     this.on("pointerout", (pointer)=>{  //clears hover tint
  76.         mainOutline.clearTint()
  77.     },this.scene);
  78.  
  79. }
  80.  
  81. update(){                           // loops through to show main button or show menu
  82.     for(var i=0;i<this.drops.length;i++){
  83.             for(var j=0;j<this.drops[i].length;j++){
  84.             this.drops[i][j].setVisible(!this.show)
  85.          }
  86.     }
  87.     this.text.setText(this.output[0]);// sets main button text to match selected text
  88.    
  89.    
  90. }
  91.  
  92.     makeButton(_this,x,y,label,tint, index){
  93.         var d = new Date();
  94.         var n = d.getMilliseconds();// grab date string to ensure unique names for the generated textures
  95.         var graphics = _this.make.graphics(); //grapihics object to create sprites
  96.         graphics.fillStyle(this.buttonFill, 1); //button params
  97.         graphics.fillRoundedRect(0, 0, this.boxLength,this.boxHeight,2); //draw the shape
  98.         graphics.generateTexture("box"+label, this.boxLength, this.boxHeight);  //turn it into texture
  99.         graphics.destroy(); //destroy it
  100.         var graphics2 = _this.make.graphics(); // grab date string to ensure unique names for the generated textures
  101.         graphics2.lineStyle(1, 0x333333, 0.7); //grapihics object to create sprites
  102.         graphics2.strokeRoundedRect(0, 0, this.boxLength,this.boxHeight,2); //draw it
  103.         graphics2.generateTexture("outline"+label, this.boxLength, this.boxHeight); //turn it into texture
  104.         graphics2.destroy(); //kill it
  105.         var sprite = _this.add.image(x, y, "box"+label).setInteractive();  //button is interactive
  106.         var text = _this.add.text(x-35,y-8,label,{color: this.textColor, fontFamily: "Arial", fontSize: "12px"});  //text params ..
  107.         var outline = _this.add.image(x,y, "outline"+label).setScale(1.01).setAlpha(0.4); //border
  108.         sprite.on("pointerdown",(pointer)=>{
  109.             sprite.setTint(tint);               //flash a tint on pointerdown
  110.             this.show = !this.show;            //toggle visibility bool
  111.             this.output = [label,index];        //set ouptu vales
  112.             this.data.set('last option',this.data.get('option')); //set data
  113.             this.data.set('option', label);
  114.             this.data.set('index', index);
  115.  
  116.             //tint and hover over out effects
  117.         })
  118.         sprite.on("pointerup",(pointer)=>{
  119.                sprite.clearTint();
  120.         },_this);
  121.  
  122.         sprite.on("pointerout",(pointer)=>{
  123.             sprite.clearTint();
  124.         },_this);
  125.  
  126.         sprite.on("pointerover",(pointer)=>{
  127.             sprite.setTint(tint);
  128.        
  129.         },_this);
  130.         return [sprite,outline, text]//, spriteBack
  131.     }
  132.  
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement