/cui/label.d

http://github.com/wilkie/djehuty · D · 97 lines · 60 code · 24 blank · 13 comment · 4 complexity · f615d547d224aba3033adb1ca1e265b8 MD5 · raw file

  1. module cui.label;
  2. import djehuty;
  3. import io.console;
  4. import cui.widget;
  5. // Section: Console
  6. // Description: This console control abstracts a simple static text field.
  7. class CuiLabel : CuiWidget {
  8. this( uint x, uint y, uint width, string text,
  9. Color fgclr = Color.Blue,
  10. Color bgclr = Color.Black ) {
  11. super(x,y,width,1);
  12. _forecolor = fgclr;
  13. _backcolor = bgclr;
  14. _value = text.dup;
  15. }
  16. override void onAdd() {
  17. }
  18. override void onInit() {
  19. }
  20. // Properties
  21. // Description: this peroperty sets the value of the field
  22. // newValue: the new value of the field
  23. void text(string newValue) {
  24. _value = newValue.dup;
  25. onDraw();
  26. }
  27. // Description: this property returns the value of the field
  28. string text() {
  29. return _value.dup;
  30. }
  31. // Description: this property sets the foreground color of the field
  32. // fgclr: the color to set the foreground to
  33. void forecolor(Color fgclr) {
  34. _forecolor = fgclr;
  35. onDraw();
  36. }
  37. // Description: this property returns the foreground color of the field
  38. Color forecolor() {
  39. return _forecolor;
  40. }
  41. // Description: this property sets the background color of the field
  42. // bgclr: the color to set the background to
  43. void backcolor(Color bgclr) {
  44. _backcolor = bgclr;
  45. onDraw();
  46. }
  47. // Description: this property returns the background color of the field
  48. Color backcolor() {
  49. return _backcolor;
  50. }
  51. override void onDraw() {
  52. if (canDraw) {
  53. Console.position(0, 0);
  54. Console.forecolor = _forecolor;
  55. Console.backcolor = _backcolor;
  56. // draw as much as we can
  57. if (_value.length > this.width) {
  58. Console.put((_value[0..width]));
  59. }
  60. else {
  61. Console.put(_value);
  62. for (uint i; i < this.width - _value.length; i++) {
  63. Console.put(" ");
  64. }
  65. }
  66. }
  67. }
  68. protected:
  69. private:
  70. Color _forecolor = Color.Blue;
  71. Color _backcolor = Color.Black;
  72. string _value;
  73. }