ScaleMode2D.hx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import hxd.Key;
  2. import h2d.Scene;
  3. class ScaleMode2D extends SampleApp {
  4. override function init()
  5. {
  6. var bg = new h2d.Bitmap(h2d.Tile.fromColor(0x333333), s2d);
  7. var minBg = new h2d.Bitmap(h2d.Tile.fromColor(0x222222), s2d);
  8. super.init();
  9. var mode:Int = 0;
  10. // Width and height used in Stretch, LetterBox, Fixed and AutoZoom
  11. var width:Int = 320;
  12. var height:Int = 240;
  13. // Zoom used in Fixed and Zoom
  14. var zoom:Float = 1;
  15. // Integer Scale used in LetterBox and AutoZoom
  16. var intScale:Bool = false;
  17. // Vertical and Horizontal Align used in LetterBox and Fixed.
  18. var halign:ScaleModeAlign = Center;
  19. var valign:ScaleModeAlign = Center;
  20. var sceneInfo:h2d.Text = null;
  21. function setMode()
  22. {
  23. switch ( mode ) {
  24. case 0:
  25. s2d.scaleMode = Resize;
  26. case 1:
  27. s2d.scaleMode = Stretch(width, height);
  28. case 2:
  29. s2d.scaleMode = LetterBox(width, height, intScale, halign, valign);
  30. case 3:
  31. s2d.scaleMode = Fixed(width, height, zoom, valign, halign);
  32. case 4:
  33. s2d.scaleMode = Zoom(zoom);
  34. case 5:
  35. s2d.scaleMode = AutoZoom(width, height, intScale);
  36. }
  37. minBg.scaleX = width;
  38. minBg.scaleY = height;
  39. bg.scaleX = s2d.width;
  40. bg.scaleY = s2d.height;
  41. sceneInfo.text = "Scene size: " + s2d.width + "x" + s2d.height;
  42. }
  43. addText("Press R to set ScaleMode to Resize");
  44. addChoice("ScaleMode", ScaleMode.getConstructors(), function(idx) { mode = idx; }, 0);
  45. addSlider("width", function() { return width; }, function(v) { width = Std.int(v); }, 0, 800);
  46. addSlider("height", function() { return height; }, function(v) { height = Std.int(v); }, 0, 600);
  47. addSlider("zoom", function() { return zoom; }, function(v) { zoom = v; }, 0.01, 5);
  48. addCheck("integerScale", function() { return intScale; }, function(v) { intScale = v; });
  49. addChoice("HAlign", ["Left", "Center", "Right"], function(v) { halign = [Left, Center, Right][v]; }, 1 );
  50. addChoice("VAlign", ["Top", "Center", "Bottom"], function(v) { valign = [Top, Center, Bottom][v]; }, 1 );
  51. addButton("Apply", setMode);
  52. sceneInfo = addText("");
  53. addText("Light-grey: Actual Scene width and height");
  54. addText("Dark-grey: Parameter-specified width and height");
  55. setMode();
  56. }
  57. override function update(dt:Float)
  58. {
  59. if (Key.isReleased(Key.R)) s2d.scaleMode = Resize;
  60. }
  61. static function main() {
  62. hxd.Res.initEmbed();
  63. new ScaleMode2D();
  64. }
  65. }