text_font_select.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [text] example - Font selector
  4. --
  5. -- This example has been created using raylib 1.6 (www.raylib.com)
  6. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. --
  8. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  9. --
  10. -------------------------------------------------------------------------------------------
  11. -- Initialization
  12. -------------------------------------------------------------------------------------------
  13. local screenWidth = 800
  14. local screenHeight = 450
  15. InitWindow(screenWidth, screenHeight, "raylib [text] example - font selector")
  16. -- NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  17. local fonts = {} -- SpriteFont array
  18. fonts[1] = LoadSpriteFont("resources/fonts/alagard.rbmf") -- SpriteFont loading
  19. fonts[2] = LoadSpriteFont("resources/fonts/pixelplay.rbmf") -- SpriteFont loading
  20. fonts[3] = LoadSpriteFont("resources/fonts/mecha.rbmf") -- SpriteFont loading
  21. fonts[4] = LoadSpriteFont("resources/fonts/setback.rbmf") -- SpriteFont loading
  22. fonts[5] = LoadSpriteFont("resources/fonts/romulus.rbmf") -- SpriteFont loading
  23. fonts[6] = LoadSpriteFont("resources/fonts/pixantiqua.rbmf") -- SpriteFont loading
  24. fonts[7] = LoadSpriteFont("resources/fonts/alpha_beta.rbmf") -- SpriteFont loading
  25. fonts[8] = LoadSpriteFont("resources/fonts/jupiter_crash.rbmf") -- SpriteFont loading
  26. local currentFont = 1 -- Selected font
  27. local colors = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD, RED }
  28. local fontNames = { "[1] Alagard", "[2] PixelPlay", "[3] MECHA", "[4] Setback",
  29. "[5] Romulus", "[6] PixAntiqua", "[7] Alpha Beta", "[8] Jupiter Crash" }
  30. local text = "THIS is THE FONT you SELECTED!" -- Main text
  31. local textSize = MeasureTextEx(fonts[currentFont], text, fonts[currentFont].size*3, 1)
  32. local mousePoint
  33. local btnNextOutColor = DARKBLUE -- Button color (outside line)
  34. local btnNextInColor = SKYBLUE -- Button color (inside)
  35. local framesCounter = 0 -- Useful to count frames button is 'active' = clicked
  36. local positionY = 180 -- Text selector and button Y position
  37. local btnNextRec = Rectangle(673, positionY, 109, 44) -- Button rectangle (useful for collision)
  38. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  39. -------------------------------------------------------------------------------------------
  40. -- Main game loop
  41. while not WindowShouldClose() do -- Detect window close button or ESC key
  42. -- Update
  43. ---------------------------------------------------------------------------------------
  44. -- Keyboard-based font selection (easy)
  45. if (IsKeyPressed(KEY.RIGHT)) then
  46. if (currentFont < 8) then currentFont = currentFont + 1 end
  47. end
  48. if (IsKeyPressed(KEY.LEFT)) then
  49. if (currentFont > 1) then currentFont = currentFont - 1 end
  50. end
  51. if (IsKeyPressed(KEY.ZERO)) then currentFont = 0
  52. elseif (IsKeyPressed(KEY.ONE)) then currentFont = 1
  53. elseif (IsKeyPressed(KEY.TWO)) then currentFont = 2
  54. elseif (IsKeyPressed(KEY.THREE)) then currentFont = 3
  55. elseif (IsKeyPressed(KEY.FOUR)) then currentFont = 4
  56. elseif (IsKeyPressed(KEY.FIVE)) then currentFont = 5
  57. elseif (IsKeyPressed(KEY.SIX)) then currentFont = 6
  58. elseif (IsKeyPressed(KEY.SEVEN)) then currentFont = 7
  59. end
  60. -- Mouse-based font selection (NEXT button logic)
  61. mousePoint = GetMousePosition()
  62. if (CheckCollisionPointRec(mousePoint, btnNextRec)) then
  63. -- Mouse hover button logic
  64. if (framesCounter == 0) then
  65. btnNextOutColor = DARKPURPLE
  66. btnNextInColor = PURPLE
  67. end
  68. if (IsMouseButtonDown(MOUSE.LEFT_BUTTON)) then
  69. framesCounter = 20 -- Frames button is 'active'
  70. btnNextOutColor = MAROON
  71. btnNextInColor = RED
  72. end
  73. else
  74. -- Mouse not hover button
  75. btnNextOutColor = DARKBLUE
  76. btnNextInColor = SKYBLUE
  77. end
  78. if (framesCounter > 0) then framesCounter = framesCounter - 1 end
  79. if (framesCounter == 1) then -- We change font on frame 1
  80. currentFont = currentFont + 1
  81. if (currentFont > 7) then currentFont = 0 end
  82. end
  83. -- Text measurement for better positioning on screen
  84. textSize = MeasureTextEx(fonts[currentFont], text, fonts[currentFont].size*3, 1)
  85. ---------------------------------------------------------------------------------------
  86. -- Draw
  87. ---------------------------------------------------------------------------------------
  88. BeginDrawing()
  89. ClearBackground(RAYWHITE)
  90. DrawText("font selector - use arroys, button or numbers", 160, 80, 20, DARKGRAY)
  91. DrawLine(120, 120, 680, 120, DARKGRAY)
  92. DrawRectangle(18, positionY, 644, 44, DARKGRAY)
  93. DrawRectangle(20, positionY + 2, 640, 40, LIGHTGRAY)
  94. DrawText(fontNames[currentFont], 30, positionY + 13, 20, BLACK)
  95. DrawText("< >", 610, positionY + 8, 30, BLACK)
  96. DrawRectangleRec(btnNextRec, btnNextOutColor)
  97. DrawRectangle(675, positionY + 2, 105, 40, btnNextInColor)
  98. DrawText("NEXT", 700, positionY + 13, 20, btnNextOutColor)
  99. DrawTextEx(fonts[currentFont], text, Vector2(screenWidth/2 - textSize.x/2,
  100. 260 + (70 - textSize.y)/2), fonts[currentFont].size*3,
  101. 1, colors[currentFont])
  102. EndDrawing()
  103. ---------------------------------------------------------------------------------------
  104. end
  105. -- De-Initialization
  106. -------------------------------------------------------------------------------------------
  107. for i = 1, 8 do UnloadSpriteFont(fonts[i]) end -- SpriteFont(s) unloading
  108. CloseWindow() -- Close window and OpenGL context
  109. -------------------------------------------------------------------------------------------