bookmenu.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. class BookMenu extends UIScriptedMenu
  2. {
  3. protected TextWidget m_author;
  4. protected TextWidget m_title;
  5. protected TextWidget m_page;
  6. protected HtmlWidget m_content;
  7. protected float m_page_height;
  8. protected float m_content_total_height;
  9. protected float m_content_pos;
  10. override Widget Init()
  11. {
  12. layoutRoot = GetGame().GetWorkspace().CreateWidgets("gui/layouts/day_z_book.layout");
  13. Class.CastTo(m_content, layoutRoot.FindAnyWidget("HtmlWidget"));
  14. Class.CastTo(m_author, layoutRoot.FindAnyWidget("Author"));
  15. Class.CastTo(m_title, layoutRoot.FindAnyWidget("Title"));
  16. Class.CastTo(m_page, layoutRoot.FindAnyWidget("Page"));
  17. float width;
  18. m_content.GetScreenSize(width, m_page_height);
  19. return layoutRoot;
  20. }
  21. void ReadBook(InventoryItem book)
  22. {
  23. m_content.LoadFile( book.ConfigGetString("file") );
  24. m_author.SetText( book.ConfigGetString("author") );
  25. m_title.SetText( book.ConfigGetString("title") );
  26. m_content_total_height = m_content.GetContentHeight();
  27. m_content_pos = 0;
  28. NextPrevPage(false);
  29. }
  30. override bool OnClick(Widget w, int x, int y, int button)
  31. {
  32. super.OnClick(w, x, y, button);
  33. switch (w.GetUserID())
  34. {
  35. case IDC_BOOK_VIEWER_PREV:
  36. NextPrevPage(false);
  37. return true;
  38. case IDC_BOOK_VIEWER_NEXT:
  39. NextPrevPage(true);
  40. return true;
  41. case IDC_CANCEL:
  42. Close();
  43. return true;
  44. }
  45. return false;
  46. }
  47. void NextPrevPage(bool next)
  48. {
  49. if (next)
  50. {
  51. m_content_pos = m_content_pos + m_page_height;
  52. }
  53. else
  54. {
  55. m_content_pos = m_content_pos - m_page_height;
  56. }
  57. float maxOffset = 0;
  58. if (m_content_total_height > m_page_height)
  59. {
  60. maxOffset = m_content_total_height - m_page_height;
  61. }
  62. if (m_content_pos < 0)
  63. {
  64. m_content_pos = 0;
  65. }
  66. if (m_content_pos > maxOffset)
  67. {
  68. m_content_pos = maxOffset;
  69. }
  70. m_content.SetContentOffset(m_content_pos, true);
  71. float pagesTotal = Math.Ceil(m_content_total_height / m_page_height);
  72. float currPage = Math.Round(m_content_pos / m_page_height) + 1;
  73. m_page.SetText( currPage.ToString() + " / " + pagesTotal.ToString() );
  74. }
  75. }