tb_dimension.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_DIMENSION_H
  6. #define TB_DIMENSION_H
  7. #include "tb_types.h"
  8. #include "tb_debug.h"
  9. #include "tb_str.h"
  10. namespace tb {
  11. /** Dimensions <= this value will be untouched by conversion in TBDimensionConverter.
  12. To preserve special constants, those must be <= this value. */
  13. #define TB_INVALID_DIMENSION -5555
  14. class TBTempBuffer;
  15. class TBValue;
  16. /** TBDimensionConverter converts device independant points
  17. to pixels, based on two DPI values.
  18. Dimensions in Turbo Badger are normally in pixels (if not specified differently)
  19. and conversion normally take place when loading skin. */
  20. class TBDimensionConverter
  21. {
  22. int m_src_dpi; ///< The source DPI (Normally the base_dpi from skin).
  23. int m_dst_dpi; ///< The destination DPI (Normally the supported skin DPI nearest to TBSystem::GetDPI).
  24. TBStr m_dst_dpi_str; ///< The file suffix that should be used to load bitmaps in destinatin DPI.
  25. public:
  26. TBDimensionConverter() : m_src_dpi(100), m_dst_dpi(100) {}
  27. /** Set the source and destination DPI that will affect the conversion. */
  28. void SetDPI(int src_dpi, int dst_dpi);
  29. /** Get the source DPI. */
  30. int GetSrcDPI() const { return m_src_dpi; }
  31. /** Get the destination DPI. */
  32. int GetDstDPI() const { return m_dst_dpi; }
  33. /** Get the file name suffix that should be used to load bitmaps in the destination DPI.
  34. Examples: "@96", "@196" */
  35. const char *GetDstDPIStr() const { return m_dst_dpi_str; }
  36. /** Get the file name with destination DPI suffix (F.ex "foo.png" becomes "[email protected]").
  37. The temp buffer will contain the resulting file name. */
  38. void GetDstDPIFilename(const char *filename, TBTempBuffer *tempbuf) const;
  39. /** Return true if the source and destinatin DPI are different. */
  40. bool NeedConversion() const { return m_src_dpi != m_dst_dpi; }
  41. /** Convert device independant point to pixel. */
  42. int DpToPx(int dp) const;
  43. /** Convert millimeter to pixel. */
  44. int MmToPx(int mm) const;
  45. /** Get a pixel value from string in any of the following formats:
  46. str may be nullptr. def_value is returned on fail.
  47. Device independent point: "1", "1dp"
  48. Pixel value: "1px"
  49. */
  50. int GetPxFromString(const char *str, int def_value) const;
  51. /** Get a pixel value from TBValue.
  52. value may be nullptr. def_value is returned on fail.
  53. Number formats are treated as dp.
  54. String format is treated like for GetPxFromString.
  55. */
  56. int GetPxFromValue(TBValue *value, int def_value) const;
  57. };
  58. }; // namespace tb
  59. #endif // TB_DIMENSION_H