Advertisement
Yankas

Radial Menu v1.0

Nov 9th, 2017
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 16.79 KB | None | 0 0
  1. ########################### Yankas' Radial Menu Script ##############################
  2. ########## VERSION 1.0
  3. #####################################################################################
  4.  
  5. class MenuEntry
  6.     attr_accessor :angle
  7.     attr_reader :name
  8.     attr_reader :icon
  9. end
  10.  
  11. class MenuEntryPokemon < MenuEntry
  12.     def initialize
  13.         @icon = "Graphics/Icons/item267"
  14.         @name = "Pokémon"
  15.     end
  16.     def selected(menu)
  17.         hiddenmove = nil
  18.         pbFadeOutIn(99999){
  19.             sscene = PokemonParty_Scene.new
  20.             sscreen = PokemonPartyScreen.new(sscene,$Trainer.party)
  21.             hiddenmove = sscreen.pbPokemonScreen
  22.             return (hiddenmove)
  23.         }
  24.         if hiddenmove
  25.             $game_temp.in_menu = false
  26.             Kernel.pbUseHiddenMove(hiddenmove[0],hiddenmove[1])
  27.             return
  28.         end
  29.     end
  30. end
  31.  
  32. class MenuEntryPokedex < MenuEntry
  33.     def initialize
  34.         @icon = "Graphics/Icons/item509"
  35.         @name = "Pokédex"
  36.     end
  37.     def selected(menu)
  38.         if $PokemonGlobal.pokedexViable.length==1
  39.             $PokemonGlobal.pokedexDex = $PokemonGlobal.pokedexViable[0]
  40.             $PokemonGlobal.pokedexDex = -1 if $PokemonGlobal.pokedexDex==$PokemonGlobal.pokedexUnlocked.length-1
  41.             pbFadeOutIn(99999){
  42.                   scene = PokemonPokedex_Scene.new
  43.                   screen = PokemonPokedexScreen.new(scene)
  44.                   screen.pbStartScreen
  45.             }
  46.         else
  47.             pbFadeOutIn(99999){
  48.                 scene = PokemonPokedexMenu_Scene.new
  49.                 screen = PokemonPokedexMenuScreen.new(scene)
  50.                 screen.pbStartScreen
  51.             }
  52.         end
  53.         return false
  54.     end
  55. end
  56.  
  57. class MenuEntryBag < MenuEntry
  58.     def initialize
  59.         @icon = "Graphics/Icons/item513"
  60.         @name = "Bag"
  61.     end
  62.     def selected(menu)
  63.         item = 0
  64.         pbFadeOutIn(99999){
  65.             scene = PokemonBag_Scene.new
  66.             screen = PokemonBagScreen.new(scene,$PokemonBag)
  67.             item = screen.pbStartScreen
  68.             return (item>0)
  69.         }
  70.         if item>0
  71.           Kernel.pbUseKeyItemInField(item)
  72.           return true
  73.         end
  74.     end
  75. end
  76.  
  77. class MenuEntryTrainer < MenuEntry
  78.     def initialize
  79.         @icon = "Graphics/Icons/item520"
  80.         @name = $Trainer.name
  81.     end
  82.     def selected(menu)
  83.         pbFadeOutIn(99999){
  84.             scene = PokemonTrainerCard_Scene.new
  85.             screen = PokemonTrainerCardScreen.new(scene)
  86.             screen.pbStartScreen
  87.             return false
  88.         }
  89.     end
  90. end
  91.  
  92. class MenuEntrySave < MenuEntry
  93.     def initialize
  94.         @icon = "Graphics/Icons/item204"
  95.         @name = "Save"
  96.     end
  97.     def selected(menu)
  98.         menu.pbHideMenu(true)
  99.         scene = PokemonSave_Scene.new
  100.         screen = PokemonSaveScreen.new(scene)
  101.         if screen.pbSaveScreen
  102.           return true  
  103.         end
  104.         menu.pbHideMenu(false)
  105.     end
  106. end
  107.  
  108. class MenuEntryQuit < MenuEntry
  109.     def initialize
  110.         @icon = "Graphics/Icons/itemBack"
  111.         @name = "Exit Game"
  112.     end
  113.     def selected(menu)
  114.         menu.pbHideMenu(true)
  115.         if Kernel.pbConfirmMessage(_INTL("Are you sure you want to quit the game?"))
  116.             scene = PokemonSave_Scene.new
  117.             screen = PokemonSaveScreen.new(scene)
  118.             if screen.pbSaveScreen
  119.                 return true
  120.             end
  121.             $scene = nil
  122.             return true
  123.         end
  124.         menu.pbHideMenu(false)
  125.     end
  126. end
  127.  
  128. class MenuEntryPokegear < MenuEntry
  129.     def initialize
  130.         @icon = "Graphics/Icons/itemPokegear"
  131.         @name = "Pokégear"
  132.     end
  133.     def selected(menu)
  134.         pbFadeOutIn(99999){
  135.             scene = PokemonPokegear_Scene.new
  136.             screen = PokemonPokegearScreen.new(scene)
  137.             screen.pbStartScreen
  138.             return false
  139.         }
  140.     end
  141. end
  142.  
  143. class MenuEntryOption < MenuEntry
  144.     def initialize
  145.         @icon = "Graphics/Icons/item207"
  146.         @name = "Options"
  147.     end
  148.     def selected(menu)
  149.         pbFadeOutIn(99999){
  150.             scene = PokemonOption_Scene.new
  151.             screen = PokemonOptionScreen.new(scene)
  152.             screen.pbStartScreen
  153.             return false
  154.         }
  155.     end
  156. end
  157.  
  158. class MenuEntryDebug < MenuEntry
  159.     def initialize
  160.         @icon = "Graphics/Icons/item000"
  161.         @name = "Debug"
  162.     end
  163.     def selected(menu)
  164.         pbFadeOutIn(99999){
  165.           pbDebugMenu
  166.           return false
  167.         }
  168.     end
  169. end
  170.  
  171. class MenuEntryExitSafari < MenuEntry
  172.     def initialize
  173.         @icon = "Graphics/Icons/item009"
  174.         @name = "Quit Safari"
  175.     end
  176.     def selected(menu)
  177.         if pbInSafari?
  178.           if Kernel.pbConfirmMessage(_INTL("Would you like to leave the Safari Game right now?"))
  179.                 menu.pbEndScene
  180.                 pbSafariState.decision = 1
  181.                 pbSafariState.pbGoToStart
  182.                 return false
  183.             end
  184.             return false # Since we already exited the scene, we shouldn't return an exit command.
  185.         end
  186.     end
  187. end
  188.  
  189. class MenuEntryExitBugContest < MenuEntry
  190.     def initialize
  191.         @icon = "Graphics/Icons/item009"
  192.         @name = "Quit Contest"
  193.     end
  194.     def selected(menu)
  195.         if pbInBugContest?
  196.             if Kernel.pbConfirmMessage(_INTL("Would you like to end the Contest now?"))
  197.                 menu.pbEndScene
  198.                 pbBugContestState.pbStartJudging
  199.             end
  200.             return false # Since we already exited the scene, we shouldn't return an exit command.
  201.         end
  202.     end
  203. end
  204.  
  205. class Component
  206.     attr_accessor :viewport
  207.     attr_accessor :sprites
  208.    
  209.     def initialize(viewport, spritehash)
  210.         @viewport = viewport
  211.         @sprites = spritehash
  212.     end
  213.    
  214.     def dispose
  215.         @sprites = nil
  216.     end
  217. end
  218.  
  219. class Clock < Component
  220.     def initialize(viewport, spritehash, format)
  221.         super(viewport, spritehash)
  222.         ############# CLOCK CONFIGURATION SECTION ################
  223.         @timeFormat = "%I:%M %p"
  224.         #### FORMAT         => EXAMPLE ######
  225.         # "%I:%M %p"        => 07:08 pm
  226.         # "%I:%M:%S %p"     => 07:08:20 pm
  227.         # "%H:%M:%S"        => 19:08:20
  228.         # "%H:%M            => 19:08
  229.         ############# END CONFIG#################  
  230.         @time = pbGetTimeNow
  231.         @sprites["clock"] = BitmapSprite.new(256,64,viewport)
  232.         @sprites["clock"].x = 380
  233.         @sprites["clock"].y = (pbInBugContest?) ? (Graphics.height - 64) : 10
  234.         @textColor = Color.new(244,244,244)
  235.         @highlightColor = Color.new(30,30,30)
  236.     end
  237.    
  238.  
  239.     def update
  240.         @time = pbGetTimeNow
  241.         text = @time.strftime(@timeFormat)
  242.         textDay = @time.strftime("%A")
  243.         @sprites["clock"].bitmap.clear
  244.         pbSetSystemFont(@sprites["clock"].bitmap)
  245.         pbDrawTextPositions(@sprites["clock"].bitmap,[[text,0,0,0,@textColor,@highlightColor],[textDay,0,32,0,@textColor,@highlightColor]])
  246.     end
  247. end
  248.  
  249.  
  250. class SafariHud < Component
  251.     def initialize(viewport, spritehash)
  252.         super
  253.         @sprites["safari"] = BitmapSprite.new(256,64,viewport)
  254.         @sprites["safari"].x = 10
  255.         @sprites["safari"].y = 10
  256.         @textColor = Color.new(244,244,244)
  257.         @highlightColor = Color.new(30,30,30)
  258.         @oldText
  259.         @oldText2
  260.     end
  261.    
  262.     def update
  263.         text = _INTL("Balls: {1}",pbSafariState.ballcount)
  264.         text2 = (SAFARISTEPS>0) ? _INTL("Steps: {1}/{2}", pbSafariState.steps,SAFARISTEPS) : ""
  265.        
  266.         if(@oldText != text || @oldText2 != text2)
  267.             @oldText = text
  268.             @oldText2 = text2
  269.             @sprites["safari"].bitmap.clear
  270.             pbSetSystemFont(@sprites["safari"].bitmap)
  271.             pbDrawTextPositions(@sprites["safari"].bitmap,[[text,0,0,0,@textColor,@highlightColor],[text2,0,32,0,@textColor,@highlightColor]])
  272.         end
  273.     end
  274. end
  275.  
  276. class BugContestHud < Component
  277.     def initialize(viewport, spritehash)
  278.         super
  279.         @sprites["safari"] = BitmapSprite.new(256,96,viewport)
  280.         @sprites["safari"].x = 10
  281.         @sprites["safari"].y = 10
  282.         @textColor = Color.new(244,244,244)
  283.         @highlightColor = Color.new(30,30,30)
  284.         @oldText = ""
  285.         @oldText2 = ""
  286.         @oldText3 = ""
  287.     end
  288.    
  289.     def update 
  290.         if(pbBugContestState.lastPokemon)
  291.             text =  _INTL("Caught: {1}", PBSpecies.getName(pbBugContestState.lastPokemon.species))
  292.             text2 =  _INTL("Level: {1}", pbBugContestState.lastPokemon.level)
  293.             text3 =  _INTL("Balls: {1}", pbBugContestState.ballcount)
  294.         else
  295.             text = "Caught: None"
  296.             text2 = _INTL("Balls: {1}",pbBugContestState.ballcount)
  297.             text3 = ""
  298.         end
  299.        
  300.         if(@oldText != text || @oldText2 != text2 || @oldText3 != text3)
  301.             @oldText = text
  302.             @oldText2 = text2
  303.             @oldText3 = text3
  304.             @sprites["safari"].bitmap.clear
  305.             pbSetSystemFont(@sprites["safari"].bitmap)
  306.             pbDrawTextPositions(@sprites["safari"].bitmap,[[text,0,0,0,@textColor,@highlightColor],[text2,0,32,0,@textColor,@highlightColor],[text3,0,64,0,@textColor,@highlightColor]])
  307.         end
  308.     end
  309. end
  310.  
  311. class RadialMenu < Component   
  312.     def initialize(viewport, spritehash, menu)
  313.         super(viewport, spritehash)
  314.         @menu = menu
  315.         ############################ RADIAL CONFIGURATION SECTION ############################
  316.         ####
  317.         @distance = 110 # Distance of menu icons from the center in pixel.
  318.         @iconWidth = 48 # The width of menu icons
  319.         @iconHeight = 48 # The height of menu icons
  320.         @activeIconScale = 1.5 # The active icon is scaled by this factor (1 = no resize, 2 = 200% etc.)
  321.         @activeIconOpacity = 255 # Transparency of active icon; 0=fully transparent, 255=fully solid
  322.         @inactiveIconOpacity = 155 # Transparency of inactive icons; 0=fully, 255=fully solid
  323.         @activeIconTone = Tone.new(0,0,0,0) # Tone (Red, Green, Blue, Grey) shift applied to active icon.
  324.         @inactiveIconTone = Tone.new(0,0,0,0) # Tone (Red, Green, Blue, Grey) shift applied to inactive icon.
  325.         @textColor=Color.new(244,244,244) # The text color of the menu icon's name/description.
  326.         @textHighlight=Color.new(30,30,30) # The highlight (outline) color of the text.
  327.         @movementDuration = 12 # How long the ring menu left/right animation takes in frames.
  328.         @startUpAnimation = 12 # Duration of the menu start-up animation in frames, set to 0 to disable.    
  329.         @soundTurning = "GUI sel cursor" # Sound that plays when selection is changed. 
  330.         # Determines the position of the "active/selected" item. You can set
  331.         # any angle you like (in Radians), or use one of the 4 presets here.
  332.         # TOP (default)     = -(Math::PI/2)
  333.         # RIGHT             = 0
  334.         # LEFT              = Math::PI
  335.         # BOTTOM            = Math::PI/2
  336.         @startingAngle = -(Math::PI/2)
  337.         @buttonTurnCounterClockwise = Input::LEFT # Button used to turn the menu clockwise, default=INPUT::LEFT
  338.         @buttonTurnClockwise = Input::RIGHT
  339.         ####
  340.         ############################ END OF CONFIGURATION ##############################
  341.        
  342.         @entries = []
  343.         @originX = Graphics.width / 2 - @iconWidth / 2
  344.         @originY = Graphics.height / 2 - @iconHeight / 2 - 8
  345.         @animationCounter = @startUpAnimation
  346.         @currentSelection = 0 # The current selection, used as index for @entries
  347.         @angleSize = 0
  348.         @finalDistance = @distance
  349.         @frameAngleShift = 0 # How many frames it takes to turn one time.
  350.        
  351.         # SAFARI    = Bag       Trainer     PokeGear    Quit    Options     Debug       Exit Game
  352.         # BCONTEST  = Pokemon   Trainer     PokeGear    Quit    Options     Debug       Exit Game
  353.         addMenuEntry(MenuEntryPokemon.new) if $Trainer.party.length>0
  354.         addMenuEntry(MenuEntryPokedex.new) if $Trainer.pokedex && $PokemonGlobal.pokedexViable.length>0
  355.         addMenuEntry(MenuEntryBag.new)  if !pbInBugContest?
  356.         addMenuEntry(MenuEntryExitBugContest.new) if pbInBugContest?
  357.         addMenuEntry(MenuEntryExitSafari.new) if pbInSafari?
  358.         addMenuEntry(MenuEntryOption.new)
  359.         addMenuEntry(MenuEntrySave.new)  if !pbInBugContest? && $game_system && !$game_system.save_disabled && !pbInSafari?
  360.         addMenuEntry(MenuEntryQuit.new)
  361.         addMenuEntry(MenuEntryDebug.new)if $DEBUG
  362.         addMenuEntry(MenuEntryTrainer.new)
  363.         addMenuEntry(MenuEntryPokegear.new) if $Trainer.pokegear
  364.        
  365.         @sprites["entrytext"] = BitmapSprite.new(256,32,@viewport)
  366.         @sprites["entrytext"].y = @originY + 48
  367.  
  368.         refreshMenuText
  369.         if(@animationCounter == 0)
  370.             positionMenuEntries
  371.             transformIcon(@sprites[@entries[0].name], @activeIconScale, @activeIconTone, @activeIconOpacity)
  372.             @menu.pbHideMenu(false)
  373.         end    
  374.     end
  375.    
  376.     def addMenuEntry(entry)
  377.         @entries << entry
  378.         @sprites[entry.name] = IconSprite.new(0,0,@viewport)
  379.         @sprites[entry.name].visible = false
  380.         @sprites[entry.name].setBitmap(entry.icon)
  381.         @sprites[entry.name].tone = @inactiveIconTone
  382.         @sprites[entry.name].opacity = @inactiveIconOpacity
  383.     end
  384.    
  385.     def update
  386.         exit = false # should the menu-loop continue
  387.         if(Input.trigger?(Input::B))
  388.                 @menu.shouldExit = true
  389.                 return
  390.         end
  391.         if(@animationCounter > 0)
  392.             if(@startUpAnimation > 0)
  393.                 @distance = 24 + (@startUpAnimation - @animationCounter) * ((@finalDistance - 24) / @startUpAnimation)
  394.                 positionMenuEntries
  395.                 @menu.pbHideMenu(false) if(@startUpAnimation == @animationCounter) # If it's the first frame
  396.                 transformIcon(@sprites[@entries[0].name], @activeIconScale, @activeIconTone, @activeIconOpacity)
  397.                 @animationCounter -= 1
  398.                 @startUpAnimation = 0 if @animationCounter == 0
  399.             else
  400.                 updateAnimation
  401.                 refreshMenuText
  402.             end
  403.         else
  404.             if Input.trigger?(@buttonTurnCounterClockwise)
  405.                 startAnimation(1)
  406.             elsif Input.trigger?(@buttonTurnClockwise)
  407.                 startAnimation(-1)
  408.             elsif Input.trigger?(Input::C)
  409.                 exit = @entries[@currentSelection].selected(@menu) # trigger selected entry.               
  410.             end    
  411.         end
  412.         @menu.shouldExit = exit
  413.     end
  414.  
  415.    
  416.     # direction is either 1 (clockwise) or -1 (counterclockwise)
  417.     def startAnimation(direction)
  418.         @currentSelection =  (@currentSelection - direction) % @entries.length # keep selection within array bounds
  419.         @currentAngle = @startingAngle
  420.         @frameAngleShift = direction * @angleSize / @movementDuration # in radians
  421.         @frameScaleShift = ((@activeIconScale-1) / @movementDuration)
  422.         @animationCounter = @movementDuration
  423.         pbSEPlay(@soundTurning)
  424.     end
  425.    
  426.     def updateAnimation
  427.         @animationCounter -= 1
  428.         @entries.each do |entry|
  429.             entry.angle += @frameAngleShift
  430.             repositionSprite(@sprites[entry.name], entry.angle)
  431.         end
  432.         # Transition properties of selected/deselected entries
  433.         newActive = @sprites[@entries[@currentSelection].name]
  434.         if(@frameAngleShift > 0)
  435.             oldActive = @sprites[@entries[(@currentSelection + 1) % @entries.length].name]
  436.         else
  437.             oldActive = @sprites[@entries[(@currentSelection - 1) % @entries.length].name]
  438.         end
  439.         scaleNew = 1 + @frameScaleShift * (@movementDuration - @animationCounter)
  440.         scaleOld = 1 + @frameScaleShift * @animationCounter
  441.         transformIcon(newActive, scaleNew, @activeIconTone, @activeIconOpacity)
  442.         transformIcon(oldActive, scaleOld, @inactiveIconTone, @inactiveIconOpacity)
  443.     end
  444.    
  445.     def refreshMenuText
  446.         @sprites["entrytext"].bitmap.clear
  447.         text = @entries[@currentSelection].name
  448.         @sprites["entrytext"].x = @originX + 24 - text.length * 5.5 # rough guesstimate for centering
  449.         pbSetSystemFont(@sprites["entrytext"].bitmap)
  450.         pbDrawTextPositions(@sprites["entrytext"].bitmap,[[text,0,0,0,@textColor,@textHighlight]])
  451.     end
  452.        
  453.     def positionMenuEntries
  454.         @currentAngle = @startingAngle
  455.         @angleSize = (2*Math::PI) / @entries.length
  456.         @entries.each do |entry|
  457.             entry.angle = @currentAngle
  458.             repositionSprite(@sprites[entry.name], entry.angle)
  459.             @currentAngle += @angleSize        
  460.         end
  461.     end
  462.    
  463.     def repositionSprite(sprite, theta)
  464.         sprite.y = (@distance * Math.sin(theta)) + @originY
  465.         sprite.x = (@distance * Math.cos(theta)) + @originX
  466.     end
  467.    
  468.     def transformIcon(sprite, scale, tone, opacity)
  469.         width = sprite.bitmap.width
  470.         height = sprite.bitmap.height
  471.         sprite.zoom_x = scale
  472.         sprite.zoom_y = scale
  473.         sprite.x = sprite.x - (width*scale-width)/2        
  474.         sprite.y = sprite.y - (height*scale-height)/2  
  475.         sprite.tone = tone
  476.         sprite.opacity = opacity
  477.     end
  478.  
  479. end
  480.  
  481.  
  482. class PokemonPauseMenu_Scene
  483.     attr_accessor :shouldExit
  484.    
  485.     def initialize
  486.         #################### MISC CONFIGURATION SECTION
  487.         @soundOpenMenu = "GUI menu open" # When menu opens, play the following sound.
  488.         @backgroundTint = Tone.new(0,0,0,150) # Tone (shade) applied to the background/map.
  489.         @enableClock = true # enable the clock, for clock configuration, CTRL+F: "class Clock"
  490.         #################### END CONFIGURATION
  491.        
  492.         #### INTERNAL STUFF - changing values carelessly may break stuff
  493.         @background = Viewport.new(0,0,Graphics.width,Graphics.height)
  494.         @viewport = Viewport.new(0,0,Graphics.width,Graphics.height)
  495.         @entries = []
  496.         @sprites = {}
  497.         @shouldExit = false    
  498.     end
  499.  
  500.     def pbStartScene
  501.         @viewport.z = 99999
  502.         @background.z = 99998
  503.         @background.tone = @backgroundTint
  504.         @clock = (@enableClock) ? Clock.new(@viewport, @sprites, @timeFormat) : nil
  505.         @safari = SafariHud.new(@viewport, @sprites) if (pbInSafari?)
  506.         @safari = BugContestHud.new(@viewport, @sprites) if (pbInBugContest?)
  507.         @radialMenu = RadialMenu.new(@viewport, @sprites, self)
  508.     end
  509.    
  510.     def pbHideMenu(hide)
  511.         @sprites.each do |_,sprite|
  512.             sprite.visible = !hide
  513.         end
  514.     end
  515.    
  516.     def update
  517.         @hasTerminated = false
  518.         pbSEPlay(@soundOpenMenu)
  519.         loop do
  520.             Graphics.update
  521.             Input.update
  522.             @clock.update if @enableClock
  523.             @safari.update if defined?(@safari)
  524.             @radialMenu.update
  525.             if(@hasTerminated)
  526.                 return # If pbEndScene was already called, don't call it again.
  527.             end
  528.             pbUpdateSceneMap
  529.             if(shouldExit)
  530.                 pbEndScene
  531.                 break
  532.             end
  533.         end
  534.         pbUpdateSpriteHash(@sprites)
  535.     end
  536.    
  537.     def pbRefresh ; end
  538.     def pbShowMenu ; end
  539.    
  540.     def pbEndScene
  541.     @hasTerminated = true
  542.     @background.dispose
  543.     @radialMenu.dispose
  544.     @safari.dispose if defined?(@safari)
  545.     @clock.dispose if @clockEnabled
  546.     pbDisposeSpriteHash(@sprites)
  547.     @viewport.dispose
  548.     end
  549.    
  550.  
  551. end
  552.  
  553.  
  554. class PokemonPauseMenu
  555.    
  556.     def initialize(scene)
  557.         @scene = scene
  558.        
  559.     end
  560.    
  561.     def pbShowMenu
  562.         @scene.pbRefresh
  563.         @scene.pbShowMenu  
  564.     end
  565.    
  566.     def pbStartPokemonMenu
  567.         pbSetViableDexes # ?? called in original script
  568.         @scene.pbStartScene
  569.         @scene.update
  570.     end
  571. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement