Advertisement
TheSixth

Picture Number Drawing by Sixth

Sep 2nd, 2015
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 13.81 KB | None | 0 0
  1. #===============================================================================
  2. # * [ACE] Picture Number Drawing
  3. #===============================================================================
  4. # * Made by: Sixth (www.rpgmakervxace.net, www.forums.rpgmakerweb.com)
  5. # * Version: 1.4
  6. # * Updated: 27/11/2017
  7. # * Requires: -------
  8. #-------------------------------------------------------------------------------
  9. # * < Change Log >
  10. #-------------------------------------------------------------------------------
  11. # * Version 1.0 (11/04/2015)
  12. #   - Initial release.
  13. # * Version 1.1 (13/06/2015)
  14. #   - Added in a safe-check in case an unidentified character is detected.
  15. #   - Added a custom folder setting. From now on, all pictures used by this
  16. #     script must be in the folder set up for them!
  17. # * Version 1.2 (22/06/2015)
  18. #   - Actually added in the safe-check method mentioned in v1.1.
  19. #   - Added an optional argument which will clear the previously drawn numbers
  20. #     automatically if the number is redrawn at the same place.
  21. # * Version 1.3 (07/10/2016)
  22. #   - Just added 2 new methods for getting one number's width and height.
  23. #     It is not necessary to update if you don't need that method.
  24. # * Version 1.4 (27/11/2017)
  25. #   - From now on, you can setup different number of styles (rows) and number of
  26. #     characters (columns) for each of your image sheets.
  27. #-------------------------------------------------------------------------------
  28. # * < Description >
  29. #-------------------------------------------------------------------------------
  30. # * This is a scripter's tool, so it won't do anything by itself!
  31. # * This script adds a new method for Bitmap objects.
  32. #   This new method enables the drawing of numbers with pictures.
  33. # * Besides numbers, any other character can be added to the image sheet.
  34. #   These extra characters can be drawn with pictures too.
  35. #-------------------------------------------------------------------------------
  36. # * < Usage Information >
  37. #-------------------------------------------------------------------------------
  38. # You can use the new picture drawing method for numbers by calling the
  39. # following on a bitmap object:
  40. #
  41. #   draw_numbers(x,y,num,opa,al,clear?)
  42. #
  43. # Arguments:
  44. #
  45. #   x = The X position to draw the number on the bitmap.
  46. #   y = The Y position to draw the number on the bitmap.
  47. #   num = The number to be drawn. It supports negative and float numbers too,
  48. #         as long as you define an extra char at the settings below for the
  49. #         minus and point signs.
  50. #   opa = The opacity level of the drawing. If omitted, 255 will be used
  51. #         automatically. Valid values: 0 - 255.
  52. #   al = The alignment used for the drawing.
  53. #        It works a bit different, not like draw_text's alignment, but it will
  54. #        do the alignment just fine.
  55. #        You can choose from 3 values here:
  56. #        0 = Left alignment, 1 = Middle alignment, 2 = Right alignment.
  57. #        Depending on which alignment mode you choose, the X co-ord used in the
  58. #        method call will mean different things:
  59. #        0 - The X co-ord defines the starting point of the drawing.
  60. #        1 - The X co-ord defines the center of the drawing.
  61. #        2 - The X co-ord defines the end of the drawing.
  62. #        If this setting is omitted, 0 will be used by default.
  63. #        Check the example scene to see the differences in practice!
  64. #   clear? = This is either true or false. If true, any previously drawn number
  65. #            at the same place will get cleared automatically. If false, there
  66. #            will be no automatic clearing, so you will need to do it manually!
  67. #            If omitted, this argument defaults to false!
  68. #
  69. # You can change the default drawing style (the default image file and row used)
  70. # too if you want. More info on that can be found below in the next section.
  71. #
  72. # Examples:
  73. #  
  74. #   contents.draw_numbers(10,30,$game_actors[1].hp)
  75. # Draws the HP of the actor with ID 1, using the default image file from the
  76. # Pictures folder, and will use the default row on the picture.
  77. # This example is executed in a Window class.
  78. #
  79. #   @example = Sprite.new
  80. #   @example.bitmap = Cache.picture("examplepic")
  81. #   @example.bitmap.num_style = ["numpics2",3]
  82. #   @example.bitmap.draw_numbers(10,14,$game_party.gold,180,2)
  83. # Draws the party's gold on the @example sprite's bitmap, uses the "numpics2"
  84. # image file from the Pictures folder, uses the third row on the "numpics2"
  85. # picture, and draws the numbers with an opacity level of 180.
  86. # The drawing will use right alignment.
  87. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  88. # You can change the bitmap's picture used for drawing the numbers by setting
  89. # it's "style" directly like this:
  90. #
  91. #   bitmap_object.num_style = ["pic",row]
  92. #
  93. #   pic = The name of the image used for the drawing.
  94. #         It must contain at least all 10 numbers, from 0 to 9.
  95. #         The image itself is a sheet of number pictures lined up.
  96. #         Each row equals a new style for the number drawing method.
  97. #         The numbers on the sheet must start from 0 and they increment from
  98. #         the left to the right one by one.
  99. #         If you need special characters, such as a - sign for negative numbers,
  100. #         or a . for float numbers, you must put them after the number pictures!
  101. #   row = The row used from the image. You can create more styles on the same
  102. #         image if you want, by simply inserting another sheet below the last
  103. #         one. You can than change the styles used dynamically with the
  104. #         draw_numbers method easily.
  105. #         Useful if you want to pack all of your number drawing styles into
  106. #         one single image file.
  107. #
  108. # So, this sets a new "style" for the number drawing method, just like you would
  109. # set a new font type/size/etc for a regular draw_text method before drawing
  110. # your stuffs if needed. Take a look at the example scene to see an example!
  111. #-------------------------------------------------------------------------------
  112. # * < Installation >
  113. #-------------------------------------------------------------------------------
  114. # * Place this script below Materials but above Main!
  115. #-------------------------------------------------------------------------------
  116. # * < Compatibility Info >
  117. #-------------------------------------------------------------------------------
  118. # * No known incompatibilities.
  119. #-------------------------------------------------------------------------------
  120. # * < Known Issues >
  121. #-------------------------------------------------------------------------------
  122. # * No known issues.
  123. #-------------------------------------------------------------------------------
  124. # * < Terms of Use >
  125. #-------------------------------------------------------------------------------
  126. # * Free to use for whatever purposes you want.
  127. # * Credit me (Sixth) in your game, pretty please! :P
  128. # * Posting modified versions of this script is allowed as long as you notice me
  129. #   about it with a link to it!
  130. #===============================================================================
  131. $imported = {} if $imported.nil?
  132. $imported["SixthPictureNumbers"] = true
  133. #===============================================================================
  134. # Settings:
  135. #===============================================================================
  136. module PicNumbers # <-- No touchy-touchy!
  137.   #-----------------------------------------------------------------------------
  138.   # Image Folder Settings:
  139.   #-----------------------------------------------------------------------------
  140.   # Set up a custom folder where your number pictures will be read from.
  141.   # All pictures used must be in this folder!
  142.   #-----------------------------------------------------------------------------
  143.   ImgFolder = "Graphics/Numbers/"
  144.    
  145.   #-----------------------------------------------------------------------------
  146.   # Image Sheet Settings:
  147.   #-----------------------------------------------------------------------------
  148.   # Set how many rows and columns are on your image files used for the
  149.   # number drawing methods.
  150.   # From v1.4, you can use different rows and columns for your different
  151.   # image-sheets!
  152.   # The rows represent the number of available styles on the image,
  153.   # while the columns represent the number of characters in one style
  154.   # including the extra characters set up in the next section.
  155.   #
  156.   # Example format:
  157.   #
  158.   # ---------------------------------------------------------
  159.   # | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | + | - | . | , | <- Style 1 (row 0)
  160.   # ---------------------------------------------------------
  161.   # | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | + | - | . | , | <- Style 2 (row 1)
  162.   # ---------------------------------------------------------
  163.   # | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | + | - | . | , | <- Style 3 (row 2)
  164.   # ---------------------------------------------------------
  165.   #
  166.   # This example image setup contains 3 styles. Each row is a style.
  167.   # Changing the 'row' value in the num_style property of a bitmap will change  
  168.   # the style used for the drawing.
  169.   # The first row's index is 0, the second row's index is 1, and so on.
  170.   # The example image got 4 extra characters in it (+/-/./,).
  171.   # The width of each section must be the same for all of them!
  172.   # The height of each section must be the same for all of them!
  173.   # The width and height used can differ for each image, but for optimal  
  174.   # drawing, keep the characters close to each other horizontally.
  175.   #
  176.   # And the setting's format here looks like this:
  177.   #
  178.   #   "filename" => [rows,columns],
  179.   #
  180.   # The "filename" must be the file name of an image-sheet used for this script.
  181.   #
  182.   # The rows and columns in the arrays must be positive integer numbers, and
  183.   # they will setup the number of styles (rows) and characters (columns) for
  184.   # the specified image-sheet.
  185.   #
  186.   # You can add as many as you like!
  187.   #
  188.   # If an image-sheet got no setting here, it will automatically use the
  189.   # "default" settings!
  190.   #-----------------------------------------------------------------------------
  191.   Formats = {
  192.     "default"      => [3,17], # <-- Default, do NOT remove!
  193.     "numsheet1a"   => [5,17],
  194.     "numsheet_s24" => [6,17],
  195.     "numsheet_big" => [3,17],
  196.     "pages1a"      => [1,17],
  197.     "menu1a"       => [3,18],
  198.     # <-- Add more settings here if needed!
  199.   }
  200.  
  201.   #-----------------------------------------------------------------------------
  202.   # Default Drawing Style Settings:
  203.   #-----------------------------------------------------------------------------
  204.   # If a bitmap got no style settings set up for drawing the numbers, than this
  205.   # style will be used by default.
  206.   #
  207.   # Format:
  208.   #
  209.   #   DefStyle = ["image file name", row]
  210.   #-----------------------------------------------------------------------------
  211.   DefStyle = ["pacg_nums",0]
  212.  
  213.   #-----------------------------------------------------------------------------
  214.   # Extra Character Settings:
  215.   #-----------------------------------------------------------------------------
  216.   # The extra character setup.
  217.   # For negative numbers, you must define the "minus" ( - ) sign!
  218.   # For float numbers, you must define the "point" ( . ) sign!
  219.   # You can also add any other characters if you want.
  220.   #
  221.   # Format:
  222.   #
  223.   #   "char name" => column index,
  224.   #
  225.   # The "char name" is the character itself.
  226.   # The column index is the column reserved for the character on the sheet.
  227.   # The first column's index is 0, the second column's index is 1, and so on.
  228.   # Valid values for the column index start from 10 and goes to infinity.
  229.   # The first 10 columns are reserved for the numbers, that is why you
  230.   # can NOT use any numbers below 10 here!
  231.   #
  232.   # If the number drawing method detects an undefined character which is not a
  233.   # number, it will draw an empty space instead and jumps to the next one!
  234.   #-----------------------------------------------------------------------------
  235.   ExtraChars = { # <-- No touchy-touchy!
  236.     "+" => 10,
  237.     "-" => 11,
  238.     "." => 12,
  239.     "," => 13,
  240.     "/" => 14,
  241.     "%" => 15,
  242.     ":" => 16,
  243.     "d" => 17,
  244.     # <-- Add more extra characters here!
  245.   } # <-- No touchy-touchy!
  246.  
  247. end # <-- No touchy-touchy!
  248. #===============================================================================
  249. # End of settings! O.o
  250. #===============================================================================
  251.  
  252. module Cache
  253.  
  254.   def self.picnums(filename)
  255.     load_bitmap(PicNumbers::ImgFolder, filename)
  256.   end
  257.  
  258. end
  259.  
  260. class Bitmap
  261.   attr_accessor :num_style  
  262.  
  263.   def self_num_style
  264.     @num_style.nil? ? PicNumbers::DefStyle : @num_style
  265.   end
  266.    
  267.   def num_form
  268.     if PicNumbers::Formats[self_num_style[0]]
  269.       return PicNumbers::Formats[self_num_style[0]]
  270.     else
  271.       return PicNumbers::Formats["default"]
  272.     end
  273.   end
  274.  
  275.   def draw_numbers(x,y,num,opa=255,al=0,clear=false)
  276.     img = Cache.picnums(self_num_style[0])
  277.     row = self_num_style[1]
  278.     form = num_form
  279.     wm = img.width/form[1]
  280.     hm = img.height/form[0]
  281.     numtxt = num.is_a?(String) ? num : num.to_s
  282.     case al
  283.     when 0
  284.       xx = x
  285.     when 1
  286.       xx = x-(numtxt.size*wm/2)
  287.     when 2
  288.       xx = x-numtxt.size*wm
  289.     end
  290.     index = 0
  291.     numtxt.each_char do |nm|
  292.       if PicNumbers::ExtraChars.include?(nm)
  293.         mul = PicNumbers::ExtraChars[nm]
  294.         rect = Rect.new(mul*wm, row*hm, wm, hm)
  295.       elsif numtxt[index,1].to_i.to_s == nm
  296.         rect = Rect.new(nm.to_i*wm, row*hm, wm, hm)
  297.       end
  298.       self.clear_rect(Rect.new(xx+wm*index,y,wm,hm)) if clear == true
  299.       self.blt(xx+wm*index, y, img, rect, opa) if rect
  300.       index += 1
  301.       rect = nil
  302.     end
  303.   end
  304.  
  305. end
  306. #==============================================================================
  307. # !!END OF SCRIPT - OHH, NOES!!
  308. #==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement